{-# LANGUAGE OverloadedRecordDot #-}

-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com
-- SPDX-License-Identifier: MIT

module Engine (Engine (..), building, current, fresh, stepOf, yaml) where

import AST
import Contextualize (contextualize)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import Deps (BuildTermFunc)
import Functions (buildTerm, contextualizing)
import Inference (Inference, dataizationOf, morphingOf)
import Rewriter (every, interpreted)
import Rule (Step, normal)
import qualified Yaml as Y

data Engine = Engine
  { Engine -> [Step]
_normalization :: [Step]
  , Engine -> Maybe Expression -> Expression -> Set Int
_matching :: Maybe Expression -> Expression -> Set Int
  , Engine -> Map String Step
_rules :: Map String Step
  , Engine -> Expression -> Bool
_normal :: Expression -> Bool
  , Engine -> Expression -> Expression -> IO Expression
_contextualize :: Expression -> Expression -> IO Expression
  , Engine -> [Inference Expression]
_morphing :: [Inference Expression]
  , Engine -> [Inference Bytes]
_dataization :: [Inference Bytes]
  , Engine -> [String]
_sources :: [String]
  }

yaml :: Engine
yaml :: Engine
yaml = [Step]
-> (Maybe Expression -> Expression -> Set Int)
-> Map String Step
-> (Expression -> Bool)
-> (Expression -> Expression -> IO Expression)
-> [Inference Expression]
-> [Inference Bytes]
-> [String]
-> Engine
Engine [Step]
steps ([Step] -> Maybe Expression -> Expression -> Set Int
every [Step]
steps) Map String Step
forall k a. Map k a
Map.empty Expression -> Bool
normal Expression -> Expression -> IO Expression
contextualize ((MorphRule -> Inference Expression)
-> [MorphRule] -> [Inference Expression]
forall a b. (a -> b) -> [a] -> [b]
map MorphRule -> Inference Expression
morphingOf [MorphRule]
Y.morphingRules) ((DataizeRule -> Inference Bytes)
-> [DataizeRule] -> [Inference Bytes]
forall a b. (a -> b) -> [a] -> [b]
map DataizeRule -> Inference Bytes
dataizationOf [DataizeRule]
Y.dataizationRules) [String]
current
  where
    steps :: [Step]
    steps :: [Step]
steps = (Rule -> Step) -> [Rule] -> [Step]
forall a b. (a -> b) -> [a] -> [b]
map Rule -> Step
interpreted [Rule]
Y.normalizationRules

stepOf :: Engine -> Y.Rule -> Step
stepOf :: Engine -> Rule -> Step
stepOf Engine
engine Rule
rule = Step -> Maybe Step -> Step
forall a. a -> Maybe a -> a
fromMaybe (Rule -> Step
interpreted Rule
rule) (String -> Map String Step -> Maybe Step
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Rule -> String
forall a. Show a => a -> String
show Rule
rule) Engine
engine._rules)

current :: [String]
current :: [String]
current =
  (Rule -> String) -> [Rule] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Rule -> String
forall a. Show a => a -> String
show [Rule]
Y.normalizationRules
    [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ (ContextualizeRule -> String) -> [ContextualizeRule] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ContextualizeRule -> String
forall a. Show a => a -> String
show [ContextualizeRule]
Y.contextualizationRules
    [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ (MorphRule -> String) -> [MorphRule] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map MorphRule -> String
forall a. Show a => a -> String
show [MorphRule]
Y.morphingRules
    [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ (DataizeRule -> String) -> [DataizeRule] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map DataizeRule -> String
forall a. Show a => a -> String
show [DataizeRule]
Y.dataizationRules

fresh :: Engine -> Bool
fresh :: Engine -> Bool
fresh Engine
engine = Engine
engine._sources [String] -> [String] -> Bool
forall a. Eq a => a -> a -> Bool
== [String]
current

building :: Engine -> BuildTermFunc
building :: Engine -> BuildTermFunc
building Engine
engine String
"contextualize" = (Expression -> Expression -> IO Expression) -> BuildTermMethod
contextualizing Engine
engine._contextualize
building Engine
_ String
func = BuildTermFunc
buildTerm String
func