"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maskValue = exports.DateMask = exports.FileSize = exports.PhoneMask = exports.NoSpaceMask = exports.RGMask = exports.CNPJAndCPFMask = exports.CPFMask = exports.CNPJMask = exports.CEPMask = exports.NumberMask = void 0;
var react_utils_1 = require("@devesharp/react-utils");
var NumberMask = function (_value, decimals) {
    if (decimals === void 0) { decimals = 2; }
    if (typeof _value === 'number') {
        _value = _value.toFixed(decimals).toString();
    }
    var value = _value || '';
    if (value === '')
        return value;
    var newValOnlyNumbers = react_utils_1.OnlyNumbers(value);
    if (newValOnlyNumbers.length > 15) {
        newValOnlyNumbers = newValOnlyNumbers.substr(0, 15);
    }
    return react_utils_1.NumberFormat(Number(newValOnlyNumbers) / Math.pow(10, decimals), decimals).toString();
};
exports.NumberMask = NumberMask;
var CEPMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_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.CEPMask = CEPMask;
var CNPJMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_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.CNPJMask = CNPJMask;
var CPFMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_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.CPFMask = CPFMask;
var CNPJAndCPFMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_1.OnlyNumbers(value);
    if (newVal.length <= 11) {
        return exports.CPFMask(_value);
    }
    return exports.CNPJMask(_value);
};
exports.CNPJAndCPFMask = CNPJAndCPFMask;
var RGMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_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.RGMask = RGMask;
var NoSpaceMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = value.replace(/ /g, '');
    return newVal;
};
exports.NoSpaceMask = NoSpaceMask;
var PhoneMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_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;
};
exports.PhoneMask = PhoneMask;
var FileSize = function (_value) {
    var value = Number(_value);
    if (value >= 1073741824) {
        return (value / 1073741824).toFixed(2) + " GB";
    }
    if (value >= 1048576) {
        return (value / 1048576).toFixed(2) + " MB";
    }
    return (value / 1024).toFixed(0) + " KB";
};
exports.FileSize = FileSize;
var DateMask = function (_value) {
    var value = _value || '';
    if (value === '')
        return value;
    var newVal = react_utils_1.OnlyNumbers(value);
    if (newVal.length === 0) {
        newVal = '';
    }
    else if (newVal.length <= 2) {
        newVal = newVal.replace(/^(\d{0,2})/, '$1');
    }
    else if (newVal.length <= 4) {
        newVal = newVal.replace(/^(\d{0,2})(\d)/, '$1/$2');
    }
    else {
        newVal = newVal.substring(0, 8);
        newVal = newVal.replace(/^(\d{0,2})(\d{0,2})(\d)/, '$1/$2/$3');
    }
    return newVal;
};
exports.DateMask = DateMask;
function maskValue(value, mask, options) {
    if (value === void 0) { value = ''; }
    if (options === void 0) { options = {}; }
    if (mask == 'no-spaces')
        value = exports.NoSpaceMask(value);
    if (mask == 'number')
        return !(options === null || options === void 0 ? void 0 : options.withoutDotNumber)
            ? exports.NumberMask(value, options === null || options === void 0 ? void 0 : options.decimals)
            : exports.NumberMask(value, options === null || options === void 0 ? void 0 : options.decimals).replace(/\./g, '');
    if (mask == 'CEP')
        return exports.CEPMask(value);
    if (mask == 'CNPJ')
        return exports.CNPJMask(value);
    if (mask == 'CPF')
        return exports.CPFMask(value);
    if (mask == 'CNPJ/CPF')
        return exports.CNPJAndCPFMask(value);
    if (mask == 'RG')
        return exports.RGMask(value);
    if (mask == 'phone')
        return exports.PhoneMask(value);
    if (mask == 'date')
        return exports.DateMask(value);
    if (mask == 'file-size')
        return exports.FileSize(value);
    return value;
}
exports.maskValue = maskValue;
//# sourceMappingURL=mask.utils.js.map