-- 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.
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}

-- | Fresh native inspection results copied before releasing their buffer. The
-- existing owned-buffer runner preserves native status and cleans partial output
-- on normal return, native failure and asynchronous unwind.
module Cometa.Internal.Inspection (inspect, runUnchecked) where

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

import Cometa.Error (Error (..))
import qualified Cometa.Internal.FFI.Inspection as FFI
import qualified Cometa.Internal.Runtime as Runtime
import Data.Aeson (Value, eitherDecodeStrict')
import Data.ByteString (ByteString)
import Data.Text (Text)
import Foreign.C.Types (CUInt)

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

inspect :: Text -> CUInt -> ByteString -> IO (Either Error Value)
inspect :: Text -> CUInt -> ByteString -> IO (Either Error Value)
inspect Text
operation CUInt
kind ByteString
bytes = do
  Either Error ByteString
result <- CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
kind ByteString
bytes
  Either Error Value -> IO (Either Error Value)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Value -> IO (Either Error Value))
-> Either Error Value -> IO (Either Error Value)
forall a b. (a -> b) -> a -> b
$ do
    ByteString
json <- Either Error ByteString
result
    (String -> Either Error Value)
-> (Value -> Either Error Value)
-> Either String Value
-> Either Error Value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error Value -> String -> Either Error Value
forall a b. a -> b -> a
const (Error -> Either Error Value
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
operation))) Value -> Either Error Value
forall a b. b -> Either a b
Right (ByteString -> Either String Value
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict' ByteString
json)

-- | Private malformed-CBOR/operation transport for error and cleanup tests. All
-- native owners still remain scoped through safe foreign return before copying.
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
kind ByteString
bytes = ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes ByteString
bytes ((Ptr Word8 -> CSize -> IO (Either Error ByteString))
 -> IO (Either Error ByteString))
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
input CSize
size ->
  Text
-> (Ptr (Ptr Buffer) -> IO CUInt) -> IO (Either Error ByteString)
Runtime.withOwnedBuffer Text
"inspection.compute" (CUInt -> Ptr Word8 -> CSize -> Ptr (Ptr Buffer) -> IO CUInt
FFI.cCompute CUInt
kind Ptr Word8
input CSize
size)