-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | The runtime's data-plane managers, exported for "Ecluse.Service" and its specs. Importing it opts out of "Ecluse.Service"'s stability promise.
module Ecluse.Service.Internal (
    dataPlaneManagers,
) where

import Network.HTTP.Client (Manager)

import Ecluse.Composition.Plan (BootPlan (bpPrivateConnections, bpPublicConnections, bpValidated))
import Ecluse.Composition.Sizing (newPooledManager)
import Ecluse.Composition.Validate (ValidatedPlan (vpSettings))
import Ecluse.Config (AppConfig (cfgEgress), EgressSettings (egrOperatorContact))
import Ecluse.Core.BuildIdentity (identityManagerSettings, renderUserAgent)
import Ecluse.Core.Fault.Http (typedTlsManagerSettings)

{- | The public and the private data-plane managers, each pooled at its own bound. They stay split
because public reads are anonymous and private reads forward the caller's credential.
-}
dataPlaneManagers :: BootPlan -> IO (Manager, Manager)
dataPlaneManagers :: BootPlan -> IO (Manager, Manager)
dataPlaneManagers BootPlan
bootPlan = (,) (Manager -> Manager -> (Manager, Manager))
-> IO Manager -> IO (Manager -> (Manager, Manager))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO Manager
pooled (BootPlan -> Int
bpPublicConnections BootPlan
bootPlan) IO (Manager -> (Manager, Manager))
-> IO Manager -> IO (Manager, Manager)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> IO Manager
pooled (BootPlan -> Int
bpPrivateConnections BootPlan
bootPlan)
  where
    renderedAgent :: ByteString
renderedAgent = Maybe OperatorContact -> ByteString
renderUserAgent (EgressSettings -> Maybe OperatorContact
egrOperatorContact (AppConfig -> EgressSettings
cfgEgress (ValidatedPlan -> AppConfig
vpSettings (BootPlan -> ValidatedPlan
bpValidated BootPlan
bootPlan))))
    pooled :: Int -> IO Manager
pooled Int
connections = Int -> ManagerSettings -> IO Manager
newPooledManager Int
connections (ByteString -> ManagerSettings -> ManagerSettings
identityManagerSettings ByteString
renderedAgent ManagerSettings
typedTlsManagerSettings)