import { jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { observer, useField, useFieldSchema } from "@tachybase/schema";
import { Tree } from "antd";
import { useTreeBlockContext } from "../../../index.mjs";
import { withDynamicSchemaProps } from "../../../application/hoc/index.mjs";
const useArrayField = (props)=>{
    const field = useField();
    return props.field || field;
};
const replacePlaceholders = (inputStr, values)=>inputStr.replace(/{{(.*?)}}/g, function(match, placeholder) {
        return (null == values ? void 0 : values.hasOwnProperty(placeholder)) ? values[placeholder] : match;
    });
function findIds(arr) {
    let ids = [];
    function recurse(array) {
        null == array || array.forEach((item)=>{
            if (item.id) ids.push(item.id);
            if (item.children) recurse(item.children);
        });
    }
    recurse(arr);
    return ids;
}
const Tree_Tree = withDynamicSchemaProps(observer((props)=>{
    var _field_data, _field_data1, _field_data2;
    const schema = useFieldSchema();
    const [expandedKeys, setExpandedKeys] = useState([]);
    const { expandFlag, sleepTime, titleRender: titleRenderText } = (null == schema ? void 0 : schema.parent['x-decorator-props']) || {};
    const { service, expandFlag: expandFlagCtx } = useTreeBlockContext();
    const titleRender = (nodeData)=>{
        const valueOject = {};
        let outputStr = '';
        if (!(titleRenderText && titleRenderText.includes('{{'))) return nodeData.title;
        {
            const regex = /{{(.*?)}}/g;
            let match;
            while(match = regex.exec(titleRenderText))valueOject[match[1]] = nodeData[match[1]] || '';
            outputStr = replacePlaceholders(titleRenderText, valueOject);
            return outputStr;
        }
    };
    const field = useArrayField(props);
    const { onSelectNode } = props;
    const treeData = null == field ? void 0 : null == (_field_data = field.data) ? void 0 : _field_data.treeData;
    useEffect(()=>{
        let timer = null;
        if (sleepTime > 0) timer = setInterval(()=>{
            service.refresh();
        }, 1000 * Number(sleepTime));
        return ()=>{
            timer && clearInterval(timer);
        };
    }, []);
    useEffect(()=>{
        var _field_data;
        const ids = findIds(null == field ? void 0 : null == (_field_data = field.data) ? void 0 : _field_data.treeData);
        setExpandedKeys(expandFlagCtx ? ids : []);
    }, [
        expandFlagCtx,
        null == field ? void 0 : null == (_field_data1 = field.data) ? void 0 : _field_data1.treeData
    ]);
    useEffect(()=>{
        var _field_data;
        const ids = findIds(null == field ? void 0 : null == (_field_data = field.data) ? void 0 : _field_data.treeData);
        setExpandedKeys(expandFlag ? ids : []);
    }, [
        expandFlag,
        null == field ? void 0 : null == (_field_data2 = field.data) ? void 0 : _field_data2.treeData
    ]);
    return /*#__PURE__*/ jsx(Tree, {
        ...props,
        treeData: treeData,
        titleRender: titleRender,
        onSelect: onSelectNode,
        expandedKeys: expandedKeys,
        onExpand: (expandedKey)=>{
            setExpandedKeys(expandedKey);
        }
    });
}));
export { Tree_Tree as Tree };
