"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.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeUrl = void 0;
const normalize_url_1 = __importDefault(require("normalize-url"));
const helpers_1 = require("../../Validator/helpers");
const RULE_NAME = 'normalizeUrl';
/**
 * Normalize URL
 */
exports.normalizeUrl = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, ['string'], (args) => {
        const options = Object.assign({}, args[0]);
        if (options.defaultProtocol && !options.defaultProtocol.endsWith(':')) {
            options.defaultProtocol = `${options.defaultProtocol}:`;
        }
        return {
            compiledOptions: options,
        };
    }),
    validate(value, compiledOptions, { mutate }) {
        /**
         * Ignore non string values
         */
        if (typeof value !== 'string') {
            return;
        }
        /**
         * Normalize URL
         */
        mutate((0, normalize_url_1.default)(value, compiledOptions));
    },
};
