"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.supportLatest = void 0;
var rule_interface_1 = require("../interfaces/rule.interface");
var DEFAULT_OPTIONS = {
    numberOfMajorReleases: 1,
    numberOfMinorReleases: 1,
    numberOfPatchReleases: 1,
};
exports.supportLatest = function (options, version, allVersionsSortedLatestFirst) {
    var realOptions = __assign(__assign({}, DEFAULT_OPTIONS), (options || {}));
    if (version.prerelease.length) {
        return { action: rule_interface_1.Action.continue };
    }
    var supported = new Map();
    for (var _i = 0, allVersionsSortedLatestFirst_1 = allVersionsSortedLatestFirst; _i < allVersionsSortedLatestFirst_1.length; _i++) {
        var currentVersion = allVersionsSortedLatestFirst_1[_i];
        if (currentVersion.prerelease.length) {
            continue;
        }
        var minorSupported = addMajorAndReturnMinorSupported(supported, currentVersion);
        var patchSupported = addMinorAndReturnPatchSupported(minorSupported, currentVersion);
        patchSupported.add(currentVersion.patch);
        if (shouldContinue(supported, minorSupported, patchSupported, realOptions, version, currentVersion)) {
            return { action: rule_interface_1.Action.continue };
        }
        if (currentVersion.compare(version) === 0) {
            break;
        }
    }
    return { action: rule_interface_1.Action.support };
};
function shouldContinueWith(numberOfVersionsGreaterOrEqual, releaseOption) {
    return (releaseOption !== "all" &&
        numberOfVersionsGreaterOrEqual > (releaseOption || 1));
}
function shouldContinue(supported, minorSupported, patchSupported, options, evaluatedVersion, versionInLoop) {
    return (shouldContinueWith(supported.size, options.numberOfMajorReleases) ||
        (shouldContinueWith(minorSupported.size, options.numberOfMinorReleases) &&
            evaluatedVersion.major === versionInLoop.major) ||
        (shouldContinueWith(patchSupported.size, options.numberOfPatchReleases) &&
            evaluatedVersion.major === versionInLoop.major &&
            evaluatedVersion.minor === versionInLoop.minor));
}
function addMajorAndReturnMinorSupported(supported, currentVersion) {
    if (!supported.has(currentVersion.major)) {
        supported.set(currentVersion.major, new Map());
    }
    return supported.get(currentVersion.major);
}
function addMinorAndReturnPatchSupported(minorSupported, currentVersion) {
    if (!(minorSupported === null || minorSupported === void 0 ? void 0 : minorSupported.has(currentVersion.minor))) {
        minorSupported === null || minorSupported === void 0 ? void 0 : minorSupported.set(currentVersion.minor, new Set());
    }
    return minorSupported === null || minorSupported === void 0 ? void 0 : minorSupported.get(currentVersion.minor);
}
