"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInRangeReleases = void 0;
// TODO #22198
const logger_1 = require("../../../../../logger");
const datasource_1 = require("../../../../../modules/datasource");
const versioning_1 = require("../../../../../modules/versioning");
const array_1 = require("../../../../../util/array");
function matchesMMP(version, v1, v2) {
    return (version.getMajor(v1) === version.getMajor(v2) &&
        version.getMinor(v1) === version.getMinor(v2) &&
        version.getPatch(v1) === version.getPatch(v2));
}
function matchesUnstable(version, v1, v2) {
    return !version.isStable(v1) && matchesMMP(version, v1, v2);
}
async function getInRangeReleases(config) {
    const versioning = config.versioning;
    const currentVersion = config.currentVersion;
    const newVersion = config.newVersion;
    const depName = config.depName;
    const datasource = config.datasource;
    // istanbul ignore if
    if (!(0, datasource_1.isGetPkgReleasesConfig)(config)) {
        return null;
    }
    try {
        const pkgReleases = (await (0, datasource_1.getPkgReleases)(config)).releases;
        const version = (0, versioning_1.get)(versioning);
        const releases = pkgReleases
            .filter((release) => version.isCompatible(release.version, currentVersion))
            .filter((release) => version.equals(release.version, currentVersion) ||
            version.isGreaterThan(release.version, currentVersion))
            .filter((release) => !version.isGreaterThan(release.version, newVersion))
            .filter((release) => version.isStable(release.version) ||
            matchesUnstable(version, currentVersion, release.version) ||
            matchesUnstable(version, newVersion, release.version));
        if (version.valueToVersion) {
            for (const release of (0, array_1.coerceArray)(releases)) {
                release.version = version.valueToVersion(release.version);
            }
        }
        return releases;
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.debug({ err }, 'getInRangeReleases err');
        logger_1.logger.debug(`Error getting releases for ${depName} from ${datasource}`);
        return null;
    }
}
exports.getInRangeReleases = getInRangeReleases;
//# sourceMappingURL=releases.js.map