import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import fs from "node:fs";
import path from "node:path";
import { Box, Text } from "ink";
import { flattenCellText, truncateCells } from "./design/textLayout.js";
const DEFAULT_MAX_LINES = 8;
const DEFAULT_WIDTH = 96;
export function PlanApprovalPreviewBlock(props) {
    const model = planApprovalPreviewModel(props.projectPath, props.runId, props.width ?? DEFAULT_WIDTH, props.maxLines ?? DEFAULT_MAX_LINES);
    if (!model)
        return null;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: "cyan", children: model.title }), " ", _jsx(Text, { color: "gray", children: model.path })] }), _jsx(Text, { color: "gray", children: model.size }), model.preview.map((line, index) => (_jsx(Text, { color: "gray", children: ` ${line}` }, `${index}:${line}`))), model.clipped ? _jsx(Text, { color: "gray", children: " ... plan preview clipped ..." }) : null] }));
}
export function planApprovalPreviewModel(projectPath, runId, width = DEFAULT_WIDTH, maxLines = DEFAULT_MAX_LINES) {
    if (!projectPath)
        return null;
    const relativePath = path.join(".deepseekcode", "plans", `${safePlanName(runId)}.md`).replace(/\\/g, "/");
    const filePath = path.join(projectPath, relativePath);
    let content = "";
    try {
        content = fs.readFileSync(filePath, "utf8");
    }
    catch {
        return null;
    }
    return planApprovalPreviewModelFromContent(content, relativePath, width, maxLines);
}
export function planApprovalPreviewModelFromContent(content, relativePath, width = DEFAULT_WIDTH, maxLines = DEFAULT_MAX_LINES) {
    const lines = content.split(/\r?\n/);
    const meaningful = lines
        .map((line) => flattenCellText(line.replace(/^#+\s*/, "")))
        .filter(Boolean);
    const preview = meaningful
        .slice(0, Math.max(0, maxLines))
        .map((line) => truncateCells(line, Math.max(24, width - 4)));
    return {
        title: "Plan preview",
        path: "draft plan",
        storagePath: relativePath,
        size: `${content.length} chars / ${content ? lines.length : 0} lines`,
        preview,
        clipped: meaningful.length > preview.length,
    };
}
function safePlanName(value) {
    return value.trim().replace(/[^a-zA-Z0-9._-]/g, "-") || "current";
}
