aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Model/InverseContext.hs
blob: 95cdeb888a4dfbcbf7b988b68ce7a51c24093d6f (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
module Data.JLD.Model.InverseContext (InverseContext, hasKey3, insert, selectTerm) where

import Data.JLD.Prelude

import Data.JLD.Model.Keyword (Keyword)
import Data.Map qualified as M
import Data.Set qualified as S
import Data.Vector (Vector, (!?))
import Data.Vector qualified as V (catMaybes)

type InverseContext = Map Text (Map Text (Map Keyword (Map Text Text)))

hasKey3 :: Text -> Text -> Keyword -> InverseContext -> Bool
hasKey3 var container type' inverseContext =
    M.lookup var inverseContext >>= M.lookup container |> maybe False (M.member type')

lookup4 :: Text -> Text -> Keyword -> Text -> InverseContext -> Maybe Text
lookup4 var container type' typeMapping inverseContext =
    M.lookup var inverseContext >>= M.lookup container >>= M.lookup type' >>= M.lookup typeMapping

insert :: Text -> Text -> Keyword -> Text -> Text -> InverseContext -> InverseContext
insert var container type' typeMapping value =
    M.alter (Just <. M.alter (Just <. M.alter (Just <. M.insert typeMapping value <. fromMaybe mempty) type' <. fromMaybe mempty) container <. fromMaybe mempty) var

selectTerm :: Text -> Set Text -> Keyword -> Vector Text -> InverseContext -> Maybe Text
selectTerm var containers typeLanguage preferredValues inverseContext =
    containers
        |> S.filter (\container -> hasKey3 var container typeLanguage inverseContext)
        .> foldMap' (\container -> preferredValues <&> \item -> lookup4 var container typeLanguage item inverseContext)
        .> V.catMaybes
        .> (!? 0)