import { f as defineAdminEventHandler, dc as getAvailableLocales, dd as isValidLocale, de as ensurePluginRuntimeInitialized, L as getPluginRuntimeManager, as as config, c as createError } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const DEFAULT_ADMIN_BRANDING = {
  primaryColor: "",
  logo: "",
  logoCompact: "",
  favicon: "",
  title: "",
  footerText: "",
  hidePoweredBy: false
};
const BRANDING_SETTING_MAP = {
  primaryColor: "branding.primaryColor",
  logo: "branding.logo",
  logoCompact: "branding.logoCompact",
  favicon: "branding.favicon",
  title: "branding.title",
  footerText: "branding.footerText",
  hidePoweredBy: "branding.hidePoweredBy"
};
function isThemeManifest(manifest) {
  return manifest.kind === "theme";
}
function isRuntimeActive(entry) {
  return entry.state.phase === "active" || entry.state.phase === "initialized";
}
function getActiveThemeManifests(entries) {
  return entries.filter(isRuntimeActive).map((entry) => entry.definition.manifest).filter(isThemeManifest);
}
function getActiveThemeDescriptor(themes) {
  const activeTheme = themes.at(-1);
  if (!activeTheme) {
    return null;
  }
  return {
    name: activeTheme.name,
    displayName: activeTheme.displayName
  };
}
function resolveThemeBrandingDefaults(themes) {
  return themes.reduce((branding, theme) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
    const themePrimaryColor = (_d = (_c = (_b = (_a = theme.tokens) == null ? void 0 : _a.colors) == null ? void 0 : _b.brand) == null ? void 0 : _c.primary) == null ? void 0 : _d.value;
    return {
      primaryColor: themePrimaryColor != null ? themePrimaryColor : branding.primaryColor,
      logo: (_f = (_e = theme.branding) == null ? void 0 : _e.logo) != null ? _f : branding.logo,
      logoCompact: (_h = (_g = theme.branding) == null ? void 0 : _g.logoCompact) != null ? _h : branding.logoCompact,
      favicon: (_j = (_i = theme.branding) == null ? void 0 : _i.favicon) != null ? _j : branding.favicon,
      title: (_l = (_k = theme.branding) == null ? void 0 : _k.title) != null ? _l : branding.title,
      footerText: (_n = (_m = theme.branding) == null ? void 0 : _m.footerText) != null ? _n : branding.footerText,
      hidePoweredBy: (_p = (_o = theme.branding) == null ? void 0 : _o.hidePoweredBy) != null ? _p : branding.hidePoweredBy
    };
  }, DEFAULT_ADMIN_BRANDING);
}
function applyBrandingSettingOverrides(branding, getSettingMeta) {
  const resolved = { ...branding };
  for (const [brandingKey, settingKey] of Object.entries(BRANDING_SETTING_MAP)) {
    const setting = getSettingMeta(settingKey);
    if (!setting || isSchemaDefaultValue(setting)) {
      continue;
    }
    if (brandingKey === "hidePoweredBy") {
      if (typeof setting.value === "boolean") {
        resolved.hidePoweredBy = setting.value;
      }
      continue;
    }
    if (typeof setting.value === "string") {
      resolved[brandingKey] = setting.value;
    }
  }
  return resolved;
}
function isSchemaDefaultValue(setting) {
  if (setting.source === "default") {
    return true;
  }
  return setting.source === "database" && Object.is(setting.value, setting.defaultValue);
}
function resolveAdminBrandingRuntime(entries, getSettingMeta) {
  const themes = getActiveThemeManifests(entries);
  const brandingDefaults = resolveThemeBrandingDefaults(themes);
  return {
    branding: applyBrandingSettingOverrides(brandingDefaults, getSettingMeta),
    activeTheme: getActiveThemeDescriptor(themes),
    activeThemeCount: themes.length
  };
}

const uiConfig_get = defineAdminEventHandler("admin:ui-config:get", async () => {
  var _a, _b, _c;
  try {
    let enabledLocales = ["ru", "en"];
    if (process.env.ADMIN_ENABLED_LOCALES) {
      try {
        const parsed = JSON.parse(process.env.ADMIN_ENABLED_LOCALES);
        if (Array.isArray(parsed)) {
          enabledLocales = parsed;
        }
      } catch {
      }
    }
    const allLocales = getAvailableLocales();
    const locales = allLocales.filter((l) => enabledLocales.includes(l.code)).map((l) => ({
      code: l.code,
      name: l.name,
      nativeName: l.nativeName,
      direction: l.direction
    }));
    for (const code of enabledLocales) {
      if (!isValidLocale(code) && !locales.find((l) => l.code === code)) {
        locales.push({
          code,
          name: code,
          nativeName: code,
          direction: "ltr"
        });
      }
    }
    let customLocales = {};
    if (process.env.ADMIN_CUSTOM_LOCALES) {
      try {
        const parsed = JSON.parse(process.env.ADMIN_CUSTOM_LOCALES);
        if (typeof parsed === "object" && parsed !== null) {
          customLocales = parsed;
        }
      } catch {
        console.warn("[GET /api/admin/ui-config] Failed to parse ADMIN_CUSTOM_LOCALES");
      }
    }
    await ensurePluginRuntimeInitialized();
    const manager = getPluginRuntimeManager();
    const runtimeBranding = resolveAdminBrandingRuntime(
      (_a = manager == null ? void 0 : manager.getRegistry().getAll()) != null ? _a : [],
      (key) => config.getWithMeta(key)
    );
    const branding = runtimeBranding.branding;
    if (runtimeBranding.activeThemeCount > 1 && runtimeBranding.activeTheme) {
      console.warn(
        `[GET /api/admin/ui-config] Multiple active themes detected; using merged result with "${runtimeBranding.activeTheme.name}" as the last loaded theme.`
      );
    }
    return {
      success: true,
      data: {
        enabledLocales: locales,
        defaultLocale: ((_b = locales[0]) == null ? void 0 : _b.code) || "ru",
        customLocales,
        branding,
        theme: (_c = runtimeBranding.activeTheme) != null ? _c : void 0
      }
    };
  } catch (error) {
    console.error("[GET /api/admin/ui-config] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to get UI configuration",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { uiConfig_get as default };
//# sourceMappingURL=ui-config.get.mjs.map
