"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.match = exports.reportErrors = void 0;
const tslib_1 = require("tslib");
const hasha_1 = tslib_1.__importDefault(require("hasha"));
const logger_1 = require("../logger");
const memCache = tslib_1.__importStar(require("./cache/memory"));
const stringify_1 = require("./stringify");
function getCacheKey(error) {
    const content = (0, stringify_1.safeStringify)(error);
    const key = (0, hasha_1.default)(content).slice(0, 32);
    return `err_${key}`;
}
function collectError(schema, error) {
    const { description = 'Unspecified schema' } = schema;
    const schemaErrorsMap = memCache.get('schema-errors') ?? {};
    const schemaErrors = schemaErrorsMap[description] ?? {};
    const key = getCacheKey(error);
    const schemaError = schemaErrors[key];
    if (!schemaError) {
        schemaErrors[key] = error;
        schemaErrorsMap[description] = schemaErrors;
    }
    memCache.set('schema-errors', schemaErrorsMap);
}
function reportErrors() {
    const schemaErrorsMap = memCache.get('schema-errors');
    if (!schemaErrorsMap) {
        return;
    }
    for (const [description, schemaErrors] of Object.entries(schemaErrorsMap)) {
        const errors = Object.values(schemaErrors);
        for (const err of errors) {
            logger_1.logger.warn({ description, err }, `Schema validation error`);
        }
    }
    memCache.set('schema-errors', null);
}
exports.reportErrors = reportErrors;
function match(schema, input, onError) {
    const res = schema.safeParse(input);
    const { success } = res;
    if (!success) {
        if (onError === 'warn') {
            collectError(schema, res.error);
        }
        if (onError === 'throw') {
            throw res.error;
        }
        return false;
    }
    return true;
}
exports.match = match;
//# sourceMappingURL=schema.js.map