"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseReleaseVersion = parseReleaseVersion;
exports.describeReleaseVersionRejection = describeReleaseVersionRejection;
const cli_constants_1 = require("../../../lib/config/cli-constants");
function parseReleaseVersion(value) {
    if (typeof value !== 'string') {
        return rejectReleaseVersion(null, 'missing');
    }
    const input = value.trim();
    if (input.length === 0) {
        return rejectReleaseVersion(value, 'missing');
    }
    if (input === '0.0.0') {
        return rejectReleaseVersion(input, 'placeholder');
    }
    if (cli_constants_1.RELEASE_VERSION_ALIAS_PATTERN.test(input) || input.startsWith('v')) {
        return rejectReleaseVersion(input, 'ambiguous');
    }
    if (input.includes('-') || input.includes('+')) {
        return rejectReleaseVersion(input, 'unsupported-prerelease');
    }
    const match = cli_constants_1.RELEASE_STABLE_SEMVER_PATTERN.exec(input);
    if (match === null) {
        return rejectReleaseVersion(input, 'malformed');
    }
    return {
        ok: true,
        input,
        version: input,
        major: Number(match[1]),
        minor: Number(match[2]),
        patch: Number(match[3]),
    };
}
function describeReleaseVersionRejection(reason) {
    switch (reason) {
        case 'missing':
            return 'Provide an explicit stable release version with --release-version, for example 0.1.0.';
        case 'placeholder':
            return 'Use the first public release version 0.1.0 or another publishable stable version; package metadata may remain 0.0.0 for development.';
        case 'ambiguous':
            return 'Use an exact stable semantic version without aliases or a leading v prefix.';
        case 'malformed':
            return 'Use a stable semantic version in major.minor.patch form, for example 0.1.0.';
        case 'unsupported-prerelease':
            return 'Use a stable release version; pre-release and build metadata are disabled until manifest and website distribution can mark them separately.';
    }
}
function rejectReleaseVersion(input, reason) {
    const correctiveAction = describeReleaseVersionRejection(reason);
    const received = input === null || input.trim().length === 0 ? '(not provided)' : input;
    return {
        ok: false,
        input,
        reason,
        message: `Rejected release version ${received}: ${correctiveAction}`,
        correctiveAction,
    };
}
