"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveReleaseInstallerSafetyValidation = resolveReleaseInstallerSafetyValidation;
exports.resolveReleaseLocalValidation = resolveReleaseLocalValidation;
const output_lifecycle_1 = require("./output-lifecycle");
const index_local_validation_1 = require("./index-local-validation");
function resolveReleaseInstallerSafetyValidation(options) {
    const installRoot = options.outputState.versionDirectoryRelative === null
        ? null
        : `${options.outputState.versionDirectoryRelative}/installer-validation`;
    if (options.releaseSubcommand.name !== 'build') {
        return {
            status: 'not-applicable',
            installRoot,
            targets: [],
        };
    }
    if (options.dryRun) {
        return {
            status: 'skipped-dry-run',
            installRoot,
            targets: [],
        };
    }
    if (options.version === null ||
        options.outputState.versionDirectory === null ||
        options.outputState.versionDirectoryRelative === null ||
        options.archivePayloads.status !== 'created' ||
        options.releaseManifest.status !== 'created' ||
        options.releaseManifest.data === null ||
        installRoot === null) {
        return {
            status: 'not-applicable',
            installRoot,
            targets: [],
        };
    }
    return {
        status: 'passed',
        installRoot,
        targets: options.archivePayloads.targets.map((target) => (0, output_lifecycle_1.validateReleaseInstallerSafety)({
            versionDirectory: options.outputState.versionDirectory,
            versionDirectoryRelative: options.outputState.versionDirectoryRelative,
            expectedVersion: options.version,
            expectedInstalledCommandName: options.commandName,
            expectedStagedCommandPath: options.stagedCommandPath,
            manifest: options.releaseManifest.data,
            target: target.target,
            payloadDirectory: target.payloadDirectory,
            installRoot: `${installRoot}/${target.target}`,
            nodeVersion: process.version,
        })),
    };
}
function resolveReleaseLocalValidation(options) {
    if (options.dryRun) {
        return {
            status: 'skipped-dry-run',
            staging: null,
            archives: [],
        };
    }
    if (options.releaseSubcommand.name === 'build' &&
        options.version !== null &&
        options.stagingPayload.status === 'created' &&
        options.archivePayloads.status === 'created') {
        return {
            status: 'requires-post-build-check',
            staging: null,
            archives: [],
        };
    }
    if (options.releaseSubcommand.name === 'check') {
        const validationInputs = (0, index_local_validation_1.resolveExistingReleaseValidationInputs)({
            repositoryRoot: options.repositoryRoot,
            outputState: options.outputState,
            version: options.version,
            commandName: options.commandName,
        });
        if (validationInputs === null) {
            return {
                status: 'not-applicable',
                staging: null,
                archives: [],
            };
        }
        const staging = (0, index_local_validation_1.validateReleaseExecutable)({
            executable: validationInputs.stagedExecutable,
            artifactRoot: validationInputs.stagingArtifactRoot,
            repositoryRoot: options.repositoryRoot,
            versionDirectory: validationInputs.versionDirectory,
            version: validationInputs.version,
            commandName: options.commandName,
        });
        const archives = validationInputs.archivePayloads.targets.map((target) => (0, index_local_validation_1.validateReleaseArchiveExecution)({
            target,
            repositoryRoot: options.repositoryRoot,
            versionDirectory: validationInputs.versionDirectory,
            version: validationInputs.version,
            commandName: options.commandName,
        }));
        return {
            status: 'passed',
            staging,
            archives,
        };
    }
    if (options.version === null ||
        options.outputState.versionDirectory === null ||
        options.stagingPayload.status !== 'created' ||
        options.stagingPayload.stagingDirectory === null ||
        options.archivePayloads.status !== 'created') {
        return {
            status: 'not-applicable',
            staging: null,
            archives: [],
        };
    }
    return {
        status: 'not-applicable',
        staging: null,
        archives: [],
    };
}
