"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fulfillsVersionRange = fulfillsVersionRange;
exports.createVersionBucket = createVersionBucket;
const semver_1 = require("semver");
function fulfillsVersionRange({ version, acceptableVersions, canBeLatest, }) {
    if (version === 'latest') {
        return canBeLatest;
    }
    let cleanedUserVersion, isRange;
    if ((0, semver_1.valid)(version)) {
        cleanedUserVersion = (0, semver_1.valid)(version);
        isRange = false;
    }
    else if ((0, semver_1.validRange)(version)) {
        cleanedUserVersion = (0, semver_1.validRange)(version);
        isRange = true;
    }
    return (
    // If the given version is a bogus format, this will still be undefined and we'll automatically reject it
    !!cleanedUserVersion &&
        (isRange
            ? (0, semver_1.subset)(cleanedUserVersion, acceptableVersions)
            : (0, semver_1.satisfies)(cleanedUserVersion, acceptableVersions)));
}
/**
 * Creates a version bucket function for analytics.
 * Converts versions like "1.2.3" to "1.x" for grouping in analytics.
 *
 * @param minMajorVersion - Optional minimum major version threshold.
 *   Versions below this will be bucketed as "<{min}.0.0"
 *
 * @example
 * const getVersionBucket = createVersionBucket(); // no minimum
 * getVersionBucket("1.2.3") // "1.x"
 *
 * const getVersionBucket = createVersionBucket(11);
 * getVersionBucket("15.3.0") // "15.x"
 * getVersionBucket("10.0.0") // "<11.0.0"
 */
function createVersionBucket(minMajorVersion) {
    return (version) => {
        if (!version) {
            return 'none';
        }
        try {
            const minVer = (0, semver_1.minVersion)(version);
            if (!minVer) {
                return 'invalid';
            }
            const majorVersion = (0, semver_1.major)(minVer);
            if (minMajorVersion !== undefined && majorVersion < minMajorVersion) {
                return `<${minMajorVersion}.0.0`;
            }
            return `${majorVersion}.x`;
        }
        catch {
            return 'unknown';
        }
    };
}
