import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from "react";
import { DataSourceProvider, useDataSource } from "../data-source/DataSourceProvider.mjs";
import { useExtendCollections } from "./ExtendCollectionsProvider.mjs";
const CollectionManagerContext = /*#__PURE__*/ createContext(null);
CollectionManagerContext.displayName = 'CollectionManagerContext';
const CollectionManagerProviderInner = (param)=>{
    let { instance, children } = param;
    const dataSource = useDataSource();
    const extendCollections = useExtendCollections();
    const cm = 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__*/ jsx(CollectionManagerContext.Provider, {
        value: cm,
        children: children
    });
};
const CollectionManagerProvider = (param)=>{
    let { instance, dataSource, children } = param;
    return /*#__PURE__*/ jsx(DataSourceProvider, {
        dataSource: dataSource,
        children: /*#__PURE__*/ jsx(CollectionManagerProviderInner, {
            instance: instance,
            children: children
        })
    });
};
function useCollectionManager() {
    const context = useContext(CollectionManagerContext);
    return context;
}
const useCollections = (predicate)=>{
    const collectionManager = useCollectionManager();
    const collections = useMemo(()=>collectionManager.getCollections(predicate), [
        collectionManager,
        predicate
    ]);
    return collections;
};
export { CollectionManagerContext, CollectionManagerProvider, useCollectionManager, useCollections };
