"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = validate;
const app_1 = require("../../app");
const http_1 = require("../../http");
const utils_1 = require("../../utils");
function validate(op, callBack) {
    const { element, rules = {}, messages = {}, afterValidation } = op;
    if (element)
        var $form = utils_1.Dom.getElementById(element);
    // @ts-ignore
    $form.validate({
        rules,
        messages: messages,
        ignore: ':hidden:not(textarea)',
        errorElement: 'em',
        errorPlacement(error, el) {
            error.addClass('text-red-600');
            if (el.prop('type') === 'checkbox') {
                error.insertAfter(el.parent().last());
                return;
            }
            if (el.prop('type') === 'radio') {
                error.insertAfter(el.parent().parent().find('span'));
                return;
            }
            error.insertAfter(el);
        },
        highlight(el) {
            $(el).addClass('border-yellow-400 placeholder:text-red-600 focus:border-yellow-400').removeClass('border-stroke placeholder:text-black focus:border-primary');
        },
        unhighlight(el) {
            $(el).addClass('border-stroke placeholder:text-black focus:border-primary').removeClass('border-yellow-400 placeholder:text-red-600 focus:border-yellow-400');
        },
        submitHandler(form) {
            if (afterValidation) {
                afterValidation(form, op);
                return;
            }
            if (!utils_1.Guard.hasInternet())
                return;
            const data = new FormData(form);
            const csrf_token = app_1.Config.csrf_token;
            const locale = app_1.Config.locale;
            if (csrf_token && locale) {
                data.append('_token', csrf_token);
                data.append('client', 'w');
                data.append('lang', locale);
            }
            http_1.Http.ajax.send({ ...op, payload: data }, callBack);
            return;
        },
    });
}
//# sourceMappingURL=validate.js.map