module Plutarch.LedgerApi.Value.TokenName (
  PTokenName (..),
  padaToken,
) where

import Data.ByteString (ByteString)
import GHC.Generics (Generic)
import Generics.SOP qualified as SOP
import Plutarch.Prelude
import PlutusLedgerApi.V3 qualified as Plutus
import PlutusTx.Builtins.Internal qualified as PlutusTx

-- | @since 2.0.0
newtype PTokenName (s :: S) = PTokenName (Term s PByteString)
  deriving stock
    ( -- | @since 2.0.0
      (forall x. PTokenName s -> Rep (PTokenName s) x)
-> (forall x. Rep (PTokenName s) x -> PTokenName s)
-> Generic (PTokenName s)
forall x. Rep (PTokenName s) x -> PTokenName s
forall x. PTokenName s -> Rep (PTokenName s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PTokenName s) x -> PTokenName s
forall (s :: S) x. PTokenName s -> Rep (PTokenName s) x
$cfrom :: forall (s :: S) x. PTokenName s -> Rep (PTokenName s) x
from :: forall x. PTokenName s -> Rep (PTokenName s) x
$cto :: forall (s :: S) x. Rep (PTokenName s) x -> PTokenName s
to :: forall x. Rep (PTokenName s) x -> PTokenName s
Generic
    )
  deriving anyclass
    ( -- | @since 3.3.0
      All SListI (Code (PTokenName s))
All SListI (Code (PTokenName s)) =>
(PTokenName s -> Rep (PTokenName s))
-> (Rep (PTokenName s) -> PTokenName s) -> Generic (PTokenName s)
Rep (PTokenName s) -> PTokenName s
PTokenName s -> Rep (PTokenName s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PTokenName s))
forall (s :: S). Rep (PTokenName s) -> PTokenName s
forall (s :: S). PTokenName s -> Rep (PTokenName s)
$cfrom :: forall (s :: S). PTokenName s -> Rep (PTokenName s)
from :: PTokenName s -> Rep (PTokenName s)
$cto :: forall (s :: S). Rep (PTokenName s) -> PTokenName s
to :: Rep (PTokenName s) -> PTokenName s
SOP.Generic
    , -- | @since 2.0.0
      (forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName)
-> (forall (s :: S). Term s PTokenName -> Term s PData)
-> PIsData PTokenName
forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
forall (s :: S). Term s PTokenName -> Term s PData
forall (a :: S -> Type).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
pfromDataImpl :: forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
$cpdataImpl :: forall (s :: S). Term s PTokenName -> Term s PData
pdataImpl :: forall (s :: S). Term s PTokenName -> Term s PData
PIsData
    , -- | @since 2.0.0
      (forall (s :: S).
 Term s PTokenName -> Term s PTokenName -> Term s PBool)
-> PEq PTokenName
forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
forall (t :: S -> Type).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
#== :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
PEq
    , -- | @since 2.0.0
      PEq PTokenName
PEq PTokenName =>
(forall (s :: S).
 Term s PTokenName -> Term s PTokenName -> Term s PBool)
-> (forall (s :: S).
    Term s PTokenName -> Term s PTokenName -> Term s PBool)
-> (forall (s :: S).
    Term s PTokenName -> Term s PTokenName -> Term s PTokenName)
-> (forall (s :: S).
    Term s PTokenName -> Term s PTokenName -> Term s PTokenName)
-> POrd PTokenName
forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PTokenName
forall (t :: S -> Type).
PEq t =>
(forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s t)
-> (forall (s :: S). Term s t -> Term s t -> Term s t)
-> POrd t
$c#<= :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
#<= :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
$c#< :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
#< :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
$cpmax :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PTokenName
pmax :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PTokenName
$cpmin :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PTokenName
pmin :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PTokenName
POrd
    , -- | @since 2.0.0
      (forall (s :: S). Bool -> Term s PTokenName -> Term s PString)
-> PShow PTokenName
forall (s :: S). Bool -> Term s PTokenName -> Term s PString
forall (t :: S -> Type).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PTokenName -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PTokenName -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.3.0
      (forall (s :: S). PTokenName s -> Term s (PInner PTokenName))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PTokenName)
    -> (PTokenName s -> Term s b) -> Term s b)
-> PlutusType PTokenName
forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
forall (s :: S) (b :: S -> Type).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
forall (a :: S -> Type).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
pcon' :: forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveNewtypePlutusType PTokenName)

