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 shared = INTERNAL_API.LIDCOUNCIL_LootRuntimeShared_Impl
if type(shared) ~= "table" then
  error("LidCouncil missing loot runtime shared helpers.")
end

local simulation = INTERNAL_API.LIDCOUNCIL_LootRuntimeSimulation_Impl
if type(simulation) ~= "table" then
  error("LidCouncil missing loot runtime simulation helpers.")
end

local GetLootProfile = shared.GetLootProfile
local GetActiveLootSession = shared.GetActiveLootSession
local GetLootProfileAccessInfo = shared.GetLootProfileAccessInfo
local GetLootProfileAccessInfoForRole = shared.GetLootProfileAccessInfoForRole
local ResolveLootPointValue = shared.ResolveLootPointValue
local ResolveLootAwardOutcome = shared.ResolveLootAwardOutcome
local ApplyFrameSkin = shared.ApplyFrameSkin
local SendSessionComm = shared.SendSessionComm
local ResolveOptionalFunction = shared.ResolveOptionalFunction
local Print = shared.Print
local DeepCopy = shared.DeepCopy
local DEFAULT_OPTIONS = shared.DEFAULT_OPTIONS
local NOTE_ICON_TEXTURE = shared.NOTE_ICON_TEXTURE
local MAX_SELECTION_NOTE_LENGTH = shared.MAX_SELECTION_NOTE_LENGTH
local REAL_SIMULATION_ITEM_COUNT = shared.REAL_SIMULATION_ITEM_COUNT
local runtime = shared.RuntimeState
local uiState = shared.UIState
local TrimText = shared.TrimText
local IsRealSimulationItemKey = shared.IsRealSimulationItemKey
local ExtractItemID = shared.ExtractItemID
local NormalizeSelectionNote = shared.NormalizeSelectionNote
local RefreshEditor = shared.RefreshEditor
local WrapColor = shared.WrapColor
local ResolveOptionIconTexture = shared.ResolveOptionIconTexture
local ResolveRollOptions = shared.ResolveRollOptions
local GetSelfKey = shared.GetSelfKey
local BuildMemberDisplayText = shared.BuildMemberDisplayText
local IsSelfHost = shared.IsSelfHost
local GetJoinedSessionState = shared.GetJoinedSessionState
local GetBroadcastDistribution = shared.GetBroadcastDistribution
local BuildPendingSelection = shared.BuildPendingSelection
local EnsurePreviewBoardContext = shared.EnsurePreviewBoardContext
local PersistRuntimeBoardHistory = shared.PersistRuntimeBoardHistory
local PersistPreviewBoardHistory = shared.PersistPreviewBoardHistory
local GetLiveBoardHistoryEntries = shared.GetLiveBoardHistoryEntries
local RestoreLatestPreviewBoardState = shared.RestoreLatestPreviewBoardState
local ResolveLootLedgerTotals = shared.ResolveLootLedgerTotals
local ClearRuntimeState = shared.ClearRuntimeState
local EnsureRuntimeSession = shared.EnsureRuntimeSession
local EnsureSelectedMember = shared.EnsureSelectedMember
local CycleSelectedMember = shared.CycleSelectedMember
local BuildAwardNote = shared.BuildAwardNote
local RemoveAwardFromEntry = shared.RemoveAwardFromEntry
local ApplyAwardMessage = shared.ApplyAwardMessage
local RefreshBoardAndEditor = shared.RefreshBoardAndEditor
local RecordChoice = shared.RecordChoice
local GetLiveLootBoardSummary = shared.GetLiveLootBoardSummary
local GetEntryVoteTargetForVoter = shared.GetEntryVoteTargetForVoter
local SetEntryVoteTarget = shared.SetEntryVoteTarget
local GetEntryVoteVoterKeys = shared.GetEntryVoteVoterKeys
local GetEntryVoteCount = shared.GetEntryVoteCount

