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

local function CreateEditorWindowStateBridge(deps)
  deps = deps or {}
  local RootDB = deps.RootDB
  if type(RootDB) ~= "function" then
    error("LidCouncil window state bridge missing RootDB dependency.")
  end

  local function ApplyEditorState(frame)
    local ui = RootDB().ui
    ui.editor = ui.editor or {}
    local st = ui.editor

    local screenW = (UIParent and UIParent.GetWidth and UIParent:GetWidth()) or 1920
    local screenH = (UIParent and UIParent.GetHeight and UIParent:GetHeight()) or 1080
    local minW, minH = 520, 340
    local maxW = math.max(minW, math.floor(screenW - 40))
    local maxH = math.max(minH, math.floor(screenH - 40))

    local baseW = math.min(940, math.floor(screenW * 0.75))
    local baseH = math.min(760, math.floor(screenH * 0.75))
    local defaultW = math.floor((baseW * 1.1) + 0.5)
    local defaultH = math.floor((baseH * 1.1) + 0.5)
    local w = tonumber(st.w) or defaultW
    local h = tonumber(st.h) or defaultH

    -- Legacy saved caps made fresh installs effectively non-resizable past a small width.
    st.maxW = nil
    st.maxH = nil
    if w < minW then w = minW end
    if h < minH then h = minH end
    if w > maxW then w = maxW end
    if h > maxH then h = maxH end

    frame:SetSize(w, h)
    if frame.SetMinResize then frame:SetMinResize(minW, minH) end
    if frame.SetMaxResize then frame:SetMaxResize(maxW, maxH) end

    frame:ClearAllPoints()
    if st.point and st.relPoint and st.x and st.y then
      frame:SetPoint(st.point, UIParent, st.relPoint, st.x, st.y)
    else
      frame:SetPoint("CENTER", UIParent, "CENTER", 60, 0)
    end

    if frame.GetLeft and frame.GetRight and frame.GetTop and frame.GetBottom then
      local left, right, top, bottom = frame:GetLeft(), frame:GetRight(), frame:GetTop(), frame:GetBottom()
      if (not left) or (not right) or (not top) or (not bottom)
        or right < 10 or left > (screenW - 10) or top < 10 or bottom > (screenH - 10) then
        frame:ClearAllPoints()
        frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
      end
    end
  end

  local function GetEditorScale()
    local ui = RootDB().ui
    local scale = (ui and ui.editorScale) or 1
    if scale > 1 then scale = 1 end
    if scale < 0.5 then scale = 0.5 end
    return scale
  end

  local function ApplyEditorScale(frame)
    if not frame or not frame.SetScale then return end
    local scale = GetEditorScale()
    frame:SetScale(scale)
    frame.__uiScale = scale
  end

  local function SetDropdownSelection(dropdown, value, text)
    if not dropdown then return end
    if dropdown.__lidcouncilCustomDropdown then
      if value ~= nil and dropdown.SetSelectedValue then dropdown:SetSelectedValue(value) end
      if text ~= nil and dropdown.SetText then dropdown:SetText(text) end
      return
    end
    if value ~= nil then
      UIDropDownMenu_SetSelectedValue(dropdown, value)
    end
    if text ~= nil then
      UIDropDownMenu_SetText(dropdown, text)
    end
  end

  local function ApplyDropdownLimits(editor)
    if not UIDropDownMenu_SetMaxVisibleButtons then return end
    local drops = {
      editor.bgDrop, editor.customBgDrop, editor.borderDrop, editor.dirDrop,
      editor.specDrop, editor.charDrop
    }
    for _, dropdown in ipairs(drops) do
      if dropdown and (not dropdown.__lidcouncilCustomDropdown) then
        UIDropDownMenu_SetMaxVisibleButtons(dropdown, 20)
      end
    end
  end

  local function SaveEditorState(frame)
    local ui = RootDB().ui
    ui.editor = ui.editor or {}
    local st = ui.editor

    local point, _, relPoint, x, y = frame:GetPoint(1)
    st.point, st.relPoint, st.x, st.y = point, relPoint, x, y

    local screenW = (UIParent and UIParent.GetWidth and UIParent:GetWidth()) or 1920
    local screenH = (UIParent and UIParent.GetHeight and UIParent:GetHeight()) or 1080
    local minW, minH = 520, 340
    local maxW = math.max(minW, math.floor(screenW - 40))
    local maxH = math.max(minH, math.floor(screenH - 40))

    local w, h = frame:GetSize()
    w = math.floor((w or 720) + 0.5)
    h = math.floor((h or 520) + 0.5)
    if w < minW then w = minW end
    if h < minH then h = minH end
    if w > maxW then w = maxW end
    if h > maxH then h = maxH end

    st.w, st.h = w, h
  end

  return {
    ApplyEditorState = ApplyEditorState,
    GetEditorScale = GetEditorScale,
    ApplyEditorScale = ApplyEditorScale,
    SetDropdownSelection = SetDropdownSelection,
    ApplyDropdownLimits = ApplyDropdownLimits,
    SaveEditorState = SaveEditorState,
  }
end

INTERNAL_API.LIDCOUNCIL_CreateEditorWindowStateBridge = CreateEditorWindowStateBridge
