"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = void 0;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const types = __importStar(require("./types"));
const general = __importStar(require("./general"));
function getConfig({ baseDir = process.cwd(), paths = ['applitools.config.cjs', 'applitools.config.js', 'eyes.config.js', 'eyes.json'], params = [], traverse = { stopDir: os.homedir() }, strict, logger, } = {}) {
    const envPath = general.getEnvValue('CONFIG_PATH');
    if (envPath) {
        traverse = false;
        strict = true;
        if (fs.statSync(envPath).isDirectory()) {
            baseDir = envPath;
        }
        else {
            paths = [general.getEnvValue('CONFIG_PATH')];
        }
    }
    const searchedPaths = [];
    let priorityPath;
    while (!priorityPath) {
        const resolvedPaths = paths.map(probablePath => path.resolve(baseDir, probablePath));
        priorityPath = resolvedPaths.find(resolvedPath => {
            searchedPaths.push(resolvedPath);
            return fs.existsSync(resolvedPath);
        });
        if (!traverse)
            break;
        const nextDir = path.dirname(baseDir);
        if (nextDir === baseDir || baseDir === traverse.stopDir)
            break;
        baseDir = nextDir;
    }
    let config = {};
    if (priorityPath) {
        try {
            const moduleCache = require.cache[priorityPath];
            delete require.cache[priorityPath];
            config = { ...config, ...require(priorityPath) };
            require.cache[priorityPath] = moduleCache;
        }
        catch (error) {
            logger === null || logger === void 0 ? void 0 : logger.error(`An error occurred while loading configuration file (${priorityPath}):`, error);
            if (strict)
                throw error;
        }
    }
    else if (strict && !traverse) {
        logger === null || logger === void 0 ? void 0 : logger.error(`Could not find configuration file at: ${searchedPaths.join(', ')}`);
        throw new Error('Could not find configuration file');
    }
    params.forEach(param => {
        const value = general.getEnvValue(param.replace(/(.)([A-Z])/g, '$1_$2').toUpperCase());
        if (!types.isNotDefined(value)) {
            config[param] = Number(value) || (value === 'true' ? true : value === 'false' ? false : value);
        }
    });
    return config;
}
exports.getConfig = getConfig;
