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

-- | Four-bit Shelley network identifiers, distinct from network magic.
--
-- @
-- import qualified Cometa.NetworkId as Network
--
-- network :: Network.NetworkId
-- network = Network.testnet
-- @
module Cometa.NetworkId (NetworkId, mkNetworkId, networkIdValue, mainnet, testnet) where

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

import Cometa.Error (Error (InvalidNetworkId))
import Data.Word (Word8)

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

-- | Four-bit network tag, 0 through 15. Tag 1 denotes mainnet; every other
-- tag uses the test-network Bech32 prefix. This tag is not a network magic.
-- Equality and ordering compare the number; 'Show' is diagnostic.
-- 'minBound' is 0 and 'maxBound' is 15.
newtype NetworkId = NetworkId Word8 deriving (NetworkId -> NetworkId -> Bool
(NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> Bool) -> Eq NetworkId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NetworkId -> NetworkId -> Bool
== :: NetworkId -> NetworkId -> Bool
$c/= :: NetworkId -> NetworkId -> Bool
/= :: NetworkId -> NetworkId -> Bool
Eq, Eq NetworkId
Eq NetworkId =>
(NetworkId -> NetworkId -> Ordering)
-> (NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> NetworkId)
-> (NetworkId -> NetworkId -> NetworkId)
-> Ord NetworkId
NetworkId -> NetworkId -> Bool
NetworkId -> NetworkId -> Ordering
NetworkId -> NetworkId -> NetworkId
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 :: NetworkId -> NetworkId -> Ordering
compare :: NetworkId -> NetworkId -> Ordering
$c< :: NetworkId -> NetworkId -> Bool
< :: NetworkId -> NetworkId -> Bool
$c<= :: NetworkId -> NetworkId -> Bool
<= :: NetworkId -> NetworkId -> Bool
$c> :: NetworkId -> NetworkId -> Bool
> :: NetworkId -> NetworkId -> Bool
$c>= :: NetworkId -> NetworkId -> Bool
>= :: NetworkId -> NetworkId -> Bool
$cmax :: NetworkId -> NetworkId -> NetworkId
max :: NetworkId -> NetworkId -> NetworkId
$cmin :: NetworkId -> NetworkId -> NetworkId
min :: NetworkId -> NetworkId -> NetworkId
Ord, Int -> NetworkId -> ShowS
[NetworkId] -> ShowS
NetworkId -> String
(Int -> NetworkId -> ShowS)
-> (NetworkId -> String)
-> ([NetworkId] -> ShowS)
-> Show NetworkId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NetworkId -> ShowS
showsPrec :: Int -> NetworkId -> ShowS
$cshow :: NetworkId -> String
show :: NetworkId -> String
$cshowList :: [NetworkId] -> ShowS
showList :: [NetworkId] -> ShowS
Show)

instance Bounded NetworkId where
  minBound :: NetworkId
minBound = Word8 -> NetworkId
NetworkId Word8
0
  maxBound :: NetworkId
maxBound = Word8 -> NetworkId
NetworkId Word8
15

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

-- | Validate a network tag. Values outside 0..15 return 'InvalidNetworkId'.
mkNetworkId :: Integer -> Either Error NetworkId
mkNetworkId :: Integer -> Either Error NetworkId
mkNetworkId Integer
value
  | Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
15 = Error -> Either Error NetworkId
forall a b. a -> Either a b
Left (Integer -> Error
InvalidNetworkId Integer
value)
  | Bool
otherwise = NetworkId -> Either Error NetworkId
forall a b. b -> Either a b
Right (Word8 -> NetworkId
NetworkId (Integer -> Word8
forall a. Num a => Integer -> a
fromInteger Integer
value))

-- | Numeric four-bit tag.
networkIdValue :: NetworkId -> Word8
networkIdValue :: NetworkId -> Word8
networkIdValue (NetworkId Word8
value) = Word8
value

-- | Mainnet tag 1.
mainnet :: NetworkId
mainnet :: NetworkId
mainnet = Word8 -> NetworkId
NetworkId Word8
1

-- | Conventional test-network tag 0. Does not distinguish preview or preprod.
testnet :: NetworkId
testnet :: NetworkId
testnet = Word8 -> NetworkId
NetworkId Word8
0