"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.replacePart = void 0;
const logger_1 = require("../../../../logger");
const operator_1 = require("../operator");
const range_1 = require("../range");
const version_1 = require("../version");
// Common logic for replace, widen, and bump strategies
// It basically makes the range stick to the new version.
function replacePart(part, to) {
    const { operator, version: ver, companion } = part;
    switch (operator) {
        case operator_1.LT:
            return { ...part, version: (0, version_1.increment)(ver, to) };
        case operator_1.LTE:
            return { ...part, version: to };
        case operator_1.PGTE:
            if (companion) {
                return {
                    ...part,
                    version: (0, version_1.floor)((0, version_1.adapt)(to, ver)),
                    companion: { ...companion, version: to },
                };
            }
            else {
                return { ...part, version: (0, version_1.floor)((0, version_1.adapt)(to, ver)) };
            }
        case operator_1.GT:
            return { ...part, version: (0, version_1.decrement)(to) };
        case operator_1.GTE:
        case operator_1.EQUAL:
            return { ...part, version: to };
        case operator_1.NOT_EQUAL:
            return part;
        // istanbul ignore next
        default:
            logger_1.logger.warn(`Unsupported operator '${operator}'`);
            return { operator: '', delimiter: ' ', version: '' };
    }
}
exports.replacePart = replacePart;
exports.default = ({ range, to }) => {
    const parts = (0, range_1.parseRanges)(range).map((part) => {
        if ((0, range_1.satisfiesRange)(to, part)) {
            // The new version satisfies the range. Keep it as-is.
            // Note that consecutive `~>` and `>=` parts are combined into one Range object,
            // therefore both parts are updated if the new version violates one of them.
            return part;
        }
        return replacePart(part, to);
    });
    return (0, range_1.stringifyRanges)(parts);
};
//# sourceMappingURL=replace.js.map