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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_index_mjs__ from "../../../application/hoc/index.mjs";
const useArrayField = (props)=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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 = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_index_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _field_data, _field_data1, _field_data2;
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const [expandedKeys, setExpandedKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const { expandFlag, sleepTime, titleRender: titleRenderText } = (null == schema ? void 0 : schema.parent['x-decorator-props']) || {};
    const { service, expandFlag: expandFlagCtx } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.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 ? void 0 : _field_data.treeData;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        let timer = null;
        if (sleepTime > 0) timer = setInterval(()=>{
            service.refresh();
        }, 1000 * Number(sleepTime));
        return ()=>{
            timer && clearInterval(timer);
        };
    }, []);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        var _field_data;
        const ids = findIds(null == field ? void 0 : null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.treeData);
        setExpandedKeys(expandFlagCtx ? ids : []);
    }, [
        expandFlagCtx,
        null == field ? void 0 : null === (_field_data1 = field.data) || void 0 === _field_data1 ? void 0 : _field_data1.treeData
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        var _field_data;
        const ids = findIds(null == field ? void 0 : null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.treeData);
        setExpandedKeys(expandFlag ? ids : []);
    }, [
        expandFlag,
        null == field ? void 0 : null === (_field_data2 = field.data) || void 0 === _field_data2 ? void 0 : _field_data2.treeData
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tree, {
        ...props,
        treeData: treeData,
        titleRender: titleRender,
        onSelect: onSelectNode,
        expandedKeys: expandedKeys,
        onExpand: (expandedKey)=>{
            setExpandedKeys(expandedKey);
        }
    });
}));
export { Tree };
