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

local shared = INTERNAL_API.LIDCOUNCIL_LootEditorShared_Impl
if type(shared) ~= "table" then
  error("LidCouncil missing loot editor shared helpers.")
end

local RequireApi = shared.RequireApi
local GetEnabledLootProfileName = shared.GetEnabledLootProfileName
  or RequireApi("LIDCOUNCIL_GetEnabledLootProfileName", _G.LIDCOUNCIL_GetEnabledLootProfileName)
local TrimText = shared.TrimText
local ApplyFrameSkin = shared.ApplyFrameSkin

local function Trim(value)
  if type(TrimText) == "function" then
    return TrimText(value)
  end
  local text = tostring(value or "")
  text = text:gsub("^%s+", "")
  text = text:gsub("%s+$", "")
  return text
end

local function IsEffectivelyVisible(frame)
  if type(frame) ~= "table" then
    return false
  end
  if type(frame.IsVisible) == "function" then
    local ok, visible = pcall(frame.IsVisible, frame)
    if ok then
      return visible == true
    end
  end
  if type(frame.IsShown) == "function" then
    local ok, shown = pcall(frame.IsShown, frame)
    if ok then
      return shown == true
    end
  end
  return false
end

local function BuildLootPresetsPage(editor, page)
  if not editor or not page or page.__lootPresetsBuilt then
    return
  end
  page.__lootPresetsBuilt = true
  page.__contentHeight = 670

  local workspace = CreateFrame("Frame", nil, page, "BackdropTemplate")
  workspace:SetPoint("TOPLEFT", page, "TOPLEFT", 0, 0)
  workspace:SetPoint("BOTTOMRIGHT", page, "BOTTOMRIGHT", 0, 0)
  if type(ApplyFrameSkin) == "function" then
    ApplyFrameSkin(workspace)
  end
  if workspace.SetBackdropColor then
    workspace:SetBackdropColor(0.012, 0.016, 0.028, 0.94)
  end
  if workspace.SetBackdropBorderColor then
    workspace:SetBackdropBorderColor(0.16, 0.20, 0.29, 0.72)
  end
  editor.lootPresetsWorkspace = workspace
  workspace:Hide()

  page:HookScript("OnHide", function()
    workspace:Hide()
  end)
  editor:HookScript("OnHide", function()
    workspace:Hide()
  end)

  local unavailable = page:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  unavailable:SetPoint("CENTER", workspace, "CENTER", 0, 0)
  unavailable:SetWidth(520)
  unavailable:SetJustifyH("CENTER")
  unavailable:SetJustifyV("MIDDLE")
  unavailable:SetTextColor(1.00, 0.42, 0.30, 1.00)
  unavailable:Hide()
  editor.lootPresetsUnavailable = unavailable

  editor.RefreshLootPresetsPage = function(self)
    if tostring(self.currentPage or "") ~= "LootPresets"
      or not IsEffectivelyVisible(self)
      or not IsEffectivelyVisible(page) then
      workspace:Hide()
      unavailable:Hide()
      return false
    end
    local pageStartedAt = INTERNAL_API.LIDCOUNCIL_BeginSyncPerformanceSample_Impl("editor_page_loot_presets", 1)
    local profileName = Trim(type(GetEnabledLootProfileName) == "function" and GetEnabledLootProfileName() or "")
    if profileName == "" then
      workspace:Hide()
      unavailable:SetText("Activate a profile before editing Loot Presets.")
      unavailable:Show()
    elseif type(self.MountLootPresetsRecommendations) == "function" then
      workspace:Show()
      unavailable:Hide()
      if not self:MountLootPresetsRecommendations(workspace, profileName) then
        workspace:Hide()
        unavailable:SetText("Loot Presets are unavailable until the editor finishes loading.")
        unavailable:Show()
      end
    else
      workspace:Hide()
      unavailable:SetText("Loot Presets are loading.")
      unavailable:Show()
    end
    if self.currentPage == "LootPresets" and self.UpdateScrollLayout then
      self:UpdateScrollLayout()
    end
    INTERNAL_API.LIDCOUNCIL_RecordSyncPerformance_Impl("editor_page_loot_presets", pageStartedAt, {
      context = "personal_recommendations",
    })
    return profileName ~= ""
  end
end

INTERNAL_API.LIDCOUNCIL_BuildLootPresetsPage_Impl = BuildLootPresetsPage
