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

  local ApplyFrameSkin = deps.ApplyFrameSkin
  local ApplyLootListToolbarButtonStyle = deps.ApplyLootListToolbarButtonStyle
  local GetLootListViewMode = deps.GetLootListViewMode

  local function CaptureFrameAnchorData(frame)
    local anchors = {}
    if not frame or type(frame.GetNumPoints) ~= "function" then
      return anchors
    end
    for index = 1, frame:GetNumPoints() do
      local point, relativeTo, relativePoint, xOfs, yOfs = frame:GetPoint(index)
      anchors[#anchors + 1] = {
        point = point,
        relativeTo = relativeTo,
        relativePoint = relativePoint,
        xOfs = xOfs,
        yOfs = yOfs,
      }
    end
    return anchors
  end

  local function RestoreFrameAnchorData(frame, anchors)
    if not frame then
      return
    end
    frame:ClearAllPoints()
    for _, anchor in ipairs(anchors or {}) do
      frame:SetPoint(anchor.point, anchor.relativeTo, anchor.relativePoint, anchor.xOfs, anchor.yOfs)
    end
  end

  local function RestoreLootListDetailsPane(editor)
    if not editor or not editor.lootListRightBg then
      return
    end

    local pane = editor.lootListRightBg
    if editor.__lootLedgerPopupOriginalParent then
      pane:SetParent(editor.__lootLedgerPopupOriginalParent)
      RestoreFrameAnchorData(pane, editor.__lootLedgerPopupOriginalPoints)
    end

    editor.__lootLedgerPopupOpen = nil
    if editor.RefreshLootListPage then
      editor:RefreshLootListPage()
    else
      pane:SetShown(GetLootListViewMode() == "DETAILS")
    end
  end

  local function EnsureLootListDetailsPopup(editor)
    if editor and editor.lootLedgerPopupFrame then
      return editor.lootLedgerPopupFrame
    end
    if not editor then
      return nil
    end

    local frame = CreateFrame("Frame", "LIDCOUNCIL_LootListPopup", UIParent, "BackdropTemplate")
    frame:SetSize(760, 720)
    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:SetScript("OnHide", function()
      RestoreLootListDetailsPane(editor)
    end)
    frame:Hide()

    local closeBtn = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate,BackdropTemplate")
    closeBtn:SetSize(78, 22)
    closeBtn:SetPoint("TOPRIGHT", -14, -14)
    closeBtn:SetText("Close")
    ApplyLootListToolbarButtonStyle(closeBtn)
    closeBtn:SetScript("OnClick", function()
      frame:Hide()
    end)
    frame.closeBtn = closeBtn

    editor.lootLedgerPopupFrame = frame
    return frame
  end

  local function OpenLootListDetailsPopup(editor, row)
    if not editor or type(row) ~= "table" then
      return
    end

    local frame = EnsureLootListDetailsPopup(editor)
    local pane = editor.lootListRightBg
    if not frame or not pane then
      return
    end
    editor.selectedLootLedgerType = row.type
    editor.selectedLootLedgerKey = row.character
    editor.selectedLootLedgerCharacter = row.character

    if not editor.__lootLedgerPopupOriginalParent or pane:GetParent() ~= frame then
      if pane:GetParent() ~= frame then
        editor.__lootLedgerPopupOriginalParent = pane:GetParent()
        editor.__lootLedgerPopupOriginalPoints = CaptureFrameAnchorData(pane)
      end
    end

    editor.__lootLedgerPopupOpen = true
    pane:SetParent(frame)
    pane:ClearAllPoints()
    pane:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -44)
    pane:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -14, 14)
    pane:Show()

    frame:Show()
    if frame.Raise then
      frame:Raise()
    end
    if editor.RefreshLootListPage then
      editor:RefreshLootListPage()
    end
  end

  return {
    OpenLootListDetailsPopup = OpenLootListDetailsPopup,
  }
end