local BuildPreviewBoardState = simulation.BuildPreviewBoardState
local BuildSimulationBoardState = simulation.BuildSimulationBoardState
local BuildRealSimulationItemDefs = shared.BuildRealSimulationItemDefs
local GetSelectionPriority = simulation.GetSelectionPriority
local FormatSelectionLabel = simulation.FormatSelectionLabel
local EnsurePreviewSelectedMember = simulation.EnsurePreviewSelectedMember
local CyclePreviewSelectedMember = simulation.CyclePreviewSelectedMember
local ApplySimulationAward = simulation.ApplySimulationAward
local ApplySimulationChoice = simulation.ApplySimulationChoice

local function GetBoardPreviewState(frame)
  if type(uiState.previewState) == "table" then
    return uiState.previewState
  end
  local framePreview = type(frame) == "table" and frame.__previewState or nil
  if type(framePreview) == "table" then
    return framePreview
  end

  local activeSession = GetActiveLootSession()
  if activeSession then
    return nil
  end

  local _, currentProfileName = GetLootProfile(nil)
  local restoredPreview = type(RestoreLatestPreviewBoardState) == "function" and RestoreLatestPreviewBoardState(currentProfileName) or nil
  if type(restoredPreview) == "table" then
    uiState.previewState = restoredPreview
    if type(frame) == "table" then
      frame.__previewState = restoredPreview
    end
    return restoredPreview
  end
  return nil
end

local function SetBoardPreviewState(frame, preview)
  uiState.previewState = type(preview) == "table" and preview or nil
  if frame then
    frame.__previewState = uiState.previewState
  end
end

local function PersistBoardState(frame, opts)
  local preview = GetBoardPreviewState(frame)
  if type(preview) == "table" then
    EnsurePreviewBoardContext(preview)
    PersistPreviewBoardHistory(preview)
  else
    PersistRuntimeBoardHistory(opts)
  end
end

local function ResolveBoardProfileName(frame, preview)
  preview = type(preview) == "table" and preview or GetBoardPreviewState(frame)
  if type(preview) == "table" then
    EnsurePreviewBoardContext(preview)
    return TrimText(preview.historyProfileName ~= "" and preview.historyProfileName or preview.profileName)
  end
  return TrimText(runtime.historyProfileName ~= "" and runtime.historyProfileName or runtime.profileName)
end

