import { Fragment, jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from "react";
import { CollectionDeletedPlaceholder } from "../components/CollectionDeletedPlaceholder.mjs";
import { useCollectionManager } from "./CollectionManagerProvider.mjs";
const CollectionContext = /*#__PURE__*/ createContext(null);
CollectionContext.displayName = 'CollectionContext';
const CollectionProvider = (props)=>{
    const { name, children, allowNull } = props;
    const collectionManager = useCollectionManager();
    const collection = useMemo(()=>null == collectionManager ? void 0 : collectionManager.getCollection(name), [
        collectionManager,
        name
    ]);
    if (!collection && allowNull) return /*#__PURE__*/ jsx(Fragment, {
        children: props.children
    });
    if (!collection && !allowNull) return /*#__PURE__*/ jsx(CollectionDeletedPlaceholder, {
        type: "Collection",
        name: name
    });
    return /*#__PURE__*/ jsx(CollectionContext.Provider, {
        value: collection,
        children: children
    });
};
function useCollection() {
    const context = useContext(CollectionContext);
    return context;
}
const useCollectionFields = (predicate)=>{
    const collection = useCollection();
    const fields = useMemo(()=>collection.getFields(predicate), [
        collection,
        predicate
    ]);
    return fields;
};
export { CollectionContext, CollectionProvider, useCollection, useCollectionFields };
