-- Copyright (c) 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree.


{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}

module Duckling.AmountOfMoney.Rules
  ( rules
  ) where

import Data.HashMap.Strict (HashMap)
import Data.Maybe
import Data.String
import Data.Text (Text)
import Prelude
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text

import Duckling.AmountOfMoney.Helpers
import Duckling.AmountOfMoney.Types (Currency(..), AmountOfMoneyData (..))
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers (isPositive)
import Duckling.Numeral.Types (NumeralData (..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.AmountOfMoney.Types as TAmountOfMoney
import qualified Duckling.Numeral.Types as TNumeral

currencies :: HashMap Text Currency
currencies :: HashMap Text Currency
currencies = [(Text, Currency)] -> HashMap Text Currency
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ (Text
"aed", Currency
AED)
  , (Text
"aud", Currency
AUD)
  , (Text
"bgn", Currency
BGN)
  , (Text
"brl", Currency
BRL)
  , (Text
"byn", Currency
BYN)
  , (Text
"cad", Currency
CAD)
  , (Text
"¢", Currency
Cent)
  , (Text
"c", Currency
Cent)
  , (Text
"chf", Currency
CHF)
  , (Text
"cny", Currency
CNY)
  , (Text
"czk", Currency
CZK)
  , (Text
"rmb", Currency
CNY)
  , (Text
"yuan", Currency
CNY)
  , (Text
"$", Currency
Dollar)
  , (Text
"dinar", Currency
Dinar)
  , (Text
"dinars", Currency
Dinar)
  , (Text
"dkk", Currency
DKK)
  , (Text
"dollar", Currency
Dollar)
  , (Text
"dollars", Currency
Dollar)
  , (Text
"egp", Currency
EGP)
  , (Text
"€", Currency
EUR)
  , (Text
"eur", Currency
EUR)
  , (Text
"euro", Currency
EUR)
  , (Text
"euros", Currency
EUR)
  , (Text
"eurs", Currency
EUR)
  , (Text
"€ur", Currency
EUR)
  , (Text
"€uro", Currency
EUR)
  , (Text
"€uros", Currency
EUR)
  , (Text
"€urs", Currency
EUR)
  , (Text
"gbp", Currency
GBP)
  , (Text
"gel", Currency
GEL)
  , (Text
"hkd", Currency
HKD)
  , (Text
"hrk", Currency
HRK)
  , (Text
"idr", Currency
IDR)
  , (Text
"ils", Currency
ILS)
  , (Text
"₪", Currency
ILS)
  , (Text
"nis", Currency
ILS)
  , (Text
"inr", Currency
INR)
  , (Text
"iqd", Currency
IQD)
  , (Text
"rs", Currency
INR)
  , (Text
"rs.", Currency
INR)
  , (Text
"rupee", Currency
INR)
  , (Text
"rupees", Currency
INR)
  , (Text
"jmd", Currency
JMD)
  , (Text
"jod", Currency
JOD)
  , (Text
"¥", Currency
JPY)
  , (Text
"jpy", Currency
JPY)
  , (Text
"lari", Currency
GEL)
  , (Text
"\x20BE", Currency
GEL)
  , (Text
"yen", Currency
JPY)
  , (Text
"krw", Currency
KRW)
  , (Text
"kwd", Currency
KWD)
  , (Text
"lbp", Currency
LBP)
  , (Text
"mad", Currency
MAD)
  , (Text
"mnt", Currency
MNT)
  , (Text
"myr", Currency
MYR)
  , (Text
"rm", Currency
MYR)
  , (Text
"₮", Currency
MNT)
  , (Text
"tugrik", Currency
MNT)
  , (Text
"tugriks", Currency
MNT)
  , (Text
"nok", Currency
NOK)
  , (Text
"nzd", Currency
NZD)
  , (Text
"pkr", Currency
PKR)
  , (Text
"pln", Currency
PLN)
  , (Text
"£", Currency
Pound)
  , (Text
"pt", Currency
PTS)
  , (Text
"pta", Currency
PTS)
  , (Text
"ptas", Currency
PTS)
  , (Text
"pts", Currency
PTS)
  , (Text
"qar", Currency
QAR)
  , (Text
"₽", Currency
RUB)
  , (Text
"rial", Currency
Rial)
  , (Text
"rials", Currency
Rial)
  , (Text
"riyal", Currency
Riyal)
  , (Text
"riyals", Currency
Riyal)
  , (Text
"ron", Currency
RON)
  , (Text
"rub", Currency
RUB)
  , (Text
"sar", Currency
SAR)
  , (Text
"sek", Currency
SEK)
  , (Text
"sgd", Currency
SGD)
  , (Text
"shekel", Currency
ILS)
  , (Text
"shekels", Currency
ILS)
  , (Text
"thb", Currency
THB)
  , (Text
"ttd", Currency
TTD)
  , (Text
"₴", Currency
UAH)
  , (Text
"uah", Currency
UAH)
  , (Text
"usd", Currency
USD)
  , (Text
"us$", Currency
USD)
  , (Text
"vnd", Currency
VND)
  , (Text
"zar", Currency
ZAR)
  , (Text
"tl", Currency
TRY)
  , (Text
"lira", Currency
TRY)
  , (Text
"₺", Currency
TRY)
  ]

ruleCurrencies :: Rule
ruleCurrencies :: Rule
ruleCurrencies = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"currencies"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(aed|aud|bgn|brl|byn|¢|cad|chf|cny|c|\\$|dinars?|dkk|dollars?|egp|(e|€)uro?s?|€|gbp|gel|\x20BE|hkd|hrk|idr|ils|₪|inr|iqd|jmd|jod|¥|jpy|lari|krw|kwd|lbp|mad|₮|mnt|tugriks?|myr|rm|nis|nok|nzd|£|pkr|pln|pta?s?|qar|₽|rs\\.?|riy?als?|ron|rub|rupees?|sar|sek|sgb|shekels?|thb|ttd|₴|uah|us(d|\\$)|vnd|yen|yuan|zar|tl|lira|₺)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) -> do
        Currency
