import { BimComponent } from '../core/component';
import { BimTree } from '../components/tree/index';
import { BimDialog } from '../components/dialog/index';
import { TreeNodeConfig } from '../components/tree/types';

/**
 * 模型树业务管理器
 * 负责组装 Tree 和 Dialog 组件，提供开箱即用的业务弹窗
 */
export class ModelTreeManager extends BimComponent {

    /**
     * 显示带复选框的模型结构树弹窗
     * @param data 树节点数据
     * @param title 弹窗标题 (翻译键)
     */
    public showStructTree(data: TreeNodeConfig[], title: string = 'tree.modelStruct'): { tree: BimTree, dialog: BimDialog } {
        // 1. 创建 Tree 实例
        const tree = this.engine.tree!.create({
            data: data,
            checkable: true,
            checkStrictly: true,
            defaultExpandAll: true
        });

        // 2. 创建 Dialog 实例
        const dialog = this.engine.dialog!.create({
            title: title,
            width: 300,
            height: 400,
            content: tree.element,
            position: 'left-center',
            resizable: true,
            // 关键：绑定生命周期
            onClose: () => {
                tree.destroy();
            }
        });

        return { tree, dialog };
    }

    /**
     * 显示简单的树形弹窗 (无复选框)
     * @param data 树节点数据
     * @param title 弹窗标题 (翻译键)
     */
    public showSimpleTree(data: TreeNodeConfig[], title: string = 'menu.home'): { tree: BimTree, dialog: BimDialog } {
        // 1. 创建 Tree 实例
        const tree = this.engine.tree!.create({
            data: data,
            checkable: false,
            defaultExpandAll: true
        });

        // 2. 创建 Dialog 实例
        const dialog = this.engine.dialog!.create({
            title: title,
            width: 250,
            height: 300,
            content: tree.element,
            position: 'center',
            resizable: true,
            onClose: () => {
                tree.destroy();
            }
        });

        return { tree, dialog };
    }

    public destroy(): void {
        // 具体的 Tree 和 Dialog 实例由它们各自的生命周期管理
        // 这里不需要销毁它们，除非我们持有了全局引用
    }
}
