"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLicenseInstallCommand = createLicenseInstallCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const license_install_1 = require("../../../lib/license/license-install");
const license_guidance_1 = require("../../../lib/license/license-guidance");
const global_format_1 = require("../global-format");
async function createLicenseInstallCommand() {
    const { Command } = await import("commander");
    const command = new Command("install");
    command
        .description("Install a signed Day Shift license file into user-local license storage.")
        .argument("<license-file>", "Signed Day Shift license file to install.")
        .addHelpText("after", [
        "",
        "Commercial guidance:",
        ...(0, license_guidance_1.renderLicenseWebsiteGuidanceLines)().map((line) => `  ${line}`)
    ].join("\n"))
        .action((licenseFile, ...actionArgs) => {
        const commandInstance = actionArgs.at(-1);
        const requestedFormat = commandInstance.optsWithGlobals?.().format ?? (0, global_format_1.getGlobalFormat)(commandInstance);
        const format = (0, output_1.resolveOutputFormat)(requestedFormat);
        if (format === null) {
            throw (0, errors_1.createUnsupportedOutputFormatError)({
                received: requestedFormat,
                commandBoundary: "license install command output format"
            });
        }
        const result = (0, license_install_1.installLicenseFile)({ sourcePath: licenseFile });
        const data = createLicenseInstallCommandData(result);
        (0, output_1.emitSuccess)({
            command: "license install",
            format,
            text: renderLicenseInstallText(data),
            data
        });
        getRootCommand(commandInstance ?? command).runtimeExitCode =
            data.exit_code === 0 ? errors_1.RuntimeExitCode.SUCCESS : errors_1.RuntimeExitCode.ERROR;
    });
    return command;
}
function createLicenseInstallCommandData(result) {
    return {
        installed: result.installed,
        source_path: result.sourcePath,
        license_directory: result.licenseDirectory,
        installed_path: result.installedPath,
        installed_filename: result.installedFilename,
        replaced_existing: result.replacedExisting,
        host_license_path: result.hostLicensePath,
        automation_host_guidance: result.automationHostGuidance,
        failure_reason: result.ok ? null : result.reason,
        message: result.ok
            ? "License installed into user-local license storage."
            : result.message,
        exit_code: result.ok ? 0 : 2
    };
}
function renderLicenseInstallText(data) {
    return [
        (0, output_1.renderTerminalSection)({
            heading: "License install",
            headingStyle: "title",
            fields: [
                { label: "Installed", value: data.installed },
                { label: "Source path", value: data.source_path },
                { label: "License directory", value: data.license_directory },
                { label: "Installed filename", value: data.installed_filename },
                { label: "Installed path", value: data.installed_path },
                { label: "Replaced existing", value: data.replaced_existing },
                { label: "Failure reason", value: data.failure_reason },
                { label: "Message", value: data.message },
                { label: "Exit code", value: data.exit_code }
            ]
        }),
        (0, output_1.renderTerminalSection)({
            heading: "Automation host guidance",
            headingStyle: "section",
            fields: [
                { label: "Host-local automation path", value: data.host_license_path }
            ],
            lines: [data.automation_host_guidance]
        }),
        (0, output_1.renderTerminalSection)({
            heading: "Commercial guidance",
            headingStyle: "section",
            lines: (0, license_guidance_1.renderLicenseWebsiteGuidanceLines)()
        }),
        data.installed
            ? (0, output_1.renderTerminalBoundary)({
                kind: "write-applied",
                target: data.installed_path,
                targetLabel: "Installed license",
                stateSurface: "user-local license directory"
            })
            : (0, output_1.renderTerminalBoundary)({
                kind: "no-write-failure",
                reason: data.failure_reason ?? "license install failed"
            })
    ].join("\n\n");
}
function getRootCommand(commandInstance) {
    let current = commandInstance;
    while (current.parent !== undefined && current.parent !== null) {
        current = current.parent;
    }
    return current;
}
