"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.determineNewReplacementValue = exports.determineNewReplacementName = exports.isReplacementRulesConfigured = exports.addReplacementUpdateIfValid = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const manager_1 = require("../../../../modules/manager");
const allVersioning = tslib_1.__importStar(require("../../../../modules/versioning"));
const template = tslib_1.__importStar(require("../../../../util/template"));
function addReplacementUpdateIfValid(updates, config) {
    const replacementNewName = determineNewReplacementName(config);
    const replacementNewValue = determineNewReplacementValue(config);
    if (config.packageName !== replacementNewName ||
        config.currentValue !== replacementNewValue) {
        updates.push({
            updateType: 'replacement',
            newName: replacementNewName,
            newValue: replacementNewValue,
        });
    }
}
exports.addReplacementUpdateIfValid = addReplacementUpdateIfValid;
function isReplacementRulesConfigured(config) {
    return (is_1.default.nonEmptyString(config.replacementName) ||
        is_1.default.nonEmptyString(config.replacementNameTemplate) ||
        is_1.default.nonEmptyString(config.replacementVersion));
}
exports.isReplacementRulesConfigured = isReplacementRulesConfigured;
function determineNewReplacementName(config) {
    return (config.replacementName ??
        template.compile(config.replacementNameTemplate, config, true));
}
exports.determineNewReplacementName = determineNewReplacementName;
function determineNewReplacementValue(config) {
    const versioning = allVersioning.get(config.versioning);
    const rangeStrategy = (0, manager_1.getRangeStrategy)(config);
    if (!is_1.default.nullOrUndefined(config.replacementVersion)) {
        return versioning.getNewValue({
            // TODO #22198
            currentValue: config.currentValue,
            newVersion: config.replacementVersion,
            rangeStrategy: rangeStrategy,
            isReplacement: true,
        });
    }
    return config.currentValue;
}
exports.determineNewReplacementValue = determineNewReplacementValue;
//# sourceMappingURL=utils.js.map