"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCommitMessage = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
const chalk_pipe_1 = __importDefault(require("chalk-pipe"));
const shellsync_1 = __importDefault(require("shellsync"));
const executeCommand_1 = __importDefault(require("./executeCommand"));
const getBranchName_1 = __importDefault(require("./getBranchName"));
const getGitRootDir_1 = __importDefault(require("./getGitRootDir"));
const questionConfig_1 = require("./questionConfig");
const commitMsgFile = (0, path_1.join)((0, getGitRootDir_1.default)(), '.git', 'COMMIT_EDITMSG');
const getCommitMessage = (state) => {
    const { answers, config } = state;
    const branchName = Boolean(answers === null || answers === void 0 ? void 0 : answers.branchFlag)
        ? state.config.commit.before.branchName +
            (0, getBranchName_1.default)().split('/').slice(-1)[0] +
            state.config.commit.after.branchName
        : '';
    const emoji = state.config.commit.before.emoji +
        state.types.commit[answers.commitTypes].emoji +
        state.config.commit.after.emoji;
    const scope = answers.commitScopes
        ? state.config.commit.before.scope +
            answers.commitScopes.trim() +
            state.config.commit.after.scope
        : '';
    const subject = answers.commitSubject.trim();
    const type = answers.commitTypes ? answers.commitTypes : '';
    let message = state.config.commit.format
        .replace(/\{emoji\}/g, emoji)
        .replace(/\{scope\}/g, scope)
        .replace(/\{branchName\}/g, branchName)
        .replace(/\{subject\}/g, subject)
        .replace(/\{type\}/g, type);
    const hasTicketUrl = Boolean(branchName) && Boolean(config.branch.url);
    const hasBody = Boolean(answers.commitBody);
    const hasBreaking = Boolean(answers.commitBreaking);
    if (hasTicketUrl) {
        message += `\n\n**Ticket:** ${config.branch.url}${branchName}`;
    }
    if (hasBody) {
        message += `\n\n${answers.commitBody}`;
    }
    if (hasBreaking) {
        message += answers.commitBreaking;
    }
    return message;
};
exports.getCommitMessage = getCommitMessage;
const setCommit = ({ appendedArgs = [], cliAnswers, cliOptions, passThroughParams, state, }) => {
    const message = cliOptions.mode === questionConfig_1.TYPE_COMMIT ? getCommitMessage(state) : questionConfig_1.TYPE_BRANCH;
    const commandArray = ['git', 'commit', '--file', commitMsgFile, ...appendedArgs];
    const command = commandArray.join(' ');
    if (cliOptions.dryRun) {
        console.log((0, chalk_pipe_1.default)('orange.bold')('⚗️  DRY RUN: TESTING'));
        const commandReplace = command.replace(commitMsgFile, '.git/COMMIT_EDITMSG');
        console.log((0, chalk_pipe_1.default)('orange')('> commandReplace:'));
        console.log((0, chalk_pipe_1.default)('white.italic')(commandReplace));
        console.log((0, chalk_pipe_1.default)('orange')('> message:'));
        console.log((0, chalk_pipe_1.default)('white.italic')(message));
        console.log();
    }
    else {
        fs_1.default.writeFileSync(commitMsgFile, message);
        if (cliOptions.hook) {
            process.exit(0);
        }
        (0, executeCommand_1.default)((0, shellsync_1.default) `${command}`);
    }
};
exports.default = setCommit;
//# sourceMappingURL=setCommit.js.map