local API = rawget(_G, "LIDCOUNCIL_API")
local INTERNAL_API = type(API) == "table" and type(API.__internal) == "table" and API.__internal or nil
if type(INTERNAL_API) ~= "table" then
  error("LidCouncil missing internal API registry.")
end

INTERNAL_API.LIDCOUNCIL_CreateLootSessionAutoPassHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}

  local TrimText = deps.TrimText
  local ResolveOptionalApiFunction = deps.ResolveOptionalApiFunction
  local ApplyTrafficLight = deps.ApplyTrafficLight

  local function RefreshAutoPassView(editor, context)
    if not editor then
      return
    end

    context = type(context) == "table" and context or {}
    local activeSession = context.activeSession
    local selfCharacter = TrimText(context.selfCharacter)

    local getLocalAutoPassEnabled = ResolveOptionalApiFunction("LIDCOUNCIL_GetLocalAutoPassEnabled")
    local localAutoPassEnabled = type(getLocalAutoPassEnabled) == "function" and (getLocalAutoPassEnabled() and true or false) or true
    if activeSession and type(activeSession.members) == "table" then
      local selfMember = activeSession.members[selfCharacter]
      if type(selfMember) == "table" and selfMember.localAutoPassEnabled ~= nil then
        localAutoPassEnabled = selfMember.localAutoPassEnabled and true or false
      end
    end

    if editor.lootSessionTitle then
      editor.lootSessionTitle:SetText("Personal Auto Pass")
    end
    if editor.lootSessionStatus then
      if not activeSession then
        editor.lootSessionStatus:SetText("No active session | Auto Pass is OFF")
        editor.lootSessionStatus:SetTextColor(1.00, 0.32, 0.28, 1.00)
        ApplyTrafficLight(editor.lootSessionStatusIndicator, "DECLINED")
      elseif localAutoPassEnabled then
        editor.lootSessionStatus:SetText("Auto Pass activated. You will automatically pass on all items and only the LidCouncil roll window will appear.")
        editor.lootSessionStatus:SetTextColor(0.30, 0.90, 0.36, 1.00)
        ApplyTrafficLight(editor.lootSessionStatusIndicator, "SYNCED")
      else
        editor.lootSessionStatus:SetText("Auto Pass deactivated. You will receive the Blizzard loot roll window and the LidCouncil roll window at the same time.")
        editor.lootSessionStatus:SetTextColor(1.00, 0.32, 0.28, 1.00)
        ApplyTrafficLight(editor.lootSessionStatusIndicator, "DECLINED")
      end
    end

    if editor.lootSessionAutoPassDesc then
      editor.lootSessionAutoPassDesc:SetText(
        "Auto Pass is a |cff4adf6bpersonal setting|r.\n" ..
        "- When you |cff4adf6bstart|r a session as the host, your personal Auto Pass defaults to |cffff5a5aOFF|r.\n" ..
        "- When you |cff4adf6bjoin|r a session, your personal Auto Pass defaults to |cff4adf6bON|r.\n" ..
        "- Starting or accepting a session applies those defaults again for that session.\n" ..
        "- You can change your own setting at any time in this page.\n\n" ..
        "If your personal Auto Pass is |cff4adf6bON|r, LidCouncil will auto-pass Blizzard loot rolls on your client.\n" ..
        "If your personal Auto Pass is |cffff5a5aOFF|r, you keep Blizzard's normal loot roll window."
      )
    end
    if editor.lootSessionAutoPassCurrent then
      editor.lootSessionAutoPassCurrent:SetText("")
    end
    if editor.sessionAutoPassLocalBtn then
      editor.sessionAutoPassLocalBtn:SetText(localAutoPassEnabled and "Deactivate Auto-Pass" or "Activate Auto-Pass")
      if editor.sessionAutoPassLocalBtn.Enable then
        editor.sessionAutoPassLocalBtn:Enable()
      end
    end
    if editor.lootSessionAutoPassState then
      editor.lootSessionAutoPassState:SetText(
        "|cffff5a5aOFF|r - Blizzard's Loot Window will appear as usual together with the LidCouncil Loot Window.\n" ..
        "|cff4adf6bON|r - You will pass on all items automatically. Only LidCouncil's Loot Window will appear."
      )
    end

    if editor.lootSessionBody then
      editor.lootSessionBody:SetText("")
      editor.lootSessionBody:Hide()
    end
  end

  local function LayoutAutoPassSection(editor, page, rightContentWidth)
    if not editor then
      return 220
    end

    if editor.sessionSyncBoardBtn then
      editor.sessionSyncBoardBtn:Hide()
    end
    if editor.lootSessionAutoPassCurrent then
      editor.lootSessionAutoPassCurrent:Hide()
    end

    local statusHeight = math.max(18, math.ceil((editor.lootSessionStatus and editor.lootSessionStatus:GetStringHeight()) or 0))
    local buttonTop = -48 - statusHeight - 12
    if editor.sessionAutoPassLocalBtn then
      editor.sessionAutoPassLocalBtn:ClearAllPoints()
      editor.sessionAutoPassLocalBtn:SetPoint("TOPLEFT", 18, buttonTop)
    end

    local stateTop = buttonTop - 34
    if editor.lootSessionAutoPassState then
      editor.lootSessionAutoPassState:ClearAllPoints()
      editor.lootSessionAutoPassState:SetPoint("TOPLEFT", 18, stateTop)
      editor.lootSessionAutoPassState:SetWidth(rightContentWidth)
    end
    local stateHeight = math.max(38, math.ceil((editor.lootSessionAutoPassState and editor.lootSessionAutoPassState:IsShown() and editor.lootSessionAutoPassState:GetStringHeight() or 0)))

    local notesHeaderTop = stateTop - stateHeight - 18
    if editor.lootSessionAutoPassHeader then
      editor.lootSessionAutoPassHeader:ClearAllPoints()
      editor.lootSessionAutoPassHeader:SetPoint("TOPLEFT", 18, notesHeaderTop)
    end

    local notesTop = notesHeaderTop - 28
    if editor.lootSessionAutoPassDesc then
      editor.lootSessionAutoPassDesc:ClearAllPoints()
      editor.lootSessionAutoPassDesc:SetPoint("TOPLEFT", 18, notesTop)
      editor.lootSessionAutoPassDesc:SetWidth(rightContentWidth)
    end
    local descHeight = math.max(84, math.ceil((editor.lootSessionAutoPassDesc and editor.lootSessionAutoPassDesc:IsShown() and editor.lootSessionAutoPassDesc:GetStringHeight() or 0)))

    local contentBottom = math.abs(notesTop) + descHeight + 24
    if page then
      page.__contentHeight = math.max(420, contentBottom)
    end
    return contentBottom
  end

  return {
    RefreshAutoPassView = RefreshAutoPassView,
    LayoutAutoPassSection = LayoutAutoPassSection,
  }
end
