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__filter_provider_index_mjs__ from "../filter-provider/index.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";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "./hooks/index.mjs";
const TreeBlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
const InternalTreeBlockProvider = (props)=>{
    const { params, showIndex, fieldNames, ...others } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { resource, service } = (0, __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.useBlockRequestContext)();
    const [expandFlag, setExpandFlag] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    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)(TreeBlockContext.Provider, {
            value: {
                ...others,
                field,
                service,
                resource,
                params,
                expandFlag,
                setExpandFlag: ()=>setExpandFlag(!expandFlag)
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.RenderChildrenWithAssociationFilter, {
                ...props
            })
        })
    });
};
const TreeBlockProvider = (props)=>{
    const params = {
        ...props.params
    };
    params.tree = true;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.BlockProvider, {
        ...props,
        params: params,
        runWhenParamsChanged: true,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalTreeBlockProvider, {
            ...props,
            params: params
        })
    });
};
const useTreeBlockContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const ctx = useTreeBlockContext();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getDataBlocks } = (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.useFilterBlock)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)(filter)),
                    block.defaultFilter
                ]);
                return block.doFilter({
                    ...param,
                    page: 1,
                    filter: mergedFilter
                }, {
                    filters: storedFilter
                });
            });
        }
    };
};
export { TreeBlockContext, TreeBlockProvider, useTreeBlockContext, useTreeBlockProps };
