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__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__collection_index_mjs__ from "../collection/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_CollectionDeletedPlaceholder_mjs__ from "../components/CollectionDeletedPlaceholder.mjs";
const CollectionFieldContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
CollectionFieldContext.displayName = 'CollectionFieldContext';
const CollectionFieldProvider = (props)=>{
    const { name, children, allowNull } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_index_mjs__.useCollection)();
    const collectionManager = (0, __WEBPACK_EXTERNAL_MODULE__collection_index_mjs__.useCollectionManager)();
    const value = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _fieldSchema_xcomponentprops;
        if (!collection) return null;
        const field = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['field'];
        return collectionManager.getCollectionField(null == fieldSchema ? void 0 : fieldSchema['x-collection-field']) || field || collection.getField((null == field ? void 0 : field.name) || name);
    }, [
        collection,
        fieldSchema,
        name,
        collectionManager
    ]);
    if (!value && allowNull) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: children
    });
    if (!value) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__components_CollectionDeletedPlaceholder_mjs__.CollectionDeletedPlaceholder, {
        type: "Field",
        name: name
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionFieldContext.Provider, {
        value: value,
        children: children
    });
};
const useCollectionField = ()=>{
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CollectionFieldContext);
    return context;
};
export { CollectionFieldContext, CollectionFieldProvider, useCollectionField };
