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

-- | Tag edits on already validated ledger set encodings.
module Cometa.Internal.SetEncoding (encoding, withEncoding) where

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

import Cometa.Encoding.Set (SetEncoding (..))
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B

-- FUNCTIONS ------------------------------------------------------------------

-- | Observe the outer tag of a validated set array.
encoding :: ByteString -> SetEncoding
encoding :: ByteString -> SetEncoding
encoding ByteString
bytes = case ByteString -> Maybe (Word8, ByteString)
B.uncons ByteString
bytes of
  Just (Word8
header, ByteString
_) | Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
224 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
192 -> SetEncoding
TaggedSet
  Maybe (Word8, ByteString)
_ -> SetEncoding
UntaggedSet

-- | Preserve an unchanged representation exactly. Changed tags retain every byte
-- of the array, including indefinite lengths and nonminimal element encodings.
-- The input must already be validated as an array optionally prefixed by tag 258.
withEncoding :: SetEncoding -> ByteString -> ByteString
withEncoding :: SetEncoding -> ByteString -> ByteString
withEncoding SetEncoding
requested ByteString
bytes
  | SetEncoding
requested SetEncoding -> SetEncoding -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> SetEncoding
encoding ByteString
bytes = ByteString
bytes
  | SetEncoding
requested SetEncoding -> SetEncoding -> Bool
forall a. Eq a => a -> a -> Bool
== SetEncoding
TaggedSet = [Word8] -> ByteString
B.pack [Word8
217, Word8
1, Word8
2] ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bytes
  | Bool
otherwise = Int -> ByteString -> ByteString
B.drop (ByteString -> Int
tagWidth ByteString
bytes) ByteString
bytes

-- | Header width of a validated tag 258, including nonminimal integer widths.
tagWidth :: ByteString -> Int
tagWidth :: ByteString -> Int
tagWidth ByteString
bytes = case ByteString -> Maybe (Word8, ByteString)
B.uncons ByteString
bytes of
  Just (Word8
header, ByteString
_) -> case Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
31 of
    Word8
25 -> Int
3
    Word8
26 -> Int
5
    Word8
27 -> Int
9
    Word8
_ -> Int
0
  Maybe (Word8, ByteString)
Nothing -> Int
0