"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.oneOf = void 0;
const helpers_1 = require("../../Validator/helpers");
const RULE_NAME = 'enum';
const DEFAULT_MESSAGE = 'enum validation failed';
/**
 * Ensure the value is one of the defined choices
 */
exports.oneOf = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, [], ([choices]) => {
        /**
         * Choices are defined as a ref
         */
        if ((0, helpers_1.isRef)(choices)) {
            return {
                compiledOptions: { ref: choices.key },
            };
        }
        /**
         * Ensure value is an array or a ref
         */
        if (!choices || !Array.isArray(choices)) {
            throw new Error(`"${RULE_NAME}": expects an array of choices or a "ref"`);
        }
        return { compiledOptions: { choices: choices } };
    }),
    validate(value, compiledOptions, { errorReporter, pointer, arrayExpressionPointer, refs }) {
        let choices = [];
        /**
         * Resolve choices from the ref or use as it is, if defined as an array
         */
        if (compiledOptions.ref) {
            const runtimeChoices = refs[compiledOptions.ref].value;
            (0, helpers_1.enforceArray)(runtimeChoices, `"${RULE_NAME}": expects "refs.${compiledOptions.ref}" to be an array`);
            choices = runtimeChoices;
        }
        else if (compiledOptions.choices) {
            choices = compiledOptions.choices;
        }
        /**
         * Validation
         */
        if (!choices.includes(value)) {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer, { choices });
        }
    },
};
