"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.VanillaErrorReporter = void 0;
const ValidationException_1 = require("../ValidationException");
/**
 * The vanilla error reporter to stores an array of messages in
 * reference to a given field. Tailored for displaying messages
 * next to a form field.
 */
class VanillaErrorReporter {
    constructor(messages, bail) {
        this.messages = messages;
        this.bail = bail;
        /**
         * Collected errors
         */
        this.errors = {};
        /**
         * A boolean to know if an error has been reported or
         * not
         */
        this.hasErrors = false;
    }
    /**
     * Report a new error
     */
    report(pointer, rule, message, arrayExpressionPointer, args) {
        this.hasErrors = true;
        this.errors[pointer] = this.errors[pointer] || [];
        this.errors[pointer].push(this.messages.get(pointer, rule, message, arrayExpressionPointer, args));
        /**
         * Raise exception right away when `bail=true`.
         */
        if (this.bail) {
            throw this.toError();
        }
    }
    /**
     * Returns an instance of [[ValidationException]]
     */
    toError() {
        return new ValidationException_1.ValidationException(true, this.toJSON());
    }
    /**
     * Return errors
     */
    toJSON() {
        return this.errors;
    }
}
exports.VanillaErrorReporter = VanillaErrorReporter;
