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_CreateOutboundRetryHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}
  local TrimText = type(deps.TrimText) == "function" and deps.TrimText or function(value)
    return tostring(value or "")
  end

  local function GetSendAddonResultState(result)
    if result == nil or result == true or result == 0 then
      return "success"
    end
    local sendResultEnum = type(Enum) == "table" and Enum.SendAddonMessageResult or nil
    if type(sendResultEnum) == "table" then
      if result == sendResultEnum.Success then
        return "success"
      end
      if result == sendResultEnum.AddonMessageThrottle
        or result == sendResultEnum.ChannelThrottle
      then
        return "throttle"
      end
      if result == sendResultEnum.AddOnMessageLockdown
        or result == sendResultEnum.AddonMessageLockdown
      then
        return "lockdown"
      end
      if result == sendResultEnum.TargetOffline then
        return "target_offline"
      end
      if result == sendResultEnum.InvalidPrefix
        or result == sendResultEnum.InvalidMessage
        or result == sendResultEnum.InvalidChatType
        or result == sendResultEnum.NotInGroup
        or result == sendResultEnum.TargetRequired
        or result == sendResultEnum.InvalidChannel
        or result == sendResultEnum.NotInGuild
      then
        return "permanent_failure"
      end
    end
    local resultText = tostring(result or "")
    if resultText:find("Success", 1, true) then
      return "success"
    end
    if resultText:find("AddonMessageThrottle", 1, true)
      or resultText:find("ChannelThrottle", 1, true)
      or resultText:find("Throttle", 1, true)
    then
      return "throttle"
    end
    if resultText:find("Lockdown", 1, true) or tonumber(result) == 11 then
      return "lockdown"
    end
    if resultText:find("TargetOffline", 1, true) then
      return "target_offline"
    end
    return "failed"
  end

  local function FormatSendAddonResult(result)
    if result == nil then
      return "nil"
    end
    local sendResultEnum = type(Enum) == "table" and Enum.SendAddonMessageResult or nil
    if type(sendResultEnum) == "table" then
      for _, resultName in ipairs({
        "Success",
        "InvalidPrefix",
        "InvalidMessage",
        "AddonMessageThrottle",
        "InvalidChatType",
        "NotInGroup",
        "TargetRequired",
        "InvalidChannel",
        "ChannelThrottle",
        "NotInGuild",
        "TargetOffline",
        "AddOnMessageLockdown",
      }) do
        if result == sendResultEnum[resultName] then
          return resultName
        end
      end
    end
    if tonumber(result) == 11 then
      return "AddOnMessageLockdown"
    end
    return tostring(result)
  end

  local function ResolveOutboundCommRetryDecision(item, state, limits)
    item = type(item) == "table" and item or {}
    limits = type(limits) == "table" and limits or {}
    local resolvedState = string.lower(TrimText(state))
    if resolvedState == "success" then
      return {
        outcome = "sent",
        retry = false,
        throttled = false,
        failed = false,
      }
    end

    if resolvedState == "lockdown" or resolvedState == "deferred_lockdown" then
      return {
        outcome = "deferred_lockdown",
        retry = true,
        deferred = true,
        lockdown = true,
        throttled = false,
        failed = false,
      }
    end

    if resolvedState == "throttle" or resolvedState == "throttled" then
      item.throttleCount = math.max(0, tonumber(item.throttleCount) or 0) + 1
      local maxThrottleRetries = math.max(0, tonumber(limits.maxThrottleRetries) or 0)
      local shouldRetry = item.throttleCount <= maxThrottleRetries
      return {
        outcome = shouldRetry and "retry_throttled" or "failed_throttled",
        retry = shouldRetry,
        throttled = true,
        failed = not shouldRetry,
      }
    end

    if resolvedState == "permanent_failure" or resolvedState == "target_offline" then
      return {
        outcome = resolvedState,
        retry = false,
        deferred = false,
        lockdown = false,
        throttled = false,
        failed = true,
      }
    end

    item.failureRetryCount = math.max(0, tonumber(item.failureRetryCount) or 0) + 1
    local maxFailureRetries = math.max(0, tonumber(limits.maxFailureRetries) or 0)
    local shouldRetry = item.failureRetryCount <= maxFailureRetries
    return {
      outcome = shouldRetry and "retry_failed" or "failed",
      retry = shouldRetry,
      deferred = false,
      lockdown = false,
      throttled = false,
      failed = not shouldRetry,
    }
  end

  local function ResolveGlobalThrottleCooldownSeconds(streak, baseSeconds, maxSeconds)
    local resolvedStreak = math.max(1, math.floor(tonumber(streak) or 1))
    local resolvedBase = math.max(0.25, tonumber(baseSeconds) or 1.05)
    local resolvedMax = math.max(resolvedBase, tonumber(maxSeconds) or 6)
    return math.min(resolvedMax, resolvedBase + ((resolvedStreak - 1) * 0.35))
  end

  local function ResolveGlobalThrottleRecoveryIntervalSeconds(streak)
    local resolvedStreak = math.max(1, math.floor(tonumber(streak) or 1))
    return math.min(1.25, 0.45 + ((resolvedStreak - 1) * 0.10))
  end

  return {
    GetSendAddonResultState = GetSendAddonResultState,
    FormatSendAddonResult = FormatSendAddonResult,
    ResolveOutboundCommRetryDecision = ResolveOutboundCommRetryDecision,
    ResolveGlobalThrottleCooldownSeconds = ResolveGlobalThrottleCooldownSeconds,
    ResolveGlobalThrottleRecoveryIntervalSeconds = ResolveGlobalThrottleRecoveryIntervalSeconds,
  }
end
