import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo, useState } from "react";
import { FormContext, createForm, useField, useFieldSchema } from "@tachybase/schema";
import { withDynamicSchemaProps } from "../application/hoc/withDynamicSchemaProps.mjs";
import { useCollectionManager_deprecated } from "../collection-manager/index.mjs";
import { useTableBlockParams } from "../modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.mjs";
import { FixedBlockWrapper, SchemaComponentOptions } from "../schema-component/index.mjs";
import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from "./BlockProvider.mjs";
const TableBlockContext = /*#__PURE__*/ 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 = useField();
    const { resource, service } = useBlockRequestContext();
    const fieldSchema = useFieldSchema();
    const { treeTable } = (null == fieldSchema ? void 0 : fieldSchema['x-decorator-props']) || {};
    const [expandFlag, setExpandFlag] = useState(!!fieldNames);
    const allIncludesChildren = useMemo(()=>{
        if (false !== treeTable) {
            var _service_data;
            const keys = getIdsWithChildren(null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data);
            return keys || [];
        }
    }, [
        null == service ? void 0 : service.loading
    ]);
    return /*#__PURE__*/ jsx(FixedBlockWrapper, {
        children: /*#__PURE__*/ jsx(TableBlockContext.Provider, {
            value: {
                ...others,
                field,
                service,
                resource,
                params,
                showIndex,
                dragSort,
                rowKey,
                expandFlag,
                childrenColumnName,
                allIncludesChildren,
                setExpandFlag: ()=>setExpandFlag(!expandFlag)
            },
            children: /*#__PURE__*/ jsx(RenderChildrenWithAssociationFilter, {
                ...props
            })
        })
    });
};
const useTableBlockParamsCompat = (props)=>{
    const fieldSchema = useFieldSchema();
    let params;
    params = fieldSchema['x-use-decorator-props'] ? props.params : useTableBlockParams(props);
    return params;
};
const TableBlockProvider = withDynamicSchemaProps((props)=>{
    const resourceName = props.resource || props.association;
    const fieldSchema = useFieldSchema();
    const { getCollection, getCollectionField } = 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 = useMemo(()=>createForm(), [
        treeTable
    ]);
    return /*#__PURE__*/ jsx(SchemaComponentOptions, {
        scope: {
            treeTable
        },
        children: /*#__PURE__*/ jsx(FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ jsx(BlockProvider, {
                name: props.name || 'table',
                ...props,
                params: params,
                runWhenParamsChanged: true,
                children: /*#__PURE__*/ jsx(InternalTableBlockProvider, {
                    ...props,
                    childrenColumnName: childrenColumnName,
                    params: params
                })
            })
        })
    });
});
const useTableBlockContext = ()=>useContext(TableBlockContext);
export { TableBlockContext, TableBlockProvider, getIdsWithChildren, useTableBlockContext };
