"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    CollectionSelect: ()=>CollectionSelect,
    DataSourceCollectionCascader: ()=>DataSourceCollectionCascader,
    DataSourceSelect: ()=>DataSourceSelect
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const action_hooks_js_namespaceObject = require("../../../collection-manager/action-hooks.js");
const hooks_index_js_namespaceObject = require("../../../collection-manager/hooks/index.js");
const external_data_source_index_js_namespaceObject = require("../../../data-source/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const context_js_namespaceObject = require("../filter/context.js");
function useCollectionOptions(param) {
    let { filter, isTableOid } = param;
    var _ctx_field;
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const field = (0, schema_namespaceObject.useField)();
    const ctx = (0, external_react_namespaceObject.useContext)(context_js_namespaceObject.FilterContext);
    const collection = (0, hooks_index_js_namespaceObject.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, action_hooks_js_namespaceObject.useSelfAndChildrenCollections)(targetCollection);
    const { collections = [] } = (0, hooks_index_js_namespaceObject.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, external_react_namespaceObject.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, schema_namespaceObject.connect)((props)=>{
    const { filter, ...others } = props;
    const options = useCollectionOptions(props);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const optionFilter = (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
        role: "button",
        "data-testid": "select-collection",
        placeholder: t('Select collection'),
        popupMatchSelectWidth: false,
        ...others,
        showSearch: true,
        filterOption: optionFilter,
        options: options
    });
}, (0, schema_namespaceObject.mapReadPretty)((0, schema_namespaceObject.observer)((props)=>{
    const { mode } = props;
    const compile = (0, external_hooks_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
        children: options.map((option)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                color: option.color,
                children: compile(option.label)
            }, option.value))
    });
}, {
    displayName: 'CollectionSelectObserver'
})));
function useDataSourceOptions(param) {
    let { filter } = param;
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const dataSourceManager = (0, external_data_source_index_js_namespaceObject.useDataSourceManager)();
    const dataSources = dataSourceManager.getDataSources();
    return (0, external_react_namespaceObject.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, schema_namespaceObject.connect)((props)=>{
    const { filter, ...others } = props;
    const options = useDataSourceOptions(props);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const optionFilter = (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
        role: "button",
        "data-testid": "select-datasource",
        placeholder: t('Select data source'),
        popupMatchSelectWidth: false,
        ...others,
        showSearch: true,
        filterOption: optionFilter,
        options: options
    });
});
const DataSourceCollectionCascader = (0, schema_namespaceObject.connect)((props)=>{
    const dataSourceManager = (0, external_data_source_index_js_namespaceObject.useDataSourceManager)();
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const { value, onChange, dataSourceFilter, collectionFilter, ...others } = props;
    const [dataSourceName, collectionName] = (0, index_js_namespaceObject.parseCollectionName)(value);
    const path = [
        dataSourceName,
        collectionName
    ].filter(Boolean);
    const dataSources = dataSourceManager.getDataSources();
    const options = (0, external_react_namespaceObject.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, external_react_namespaceObject.useCallback)((value)=>{
        if (!value) return onChange(value);
        onChange('main' === value[0] ? value[1] : value.join(':'));
    }, [
        onChange
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Cascader, {
        showSearch: true,
        ...others,
        options: options,
        value: path,
        onChange: handleChange
    });
});
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
});
