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_CreateLootBoardOverlayHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}

  local UIState = deps.UIState
  local ApplyFrameSkin = deps.ApplyFrameSkin
  local RaisePopupFrameAboveEditor = deps.RaisePopupFrameAboveEditor
  local ApplyBoardButtonStyle = deps.ApplyBoardButtonStyle
  local NormalizeSelectionNote = deps.NormalizeSelectionNote
  local WrapColor = deps.WrapColor
  local TrimText = deps.TrimText
  local RenderAuthoritativeItemTooltip = deps.RenderAuthoritativeItemTooltip

  local function EnsureNotePopup()
    if UIState.notePopup then
      return UIState.notePopup
    end

    local frame = CreateFrame("Frame", "LIDCOUNCIL_NotePopup", UIParent, "BackdropTemplate")
    frame:SetSize(360, 210)
    frame:SetPoint("CENTER", 0, 0)
    frame:SetFrameStrata("TOOLTIP")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:SetToplevel(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    ApplyFrameSkin(frame)
    frame:HookScript("OnShow", function(self)
      RaisePopupFrameAboveEditor(self, "TOOLTIP")
      if self.Raise then
        self:Raise()
      end
    end)
    frame:Hide()

    local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 14, -14)
    title:SetText("LidCouncil Note")
    frame.title = title

    local author = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    author:SetPoint("TOPLEFT", 14, -42)
    author:SetWidth(332)
    author:SetJustifyH("LEFT")
    frame.author = author

    local body = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    body:SetPoint("TOPLEFT", 14, -72)
    body:SetWidth(332)
    body:SetJustifyH("LEFT")
    body:SetText("")
    frame.body = body

    local closeBtn = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    closeBtn:SetSize(70, 22)
    closeBtn:SetPoint("BOTTOMRIGHT", -14, 12)
    closeBtn:SetText("Close")
    ApplyBoardButtonStyle(closeBtn)
    closeBtn:SetScript("OnClick", function()
      frame:Hide()
    end)
    frame.closeBtn = closeBtn

    UIState.notePopup = frame
    return frame
  end

  local function ShowSelectionNotePopup(memberName, selection)
    local note = NormalizeSelectionNote(selection and selection.note or "")
    if note == "" then
      return
    end
    local frame = EnsureNotePopup()
    frame.author:SetText(WrapColor("Note from " .. tostring(memberName or "?"), { 0.74, 0.22, 0.30 }))
    frame.body:SetText(note)
    frame:Show()
  end

  local function HideBoardItemTooltip()
    if type(GameTooltip) == "table" then
      GameTooltip:Hide()
    end
  end

  local function ShowBoardItemTooltip(self)
    if not self or type(GameTooltip) ~= "table" then
      return
    end

    local itemLink = TrimText(self.__itemTooltipLink)
    local itemID = self.__itemTooltipItemID
    local itemName = TrimText(self.__itemTooltipName)
    local authoritativeItemLevel = tonumber(self.__itemTooltipItemLevel)
    if itemLink == "" and not itemID and itemName == "" then
      return
    end

    if type(RenderAuthoritativeItemTooltip) == "function" then
      RenderAuthoritativeItemTooltip(GameTooltip, self, "ANCHOR_RIGHT", {
        link = itemLink,
        itemID = itemID,
        name = itemName,
        itemLevel = authoritativeItemLevel,
      })
    else
      GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
      GameTooltip:SetText(itemName ~= "" and itemName or ("Item #" .. tostring(itemID or "?")), 1, 0.82, 0.28, 1, true)
    end
    if self.__hasLocalVote then
      GameTooltip:AddLine("You have voted on this item.", 0.50, 0.82, 1.00, true)
    end
    local awardRecipient = TrimText(self.__awardRecipient)
    if awardRecipient ~= "" then
      GameTooltip:AddLine("Assigned to " .. awardRecipient .. ".", 0.38, 0.88, 0.70, true)
    end
    GameTooltip:Show()
  end

  return {
    ShowSelectionNotePopup = ShowSelectionNotePopup,
    HideBoardItemTooltip = HideBoardItemTooltip,
    ShowBoardItemTooltip = ShowBoardItemTooltip,
  }
end
