module Data.JLD.Compaction.InverseContext (buildInverseContext) where import Data.JLD.Prelude import Data.JLD.Model.ActiveContext (ActiveContext (..)) import Data.JLD.Model.Direction (Direction (..)) import Data.JLD.Model.InverseContext (InverseContext, insert) import Data.JLD.Model.Keyword (Keyword (..)) import Data.JLD.Model.Language (Language (Language)) import Data.JLD.Model.TermDefinition (TermDefinition (..)) import Data.Map qualified as M (foldlWithKey) processTerm :: Text -> InverseContext -> Text -> TermDefinition -> InverseContext processTerm defaultLangDir out termName TermDefinition{..} | Just variableName <- termDefinitionIriMapping = out |> insert variableName container KeywordAny (show KeywordNone) termName .> if | termDefinitionReversePropertyFlag -> insert variableName container KeywordType (show KeywordReverse) termName | termDefinitionTypeMapping == Just (show KeywordNone) -> insert variableName container KeywordLanguage (show KeywordAny) termName .> insert variableName container KeywordType (show KeywordAny) termName | Just typeMapping <- termDefinitionTypeMapping -> insert variableName container KeywordType typeMapping termName | Just langDir <- maybeLangDir -> insert variableName container KeywordLanguage langDir termName | otherwise -> insert variableName container KeywordLanguage defaultLangDir termName .> insert variableName container KeywordLanguage (show KeywordNone) termName .> insert variableName container KeywordType (show KeywordNone) termName | otherwise = out where container = if null termDefinitionContainerMapping then show KeywordNone else fold termDefinitionContainerMapping maybeLangDir = case (termDefinitionLanguageMapping, termDefinitionDirectionMapping) of (Just (Language language), Just LTR) -> Just <| language <> "_ltr" (Just (Language language), Just RTL) -> Just <| language <> "_rtl" (Just (Language language), _) -> Just <| language (Just _, Just LTR) -> Just "_ltr" (Just _, Just RTL) -> Just "_rtl" (Just _, _) -> Just <| show KeywordNull (Nothing, Just LTR) -> Just "_ltr" (Nothing, Just RTL) -> Just "_rtl" (Nothing, Just NoDirection) -> Just <| show KeywordNone (Nothing, Nothing) -> Nothing buildInverseContext :: ActiveContext -> InverseContext buildInverseContext ActiveContext{..} = M.foldlWithKey (processTerm defaultLangDir) mempty activeContextTerms where defaultLangDir = case (activeContextDefaultBaseDirection, activeContextDefaultLanguage) of (Just bd, Just (Language dl)) -> dl <> "_" <> show bd (Just bd, _) -> "_" <> show bd (_, _) -> show KeywordNone