-- 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.

-- | Locate objects in a top-level transaction for deferred redeemers and script
-- arguments. Indices start at zero and can change when the body changes.
-- Missing objects return 'Nothing'. Nested levels remain independent.
--
-- @
-- import qualified Cometa.Transaction.Indices as Indices
--
-- spendingPosition reference transaction = Indices.findInput reference transaction
-- @
module Cometa.Transaction.Indices
  ( findInput,
    findReferenceInput,
    findOutput,
    findMintPolicy,
    findWithdrawal,
    findCertificate,
    findVote,
    findProposal,
    findRedeemer,
  )
where

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

import Cometa.Address (Address)
import Cometa.Address.Reward (RewardAddress)
import Cometa.Certificate (EncodedCertificate)
import Cometa.Error (Error)
import Cometa.Evaluation (RedeemerId)
import Cometa.Governance.Proposal (Proposal)
import Cometa.Governance.Voting (Voter)
import qualified Cometa.Internal.ContextualTransaction as Native
import qualified Cometa.Internal.TransactionIndices as Indices
import Cometa.PolicyId (PolicyId)
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Body as Body
import qualified Cometa.Transaction.Fields as Fields
import Cometa.Transaction.Input (Input)
import qualified Cometa.Transaction.Witnesses as Witnesses
import Data.ByteString (ByteString)
import Data.Word (Word64)

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

-- | First spending reference in hash-byte then numeric-index order. Duplicates
-- occupy separate positions.
findInput :: Input -> Tx.Transaction -> Maybe Word64
findInput :: Input -> Transaction -> Maybe Word64
findInput Input
reference = Input -> Fields -> Maybe Word64
Indices.findInput Input
reference (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | First reference input in the same order as 'findInput'.
findReferenceInput :: Input -> Tx.Transaction -> Maybe Word64
findReferenceInput :: Input -> Transaction -> Maybe Word64
findReferenceInput Input
reference = Input -> Fields -> Maybe Word64
Indices.findReferenceInput Input
reference (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | First output at the exact address containing at least the minimum lovelace.
-- Output creation order is retained; the minimum spans unsigned 64-bit values.
findOutput :: Address -> Word64 -> Tx.Transaction -> Maybe Word64
findOutput :: Address -> Word64 -> Transaction -> Maybe Word64
findOutput Address
destination Word64
minimumLovelace = Address -> Word64 -> Fields -> Maybe Word64
Indices.findOutput Address
destination Word64
minimumLovelace (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | Policy position in ascending hash-byte order. Imported policies with only
-- zero quantities or an empty asset map still occupy positions.
findMintPolicy :: PolicyId -> Tx.Transaction -> Maybe Word64
findMintPolicy :: PolicyId -> Transaction -> Maybe Word64
findMintPolicy PolicyId
policy = PolicyId -> ByteString -> Maybe Word64
Indices.findMintPolicy PolicyId
policy (ByteString -> Maybe Word64)
-> (Transaction -> ByteString) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> ByteString
bodyBytes

-- | Withdrawal position in ledger reward-account order: test networks before
-- mainnet, then script credentials before key credentials, then hash bytes.
-- All network tags other than 1 belong to the same test-network class.
findWithdrawal :: RewardAddress -> Tx.Transaction -> Maybe Word64
findWithdrawal :: RewardAddress -> Transaction -> Maybe Word64
findWithdrawal RewardAddress
account = RewardAddress -> Fields -> Maybe Word64
Indices.findWithdrawal RewardAddress
account (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | First certificate with the same native canonical encoding. Certificate
-- list order and duplicates count. Imported representation differences are
-- normalized by the native certificate codecs; no transaction bytes change.
findCertificate :: EncodedCertificate -> Tx.Transaction -> IO (Either Error (Maybe Word64))
findCertificate :: EncodedCertificate
-> Transaction -> IO (Either Error (Maybe Word64))
findCertificate EncodedCertificate
certificate = EncodedCertificate
-> Maybe ByteString -> IO (Either Error (Maybe Word64))
Native.findCertificate EncodedCertificate
certificate (Maybe ByteString -> IO (Either Error (Maybe Word64)))
-> (Transaction -> Maybe ByteString)
-> Transaction
-> IO (Either Error (Maybe Word64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe ByteString
Indices.certificateBytes (ByteString -> Maybe ByteString)
-> (Transaction -> ByteString) -> Transaction -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> ByteString
bodyBytes

-- | Voter position in ledger role order: committee, DRep, stake pool. Within
-- each role script credentials precede key credentials, then hash bytes order
-- each kind. Multiple action votes by one voter occupy a single position.
findVote :: Voter -> Tx.Transaction -> Maybe Word64
findVote :: Voter -> Transaction -> Maybe Word64
findVote Voter
voter = Voter -> Fields -> Maybe Word64
Indices.findVote Voter
voter (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | First proposal with the same canonical encoding, including its deposit,
-- return account and anchor. Positions follow proposal insertion order, counting
-- actions without scripts. A proposing redeemer uses this same position.
findProposal :: Proposal -> Tx.Transaction -> Maybe Word64
findProposal :: Proposal -> Transaction -> Maybe Word64
findProposal Proposal
proposal = Proposal -> Fields -> Maybe Word64
Indices.findProposal Proposal
proposal (Fields -> Maybe Word64)
-> (Transaction -> Fields) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Fields
fields

-- | Redeemer rank ordered by purpose then purpose index, independently of its
-- imported witness collection order. This is its position among all redeemers,
-- rather than its position in the body collection authorized by the script.
findRedeemer :: RedeemerId -> Tx.Transaction -> Maybe Word64
findRedeemer :: RedeemerId -> Transaction -> Maybe Word64
findRedeemer RedeemerId
identifier = RedeemerId -> [Redeemer] -> Maybe Word64
Indices.findRedeemer RedeemerId
identifier ([Redeemer] -> Maybe Word64)
-> (Transaction -> [Redeemer]) -> Transaction -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Witnesses -> [Redeemer]
Witnesses.redeemers (Witnesses -> [Redeemer])
-> (Transaction -> Witnesses) -> Transaction -> [Redeemer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedWitnesses -> Witnesses
Witnesses.toWitnesses (EncodedWitnesses -> Witnesses)
-> (Transaction -> EncodedWitnesses) -> Transaction -> Witnesses
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> EncodedWitnesses
Tx.witnesses

fields :: Tx.Transaction -> Fields.Fields
fields :: Transaction -> Fields
fields = Body -> Fields
Body.bodyFields (Body -> Fields) -> (Transaction -> Body) -> Transaction -> Fields
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedBody -> Body
Body.toBody (EncodedBody -> Body)
-> (Transaction -> EncodedBody) -> Transaction -> Body
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> EncodedBody
Tx.body

bodyBytes :: Tx.Transaction -> ByteString
bodyBytes :: Transaction -> ByteString
bodyBytes = EncodedBody -> ByteString
Body.toCbor (EncodedBody -> ByteString)
-> (Transaction -> EncodedBody) -> Transaction -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> EncodedBody
Tx.body