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__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__icon_index_mjs__ from "../../icon/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__ from "../../schema-templates/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
const MENU_ITEM_HEIGHT = 40;
const STEP = 15;
const SearchCollections = (param)=>{
    let { value: outValue, onChange } = param;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [value, setValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(outValue);
    const inputRef = __WEBPACK_EXTERNAL_MODULE_react__["default"].useRef(null);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        setValue(outValue);
    }, [
        outValue
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        inputRef.current.input.focus();
        const observer = new IntersectionObserver((entries)=>{
            if (entries[0].isIntersecting) inputRef.current.input.focus();
        });
        observer.observe(inputRef.current.input);
        return ()=>{
            observer.disconnect();
        };
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        style: {
            width: 210
        },
        onClick: (e)=>e.stopPropagation(),
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input, {
                ref: inputRef,
                allowClear: true,
                style: {
                    padding: '0 4px 6px',
                    boxShadow: 'none'
                },
                bordered: false,
                placeholder: t('Search and select collection'),
                value: value,
                onClick: (e)=>{
                    e.stopPropagation();
                },
                onChange: (e)=>{
                    onChange(e.target.value);
                    setValue(e.target.value);
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Divider, {
                style: {
                    margin: 0
                }
            })
        ]
    });
};
const LoadingItem = (param)=>{
    let { loadMore, maxHeight } = param;
    const spinRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const el = spinRef.current;
        if (!el) return;
        let container = el.parentElement;
        while(container && 'UL' !== container.tagName)container = container.parentElement;
        const checkLoadMore = function() {
            if (!container) return;
            if (Math.abs(container.scrollHeight - container.scrollTop - container.clientHeight) <= MENU_ITEM_HEIGHT) loadMore();
        };
        if (container) {
            container.style.height = "".concat(maxHeight - MENU_ITEM_HEIGHT, "px");
            container.style.maxHeight = 'inherit';
            container.style.overflowY = 'scroll';
            container.addEventListener('scroll', checkLoadMore);
        }
        return ()=>{
            if (container) {
                container.removeEventListener('scroll', checkLoadMore);
                delete container.style.height;
            }
        };
    }, [
        loadMore,
        maxHeight
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        ref: spinRef,
        onClick: (e)=>e.stopPropagation(),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {
            size: "small",
            style: {
                width: '100%'
            }
        })
    });
};
function useMenuSearch(param) {
    let { data, openKeys, showType, hideSearch } = param;
    const [searchValue, setSearchValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('');
    const [count, setCount] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(STEP);
    const isMuliSource = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>data.length > 1, [
        data
    ]);
    const openKey = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>isMuliSource ? null == openKeys ? void 0 : openKeys[1] : (null == openKeys ? void 0 : openKeys.length) > 0, [
        openKeys
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!openKey) setSearchValue('');
    }, [
        openKey
    ]);
    const currentItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _data_;
        if (isMuliSource) {
            var _data_find;
            if (!openKey) return [];
            return (null === (_data_find = data.find((item)=>(item.key || item.name) === openKey)) || void 0 === _data_find ? void 0 : _data_find.children) || [];
        }
        return (null === (_data_ = data[0]) || void 0 === _data_ ? void 0 : _data_.children) || [];
    }, [
        data,
        isMuliSource,
        openKey
    ]);
    const searchedItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (!searchValue) return currentItems;
        const lowerSearchValue = searchValue.toLocaleLowerCase();
        return currentItems.filter((item)=>(item.label || item.title) && String(item.label || item.title).toLocaleLowerCase().includes(lowerSearchValue));
    }, [
        searchValue,
        currentItems
    ]);
    const shouldLoadMore = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>searchedItems.length > count, [
        count,
        searchedItems
    ]);
    const limitedSearchedItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>searchedItems.slice(0, count), [
        searchedItems,
        count
    ]);
    const resultItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const res = [];
        if (!hideSearch) res.push(Object.assign({
            key: 'search',
            label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SearchCollections, {
                value: searchValue,
                onChange: (val)=>{
                    setCount(STEP);
                    setSearchValue(val);
                }
            }),
            onClick (param) {
                let { domEvent } = param;
                domEvent.stopPropagation();
            }
        }, showType ? {
            isMenuType: true
        } : {}));
        if (limitedSearchedItems.length > 0) {
            res.push(...limitedSearchedItems);
            if (shouldLoadMore) res.push(Object.assign({
                key: 'load-more',
                label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(LoadingItem, {
                    maxHeight: STEP * MENU_ITEM_HEIGHT,
                    loadMore: ()=>{
                        setCount((count)=>count + STEP);
                    }
                })
            }, showType ? {
                isMenuType: true
            } : {}));
        } else res.push(Object.assign({
            key: 'empty',
            style: {
                height: 150
            },
            label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                onClick: (e)=>e.stopPropagation(),
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Empty, {
                    image: __WEBPACK_EXTERNAL_MODULE_antd__.Empty.PRESENTED_IMAGE_SIMPLE
                })
            })
        }, showType ? {
            isMenuType: true
        } : {}));
        return res;
    }, [
        hideSearch,
        limitedSearchedItems,
        searchValue,
        shouldLoadMore,
        showType
    ]);
    const res = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (!isMuliSource) return resultItems;
        return data.map((item)=>{
            if (openKey && item.key === openKey) return {
                ...item,
                children: resultItems
            };
            return {
                ...item,
                children: []
            };
        });
    }, [
        data,
        isMuliSource,
        openKey,
        resultItems
    ]);
    return res;
}
const DataBlockInitializer = (props)=>{
    var _childItems__children;
    const { templateWrap, onCreateBlockSchema, componentType, icon = __WEBPACK_EXTERNAL_MODULE__ant_design_icons__.TableOutlined, name, title, filter, onlyCurrentDataSource, hideSearch, showAssociationFields, hideChildrenIfSingleCollection, filterDataSource, items: itemsFromProps, fromOthersInPopup, hideOtherRecordsInPopup } = props;
    const { insert, setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { getTemplateSchemaByMode } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const onClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async (param)=>{
        let { item } = param;
        if (item.template) {
            const s = await getTemplateSchemaByMode(item);
            templateWrap ? insert(templateWrap(s, {
                item,
                fromOthersInPopup
            })) : insert(s);
        } else if (onCreateBlockSchema) onCreateBlockSchema({
            item,
            fromOthersInPopup
        });
        setVisible(false);
    }, [
        fromOthersInPopup,
        getTemplateSchemaByMode,
        insert,
        onCreateBlockSchema,
        setVisible,
        templateWrap
    ]);
    const items = itemsFromProps || (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useCollectionDataSourceItems)({
        componentName: componentType,
        filter,
        filterDataSource,
        onlyCurrentDataSource,
        showAssociationFields,
        dataBlockInitializerProps: props,
        hideOtherRecordsInPopup
    });
    const getMenuItems = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useGetSchemaInitializerMenuItems)(onClick);
    const childItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getMenuItems(items, name), [
        getMenuItems,
        items,
        name
    ]);
    const [openMenuKeys, setOpenMenuKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const searchedChildren = useMenuSearch({
        data: childItems,
        openKeys: openMenuKeys,
        hideSearch
    });
    const compiledMenuItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        let children = searchedChildren.filter((item)=>'search' !== item.key && 'empty' !== item.key);
        children = hideChildrenIfSingleCollection && 1 === children.length ? children[0].children : searchedChildren;
        return [
            {
                key: name,
                label: compile(title),
                icon: 'string' == typeof icon ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__icon_index_mjs__.Icon, {
                    type: icon
                }) : icon,
                onClick: (info)=>{
                    if (info.key !== name) return;
                    onClick({
                        ...info,
                        item: props
                    });
                },
                children
            }
        ];
    }, [
        searchedChildren,
        hideChildrenIfSingleCollection,
        name,
        compile,
        title,
        icon,
        onClick,
        props
    ]);
    if (childItems.length > 1 || 1 === childItems.length && (null === (_childItems__children = childItems[0].children) || void 0 === _childItems__children ? void 0 : _childItems__children.length) > 0) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerMenu, {
        onOpenChange: (keys)=>{
            setOpenMenuKeys(keys);
        },
        items: compiledMenuItems
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerItem, {
        ...props,
        onClick: onClick
    });
};
export { DataBlockInitializer, SearchCollections, useMenuSearch };
