"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInstallCommand = createInstallCommand;
const node_path_1 = __importDefault(require("node:path"));
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const global_format_1 = require("../global-format");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const workspace_ignore_1 = require("../../../lib/workspace-ignore");
const install_flow_1 = require("./install-flow");
async function createInstallCommand() {
    const { Command } = await import("commander");
    const command = new Command("install");
    command
        .description("Preview and safely apply canonical Day Shift local-only ignore entries.")
        .option("--yes", "Apply proposed additions without interactive confirmation.")
        .action(async (_, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(commandInstance));
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String((0, global_format_1.getGlobalFormat)(commandInstance))}`]);
        }
        const repositoryContext = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const preview = (0, workspace_ignore_1.createIgnoreInstallPreview)(repositoryContext.root);
        const yes = Boolean(commandInstance.opts().yes);
        const canPrompt = process.stdin.isTTY === true && process.stdout.isTTY === true;
        const confirmation = await (0, install_flow_1.resolveIgnoreInstallConfirmation)({ preview, yes, canPrompt });
        const applyResult = confirmation.confirmed
            ? (0, workspace_ignore_1.applyIgnoreInstallPreview)(preview)
            : {
                changed: false,
                action: "none",
                writtenEntries: []
            };
        const targetPath = node_path_1.default.relative(repositoryContext.root, preview.gitignorePath) || ".gitignore";
        (0, output_1.emitSuccess)({
            command: "ignore install",
            format,
            text: (0, install_flow_1.renderInstallFlowText)({
                repositoryRoot: repositoryContext.root,
                targetPath,
                preview,
                applyResult,
                skippedWriteReason: confirmation.skippedWriteReason
            }),
            data: {
                contractVersion: "v1",
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                localOnlyIgnoreEntries: (0, workspace_ignore_1.getCanonicalDayShiftLocalIgnoreEntries)(),
                localOnlyIgnoreRules: workspace_ignore_1.CANONICAL_DAY_SHIFT_LOCAL_IGNORE_RULES,
                trackableSharedAssetPaths: workspace_ignore_1.TRACKABLE_DAY_SHIFT_SHARED_ASSET_PATHS,
                preview: {
                    targetPath,
                    outcome: preview.outcome,
                    existingCanonicalEntries: preview.existingCanonicalEntries,
                    proposedAdditions: preview.proposedAdditions
                },
                confirmation: {
                    mode: confirmation.mode,
                    confirmed: confirmation.confirmed
                },
                apply: applyResult,
                notes: [
                    "Install flow consumes the canonical rule set from milestone 01.",
                    "Apply behavior appends only missing canonical entries or creates .gitignore when absent."
                ]
            }
        });
    });
    return command;
}
