"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackage = exports.getConfig = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const chalk_pipe_1 = __importDefault(require("chalk-pipe"));
const gitmoji_1 = __importDefault(require("../themes/gitmoji"));
const configFiles = ['.git-cz.json', 'changelog.config.js', 'changelog.config.json'];
const packageFile = ['package.json'];
const findOverrides = (root, files) => {
    const dir = root || process.cwd();
    for (const file of files) {
        const filename = path_1.default.resolve(dir, file);
        if (fs_1.default.existsSync(filename) && fs_1.default.statSync(filename).isFile()) {
            return require(filename);
        }
    }
    const parent = path_1.default.resolve(dir, '..');
    if (parent !== dir) {
        return findOverrides(parent, files);
    }
    const pkgFilename = path_1.default.join(dir, 'package.json');
    if (fs_1.default.existsSync(pkgFilename)) {
        try {
            const changelog = require(pkgFilename).config.commitizen.changelog;
            if (changelog) {
                return changelog;
            }
        }
        catch (error) { }
    }
    return {};
};
const getConfig = (root) => {
    const overrides = findOverrides(root, configFiles);
    if (typeof overrides !== 'object') {
        console.log((0, chalk_pipe_1.default)('red.bold')('Expected changelog config to be an object.'));
        process.exit(1);
    }
    const config = Object.assign(Object.assign({}, gitmoji_1.default), overrides);
    return config;
};
exports.getConfig = getConfig;
const getPackage = (root) => {
    const overrides = findOverrides(root, packageFile);
    return Object.assign({}, overrides);
};
exports.getPackage = getPackage;
//# sourceMappingURL=getFile.js.map