"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecycleTreeAdaptiveDecorator = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const types_1 = require("../../types");
/**
 * 将 RecycleTree 组件装饰到能够自适应高度
 * @param recycleTreeComp RecycleTree 组件
 */
const RecycleTreeAdaptiveDecorator = (recycleTreeComp) => (props) => {
    const { model, itemHeight, onReady } = props;
    const ref = react_1.default.useRef();
    const destroyWhileBlur = react_1.default.useRef(false);
    const [currentHeight, setCurrentHeight] = react_1.default.useState(0);
    const handleExpansionChange = () => {
        setCurrentHeight(model.root.branchSize * itemHeight);
    };
    const handleOnReady = (handle) => {
        if (!onReady) {
            return;
        }
        onReady({
            ...handle,
            promptNewTreeNode: async (at) => {
                var _a;
                const promptHandle = await handle.promptNewTreeNode(at);
                if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.root.branchSize) {
                    setCurrentHeight(ref.current.root.branchSize * itemHeight + itemHeight);
                }
                else {
                    // 添加节点时，如果不存在 ref.current，即不存在可渲染节点
                    // 补全高度便于插入输入框
                    setCurrentHeight(itemHeight);
                }
                destroyWhileBlur.current = false;
                promptHandle.onDestroy(() => {
                    var _a;
                    if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.root.branchSize) {
                        setCurrentHeight(ref.current.root.branchSize * itemHeight);
                    }
                });
                promptHandle.onCancel(() => {
                    if (ref.current) {
                        setCurrentHeight(ref.current.root.branchSize * itemHeight);
                    }
                });
                promptHandle.onBlur(() => destroyWhileBlur.current);
                return promptHandle;
            },
            promptNewCompositeTreeNode: async (at) => {
                var _a;
                const promptHandle = await handle.promptNewCompositeTreeNode(at);
                if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.root.branchSize) {
                    setCurrentHeight(ref.current.root.branchSize * itemHeight + itemHeight);
                }
                else {
                    // 添加节点时，如果不存在 ref.current，即不存在可渲染节点
                    // 补全高度便于插入输入框
                    setCurrentHeight(itemHeight);
                }
                promptHandle.onDestroy(() => {
                    var _a;
                    if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.root.branchSize) {
                        setCurrentHeight(ref.current.root.branchSize * itemHeight);
                    }
                });
                promptHandle.onCancel(() => {
                    var _a;
                    if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.root.branchSize) {
                        setCurrentHeight(ref.current.root.branchSize * itemHeight);
                    }
                });
                promptHandle.onBlur(() => destroyWhileBlur.current);
                return promptHandle;
            },
        });
    };
    react_1.default.useEffect(() => {
        ref.current = model;
        if (ref.current.root) {
            setCurrentHeight(ref.current.root.branchSize * itemHeight);
            ref.current.root.watcher.on(types_1.TreeNodeEvent.DidChangeExpansionState, handleExpansionChange);
        }
    }, [model]);
    react_1.default.useEffect(() => {
        // currentHeight 更新时，有概率让整个 Tree 重绘
        // 导致 prompt 的 blur 事件触发
        // 这里等待 100 ms 后再将 prompt 失去焦点后关闭的逻辑打开
        setTimeout(() => {
            destroyWhileBlur.current = true;
        }, 100);
    }, [currentHeight]);
    return (react_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement(recycleTreeComp, {
        ...props,
        height: currentHeight,
        onReady: handleOnReady,
    })));
};
exports.RecycleTreeAdaptiveDecorator = RecycleTreeAdaptiveDecorator;
//# sourceMappingURL=index.js.map