"use strict";
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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnformInput = void 0;
var react_1 = __importStar(require("react"));
var react_hooks_1 = require("@devesharp/react-hooks");
var core_1 = require("@unform/core");
exports.UnformInput = react_1.default.forwardRef(function UnformInput(props, inputRef) {
    var _a = 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;
    var ref = react_1.useRef({
        value: defaultValue !== null && defaultValue !== void 0 ? defaultValue : null,
    });
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    react_1.useEffect(function () {
        if (props.onError)
            props.onError(error);
    }, [error]);
    react_hooks_1.useDidUpdateEffect(function () {
        if (error) {
            clearError();
        }
    }, [props.value]);
    react_1.useEffect(function () {
        if (props.value !== undefined) {
            ref.current.value = props.value;
        }
    }, [props.value]);
    react_1.useEffect(function () {
        if (props.onChange)
            props.onChange(defaultValue);
    }, []);
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: ref,
            path: 'current',
            clearValue: function (_ref) {
                ref.current.value = null;
                if (props.onChange)
                    props.onChange(null);
            },
            setValue: function (_ref, value) {
                ref.current.value = value;
                if (props.onChange)
                    props.onChange(value);
            },
            getValue: function (_ref) {
                return ref.current.value;
            },
        });
    }, [fieldName, registerField]);
    return null;
});
//# sourceMappingURL=UnformInput.js.map