c <- Text -> HashMap Text Currency -> Maybe Currency
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Currency
currencies
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (AmountOfMoneyData -> Token) -> AmountOfMoneyData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension AmountOfMoneyData -> AmountOfMoneyData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension AmountOfMoneyData
AmountOfMoney (AmountOfMoneyData -> Maybe Token)
-> AmountOfMoneyData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Currency -> AmountOfMoneyData
currencyOnly Currency
c
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleAmountUnit :: Rule
ruleAmountUnit :: Rule
ruleAmountUnit = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<amount> <unit>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , Predicate -> PatternItem
Predicate Predicate
isCurrencyOnly
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = v}:
       Token Dimension a
AmountOfMoney AmountOfMoneyData{TAmountOfMoney.currency = c}:
       [Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (AmountOfMoneyData -> Token) -> AmountOfMoneyData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension AmountOfMoneyData -> AmountOfMoneyData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension AmountOfMoneyData
AmountOfMoney (AmountOfMoneyData -> Token)
-> (AmountOfMoneyData -> AmountOfMoneyData)
-> AmountOfMoneyData
-> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> AmountOfMoneyData -> AmountOfMoneyData
withValue Double
v (AmountOfMoneyData -> Maybe Token)
-> AmountOfMoneyData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Currency -> AmountOfMoneyData
currencyOnly Currency
c
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleAmountLatent :: Rule
ruleAmountLatent :: Rule
ruleAmountLatent = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<amount> (latent)"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = v}:[Token]
_) ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (AmountOfMoneyData -> Token) -> AmountOfMoneyData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension AmountOfMoneyData -> AmountOfMoneyData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension AmountOfMoneyData
AmountOfMoney (AmountOfMoneyData -> Token)
-> (AmountOfMoneyData -> AmountOfMoneyData)
-> AmountOfMoneyData
-> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountOfMoneyData -> AmountOfMoneyData
mkLatent (AmountOfMoneyData -> Maybe Token)
-> AmountOfMoneyData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> AmountOfMoneyData
valueOnly Double
v
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleAmountUnit
  , Rule
ruleAmountLatent
  , Rule
ruleCurrencies
  ]