"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TYPE_COMMIT = exports.TYPE_BRANCH = void 0;
const chalk_pipe_1 = __importDefault(require("chalk-pipe"));
const map_1 = __importDefault(require("lodash/map"));
const findSource_1 = require("./findSource");
const getBranchName_1 = __importDefault(require("./getBranchName"));
const TYPE_BRANCH = 'branch';
exports.TYPE_BRANCH = TYPE_BRANCH;
const TYPE_COMMIT = 'commit';
exports.TYPE_COMMIT = TYPE_COMMIT;
const answerBranchName = (state, input, answers, isFilter = true) => {
    const branchTypePrefix = answers.branchFlag
        ? `${answers.branchTypes}/`
        : state.config.branch.prefix;
    const branchTypePrefixTemp = isFilter
        ? branchTypePrefix
        :
            (0, chalk_pipe_1.default)('blue.bold')(branchTypePrefix);
    const branchName = input
        .replace(/ {2}/g, '--')
        .replace(/ /g, '-')
        .toLowerCase()
        .trim();
    const branchNameFilter = answers.branchFlag
        ? branchName
        : `${state.config.branch.prefix}${branchName}`;
    const branchNameTransformer = state.config.branch.format
        .replace(/\{branchType\}/g, branchTypePrefixTemp)
        .replace(/\{branchName\}/g, (0, chalk_pipe_1.default)('bold')(branchName));
    return isFilter ? branchNameFilter : branchNameTransformer;
};
const answerBreaking = (input = '', answers, isFilter = true) => {
    const breakingLabel = '💥️  BREAKING CHANGE: ';
    const breakingPrefix = answers.commitBreakingFlag ? breakingLabel : '';
    const breakingPrefixTemp = isFilter
        ? breakingPrefix
        : (0, chalk_pipe_1.default)('red.bold')(breakingPrefix);
    const breaking = input.trim();
    const breakingFilter = `\n\n${breakingPrefix}${breaking}`;
    const breakingTransformer = '{b1}{b2}'
        .replace(/\{b1\}/g, breakingPrefixTemp)
        .replace(/\{b2\}/g, (0, chalk_pipe_1.default)('bold')(breaking));
    return isFilter ? breakingFilter : breakingTransformer;
};
const questionConfig = (state, branchFlag, commitBodyFlag) => ({
    branchFlag: {
        default: branchFlag,
        message() {
            return Boolean(branchFlag)
                ? '> branchFlag (gitflow prefix):'
                : '> branchFlag (add to subject):';
        },
        name: 'branchFlag',
        type: 'confirm',
    },
    branchName: {
        filter: (input, answers) => answerBranchName(state, input, answers),
        message: '> branchName:',
        name: 'branchName',
        transformer(input, answers) {
            return answerBranchName(state, input, answers, false);
        },
        type: 'input',
    },
    branchTypes: {
        choices: (0, map_1.default)(state.types.branch, (type) => type.branch),
        message: '> branchTypes:',
        name: 'branchTypes',
        source: (_answers, input) => (0, findSource_1.findChoice)(input, state.types.branch, 'branch'),
        type: 'autocomplete',
        when(answers) {
            return answers.branchFlag;
        },
    },
    commitBody: {
        message: '> commitBody:',
        name: 'commitBody',
        type: 'editor',
        when(answers) {
            return answers.commitBodyFlag;
        },
    },
    commitBodyFlag: {
        default: commitBodyFlag,
        message: '> commitBodyFlag',
        name: 'commitBodyFlag',
        type: 'confirm',
    },
    commitBreaking: {
        filter: (input, answers) => answerBreaking(input, answers),
        message: '> commitBreaking:',
        name: 'commitBreaking',
        transformer(input, answers) {
            return answerBreaking(input, answers, false);
        },
        type: 'input',
        when(answers) {
            return answers.commitBreakingFlag;
        },
    },
    commitBreakingFlag: {
        default: false,
        message: '> commitBreakingFlag:',
        name: 'commitBreakingFlag',
        type: 'confirm',
    },
    commitMessage: {
        commitMessage: '> commitMessage',
        name: 'message',
        type: 'editor',
    },
    commitScopes: {
        message: '> commitScopes:',
        name: 'commitScopes',
        source: (_answers, input) => (0, findSource_1.findScope)(input, state.config.commit.scopes),
        type: 'autocomplete',
        when() {
            if (!state.config.commit.scopes) {
                return false;
            }
            if (!Array.isArray(state.config.commit.scopes)) {
                return false;
            }
            if (state.config.commit.scopes.length < 1) {
                return false;
            }
            return true;
        },
    },
    commitSubject: {
        filter: (input) => input.trim(),
        leadingLabel: (answers) => {
            const branchName = Boolean(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 type = answers.commitTypes;
            return state.config.commit.format
                .replace(/\{emoji\}/g, emoji)
                .replace(/\{scope\}/g, scope)
                .replace(/\{branchName\}/g, branchName)
                .replace(/\{subject\}/g, '')
                .replace(/\{type\}/g, type);
        },
        maxLength: state.config.commit.maxMessageLength,
        message: '> commitSubject:',
        name: 'commitSubject',
        source: (_answers, input) => (0, findSource_1.findChoice)(input, state.types.commit, 'commit'),
        type: 'limitedInput',
        validate: (input) => {
            const minMessageLength = state.config.commit.minMessageLength;
            const minTitleLengthErrorMessage = `The subject must have at least ${minMessageLength} chars`;
            return input.length >= minMessageLength || minTitleLengthErrorMessage;
        },
    },
    commitTypes: {
        choices: (0, map_1.default)(state.types.commit, (type) => type.commit),
        message: '> commitTypes:',
        name: 'commitTypes',
        source: (_answers, input) => (0, findSource_1.findChoice)(input, state.types.commit, 'commit'),
        type: 'autocomplete',
    },
});
exports.default = questionConfig;
//# sourceMappingURL=questionConfig.js.map