import { z } from "zod";
import { resolveLocalSpeechConfig } from "./providers/local/config.js";
import { resolveOpenAiSpeechConfig } from "./providers/openai/config.js";
import { SpeechProviderIdSchema, } from "./speech-types.js";
const OptionalSpeechProviderSchema = z
    .string()
    .trim()
    .toLowerCase()
    .pipe(SpeechProviderIdSchema)
    .optional();
const OptionalBooleanFlagSchema = z
    .union([z.boolean(), z.string().trim().toLowerCase()])
    .optional()
    .transform((value) => {
    if (typeof value === "boolean") {
        return value;
    }
    if (value === undefined) {
        return undefined;
    }
    if (["1", "true", "yes", "y", "on"].includes(value)) {
        return true;
    }
    if (["0", "false", "no", "n", "off"].includes(value)) {
        return false;
    }
    return undefined;
});
const RequestedSpeechProvidersSchema = z.object({
    dictationStt: OptionalSpeechProviderSchema.default("local"),
    voiceTurnDetection: OptionalSpeechProviderSchema.default("local"),
    voiceStt: OptionalSpeechProviderSchema.default("local"),
    voiceTts: OptionalSpeechProviderSchema.default("local"),
});
function resolveOptionalBooleanFlag(value) {
    return OptionalBooleanFlagSchema.parse(value) ?? true;
}
function resolveRequestedSpeechProviders(params) {
    const resolveFeatureProvider = (configuredValue, parsedValue, enabled) => ({
        provider: parsedValue,
        explicit: configuredValue !== undefined,
        enabled,
    });
    const voiceModeEnabled = resolveOptionalBooleanFlag(params.env.SEAWORK_VOICE_MODE_ENABLED ?? params.persisted.features?.voiceMode?.enabled);
    const featureProviders = {
        dictationStt: {
            configuredValue: params.env.SEAWORK_DICTATION_STT_PROVIDER ??
                params.persisted.features?.dictation?.stt?.provider,
            enabled: resolveOptionalBooleanFlag(params.env.SEAWORK_DICTATION_ENABLED ?? params.persisted.features?.dictation?.enabled),
        },
        voiceTurnDetection: {
            configuredValue: params.env.SEAWORK_VOICE_TURN_DETECTION_PROVIDER ??
                params.persisted.features?.voiceMode?.turnDetection?.provider,
            enabled: voiceModeEnabled,
        },
        voiceStt: {
            configuredValue: params.env.SEAWORK_VOICE_STT_PROVIDER ??
                params.persisted.features?.voiceMode?.stt?.provider,
            enabled: voiceModeEnabled,
        },
        voiceTts: {
            configuredValue: params.env.SEAWORK_VOICE_TTS_PROVIDER ??
                params.persisted.features?.voiceMode?.tts?.provider,
            enabled: voiceModeEnabled,
        },
    };
    const parsed = RequestedSpeechProvidersSchema.parse({
        dictationStt: featureProviders.dictationStt.configuredValue ?? "local",
        voiceTurnDetection: featureProviders.voiceTurnDetection.configuredValue ?? "local",
        voiceStt: featureProviders.voiceStt.configuredValue ?? "local",
        voiceTts: featureProviders.voiceTts.configuredValue ?? "local",
    });
    return {
        dictationStt: resolveFeatureProvider(featureProviders.dictationStt.configuredValue, parsed.dictationStt, featureProviders.dictationStt.enabled),
        voiceTurnDetection: resolveFeatureProvider(featureProviders.voiceTurnDetection.configuredValue, parsed.voiceTurnDetection, featureProviders.voiceTurnDetection.enabled),
        voiceStt: resolveFeatureProvider(featureProviders.voiceStt.configuredValue, parsed.voiceStt, featureProviders.voiceStt.enabled),
        voiceTts: resolveFeatureProvider(featureProviders.voiceTts.configuredValue, parsed.voiceTts, featureProviders.voiceTts.enabled),
    };
}
export function resolveSpeechConfig(params) {
    const providers = resolveRequestedSpeechProviders({
        env: params.env,
        persisted: params.persisted,
    });
    const local = resolveLocalSpeechConfig({
        seaworkHome: params.seaworkHome,
        env: params.env,
        persisted: params.persisted,
        providers,
    });
    const openai = resolveOpenAiSpeechConfig({
        env: params.env,
        persisted: params.persisted,
        providers,
    });
    return {
        openai,
        speech: {
            providers,
            ...(local.local ? { local: local.local } : {}),
        },
    };
}
//# sourceMappingURL=speech-config-resolver.js.map