import { PROVIDER_ENV_MAP, init_env_keys } from "../providers/env-keys.js";
import { init_providers, isProviderConfiguredSync } from "../providers/index.js";
import { collectTtsProviderConfigEntries } from "../voice/tts/config-slice.js";
import { isProviderConfigured } from "../voice/tts/factory.js";
import { mergeTtsConfigFromAppConfig } from "../voice/tts/merge-config.js";
import { collectSttProviderConfigEntries } from "../voice/stt/config-slice.js";
import { mergeSttConfigFromAppConfig } from "../channels/attachments/voice-stt-webchat.js";
import { isSttProviderConfigured } from "../voice/stt/list-providers.js";
//#region src/extensions/activation-context.ts
init_providers();
init_env_keys();
function isRecord(x) {
	return typeof x === "object" && x !== null && !Array.isArray(x);
}
function telegramConfigured(ch) {
	if (!ch) return false;
	if (ch.enabled === true) return true;
	const accounts = ch.accounts;
	if (isRecord(accounts)) for (const acc of Object.values(accounts)) {
		if (!isRecord(acc)) continue;
		if (acc.enabled === false) continue;
		if (typeof acc.botToken === "string" && acc.botToken.trim().length > 0) return true;
	}
	return false;
}
function weixinConfigured(ch) {
	if (!ch) return false;
	if (ch.enabled === true) return true;
	const accounts = ch.accounts;
	if (isRecord(accounts) && Object.keys(accounts).length > 0) for (const acc of Object.values(accounts)) {
		if (isRecord(acc) && acc.enabled === false) continue;
		return true;
	}
	return false;
}
/**
* Derive configured channel plugin ids from `channels.*` config.
*/
function collectConfiguredChannelIds(config) {
	const channels = config?.channels;
	if (!isRecord(channels)) return void 0;
	const ids = [];
	if (telegramConfigured(channels.telegram)) ids.push("telegram");
	if (weixinConfigured(channels.weixin)) ids.push("weixin");
	for (const [key, value] of Object.entries(channels)) {
		if (key === "telegram" || key === "weixin") continue;
		if (isRecord(value) && value.enabled === true && !ids.includes(key)) ids.push(key);
	}
	return ids.length ? ids : void 0;
}
function collectConfiguredLlmProviderIds(config) {
	const root = config;
	const candidateIds = new Set(Object.keys(PROVIDER_ENV_MAP));
	const providers = root?.providers;
	if (isRecord(providers)) for (const key of Object.keys(providers)) candidateIds.add(key);
	const ids = [];
	for (const key of candidateIds) if (isProviderConfiguredSync(key)) ids.push(key);
	return ids;
}
function collectConfiguredSpeechProviderIds(config) {
	const messages = config?.messages;
	if (!isRecord(messages) || !isRecord(messages.tts)) return [];
	const ttsConfig = mergeTtsConfigFromAppConfig(messages.tts);
	const ids = /* @__PURE__ */ new Set();
	const primary = ttsConfig.provider?.trim();
	if (primary && isProviderConfigured(primary, ttsConfig)) ids.add(primary);
	for (const providerId of Object.keys(collectTtsProviderConfigEntries(ttsConfig))) if (isProviderConfigured(providerId, ttsConfig)) ids.add(providerId);
	return [...ids];
}
function collectConfiguredMediaUnderstandingProviderIds(config) {
	const tools = config?.tools;
	if (!isRecord(tools) || !isRecord(tools.media)) return [];
	const sttConfig = mergeSttConfigFromAppConfig(tools.media.audio, tools.media);
	const ids = /* @__PURE__ */ new Set();
	const primary = sttConfig.provider?.trim();
	if (primary && isSttProviderConfigured(primary, sttConfig)) ids.add(primary);
	for (const providerId of Object.keys(collectSttProviderConfigEntries(sttConfig))) if (isSttProviderConfigured(providerId, sttConfig)) ids.add(providerId);
	return [...ids];
}
/**
* Derive configured provider ids for extension activation (LLM + speech + STT).
*/
function collectConfiguredProviderIds(config) {
	const ids = new Set([
		...collectConfiguredLlmProviderIds(config),
		...collectConfiguredSpeechProviderIds(config),
		...collectConfiguredMediaUnderstandingProviderIds(config)
	]);
	return ids.size > 0 ? [...ids] : void 0;
}
function defaultModelId(config) {
	const agents = config?.agents;
	if (!isRecord(agents)) return void 0;
	const defaults = agents.defaults;
	if (!isRecord(defaults)) return void 0;
	const model = defaults.model;
	if (isRecord(model)) {
		const primary = model.primary;
		if (typeof primary === "string" && primary.trim().length > 0) return primary;
	}
}
/**
* Merge partial activation context over values inferred from loaded app config.
*/
function mergeActivationContext(appConfig, partial) {
	const ext = appConfig && isRecord(appConfig.extensions) ? appConfig.extensions : void 0;
	const base = {
		enabledIds: Array.isArray(ext?.enabled) ? ext.enabled.filter((x) => typeof x === "string") : void 0,
		disabledIds: Array.isArray(ext?.disabled) ? ext.disabled.filter((x) => typeof x === "string") : void 0,
		requestedModelId: defaultModelId(appConfig),
		configuredProviderIds: collectConfiguredProviderIds(appConfig),
		configuredChannelIds: collectConfiguredChannelIds(appConfig),
		env: process.env
	};
	return {
		env: partial?.env ?? base.env,
		enabledIds: partial?.enabledIds !== void 0 ? partial.enabledIds : base.enabledIds,
		disabledIds: partial?.disabledIds !== void 0 ? partial.disabledIds : base.disabledIds,
		requestedModelId: partial?.requestedModelId !== void 0 ? partial.requestedModelId : base.requestedModelId,
		configuredProviderIds: partial?.configuredProviderIds !== void 0 ? partial.configuredProviderIds : base.configuredProviderIds,
		configuredChannelIds: partial?.configuredChannelIds !== void 0 ? partial.configuredChannelIds : base.configuredChannelIds
	};
}
//#endregion
export { collectConfiguredChannelIds, collectConfiguredProviderIds, mergeActivationContext };

//# sourceMappingURL=activation-context.js.map