// 假设这些类型在其他文件中定义，这里使用 import 引入
import { ObservableObject } from './ObservableObject';
import { RootViewModel } from './RootViewModel';
import { TreeViewXmlSource } from './TreeViewXmlSource';
import { TreeViewRoot } from './TreeViewRoot';

// 模拟 ObservableCollection
class ObservableCollection<T> {
    private items: T[] = [];

    add(item: T) {
        this.items.push(item);
    }

    getItems() {
        return this.items;
    }
}

export class TreeViewModel extends ObservableObject {
    private rootCollection: ObservableCollection<RootViewModel> | null = null;

    get RootCollection(): ObservableCollection<RootViewModel> {
        if (!this.rootCollection) {
            throw new Error('RootCollection is not initialized.');
        }
        return this.rootCollection;
    }

    set RootCollection(value: ObservableCollection<RootViewModel>) {
        if (this.rootCollection !== value) {
            this.rootCollection = value;
            this.RaisePropertyChanged('RootCollection');
        }
    }

    constructor() {
        super();
        this.RootCollection = new ObservableCollection<RootViewModel>();
        const tvs = new TreeViewXmlSource();
        const listItems = tvs.GetXmlMenu();

        listItems.forEach((item: TreeViewRoot) => {
            this.RootCollection.add(new RootViewModel(item));
        });
    }

    RaisePropertyChanged(propertyName: string) {
        // 这里可以实现具体的属性更改通知逻辑
        console.log(`${propertyName} property has changed.`);
    }
}