aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Compaction/InverseContext.hs
blob: 03a45f2f450229675a3a7d28fb618630a4c3014e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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