window.XiaobaiAPI.configure({
  csrfToken: () => state.csrfToken,
  onUnauthorized: () => showAuthGate("登录状态已失效，请重新登录。"),
});

const applicationShell = window.XiaobaiShell.create({
  state,
  pages: window.XiaobaiPages,
  motionEnabled,
  animateRows,
  refreshIcons,
  tradeDate: () => displayCompactDate(
    state.dashboard?.meta?.trade_date || document.querySelector("#tradeDate")?.value || "",
  ),
  onNavigate: (viewId) => openView(viewId),
  onNavigationSync: () => toggleAccountDropdown(false),
});

const pageModules = window.XiaobaiPageModules.create({
  pages: window.XiaobaiPages,
  actions: {
    closeTransientUi: () => closeStockPreview(),
    applyAccess: () => applyMembershipAccess(),
    clearAuction: () => clearAuctionTimer(),
    stopHeaven: () => {
      stopQiFieldCanvas();
      stopHeartDust();
      cancelHeavenPerformance();
    },
    loadSentiment: () => loadSentimentHistory(),
    loadRotation: () => loadRotationHistory(),
    loadAuction: () => loadAuctionCenter(),
    loadThemes: () => loadThemeLibrary(),
    loadPopularity: () => loadPopularity(),
    loadDragonTiger: () => loadDragonTiger(),
    loadReview: () => loadReviewWorkspace(),
    loadScreener: () => {
      if (hasMemberAccess() && state.dashboard) loadScreenerSetup();
    },
    loadMentor: () => {
      if (hasMemberAccess()) loadMentorSetup();
    },
    loadHeaven: () => {
      if (!hasMemberAccess()) return;
      loadHeavenSetup(false, "", document.querySelector("#heavenStockInput").value.trim());
    },
  },
});


function openModalDialog(dialog) {
  applicationShell.openModalDialog(dialog);
}
