import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__input_index_mjs__ from "../input/index.mjs";
const AutoComplete = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { fieldNames, params: fieldFilter } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const targetKey = null == fieldNames ? void 0 : fieldNames.value;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const [defultOptions, setDefultOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [options, setOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const getNoDuplicateTextOptions = (rawOptions)=>{
        const targetOptions = rawOptions.filter((option, index, self)=>self.findIndex((item)=>item[targetKey] === option[targetKey]) === index);
        return targetOptions;
    };
    (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useAsyncEffect)(async ()=>{
        const { data: { data: rawOptions } } = await api.request({
            url: fieldSchema['collectionName'] + ':list',
            params: {
                paginate: false,
                filter: fieldFilter ? {
                    ...fieldFilter.filter
                } : {}
            }
        });
        const targetOptions = getNoDuplicateTextOptions(rawOptions);
        setDefultOptions(targetOptions);
        setOptions(targetOptions);
    }, [
        null == fieldFilter ? void 0 : fieldFilter.filter,
        fieldSchema['collectionName']
    ]);
    const onSearch = (value)=>{
        value ? setOptions([
            {
                [targetKey]: value
            },
            ...defultOptions
        ]) : setOptions(defultOptions);
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.AutoComplete, {
        ...props,
        options: options,
        filterOption: (inputValue, option)=>{
            var _option_targetKey;
            return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.fuzzysearch)(inputValue || '', (null === (_option_targetKey = option[targetKey]) || void 0 === _option_targetKey ? void 0 : _option_targetKey.toString()) || '');
        },
        allowClear: true,
        onSearch: onSearch
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    dataSource: 'options'
}), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__input_index_mjs__.ReadPretty.Input));
export { AutoComplete };
