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