"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.ApiErrorReporter = void 0;
const ValidationException_1 = require("../ValidationException");
/**
 * The API Error reporter formats messages as an array of objects
 */
class ApiErrorReporter {
    constructor(messages, bail) {
        this.messages = messages;
        this.bail = bail;
        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.push({
            rule,
            field: pointer,
            message: this.messages.get(pointer, rule, message, arrayExpressionPointer, args),
            ...(args ? { 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(false, this.toJSON());
    }
    /**
     * Return errors
     */
    toJSON() {
        return {
            errors: this.errors,
        };
    }
}
exports.ApiErrorReporter = ApiErrorReporter;
