import { jsx } from "react/jsx-runtime";
import { createContext, useContext } from "react";
import { CollectionFieldProvider, useCollectionField } from "../collection-field/index.mjs";
import { CollectionDeletedPlaceholder } from "../components/CollectionDeletedPlaceholder.mjs";
import { useCollectionManager } from "./CollectionManagerProvider.mjs";
import { CollectionProvider, useCollection } from "./CollectionProvider.mjs";
const ParentCollectionContext = /*#__PURE__*/ createContext(null);
ParentCollectionContext.displayName = 'ParentCollectionContext';
const ParentCollectionProvider = (props)=>{
    const collection = useCollection();
    return /*#__PURE__*/ jsx(ParentCollectionContext.Provider, {
        value: collection,
        children: props.children
    });
};
const useParentCollection = ()=>useContext(ParentCollectionContext);
const AssociationProvider = (props)=>{
    const { name, children } = props;
    const collectionManager = useCollectionManager();
    const collectionName = collectionManager.getCollectionName(name);
    if (!collectionName) return /*#__PURE__*/ jsx(CollectionDeletedPlaceholder, {
        type: "Collection",
        name: name
    });
    return /*#__PURE__*/ jsx(CollectionProvider, {
        name: String(name).split('.')[0],
        children: /*#__PURE__*/ jsx(ParentCollectionProvider, {
            children: /*#__PURE__*/ jsx(CollectionFieldProvider, {
                name: name,
                children: /*#__PURE__*/ jsx(CollectionProvider, {
                    name: collectionName,
                    children: children
                })
            })
        })
    });
};
const useAssociationName = ()=>{
    const field = useCollectionField();
    return field ? "".concat(field.collectionName, ".").concat(field.name) : null;
};
export { AssociationProvider, useAssociationName, useParentCollection };
