import { parseDiarizationMode, parseDurationMs, parseEmbeddedVideoMode, parseFirecrawlMode, parseLengthArg, parseMarkdownMode, parseMaxOutputTokensArg, parsePreprocessMode, parseRetriesArg, parseVideoMode, parseYoutubeMode, } from "../flags.js";
import { resolveOutputLanguage } from "../language.js";
import { formatPresetLengthGuidance } from "../prompts/index.js";
import { parseCliProvider, parseOptionalBoolean, parseOptionalCliProviderOrder, parseOptionalSetting, } from "./run-settings-parse.js";
export function createEmptyRunOverrides() {
    return {
        firecrawlMode: null,
        markdownMode: null,
        preprocessMode: null,
        youtubeMode: null,
        videoMode: null,
        embeddedVideoMode: null,
        transcriptTimestamps: null,
        transcriptDiarization: null,
        forceSummary: null,
        timeoutMs: null,
        retries: null,
        maxOutputTokensArg: null,
        transcriber: null,
        autoCliFallbackEnabled: null,
        autoCliOrder: null,
    };
}
export function resolveSummaryLength(raw, fallback = "long") {
    const value = typeof raw === "string" ? raw.trim() : "";
    const lengthArg = parseLengthArg(value || fallback);
    const summaryLength = lengthArg.kind === "preset" ? lengthArg.preset : { maxCharacters: lengthArg.maxCharacters };
    return { lengthArg, summaryLength };
}
export function buildPromptLengthInstruction(lengthArg) {
    return lengthArg.kind === "chars"
        ? `Output is ${lengthArg.maxCharacters.toLocaleString()} characters.`
        : formatPresetLengthGuidance(lengthArg.preset);
}
export function resolveOutputLanguageSetting({ raw, fallback, }) {
    const value = typeof raw === "string" ? raw.trim() : "";
    if (!value)
        return fallback;
    return resolveOutputLanguage(value);
}
export function resolveCliRunSettings({ length, firecrawl, markdownMode, markdown, format, preprocess, youtube, timeout, retries, maxOutputTokens, }) {
    const strictOverrides = resolveRunOverrides({
        firecrawl,
        markdownMode: format === "markdown" ? (markdownMode ?? markdown ?? "readability") : "off",
        preprocess,
        youtube,
        timeout,
        retries,
        maxOutputTokens,
    }, { strict: true });
    const requireOverride = (value, label) => {
        if (value == null) {
            throw new Error(`Missing ${label} override value.`);
        }
        return value;
    };
    return {
        lengthArg: parseLengthArg(length),
        firecrawlMode: requireOverride(strictOverrides.firecrawlMode, "--firecrawl"),
        markdownMode: requireOverride(strictOverrides.markdownMode, "--markdown-mode"),
        preprocessMode: requireOverride(strictOverrides.preprocessMode, "--preprocess"),
        youtubeMode: requireOverride(strictOverrides.youtubeMode, "--youtube"),
        timeoutMs: requireOverride(strictOverrides.timeoutMs, "--timeout"),
        retries: requireOverride(strictOverrides.retries, "--retries"),
        maxOutputTokensArg: strictOverrides.maxOutputTokensArg,
    };
}
export function resolveRunOverrides({ firecrawl, markdownMode, preprocess, youtube, videoMode, embeddedVideo, timestamps, diarize, forceSummary, timeout, retries, maxOutputTokens, transcriber, autoCliFallback, autoCliOrder, autoCliRememberLastSuccess, magicCliAuto, magicCliOrder, magicCliRememberLastSuccess, }, options = {}) {
    const strict = options.strict ?? false;
    const timeoutMs = (() => {
        if (typeof timeout === "number") {
            if (Number.isFinite(timeout) && timeout > 0) {
                return Math.floor(timeout);
            }
            if (strict) {
                throw new Error(`Unsupported --timeout: ${String(timeout)}`);
            }
            return null;
        }
        if (typeof timeout !== "string")
            return null;
        try {
            return parseDurationMs(timeout);
        }
        catch (error) {
            if (strict)
                throw error;
            return null;
        }
    })();
    const retriesResolved = (() => {
        if (typeof retries === "number") {
            if (Number.isFinite(retries) && Number.isInteger(retries)) {
                try {
                    return parseRetriesArg(String(retries));
                }
                catch (error) {
                    if (strict)
                        throw error;
                    return null;
                }
            }
            if (strict) {
                throw new Error(`Unsupported --retries: ${String(retries)}`);
            }
            return null;
        }
        if (typeof retries !== "string")
            return null;
        try {
            return parseRetriesArg(retries);
        }
        catch (error) {
            if (strict)
                throw error;
            return null;
        }
    })();
    const maxOutputTokensArg = (() => {
        if (typeof maxOutputTokens === "number") {
            if (Number.isFinite(maxOutputTokens) && maxOutputTokens > 0) {
                try {
                    return parseMaxOutputTokensArg(String(maxOutputTokens));
                }
                catch (error) {
                    if (strict)
                        throw error;
                    return null;
                }
            }
            if (strict) {
                throw new Error(`Unsupported --max-output-tokens: ${String(maxOutputTokens)}`);
            }
            return null;
        }
        if (typeof maxOutputTokens !== "string")
            return null;
        try {
            return parseMaxOutputTokensArg(maxOutputTokens);
        }
        catch (error) {
            if (strict)
                throw error;
            return null;
        }
    })();
    const transcriberOverride = (() => {
        if (typeof transcriber !== "string")
            return null;
        const normalized = transcriber.trim().toLowerCase();
        if (normalized === "auto" ||
            normalized === "whisper" ||
            normalized === "parakeet" ||
            normalized === "canary") {
            return normalized;
        }
        if (strict) {
            throw new Error(`Unsupported transcriber: ${transcriber}`);
        }
        return null;
    })();
    const forceSummaryResolved = parseOptionalBoolean(forceSummary, strict, "--force-summary");
    const autoCliFallbackEnabled = parseOptionalBoolean(typeof autoCliFallback !== "undefined" ? autoCliFallback : magicCliAuto, strict, "--auto-cli-fallback");
    // Kept for backward compatibility with older extension payloads. Ignored now.
    void autoCliRememberLastSuccess;
    // Kept for backward compatibility with older extension payloads. Ignored now.
    void magicCliRememberLastSuccess;
    const autoCliOrderResolved = parseOptionalCliProviderOrder(typeof autoCliOrder !== "undefined" ? autoCliOrder : magicCliOrder, strict);
    return {
        firecrawlMode: parseOptionalSetting(firecrawl, parseFirecrawlMode, strict),
        markdownMode: parseOptionalSetting(markdownMode, parseMarkdownMode, strict),
        preprocessMode: parseOptionalSetting(preprocess, parsePreprocessMode, strict),
        youtubeMode: parseOptionalSetting(youtube, parseYoutubeMode, strict),
        videoMode: parseOptionalSetting(videoMode, parseVideoMode, strict),
        embeddedVideoMode: parseOptionalSetting(embeddedVideo, parseEmbeddedVideoMode, strict),
        transcriptTimestamps: parseOptionalBoolean(timestamps, strict, "--timestamps"),
        transcriptDiarization: parseOptionalSetting(diarize, parseDiarizationMode, strict),
        forceSummary: forceSummaryResolved,
        timeoutMs,
        retries: retriesResolved,
        maxOutputTokensArg,
        transcriber: transcriberOverride,
        autoCliFallbackEnabled,
        autoCliOrder: autoCliOrderResolved,
    };
}
//# sourceMappingURL=run-settings.js.map