import { maskSecretLength } from "./mask-secret-length.js";
import { isMaskedApiKey } from "../../config-tools-web.js";
//#region src/gateway/hono/lib/safe-voice-config.ts
function maskProviderSlice(slice) {
	const next = { ...slice };
	if (typeof next.apiKey === "string" && next.apiKey.trim()) next.apiKey = maskSecretLength(next.apiKey);
	return next;
}
function mergeApiKeyField(incoming, previous) {
	if (typeof incoming !== "string") return previous;
	if (isMaskedApiKey(incoming) && typeof previous === "string" && previous.trim()) return previous;
	return incoming;
}
function mergeProviderSlice(incoming, previous) {
	const next = {
		...previous,
		...incoming
	};
	if ("apiKey" in incoming) next.apiKey = mergeApiKeyField(incoming.apiKey, previous.apiKey);
	return next;
}
function maskProviderMap(providers) {
	const out = {};
	for (const [id, value] of Object.entries(providers)) out[id] = value && typeof value === "object" && !Array.isArray(value) ? maskProviderSlice(value) : value;
	return out;
}
function mergeProviderMap(incoming, previous) {
	const out = { ...previous };
	for (const [id, value] of Object.entries(incoming)) if (value && typeof value === "object" && !Array.isArray(value)) out[id] = mergeProviderSlice(value, out[id] ?? {});
	else out[id] = value;
	return out;
}
function maskVoiceConfigForWeb(cfg) {
	if (!cfg || typeof cfg !== "object" || Array.isArray(cfg)) return cfg;
	const raw = { ...cfg };
	if (raw.providers && typeof raw.providers === "object" && !Array.isArray(raw.providers)) raw.providers = maskProviderMap(raw.providers);
	return raw;
}
/** Mask STT api keys for GET `/api/config`. */
function maskSttConfigForWeb(stt) {
	return maskVoiceConfigForWeb(stt);
}
/** Mask TTS api keys for GET `/api/config`. */
function maskTtsConfigForWeb(tts) {
	return maskVoiceConfigForWeb(tts);
}
function mergeVoiceConfigPatch(previous, incoming) {
	if (!incoming || typeof incoming !== "object" || Array.isArray(incoming)) return previous ?? incoming;
	const prev = previous && typeof previous === "object" && !Array.isArray(previous) ? previous : {};
	const patch = incoming;
	const next = {
		...prev,
		...patch
	};
	if (patch.providers && typeof patch.providers === "object" && !Array.isArray(patch.providers)) next.providers = mergeProviderMap(patch.providers, prev.providers ?? {});
	return next;
}
/** Merge incoming STT patch, preserving api keys when the UI sends masked sentinels. */
function mergeSttConfigPatch(previous, incoming) {
	return mergeVoiceConfigPatch(previous, incoming);
}
/** Merge incoming TTS patch, preserving api keys when the UI sends masked sentinels. */
function mergeTtsConfigPatch(previous, incoming) {
	return mergeVoiceConfigPatch(previous, incoming);
}
//#endregion
export { maskSttConfigForWeb, maskTtsConfigForWeb, mergeSttConfigPatch, mergeTtsConfigPatch };

//# sourceMappingURL=safe-voice-config.js.map