-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}

-- | Explicit native representation changes for advanced encoding workflows.
-- These operations produce distinct snapshots; their source values remain exact.
-- They follow native representation policy, including retained indefinite script
-- lists and mixed-depth transaction rules. They are not canonical-CBOR codecs.
--
-- Re-encoding can change script hashes, transaction IDs, auxiliary-data hashes
-- and script-data commitments. Transaction signatures are retained verbatim and
-- may become invalid. No commitment, signature or ledger rule is repaired.
-- Inspect the result and assemble/sign a new transaction when identity changes.
-- Ordinary transaction setters remain the interface for normal body edits.
--
-- @
-- import qualified Cometa.Encoding.Reencode as Reencode
-- import qualified Cometa.Script.Native as Native
--
-- reencodedCondition = Reencode.nativeScript (Native.invalidBefore 100)
-- @
module Cometa.Encoding.Reencode
  ( nativeScript,
    nativeScripts,
    transaction,
    subTransaction,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Cometa.Encoding.Cbor as Cbor
import Cometa.Error (Error)
import qualified Cometa.Internal.Reencode as Internal
import qualified Cometa.Script.Native as Native
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.Transaction as Tx

-- DEFINITIONS ----------------------------------------------------------------

-- | Re-encode native condition nodes with fresh definite node arrays and
-- shortest scalar encodings, recursively retaining each child list's existing
-- definite/indefinite shape, order and duplicates. The resulting script hash may
-- differ. Native parsing/encoding failures are explicit; the source is unchanged.
nativeScript :: Native.NativeScript -> IO (Either Error Native.NativeScript)
nativeScript :: NativeScript -> IO (Either Error NativeScript)
nativeScript NativeScript
source = (Either Error ByteString -> Either Error NativeScript)
-> IO (Either Error ByteString) -> IO (Either Error NativeScript)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error NativeScript)
-> Either Error NativeScript
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error NativeScript
Native.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.nativeScript" CUInt
0 (NativeScript -> ByteString
Native.encodeCbor NativeScript
source))

-- | Validate and re-encode an encoded native-script list. Retain the outer
-- list's definite/indefinite shape and recursively apply 'nativeScript' policy
-- to its children. Definite lengths use their shortest encoding. Empty lists,
-- order and duplicates are retained. A non-list or invalid script returns a
-- native error. Source bytes remain unchanged.
nativeScripts :: Cbor.Cbor -> IO (Either Error Cbor.Cbor)
nativeScripts :: Cbor -> IO (Either Error Cbor)
nativeScripts Cbor
source = do
  Either Error ByteString
result <- Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.nativeScripts" CUInt
1 (Cbor -> ByteString
Cbor.toBytes Cbor
source)
  (Error -> IO (Either Error Cbor))
-> (ByteString -> IO (Either Error Cbor))
-> Either Error ByteString
-> IO (Either Error Cbor)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error Cbor -> IO (Either Error Cbor)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Cbor -> IO (Either Error Cbor))
-> (Error -> Either Error Cbor) -> Error -> IO (Either Error Cbor)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> Either Error Cbor
forall a b. a -> Either a b
Left) ByteString -> IO (Either Error Cbor)
Cbor.fromBytes Either Error ByteString
result

-- | Re-encode an outer transaction body, witness collections and auxiliary
-- container under native policy. Nested intent bodies remain exact. Witness
-- datum/redeemer data are re-encoded recursively; native-script child encodings
-- in witnesses are retained and witness set tags are freshly emitted.
-- Existing signatures and commitments are retained, so they may become invalid.
-- Native decoding failures include unsupported source representations, such as
-- indefinite body maps. The original transaction is unchanged.
transaction :: Tx.Transaction -> IO (Either Error Tx.Transaction)
transaction :: Transaction -> IO (Either Error Transaction)
transaction Transaction
source = (Either Error ByteString -> Either Error Transaction)
-> IO (Either Error ByteString) -> IO (Either Error Transaction)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error Transaction)
-> Either Error Transaction
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error Transaction
Tx.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.transaction" CUInt
2 (Transaction -> ByteString
Tx.encodeCbor Transaction
source))

-- | Re-encode one intent's body, witness collections and auxiliary container
-- with the same policy and signature/commitment caveats as 'transaction'. Its
-- body ID may change. This operation does not alter an enclosing transaction or
-- the original intent; native parsing/encoding failures remain explicit.
subTransaction :: Sub.SubTransaction -> IO (Either Error Sub.SubTransaction)
subTransaction :: SubTransaction -> IO (Either Error SubTransaction)
subTransaction SubTransaction
source = (Either Error ByteString -> Either Error SubTransaction)
-> IO (Either Error ByteString) -> IO (Either Error SubTransaction)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error SubTransaction)
-> Either Error SubTransaction
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error SubTransaction
Sub.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.subTransaction" CUInt
3 (SubTransaction -> ByteString
Sub.encodeCbor SubTransaction
source))