"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConfigCommand = createConfigCommand;
const node_fs_1 = __importDefault(require("node:fs"));
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const global_format_1 = require("../global-format");
const config_store_1 = require("../../../lib/config/config-store");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
function getRepositoryContext() {
    return (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
}
function getConfigCommandFormat(commandInstance) {
    return commandInstance.optsWithGlobals?.().format ?? (0, global_format_1.getGlobalFormat)(commandInstance);
}
const CONFIG_GET_SELECTOR_HELP = [
    "Supported selectors:",
    "  - default_readiness, cli_command, cli_resolution, cli_verified, cli_verified_via, editor",
    "  - structural_pressure (family)",
    "  - structural_pressure.source (family), structural_pressure.test (family)",
    "  - structural_pressure.excluded_extensions, structural_pressure.blocked_segments, structural_pressure.excluded_basenames",
    "  - structural_pressure.source.watch, structural_pressure.source.pressure, structural_pressure.source.hotspot",
    "  - structural_pressure.test.watch, structural_pressure.test.pressure, structural_pressure.test.hotspot"
].join("\n");
const CONFIG_SET_KEY_HELP = [
    "Writable keys:",
    "  - default_readiness, cli_command, cli_resolution, cli_verified, cli_verified_via, editor",
    "  - structural_pressure.excluded_extensions, structural_pressure.blocked_segments, structural_pressure.excluded_basenames",
    "  - structural_pressure.source.watch, structural_pressure.source.pressure, structural_pressure.source.hotspot",
    "  - structural_pressure.test.watch, structural_pressure.test.pressure, structural_pressure.test.hotspot",
    "",
    "Array value example:",
    "  day-shift config set structural_pressure.excluded_basenames '[\"pnpm-lock.yaml\",\"package-lock.json\"]'"
].join("\n");
async function createConfigCommand() {
    const { Command } = await import("commander");
    const command = new Command("config");
    command.description("Read or write layered Day Shift configuration.");
    command
        .command("get [selector]")
        .description("Show the resolved configuration values.")
        .addHelpText("after", `\n${CONFIG_GET_SELECTOR_HELP}\n`)
        .action((selector, ...actionArgs) => {
        const commandInstance = actionArgs.at(-1);
        const requestedFormat = getConfigCommandFormat(commandInstance);
        const format = (0, output_1.resolveOutputFormat)(requestedFormat);
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String(requestedFormat)}`]);
        }
        const result = (0, config_store_1.readResolvedConfig)(getRepositoryContext());
        const selection = selector === undefined
            ? undefined
            : (0, config_store_1.resolveConfigGetSelection)({
                resolvedConfig: result.resolved,
                selector
            });
        const data = {
            values: result.resolved.values,
            sources: result.resolved.sources,
            derived: result.resolved.derived,
            resolvedEntries: result.resolved.resolvedEntries,
            ...(selection === undefined
                ? {}
                : {
                    selection: {
                        selector: selection.selector,
                        kind: selection.kind,
                        selectedEntryKeys: selection.entryKeys,
                        selectedEntries: selection.entries
                    }
                })
        };
        (0, output_1.emitSuccess)({
            command: "config get",
            format,
            text: (0, config_store_1.createConfigGetText)(result, selection),
            data
        });
    });
    command
        .command("inventory")
        .description("List catalog-backed configuration inventory without writing files.")
        .option("--license-file <path>", "Use a license file for this inventory run without writing config.")
        .action((...actionArgs) => {
        const commandInstance = actionArgs.at(-1);
        const requestedFormat = getConfigCommandFormat(commandInstance);
        const format = (0, output_1.resolveOutputFormat)(requestedFormat);
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String(requestedFormat)}`]);
        }
        const commandOptions = commandInstance.opts();
        const result = (0, config_store_1.readResolvedConfig)(getRepositoryContext(), {
            includeLicenseProvenance: true,
            licenseFile: commandOptions.licenseFile
        });
        const inventory = (0, config_store_1.createConfigInventory)(result);
        (0, output_1.emitSuccess)({
            command: "config inventory",
            format,
            text: (0, config_store_1.createConfigInventoryText)(inventory),
            data: inventory
        });
    });
    command
        .command("set <key> <value>")
        .description("Set a writable configuration value.")
        .addHelpText("after", `\n${CONFIG_SET_KEY_HELP}\n`)
        .action((key, value, ...actionArgs) => {
        const commandInstance = actionArgs.at(-1);
        const requestedFormat = getConfigCommandFormat(commandInstance);
        const format = (0, output_1.resolveOutputFormat)(requestedFormat);
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String(requestedFormat)}`]);
        }
        const result = (0, config_store_1.writeConfigValue)({
            repositoryContext: getRepositoryContext(),
            key,
            value
        });
        node_fs_1.default.mkdirSync(require("node:path").dirname(result.filePath), { recursive: true });
        node_fs_1.default.writeFileSync(result.filePath, result.contents, "utf8");
        (0, output_1.emitSuccess)({
            command: "config set",
            format,
            text: (0, config_store_1.createConfigSetText)(result),
            data: {
                key: result.key,
                filePath: result.filePath,
                targetLayer: result.targetLayer,
                targetRelativePath: result.targetRelativePath,
                acceptedValue: result.acceptedValue,
                displayValue: result.displayValue,
                preservationStrategy: result.preservationStrategy,
                value: result.displayValue
            }
        });
    });
    return command;
}
