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__data_source_DataSourceProvider_mjs__ from "../data-source/DataSourceProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ExtendCollectionsProvider_mjs__ from "./ExtendCollectionsProvider.mjs";
const CollectionManagerContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
CollectionManagerContext.displayName = 'CollectionManagerContext';
const CollectionManagerProviderInner = (param)=>{
    let { instance, children } = param;
    const dataSource = (0, __WEBPACK_EXTERNAL_MODULE__data_source_DataSourceProvider_mjs__.useDataSource)();
    const extendCollections = (0, __WEBPACK_EXTERNAL_MODULE__ExtendCollectionsProvider_mjs__.useExtendCollections)();
    const cm = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const res = instance || (null == dataSource ? void 0 : dataSource.collectionManager);
        if (null == extendCollections ? void 0 : extendCollections.length) return res.clone(extendCollections);
        return res;
    }, [
        instance,
        extendCollections,
        dataSource
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionManagerContext.Provider, {
        value: cm,
        children: children
    });
};
const CollectionManagerProvider = (param)=>{
    let { instance, dataSource, children } = param;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__data_source_DataSourceProvider_mjs__.DataSourceProvider, {
        dataSource: dataSource,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionManagerProviderInner, {
            instance: instance,
            children: children
        })
    });
};
function useCollectionManager() {
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CollectionManagerContext);
    return context;
}
const useCollections = (predicate)=>{
    const collectionManager = useCollectionManager();
    const collections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collectionManager.getCollections(predicate), [
        collectionManager,
        predicate
    ]);
    return collections;
};
export { CollectionManagerContext, CollectionManagerProvider, useCollectionManager, useCollections };
