-- 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 #-}

-- | Private adapter scopes used by native builder integration and dispatch tests.
-- Public direct selection does not acquire a callback registration. Factories and
-- native invokes run inside the shared operation gate, which contains callback
-- exceptions and joins cancellation before releasing native or Haskell owners.
module Cometa.Internal.CoinSelection.Native (withSelector, invoke, invokeWith, dispatch) where

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

import Cometa.Error (Error (..))
import Cometa.Internal.CoinSelection (Selection, SelectionRequest, Selector)
import qualified Cometa.Internal.CoinSelection as Selection
import qualified Cometa.Internal.FFI.CoinSelection as Native
import Cometa.Internal.Runtime (Dispatch, Runtime)
import qualified Cometa.Internal.Runtime as Runtime

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

withSelector :: Selector -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
withSelector :: forall a.
Selector -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
withSelector Selector
selector = Dispatch -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
forall a.
Dispatch -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withRuntime (Selector -> Dispatch
dispatch Selector
selector)

dispatch :: Selector -> Dispatch
dispatch :: Selector -> Dispatch
dispatch Selector
selector Word32
operation ByteString
bytes
  | Word32
operation Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
200 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Text -> Error
InvalidSelectionRequest Text
"Unknown selector operation"))
  | Bool
otherwise = case ByteString -> Either Error SelectionRequest
Selection.decodeRequest ByteString
bytes of
      Left Error
failure -> Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
failure)
      Right SelectionRequest
request -> (Selection -> ByteString)
-> Either Error Selection -> Either Error ByteString
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Selection -> ByteString
Selection.encodeSelection (Either Error Selection -> Either Error ByteString)
-> IO (Either Error Selection) -> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Selector -> SelectionRequest -> IO (Either Error Selection)
Selection.select Selector
selector SelectionRequest
request

invoke :: Selector -> SelectionRequest -> IO (Either Error Selection)
invoke :: Selector -> SelectionRequest -> IO (Either Error Selection)
invoke Selector
selector SelectionRequest
request = Selector
-> (Runtime -> IO (Either Error Selection))
-> IO (Either Error Selection)
forall a.
Selector -> (Runtime -> IO (Either Error a)) -> IO (Either Error a)
withSelector Selector
selector (Runtime -> SelectionRequest -> IO (Either Error Selection)
`invokeWith` SelectionRequest
request)

invokeWith :: Runtime -> SelectionRequest -> IO (Either Error Selection)
invokeWith :: Runtime -> SelectionRequest -> IO (Either Error Selection)
invokeWith Runtime
runtime SelectionRequest
request = do
  Either Error ByteString
response <- Runtime
-> (Ptr Context -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a.
Runtime
-> (Ptr Context -> IO (Either Error a)) -> IO (Either Error a)
Runtime.withOperation Runtime
runtime ((Ptr Context -> IO (Either Error ByteString))
 -> IO (Either Error ByteString))
-> (Ptr Context -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr Context
context ->
    ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes (SelectionRequest -> ByteString
Selection.encodeRequest SelectionRequest
request) ((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
bytes CSize
size ->
      Text
-> (Ptr (Ptr Buffer) -> IO NativeCode)
-> IO (Either Error ByteString)
Runtime.withOwnedBuffer Text
"coinSelection.nativeDispatch" (Ptr Context
-> Ptr Word8 -> CSize -> Ptr (Ptr Buffer) -> IO NativeCode
Native.cSelect Ptr Context
context Ptr Word8
bytes CSize
size)
  Either Error Selection -> IO (Either Error Selection)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ByteString
response Either Error ByteString
-> (ByteString -> Either Error Selection) -> Either Error Selection
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error Selection
Selection.decodeSelection Either Error Selection
-> (Selection -> Either Error Selection) -> Either Error Selection
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Selection -> Either Error Selection
checked)
  where
    checked :: Selection -> Either Error Selection
checked Selection
value = SelectionRequest -> Selection -> Either Error ()
Selection.validateSelection SelectionRequest
request Selection
value Either Error () -> Either Error Selection -> Either Error Selection
forall a b. Either Error a -> Either Error b -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Selection -> Either Error Selection
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Selection
value