module U.Codebase.Sqlite.HashHandle
  ( HashHandle (..),
    HashMismatch (..),
    DeclHashingError (..),
  )
where

import U.Codebase.Branch.Type (Branch)
import U.Codebase.BranchV3 (BranchV3)
import U.Codebase.HashTags
import U.Codebase.Reference qualified as C
import U.Codebase.Sqlite.Branch.Format (HashBranchLocalIds)
import U.Codebase.Sqlite.Branch.Full (LocalBranch)
import U.Codebase.Sqlite.Decl.Format qualified as DeclFormat
import U.Codebase.Sqlite.Patch.Format (HashPatchLocalIds)
import U.Codebase.Sqlite.Patch.Full (LocalPatch)
import U.Codebase.Sqlite.Symbol (Symbol)
import U.Codebase.Sqlite.Term.Format qualified as TermFormat
import U.Codebase.Term qualified as C.Term
import U.Codebase.Type qualified as C.Type
import Unison.Hash (Hash)
import Unison.Prelude

data HashMismatch = HashMismatch
  { HashMismatch -> Hash
expectedHash :: Hash,
    HashMismatch -> Hash
actualHash :: Hash
  }

data DeclHashingError
  = DeclHashMismatch HashMismatch
  | DeclHashResolutionFailure

data HashHandle = HashHandle
  { -- | Hash type
    HashHandle -> Type Symbol -> Reference
toReference :: C.Term.Type Symbol -> C.Reference,
    -- | Hash type's mentions
    HashHandle -> Type Symbol -> Set Reference
toReferenceMentions :: C.Term.Type Symbol -> Set C.Reference,
    -- | Hash the type of a single constructor in a decl component. The provided hash argument is the hash of the decl component.
    HashHandle -> Hash -> TypeD Symbol -> Reference
toReferenceDecl :: Hash -> C.Type.TypeD Symbol -> C.Reference,
    -- | Hash decl's mentions
    HashHandle -> Hash -> TypeD Symbol -> Set Reference
toReferenceDeclMentions :: Hash -> C.Type.TypeD Symbol -> Set C.Reference,
    HashHandle
-> forall (m :: * -> *). Monad m => Branch m -> m BranchHash
hashBranch :: forall m. (Monad m) => Branch m -> m BranchHash,
    HashHandle -> forall (m :: * -> *). BranchV3 m -> BranchHash
hashBranchV3 :: forall m. BranchV3 m -> BranchHash,
    HashHandle -> BranchHash -> Set CausalHash -> CausalHash
hashCausal ::
      -- The causal's namespace hash
      BranchHash ->
      -- The causal's parents
      Set CausalHash ->
      CausalHash,
    HashHandle -> HashBranchLocalIds -> LocalBranch -> BranchHash
hashBranchFormatFull ::
      HashBranchLocalIds ->
      LocalBranch ->
      BranchHash,
    HashHandle -> HashPatchLocalIds -> LocalPatch -> PatchHash
hashPatchFormatFull ::
      HashPatchLocalIds ->
      LocalPatch ->
      PatchHash,
    HashHandle -> ComponentHash -> HashTermFormat -> Maybe HashMismatch
verifyTermFormatHash ::
      ComponentHash ->
      TermFormat.HashTermFormat ->
      Maybe (HashMismatch),
    HashHandle
-> ComponentHash -> HashDeclFormat -> Maybe DeclHashingError
verifyDeclFormatHash ::
      ComponentHash ->
      DeclFormat.HashDeclFormat ->
      Maybe DeclHashingError
  }