"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.file = void 0;
const helpers_1 = require("../../Validator/helpers");
const DEFAULT_MESSAGE = 'file validation failed';
const RULE_NAME = 'file';
/**
 * Ensure the value is a valid file instance
 */
exports.file = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, [], ([options]) => {
        const validationOptions = {};
        if (options && options.size) {
            validationOptions.size = options.size;
        }
        if (options && options.extnames) {
            validationOptions.extnames = options.extnames;
        }
        return {
            compiledOptions: validationOptions,
        };
    }),
    validate(fileToValidate, compiledOptions, { errorReporter, pointer, arrayExpressionPointer }) {
        /**
         * Raise error when not a multipart file instance
         */
        if (!fileToValidate.isMultipartFile) {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer, compiledOptions);
            return;
        }
        /**
         * Set size when it's defined in the options
         */
        if (fileToValidate.sizeLimit === undefined && compiledOptions.size) {
            fileToValidate.sizeLimit = compiledOptions.size;
        }
        /**
         * Set extensions when it's defined in the options
         */
        if (fileToValidate.allowedExtensions === undefined && compiledOptions.extnames) {
            fileToValidate.allowedExtensions = compiledOptions.extnames;
        }
        /**
         * Validate the file
         */
        fileToValidate.validate();
        /**
         * Report errors
         */
        fileToValidate.errors.forEach((error) => {
            errorReporter.report(pointer, `${RULE_NAME}.${error.type}`, error.message, arrayExpressionPointer, compiledOptions);
        });
    },
};
