"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReleaseCommand = createReleaseCommand;
exports.createReleaseSubcommand = createReleaseSubcommand;
// Responsibility-focused extraction from index-command-surface.ts.
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const registration_1 = require("../registration");
const output_lifecycle_1 = require("./output-lifecycle");
const index_build_preflight_1 = require("./index-build-preflight");
const index_command_contracts_1 = require("./index-command-contracts");
const index_command_orchestration_1 = require("./index-command-orchestration");
const index_contract_renderer_1 = require("./index-contract-renderer");
const index_local_validation_1 = require("./index-local-validation");
const index_command_surface_mapping_1 = require("./index-command-surface-mapping");
async function createReleaseCommand() {
    const { Command } = await import('commander');
    const command = new Command('release');
    command.description('Coordinate local release command surfaces without public npm publication or website upload.');
    for (const releaseSubcommand of index_command_contracts_1.RELEASE_SUBCOMMANDS) {
        (0, registration_1.registerCommand)(command, createReleaseSubcommand(Command, releaseSubcommand));
    }
    return command;
}
function createReleaseSubcommand(Command, releaseSubcommand) {
    const command = new Command(releaseSubcommand.name);
    command.description(releaseSubcommand.description);
    (0, index_contract_renderer_1.addReleaseOptions)(command, releaseSubcommand.options);
    command.action((options, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)(commandInstance.optsWithGlobals().format);
        if (format === null) {
            throw (0, errors_1.createUnsupportedOutputFormatError)({
                received: commandInstance.optsWithGlobals().format,
                commandBoundary: `release ${releaseSubcommand.name} command output format`,
            });
        }
        const commandName = (0, index_command_surface_mapping_1.releaseSubcommandHasOption)(releaseSubcommand, 'commandName')
            ? (0, index_command_surface_mapping_1.requireInstalledCommandName)(releaseSubcommand, options.commandName)
            : null;
        const parsedVersion = (0, index_command_surface_mapping_1.requireReleaseVersion)(releaseSubcommand, options.releaseVersion);
        const dirty = (0, index_command_surface_mapping_1.releaseSubcommandHasOption)(releaseSubcommand, 'dirty') && options.dirty === true;
        const effectiveOutputDir = (0, index_command_surface_mapping_1.resolveReleaseOutputDirOption)({
            outputDir: options.outputDir,
            dirty,
        });
        const repositoryState = (0, index_command_orchestration_1.checkReleaseState)(releaseSubcommand, effectiveOutputDir, commandInstance, dirty);
        const dryRun = options.dryRun === true;
        const rootCommand = (0, index_contract_renderer_1.getRootCommand)(commandInstance);
        const repositoryRoot = rootCommand.configRepositoryContext?.root ?? process.cwd();
        const outputState = (0, output_lifecycle_1.resolveReleaseOutputState)({
            repositoryRoot,
            outputDir: effectiveOutputDir,
            version: parsedVersion.ok ? parsedVersion.version : null,
        });
        const invocation = commandName === null
            ? null
            : (0, index_command_surface_mapping_1.createReleaseCommandInvocationMapping)({
                installedCommandName: commandName,
                stagedExecutablePath: (0, index_command_surface_mapping_1.resolveStagedReleaseExecutablePath)(outputState, commandName),
                args: (0, index_command_surface_mapping_1.createReleaseInvocationArgs)(releaseSubcommand, parsedVersion, options, effectiveOutputDir),
            });
        const packageBoundary = (0, index_command_surface_mapping_1.createReleasePackageBoundaryMetadata)({
            repositoryRoot,
            installedCommandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
        });
        const buildPreflight = (0, index_build_preflight_1.resolveReleaseBuildPreflight)({
            releaseSubcommand,
            dryRun,
            repositoryRoot,
        });
        const operation = (0, index_command_orchestration_1.resolveReleaseOutputOperation)({
            releaseSubcommand,
            dryRun,
            confirm: options.confirm === true,
            keep: options.keep,
            version: parsedVersion.ok ? parsedVersion.version : null,
            outputState,
        });
        const releaseNotesSourceSummary = (0, index_command_orchestration_1.resolveReleaseNotesSourceSummary)({
            releaseSubcommand,
            repositoryRoot,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
        });
        const releaseNotesDraft = (0, index_command_orchestration_1.resolveReleaseNotesDraft)({
            releaseSubcommand,
            dryRun,
            repositoryRoot,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            releaseNotesSourceSummary,
        });
        const effectiveReleaseNotesSourceSummary = releaseNotesDraft.sourceSummary ?? releaseNotesSourceSummary;
        const stagingPayload = (0, index_command_orchestration_1.resolveReleaseStagingPayload)({
            releaseSubcommand,
            dryRun,
            repositoryRoot,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            installedCommandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            operation,
            releaseNotesSourceSummary: effectiveReleaseNotesSourceSummary,
        });
        const installationReadme = (0, index_command_orchestration_1.resolveReleaseInstallationReadme)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            commandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            packageName: packageBoundary.name,
            requiredNodeVersion: packageBoundary.engines.node,
        });
        const archivePayloads = (0, index_command_orchestration_1.resolveReleaseArchivePayloads)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            installedCommandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            packageName: packageBoundary.name,
            stagingPayload,
        });
        const installerAssets = (0, index_command_orchestration_1.resolveReleaseInstallerAssets)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            installedCommandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            archivePayloads,
        });
        const artifactChecksums = (0, index_command_orchestration_1.resolveReleaseArtifactChecksums)({
            releaseSubcommand,
            dryRun,
            outputState,
            archivePayloads,
            installerAssets,
        });
        const releaseManifest = (0, index_command_orchestration_1.resolveReleaseManifest)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            commandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            stagedCommandPath: invocation?.stagedRelease.executable ??
                (0, index_command_surface_mapping_1.resolveStagedReleaseExecutablePath)(outputState, commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME),
            cliVersion: packageBoundary.version,
            requiredNodeVersion: packageBoundary.engines.node,
            artifactChecksums,
            releaseNotesSourceSummary: effectiveReleaseNotesSourceSummary,
        });
        const downloadBundle = (0, index_command_orchestration_1.resolveReleaseDownloadBundle)({
            releaseSubcommand,
            dryRun,
            outputState,
            stagingPayload,
            archivePayloads,
            installerAssets,
            artifactChecksums,
            releaseManifest,
            installationReadme,
        });
        const installerSafetyValidation = (0, index_command_orchestration_1.resolveReleaseInstallerSafetyValidation)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            commandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            stagedCommandPath: invocation?.stagedRelease.executable ??
                (0, index_command_surface_mapping_1.resolveStagedReleaseExecutablePath)(outputState, commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME),
            archivePayloads,
            releaseManifest,
        });
        const localValidation = (0, index_command_orchestration_1.resolveReleaseLocalValidation)({
            releaseSubcommand,
            dryRun,
            repositoryRoot,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            commandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            stagingPayload,
            archivePayloads,
        });
        const localValidationReport = (0, index_local_validation_1.writeReleaseLocalValidationReport)({
            releaseSubcommand,
            dryRun,
            outputState,
            version: parsedVersion.ok ? parsedVersion.version : null,
            commandName: commandName ?? index_command_contracts_1.DEFAULT_INSTALLED_COMMAND_NAME,
            installerSafetyValidation,
            localValidation,
        });
        const successMarker = parsedVersion.ok
            ? (0, index_contract_renderer_1.resolveReleaseSuccessMarkerState)({
                releaseSubcommand,
                dryRun,
                outputState,
                version: parsedVersion.version,
                operation,
            })
            : (0, index_contract_renderer_1.createUnavailableMarkerState)();
        const data = {
            command: `release ${releaseSubcommand.name}`,
            mode: releaseSubcommand.mode,
            boundary: releaseSubcommand.boundary,
            noWriteInThisMilestone: dryRun || !operation.changed,
            options: {
                version: parsedVersion.ok ? parsedVersion.version : null,
                commandName,
                outputDir: effectiveOutputDir,
                keep: options.keep ?? null,
                confirm: options.confirm === true,
                dryRun,
                dirty,
            },
            invocation,
            packageBoundary,
            buildPreflight,
            stagingPayload,
            archivePayloads,
            installerAssets,
            artifactChecksums,
            releaseManifest,
            downloadBundle,
            installationReadme,
            releaseNotesSourceSummary: effectiveReleaseNotesSourceSummary,
            releaseNotesDraft,
            installerSafetyValidation,
            localValidation,
            localValidationReport,
            output: outputState,
            operation,
            successMarker,
            repositoryState,
            publication: {
                websiteUpload: 'out-of-scope',
                publicNpmPublication: 'out-of-scope',
            },
            nextImplementation: 'downstream release pipeline milestones implement payload internals',
        };
        (0, output_1.emitSuccess)({
            command: `release ${releaseSubcommand.name}`,
            format,
            text: (0, index_contract_renderer_1.renderReleaseContractSummary)(data),
            data,
        });
    });
    return command;
}
