"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveIgnoreInstallConfirmation = resolveIgnoreInstallConfirmation;
exports.renderInstallFlowText = renderInstallFlowText;
const output_1 = require("../../../lib/output");
async function resolveIgnoreInstallConfirmation(options) {
    const needsConfirmation = options.preview.proposedAdditions.length > 0;
    if (!needsConfirmation) {
        return {
            mode: "not-required",
            confirmed: true,
            skippedWriteReason: null
        };
    }
    if (options.yes) {
        return {
            mode: "yes",
            confirmed: true,
            skippedWriteReason: null
        };
    }
    if (options.canPrompt) {
        const confirmed = await requestConfirmation(options.preview.proposedAdditions, options.preview.gitignorePath);
        return {
            mode: "prompt",
            confirmed,
            skippedWriteReason: confirmed ? null : "Confirmation declined; no files were changed."
        };
    }
    return {
        mode: "non-interactive",
        confirmed: false,
        skippedWriteReason: "Confirmation required; rerun with --yes or use an interactive terminal."
    };
}
function renderInstallFlowText(options) {
    const lines = [
        (0, output_1.renderTerminalHeading)("Ignore Install", "title"),
        (0, output_1.renderTerminalSection)({
            heading: "Summary",
            fields: [
                { label: "Contract version", value: "v1" },
                { label: "Outcome", value: options.preview.outcome },
                { label: "Existing canonical entries", value: options.preview.existingCanonicalEntries.length },
                { label: "Proposed additions", value: options.preview.proposedAdditions.length },
                { label: "Action", value: options.applyResult.action }
            ]
        }),
        "",
        (0, output_1.renderTerminalSection)({
            heading: "Paths",
            fields: [
                { label: "Repository root", value: options.repositoryRoot },
                { label: "Destination", value: options.targetPath }
            ]
        }),
        "",
        (0, output_1.renderTerminalCompactList)({
            heading: "Existing Canonical Entries",
            entries: options.preview.existingCanonicalEntries.map((entry) => ({
                label: entry
            })),
            emptyMessage: "No canonical entries are currently installed."
        }),
        "",
        (0, output_1.renderTerminalCompactList)({
            heading: "Proposed Additions",
            entries: options.preview.proposedAdditions.map((entry) => ({
                label: entry
            })),
            emptyMessage: "No additions are needed; canonical entries are already installed."
        })
    ];
    if (options.applyResult.changed) {
        lines.push("", (0, output_1.renderTerminalSection)({
            heading: "Write Boundary",
            lines: [
                (0, output_1.renderTerminalBoundary)({
                    kind: "write-applied",
                    target: options.targetPath,
                    targetLabel: "Destination"
                }),
                options.applyResult.action === "create-file"
                    ? "Applied: created .gitignore with canonical Day Shift entries."
                    : "Applied: appended missing canonical Day Shift entries to .gitignore."
            ]
        }));
    }
    else if (options.preview.proposedAdditions.length === 0) {
        lines.push("", (0, output_1.renderTerminalSection)({
            heading: "Write Boundary",
            lines: [
                (0, output_1.renderTerminalBoundary)({
                    kind: "mutating-no-op",
                    reason: "Canonical Day Shift entries are already installed.",
                    target: options.targetPath,
                    targetLabel: "Destination"
                }),
                "No changes needed; canonical Day Shift entries are already installed."
            ]
        }));
    }
    else if (options.skippedWriteReason !== null) {
        lines.push("", (0, output_1.renderTerminalSection)({
            heading: "Write Boundary",
            lines: [
                (0, output_1.renderTerminalBoundary)({
                    kind: "no-write-failure",
                    reason: options.skippedWriteReason
                })
            ]
        }), "", (0, output_1.renderTerminalSection)({
            heading: "Recovery",
            lines: [options.skippedWriteReason]
        }));
    }
    return lines.join("\n");
}
async function requestConfirmation(proposedAdditions, gitignorePath) {
    const { createInterface } = await import("node:readline/promises");
    const readline = createInterface({
        input: process.stdin,
        output: process.stdout
    });
    const lines = [
        `Proposed additions for ${gitignorePath}:`,
        ...proposedAdditions.map((entry) => `- ${entry}`),
        "Apply these additions? [y/N] "
    ];
    try {
        const answer = await readline.question(lines.join("\n"));
        return /^(y|yes)$/i.test(answer.trim());
    }
    finally {
        readline.close();
    }
}
