"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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectCoreWithoutUnform = exports.SelectCoreWithUnform = exports.SelectCore = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var _ = __importStar(require("lodash"));
var ReactNative = typeof navigator !== 'undefined' && (navigator === null || navigator === void 0 ? void 0 : navigator.product) === 'ReactNative';
exports.SelectCore = react_1.default.forwardRef(function SelectCore(props, inputRef) {
    var _a;
    if (props.name) {
        return react_1.default.createElement(exports.SelectCoreWithUnform, __assign({}, props, { ref: inputRef }));
    }
    return react_1.default.createElement(exports.SelectCoreWithoutUnform, __assign({}, props, { ref: inputRef, value: (_a = props.value) !== null && _a !== void 0 ? _a : null }));
});
exports.SelectCoreWithUnform = react_1.default.forwardRef(function SelectCoreWithUnform(props, inputRef) {
    var _a, _b;
    var ref = react_1.useRef();
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var _c = core_1.useField(props.name), fieldName = _c.fieldName, registerField = _c.registerField, _d = _c.defaultValue, defaultValue = _d === void 0 ? null : _d, error = _c.error, clearError = _c.clearError;
    var inputValueRef = react_1.useRef(ReactNative
        ?
            (_b = (_a = props.options.find(function (i) { return _.isEqual(i.value, defaultValue); })) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null
        : JSON.stringify(defaultValue));
    var _e = react_1.useState(ReactNative ? defaultValue : undefined), value = _e[0], setValue = _e[1];
    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 = JSON.stringify(v);
                if (ReactNative) {
                    _ref.current = v;
                    setValue(v);
                }
                else {
                    ref.current.value = JSON.stringify(v);
                }
            },
            getValue: function (ref) {
                if (ReactNative) {
                    return ref.current;
                }
                return JSON.parse(ref.current);
            },
        });
    }, [fieldName, registerField, props.options]);
    var onChange = react_1.useCallback(function onSelectItem(newValue) {
        if (error)
            clearError();
        inputValueRef.current = JSON.stringify(newValue);
        if (ReactNative) {
            setValue(newValue);
        }
        if (props.onChange)
            props.onChange(newValue);
    }, [error]);
    return (react_1.default.createElement(exports.SelectCoreWithoutUnform, __assign({}, props, { ref: ref, onChange: onChange, value: value, error: error, defaultValue: defaultValue })));
});
exports.SelectCoreWithoutUnform = react_1.default.forwardRef(function SelectCoreWithoutUnform(props, inputRef) {
    var ref = react_1.useRef(null);
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var PureComponent = react_1.useMemo(function () { return props.as; }, [props.as]);
    return react_1.default.createElement(PureComponent, __assign({}, props, { ref: ref }));
});
//# sourceMappingURL=SelectCore.js.map