// SPDX-License-Identifier: Apache-2.0
import { SemVer } from 'semver';
import { Version } from './version.js';
/**
 * A range of versions which includes the beginning version and excludes the end version.
 */
export class VersionRange {
    begin;
    end;
    constructor(
    /**
     * The beginning of the version range (inclusive).
     */
    begin, 
    /**
     * The end of the version range (exclusive).
     */
    end) {
        this.begin = begin;
        this.end = end;
        if (this.begin !== null && this.end !== null && this.begin.compare(this.end) >= 0) {
            throw new RangeError('Invalid version range');
        }
    }
    /**
     * Creates a version range from the given integer bounds.
     *
     * @param begin - the beginning of the version range (inclusive).
     * @param end - the end of the version range (exclusive).
     * @returns the version range.
     * @throws RangeError if the bounds are invalid.
     */
    static fromIntegerBounds(begin, end) {
        return new VersionRange(new Version(begin), new Version(end));
    }
    /**
     * Creates a version range from the given integer version.
     *
     * @param version - the specific version for which to create a range.
     * @returns the version range.
     * @throws RangeError if the version is invalid.
     */
    static fromIntegerVersion(version) {
        return new VersionRange(new Version(version), new Version(version + 1));
    }
    /**
     * Creates a version range from the given semantic version bounds.
     *
     * @param begin - the beginning of the version range (inclusive).
     * @param end - the end of the version range (exclusive).
     * @returns the version range.
     * @throws RangeError if the bounds are invalid.
     */
    static fromSemVerBounds(begin, end) {
        return new VersionRange(new Version(begin), new Version(end));
    }
    /**
     * Creates a version range which includes all patch releases for the given major and minor version.
     *
     * @param version - the semantic version.
     * @returns the version range.
     */
    static patchVersionBounds(version) {
        // clone the version to avoid modifying the original
        const rangeEnd = new SemVer(version.toString());
        return new VersionRange(new Version(version), new Version(rangeEnd.inc('minor')));
    }
    /**
     * Creates a version range which includes all minor and patch releases for the given major version.
     *
     * @param version - the semantic version.
     * @returns the version range.
     */
    static minorVersionBounds(version) {
        // clone the version to avoid modifying the original
        const rangeEnd = new SemVer(version.toString());
        return new VersionRange(new Version(version), new Version(rangeEnd.inc('major')));
    }
    equals(other) {
        return this.begin !== null && this.end !== null && this.begin.equals(other.begin) && this.end.equals(other.end);
    }
    compare(other) {
        if (this.begin === null || this.end === null) {
            throw new RangeError('Invalid version range');
        }
        const beginComparison = this.begin.compare(other.begin);
        if (beginComparison !== 0) {
            return beginComparison;
        }
        return this.end.compare(other.end);
    }
    contains(version) {
        if (this.begin === null || this.end === null) {
            throw new RangeError('Invalid version range');
        }
        return this.begin.compare(version) <= 0 && this.end.compare(version) > 0;
    }
    toString() {
        return `[${this.begin}, ${this.end})`;
    }
}
//# sourceMappingURL=version-range.js.map