import { llmProviderRegistry } from "@open-mercato/shared/lib/ai/llm-provider-registry";
import "./llm-bootstrap.js";
const CHAT_CONFIG_KEY = "chat_provider";
function providerToChatInfo(provider) {
  return {
    name: provider.name,
    envKeyRequired: provider.envKeys[0],
    defaultModel: provider.defaultModel,
    models: provider.defaultModels.map((m) => ({
      id: m.id,
      name: m.name,
      contextWindow: m.contextWindow
    }))
  };
}
const CHAT_PROVIDERS = new Proxy(
  {},
  {
    get(_target, prop) {
      if (typeof prop !== "string") return void 0;
      const provider = llmProviderRegistry.get(prop);
      return provider ? providerToChatInfo(provider) : void 0;
    },
    has(_target, prop) {
      if (typeof prop !== "string") return false;
      return llmProviderRegistry.get(prop) !== null;
    },
    ownKeys() {
      return llmProviderRegistry.list().map((p) => p.id);
    },
    getOwnPropertyDescriptor(_target, prop) {
      if (typeof prop !== "string") return void 0;
      const provider = llmProviderRegistry.get(prop);
      if (!provider) return void 0;
      return {
        enumerable: true,
        configurable: true,
        value: providerToChatInfo(provider)
      };
    }
  }
);
const DEFAULT_CHAT_CONFIG = {
  providerId: "openai",
  get model() {
    const provider = llmProviderRegistry.get("openai");
    return provider?.defaultModel ?? "gpt-5-mini";
  }
};
function isProviderConfigured(providerId) {
  const provider = llmProviderRegistry.get(providerId);
  return provider?.isConfigured() ?? false;
}
function getConfiguredProviders() {
  return llmProviderRegistry.listConfigured().map((p) => p.id);
}
async function resolveChatConfig(resolver, options) {
  const fallback = options?.defaultValue ?? null;
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    return fallback;
  }
  try {
    const value = await service.getValue("ai_assistant", CHAT_CONFIG_KEY, { defaultValue: fallback });
    return value;
  } catch {
    return fallback;
  }
}
async function saveChatConfig(resolver, config) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    throw new Error("Configuration service unavailable");
  }
  const fullConfig = {
    ...config,
    updatedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
  await service.setValue("ai_assistant", CHAT_CONFIG_KEY, fullConfig);
  return fullConfig;
}
function createDefaultConfig() {
  return {
    providerId: DEFAULT_CHAT_CONFIG.providerId,
    model: DEFAULT_CHAT_CONFIG.model,
    updatedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
}
function getModelInfo(providerId, modelId) {
  const provider = CHAT_PROVIDERS[providerId];
  if (!provider) return null;
  return provider.models.find((m) => m.id === modelId) ?? null;
}
function formatContextWindow(contextWindow) {
  if (contextWindow >= 1e6) {
    return `${(contextWindow / 1e6).toFixed(1)}M`;
  }
  return `${(contextWindow / 1e3).toFixed(0)}K`;
}
export {
  CHAT_CONFIG_KEY,
  CHAT_PROVIDERS,
  DEFAULT_CHAT_CONFIG,
  createDefaultConfig,
  formatContextWindow,
  getConfiguredProviders,
  getModelInfo,
  isProviderConfigured,
  resolveChatConfig,
  saveChatConfig
};
//# sourceMappingURL=chat-config.js.map
