module Unison.Merge.Mergeblob4
  ( Mergeblob4 (..),
    makeMergeblob4,
  )
where

import Data.Map.Strict qualified as Map
import Unison.Merge.Mergeblob3 (Mergeblob3 (..))
import Unison.Names (Names (..))
import Unison.Parser.Ann (Ann)
import Unison.Parsers qualified as Parsers
import Unison.Prelude
import Unison.Reference (TermReference, TypeReference)
import Unison.Symbol (Symbol)
import Unison.Syntax.Parser (ParsingEnv (..))
import Unison.Syntax.Parser qualified as Parser
import Unison.UnisonFile (UnisonFile)
import Unison.UnisonFile qualified as UnisonFile
import Unison.Util.Defns (Defns (..), DefnsF)
import Unison.Util.Pretty qualified as Pretty
import Unison.Util.Relation qualified as Relation

data Mergeblob4 = Mergeblob4
  { Mergeblob4 -> DefnsF Set TypeReference TypeReference
dependencies :: DefnsF Set TermReference TypeReference,
    Mergeblob4 -> UnisonFile Symbol Ann
file :: UnisonFile Symbol Ann
  }

makeMergeblob4 :: Mergeblob3 -> Either (Parser.Err Symbol) Mergeblob4
makeMergeblob4 :: Mergeblob3 -> Either (Err Symbol) Mergeblob4
makeMergeblob4 Mergeblob3
blob = do
  let stageOneNames :: Names
stageOneNames =
        Relation Name Referent -> Relation Name TypeReference -> Names
Names (Map Name Referent -> Relation Name Referent
forall a b. (Ord a, Ord b) => Map a b -> Relation a b
Relation.fromMap Mergeblob3
blob.stageOne.terms) (Map Name TypeReference -> Relation Name TypeReference
forall a b. (Ord a, Ord b) => Map a b -> Relation a b
Relation.fromMap Mergeblob3
blob.stageOne.types) Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> Mergeblob3
blob.libdeps

      parsingEnv :: ParsingEnv Identity
parsingEnv =
        ParsingEnv
          { -- We don't expect to have to generate any new GUIDs, since the uniqueTypeGuid lookup function below should
            -- cover all name in the merged file we're about to parse and typecheck. So, this might be more correct as a
            -- call to `error`.
            $sel:uniqueNames:ParsingEnv :: UniqueName
uniqueNames = (Pos -> Int -> Maybe Text) -> UniqueName
Parser.UniqueName \Pos
_ Int
_ -> Maybe Text
forall a. Maybe a
Nothing,
            $sel:uniqueTypeGuid:ParsingEnv :: Name -> Identity (Maybe Text)
uniqueTypeGuid = \Name
name -> Maybe Text -> Identity (Maybe Text)
forall a. a -> Identity a
Identity (Name -> Map Name Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Name
name Mergeblob3
blob.uniqueTypeGuids),
            $sel:names:ParsingEnv :: Names
names = Names
stageOneNames,
            $sel:maybeNamespace:ParsingEnv :: Maybe Name
maybeNamespace = Maybe Name
forall a. Maybe a
Nothing,
            $sel:localNamespacePrefixedTypesAndConstructors:ParsingEnv :: Names
localNamespacePrefixedTypesAndConstructors = Names
forall a. Monoid a => a
mempty
          }
  UnisonFile Symbol Ann
file <- Identity (Either (Err Symbol) (UnisonFile Symbol Ann))
-> Either (Err Symbol) (UnisonFile Symbol Ann)
forall a. Identity a -> a
runIdentity (FilePath
-> FilePath
-> ParsingEnv Identity
-> Identity (Either (Err Symbol) (UnisonFile Symbol Ann))
forall (m :: * -> *) v.
(Monad m, Var v) =>
FilePath
-> FilePath
-> ParsingEnv m
-> m (Either (Err v) (UnisonFile v Ann))
Parsers.parseFile FilePath
"<merge>" (Width -> Pretty ColorText -> FilePath
Pretty.toPlain Width
80 Mergeblob3
blob.unparsedFile) ParsingEnv Identity
parsingEnv)
  Mergeblob4 -> Either (Err Symbol) Mergeblob4
forall a b. b -> Either a b
Right
    Mergeblob4
      { $sel:dependencies:Mergeblob4 :: DefnsF Set TypeReference TypeReference
dependencies = UnisonFile Symbol Ann -> DefnsF Set TypeReference TypeReference
forall a v.
(Monoid a, Var v) =>
UnisonFile v a -> DefnsF Set TypeReference TypeReference
UnisonFile.dependencies UnisonFile Symbol Ann
file,
        UnisonFile Symbol Ann
$sel:file:Mergeblob4 :: UnisonFile Symbol Ann
file :: UnisonFile Symbol Ann
file
      }