"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__, {
    TreeBlockProvider: ()=>TreeBlockProvider,
    useTreeBlockProps: ()=>useTreeBlockProps,
    TreeBlockContext: ()=>TreeBlockContext,
    useTreeBlockContext: ()=>useTreeBlockContext
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const index_js_namespaceObject = require("../filter-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const external_hooks_index_js_namespaceObject = require("./hooks/index.js");
const TreeBlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
const InternalTreeBlockProvider = (props)=>{
    const { params, showIndex, fieldNames, ...others } = props;
    const field = (0, schema_namespaceObject.useField)();
    const { resource, service } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const [expandFlag, setExpandFlag] = (0, external_react_namespaceObject.useState)(false);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.FixedBlockWrapper, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(TreeBlockContext.Provider, {
            value: {
                ...others,
                field,
                service,
                resource,
                params,
                expandFlag,
                setExpandFlag: ()=>setExpandFlag(!expandFlag)
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.RenderChildrenWithAssociationFilter, {
                ...props
            })
        })
    });
};
const TreeBlockProvider = (props)=>{
    const params = {
        ...props.params
    };
    params.tree = true;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
        ...props,
        params: params,
        runWhenParamsChanged: true,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalTreeBlockProvider, {
            ...props,
            params: params
        })
    });
};
const useTreeBlockContext = ()=>(0, external_react_namespaceObject.useContext)(TreeBlockContext);
function getDescendants(tree, targetId) {
    let result = [];
    function traverse(node, pId) {
        if (node.id === pId) {
            result.push(node.id);
            if (node.children) for (let child of node.children)traverse(child, child.id);
        } else if (node.children) for (let child of node.children)traverse(child, targetId);
    }
    for (let node of tree)traverse(node, targetId);
    return result;
}
function buildTree(data) {
    null == data || data.forEach((item)=>{
        item.title = item.id;
        item.key = item.id;
        buildTree((null == item ? void 0 : item.children) || []);
    });
    return data;
}
const useTreeBlockProps = ()=>{
    var _ctx_service, _ctx_service1;
    const field = (0, schema_namespaceObject.useField)();
    const ctx = useTreeBlockContext();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getDataBlocks } = (0, index_js_namespaceObject.useFilterBlock)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _ctx_service;
        if (!(null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading)) {
            var _ctx_service_data, _ctx_service1;
            field.data = field.data || {};
            field.data.treeData = buildTree((null == ctx ? void 0 : null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : null === (_ctx_service_data = _ctx_service1.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data) || []);
        }
    }, [
        null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading
    ]);
    return {
        loading: null == ctx ? void 0 : null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : _ctx_service1.loading,
        onSelectNode: (selectedKeys, param)=>{
            let { selected } = param;
            const { targets, uid } = (0, external_hooks_index_js_namespaceObject.findFilterTargets)(fieldSchema);
            const dataBlocks = getDataBlocks();
            null == dataBlocks || dataBlocks.forEach((block)=>{
                var _block_service_params, _block_service_params_, _block_service_params1;
                const target = targets.find((target)=>target.uid === block.uid);
                if (!target) return;
                const param = (null === (_block_service_params = block.service.params) || void 0 === _block_service_params ? void 0 : _block_service_params[0]) || {};
                const storedFilter = (null === (_block_service_params1 = block.service.params) || void 0 === _block_service_params1 ? void 0 : null === (_block_service_params_ = _block_service_params1[1]) || void 0 === _block_service_params_ ? void 0 : _block_service_params_.filters) || {};
                const ids = getDescendants(field.data.treeData, selectedKeys[0]);
                storedFilter[uid] = {
                    $or: selected ? ids.map((id)=>({
                            [target.field || 'id']: {
                                ['$eq']: id
                            }
                        })) : []
                };
                const mergedFilter = (0, index_js_namespaceObject.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, external_schema_component_index_js_namespaceObject.removeNullCondition)(filter)),
                    block.defaultFilter
                ]);
                return block.doFilter({
                    ...param,
                    page: 1,
                    filter: mergedFilter
                }, {
                    filters: storedFilter
                });
            });
        }
    };
};
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
});
