import { ansi256ToHex, hexToRgb } from "./color-utils.js";
export function parseAutoThemeSetting(themeSetting) {
    if (!themeSetting)
        return undefined;
    const slashIndex = themeSetting.indexOf("/");
    if (slashIndex === -1 || themeSetting.indexOf("/", slashIndex + 1) !== -1) {
        return undefined;
    }
    const lightTheme = themeSetting.slice(0, slashIndex).trim();
    const darkTheme = themeSetting.slice(slashIndex + 1).trim();
    if (!lightTheme || !darkTheme) {
        return undefined;
    }
    return { lightTheme, darkTheme };
}
export function resolveThemeSetting(themeSetting, terminalTheme) {
    const autoTheme = parseAutoThemeSetting(themeSetting);
    if (autoTheme) {
        return terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme;
    }
    if (themeSetting?.includes("/"))
        return undefined;
    if (typeof themeSetting === "string")
        return themeSetting;
    return undefined;
}
function getColorFgBgBackgroundIndex(colorfgbg) {
    const parts = colorfgbg.split(";");
    for (let i = parts.length - 1; i >= 0; i--) {
        const bg = parseInt(parts[i].trim(), 10);
        if (Number.isInteger(bg) && bg >= 0 && bg <= 255) {
            return bg;
        }
    }
    return undefined;
}
function getRgbColorLuminance({ r, g, b }) {
    const toLinear = (channel) => {
        const value = channel / 255;
        return value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
    };
    return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b);
}
function getAnsiColorLuminance(index) {
    return getRgbColorLuminance(hexToRgb(ansi256ToHex(index)));
}
export function getThemeForRgbColor(rgb) {
    return getRgbColorLuminance(rgb) >= 0.5 ? "light" : "dark";
}
export function detectTerminalBackgroundFromEnv(options = {}) {
    const env = options.env ?? process.env;
    const colorfgbg = env.COLORFGBG || "";
    const bg = getColorFgBgBackgroundIndex(colorfgbg);
    if (bg !== undefined) {
        return {
            theme: getAnsiColorLuminance(bg) >= 0.5 ? "light" : "dark",
            source: "COLORFGBG",
            detail: `background color index ${bg}`,
            confidence: "high",
        };
    }
    return {
        theme: "dark",
        source: "fallback",
        detail: "no terminal background hint found",
        confidence: "low",
    };
}
export async function detectTerminalBackgroundTheme({ ui, timeoutMs, env, }) {
    try {
        const rgb = await ui.queryTerminalBackgroundColor({ timeoutMs });
        if (rgb) {
            return {
                theme: getThemeForRgbColor(rgb),
                source: "terminal background",
                detail: `OSC 11 background rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`,
                confidence: "high",
            };
        }
    }
    catch {
        // Fall back to environment-based detection when the terminal query fails.
    }
    return detectTerminalBackgroundFromEnv({ env });
}
export function getDefaultTheme() {
    return detectTerminalBackgroundFromEnv().theme;
}
//# sourceMappingURL=terminal-detection.js.map