"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    getIdsWithChildren: ()=>getIdsWithChildren,
    TableBlockContext: ()=>TableBlockContext,
    useTableBlockContext: ()=>useTableBlockContext,
    TableBlockProvider: ()=>TableBlockProvider
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const withDynamicSchemaProps_js_namespaceObject = require("../application/hoc/withDynamicSchemaProps.js");
const index_js_namespaceObject = require("../collection-manager/index.js");
const useTableBlockDecoratorProps_js_namespaceObject = require("../modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const TableBlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.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, schema_namespaceObject.useField)();
    const { resource, service } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { treeTable } = (null == fieldSchema ? void 0 : fieldSchema['x-decorator-props']) || {};
    const [expandFlag, setExpandFlag] = (0, external_react_namespaceObject.useState)(!!fieldNames || false);
    const allIncludesChildren = (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.FixedBlockWrapper, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(TableBlockContext.Provider, {
            value: {
                ...others,
                field,
                service,
                resource,
                params,
                showIndex,
                dragSort,
                rowKey,
                expandFlag,
                childrenColumnName,
                allIncludesChildren,
                setExpandFlag: ()=>setExpandFlag(!expandFlag)
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.RenderChildrenWithAssociationFilter, {
                ...props
            })
        })
    });
};
const useTableBlockParamsCompat = (props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    let params;
    params = fieldSchema['x-use-decorator-props'] ? props.params : (0, useTableBlockDecoratorProps_js_namespaceObject.useTableBlockParams)(props);
    return params;
};
const TableBlockProvider = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((props)=>{
    const resourceName = props.resource || props.association;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getCollection, getCollectionField } = (0, index_js_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)(), [
        treeTable
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponentOptions, {
        scope: {
            treeTable
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
                name: props.name || 'table',
                ...props,
                params: params,
                runWhenParamsChanged: true,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalTableBlockProvider, {
                    ...props,
                    childrenColumnName: childrenColumnName,
                    params: params
                })
            })
        })
    });
});
const useTableBlockContext = ()=>(0, external_react_namespaceObject.useContext)(TableBlockContext);
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
