-- SPDX-FileCopyrightText: 2024 Steffen Jost <s.jost@fraport.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

-- TODO: why is this Handler.Utils.Profile instead of Utils.Profile? 
-- TODO: consider merging with Handler.Utils.Users?
module Utils.Postal
  ( validPostAddress, validPostAddressText  
  ) where

import Import.NoModel
import Model.Types.Markup

import Data.Char
import qualified Data.Text.Lazy as LT


-- | Primitive postal address requires at least one alphabetic character, one digit and a line break
validPostAddress :: Maybe StoredMarkup -> Bool
validPostAddress (Just StoredMarkup {markupInput = addr}) = validPostAddressLazyText addr
validPostAddress _ = False

validPostAddressText :: Text -> Bool
validPostAddressText = validPostAddressLazyText . LT.fromStrict

validPostAddressLazyText :: LT.Text -> Bool
validPostAddressLazyText addr
  | Just _ <- LT.find isLetter addr
  , Just _ <- LT.find isNumber addr
  -- , Just _ <- LT.find ((LineSeparator ==) . generalCategory) addr -- THIS DID NOT WORK
    = 1 < length (LT.lines addr)    
validPostAddressLazyText _ = False
