"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SEMVER;
(function (SEMVER) {
    SEMVER["major"] = "major";
    SEMVER["premajor"] = "premajor";
    SEMVER["minor"] = "minor";
    SEMVER["preminor"] = "preminor";
    SEMVER["patch"] = "patch";
    SEMVER["prepatch"] = "prepatch";
    SEMVER["noVersion"] = "";
})(SEMVER || (SEMVER = {}));
exports.default = SEMVER;
/** Given two labels determine the next SEMVER bump. */
function getHigherSemverTag(left, right) {
    if (left === SEMVER.major || right === SEMVER.major) {
        return SEMVER.major;
    }
    if (left === SEMVER.minor || right === SEMVER.minor) {
        return SEMVER.minor;
    }
    return SEMVER.patch;
}
exports.getHigherSemverTag = getHigherSemverTag;
/**
 * Determine the version bump from the labels on merged PRs.
 * Respects skip-release labels and the "onlyPublishWithReleaseLabel"
 * strategy.
 */
function calculateSemVerBump(labels, labelMap, { onlyPublishWithReleaseLabel, skipReleaseLabels = [] } = {}) {
    const labelSet = new Set();
    const skip = labelMap.get('skip-release') || [];
    skip.forEach(skipLabel => {
        if (!skipReleaseLabels.includes(skipLabel)) {
            skipReleaseLabels.push(skipLabel);
        }
    });
    labels.forEach(pr => {
        pr.forEach(label => {
            const userLabel = [...labelMap.entries()].find(pair => pair[1].includes(label));
            labelSet.add(userLabel ? userLabel[0] : label);
        });
    });
    let skipRelease = false;
    let isPrerelease = false;
    if (labels.length > 0 && labels[0].length > 0) {
        const prereleaseLabels = labelMap.get('prerelease') || [];
        const releaseLabels = labelMap.get('release') || [];
        isPrerelease = labels[0].some(label => prereleaseLabels.includes(label));
        skipRelease = onlyPublishWithReleaseLabel
            ? !labels[0].some(label => releaseLabels.includes(label))
            : labels[0].some(label => skipReleaseLabels.includes(label));
    }
    const version = [...labelSet].reduce(getHigherSemverTag, SEMVER.patch);
    if (skipRelease) {
        return SEMVER.noVersion;
    }
    if (isPrerelease) {
        if (version === SEMVER.major) {
            return SEMVER.premajor;
        }
        if (version === SEMVER.minor) {
            return SEMVER.preminor;
        }
        return SEMVER.prepatch;
    }
    return version;
}
exports.calculateSemVerBump = calculateSemVerBump;
//# sourceMappingURL=semver.js.map