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