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 OverloadedStrings #-}
  module Duckling.Numeral.ES.ES.Rules
    ( rules
    , rulesBackwardCompatible
    ) where
  
  import qualified Data.HashMap.Strict as HashMap
  import Data.Maybe
  import Data.String
  import qualified Data.Text as Text
  import Prelude
  
  import Duckling.Dimensions.Types
  import Duckling.Numeral.Helpers
  import Duckling.Numeral.Types (NumeralData(..))
  import qualified Duckling.Numeral.Types as TNumeral
  import Duckling.Regex.Types
  import Duckling.Types
  
  ruleDecimalWithThousandsSeparator :: Rule
  ruleDecimalWithThousandsSeparator = Rule
    { name = "decimal with thousands separator ."
    , pattern = [regex "(\\d+(\\.\\d\\d\\d)+,\\d+)"]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match : _)) : _) ->
          let fmt = Text.replace "," "." $ Text.replace "." Text.empty match
          in parseDouble fmt >>= double
        _ -> Nothing
    }
  
  ruleDecimalNumeral :: Rule
  ruleDecimalNumeral = Rule
    { name = "decimal number ,"
    , pattern = [regex "(\\d*,\\d+)"]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match : _)) : _) ->
          parseDecimal False match
        _ -> Nothing
    }
  
  ruleIntegerWithThousandsSeparator :: Rule
  ruleIntegerWithThousandsSeparator = Rule
    { name = "integer with thousands separator ."
    , pattern = [regex "(\\d{1,3}(\\.\\d\\d\\d){1,5})"]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match : _)) : _) ->
          parseDouble (Text.replace "." Text.empty match) >>= double
        _ -> Nothing
    }
  
  rulesBackwardCompatible :: [Rule]
  rulesBackwardCompatible =
    [ ruleDecimalNumeral
    , ruleDecimalWithThousandsSeparator
    , ruleIntegerWithThousandsSeparator
    ]
  
  rules :: [Rule]
  rules = rulesBackwardCompatible
  