var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "jquery-validation/dist/jquery.validate.min", "jquery-validation/dist/additional-methods.min", "jquery-validation/dist/localization/messages_uk.min"], function (require, exports, jquery_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.Validator = exports.is_valid_inn = exports.is_valid_okpo = exports.validatorAddMethod = exports.validatorAddFuncMethod = exports.elementRules = void 0;
    jquery_1 = __importDefault(jquery_1);
    //import 'cci-ui/css/limitless.validation.css';
    function elementRules(element, action, options) {
        // @ts-ignore
        jQuery(element).rules(action, options);
    }
    exports.elementRules = elementRules;
    function validatorAddFuncMethod(name, method, message) {
        // @ts-ignore
        jQuery.validator.addMethod(name, method, message);
    }
    exports.validatorAddFuncMethod = validatorAddFuncMethod;
    function validatorAddMethod(name, regexp, message) {
        validatorAddFuncMethod(name, function (value, element) {
            let result;
            if (regexp instanceof RegExp) {
                result = regexp.test(value);
            }
            else {
                result = regexp.map(i => i.test(value)).filter(i => i).length > 0;
            }
            // @ts-ignore
            return this.optional(element) || result;
        }, message);
    }
    exports.validatorAddMethod = validatorAddMethod;
    // @ts-ignore
    validatorAddMethod('uaDate', /^(0[1-9]|[12]\d|3[01])[.](0[1-9]|1[012])[.](19|20)\d{2}$/, jQuery.validator.messages.date);
    validatorAddMethod('uaTaxCode', [/^\d{8}$/, /^\d{10}$/, /^[А-Я]{2}\d{6}$/i], 'Невірний формат коду РНОКПП/ЄДРПОУ');
    validatorAddMethod('uaPassNum', [/^\d{6}$/, /^\d{9}$/], 'Невірний формат номеру паспорта');
    validatorAddMethod('phoneNum', /^\+\d{12}$/, 'Номер потрібно зазначати у міжнародному форматі! Наприклад: +380443399545');
    validatorAddFuncMethod('uaTaxCodeDigit', function (value, element) {
        let result;
        if (value) {
            if (/^0+$/.test(value)) {
                result = false;
            }
            else {
                if (/^\d{8}$/.test(value)) {
                    result = is_valid_okpo(value);
                }
                else if (/^\d{10}$/.test(value)) {
                    result = is_valid_inn(value);
                }
            }
        }
        else {
            result = false;
        }
        // @ts-ignore
        return this.optional(element) || result;
    }, 'Невірний код РНОКПП/ЄДРПОУ');
    function multiplyMatrices(matrixA, matrixB) {
        return matrixA.reduce((previousValue, currentValue, currentIndex) => previousValue + currentValue * matrixB[currentIndex], 0);
    }
    function is_valid_okpo(code) {
        const asMatrix2 = parseInt(code) > 30000000 && parseInt(code) < 60000000;
        const array = code.padStart(8, '0').split('').map(i => parseInt(i));
        let matrix = asMatrix2 ? [7, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 7];
        let result = multiplyMatrices(array.slice(0, 7), matrix) % 11;
        if (result > 9) {
            matrix = asMatrix2 ? [9, 3, 4, 5, 6, 7, 8] : [3, 4, 5, 6, 7, 8, 9];
            result = multiplyMatrices(array.slice(0, 7), matrix) % 11;
            if (result > 9) {
                result = result % 10;
            }
        }
        return result == array[7];
    }
    exports.is_valid_okpo = is_valid_okpo;
    function is_valid_inn(code) {
        const array = code.padStart(10, '0').split('').map(i => parseInt(i));
        let matrix = [-1, 5, 7, 9, 4, 6, 10, 5, 7, 0];
        return (multiplyMatrices(array.slice(0, 9), matrix) % 11) % 10 == array[9];
    }
    exports.is_valid_inn = is_valid_inn;
    // @ts-ignore
    jQuery.validator.setDefaults({
        ignore: 'input[type=hidden], .select2-search__field',
        errorClass: 'validation-invalid-label',
        validClass: 'validation-valid-label',
        highlight: (element, errorClass) => {
            (0, jquery_1.default)(element).removeClass(errorClass);
        },
        unhighlight: (element, errorClass) => {
            (0, jquery_1.default)(element).removeClass(errorClass);
        },
        // Different components require proper error label placement
        errorPlacement: function (error, element) {
            // Unstyled checkboxes, radios
            if (element.parents().hasClass('form-check')) {
                error.appendTo(element.parents('.form-check').parent());
            }
            // Input with icons and Select2
            else if (element.parents().hasClass('form-group-feedback') || element.hasClass('select2-hidden-accessible')) {
                error.appendTo(element.parent());
            }
            // Input group, styled file input
            else if (element.parent().is('.uniform-uploader, .uniform-select') || element.parents().hasClass('input-group')) {
                error.appendTo(element.parent().parent());
            }
            // Other elements
            else {
                error.insertAfter(element);
            }
        }
    });
    //  add check to element with similar name
    // @ts-ignore
    jQuery.validator.prototype.checkForm = function () {
        this.prepareForm();
        for (let i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++) {
            if (this.findByName(elements[i].name).length != undefined && this.findByName(elements[i].name).length > 1) {
                for (let cnt = 0; cnt < this.findByName(elements[i].name).length; cnt++) {
                    this.check(this.findByName(elements[i].name)[cnt]);
                }
            }
            else {
                this.check(elements[i]);
            }
        }
        return this.valid();
    };
    //show label near right element (with similar name)
    // @ts-ignore
    jQuery.validator.prototype.idOrName = function (element) {
        if (!element.id) {
            element.id = new Date().getTime();
        }
        return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
    };
    function Validator(form, options) {
        // @ts-ignore
        return (0, jquery_1.default)(form).validate(options);
    }
    exports.Validator = Validator;
});
