"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bump = exports.getCommitMessage = void 0;
const standardVersion = require("standard-version");
const getBranchVersionRegex = (ticketPrefix) => new RegExp(`^release/(${ticketPrefix}-[0-9]+).*$`);
const getCommitVersionRegex = (ticketPrefix) => new RegExp(`(${ticketPrefix}-[0-9]+).*$`);
exports.getCommitMessage = (prefix, ticketId, extra) => {
    const messageSuffix = [
        "Version: {{currentTag}}",
        ticketId ? `Ticket: ${ticketId}` : undefined,
        extra ? `${extra}` : undefined,
    ]
        .filter(str => !!str)
        .join(", ");
    return `chore(${prefix}): ${messageSuffix} [skip ci]`;
};
const executeStandardVersion = (tagPrefix, ticketId, preRelease, extraMessage) => {
    standardVersion({
        noVerify: true,
        prerelease: preRelease,
        releaseCommitMessageFormat: exports.getCommitMessage(preRelease ? "pre-release" : "release", ticketId, extraMessage),
        path: ".",
        tagPrefix,
    });
};
const extractTicketId = (ticketPrefix, branch, lastCommitMsg) => {
    const matches = lastCommitMsg.match(getCommitVersionRegex(ticketPrefix)) || branch.match(getBranchVersionRegex(ticketPrefix));
    return (matches && matches[1]) || undefined;
};
exports.bump = (tagPrefix, ticketPrefix, branch, lastCommitMsg, extraMessage) => {
    if (branch === "master") {
        const ticketId = extractTicketId(ticketPrefix, branch, lastCommitMsg);
        console.log("On branch master. Creating release...", ticketId, branch);
        executeStandardVersion(tagPrefix, ticketId, undefined, extraMessage);
    }
    else if (branch.match(/^release\/.+$/)) {
        const ticketId = extractTicketId(ticketPrefix, branch, lastCommitMsg);
        console.log("On release branch. Creating pre-release...", ticketId, branch);
        executeStandardVersion(tagPrefix, ticketId, "beta", extraMessage);
    }
    else {
        console.log(`Current branch (${branch}) is neither 'release/*' nor 'master'. Not bumping...`);
    }
    console.log(`Successfully bumped version, updated changelog and created commit`);
};
//# sourceMappingURL=bump.js.map