import { BimComponent } from '../core/component';
import { BimTree } from '../components/tree/index';
import { TreeOptions } from '../components/tree/types';
import type { BimEngine } from '../bim-engine';

/**
 * 树组件管理器
 * 负责创建和管理 BimTree 实例
 */
export class TreeManager extends BimComponent {
    
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    constructor(engine: BimEngine, _container: HTMLElement) {
        super(engine);
    }
    
    /**
     * 创建一个新的树组件实例
     * @param options 配置选项
     */
    public create(options: TreeOptions): BimTree {
        const tree = new BimTree(options);
        
        // 绑定事件桥接
        tree.onNodeCheck = (node) => {
            this.emit('ui:tree-node-check', { 
                id: node.config.id, 
                checked: node.config.checked || false, 
                node: node.config 
            });
        };
        
        tree.onNodeSelect = (node) => {
            this.emit('ui:tree-node-select', { 
                id: node.config.id, 
                selected: true, 
                node: node.config 
            });
        };
        
        tree.onNodeExpand = (node) => {
            this.emit('ui:tree-node-expand', { 
                id: node.config.id, 
                expanded: node.config.expanded || false
            });
        };
        
        tree.init();
        return tree;
    }

    public destroy(): void {
        // TreeManager 本身不持有 Tree 实例的强引用列表
        // 实例通常由调用者（如 Dialog）持有并销毁
        // 这里可以做一些全局清理工作
    }
}
