"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;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputCoreWithoutUnform = exports.InputCoreWithUnform = exports.InputCore = void 0;
var react_1 = __importStar(require("react"));
var Utils_1 = require("@devesharp/react-utils/lib/Utils");
var unform_core_1 = require("@devesharp/unform-core");
var mask_utils_1 = require("../MaskText/mask.utils");
exports.InputCore = react_1.default.forwardRef(function InputCore(props, inputRef) {
    var ref = (0, react_1.useRef)();
    (0, react_1.useImperativeHandle)(inputRef, function () { return ref.current; }, []);
    if (props.name) {
        return react_1.default.createElement(exports.InputCoreWithUnform, __assign({}, props, { ref: ref }));
    }
    return react_1.default.createElement(exports.InputCoreWithoutUnform, __assign({}, props, { ref: ref }));
});
exports.InputCoreWithUnform = react_1.default.forwardRef(function InputCoreWithUnform(props, inputRef) {
    var ref = (0, react_1.useRef)();
    (0, react_1.useImperativeHandle)(inputRef, function () { return ref.current; }, []);
    var _a = (0, unform_core_1.useField)(props.name), fieldName = _a.fieldName, registerField = _a.registerField, _b = _a.defaultValue, defaultValue = _b === void 0 ? null : _b, error = _a.error, clearError = _a.clearError;
    (0, react_1.useEffect)(function () {
        registerField({
            name: fieldName,
            ref: ref,
            path: 'current',
            clearValue: function (_ref) {
                var _a, _b;
                ref.current.value = '';
                if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.clear)
                    (_b = ref.current) === null || _b === void 0 ? void 0 : _b.clear();
                ref.current.dispatchEvent(new InputEvent('input', { bubbles: true }));
            },
            setValue: function (_ref, value) {
                ref.current.value = (0, mask_utils_1.maskValue)(value, props.mask, props);
                ref.current.dispatchEvent(new InputEvent('input', { bubbles: true }));
            },
            getValue: function (_ref) {
                var _a;
                if (props.mask == 'number') {
                    if (!ref.current.value || ref.current.value === '')
                        return '';
                    return (Number((0, Utils_1.OnlyNumbers)(ref.current.value)) / Math.pow(10, ((_a = props.decimals) !== null && _a !== void 0 ? _a : 2)));
                }
                if (props.mask == 'CEP' || props.mask == 'phone' || props.mask == 'CPF') {
                    return (0, Utils_1.OnlyNumbers)(ref.current.value);
                }
                return ref.current.value;
            },
        });
    }, [fieldName, registerField]);
    var onInput = (0, react_1.useCallback)(function onSelectItem(text) {
        if (error)
            clearError();
        if (props.onInput)
            props.onInput(text);
    }, [error, props.onInput]);
    return (react_1.default.createElement(exports.InputCoreWithoutUnform, __assign({}, props, { ref: ref, onInput: onInput, value: null, error: error, defaultValue: defaultValue })));
});
exports.InputCoreWithoutUnform = react_1.default.forwardRef(function InputCoreWithoutUnform(props, inputRef) {
    var ref = (0, react_1.useRef)(null);
    (0, react_1.useImperativeHandle)(inputRef, function () { return ref.current; }, []);
    var PureComponent = (0, react_1.useMemo)(function () { return props.as || 'input'; }, [props.as]);
    return react_1.default.createElement(PureComponent, __assign({}, props, { ref: ref }));
});
//# sourceMappingURL=InputCore.js.map