"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleIContainsAll = exports.handleContainsAll = exports.handleIContainsAny = exports.handleContainsAny = exports.handleIContains = exports.handleContains = void 0;
const invariant_1 = __importDefault(require("../util/invariant"));
const handleContains = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"contains" assertion type must have a string or number value');
    (0, invariant_1.default)(typeof renderedValue === 'string' || typeof renderedValue === 'number', '"contains" assertion type must have a string or number value');
    const pass = outputString.includes(String(renderedValue)) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain "${renderedValue}"`,
        assertion,
    };
};
exports.handleContains = handleContains;
const handleIContains = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"icontains" assertion type must have a string or number value');
    (0, invariant_1.default)(typeof renderedValue === 'string' || typeof renderedValue === 'number', '"icontains" assertion type must have a string or number value');
    const pass = outputString.toLowerCase().includes(String(renderedValue).toLowerCase()) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain "${renderedValue}"`,
        assertion,
    };
};
exports.handleIContains = handleIContains;
const handleContainsAny = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"contains-any" assertion type must have a value');
    if (typeof renderedValue === 'string') {
        // Handle quoted values and escaped commas
        renderedValue =
            renderedValue
                .match(/(".*?"|[^,]+)(?=\s*,|\s*$)/g)
                ?.map((v) => v.trim().replace(/^"|"$/g, '')) ?? [];
    }
    (0, invariant_1.default)(Array.isArray(renderedValue), '"contains-any" assertion type must have an array value');
    const pass = renderedValue.some((value) => outputString.includes(String(value))) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain one of "${renderedValue.join(', ')}"`,
        assertion,
    };
};
exports.handleContainsAny = handleContainsAny;
const handleIContainsAny = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"icontains-any" assertion type must have a value');
    if (typeof renderedValue === 'string') {
        renderedValue = renderedValue.split(',').map((v) => v.trim());
    }
    (0, invariant_1.default)(Array.isArray(renderedValue), '"icontains-any" assertion type must have an array value');
    const pass = renderedValue.some((value) => outputString.toLowerCase().includes(String(value).toLowerCase())) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain one of "${renderedValue.join(', ')}"`,
        assertion,
    };
};
exports.handleIContainsAny = handleIContainsAny;
const handleContainsAll = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"contains-all" assertion type must have a value');
    if (typeof renderedValue === 'string') {
        renderedValue = renderedValue.split(',').map((v) => v.trim());
    }
    (0, invariant_1.default)(Array.isArray(renderedValue), '"contains-all" assertion type must have an array value');
    const missingStrings = renderedValue.filter((value) => !outputString.includes(String(value)));
    const pass = (missingStrings.length === 0) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain all of [${renderedValue.join(', ')}]. Missing: [${missingStrings.join(', ')}]`,
        assertion,
    };
};
exports.handleContainsAll = handleContainsAll;
const handleIContainsAll = ({ assertion, renderedValue, outputString, inverse, }) => {
    (0, invariant_1.default)(renderedValue, '"icontains-all" assertion type must have a value');
    if (typeof renderedValue === 'string') {
        renderedValue = renderedValue.split(',').map((v) => v.trim());
    }
    (0, invariant_1.default)(Array.isArray(renderedValue), '"icontains-all" assertion type must have an array value');
    const missingStrings = renderedValue.filter((value) => !outputString.toLowerCase().includes(String(value).toLowerCase()));
    const pass = (missingStrings.length === 0) !== inverse;
    return {
        pass,
        score: pass ? 1 : 0,
        reason: pass
            ? 'Assertion passed'
            : `Expected output to ${inverse ? 'not ' : ''}contain all of [${renderedValue.join(', ')}]. Missing: [${missingStrings.join(', ')}]`,
        assertion,
    };
};
exports.handleIContainsAll = handleIContainsAll;
//# sourceMappingURL=contains.js.map