module Common.Util.Text
  ( search
  , formatSearch
  ) where

import           Data.Text (Text)
import qualified Data.Text as T

search :: Text -> Text -> Bool
search s t = (formatSearch s) `T.isInfixOf` (formatSearch t)

formatSearch :: Text -> Text
formatSearch = T.toLower . unaccent

unaccent :: Text -> Text
unaccent = T.map unaccentChar

unaccentChar :: Char -> Char
unaccentChar c = case c of
  'à' -> 'a'
  'á' -> 'a'
  'â' -> 'a'
  'ã' -> 'a'
  'ä' -> 'a'
  'ç' -> 'c'
  'è' -> 'e'
  'é' -> 'e'
  'ê' -> 'e'
  'ë' -> 'e'
  'ì' -> 'i'
  'í' -> 'i'
  'î' -> 'i'
  'ï' -> 'i'
  'ñ' -> 'n'
  'ò' -> 'o'
  'ó' -> 'o'
  'ô' -> 'o'
  'õ' -> 'o'
  'ö' -> 'o'
  'š' -> 's'
  'ù' -> 'u'
  'ú' -> 'u'
  'û' -> 'u'
  'ü' -> 'u'
  'ý' -> 'y'
  'ÿ' -> 'y'
  'ž' -> 'z'
  _   -> c