"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.SelectCoreWithoutUnform = exports.SelectCoreWithUnform = exports.SelectCore = exports.convertToString = exports.convertToJSON = void 0;
var react_1 = __importStar(require("react"));
var useDidUpdateEffect_1 = require("@devesharp/react-hooks/lib/useDidUpdateEffect");
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';
function isJson(str) {
    if (typeof str === 'number' || str == null)
        return false;
    try {
        JSON.parse(str);
    }
    catch (e) {
        return false;
    }
    return true;
}
function convertToJSON(value) {
    if (value === null)
        return '';
    if (typeof value === 'object')
        return JSON.stringify(value);
    return value;
}
exports.convertToJSON = convertToJSON;
function convertToString(value) {
    if (isJson(value))
        return JSON.parse(value);
    if (value === '')
        return null;
    return value;
}
exports.convertToString = convertToString;
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 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 getDefaultValue = function () {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        var value = ReactNative
            ?
                (_c = (_b = (_a = props.options) === null || _a === void 0 ? void 0 : _a.find(function (i) { return (0, isEqual_1.default)(i.value, defaultValue); })) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : null
            : convertToJSON(defaultValue);
        if (value == '' || value == null) {
            value = (_f = (_e = (_d = props.options) === null || _d === void 0 ? void 0 : _d.find(function (i) { return (0, isEqual_1.default)(i.value, value); })) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : value;
            if ((value == '' || value == null) && ((_g = props.options) === null || _g === void 0 ? void 0 : _g.length)) {
                value = (_j = (_h = props.options[0]) === null || _h === void 0 ? void 0 : _h.value) !== null && _j !== void 0 ? _j : value;
            }
        }
        return value;
    };
    var defaultOption = (0, react_1.useMemo)(getDefaultValue, []);
    var inputValueRef = (0, react_1.useRef)(defaultOption);
    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 = '';
                if (ReactNative) {
                    setValue(null);
                }
            },
            setValue: function (_ref, v) {
                _ref.current = convertToJSON(v);
                if (ReactNative) {
                    _ref.current = v;
                    setValue(v);
                }
                else if (v == undefined) {
                    ref.current.value = convertToJSON(null);
                }
                else {
                    ref.current.value = convertToJSON(v);
                }
            },
            getValue: function (_ref) {
                if (ReactNative) {
                    return _ref.current;
                }
                return convertToString(_ref.current);
            },
        });
    }, [fieldName, registerField, props.options]);
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        var v = inputValueRef.current;
        if (v) {
            ref.current.value = convertToJSON(v);
        }
        if (v == '' || v == undefined || v == null) {
            ref.current.value = getDefaultValue();
            inputValueRef.current = getDefaultValue();
        }
    }, [props.options]);
    var onChange = (0, react_1.useCallback)(function onSelectItem(newValue) {
        if (error)
            clearError();
        inputValueRef.current = convertToJSON(newValue);
        if (ReactNative) {
            setValue(newValue);
        }
        if (props.onChange)
            props.onChange(newValue);
    }, [error, props.onChange]);
    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 = (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=SelectCore.js.map