-- 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.CreditCardNumber.Rules
  ( rules ) where

import Prelude
import Data.String
import Data.Text (Text)
import Data.Bool
import qualified Data.Text as T
import qualified Data.Char as C

import Duckling.Dimensions.Types
import Duckling.CreditCardNumber.Helpers
import qualified Duckling.CreditCardNumber.Types as TCreditCardNumber
import Duckling.Regex.Types
import Duckling.Types

creditCards :: [(Text, PatternItem, TCreditCardNumber.Issuer)]
creditCards :: [(Text, PatternItem, Issuer)]
creditCards =
  [ ( Text
"visa credit card number"
    , String -> PatternItem
regex String
visaCreditCardNumberRegex
    , Issuer
TCreditCardNumber.Visa
    )
  , ( Text
"amex card number"
    , String -> PatternItem
regex String
amexCreditCardNumberRegex
    , Issuer
TCreditCardNumber.Amex
    )
  , ( Text
"discover card number"
    , String -> PatternItem
regex String
discoverCreditCardNumberRegex
    , Issuer
TCreditCardNumber.Discover
    )
  , ( Text
"mastercard card number"
    , String -> PatternItem
regex String
mastercardCreditCardNumberRegex
    , Issuer
TCreditCardNumber.Mastercard
    )
  , ( Text
"diner club card number"
    , String -> PatternItem
regex String
dinerClubCreditCardNumberRegex
    , Issuer
TCreditCardNumber.DinerClub
    )
  , ( Text
"credit card number"
    , String -> PatternItem
regex String
otherCreditCardNumberRegex
    , Issuer
TCreditCardNumber.Other
    )
  ]

rules :: [Rule]
rules :: [Rule]
rules = ((Text, PatternItem, Issuer) -> Rule)
-> [(Text, PatternItem, Issuer)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, PatternItem, Issuer) -> Rule
go [(Text, PatternItem, Issuer)]
creditCards
  where
    go :: (Text, PatternItem, TCreditCardNumber.Issuer) -> Rule
    go :: (Text, PatternItem, Issuer) -> Rule
go (Text
name, PatternItem
regexPattern, Issuer
i) = Rule :: Text -> Pattern -> Production -> Rule
Rule
      { name :: Text
name = Text
name
      , pattern :: Pattern
pattern = [ PatternItem
regexPattern ]
      , prod :: Production
prod = \case
          (Token Dimension a
RegexMatch (GroupMatch (ccNumGroup:_)):[Token]
_) ->
            Maybe Token -> Maybe Token -> Bool -> Maybe Token
forall a. a -> a -> Bool -> a
bool
              Maybe Token
forall a. Maybe a
Nothing
              (Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> Token -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Dimension CreditCardNumberData -> CreditCardNumberData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension CreditCardNumberData
CreditCardNumber (CreditCardNumberData -> Token) -> CreditCardNumberData -> Token
forall a b. (a -> b) -> a -> b
$ Text -> Issuer -> CreditCardNumberData
creditCard Text
ccNum Issuer
i)
              (Text -> Bool
isValidCreditCardNumber Text
ccNum)
            where
              ccNum :: Text
ccNum = (Char -> Bool) -> Text -> Text
T.filter Char -> Bool
C.isDigit Text
ccNumGroup
          [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
      }