content: -- 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 LambdaCase #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  module Duckling.AmountOfMoney.MN.Rules
    ( rules
    ) where
  
  import Data.Maybe
  import Data.String
  import Prelude
  
  import Duckling.AmountOfMoney.Helpers
  import Duckling.AmountOfMoney.Types (Currency(..), AmountOfMoneyData(..))
  import Duckling.Dimensions.Types
  import Duckling.Numeral.Helpers (isNatural, 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
  
  ruleUnitAmount :: Rule
  ruleUnitAmount = Rule
    { name = "<unit> <amount>"
    , pattern =
      [ Predicate isCurrencyOnly
      , Predicate isPositive
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.currency = c}:
         Token Numeral NumeralData{TNumeral.value = v}:
         _) -> Just . Token AmountOfMoney . withValue v $ currencyOnly c
        _ -> Nothing
    }
  
  ruleTugriks :: Rule
  ruleTugriks = Rule
    { name = "төг"
    , pattern =
      [ regex "төг(рөг(ийн)?)?"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly MNT
    }
  
  rulePounds :: Rule
  rulePounds = Rule
    { name = "£"
    , pattern =
      [ regex "фунт(аар|тай|аас)?"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Pound
    }
  
  ruleGBP :: Rule
  ruleGBP = Rule
    { name = "Mongolian GBP"
    , pattern =
      [ regex "Английн\\s+фунт"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly GBP
    }
  
  ruleDollar :: Rule
  ruleDollar = Rule
    { name = "$"
    , pattern =
      [ regex "доллар(ын|оор|оос|той)?"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Dollar
    }
  
  ruleCent :: Rule
  ruleCent = Rule
    { name = "cent"
    , pattern =
      [ regex "цент(ийн|ээс|ээр|тэй)?|пени|пенс(ээр|гээр|тэй|ээс|гээс)?|ц"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Cent
    }
  
  ruleEUR :: Rule
  ruleEUR = Rule
    { name = "€"
    , pattern =
      [ regex "евро"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly EUR
    }
  
  ruleBucks :: Rule
  ruleBucks = Rule
    { name = "bucks"
    , pattern =
      [ regex "бакс(аар|тай|аас)?"
      ]
    , prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Unnamed
    }
  
  rulePrecision :: Rule
  rulePrecision = Rule
    { name = "about|exactly <amount-of-money>"
    , pattern =
      [ regex "яг|ойролцоогоор|бараг"
      , Predicate isMoneyWithValue
      ]
    , prod = \case
        (_:token:_) -> Just token
        _ -> Nothing
    }
  
  ruleIntervalBetweenNumeral :: Rule
  ruleIntervalBetweenNumeral = Rule
    { name = "between|from <numeral> to|and <amount-of-money>"
    , pattern =
      [ Predicate isPositive
      , regex "-c"
      , Predicate isSimpleAmountOfMoney
      , regex "(-н\\s+)?(хооронд|хүртэл)"
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = from}:
         _:
         Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c}:
         _) | from < to ->
          Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c
        _ -> Nothing
    }
  
  ruleIntervalBetweenNumeral2 :: Rule
  ruleIntervalBetweenNumeral2 = Rule
    { name = "between|from <amount-of-money> to|and <numeral>"
    , pattern =
      [ Predicate isSimpleAmountOfMoney
      , regex "-c"
      , Predicate isNatural
      , regex "(-н\\s+)?(хооронд|хүртэл)"
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just from,
                    TAmountOfMoney.currency = c}:
         _:
         Token Numeral NumeralData{TNumeral.value = to}:
         _) | from < to ->
          Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c
        _ -> Nothing
    }
  
  ruleIntervalBetween :: Rule
  ruleIntervalBetween = Rule
    { name = "between|from <amount-of-money> to|and <amount-of-money>"
    , pattern =
      [ Predicate isSimpleAmountOfMoney
      , regex "-c"
      , Predicate isSimpleAmountOfMoney
      , regex "(-н\\s+)?(хооронд|хүртэл)"
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just from,
                    TAmountOfMoney.currency = c1}:
         _:
         Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c2}:
         _) | from < to && c1 == c2 ->
          Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c1
        _ -> Nothing
    }
  
  ruleIntervalNumeralDash :: Rule
  ruleIntervalNumeralDash = Rule
    { name = "<numeral> - <amount-of-money>"
    , pattern =
      [ Predicate isPositive
      , regex "-"
      , Predicate isSimpleAmountOfMoney
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = from}:
         _:
         Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c}:
         _) | from < to ->
           Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c
        _ -> Nothing
    }
  
  ruleIntervalDash :: Rule
  ruleIntervalDash = Rule
    { name = "<amount-of-money> - <amount-of-money>"
    , pattern =
      [ Predicate isSimpleAmountOfMoney
      , regex "-"
      , Predicate isSimpleAmountOfMoney
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just from,
                    TAmountOfMoney.currency = c1}:
         _:
         Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c2}:
         _) | from < to && c1 == c2 ->
          Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c1
        _ -> Nothing
    }
  
  ruleIntervalMax :: Rule
  ruleIntervalMax = Rule
    { name = "under/less/lower/no more than <amount-of-money>"
    , pattern =
      [ Predicate isSimpleAmountOfMoney
      , regex "-c\\s+(бага|доогуур|ихгүй)"
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c}:
         _) -> Just . Token AmountOfMoney . withMax to $ currencyOnly c
        _ -> Nothing
    }
  
  ruleIntervalMin :: Rule
  ruleIntervalMin = Rule
    { name = "over/above/at least/more than <amount-of-money>"
    , pattern =
      [ Predicate isSimpleAmountOfMoney
      , regex "-c\\s+(их|дээгүүр|илүү)"
      ]
    , prod = \case
        (Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
                    TAmountOfMoney.currency = c}:
         _) -> Just . Token AmountOfMoney . withMin to $ currencyOnly c
        _ -> Nothing
    }
  
  rules :: [Rule]
  rules =
    [ ruleUnitAmount
    , ruleBucks
    , ruleCent
    , ruleDollar
    , ruleEUR
    , ruleGBP
    , ruleIntervalBetweenNumeral
    , ruleIntervalBetweenNumeral2
    , ruleIntervalBetween
    , ruleIntervalMax
    , ruleIntervalMin
    , ruleIntervalNumeralDash
    , ruleIntervalDash
    , rulePounds
    , rulePrecision
    , ruleTugriks
    ]
  