local EnsureApiRegistry = rawget(_G, "LIDCOUNCIL_EnsureApiRegistry")
local API, INTERNAL_API
if type(EnsureApiRegistry) == "function" then
  API, INTERNAL_API = EnsureApiRegistry()
else
  API = rawget(_G, "LIDCOUNCIL_API")
  if type(API) ~= "table" then
    API = {}
    _G.LIDCOUNCIL_API = API
  end
  if type(API.__internal) ~= "table" then
    API.__internal = {}
  end
  INTERNAL_API = API.__internal
end

INTERNAL_API.LIDCOUNCIL_CreateOutboundArchivePressureHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}
  local stats = type(deps.Stats) == "table" and deps.Stats or {}
  local TrimText = type(deps.TrimText) == "function" and deps.TrimText or function(value)
    return tostring(value or "")
  end
  local GetNow = type(deps.GetNow) == "function" and deps.GetNow or function()
    return 0
  end
  local GetActiveSessionId = type(deps.GetActiveSessionId) == "function" and deps.GetActiveSessionId or function()
    return ""
  end
  local GetQueue = type(deps.GetQueue) == "function" and deps.GetQueue or function()
    return {}
  end
  local ResolveLane = type(deps.ResolveLane) == "function" and deps.ResolveLane or function()
    return "control"
  end
  local ResolveQueueSessionId = type(deps.ResolveQueueSessionId) == "function" and deps.ResolveQueueSessionId
    or function(_, metadata)
      return TrimText(type(metadata) == "table" and metadata.sessionId or "")
    end
  local BuildLanePressure = type(deps.BuildLanePressure) == "function" and deps.BuildLanePressure or function()
    return {}
  end
  local ShouldYieldArchiveForLanePressure = type(deps.ShouldYieldArchiveForLanePressure) == "function"
    and deps.ShouldYieldArchiveForLanePressure
    or function()
      return false, "", ""
    end

  local function ShouldDeferNewArchiveQueueItem(item)
    item = type(item) == "table" and item or {}
    if ResolveLane(item) ~= "archive" then
      return false, "", ""
    end
    local activeSessionId = TrimText(GetActiveSessionId())
    if activeSessionId == "" then
      return false, "", ""
    end
    local now = GetNow()
    local cache = type(stats.archiveYieldPressureCache) == "table" and stats.archiveYieldPressureCache or {}
    stats.archiveYieldPressureCache = cache
    local cacheSeconds = math.max(0.05, tonumber(stats.archiveYieldPressureCacheSeconds) or 0.20)
    if TrimText(cache.sessionId) == activeSessionId
      and now - math.max(0, tonumber(cache.at) or 0) < cacheSeconds
    then
      return cache.shouldYield == true, TrimText(cache.lane), TrimText(cache.severity)
    end

    local laneTelemetry = {}
    for _, lane in ipairs({ "live", "bootstrap", "archive", "stats", "control" }) do
      laneTelemetry[lane] = {
        queuedNow = 0,
      }
    end
    local queuedByLane = {}
    for _, queued in ipairs(GetQueue()) do
      local kind = TrimText(type(queued) == "table" and queued.kind or "")
      local metadata = type(queued) == "table" and type(queued.metadata) == "table" and queued.metadata or {}
      local queuedSessionId = ResolveQueueSessionId(kind, metadata)
      if queuedSessionId == "" or queuedSessionId == activeSessionId then
        local lane = TrimText(ResolveLane(queued))
        if lane ~= "" then
          queuedByLane[lane] = math.max(0, tonumber(queuedByLane[lane]) or 0) + 1
        end
      end
    end
    for _, lane in ipairs({ "live", "bootstrap", "archive", "stats", "control" }) do
      local entry = type(laneTelemetry[lane]) == "table" and laneTelemetry[lane] or {}
      entry.queuedNow = math.max(0, tonumber(queuedByLane[lane]) or 0)
      laneTelemetry[lane] = entry
    end
    local lanePressure = BuildLanePressure(laneTelemetry, stats.lanePressureThresholds)
    local shouldYield, lane, severity = ShouldYieldArchiveForLanePressure(lanePressure)
    cache.at = now
    cache.sessionId = activeSessionId
    cache.shouldYield = shouldYield == true
    cache.lane = TrimText(lane)
    cache.severity = TrimText(severity)
    return cache.shouldYield, cache.lane, cache.severity
  end

  return {
    ShouldDeferNewArchiveQueueItem = ShouldDeferNewArchiveQueueItem,
  }
end
