"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.configCache = void 0;
exports.loadDefaultConfig = loadDefaultConfig;
exports.clearConfigCache = clearConfigCache;
const path_1 = __importDefault(require("path"));
const load_1 = require("./load");
/**
 * Cache to store loaded configurations for different directories.
 */
exports.configCache = new Map();
/**
 * Loads the default configuration file from the specified directory.
 *
 * @param dir - The directory to search for configuration files. Defaults to the current working directory.
 * @param configName - The name of the configuration file to load. Defaults to 'trusysconfig'.
 * @returns A promise that resolves to an object containing the default configuration and its file path.
 * The default configuration is partial, and the file path may be undefined if no configuration is found.
 */
async function loadDefaultConfig(dir, configName = 'trusysconfig') {
    dir = dir || process.cwd();
    // Check if the result is already cached
    const cacheKey = `${dir}:${configName}`;
    if (exports.configCache.has(cacheKey)) {
        return exports.configCache.get(cacheKey);
    }
    let defaultConfig = {};
    let defaultConfigPath;
    // NOTE: sorted by frequency of use
    const extensions = ['yaml', 'yml', 'json', 'cjs', 'cts', 'js', 'mjs', 'mts', 'ts'];
    for (const ext of extensions) {
        const configPath = path_1.default.join(dir, `${configName}.${ext}`);
        const maybeConfig = await (0, load_1.maybeReadConfig)(configPath);
        if (maybeConfig) {
            defaultConfig = maybeConfig;
            defaultConfigPath = configPath;
            break;
        }
    }
    const result = { defaultConfig, defaultConfigPath };
    exports.configCache.set(cacheKey, result);
    return result;
}
function clearConfigCache() {
    exports.configCache.clear();
}
//# sourceMappingURL=default.js.map