import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { FilePathLink, isLikelyLocalPath } from "./FilePathLink.js";
import { ModelPicker, modelPickerModel } from "./ModelPicker.js";
export function ProjectPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(56, Math.min(112, columns - 4));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "project", tone: "brand", paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: "cyan", 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, { flexDirection: "column", marginTop: 1, children: props.model.rows.length === 0 ? (_jsx(Text, { color: "gray", children: "No project details" })) : props.model.rows.map((row) => (_jsx(ProjectPanelRowView, { row: row, width: width }, row.key))) }), props.model.modelPicker ? (_jsx(ModelPicker, { model: props.model.modelPicker, width: width - 2 })) : null, _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function modelPanelModel(input) {
    return {
        title: "Model",
        subtitle: input.providerReady ? "DeepSeek provider configured" : "Provider configuration missing",
        badge: input.providerReady ? "ready" : "missing",
        badgeTone: input.providerReady ? "success" : "warning",
        rows: [
            {
                key: "model",
                label: "active",
                value: input.verifiedModel || input.model,
                tone: "brand",
            },
            {
                key: "provider",
                label: "provider",
                value: input.providerName || "not configured",
                tone: input.providerReady ? "success" : "warning",
            },
            ...(input.verifiedText ? [{
                    key: "reply",
                    label: "verify",
                    value: input.verifiedText,
                    tone: "success",
                }] : []),
        ],
        modelPicker: modelPickerModel({
            activeModel: input.model,
            providerName: input.providerName,
            providerReady: input.providerReady,
            verifiedModel: input.verifiedModel,
        }),
        footer: "/model verify | /config | /doctor",
    };
}
export function branchPanelModel(input) {
    return {
        title: "Git branch",
        subtitle: input.branchOk ? "Current workspace branch and status" : "Git branch unavailable",
        badge: input.branchOk ? "git" : "unavailable",
        badgeTone: input.branchOk ? "success" : "warning",
        rows: [
            {
                key: "branch",
                label: "branch",
                value: input.branchOk ? input.branch || "(detached HEAD or unnamed)" : input.error || "unknown",
                tone: input.branchOk ? "brand" : "warning",
            },
            {
                key: "status",
                label: "status",
                value: input.gitStatus,
                tone: input.gitStatus === "clean" ? "success" : "warning",
            },
        ],
        footer: "/diff git | /status | /review git",
    };
}
export function projectPanelModel(input) {
    return {
        title: "Project",
        subtitle: input.projectPath,
        badge: input.permissionProfile,
        badgeTone: input.permissionProfile === "safe" ? "success" : "warning",
        rows: [
            { key: "project", label: "project", value: input.projectPath, tone: "brand" },
            { key: "data", label: "data", value: input.dataDir, tone: "muted" },
            { key: "state", label: "state", value: input.stateDbPath, tone: "muted" },
            { key: "model", label: "model", value: input.model, tone: "brand" },
            { key: "permissions", label: "permissions", value: input.permissionProfile, tone: input.permissionProfile === "safe" ? "success" : "warning" },
        ],
        footer: "/status | /config | /init",
    };
}
export function initPanelModel(result, force) {
    const rows = [
        ...result.created.map((item) => ({
            key: `created-${item}`,
            label: "created",
            value: item,
            tone: "success",
        })),
        ...result.existing.map((item) => ({
            key: `existing-${item}`,
            label: force ? "reused" : "existing",
            value: item,
            tone: "muted",
        })),
    ];
    return {
        title: "Init",
        subtitle: `${result.created.length} created | ${result.existing.length} existing`,
        badge: result.created.length > 0 ? "updated" : "ready",
        badgeTone: result.created.length > 0 ? "success" : "brand",
        rows,
        footer: "/memory | /cache pin | /plugins | /skills",
    };
}
export function exportPanelModel(input) {
    return {
        title: "Export",
        subtitle: `${input.kind}${input.id ? ` ${input.id}` : ""}`,
        badge: input.result.format,
        badgeTone: "success",
        rows: [
            { key: "path", label: "path", value: input.result.path, tone: "brand" },
            { key: "bytes", label: "bytes", value: String(input.result.bytes), tone: "success" },
            { key: "format", label: "format", value: input.result.format, tone: "muted" },
        ],
        footer: "/export run attached | /export status | /trace attached",
    };
}
function ProjectPanelRowView(props) {
    const labelWidth = 13;
    const valueWidth = Math.max(24, props.width - labelWidth - 8);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.label, tone: props.row.tone ?? "muted" }), _jsx(Text, { children: " " }), isLikelyLocalPath(props.row.value) ? (_jsx(FilePathLink, { filePath: props.row.value, width: valueWidth, color: "gray" })) : (_jsx(Text, { color: "gray", children: truncateCells(props.row.value, valueWidth) }))] }), props.row.note ? (_jsx(Text, { color: "gray", children: truncateCells(`  ${props.row.note}`, Math.max(24, props.width - 4)) })) : null] }));
}
