import { logger } from "../../../../logger/index.js";
import { EQUAL, GT, GTE, LT, LTE, NOT_EQUAL, PGTE } from "../operator.js";
import { parseRanges, satisfiesRange, stringifyRanges } from "../range.js";
import { adapt, decrement, floor, increment } from "../version.js";

//#region lib/modules/versioning/ruby/strategies/replace.ts
function replacePart(part, to) {
	const { operator, version: ver, companion } = part;
	switch (operator) {
		case LT: return {
			...part,
			version: increment(ver, to)
		};
		case LTE: return {
			...part,
			version: to
		};
		case PGTE: if (companion) return {
			...part,
			version: floor(adapt(to, ver)),
			companion: {
				...companion,
				version: to
			}
		};
		else return {
			...part,
			version: floor(adapt(to, ver))
		};
		case GT: return {
			...part,
			version: decrement(to)
		};
		case GTE:
		case EQUAL: return {
			...part,
			version: to
		};
		case NOT_EQUAL: return part;
		default:
			logger.warn({ operator }, `Unsupported ruby versioning operator`);
			return {
				operator: "",
				delimiter: " ",
				version: ""
			};
	}
}
var replace_default = ({ range, to }) => {
	return stringifyRanges(parseRanges(range).map((part) => {
		if (satisfiesRange(to, part)) return part;
		if (part.version.split(".").length > to.split(".").length) {
			const diff = part.version.split(".").length - to.split(".").length;
			const replacement = replacePart(part, [to, ...Array(diff).fill("0")].join("."));
			return {
				...replacement,
				version: replacement.version.split(".").slice(0, -diff).join(".")
			};
		}
		return replacePart(part, to);
	}));
};

//#endregion
export { replace_default as default, replacePart };
//# sourceMappingURL=replace.js.map