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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_action_hooks_mjs__ from "../../../collection-manager/action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_hooks_index_mjs__ from "../../../collection-manager/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_context_mjs__ from "../filter/context.mjs";
function useCollectionOptions(param) {
    let { filter, isTableOid } = param;
    var _ctx_field;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__filter_context_mjs__.FilterContext);
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_hooks_index_mjs__.useCollection_deprecated)();
    const targetCollection = isTableOid && ((null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : _ctx_field.collectionName) || (null == ctx ? void 0 : ctx.collectionName) || collection.name);
    const inheritCollections = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_action_hooks_mjs__.useSelfAndChildrenCollections)(targetCollection);
    const { collections = [] } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_hooks_index_mjs__.useCollectionManager_deprecated)();
    const currentCollections = (null == field ? void 0 : field.dataSource) ? collections.filter((v)=>(null == field ? void 0 : field.dataSource.find((i)=>i.value === v.name)) || (null == field ? void 0 : field.dataSource.includes(v.name))) : collections;
    const filtered = 'function' == typeof filter ? (inheritCollections || currentCollections).filter(filter) : inheritCollections || currentCollections;
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>filtered.filter((item)=>!item.hidden).map((item)=>{
            var _item_category;
            return {
                label: compile(item.title || item.label),
                value: item.name || item.value,
                color: null === (_item_category = item.category) || void 0 === _item_category ? void 0 : _item_category.color
            };
        }), [
        filtered
    ]);
}
const CollectionSelect = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { filter, ...others } = props;
    const options = useCollectionOptions(props);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const optionFilter = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((input, option)=>{
        var _option_label_toLowerCase, _option_value_toString_toLowerCase;
        return (null !== (_option_label_toLowerCase = null == option ? void 0 : option.label.toLowerCase()) && void 0 !== _option_label_toLowerCase ? _option_label_toLowerCase : '').includes(input.toLocaleLowerCase()) || (null !== (_option_value_toString_toLowerCase = null == option ? void 0 : option.value.toString().toLowerCase()) && void 0 !== _option_value_toString_toLowerCase ? _option_value_toString_toLowerCase : '').includes(input.toLocaleLowerCase());
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
        role: "button",
        "data-testid": "select-collection",
        placeholder: t('Select collection'),
        popupMatchSelectWidth: false,
        ...others,
        showSearch: true,
        filterOption: optionFilter,
        options: options
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { mode } = props;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const options = useCollectionOptions(props).filter((option)=>{
        if ('multiple' === mode) {
            var _props_value;
            return (null !== (_props_value = props.value) && void 0 !== _props_value ? _props_value : []).includes(option.value);
        }
        return props.value === option.value;
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: options.map((option)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                color: option.color,
                children: compile(option.label)
            }, option.value))
    });
}, {
    displayName: 'CollectionSelectObserver'
})));
function useDataSourceOptions(param) {
    let { filter } = param;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const dataSourceManager = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useDataSourceManager)();
    const dataSources = dataSourceManager.getDataSources();
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>[
            {
                label: compile('Main'),
                value: 'main'
            },
            ...('function' == typeof filter ? dataSources.filter(filter) : dataSources).map((item)=>({
                    label: item.displayName,
                    value: item.key
                }))
        ], [
        dataSources,
        filter
    ]);
}
const DataSourceSelect = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { filter, ...others } = props;
    const options = useDataSourceOptions(props);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const optionFilter = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((input, option)=>{
        var _option_label_toLowerCase, _option_value_toString_toLowerCase;
        return (null !== (_option_label_toLowerCase = null == option ? void 0 : option.label.toLowerCase()) && void 0 !== _option_label_toLowerCase ? _option_label_toLowerCase : '').includes(input.toLocaleLowerCase()) || (null !== (_option_value_toString_toLowerCase = null == option ? void 0 : option.value.toString().toLowerCase()) && void 0 !== _option_value_toString_toLowerCase ? _option_value_toString_toLowerCase : '').includes(input.toLocaleLowerCase());
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
        role: "button",
        "data-testid": "select-datasource",
        placeholder: t('Select data source'),
        popupMatchSelectWidth: false,
        ...others,
        showSearch: true,
        filterOption: optionFilter,
        options: options
    });
});
const DataSourceCollectionCascader = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const dataSourceManager = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useDataSourceManager)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { value, onChange, dataSourceFilter, collectionFilter, ...others } = props;
    const [dataSourceName, collectionName] = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.parseCollectionName)(value);
    const path = [
        dataSourceName,
        collectionName
    ].filter(Boolean);
    const dataSources = dataSourceManager.getDataSources();
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(dataSourceFilter ? dataSources.filter(dataSourceFilter) : dataSources).map((dataSource)=>({
                label: compile(dataSource.displayName),
                value: dataSource.key,
                children: dataSource.collectionManager.collectionInstancesArr.filter(null != collectionFilter ? collectionFilter : (collection)=>!collection.hidden).map((collection)=>({
                        label: compile(collection.title),
                        value: collection.name
                    }))
            })), [
        dataSources,
        dataSourceFilter,
        collectionFilter
    ]);
    const handleChange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((value)=>{
        if (!value) return onChange(value);
        onChange('main' === value[0] ? value[1] : value.join(':'));
    }, [
        onChange
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Cascader, {
        showSearch: true,
        ...others,
        options: options,
        value: path,
        onChange: handleChange
    });
});
export { CollectionSelect, DataSourceCollectionCascader, DataSourceSelect };
