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__application_hoc_withDynamicSchemaProps_mjs__ from "../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_table_hooks_useTableBlockDecoratorProps_mjs__ from "../modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__ from "./BlockProvider.mjs";
const TableBlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
TableBlockContext.displayName = 'TableBlockContext';
function getIdsWithChildren(nodes) {
    const ids = [];
    if (nodes) {
        for (const node of nodes)if ((null == node ? void 0 : node.children) && node.children.length > 0) {
            ids.push(node.id);
            ids.push(...getIdsWithChildren(null == node ? void 0 : node.children));
        }
    }
    return ids;
}
const InternalTableBlockProvider = (props)=>{
    const { params, showIndex, dragSort, rowKey, childrenColumnName, fieldNames, ...others } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { resource, service } = (0, __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.useBlockRequestContext)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { treeTable } = (null == fieldSchema ? void 0 : fieldSchema['x-decorator-props']) || {};
    const [expandFlag, setExpandFlag] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(!!fieldNames || false);
    const allIncludesChildren = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (false !== treeTable) {
            var _service_data;
            const keys = getIdsWithChildren(null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.data);
            return keys || [];
        }
    }, [
        null == service ? void 0 : service.loading
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.FixedBlockWrapper, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(TableBlockContext.Provider, {
            value: {
                ...others,
                field,
                service,
                resource,
                params,
                showIndex,
                dragSort,
                rowKey,
                expandFlag,
                childrenColumnName,
                allIncludesChildren,
                setExpandFlag: ()=>setExpandFlag(!expandFlag)
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.RenderChildrenWithAssociationFilter, {
                ...props
            })
        })
    });
};
const useTableBlockParamsCompat = (props)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    let params;
    params = fieldSchema['x-use-decorator-props'] ? props.params : (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_table_hooks_useTableBlockDecoratorProps_mjs__.useTableBlockParams)(props);
    return params;
};
const TableBlockProvider = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((props)=>{
    const resourceName = props.resource || props.association;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getCollection, getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)(props.dataSource);
    const collection = getCollection(props.collection, props.dataSource);
    const { treeTable } = (null == fieldSchema ? void 0 : fieldSchema['x-decorator-props']) || {};
    const params = useTableBlockParamsCompat(props) || {};
    let childrenColumnName = 'children';
    if ((null == collection ? void 0 : collection.tree) && false !== treeTable) {
        if (null == resourceName ? void 0 : resourceName.includes('.')) {
            const f = getCollectionField(resourceName);
            if (null == f ? void 0 : f.treeChildren) {
                childrenColumnName = f.name;
                params['tree'] = true;
            }
        } else {
            const f = collection.fields.find((f)=>f.treeChildren);
            if (f) childrenColumnName = f.name;
            params['tree'] = true;
        }
    }
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)(), [
        treeTable
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponentOptions, {
        scope: {
            treeTable
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.BlockProvider, {
                name: props.name || 'table',
                ...props,
                params: params,
                runWhenParamsChanged: true,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalTableBlockProvider, {
                    ...props,
                    childrenColumnName: childrenColumnName,
                    params: params
                })
            })
        })
    });
});
const useTableBlockContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(TableBlockContext);
export { TableBlockContext, TableBlockProvider, getIdsWithChildren, useTableBlockContext };
