"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.equalTo = void 0;
const helpers_1 = require("../../Validator/helpers");
const RULE_NAME = 'equalTo';
const DEFAULT_MESSAGE = 'equalTo validation failed';
/**
 * Validation signature for the "equalTo". Non-string values are
 * ignored.
 */
exports.equalTo = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, ['string'], ([equalToValue], _) => {
        if ((0, helpers_1.isRef)(equalToValue)) {
            return {
                compiledOptions: { ref: equalToValue.key },
            };
        }
        if (!equalToValue || typeof equalToValue !== 'string') {
            throw new Error(`The "${RULE_NAME}" rule expects equalToValue to be a string`);
        }
        return {
            compiledOptions: { fieldValue: equalToValue },
        };
    }),
    validate(value, compiledOptions, { errorReporter, arrayExpressionPointer, pointer, refs }) {
        /**
         * Ignore non-string values. The user must apply string rule
         * to validate string
         */
        if (typeof value !== 'string') {
            return;
        }
        let fieldValue;
        if (compiledOptions.ref) {
            const runtimeFieldValue = refs[compiledOptions.ref].value;
            fieldValue = runtimeFieldValue;
        }
        else if (compiledOptions.fieldValue) {
            fieldValue = compiledOptions.fieldValue;
        }
        if (value !== fieldValue) {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer);
        }
    },
};
