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 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("FULLSCREEN_DIALOG")
    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, "FULLSCREEN_DIALOG")
    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)
    if itemLink == "" and not itemID and itemName == "" then
      return
    end

    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
    if itemLink ~= "" and GameTooltip.SetHyperlink then
      GameTooltip:SetHyperlink(itemLink)
    elseif itemID and GameTooltip.SetItemByID then
      GameTooltip:SetItemByID(itemID)
    else
      GameTooltip:SetText(itemName ~= "" and itemName or ("Item #" .. tostring(itemID or "?")), 1, 0.82, 0.28, 1, true)
    end
    GameTooltip:Show()
  end

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