"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    RemoteSelect: ()=>RemoteSelect,
    default: ()=>RemoteSelect_rslib_entry_
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_dayjs_namespaceObject = require("dayjs");
var external_dayjs_default = /*#__PURE__*/ __webpack_require__.n(external_dayjs_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
const index_js_namespaceObject = require("../../../api-client/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const DataSourceProvider_js_namespaceObject = require("../../../data-source/data-source/DataSourceProvider.js");
const utils_js_namespaceObject = require("../../../data-source/utils.js");
const external_filter_provider_utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_select_index_js_namespaceObject = require("../select/index.js");
const external_ReadPretty_js_namespaceObject = require("./ReadPretty.js");
const EMPTY = 'N/A';
const InternalRemoteSelect = (0, schema_namespaceObject.connect)((props)=>{
    var _fieldSchema_xcomponentprops, _service_params;
    const { fieldNames = {}, service = {}, wait = 300, value, defaultValue, objectValue, manual = true, mapOptions, targetField: _targetField, CustomDropdownRender, optionFilter, ...others } = props;
    const dataSource = (0, DataSourceProvider_js_namespaceObject.useDataSourceKey)();
    const headers = (0, utils_js_namespaceObject.useDataSourceHeaders)(dataSource);
    const [open, setOpen] = (0, external_react_namespaceObject.useState)(false);
    const firstRun = (0, external_react_namespaceObject.useRef)(false);
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const isQuickAdd = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.addMode) === 'quickAdd';
    const { getField } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const searchData = (0, external_react_namespaceObject.useRef)(null);
    const { getCollectionJoinField, getInterface } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const colletionFieldName = fieldSchema['x-collection-field'] || fieldSchema.name;
    const collectionField = getField(colletionFieldName) || getCollectionJoinField(colletionFieldName);
    const targetField = _targetField || (null == collectionField ? void 0 : collectionField.target) && (null == fieldNames ? void 0 : fieldNames.label) && getCollectionJoinField("".concat(collectionField.target, ".").concat(fieldNames.label));
    const operator = (0, external_react_namespaceObject.useMemo)(()=>{
        if (null == targetField ? void 0 : targetField.interface) {
            var _getInterface_filterable_operators_, _getInterface_filterable_operators, _getInterface_filterable, _getInterface;
            return (null === (_getInterface = getInterface(targetField.interface)) || void 0 === _getInterface ? void 0 : null === (_getInterface_filterable = _getInterface.filterable) || void 0 === _getInterface_filterable ? void 0 : null === (_getInterface_filterable_operators = _getInterface_filterable.operators) || void 0 === _getInterface_filterable_operators ? void 0 : null === (_getInterface_filterable_operators_ = _getInterface_filterable_operators[0]) || void 0 === _getInterface_filterable_operators_ ? void 0 : _getInterface_filterable_operators_.value) || '$includes';
        }
        return '$includes';
    }, [
        targetField
    ]);
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const mapOptionsToTags = (0, external_react_namespaceObject.useCallback)((options)=>{
        try {
            return options.filter((v)=>[
                    'number',
                    'string'
                ].includes(typeof v[fieldNames.value])).map((option)=>{
                var _targetField_uiSchema;
                let label = compile(option[fieldNames.label]);
                if (null == targetField ? void 0 : null === (_targetField_uiSchema = targetField.uiSchema) || void 0 === _targetField_uiSchema ? void 0 : _targetField_uiSchema.enum) {
                    if (Array.isArray(label)) label = label.map((item, index)=>{
                        const option = targetField.uiSchema.enum.find((i)=>i.value === item);
                        if (option) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            role: "button",
                            color: option.color,
                            style: {
                                marginRight: 3
                            },
                            children: (null == option ? void 0 : option.label) || item
                        }, index);
                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            role: "button",
                            children: item
                        }, item);
                    }).reverse();
                    else {
                        const item = targetField.uiSchema.enum.find((i)=>i.value === label);
                        if (item) label = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            role: "button",
                            color: item.color,
                            children: item.label
                        });
                    }
                }
                if ((null == targetField ? void 0 : targetField.type) === 'date') label = external_dayjs_default()(label).format('YYYY-MM-DD');
                if (mapOptions) return mapOptions({
                    [fieldNames.label]: label || EMPTY,
                    [fieldNames.value]: option[fieldNames.value]
                });
                return {
                    ...option,
                    [fieldNames.label]: label || EMPTY,
                    [fieldNames.value]: option[fieldNames.value]
                };
            }).filter(Boolean);
        } catch (err) {
            console.error(err);
            return options;
        }
    }, [
        null == targetField ? void 0 : targetField.uiSchema,
        fieldNames
    ]);
    const params = {
        ...null == service ? void 0 : service.params,
        filter: null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter
    };
    if (fieldNames['formula']) params['paginate'] = false;
    else params['pageSize'] = 200;
    const { data, run, loading } = (0, index_js_namespaceObject.useRequest)({
        action: 'list',
        ...service,
        headers,
        params
    }, {
        manual,
        debounceWait: wait
    });
    const runDep = (0, external_react_namespaceObject.useMemo)(()=>JSON.stringify({
            service,
            fieldNames
        }), [
        service,
        fieldNames
    ]);
    const CustomRenderCom = (0, external_react_namespaceObject.useCallback)(()=>{
        if (searchData.current && CustomDropdownRender) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CustomDropdownRender, {
            search: searchData.current,
            callBack: ()=>{
                searchData.current = null;
                setOpen(false);
            }
        });
        return null;
    }, [
        searchData.current
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (firstRun.current) run();
    }, [
        runDep
    ]);
    const onSearch = async (search)=>{
        var _service_params;
        run({
            filter: (0, external_filter_provider_utils_js_namespaceObject.mergeFilter)([
                search ? {
                    [fieldNames.label]: {
                        [operator]: search
                    }
                } : {},
                null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter
            ])
        });
        searchData.current = search;
    };
    const options = (0, external_react_namespaceObject.useMemo)(()=>{
        var _data_data;
        const v = value || defaultValue;
        if (!(null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.length)) return null != v ? Array.isArray(v) ? v : [
            v
        ] : [];
        const valueOptions = null != v && (Array.isArray(v) ? v : [
            {
                ...v,
                [fieldNames.value]: v[fieldNames.value] || v
            }
        ]) || [];
        const filtered = 'function' == typeof optionFilter ? data.data.filter(optionFilter) : data.data;
        return (0, external_lodash_namespaceObject.uniqBy)(filtered.concat(null != valueOptions ? valueOptions : []), fieldNames.value);
    }, [
        value,
        defaultValue,
        null == data ? void 0 : data.data,
        fieldNames.value,
        optionFilter
    ]);
    const onDropdownVisibleChange = (visible)=>{
        setOpen(visible);
        searchData.current = null;
        if (visible) run();
        firstRun.current = true;
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_select_index_js_namespaceObject.Select, {
        open: open,
        popupMatchSelectWidth: false,
        autoClearSearchValue: true,
        filterOption: false,
        filterSort: null,
        fieldNames: fieldNames,
        onSearch: onSearch,
        onDropdownVisibleChange: onDropdownVisibleChange,
        objectValue: objectValue,
        value: value,
        defaultValue: defaultValue,
        ...others,
        loading: !data || loading,
        options: mapOptionsToTags(options),
        rawOptions: options,
        dropdownRender: (menu)=>{
            const isFullMatch = options.some((v)=>v[fieldNames.label] === searchData.current);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
                children: isQuickAdd ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        !((null == data ? void 0 : data.data.length) === 0 && (null == searchData ? void 0 : searchData.current)) && menu,
                        (null == data ? void 0 : data.data.length) > 0 && (null == searchData ? void 0 : searchData.current) && !isFullMatch && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Divider, {
                            style: {
                                margin: 0
                            }
                        }),
                        !isFullMatch && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CustomRenderCom, {})
                    ]
                }) : menu
            });
        }
    });
}, (0, schema_namespaceObject.mapProps)({
    dataSource: 'options'
}, (props, field)=>{
    var _fieldSchema_xcomponentprops;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    return {
        ...props,
        fieldNames: {
            ...external_select_index_js_namespaceObject.defaultFieldNames,
            ...props.fieldNames,
            ...field.componentProps.fieldNames,
            ...null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.fieldNames
        },
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.LoadingOutlined, {}) : props.suffixIcon
    };
}), (0, schema_namespaceObject.mapReadPretty)(external_ReadPretty_js_namespaceObject.ReadPretty));
const RemoteSelect = InternalRemoteSelect;
RemoteSelect.ReadPretty = external_ReadPretty_js_namespaceObject.ReadPretty;
const RemoteSelect_rslib_entry_ = RemoteSelect;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
