"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyConstraintsFiltering = exports.sortAndRemoveDuplicates = exports.filterValidVersions = exports.applyExtractVersion = exports.isGetPkgReleasesConfig = exports.getDefaultVersioning = exports.getDatasourceFor = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const logger_1 = require("../../logger");
const filter_map_1 = require("../../util/filter-map");
const regex_1 = require("../../util/regex");
const versioning_1 = require("../versioning");
const allVersioning = tslib_1.__importStar(require("../versioning"));
const api_1 = tslib_1.__importDefault(require("./api"));
const custom_1 = require("./custom");
function getDatasourceFor(datasource) {
    if (datasource?.startsWith('custom.')) {
        return getDatasourceFor(custom_1.CustomDatasource.id);
    }
    return api_1.default.get(datasource) ?? null;
}
exports.getDatasourceFor = getDatasourceFor;
function getDefaultVersioning(datasourceName) {
    if (!datasourceName) {
        return versioning_1.defaultVersioning.id;
    }
    const datasource = getDatasourceFor(datasourceName);
    // istanbul ignore if: wrong regex manager config?
    if (!datasource) {
        logger_1.logger.warn({ datasourceName }, 'Missing datasource!');
    }
    return datasource?.defaultVersioning ?? versioning_1.defaultVersioning.id;
}
exports.getDefaultVersioning = getDefaultVersioning;
function isGetPkgReleasesConfig(input) {
    return (is_1.default.nonEmptyStringAndNotWhitespace(input.datasource) &&
        is_1.default.nonEmptyStringAndNotWhitespace(input.packageName));
}
exports.isGetPkgReleasesConfig = isGetPkgReleasesConfig;
function applyExtractVersion(config, releaseResult) {
    if (!config.extractVersion) {
        return releaseResult;
    }
    const extractVersionRegEx = (0, regex_1.regEx)(config.extractVersion);
    releaseResult.releases = (0, filter_map_1.filterMap)(releaseResult.releases, (release) => {
        const version = extractVersionRegEx.exec(release.version)?.groups?.version;
        if (!version) {
            return null;
        }
        release.version = version;
        return release;
    });
    return releaseResult;
}
exports.applyExtractVersion = applyExtractVersion;
function filterValidVersions(config, releaseResult) {
    const versioningName = config.versioning ?? getDefaultVersioning(config.datasource);
    const versioning = allVersioning.get(versioningName);
    releaseResult.releases = (0, filter_map_1.filterMap)(releaseResult.releases, (release) => versioning.isVersion(release.version) ? release : null);
    return releaseResult;
}
exports.filterValidVersions = filterValidVersions;
function sortAndRemoveDuplicates(config, releaseResult) {
    const versioningName = config.versioning ?? getDefaultVersioning(config.datasource);
    const versioning = allVersioning.get(versioningName);
    releaseResult.releases = releaseResult.releases.sort((a, b) => versioning.sortVersions(a.version, b.version));
    // Once releases are sorted, deduplication is straightforward and efficient
    let previousVersion = null;
    releaseResult.releases = (0, filter_map_1.filterMap)(releaseResult.releases, (release) => {
        if (previousVersion === release.version) {
            return null;
        }
        previousVersion = release.version;
        return release;
    });
    return releaseResult;
}
exports.sortAndRemoveDuplicates = sortAndRemoveDuplicates;
function applyConstraintsFiltering(config, releaseResult) {
    if (config?.constraintsFiltering !== 'strict') {
        for (const release of releaseResult.releases) {
            delete release.constraints;
        }
        return releaseResult;
    }
    const versioningName = config.versioning ?? getDefaultVersioning(config.datasource);
    const versioning = allVersioning.get(versioningName);
    const configConstraints = config.constraints;
    const filteredReleases = [];
    releaseResult.releases = (0, filter_map_1.filterMap)(releaseResult.releases, (release) => {
        const releaseConstraints = release.constraints;
        delete release.constraints;
        // istanbul ignore if
        if (!configConstraints || !releaseConstraints) {
            return release;
        }
        for (const [name, configConstraint] of Object.entries(configConstraints)) {
            // istanbul ignore if
            if (!versioning.isValid(configConstraint)) {
                continue;
            }
            const constraint = releaseConstraints[name];
            if (!is_1.default.nonEmptyArray(constraint)) {
                // A release with no constraints is OK
                continue;
            }
            const satisfiesConstraints = constraint.some(
            // If the constraint value is a subset of any release's constraints, then it's OK
            // fallback to release's constraint match if subset is not supported by versioning
            (releaseConstraint) => !releaseConstraint ||
                (versioning.subset?.(configConstraint, releaseConstraint) ??
                    versioning.matches(configConstraint, releaseConstraint)));
            if (!satisfiesConstraints) {
                filteredReleases.push(release.version);
                return null;
            }
        }
        return release;
    });
    if (filteredReleases.length) {
        const count = filteredReleases.length;
        const packageName = config.packageName;
        const releases = filteredReleases.join(', ');
        logger_1.logger.debug(`Filtered ${count} releases for ${packageName} due to constraintsFiltering=strict: ${releases}`);
    }
    return releaseResult;
}
exports.applyConstraintsFiltering = applyConstraintsFiltering;
//# sourceMappingURL=common.js.map