import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/voice/tts/speech-registry.ts
init_logger();
const log = createLogger("SpeechRegistry");
const registry = /* @__PURE__ */ new Map();
function normalizeId(id) {
	return id.trim().toLowerCase();
}
function indexProvider(provider) {
	const keys = [normalizeId(provider.id)];
	for (const alias of provider.aliases ?? []) keys.push(normalizeId(alias));
	return keys;
}
/**
* Register a speech provider. Safe to call multiple times during boot — later
* registrations overwrite earlier ones for the same id (with a warn log).
*
* Returns an unregister function for tests / hot-reload scenarios.
*/
function registerSpeechProvider(provider) {
	const keys = indexProvider(provider);
	const conflicts = [];
	for (const key of keys) if (registry.has(key)) conflicts.push(key);
	if (conflicts.length > 0) log.warn({
		providerId: provider.id,
		conflicts
	}, `Speech provider "${provider.id}" overrides existing registration(s): ${conflicts.join(", ")}`);
	for (const key of keys) registry.set(key, provider);
	return () => {
		for (const key of keys) if (registry.get(key) === provider) registry.delete(key);
	};
}
/**
* Look up a provider by id or alias (case-insensitive). Returns undefined when
* no provider matches — callers must NOT throw here; the orchestrator decides
* whether the absence is fatal or means "skip and try next provider".
*/
function getSpeechProvider(id) {
	return registry.get(normalizeId(id));
}
/**
* Resolve the canonical id for a provider lookup key (id or alias).
* Useful for normalizing config strings before persistence.
*/
function canonicalizeSpeechProviderId(id) {
	return registry.get(normalizeId(id))?.id;
}
/**
* List all registered providers. Order is registration order (Map preserves
* insertion order, and we de-dup by id at registration time).
*/
function listSpeechProviders() {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const provider of registry.values()) if (!seen.has(provider)) {
		seen.add(provider);
		result.push(provider);
	}
	return result;
}
/** Test-only: clear the registry. Not exported from the public barrel. */
function _clearSpeechRegistryForTests() {
	registry.clear();
}
//#endregion
export { _clearSpeechRegistryForTests, canonicalizeSpeechProviderId, getSpeechProvider, listSpeechProviders, registerSpeechProvider };

//# sourceMappingURL=speech-registry.js.map