import { buildPromptLengthInstruction, createEmptyRunOverrides, resolveOutputLanguageSetting, resolveSummaryLength, } from "../run/run-settings.js";
import { createRunConfigInput } from "./config-state.js";
import { resolveRunContextState } from "./context.js";
import { resolveRunModelSpec } from "./model-runtime.js";
function applyAutoCliFallbackOverrides(config, overrides) {
    const hasOverride = overrides.autoCliFallbackEnabled !== null || overrides.autoCliOrder !== null;
    if (!hasOverride)
        return config;
    const current = config ?? {};
    const currentCli = current.cli ?? {};
    const currentAutoFallback = currentCli.autoFallback ?? currentCli.magicAuto ?? {};
    return {
        ...current,
        cli: {
            ...currentCli,
            autoFallback: {
                ...currentAutoFallback,
                ...(typeof overrides.autoCliFallbackEnabled === "boolean"
                    ? { enabled: overrides.autoCliFallbackEnabled }
                    : {}),
                ...(Array.isArray(overrides.autoCliOrder) ? { order: overrides.autoCliOrder } : {}),
            },
        },
    };
}
function toPublicModelSpec(model) {
    const { configForModelSelection, ...publicModel } = model;
    void configForModelSelection;
    return publicModel;
}
function createDefaultConfigInput(request, overrides) {
    const languageRaw = typeof request.languageRaw === "string" ? request.languageRaw : null;
    return createRunConfigInput({
        languageRaw,
        languageExplicit: Boolean(languageRaw?.trim()),
        videoModeRaw: overrides.videoMode ?? "auto",
        videoModeExplicit: overrides.videoMode != null,
        embeddedVideoModeRaw: overrides.embeddedVideoMode ?? "auto",
        embeddedVideoModeExplicit: overrides.embeddedVideoMode != null,
    });
}
export function resolveSummarizeRun({ request, env, configInput, useConfigPromptDefault = false, }) {
    const envForRun = { ...env };
    const overrides = request.overrides ?? createEmptyRunOverrides();
    if (overrides.transcriber) {
        envForRun.SUMMARIZE_TRANSCRIBER = overrides.transcriber;
    }
    const context = resolveRunContextState({
        env: envForRun,
        envForRun,
        configInput: configInput ?? createDefaultConfigInput(request, overrides),
    });
    const format = request.format === "markdown" ? "markdown" : "text";
    const { lengthArg } = resolveSummaryLength(request.lengthRaw, context.config?.output?.length ?? "long");
    const maxOutputTokensArg = overrides.maxOutputTokensArg;
    const configForSelection = applyAutoCliFallbackOverrides(context.configForCli, overrides);
    const model = resolveRunModelSpec({
        context,
        envForRun,
        explicitModelArg: request.modelOverride?.trim() ? request.modelOverride.trim() : null,
        configForSelection,
        lengthArg,
        maxOutputTokensArg,
    });
    const outputLanguage = resolveOutputLanguageSetting({
        raw: request.languageRaw,
        fallback: context.outputLanguage,
    });
    const promptOverride = request.promptOverride ??
        (useConfigPromptDefault && typeof context.config?.prompt === "string"
            ? context.config.prompt.trim() || null
            : null);
    return {
        spec: {
            format,
            maxExtractCharacters: request.input.kind === "url" || request.input.kind === "input-url"
                ? request.input.maxCharacters
                : null,
            timeoutMs: overrides.timeoutMs ?? 120_000,
            retries: overrides.retries ?? 1,
            markdownMode: overrides.markdownMode ?? (format === "markdown" ? "readability" : "off"),
            preprocessMode: overrides.preprocessMode ?? "auto",
            youtubeMode: overrides.youtubeMode ?? "auto",
            firecrawlMode: overrides.firecrawlMode ?? "off",
            videoMode: context.videoMode,
            embeddedVideoMode: context.embeddedVideoMode,
            transcriptTimestamps: overrides.transcriptTimestamps ?? false,
            transcriptDiarization: overrides.transcriptDiarization ?? null,
            outputLanguage,
            lengthArg,
            forceSummary: overrides.forceSummary ?? false,
            promptOverride,
            lengthInstruction: promptOverride ? buildPromptLengthInstruction(lengthArg) : null,
            languageInstruction: promptOverride && outputLanguage.kind === "fixed"
                ? `Output should be ${outputLanguage.label}.`
                : null,
            maxOutputTokensArg,
            allowAutoCliFallback: overrides.autoCliFallbackEnabled === true,
            model: toPublicModelSpec(model),
            configPath: context.configPath,
            configModelLabel: context.configModelLabel,
        },
        bindings: {
            context,
            model,
            envForRun,
        },
    };
}
//# sourceMappingURL=run-spec.js.map