INTERNAL_API.LIDCOUNCIL_LootRuntimeUiShared_Impl = {
  GetLootProfile = GetLootProfile,
  GetActiveLootSession = GetActiveLootSession,
  GetLootProfileAccessInfo = GetLootProfileAccessInfo,
  GetLootProfileAccessInfoForRole = GetLootProfileAccessInfoForRole,
  ResolveLootPointValue = ResolveLootPointValue,
  ResolveLootAwardOutcome = ResolveLootAwardOutcome,
  ApplyFrameSkin = ApplyFrameSkin,
  SendSessionComm = SendSessionComm,
  ResolveOptionalFunction = ResolveOptionalFunction,
  Print = Print,
  DeepCopy = DeepCopy,
  DEFAULT_OPTIONS = DEFAULT_OPTIONS,
  NOTE_ICON_TEXTURE = NOTE_ICON_TEXTURE,
  MAX_SELECTION_NOTE_LENGTH = MAX_SELECTION_NOTE_LENGTH,
  REAL_SIMULATION_ITEM_COUNT = REAL_SIMULATION_ITEM_COUNT,
  RuntimeState = runtime,
  UIState = uiState,
  TrimText = TrimText,
  IsRealSimulationItemKey = IsRealSimulationItemKey,
  ExtractItemID = ExtractItemID,
  NormalizeSelectionNote = NormalizeSelectionNote,
  RefreshEditor = RefreshEditor,
  WrapColor = WrapColor,
  ResolveOptionIconTexture = ResolveOptionIconTexture,
  ResolveRollOptions = ResolveRollOptions,
  GetSelfKey = GetSelfKey,
  BuildMemberDisplayText = BuildMemberDisplayText,
  BuildPendingSelection = BuildPendingSelection,
  IsSelfHost = IsSelfHost,
  GetJoinedSessionState = GetJoinedSessionState,
  GetBroadcastDistribution = GetBroadcastDistribution,
  EnsurePreviewBoardContext = EnsurePreviewBoardContext,
  PersistRuntimeBoardHistory = PersistRuntimeBoardHistory,
  PersistPreviewBoardHistory = PersistPreviewBoardHistory,
  GetLiveBoardHistoryEntries = GetLiveBoardHistoryEntries,
  ResolveLootLedgerTotals = ResolveLootLedgerTotals,
  ClearRuntimeState = ClearRuntimeState,
  EnsureRuntimeSession = EnsureRuntimeSession,
  EnsureSelectedMember = EnsureSelectedMember,
  CycleSelectedMember = CycleSelectedMember,
  BuildAwardNote = BuildAwardNote,
  RemoveAwardFromEntry = RemoveAwardFromEntry,
  ApplyAwardMessage = ApplyAwardMessage,
  RefreshBoardAndEditor = RefreshBoardAndEditor,
  RecordChoice = RecordChoice,
  GetLiveLootBoardSummary = GetLiveLootBoardSummary,
  GetEntryVoteTargetForVoter = GetEntryVoteTargetForVoter,
  SetEntryVoteTarget = SetEntryVoteTarget,
  GetEntryVoteVoterKeys = GetEntryVoteVoterKeys,
  GetEntryVoteCount = GetEntryVoteCount,
  BuildPreviewBoardState = BuildPreviewBoardState,
  BuildSimulationBoardState = BuildSimulationBoardState,
  BuildRealSimulationItemDefs = BuildRealSimulationItemDefs,
  GetSelectionPriority = GetSelectionPriority,
  FormatSelectionLabel = FormatSelectionLabel,
  EnsurePreviewSelectedMember = EnsurePreviewSelectedMember,
  CyclePreviewSelectedMember = CyclePreviewSelectedMember,
  ApplySimulationAward = ApplySimulationAward,
  ApplySimulationChoice = ApplySimulationChoice,
  GetBoardPreviewState = GetBoardPreviewState,
  SetBoardPreviewState = SetBoardPreviewState,
  PersistBoardState = PersistBoardState,
  ResolveBoardProfileName = ResolveBoardProfileName,
}

local function CallUiImpl(name, ...)
  local fn = INTERNAL_API[name]
  if type(fn) ~= "function" then
    error("LidCouncil missing loot runtime UI implementation: " .. tostring(name))
  end
  return fn(...)
end

INTERNAL_API.LIDCOUNCIL_LootRuntimeUi_Impl = {
  OpenLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_OpenLiveLootBoard_Impl", ...)
  end,
  CloseLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_CloseLiveLootBoard_Impl", ...)
  end,
  AttachLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_AttachLiveLootBoard_Impl", ...)
  end,
  AttachLiveLootBoardHistory = function(...)
    return CallUiImpl("LIDCOUNCIL_AttachLiveLootBoardHistory_Impl", ...)
  end,
  PopoutLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_PopoutLiveLootBoard_Impl", ...)
  end,
  PreviewLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_PreviewLiveLootBoard_Impl", ...)
  end,
  RefreshLiveLootBoard = function(...)
    return CallUiImpl("LIDCOUNCIL_RefreshLiveLootBoard_Impl", ...)
  end,
  PreviewRollChoicePopup = function(...)
    return CallUiImpl("LIDCOUNCIL_PreviewRollChoicePopup_Impl", ...)
  end,
  RunLiveLootSimulation = function(...)
    return CallUiImpl("LIDCOUNCIL_RunLiveLootSimulation_Impl", ...)
  end,
  RunLiveLootRealSimulation = function(...)
    return CallUiImpl("LIDCOUNCIL_RunLiveLootRealSimulation_Impl", ...)
  end,
  RefreshChoicePopup = function(...)
    return CallUiImpl("LIDCOUNCIL_RefreshChoicePopup_Impl", ...)
  end,
}
