"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleNotIsJson = handleNotIsJson;
exports.handleNotContainsJson = handleNotContainsJson;
const js_yaml_1 = __importDefault(require("js-yaml"));
const invariant_1 = __importDefault(require("../util/invariant"));
const json_1 = require("../util/json");
function handleNotIsJson({ outputString, renderedValue, inverse, valueFromScript, assertion, }) {
    let parsedJson;
    let pass;
    try {
        parsedJson = JSON.parse(outputString);
        // For not-json, we want the opposite behavior - pass when JSON is invalid
        pass = inverse;
    }
    catch {
        pass = !inverse;
    }
    if (pass && renderedValue) {
        let validate;
        if (typeof renderedValue === 'string') {
            if (renderedValue.startsWith('file://')) {
                // Reference the JSON schema from external file
                const schema = valueFromScript;
                (0, invariant_1.default)(schema, 'not-is-json references a file that does not export a JSON schema');
                validate = (0, json_1.getAjv)().compile(schema);
            }
            else {
                const scheme = js_yaml_1.default.load(renderedValue);
                validate = (0, json_1.getAjv)().compile(scheme);
            }
        }
        else if (typeof renderedValue === 'object') {
            validate = (0, json_1.getAjv)().compile(renderedValue);
        }
        else {
            throw new Error('not-is-json assertion must have a string or object value');
        }
        // For not-json, we want the opposite behavior - pass when JSON doesn't conform to schema
        pass = !validate(parsedJson);
        if (!pass) {
            return {
                pass,
                score: 0,
                reason: `JSON conforms to the provided schema, but not-is-json expects it not to conform.`,
                assertion,
            };
        }
    }
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass ? 'Assertion passed' : 'Expected output to not be valid JSON',
        assertion,
    };
}
function handleNotContainsJson({ assertion, renderedValue, outputString, inverse, valueFromScript, }) {
    let errorMessage = 'Expected output to not contain valid JSON';
    const jsonObjects = (0, json_1.extractJsonObjects)(outputString);
    // For not-contains-json, we want the opposite behavior - pass when no valid JSON is found
    let pass = inverse ? jsonObjects.length > 0 : jsonObjects.length === 0;
    for (const jsonObject of jsonObjects) {
        if (renderedValue) {
            let validate;
            if (typeof renderedValue === 'string') {
                if (renderedValue.startsWith('file://')) {
                    // Reference the JSON schema from external file
                    const schema = valueFromScript;
                    (0, invariant_1.default)(schema, 'not-contains-json references a file that does not export a JSON schema');
                    validate = (0, json_1.getAjv)().compile(schema);
                }
                else {
                    const scheme = js_yaml_1.default.load(renderedValue);
                    validate = (0, json_1.getAjv)().compile(scheme);
                }
            }
            else if (typeof renderedValue === 'object') {
                validate = (0, json_1.getAjv)().compile(renderedValue);
            }
            else {
                throw new Error('not-contains-json assertion must have a string or object value');
            }
            // For not-contains-json, we want the opposite behavior - pass when JSON doesn't conform to schema
            pass = !validate(jsonObject);
            if (pass) {
                break;
            }
            else {
                errorMessage = `JSON conforms to the provided schema, but not-contains-json expects it not to conform.`;
            }
        }
    }
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass ? 'Assertion passed' : errorMessage,
        assertion,
    };
}
//# sourceMappingURL=not-json.js.map