import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { terminalThemes } from "../services/theme/themeCatalog.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { ProgressBar } from "./design/ProgressBar.js";
import { SelectList } from "./design/SelectList.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { Tabs } from "./design/Tabs.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
import { ThemePicker, themePickerModel } from "./ThemePicker.js";
export function SettingsPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(62, Math.min(116, columns - 4));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "settings", tone: "brand", paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: toneColor("brand"), children: props.model.title }), _jsx(Text, { color: "gray", children: truncateCells(props.model.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: props.model.badge, tone: props.model.badgeTone })] }), _jsx(Box, { marginTop: 1, children: _jsx(Tabs, { tabs: props.model.tabs, selectedId: props.model.selectedTab, title: "tab", width: width }) }), _jsxs(Box, { marginTop: 1, marginBottom: 1, children: [_jsx(Text, { color: "gray", children: "cache " }), _jsx(ProgressBar, { ratio: props.model.cacheRatio, width: Math.max(12, Math.min(32, width - 34)), filledTone: cacheTone(props.model.cacheRatio, props.model.cacheHitTokens + props.model.cacheMissTokens) }), _jsxs(Text, { color: "gray", children: [" hit ", props.model.cacheHitTokens, " / miss ", props.model.cacheMissTokens] })] }), _jsx(Box, { flexDirection: "column", children: props.model.rows.map((row) => (_jsx(SettingsPanelRowView, { row: row, width: width }, row.key))) }), props.model.themePicker ? (_jsx(ThemePicker, { model: props.model.themePicker, width: width - 2 })) : null, _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: "actions" }) }), _jsx(SelectList, { options: props.model.actions, selectedIndex: 0, visibleCount: 5, width: width }), _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function settingsPanelModel(input) {
    const selectedTab = normalizeSettingsTab(input.tab);
    const cacheTotal = input.status.cache.hitTokens + input.status.cache.missTokens;
    const cacheRatio = cacheTotal > 0 ? input.status.cache.hitTokens / cacheTotal : 0;
    return {
        title: "DeepSeekCode settings",
        subtitle: `${input.status.projectPath} / ${input.status.model}`,
        selectedTab,
        badge: selectedTab,
        badgeTone: selectedTab === "gates" && input.status.gates.approvalsPending > 0 ? "warning" : "brand",
        tabs: [
            { id: "status", title: "status", tone: "brand" },
            { id: "config", title: "config", tone: "muted" },
            { id: "usage", title: "usage", tone: "muted" },
            { id: "gates", title: "gates", count: input.status.gates.approvalsPending, tone: input.status.gates.approvalsPending > 0 ? "warning" : "muted" },
            { id: "theme", title: "theme", count: terminalThemes.length, tone: "brand" },
        ],
        rows: rowsForTab(selectedTab, input),
        actions: actionsForTab(selectedTab),
        cacheRatio,
        cacheHitTokens: input.status.cache.hitTokens,
        cacheMissTokens: input.status.cache.missTokens,
        themePicker: selectedTab === "theme"
            ? themePickerModel({ themes: terminalThemes, current: input.theme })
            : undefined,
        footer: "/settings status|config|usage|gates|theme",
    };
}
export function normalizeSettingsTab(value) {
    const normalized = value?.trim().toLowerCase();
    if (normalized === "config")
        return "config";
    if (normalized === "usage")
        return "usage";
    if (normalized === "gates" || normalized === "gate")
        return "gates";
    if (normalized === "theme" || normalized === "themes" || normalized === "appearance" || normalized === "color")
        return "theme";
    return "status";
}
function SettingsPanelRowView(props) {
    const detailWidth = Math.max(24, props.width - 42);
    const noteWidth = Math.max(24, props.width - 12);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.status, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: toneColor("brand"), children: truncateCells(props.row.label.padEnd(13), 13) }), _jsx(Text, { color: "gray", children: truncateCells(props.row.detail, detailWidth) })] }), props.row.note ? (_jsx(Box, { paddingLeft: 2, children: _jsx(Text, { color: "gray", children: truncateCells(props.row.note, noteWidth) }) })) : null] }));
}
function rowsForTab(tab, input) {
    if (tab === "config") {
        return [
            row("model", "active", "brand", input.status.model, input.status.providerReady ? "provider configured" : "provider missing"),
            row("theme", input.theme.source, "brand", input.theme.theme, input.theme.definition.description),
            row("style", input.outputStyle.scope, input.outputStyle.scope === "builtin" ? "brand" : "success", input.outputStyle.name, input.outputStyle.description),
            row("effort", input.inference.effort, input.inference.effort === "max" ? "warning" : "brand", `context=${input.inference.actionContextChars} dynamic=${input.inference.actionDynamicChars}`, `maxOutput=${input.inference.maxOutputTokens}`),
            row("project", "path", "muted", input.status.projectPath, "workspace root"),
            row("data", "path", "muted", input.status.dataDir, "runtime state and transcripts"),
        ];
    }
    if (tab === "usage") {
        return [
            row("cache", input.status.cache.rate, cacheTone(input.status.cache.hitTokens / Math.max(1, input.status.cache.hitTokens + input.status.cache.missTokens), input.status.cache.hitTokens + input.status.cache.missTokens), `observed runs=${input.status.cache.observedRuns}`, "DeepSeek cache hit rate matters for token savings"),
            row("runs", `${input.status.runs.totalRecent} recent`, input.status.runs.unfinished > 0 ? "warning" : "success", `unfinished=${input.status.runs.unfinished}`, input.status.runs.latest ? `latest=${input.status.runs.latest.id}` : "no runs"),
            row("tasks", `${input.status.tasks.queued} queued`, input.status.tasks.failed > 0 ? "error" : "muted", `running=${input.status.tasks.running} succeeded=${input.status.tasks.succeeded}`, `failed=${input.status.tasks.failed} paused=${input.status.tasks.paused}`),
            row("budget", input.inference.effort, input.inference.effort === "max" ? "warning" : "brand", `side=${input.inference.sideQuestionContextChars}/${input.inference.sideQuestionDynamicChars}`, `maxOutput=${input.inference.maxOutputTokens}`),
        ];
    }
    if (tab === "gates") {
        return [
            row("approval", `${input.status.gates.approvalsPending} pending`, input.status.gates.approvalsPending > 0 ? "warning" : "success", "/approval list", "manual approvals block tool execution until decided"),
            row("validation", `${input.status.gates.validationsPending} pending`, input.status.gates.validationsFailed > 0 ? "error" : "muted", `failed=${input.status.gates.validationsFailed}`, "/validation"),
            row("permissions", input.status.permissionProfile, permissionTone(input.status), `shell=${onOff(input.status.permissions.shell)} browser=${onOff(input.status.permissions.browser)}`, "/permissions profile safe|dev|browser|open"),
            row("git", input.status.git.available ? input.status.git.clean ? "clean" : "dirty" : "unavailable", gitTone(input.status), gitDetail(input.status), input.status.git.error ?? ""),
        ];
    }
    if (tab === "theme") {
        const colors = input.theme.definition.colors;
        return [
            row("current", input.theme.source, "brand", input.theme.theme, input.theme.definition.description),
            row("palette", "colors", "brand", `brand=${colors.brand ?? "default"} success=${colors.success ?? "default"}`, `warning=${colors.warning ?? "default"} error=${colors.error ?? "default"}`),
            row("override", process.env.DEEPSEEKCODE_THEME ? "env" : "project", process.env.DEEPSEEKCODE_THEME ? "warning" : "success", process.env.DEEPSEEKCODE_THEME ? "DEEPSEEKCODE_THEME" : ".deepseekcode/theme.json", "env override wins for the current process"),
            row("path", "config", "muted", input.theme.path, "/theme path shows the same file"),
        ];
    }
    return [
        row("provider", input.status.providerReady ? "ready" : "missing", input.status.providerReady ? "success" : "error", input.status.model, input.status.providerReady ? "DeepSeek provider configured" : "configure DEEPSEEK_API_KEY"),
        row("permissions", input.status.permissionProfile, permissionTone(input.status), `shell=${onOff(input.status.permissions.shell)} browser=${onOff(input.status.permissions.browser)}`, "use /permissions to switch profile"),
        row("theme", input.theme.theme, "brand", input.theme.definition.label, `source=${input.theme.source}`),
        row("runs", `${input.status.runs.unfinished} open`, input.status.runs.unfinished > 0 ? "warning" : "success", `recent=${input.status.runs.totalRecent}`, input.status.runs.latest ? `latest=${input.status.runs.latest.status} ${input.status.runs.latest.id}` : "no runs"),
        row("gates", `${input.status.gates.approvalsPending} approval`, input.status.gates.approvalsPending > 0 ? "warning" : "success", `validating=${input.status.gates.validationsPending}`, `failed validations=${input.status.gates.validationsFailed}`),
        row("git", input.status.git.available ? input.status.git.clean ? "clean" : "dirty" : "unavailable", gitTone(input.status), gitDetail(input.status), input.status.git.error ?? ""),
    ];
}
function actionsForTab(tab) {
    const common = (id, label, detail, description, tone = "brand") => ({
        id,
        label,
        detail,
        description,
        tone,
    });
    if (tab === "config") {
        return [
            common("config", "config", "/config", "show redacted runtime configuration"),
            common("theme", "theme", "/theme list", "inspect or change terminal theme"),
            common("style", "style", "/output-style list", "choose response style"),
            common("effort", "effort", "/effort status", "inspect DeepSeek prompt budget"),
        ];
    }
    if (tab === "usage") {
        return [
            common("usage", "usage", "/usage", "show token and cache usage"),
            common("stats", "stats", "/stats", "summarize workspace activity"),
            common("cost", "cost", "/cost", "estimate DeepSeek cost if pricing is configured"),
            common("cache", "cache plan", "/cache plan <goal>", "preview prompt budget before spending tokens", "success"),
        ];
    }
    if (tab === "gates") {
        return [
            common("approval", "approval", "/approval list", "decide pending tool approvals", "warning"),
            common("permissions", "permissions", "/permissions status", "review shell and browser safety"),
            common("validation", "validation", "/validation", "inspect validation gates"),
            common("plan", "plan", "/plan status", "review plan-mode approval state"),
        ];
    }
    if (tab === "theme") {
        return [
            common("theme-list", "theme list", "/theme list", "show all built-in terminal themes"),
            common("theme-cache", "cache green", "/theme set cache-green", "use the cache-first DeepSeek palette", "success"),
            common("theme-warm", "warm classic", "/theme set warm-classic", "use the warmer terminal palette"),
            common("theme-reset", "theme reset", "/theme reset", "return to the default DeepSeek palette", "warning"),
        ];
    }
    return [
        common("doctor", "doctor", "/doctor", "run diagnostics"),
        common("status", "status", "/status", "show detailed project status"),
        common("cache", "cache", "/cache doctor current", "inspect cache health", "success"),
        common("runs", "runs", "/runs", "review recent runs"),
    ];
}
function row(key, status, tone, detail, note) {
    return { key, label: key, status, tone, detail, note };
}
function cacheTone(ratio, total) {
    if (total <= 0)
        return "muted";
    if (ratio >= 0.75)
        return "success";
    if (ratio >= 0.35)
        return "warning";
    return "error";
}
function permissionTone(status) {
    if (status.permissions.shell && status.permissions.browser)
        return "warning";
    if (status.permissions.shell || status.permissions.browser)
        return "brand";
    return "success";
}
function gitTone(status) {
    if (!status.git.available)
        return "muted";
    if (status.git.conflicted > 0)
        return "error";
    return status.git.clean ? "success" : "warning";
}
function gitDetail(status) {
    const git = status.git;
    if (!git.available)
        return "git unavailable";
    if (git.clean)
        return "working tree clean";
    return `M=${git.modified} A=${git.added} D=${git.deleted} R=${git.renamed} ?=${git.untracked} U=${git.conflicted}`;
}
function onOff(value) {
    return value ? "on" : "off";
}
