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";
export function QuestionApprovalPreviewBlock(props) {
    const model = questionApprovalPreviewModel(props.projectPath, props.gateId, props.width ?? 96);
    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.status })] }), model.questions.map((question, index) => (_jsxs(Box, { flexDirection: "column", paddingLeft: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: "cyan", children: `${index + 1}. ${question.header}` }), " ", _jsx(Text, { children: question.question }), question.multiSelect ? _jsx(Text, { color: "gray", children: " multi" }) : null] }), question.options.map((option) => (_jsxs(Box, { flexDirection: "column", paddingLeft: 2, children: [_jsxs(Text, { children: [_jsx(Text, { color: "gray", children: option.label.padEnd(10) }), _jsx(Text, { color: "gray", children: option.description })] }), option.preview ? _jsx(Text, { color: "gray", children: `preview ${option.preview}` }) : null] }, option.label)))] }, `${index}:${question.header}`)))] }));
}
export function questionApprovalPreviewModel(projectPath, gateId, width = 96) {
    if (!projectPath)
        return null;
    const record = readQuestionApprovalRecord(projectPath, gateId);
    if (!record)
        return null;
    return questionApprovalPreviewModelFromRecord(record, width);
}
export function questionApprovalPreviewModelFromRecord(record, width = 96) {
    const questionWidth = Math.max(24, width - 16);
    const optionWidth = Math.max(24, width - 18);
    return {
        title: "Question preview",
        status: record.status,
        questions: record.questions.map((question) => ({
            header: truncateCells(flattenCellText(question.header), 18),
            question: truncateCells(flattenCellText(question.question), questionWidth),
            multiSelect: Boolean(question.multiSelect),
            options: question.options.map((option) => ({
                label: truncateCells(flattenCellText(option.label), 28),
                description: truncateCells(flattenCellText(option.description), optionWidth),
                preview: option.preview ? truncateCells(flattenCellText(option.preview), optionWidth) : "",
            })),
        })),
    };
}
function readQuestionApprovalRecord(projectPath, gateId) {
    const filePath = path.join(projectPath, ".deepseekcode", "questions", `${safeName(gateId)}.json`);
    try {
        return JSON.parse(fs.readFileSync(filePath, "utf8"));
    }
    catch {
        return null;
    }
}
function safeName(value) {
    return value.replace(/[^a-zA-Z0-9._-]/g, "-") || "question";
}
