"use strict";
/*
 * @adonisjs/validator
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.requiredIfNotExistsAny = void 0;
const helpers_1 = require("../../Validator/helpers");
const RULE_NAME = 'requiredIfNotExistsAny';
const DEFAULT_MESSAGE = 'requiredIfNotExistsAny validation failed';
/**
 * Ensure the value exists. `null`, `undefined` and `empty string`
 * fails the validation
 */
exports.requiredIfNotExistsAny = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, [], ([fields]) => {
        if (!fields) {
            throw new Error(`"${RULE_NAME}": expects an array of "fields"`);
        }
        if (!Array.isArray(fields)) {
            throw new Error(`"${RULE_NAME}": expects "fields" to be an array`);
        }
        return {
            allowUndefineds: true,
            compiledOptions: {
                fields,
            },
        };
    }),
    validate(value, compiledOptions, { root, tip, errorReporter, pointer, arrayExpressionPointer }) {
        const anyFieldMissing = compiledOptions.fields.find((field) => {
            const otherFieldValue = (0, helpers_1.getFieldValue)(field, root, tip);
            return !(0, helpers_1.exists)(otherFieldValue);
        });
        if (anyFieldMissing && !(0, helpers_1.exists)(value)) {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer, {
                otherFields: compiledOptions.fields,
            });
        }
    },
};
