"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONFIGS = exports.parseConfig = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const parse_1 = require("@grund/parse");
const tools_1 = require("@grund/tools");
const paths_1 = require("../paths");
const defaultConfig = {};
function parseConfig(exists, configPath) {
    const isJson = configPath === null || configPath === void 0 ? void 0 : configPath.endsWith('.json');
    if (!exists || !configPath) {
        return defaultConfig;
    }
    if (isJson) {
        const configString = fs_1.default.readFileSync(configPath, 'utf-8');
        return tools_1.safeJsonParse(configString, {});
    }
    return parse_1.parseTsFile(configPath);
}
exports.parseConfig = parseConfig;
function createConfig(fileNames, shouldParseConfig = false) {
    const filePaths = fileNames.map((fileName) => path_1.default.join(paths_1.CURRENT_DIR, fileName));
    const foundFilePath = filePaths.find((filePath) => fs_1.default.existsSync(filePath));
    const filePath = foundFilePath;
    const exists = !!foundFilePath;
    const config = shouldParseConfig ? parseConfig(exists, filePath) : null;
    return { filePath, exists, config };
}
exports.CONFIGS = {
    // Project
    LERNA: createConfig(['lerna.json']),
    TSCONFIG: createConfig(['tsconfig.json'], true),
    // Dev
    NODE_DEV: createConfig(['.node-dev.json'], true),
    NODEMON: createConfig(['nodemon.json'], true),
    WEBPACK: createConfig(['webpack.config.js', 'webpack.config.babel.js', 'webpack.config.ts']),
    // Native
    APP_CONFIG: createConfig(['app.json'], true),
    RN_CONFIG: createConfig(['react-native.config.js'], true),
    METRO: createConfig(['metro.config']),
    // Grund
    GRUND: createConfig(['grund.config.json'], true),
};
