"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestVersion = void 0;
const semver_1 = require("semver");
const I18n_1 = require("../I18n");
function getLatestVersion(packageTags, version, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const tags = getValidTags(packageTags);
        if (!tags.length) {
            return version;
        }
        if (isMasterBranch(version)) {
            return getLatestTag(tags);
        }
        if (isBranchDependency(version, tags)) {
            return chooseBranchOrTag(tags, version, options);
        }
        return getLatestVersionByMajor(tags, version, options);
    });
}
exports.getLatestVersion = getLatestVersion;
function getLatestVersionByMajor(tags, version, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { task, name, major } = options;
        if (major !== undefined) {
            const rangeVersion = !major && version;
            return getLatestTag(tags, rangeVersion);
        }
        const latestTag = getLatestTag(tags);
        if (isEquivalMajors(latestTag, version)) {
            return latestTag;
        }
        const majorsList = getLatestMajors(tags, version);
        return createSelectList(task, name, majorsList);
    });
}
function getValidTags(tags) {
    return tags.filter((tag) => (0, semver_1.valid)(tag));
}
function getLatestTag(tags, version) {
    const range = version ? `^${version}` : '*';
    return (0, semver_1.maxSatisfying)(tags, range);
}
function getLatestMajors(tags, currentVersion) {
    const majorsList = tags
        .filter(tag => (0, semver_1.gte)(tag, currentVersion))
        .map((tag) => (0, semver_1.major)(tag));
    const uniqueMajors = Array.from(new Set(majorsList));
    return uniqueMajors.map((majorTag) => (0, semver_1.maxSatisfying)(tags, majorTag.toString()));
}
function isMasterBranch(version) {
    return !version || version === 'master';
}
function isBranchDependency(version, tags) {
    return !(0, semver_1.valid)(version) || !tags.includes(version);
}
function chooseBranchOrTag(tags, branch, options) {
    const { task, name } = options;
    const latestTag = getLatestTag(tags);
    const choices = [latestTag, branch];
    return createSelectList(task, name, choices);
}
function isEquivalMajors(latestVersion, version) {
    return (0, semver_1.major)(latestVersion) === (0, semver_1.major)(version);
}
function createSelectList(task, name, choices) {
    return task.prompt([
        {
            choices,
            message: I18n_1.default.info('CHOOSE_VERSION', name),
            type: 'select',
            name: 'value',
        },
    ]).catch();
}
//# sourceMappingURL=get-latest-version.js.map