"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RadioCoreWithoutUnform = exports.RadioCoreWithUnform = exports.RadioCore = void 0;
var react_1 = __importStar(require("react"));
var unform_core_1 = require("@devesharp/unform-core");
var isEqual_1 = __importDefault(require("lodash/isEqual"));
var ReactNative = typeof navigator !== 'undefined' && (navigator === null || navigator === void 0 ? void 0 : navigator.product) === 'ReactNative';
exports.RadioCore = react_1.default.forwardRef(function RadioCore(props, inputRef) {
    var _a;
    if (props.name) {
        return react_1.default.createElement(exports.RadioCoreWithUnform, __assign({}, props, { ref: inputRef }));
    }
    return react_1.default.createElement(exports.RadioCoreWithoutUnform, __assign({}, props, { ref: inputRef, value: (_a = props.value) !== null && _a !== void 0 ? _a : null }));
});
exports.RadioCoreWithUnform = react_1.default.forwardRef(function RadioCoreWithUnform(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;
    var inputValueRef = (0, react_1.useRef)(defaultValue);
    var _c = (0, react_1.useState)(ReactNative ? defaultValue : undefined), value = _c[0], setValue = _c[1];
    (0, react_1.useEffect)(function () {
        registerField({
            name: fieldName,
            ref: inputValueRef,
            path: 'current',
            clearValue: function (_ref) {
                _ref.current = null;
                if (ReactNative) {
                    setValue(null);
                }
            },
            setValue: function (_ref, v) {
                _ref.current = v;
                var index = props.options.findIndex(function (i) { return (0, isEqual_1.default)(i.value, v); });
                if (ReactNative) {
                    setValue(v);
                }
                else if (index != -1) {
                    ref.current[index].checked = true;
                }
            },
            getValue: function (_ref) {
                return _ref.current;
            },
        });
    }, [fieldName, registerField, props.options]);
    var onChange = (0, react_1.useCallback)(function onSelectItem(newValue) {
        if (error)
            clearError();
        inputValueRef.current = newValue;
        if (ReactNative) {
            setValue(newValue);
        }
        if (props.onChange)
            props.onChange(newValue);
    }, [error]);
    return (react_1.default.createElement(exports.RadioCoreWithoutUnform, __assign({}, props, { ref: ref, onChange: onChange, value: value, error: error, defaultValue: defaultValue })));
});
exports.RadioCoreWithoutUnform = react_1.default.forwardRef(function RadioCoreWithoutUnform(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; }, [props.as]);
    return react_1.default.createElement(PureComponent, __assign({}, props, { ref: ref }));
});
//# sourceMappingURL=RadioCore.js.map