"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__, {
    Tree: ()=>Tree
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_index_js_namespaceObject = require("../../../index.js");
const index_js_namespaceObject = require("../../../application/hoc/index.js");
const useArrayField = (props)=>{
    const field = (0, schema_namespaceObject.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, index_js_namespaceObject.withDynamicSchemaProps)((0, schema_namespaceObject.observer)((props)=>{
    var _field_data, _field_data1, _field_data2;
    const schema = (0, schema_namespaceObject.useFieldSchema)();
    const [expandedKeys, setExpandedKeys] = (0, external_react_namespaceObject.useState)([]);
    const { expandFlag, sleepTime, titleRender: titleRenderText } = (null == schema ? void 0 : schema.parent['x-decorator-props']) || {};
    const { service, expandFlag: expandFlagCtx } = (0, external_index_js_namespaceObject.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, external_react_namespaceObject.useEffect)(()=>{
        let timer = null;
        if (sleepTime > 0) timer = setInterval(()=>{
            service.refresh();
        }, 1000 * Number(sleepTime));
        return ()=>{
            timer && clearInterval(timer);
        };
    }, []);
    (0, external_react_namespaceObject.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, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tree, {
        ...props,
        treeData: treeData,
        titleRender: titleRender,
        onSelect: onSelectNode,
        expandedKeys: expandedKeys,
        onExpand: (expandedKey)=>{
            setExpandedKeys(expandedKey);
        }
    });
}));
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
});
