import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from "react";
import { withDynamicSchemaProps } from "../../application/hoc/index.mjs";
import { ACLCollectionProvider } from "../../built-in/acl/index.mjs";
import { useDesignable } from "../../schema-component/index.mjs";
import { AssociationProvider, CollectionManagerProvider, CollectionProvider } from "../collection/index.mjs";
import { ConfigSettingProvider } from "./context/ConfigSetting.provider.mjs";
import { BlockRequestProvider } from "./DataBlockRequestProvider.mjs";
import { DataBlockResourceProvider } from "./DataBlockResourceProvider.mjs";
const DataBlockContext = /*#__PURE__*/ createContext({});
DataBlockContext.displayName = 'DataBlockContext';
const AssociationOrCollectionProvider = (props)=>{
    const { collection, association, allowNull = false } = props;
    const AssociationOrCollection = useMemo(()=>{
        if (association) return {
            Component: AssociationProvider,
            name: association
        };
        return {
            Component: CollectionProvider,
            name: collection
        };
    }, [
        collection,
        association
    ]);
    return /*#__PURE__*/ jsx(AssociationOrCollection.Component, {
        name: AssociationOrCollection.name,
        allowNull: allowNull,
        children: props.children
    });
};
const DataBlockProvider = withDynamicSchemaProps((props)=>{
    const { collection, association, dataSource, children, layoutDirection, ...resets } = props;
    const configSetting = {
        layoutDirection
    };
    const { dn } = useDesignable();
    return /*#__PURE__*/ jsx(DataBlockContext.Provider, {
        value: {
            dn,
            props: {
                ...resets,
                collection,
                association,
                dataSource
            }
        },
        children: /*#__PURE__*/ jsx(ConfigSettingProvider, {
            value: configSetting,
            children: /*#__PURE__*/ jsx(CollectionManagerProvider, {
                dataSource: dataSource,
                children: /*#__PURE__*/ jsx(AssociationOrCollectionProvider, {
                    collection: collection,
                    association: association,
                    children: /*#__PURE__*/ jsx(ACLCollectionProvider, {
                        children: /*#__PURE__*/ jsx(DataBlockResourceProvider, {
                            children: /*#__PURE__*/ jsx(BlockRequestProvider, {
                                children: children
                            })
                        })
                    })
                })
            })
        })
    });
}, {
    displayName: 'DataBlockProvider'
});
const useDataBlock = ()=>{
    const context = useContext(DataBlockContext);
    if (!context) throw new Error('useDataBlock() must be used within a DataBlockProvider');
    return context;
};
const useDataBlockProps = ()=>{
    const context = useDataBlock();
    return context.props;
};
export { AssociationOrCollectionProvider, DataBlockContext, DataBlockProvider, useDataBlock, useDataBlockProps };
