import * as tslib_1 from "tslib";
import React, { useState, useEffect, useCallback } from 'react';
import { Form, Select, Input } from 'antd';
import { PrototypeCheck } from '../utils';
import { debounce } from 'lodash';
var FormSelect = function (_a) {
    var label = _a.label, labelCol = _a.labelCol, wrapperCol = _a.wrapperCol, allowClear = _a.allowClear, fetchApi = _a.fetchApi, placeholder = _a.placeholder, showSearch = _a.showSearch, onSelect = _a.onSelect, onChange = _a.onChange, onDeselect = _a.onDeselect, mode = _a.mode, disabled = _a.disabled, popupContainerId = _a.popupContainerId, className = _a.className, form = _a.form, field = _a.field, changeConfirm = _a.changeConfirm, isFormData = _a.isFormData, formDataKey = _a.formDataKey, apiParamsCb = _a.apiParamsCb, options = _a.options, _b = _a.mountFetchApi, mountFetchApi = _b === void 0 ? true : _b, isHugeOptions = _a.isHugeOptions, labelInValue = _a.labelInValue, normalizeGetValue = _a.normalizeGetValue, selectProps = _a.selectProps, restProps = tslib_1.__rest(_a, ["label", "labelCol", "wrapperCol", "allowClear", "fetchApi", "placeholder", "showSearch", "onSelect", "onChange", "onDeselect", "mode", "disabled", "popupContainerId", "className", "form", "field", "changeConfirm", "isFormData", "formDataKey", "apiParamsCb", "options", "mountFetchApi", "isHugeOptions", "labelInValue", "normalizeGetValue", "selectProps"]);
    var _c = form, getFieldDecorator = _c.getFieldDecorator, getFieldValue = _c.getFieldValue, setFieldsValue = _c.setFieldsValue;
    var initialValue = restProps.initialValue, required = restProps.required, validator = restProps.validator;
    var _d = useState([]), localOptions = _d[0], setLocalOptions = _d[1];
    var setOptionsMemoized = useCallback(function (options, value) {
        function setOptions(options, value) {
            var localOptions;
            if (showSearch && value) {
                localOptions = options.reduce(function (prev, curr) {
                    curr.value.includes(value) && prev.push(setOption(curr));
                    return prev;
                }, []);
            }
            else {
                localOptions = isHugeOptions ? [] : options.map(setOption);
            }
            setLocalOptions(localOptions);
        }
        setOptions(options, value);
    }, [showSearch, isHugeOptions, setLocalOptions]);
    useEffect(function () {
        options && setOptionsMemoized(options);
    }, [options, setOptionsMemoized]);
    var fetchApiDataMemoized = useCallback(function (value) {
        function fetchApiData(value) {
            if (!fetchApi) {
                if (!options)
                    throw new Error('options 不能为空');
                setOptionsMemoized(options, value);
                return;
            }
            var apiParams;
            if (apiParamsCb) {
                if (isFormData) {
                    apiParams = new FormData();
                    apiParams.append(formDataKey || 'data', JSON.stringify(apiParamsCb(value)));
                }
                else {
                    apiParams = apiParamsCb(value);
                }
            }
            fetchApi(apiParams || value).then(function (datas) {
                setOptionsMemoized(datas, value);
            });
        }
        fetchApiData(value);
    }, [fetchApi, options, apiParamsCb, isFormData, formDataKey, setOptionsMemoized]);
    useEffect(function () {
        mountFetchApi && fetchApiDataMemoized();
    }, [fetchApiDataMemoized, mountFetchApi]);
    function setOption(v) {
        return (React.createElement(Select.Option, { key: v.key, value: v.key.toString(), "data-original": v }, v.value || v.key));
    }
    var handleSelect = function (value) {
        var _a;
        if (!PrototypeCheck.isString(value))
            value = value.key;
        var _b = form, setFieldsValue = _b.setFieldsValue, getFieldValue = _b.getFieldValue;
        if (!changeConfirm) {
            setFieldsValue((_a = {}, _a[field] = value, _a));
            return;
        }
        var oldValue = getFieldValue(field);
        var setValue = function (value) {
            var _a;
            return setFieldsValue((_a = {}, _a[field] = value, _a));
        };
        changeConfirm({
            onCancel: function () { return setValue(oldValue); },
            value: value,
            oldValue: oldValue,
        });
    };
    function filterOption(inputValue, option) {
        if (!inputValue)
            return false;
        var props = option.props;
        return props.children.includes(inputValue);
    }
    var onSearch = debounce(function (value) {
        if (value) {
            !mountFetchApi && fetchApiDataMemoized(value);
        }
    }, 500);
    return (React.createElement(Form.Item, tslib_1.__assign({}, {
        label: label,
        labelCol: labelCol,
        wrapperCol: wrapperCol,
    }, { className: className }),
        getFieldDecorator(field, {
            initialValue: initialValue,
            rules: [
                { required: required, message: "\u8BF7\u9009\u62E9" + label },
                {
                    validator: function (rule, value, callback) {
                        return validator ? validator(rule, value, callback) : callback();
                    },
                },
            ],
        })(React.createElement(Input, { style: { display: 'none' } })),
        restProps.readOnly ? (restProps.render ? (restProps.render(getFieldValue(field), form)) : (React.createElement("span", null, getFieldValue(field)))) : (React.createElement(Select, tslib_1.__assign({}, selectProps, { labelInValue: labelInValue, allowClear: allowClear, style: { width: '100%' }, showSearch: showSearch, placeholder: placeholder || "\u8BF7\u9009\u62E9" + label, onSearch: onSearch, onSelect: function (value, option) {
                onSelect && onSelect(value, option, form);
                handleSelect(value);
            }, onChange: function (value, option) {
                var _a;
                onChange && onChange(value, option, form);
                if (!value) {
                    setFieldsValue((_a = {},
                        _a[field] = PrototypeCheck.isNumber(value) ? value.toString() : value,
                        _a));
                }
            }, filterOption: filterOption, optionFilterProp: "children", onDeselect: function (value) { return onDeselect && onDeselect(value); } }, tslib_1.__assign({ mode: mode,
            disabled: disabled }, (popupContainerId && {
            getPopupContainer: function () { return document.getElementById(popupContainerId); },
        })), { value: normalizeGetValue
                ? normalizeGetValue(getFieldValue(field))
                : getFieldValue(field) }), localOptions))));
};
export default FormSelect;
//# sourceMappingURL=index.js.map