"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveSettings = exports.readUserConfigFromFile = exports.loadSettings = void 0;
const cosmiconfig_1 = require("cosmiconfig");
const consts_1 = require("../consts");
const ajv_1 = __importDefault(require("ajv"));
const better_ajv_errors_1 = __importDefault(require("better-ajv-errors"));
const debug_1 = __importDefault(require("debug"));
const schemas_json_1 = __importDefault(require("./schemas.json"));
const userFacingStrings_1 = require("../userFacingStrings/userFacingStrings");
const logger = debug_1.default('root-cause:user-settings');
async function loadSettings(startFrom) {
    const configFromFile = await readUserConfigFromFile(startFrom);
    return resolveSettings(configFromFile || {});
}
exports.loadSettings = loadSettings;
async function readUserConfigFromFile(startFrom) {
    const ajvInstance = new ajv_1.default({ allErrors: true, jsonPointers: true });
    try {
        const result = await cosmiconfig_1.cosmiconfig(consts_1.CONFIG_MODULE_NAME, {
            transform(result) {
                return result;
            },
        }).search(startFrom);
        if (!result || result.isEmpty) {
            logger('config file was empty');
            logger('file path ', result === null || result === void 0 ? void 0 : result.filepath);
            return null;
        }
        logger('Found config file', result === null || result === void 0 ? void 0 : result.filepath);
        const valid = ajvInstance.validate(schemas_json_1.default, result.config);
        if (!valid) {
            console.warn(userFacingStrings_1.malformedConfigFile());
            const betterErrors = better_ajv_errors_1.default(schemas_json_1.default, result.config, ajvInstance.errors);
            console.log(betterErrors);
            console.log('-'.repeat(5));
        }
        return result.config;
    }
    catch (configSearchError) {
        logger('cosmiconfig error', configSearchError);
        return null;
    }
}
exports.readUserConfigFromFile = readUserConfigFromFile;
function resolveSettings(input) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    const screenshots = resolveScreenshots((_a = input.features) === null || _a === void 0 ? void 0 : _a.screenshots);
    const networkLogs = (_c = (_b = input.features) === null || _b === void 0 ? void 0 : _b.networkLogs) !== null && _c !== void 0 ? _c : true;
    const console = ((_d = input.features) === null || _d === void 0 ? void 0 : _d.console) ? (_e = input.features) === null || _e === void 0 ? void 0 : _e.console : true;
    const jestAssertions = ((_f = input.features) === null || _f === void 0 ? void 0 : _f.jestAssertions) ? (_g = input.features) === null || _g === void 0 ? void 0 : _g.jestAssertions : false;
    const html = (_j = (_h = input.features) === null || _h === void 0 ? void 0 : _h.html) !== null && _j !== void 0 ? _j : false;
    const features = {
        screenshots,
        console,
        networkLogs,
        jestAssertions,
        html,
    };
    return {
        features,
    };
}
exports.resolveSettings = resolveSettings;
function resolveScreenshots(input) {
    if (input === true || input === undefined) {
        return {
            format: 'jpeg',
            quality: 85,
            fullPage: false,
        };
    }
    if (input === false) {
        return false;
    }
    return {
        quality: 85,
        fullPage: false,
        ...input,
    };
}
