"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateBranchName = generateBranchName;
const tslib_1 = require("tslib");
// TODO #22198
const clean_git_ref_1 = tslib_1.__importDefault(require("clean-git-ref"));
const slugify_1 = tslib_1.__importDefault(require("slugify"));
const logger_1 = require("../../../logger");
const hash_1 = require("../../../util/hash");
const regex_1 = require("../../../util/regex");
const template = tslib_1.__importStar(require("../../../util/template"));
const MIN_HASH_LENGTH = 6;
const RE_MULTIPLE_DASH = (0, regex_1.regEx)(/--+/g);
const RE_SPECIAL_CHARS_STRICT = (0, regex_1.regEx)(/[`~!@#$%^&*()_=+[\]\\|{};':",.<>?]/g);
/**
 * Clean git branch name
 *
 * Remove what clean-git-ref fails to:
 * - leading dot/leading dot after slash
 * - trailing dot
 * - whitespace
 * - special characters
 * - leading or trailing dashes
 * - chained dashes(breaks markdown comments) are replaced by single dash
 */
function cleanBranchName(branchName, branchNameStrict) {
    let cleanedBranchName = branchName;
    if (branchNameStrict) {
        cleanedBranchName = cleanedBranchName.replace(RE_SPECIAL_CHARS_STRICT, '-'); // massage out all special characters that slip through slugify
    }
    return clean_git_ref_1.default
        .clean(cleanedBranchName)
        .replace((0, regex_1.regEx)(/^\.|\.$/), '') // leading or trailing dot
        .replace((0, regex_1.regEx)(/\/\./g), '/') // leading dot after slash
        .replace((0, regex_1.regEx)(/\s/g), '') // whitespace
        .replace((0, regex_1.regEx)(/[[\]?:\\^~]/g), '-') // massage out all these characters: [ ] ? : \ ^ ~
        .replace((0, regex_1.regEx)(/(^|\/)-+/g), '$1') // leading dashes
        .replace((0, regex_1.regEx)(/-+(\/|$)/g), '$1') // trailing dashes
        .replace(RE_MULTIPLE_DASH, '-'); // chained dashes
}
function generateBranchName(update) {
    // Check whether to use a group name
    const newMajor = String(update.newMajor);
    const newMinor = String(update.newMinor);
    if (update.groupName) {
        update.groupName = template.compile(update.groupName, update);
        logger_1.logger.trace('Using group branchName template');
        // TODO: types (#22198)
        logger_1.logger.trace(`Dependency ${update.depName} is part of group ${update.groupName}`);
        if (update.groupSlug) {
            update.groupSlug = template.compile(update.groupSlug, update);
        }
        else {
            update.groupSlug = update.groupName;
        }
        update.groupSlug = (0, slugify_1.default)(update.groupSlug, {
            lower: true,
        });
        if (update.updateType === 'major' && update.separateMajorMinor) {
            if (update.separateMultipleMajor) {
                update.groupSlug = `major-${newMajor}-${update.groupSlug}`;
            }
            else {
                update.groupSlug = `major-${update.groupSlug}`;
            }
        }
        if (update.updateType === 'minor' && update.separateMultipleMinor) {
            update.groupSlug = `minor-${newMajor}.${newMinor}-${update.groupSlug}`;
        }
        if (update.updateType === 'patch' && update.separateMinorPatch) {
            update.groupSlug = `patch-${update.groupSlug}`;
        }
        update.branchTopic = update.group.branchTopic ?? update.branchTopic;
        update.branchName = update.group.branchName ?? update.branchName;
    }
    if (update.hashedBranchLength) {
        let hashLength = update.hashedBranchLength - update.branchPrefix.length;
        if (hashLength < MIN_HASH_LENGTH) {
            logger_1.logger.warn(`\`hashedBranchLength\` must allow for at least ${MIN_HASH_LENGTH} characters hashing in addition to \`branchPrefix\`. Using ${MIN_HASH_LENGTH} character hash instead.`);
            hashLength = MIN_HASH_LENGTH;
        }
        const additionalBranchPrefix = template.compile(String(update.additionalBranchPrefix ?? ''), update);
        const branchTopic = template.compile(String(update.branchTopic ?? ''), update);
        let hashInput = additionalBranchPrefix + branchTopic;
        // Compile extra times in case of nested templates
        hashInput = template.compile(hashInput, update);
        hashInput = template.compile(hashInput, update);
        const hashedInput = (0, hash_1.hash)(hashInput);
        // TODO: types (#22198)
        update.branchName = `${update.branchPrefix}${hashedInput.slice(0, hashLength)}`;
    }
    else {
        update.branchName = template.compile(update.branchName, update);
        // Compile extra times in case of nested templates
        update.branchName = template.compile(update.branchName, update);
        update.branchName = template.compile(update.branchName, update);
    }
    if (update.updateType === 'minor' && update.separateMultipleMinor) {
        update.branchName = update.branchName.replace('.x', `.${newMinor}.x`);
    }
    update.branchName = cleanBranchName(update.branchName, update.branchNameStrict);
}
//# sourceMappingURL=branch-name.js.map