"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Input = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var useEventListener_1 = require("../../../hooks/useEventListener");
var utils_service_1 = require("../../../services/utils.service");
var AppContext_1 = __importDefault(require("../../AppProvider/AppContext"));
var mask_utils_1 = require("../../MaskText/mask.utils");
var Input_style_1 = __importDefault(require("./Input.style"));
var InputPure_1 = require("./InputPure");
exports.Input = react_1.default.forwardRef(function Input(props, ref) {
    props = __assign({ decimals: 0 }, props);
    var name = props.name, title = props.title, _a = props.textRight, textRight = _a === void 0 ? false : _a, _b = props.textCenter, textCenter = _b === void 0 ? false : _b, prefix = props.prefix, _c = props.decimals, decimals = _c === void 0 ? 0 : _c, themeInput = props.theme, suffix = props.suffix, _d = props.loading, loading = _d === void 0 ? false : _d, _e = props.onEnter, onEnter = _e === void 0 ? function () { } : _e, _f = props.number, number = _f === void 0 ? false : _f, _g = props.withoutDotNumber, withoutDotNumber = _g === void 0 ? false : _g, _h = props.CEP, CEP = _h === void 0 ? false : _h, _j = props.CNPJ, CNPJ = _j === void 0 ? false : _j, _k = props.CPF, CPF = _k === void 0 ? false : _k, _l = props.RG, RG = _l === void 0 ? false : _l, _m = props.phone, phone = _m === void 0 ? false : _m, _o = props.noSpaces, noSpaces = _o === void 0 ? false : _o, _p = props.as, as = _p === void 0 ? InputPure_1.InputPure : _p, rest = __rest(props, ["name", "title", "textRight", "textCenter", "prefix", "decimals", "theme", "suffix", "loading", "onEnter", "number", "withoutDotNumber", "CEP", "CNPJ", "CPF", "RG", "phone", "noSpaces", "as"]);
    var _q = react_1.useContext(AppContext_1.default).pureComponents, _r = (_q === void 0 ? {} : _q).Input, InputComponent = _r === void 0 ? null : _r;
    var Component = InputComponent !== null && InputComponent !== void 0 ? InputComponent : as;
    var inputRef = react_1.useRef();
    react_1.useImperativeHandle(ref, function () { return inputRef.current; }, [inputRef]);
    var _s = react_1.useState(false), focus = _s[0], setFocus = _s[1];
    var _t = core_1.useField(name), registerField = _t.registerField, _u = _t.defaultValue, defaultValue = _u === void 0 ? '' : _u, error = _t.error, fieldName = _t.fieldName, clearError = _t.clearError;
    var defaultValueInput = mask_utils_1.maskValue(defaultValue, props);
    useEventListener_1.useEventListener('keydown.enter', function () { return onEnter(); }, inputRef);
    useEventListener_1.useEventListener('input', function (evt) {
        inputRef.current.value = mask_utils_1.maskValue(inputRef.current.value, props);
        clearError();
    }, inputRef);
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            path: 'value',
            setValue: function (_ref, value) {
                inputRef.current.value = mask_utils_1.maskValue(value, props);
            },
            getValue: function (_ref) {
                if (number)
                    return utils_service_1.OnlyNumbers(_ref.value);
                if (CEP)
                    return utils_service_1.OnlyNumbers(_ref.value);
                return _ref.value;
            },
        });
    }, [fieldName, registerField]);
    return (react_1.default.createElement(Input_style_1.default, { themeInput: themeInput },
        react_1.default.createElement("div", { className: "input-title " + (focus && 'focus') }, title),
        react_1.default.createElement(Component, __assign({ inputRef: inputRef, textRight: textRight, error: error, prefix: prefix, textCenter: textCenter, loading: loading, suffix: suffix, defaultValueInput: defaultValueInput }, rest))));
});
//# sourceMappingURL=Input.js.map