"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Login = void 0;
var v_eris_1 = require("v-eris");
var policy_1 = require("./policy");
var Login = /** @class */ (function () {
    function Login() {
    }
    Login.checkLogin = function (name) {
        if (name.length > 32 || name.length < 3 || !name.match(/^[a-z0-9]+$/i)) {
            return {
                description: "Login name must be between 3 and 32 characters long"
                    + " and can contain any letters from a to z and any numbers from 0 through 9",
                min: 3,
                max: 32,
                code: 411
            };
        }
        ;
        return false;
    };
    ;
    Login.checkEmail = function (email) {
        if (email.length > 128 || email.length < 3 || !email.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) {
            return {
                description: "Email is incorrect",
                code: 411
            };
        }
        ;
        return false;
    };
    ;
    Login.checkName = function (name) {
        if (name.length > 32 || name.length < 3 || !name.match(/^[a-z0-9]+$/i)) {
            return {
                description: "Name must be between 3 and 32 characters long"
                    + " and can contain any letters from a to z and any numbers from 0 through 9",
                min: 3,
                max: 32,
                code: 411
            };
        }
        ;
        return false;
    };
    ;
    Login.resistance = function (state, key) {
        return (state & key) == key;
    };
    ;
    Login.checkPass = function (password, policy) {
        if (!policy)
            return false;
        var value = v_eris_1.Common.string(password).trim();
        var max = v_eris_1.Common.uint(policy.max);
        var min = v_eris_1.Common.uint(policy.min);
        var resistance = v_eris_1.Common.uint(policy.resistance);
        var errors = [];
        if (value.length > max
            ||
                value.length < min) {
            errors.push({
                description: "Password must be between " + min + " and " + max + " characters long",
                tokens: ["Admin::Table::Users::PasswordCondition::LettersLength"],
                min: min,
                max: max
            });
        }
        ;
        if (this.resistance(resistance, policy_1.PolicyResistance.Characters)
            &&
                !value.match(/[A-Za-zа-яА-Я]/g)) {
            errors.push({
                description: "Password must include at least one character",
                tokens: ["Admin::Table::Users::PasswordCondition::Characters"]
            });
        }
        ;
        if (this.resistance(resistance, policy_1.PolicyResistance.Numbers)
            &&
                !value.match(/[0-9]/g)) {
            errors.push({
                description: "Password must include at least one number",
                tokens: ["Admin::Table::Users::PasswordCondition::Numbers"]
            });
        }
        ;
        if (this.resistance(resistance, policy_1.PolicyResistance.Special)
            &&
                !value.match(/[!-\/:-@[-`{-~]/g)) {
            errors.push({
                description: "Password must include at least one special character",
                tokens: ["Admin::Table::Users::PasswordCondition::Special"]
            });
        }
        ;
        if (this.resistance(resistance, policy_1.PolicyResistance.NoRepeat)
            &&
                value.match(/^(?!.*(\w)\1{1,}).+$/g)) {
            errors.push({
                description: "Password must not include repeating characters",
                tokens: ["Admin::Table::Users::PasswordCondition::NoRepeat"]
            });
        }
        ;
        if (this.resistance(resistance, policy_1.PolicyResistance.DifferentCase)
            &&
                (!value.match(/[A-ZА-Я]/g) || !value.match(/[a-zа-я]/g))) {
            errors.push({
                description: "Password must include characters width different cases",
                tokens: ["Admin::Table::Users::PasswordCondition::DifferentCase"]
            });
        }
        ;
        return errors.length ? errors : false;
    };
    ;
    Login.checkSalt = function (salt) {
        if (salt.length < 58) {
            return {
                description: "Password hashing unexpected error",
                code: 413
            };
        }
        ;
        return false;
    };
    ;
    Login.checkPassMatch = function (password1, password2) {
        if (password1 && password2 && password1 != password2) {
            return {
                description: "Passwords don't match",
                code: 414
            };
        }
        ;
        return false;
    };
    ;
    return Login;
}());
exports.Login = Login;
;
//# sourceMappingURL=login.js.map