import { DEFAULT_APP_CONFIG } from "./app.config";
import { DEFAULT_DASHBOARD_CONFIG } from "./dashboard.config";
import { ThemeService } from "../services/theme.service";
import { mergeConfigs } from "../utils/config-merge";
import { mergeRolesConfig } from "./roles-merge";
import {
  AVAILABLE_ROLES as REGISTRY_AVAILABLE_ROLES,
  ROLE_HIERARCHY as REGISTRY_ROLE_HIERARCHY,
  ROLE_DISPLAY_NAMES as REGISTRY_ROLE_DISPLAY_NAMES,
  ROLE_DESCRIPTIONS as REGISTRY_ROLE_DESCRIPTIONS
} from "@nextsparkjs/registries/permissions-registry";
function loadAppConfig() {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME;
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] Loading app config for theme:", activeTheme);
  }
  if (!activeTheme) {
    if (process.env.NODE_ENV === "development") {
      console.log("[config-sync] No active theme, using default app config");
    }
    return DEFAULT_APP_CONFIG;
  }
  const themeConfigOverrides = ThemeService.getAppConfig(activeTheme);
  if (!themeConfigOverrides) {
    if (process.env.NODE_ENV === "development") {
      console.log("[config-sync] Theme has no app config overrides, using default");
    }
    return DEFAULT_APP_CONFIG;
  }
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] Theme app config overrides loaded from registry");
  }
  const mergedConfig = mergeConfigs(DEFAULT_APP_CONFIG, themeConfigOverrides);
  if (themeConfigOverrides.userRoles) {
    mergedConfig.userRoles = mergeRolesConfig(
      DEFAULT_APP_CONFIG.userRoles,
      themeConfigOverrides.userRoles
    );
  }
  if (DEFAULT_APP_CONFIG.teamRoles) {
    mergedConfig.teamRoles = {
      ...DEFAULT_APP_CONFIG.teamRoles,
      // Override with values from permissions-registry
      availableTeamRoles: REGISTRY_AVAILABLE_ROLES,
      hierarchy: REGISTRY_ROLE_HIERARCHY,
      displayNames: REGISTRY_ROLE_DISPLAY_NAMES,
      descriptions: REGISTRY_ROLE_DESCRIPTIONS
    };
  }
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] App config merged successfully");
  }
  return mergedConfig;
}
function loadDashboardConfig() {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME;
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] activeTheme:", activeTheme);
  }
  if (!activeTheme) {
    if (process.env.NODE_ENV === "development") {
      console.log("[config-sync] No active theme, using default config");
    }
    return DEFAULT_DASHBOARD_CONFIG;
  }
  const themeConfig = ThemeService.getDashboardConfig(activeTheme);
  if (!themeConfig) {
    if (process.env.NODE_ENV === "development") {
      console.log("[config-sync] Theme has no dashboard config, using default");
    }
    return DEFAULT_DASHBOARD_CONFIG;
  }
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] Theme config loaded (validation skipped)");
  }
  const mergedConfig = mergeConfigs(DEFAULT_DASHBOARD_CONFIG, themeConfig);
  if (process.env.NODE_ENV === "development") {
    console.log("[config-sync] Config merged successfully");
    console.log("[config-sync] Merged config has helper functions:", {
      isSettingsPageEnabled: typeof mergedConfig.isSettingsPageEnabled === "function",
      getEnabledSettingsPages: typeof mergedConfig.getEnabledSettingsPages === "function",
      isTopbarFeatureEnabled: typeof mergedConfig.isTopbarFeatureEnabled === "function"
    });
  }
  return mergedConfig;
}
function loadDevConfig() {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME;
  if (!activeTheme) {
    return null;
  }
  const devConfig = ThemeService.getDevConfig(activeTheme);
  if (process.env.NODE_ENV === "development" && devConfig) {
    console.log("[config-sync] Dev config loaded for theme:", activeTheme);
  }
  return devConfig;
}
const APP_CONFIG_MERGED = loadAppConfig();
const DASHBOARD_CONFIG = loadDashboardConfig();
const DEV_CONFIG = loadDevConfig();
const SUPPORTED_LOCALES = APP_CONFIG_MERGED.i18n.supportedLocales;
const DEFAULT_LOCALE = APP_CONFIG_MERGED.i18n.defaultLocale;
const AVAILABLE_ROLES = APP_CONFIG_MERGED.userRoles.availableRoles;
const DEFAULT_ROLE = APP_CONFIG_MERGED.userRoles.defaultRole;
const APP_NAME = APP_CONFIG_MERGED.app.name;
const APP_VERSION = APP_CONFIG_MERGED.app.version;
const I18N_CONFIG = APP_CONFIG_MERGED.i18n;
const APP_CONFIG = APP_CONFIG_MERGED.app;
const USER_ROLES_CONFIG = APP_CONFIG_MERGED.userRoles;
const API_CONFIG = APP_CONFIG_MERGED.api;
const MOBILE_NAV_CONFIG = APP_CONFIG_MERGED.mobileNav;
const TEAMS_CONFIG = APP_CONFIG_MERGED.teams;
const TOPBAR_CONFIG = DASHBOARD_CONFIG.topbar;
const SIDEBAR_CONFIG = DASHBOARD_CONFIG.sidebar;
const SETTINGS_CONFIG = DASHBOARD_CONFIG.settings;
const ENTITIES_CONFIG = DASHBOARD_CONFIG.entities;
const HOMEPAGE_CONFIG = DASHBOARD_CONFIG.homepage;
const PERFORMANCE_CONFIG = DASHBOARD_CONFIG.performance;
const ACCESSIBILITY_CONFIG = DASHBOARD_CONFIG.accessibility;
const isSettingsPageEnabled = (pageName) => {
  return DASHBOARD_CONFIG.isSettingsPageEnabled(pageName);
};
const getEnabledSettingsPages = () => {
  return DASHBOARD_CONFIG.getEnabledSettingsPages();
};
const isTopbarFeatureEnabled = (feature) => {
  return DASHBOARD_CONFIG.isTopbarFeatureEnabled(feature);
};
const getTopbarFeatureConfig = (feature) => {
  const topbar = DASHBOARD_CONFIG.topbar;
  return topbar[feature];
};
export {
  ACCESSIBILITY_CONFIG,
  API_CONFIG,
  APP_CONFIG,
  APP_CONFIG_MERGED,
  APP_NAME,
  APP_VERSION,
  AVAILABLE_ROLES,
  DASHBOARD_CONFIG,
  DEFAULT_LOCALE,
  DEFAULT_ROLE,
  DEV_CONFIG,
  ENTITIES_CONFIG,
  HOMEPAGE_CONFIG,
  I18N_CONFIG,
  MOBILE_NAV_CONFIG,
  PERFORMANCE_CONFIG,
  SETTINGS_CONFIG,
  SIDEBAR_CONFIG,
  SUPPORTED_LOCALES,
  TEAMS_CONFIG,
  TOPBAR_CONFIG,
  USER_ROLES_CONFIG,
  getEnabledSettingsPages,
  getTopbarFeatureConfig,
  isSettingsPageEnabled,
  isTopbarFeatureEnabled
};
