import { auth } from "../auth.js";
import { ThemeService } from "../services/theme.service.js";
import { headers } from "next/headers";
async function getThemeSettings() {
  var _a, _b;
  const activeThemeName = process.env.NEXT_PUBLIC_ACTIVE_THEME || "default";
  const themeConfig = ThemeService.getByName(activeThemeName);
  const appConfig = ThemeService.getAppConfig(activeThemeName);
  const allowUserToggle = ((_b = (_a = appConfig == null ? void 0 : appConfig.ui) == null ? void 0 : _a.theme) == null ? void 0 : _b.allowUserToggle) ?? true;
  const configDefaultMode = (themeConfig == null ? void 0 : themeConfig.defaultMode) || "system";
  if (allowUserToggle) {
    const userTheme = await getUserThemePreference();
    if (userTheme) {
      return { defaultMode: userTheme, allowUserToggle };
    }
  }
  return { defaultMode: configDefaultMode, allowUserToggle };
}
const VALID_THEME_MODES = ["light", "dark", "system"];
function isValidThemeMode(value) {
  return typeof value === "string" && VALID_THEME_MODES.includes(value);
}
async function getUserThemePreference() {
  var _a, _b, _c;
  try {
    const session = await auth.api.getSession({
      headers: await headers()
    });
    if ((_a = session == null ? void 0 : session.user) == null ? void 0 : _a.id) {
      const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:5173";
      const response = await fetch(`${baseUrl}/api/user/profile?includeMeta=true`, {
        headers: {
          cookie: (await headers()).get("cookie") || ""
        },
        cache: "no-store"
      });
      if (response.ok) {
        const data = await response.json();
        const theme = (_c = (_b = data.meta) == null ? void 0 : _b.uiPreferences) == null ? void 0 : _c.theme;
        if (isValidThemeMode(theme)) {
          return theme;
        }
      }
    }
  } catch (error) {
    if (process.env.NODE_ENV === "development") {
      console.warn("[getThemeSettings] Failed to fetch user preference:", error);
    }
  }
  return null;
}
async function getDefaultThemeMode() {
  const { defaultMode } = await getThemeSettings();
  return defaultMode;
}
export {
  getDefaultThemeMode,
  getThemeSettings
};
