"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maskValue = exports.PhoneMask = exports.NoSpaceMask = exports.RGMask = exports.CPFMask = exports.CNPJMask = exports.CEPMask = exports.NumberMask = void 0;
var utils_service_1 = require("../../services/utils.service");
exports.NumberMask = function (_value, decimals) {
    if (decimals === void 0) { decimals = 2; }
    var value = _value || '';
    if (value === '')
        return value;
    var newValOnlyNumbers = utils_service_1.OnlyNumbers(value);
    if (newValOnlyNumbers.length > 15) {
        newValOnlyNumbers = newValOnlyNumbers.substr(0, 15);
    }
    return utils_service_1.NumberFormat(Number(newValOnlyNumbers) / Math.pow(10, decimals), decimals).toString();
};
exports.CEPMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = utils_service_1.OnlyNumbers(value);
    newVal = newVal.substring(0, 8);
    if (newVal.length > 5) {
        newVal = newVal.replace(/^(\d{0,5})(\d{0,3})/, '$1-$2');
    }
    return newVal;
};
exports.CNPJMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = utils_service_1.OnlyNumbers(value);
    if (newVal.length === 0) {
        newVal = '';
    }
    else if (newVal.length < 3) {
        newVal = newVal.replace(/^(\d{0,2})/, '$1');
    }
    else if (newVal.length < 6) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})/, '$1.$2');
    }
    else if (newVal.length < 9) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})(\d{0,3})/, '$1.$2.$3');
    }
    else if (newVal.length < 13) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})(\d{0,3})(\d{0,4})/, '$1.$2.$3/$4');
    }
    else {
        newVal = newVal.substring(0, 14);
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})(\d{0,3})(\d{0,4})(\d{0,2})/, '$1.$2.$3/$4-$5');
    }
    return newVal;
};
exports.CPFMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = utils_service_1.OnlyNumbers(value);
    if (newVal.length === 0) {
        newVal = '';
    }
    else if (newVal.length < 4) {
        newVal = newVal.replace(/^(\d{0,3})/, '$1');
    }
    else if (newVal.length < 7) {
        newVal = newVal.replace(/^(\d{0,3})(\d{0,3})/, '$1.$2');
    }
    else if (newVal.length < 10) {
        newVal = newVal.replace(/^(\d{0,3})(\d{0,3})(\d{0,3})/, '$1.$2.$3');
    }
    else {
        newVal = newVal.substring(0, 11);
        newVal = newVal.replace(/^(\d{0,3})(\d{0,3})(\d{0,3})(\d{0,2})/, '$1.$2.$3-$4');
    }
    return newVal;
};
exports.RGMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = utils_service_1.OnlyNumbers(value);
    if (newVal.length === 0) {
        newVal = '';
    }
    else if (newVal.length < 3) {
        newVal = newVal.replace(/^(\d{0,2})/, '$1');
    }
    else if (newVal.length < 6) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})/, '$1.$2');
    }
    else if (newVal.length < 9) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})(\d{0,3})/, '$1.$2.$3');
    }
    else {
        newVal = newVal.substring(0, 9);
        newVal = newVal.replace(/^(\d{0,2})(\d{0,3})(\d{0,3})(\d{0,2})/, '$1.$2.$3-$4');
    }
    return newVal;
};
exports.NoSpaceMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = value.replace(/ /g, '');
    return newVal;
};
exports.PhoneMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = utils_service_1.OnlyNumbers(value);
    if (newVal.length === 0) {
        newVal = '';
    }
    else if (newVal.length <= 3) {
        if (newVal.length === 3) {
            newVal = newVal.replace(/^(\d{0,2})(\d)/, '($1) $2');
        }
    }
    else if (newVal.length <= 6) {
        newVal = newVal.replace(/^(\d{0,2})(\d)/, '($1) $2');
    }
    else if (newVal.length <= 10) {
        newVal = newVal.replace(/^(\d{0,2})(\d{0,4})(\d{0,4})/, '($1) $2-$3');
    }
    else {
        newVal = newVal.substring(0, 11);
        newVal = newVal.replace(/^(\d{0,2})(\d{0,5})(\d{0,4})/, '($1) $2-$3');
    }
    return newVal;
};
function maskValue(value, _a) {
    if (value === void 0) { value = ''; }
    var number = _a.number, CEP = _a.CEP, CNPJ = _a.CNPJ, CPF = _a.CPF, RG = _a.RG, phone = _a.phone, noSpaces = _a.noSpaces, decimals = _a.decimals, withoutDotNumber = _a.withoutDotNumber;
    if (noSpaces)
        value = exports.NoSpaceMask(value);
    if (number)
        return !withoutDotNumber ? exports.NumberMask(value, decimals) : exports.NumberMask(value, decimals).replace(/\./g, '');
    if (CEP)
        return exports.CEPMask(value);
    if (CNPJ)
        return exports.CNPJMask(value);
    if (CPF)
        return exports.CPFMask(value);
    if (RG)
        return exports.RGMask(value);
    if (phone)
        return exports.PhoneMask(value);
    return value;
}
exports.maskValue = maskValue;
//# sourceMappingURL=mask.utils.js.map