import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { getMediaUnderstandingProvider, listMediaUnderstandingProviders } from "../../media-understanding/registry.js";
import "./providers/index.js";
import { resolveModelEntries } from "../../media-understanding/resolve-entries.js";
import { STT_LEGACY_ENV_KEYS, readSttProviderFields, resolveSttProviderConfigSlice } from "./config-slice.js";
//#region src/voice/stt/factory.ts
init_logger();
const log = createLogger("STT:Factory");
function resolveApiKey(providerId, fields, envKey) {
	return fields.apiKey ?? (envKey ? process.env[envKey] : void 0) ?? process.env[STT_LEGACY_ENV_KEYS[providerId] ?? ""];
}
/** Resolve a single STT provider config slice → runner-shaped resolved config, or null when unavailable. */
function resolveSTTProviderConfig(providerId, config, entry) {
	const plugin = getMediaUnderstandingProvider(providerId);
	if (!plugin || typeof plugin.transcribeAudio !== "function") {
		log.warn({ providerId }, `STT provider "${providerId}" is not registered or does not implement transcribeAudio`);
		return null;
	}
	const fields = readSttProviderFields(resolveSttProviderConfigSlice(providerId, config), entry);
	const apiKey = resolveApiKey(providerId, fields, plugin.envKey);
	if (plugin.requiresApiKey !== false && !apiKey) {
		log.debug({ providerId }, `STT provider "${providerId}" missing API key; skipping`);
		return null;
	}
	return {
		id: providerId,
		...apiKey ? { apiKey } : {},
		...fields.baseUrl ? { baseUrl: fields.baseUrl } : {},
		...fields.headers ? { headers: fields.headers } : {},
		...fields.model ?? entry?.model ?? plugin.defaultModels?.audio ? { model: fields.model ?? entry?.model ?? plugin.defaultModels?.audio } : {},
		...fields.language ? { language: fields.language } : {},
		...fields.prompt ? { prompt: fields.prompt } : {}
	};
}
function sortAudioProvidersByAutoPriority(plugins) {
	return [...plugins].filter((plugin) => plugin.capabilities?.includes("audio") && typeof plugin.transcribeAudio === "function").sort((left, right) => {
		const leftOrder = left.autoPriority?.audio ?? Number.MAX_SAFE_INTEGER;
		const rightOrder = right.autoPriority?.audio ?? Number.MAX_SAFE_INTEGER;
		if (leftOrder !== rightOrder) return leftOrder - rightOrder;
		return left.id.localeCompare(right.id);
	}).map((plugin) => plugin.id);
}
/** Resolve provider order: explicit fallback when enabled, otherwise auto-select by registry order. */
function resolveSTTProviderOrder(primary, fallback, config) {
	if (fallback?.enabled && fallback.order.length > 0) {
		const order = [primary];
		for (const providerId of fallback.order) if (providerId !== primary && !order.includes(providerId)) order.push(providerId);
		return order;
	}
	const configured = sortAudioProvidersByAutoPriority(listMediaUnderstandingProviders()).filter((providerId) => resolveSTTProviderConfig(providerId, config) !== null);
	const order = [primary];
	for (const providerId of configured) if (providerId !== primary && !order.includes(providerId)) order.push(providerId);
	return order;
}
function resolveModelEntryChain(config) {
	const entries = resolveModelEntries({
		capability: "audio",
		capabilityModels: config.models,
		sharedModels: config.sharedModels
	});
	const chain = [];
	for (const entry of entries) {
		const providerId = entry.provider?.trim();
		if (!providerId) continue;
		const resolved = resolveSTTProviderConfig(providerId, config, entry);
		if (resolved) chain.push(resolved);
	}
	return chain;
}
/** Build the full chain of resolved provider configs in priority order. */
function resolveSTTProviderChain(config) {
	if (!config.enabled) return [];
	const modelChain = resolveModelEntryChain(config);
	if (modelChain.length > 0) return modelChain;
	const order = resolveSTTProviderOrder(config.provider, config.fallback, config);
	const chain = [];
	for (const providerId of order) {
		const resolved = resolveSTTProviderConfig(providerId, config);
		if (resolved) chain.push(resolved);
	}
	return chain;
}
//#endregion
export { resolveSTTProviderChain, resolveSTTProviderConfig, resolveSTTProviderOrder };

//# sourceMappingURL=factory.js.map