-- Well this is kind of unfortunate, but BuiltinByteString is a thing.

-- | @since 3.3.0
instance PLiftable PTokenName where
  type AsHaskell PTokenName = Plutus.TokenName
  type PlutusRepr PTokenName = ByteString
  {-# INLINEABLE haskToRepr #-}
  haskToRepr :: AsHaskell PTokenName -> PlutusRepr PTokenName
haskToRepr (Plutus.TokenName (PlutusTx.BuiltinByteString ByteString
str)) = ByteString
PlutusRepr PTokenName
str
  {-# INLINEABLE reprToHask #-}
  reprToHask :: PlutusRepr PTokenName -> Either LiftError (AsHaskell PTokenName)
reprToHask = TokenName -> Either LiftError TokenName
forall a b. b -> Either a b
Right (TokenName -> Either LiftError TokenName)
-> (ByteString -> TokenName)
-> ByteString
-> Either LiftError TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinByteString -> TokenName
Plutus.TokenName (BuiltinByteString -> TokenName)
-> (ByteString -> BuiltinByteString) -> ByteString -> TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuiltinByteString
PlutusTx.BuiltinByteString
  {-# INLINEABLE reprToPlut #-}
  reprToPlut :: forall (s :: S). PlutusRepr PTokenName -> PLifted s PTokenName
reprToPlut = PlutusRepr PTokenName -> PLifted s PTokenName
forall (a :: S -> Type) (s :: S).
Includes DefaultUni (PlutusRepr a) =>
PlutusRepr a -> PLifted s a
reprToPlutUni
  {-# INLINEABLE plutToRepr #-}
  plutToRepr :: (forall (s :: S). PLifted s PTokenName)
-> Either LiftError (PlutusRepr PTokenName)
plutToRepr = (forall (s :: S). PLifted s PTokenName)
-> Either LiftError (PlutusRepr PTokenName)
forall (a :: S -> Type).
KnownBuiltinType
  (Term DeBruijn DefaultUni DefaultFun ()) (PlutusRepr a) =>
(forall (s :: S). PLifted s a) -> Either LiftError (PlutusRepr a)
plutToReprUni

-- | @since 3.4.0
instance PTryFrom PData (PAsData PTokenName)

{- | Checks that we have a 'PTokenName' of valid length. The underlying
'PByteString' must not exceed 32 bytes.

@since 3.6.0
-}
instance PValidateData PTokenName where
  pwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
pwithValidated Term s PData
opq Term s r
x =
    Term s PInteger -> (Term s PInteger -> Term s r) -> Term s r
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s (PByteString :--> PInteger)
forall (s :: S). Term s (PByteString :--> PInteger)
plengthBS Term s (PByteString :--> PInteger)
-> Term s PByteString -> Term s PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (PData :--> PByteString)
forall (s :: S). Term s (PData :--> PByteString)
pasByteStr Term s (PData :--> PByteString)
-> Term s PData -> Term s PByteString
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PData
opq) ((Term s PInteger -> Term s r) -> Term s r)
-> (Term s PInteger -> Term s r) -> Term s r
forall a b. (a -> b) -> a -> b
$ \Term s PInteger
bsSize ->
      Term s PBool -> Term s r -> Term s r -> Term s r
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (Term s PInteger
bsSize Term s PInteger -> Term s PInteger -> Term s PBool
forall (s :: S). Term s PInteger -> Term s PInteger -> Term s PBool
forall (t :: S -> Type) (s :: S).
POrd t =>
Term s t -> Term s t -> Term s PBool
#<= Term s PInteger
forall (s :: S). Term s PInteger
ptokenNameByteSizeLimit) Term s r
x Term s r
forall (s :: S) (a :: S -> Type). Term s a
perror

ptokenNameByteSizeLimit :: forall (s :: S). Term s PInteger
ptokenNameByteSizeLimit :: forall (s :: S). Term s PInteger
ptokenNameByteSizeLimit = Term s PInteger
32

{- | The 'PTokenName' of the Ada currency.

@since 2.1.1
-}
padaToken :: Term s PTokenName
padaToken :: forall (s :: S). Term s PTokenName
padaToken = AsHaskell PTokenName -> Term s PTokenName
forall (a :: S -> Type) (s :: S).
PLiftable a =>
AsHaskell a -> Term s a
pconstant AsHaskell PTokenName
TokenName
Plutus.adaToken