{-# OPTIONS_GHC -Wno-orphans #-}

module PlutusLedgerApi.V1.Orphans.Address () where

import PlutusLedgerApi.V1 qualified as PLA
import PlutusLedgerApi.V1.Orphans.Credential ()
import Test.QuickCheck (
  Arbitrary (arbitrary, shrink),
  CoArbitrary (coarbitrary),
  Function (function),
  functionMap,
 )

-- | @since 1.0.0
instance Arbitrary PLA.Address where
  {-# INLINEABLE arbitrary #-}
  arbitrary :: Gen Address
arbitrary = Credential -> Maybe StakingCredential -> Address
PLA.Address (Credential -> Maybe StakingCredential -> Address)
-> Gen Credential -> Gen (Maybe StakingCredential -> Address)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Credential
forall a. Arbitrary a => Gen a
arbitrary Gen (Maybe StakingCredential -> Address)
-> Gen (Maybe StakingCredential) -> Gen Address
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Gen (Maybe StakingCredential)
forall a. Arbitrary a => Gen a
arbitrary
  {-# INLINEABLE shrink #-}
  -- As Credential does not shrink, we just pass it through.
  shrink :: Address -> [Address]
shrink (PLA.Address Credential
cred Maybe StakingCredential
scred) = Credential -> Maybe StakingCredential -> Address
PLA.Address Credential
cred (Maybe StakingCredential -> Address)
-> [Maybe StakingCredential] -> [Address]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StakingCredential -> [Maybe StakingCredential]
forall a. Arbitrary a => a -> [a]
shrink Maybe StakingCredential
scred

-- | @since 1.0.0
instance CoArbitrary PLA.Address where
  {-# INLINEABLE coarbitrary #-}
  coarbitrary :: forall b. Address -> Gen b -> Gen b
coarbitrary (PLA.Address Credential
cred Maybe StakingCredential
scred) =
    Credential -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
forall b. Credential -> Gen b -> Gen b
coarbitrary Credential
cred (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe StakingCredential -> Gen b -> Gen b
forall b. Maybe StakingCredential -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Maybe StakingCredential
scred

-- | @since 1.0.0
instance Function PLA.Address where
  {-# INLINEABLE function #-}
  function :: forall b. (Address -> b) -> Address :-> b
function = (Address -> (Credential, Maybe StakingCredential))
-> ((Credential, Maybe StakingCredential) -> Address)
-> (Address -> b)
-> Address :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap Address -> (Credential, Maybe StakingCredential)
into (Credential, Maybe StakingCredential) -> Address
outOf
    where
      into :: PLA.Address -> (PLA.Credential, Maybe PLA.StakingCredential)
      into :: Address -> (Credential, Maybe StakingCredential)
into (PLA.Address Credential
cred Maybe StakingCredential
scred) = (Credential
cred, Maybe StakingCredential
scred)
      outOf :: (PLA.Credential, Maybe PLA.StakingCredential) -> PLA.Address
      outOf :: (Credential, Maybe StakingCredential) -> Address
outOf (Credential
cred, Maybe StakingCredential
scred) = Credential -> Maybe StakingCredential -> Address
PLA.Address Credential
cred Maybe StakingCredential
scred