import { regEx } from "../../../util/regex.js";
import { coerceString } from "../../../util/string.js";
import api$1 from "../pep440/index.js";
import api$2 from "../npm/index.js";
import { ascendingRange, descendingRange, exactVersion, inclusiveBound, lowerBound, upperBound, versionGroup } from "./pattern.js";
import { npm2rezplus, padZeroes, pep4402rezInclusiveBound, rez2npm, rez2pep440 } from "./transform.js";

//#region lib/modules/versioning/rez/index.ts
const id = "rez";
function equals(a, b) {
	try {
		return api$2.equals(padZeroes(a), padZeroes(b));
	} catch 	/* istanbul ignore next */ {
		return api$1.equals(a, b);
	}
}
function getMajor(version) {
	try {
		return api$2.getMajor(padZeroes(version));
	} catch 	/* istanbul ignore next */ {
		return api$1.getMajor(version);
	}
}
function getMinor(version) {
	try {
		return api$2.getMinor(padZeroes(version));
	} catch 	/* istanbul ignore next */ {
		return api$1.getMinor(version);
	}
}
function getPatch(version) {
	try {
		return api$2.getPatch(padZeroes(version));
	} catch 	/* istanbul ignore next */ {
		return api$1.getPatch(version);
	}
}
function isGreaterThan(a, b) {
	try {
		return api$2.isGreaterThan(padZeroes(a), padZeroes(b));
	} catch 	/* istanbul ignore next */ {
		return api$1.isGreaterThan(a, b);
	}
}
function isLessThanRange(version, range) {
	return api$2.isVersion(padZeroes(version)) && !!api$2.isLessThanRange?.(padZeroes(version), rez2npm(range));
}
function isValid(input) {
	return api$2.isValid(rez2npm(input));
}
function isStable(version) {
	return api$2.isStable(padZeroes(version));
}
function isVersion(input) {
	return api$2.isVersion(padZeroes(rez2npm(input)));
}
function matches(version, range) {
	return api$2.isVersion(padZeroes(version)) && api$2.matches(padZeroes(version), rez2npm(range));
}
function getSatisfyingVersion(versions, range) {
	return api$2.getSatisfyingVersion(versions, rez2npm(range));
}
function minSatisfyingVersion(versions, range) {
	return api$2.minSatisfyingVersion(versions, rez2npm(range));
}
function isSingleVersion(constraint) {
	return constraint.trim().startsWith("==") && isVersion(constraint.trim().substring(2).trim()) || isVersion(constraint.trim());
}
function sortVersions(a, b) {
	return api$2.sortVersions(padZeroes(a), padZeroes(b));
}
function getNewValue({ currentValue, rangeStrategy, currentVersion, newVersion }) {
	const pep440Value = api$1.getNewValue({
		currentValue: rez2pep440(currentValue),
		rangeStrategy,
		currentVersion,
		newVersion
	});
	if (exactVersion.test(currentValue)) return pep440Value;
	if (pep440Value && inclusiveBound.test(currentValue)) return pep4402rezInclusiveBound(pep440Value);
	if (pep440Value && lowerBound.test(currentValue)) {
		if (currentValue.includes("+")) return npm2rezplus(pep440Value);
		return pep440Value;
	}
	if (pep440Value && upperBound.test(currentValue)) return pep440Value;
	const matchAscRange = ascendingRange.exec(currentValue);
	if (pep440Value && matchAscRange?.groups) {
		const lowerBoundAscCurrent = matchAscRange.groups.range_lower_asc;
		const upperBoundAscCurrent = matchAscRange.groups.range_upper_asc;
		const lowerAscVersionCurrent = matchAscRange.groups.range_lower_asc_version;
		const upperAscVersionCurrent = matchAscRange.groups.range_upper_asc_version;
		const [lowerBoundAscPep440, upperBoundAscPep440] = pep440Value.split(", ");
		const lowerAscVersionNew = coerceString(regEx(versionGroup).exec(lowerBoundAscPep440)?.[0]);
		const upperAscVersionNew = coerceString(regEx(versionGroup).exec(upperBoundAscPep440)?.[0]);
		const lowerBoundAscNew = lowerBoundAscCurrent.replace(lowerAscVersionCurrent, lowerAscVersionNew);
		const upperBoundAscNew = upperBoundAscCurrent.replace(upperAscVersionCurrent, upperAscVersionNew);
		return lowerBoundAscNew + (currentValue.includes(",") ? "," : "") + upperBoundAscNew;
	}
	const matchDscRange = descendingRange.exec(currentValue);
	if (pep440Value && matchDscRange?.groups) {
		const upperBoundDescCurrent = matchDscRange.groups.range_upper_desc;
		const lowerBoundDescCurrent = matchDscRange.groups.range_lower_desc;
		const upperDescVersionCurrent = matchDscRange.groups.range_upper_desc_version;
		const lowerDescVersionCurrent = matchDscRange.groups.range_lower_desc_version;
		const [lowerBoundDescPep440, upperBoundDescPep440] = pep440Value.split(", ");
		const upperDescVersionNew = coerceString(regEx(versionGroup).exec(upperBoundDescPep440)?.[0]);
		const lowerDescVersionNew = coerceString(regEx(versionGroup).exec(lowerBoundDescPep440)?.[0]);
		const upperBoundDescNew = upperBoundDescCurrent.replace(upperDescVersionCurrent, upperDescVersionNew);
		const lowerBoundDescNew = lowerBoundDescCurrent.replace(lowerDescVersionCurrent, lowerDescVersionNew);
		return upperBoundDescNew + "," + lowerBoundDescNew;
	}
	return null;
}
function isCompatible(version) {
	return isVersion(version);
}
const api = {
	equals,
	getMajor,
	getMinor,
	getPatch,
	getNewValue,
	getSatisfyingVersion,
	isCompatible,
	isGreaterThan,
	isLessThanRange,
	isSingleVersion,
	isStable,
	isValid,
	isVersion,
	matches,
	minSatisfyingVersion,
	sortVersions
};

//#endregion
export { api as default, id };
//# sourceMappingURL=index.js.map