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

-- | References to consumed or observed transaction outputs.
--
-- @
-- firstOutput transactionId = input transactionId 0
-- @
module Cometa.Transaction.Input (Input, input, transactionId, index, encodeCbor, decodeCbor) where

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

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Error (Error)
import qualified Cometa.Internal.TransactionCbor as Cbor
import Cometa.TransactionId (TransactionId)
import qualified Cometa.TransactionId as Id
import Data.ByteString (ByteString)
import Data.Word (Word64)

-- TYPES ----------------------------------------------------------------------

-- | Output identity. Ordering compares transaction hash bytes then numeric index.
-- An index occupies the native unsigned 64-bit range; assembly does not establish
-- that the referenced output exists or is spendable in a particular ledger era.
data Input = Input !TransactionId !Word64 deriving (Input -> Input -> Bool
(Input -> Input -> Bool) -> (Input -> Input -> Bool) -> Eq Input
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Input -> Input -> Bool
== :: Input -> Input -> Bool
$c/= :: Input -> Input -> Bool
/= :: Input -> Input -> Bool
Eq, Eq Input
Eq Input =>
(Input -> Input -> Ordering)
-> (Input -> Input -> Bool)
-> (Input -> Input -> Bool)
-> (Input -> Input -> Bool)
-> (Input -> Input -> Bool)
-> (Input -> Input -> Input)
-> (Input -> Input -> Input)
-> Ord Input
Input -> Input -> Bool
Input -> Input -> Ordering
Input -> Input -> Input
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Input -> Input -> Ordering
compare :: Input -> Input -> Ordering
$c< :: Input -> Input -> Bool
< :: Input -> Input -> Bool
$c<= :: Input -> Input -> Bool
<= :: Input -> Input -> Bool
$c> :: Input -> Input -> Bool
> :: Input -> Input -> Bool
$c>= :: Input -> Input -> Bool
>= :: Input -> Input -> Bool
$cmax :: Input -> Input -> Input
max :: Input -> Input -> Input
$cmin :: Input -> Input -> Input
min :: Input -> Input -> Input
Ord, Int -> Input -> ShowS
[Input] -> ShowS
Input -> String
(Int -> Input -> ShowS)
-> (Input -> String) -> ([Input] -> ShowS) -> Show Input
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Input -> ShowS
showsPrec :: Int -> Input -> ShowS
$cshow :: Input -> String
show :: Input -> String
$cshowList :: [Input] -> ShowS
showList :: [Input] -> ShowS
Show)

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

-- | Reference an output by transaction-body hash and zero-based output index.
input :: TransactionId -> Word64 -> Input
input :: TransactionId -> Word64 -> Input
input = TransactionId -> Word64 -> Input
Input

-- | Hash of the transaction that created the output.
transactionId :: Input -> TransactionId
transactionId :: Input -> TransactionId
transactionId (Input TransactionId
value Word64
_) = TransactionId
value

-- | Zero-based output index, from zero through 2^64 - 1.
index :: Input -> Word64
index :: Input -> Word64
index (Input TransactionId
_ Word64
value) = Word64
value

-- | Fresh two-element CBOR array with exact hash bytes and shortest index.
encodeCbor :: Input -> ByteString
encodeCbor :: Input -> ByteString
encodeCbor (Input TransactionId
txId Word64
ix) = Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (TransactionId -> ByteString
Id.toBytes TransactionId
txId) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
E.encodeWord64 Word64
ix)

-- | Decode one definite or indefinite pair, requiring a 32-byte hash and uint64
-- index. Malformed input, extra fields and trailing bytes return 'Cometa.Error.InvalidTransactionCbor'.
decodeCbor :: ByteString -> Either Error Input
decodeCbor :: ByteString -> Either Error Input
decodeCbor = (forall s. Decoder s Input) -> ByteString -> Either Error Input
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode (Int -> Decoder s Input -> Decoder s Input
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 Decoder s Input
forall s. Decoder s Input
parser)
  where
    parser :: Decoder s Input
parser = do
      ByteString
bytes <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
      TransactionId
txId <- (Error -> Decoder s TransactionId)
-> (TransactionId -> Decoder s TransactionId)
-> Either Error TransactionId
-> Decoder s TransactionId
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s TransactionId
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s TransactionId)
-> (Error -> String) -> Error -> Decoder s TransactionId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) TransactionId -> Decoder s TransactionId
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either Error TransactionId
Id.fromBytes ByteString
bytes)
      TransactionId -> Word64 -> Input
Input TransactionId
txId (Word64 -> Input) -> Decoder s Word64 -> Decoder s Input
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64