// 假设 NotificationObject 类的定义如下
class NotificationObject {
    protected raisePropertyChanged(propertyName: string) {
        // 实现属性变更通知逻辑
    }
}

// 假设 TradeModel 类的定义如下
class TradeModel {
    GUID: string;
    ParentID: string;
    Name: string;
    Code: string;
    Type: string;
}

// 假设 ConfigManager 类的定义如下
class ConfigManager {
    private static instance: ConfigManager;
    private constructor() {}

    public static GetInstance() {
        if (!this.instance) {
            this.instance = new ConfigManager();
        }
        return this.instance;
    }

    public GetConfigValue(key: string, type: string) {
        // 实现获取配置值的逻辑
        return "";
    }
}

class TradeViewModel extends NotificationObject {
    private _isleaf: boolean = false;
    private _node: TradeModel | null = null;
    public Parent: TradeViewModel | null = null;
    public IsSelected: boolean = false;
    public IsExpanded: boolean = false;
    private _children: TradeViewModel[] = [];

    get Node() {
        return this._node;
    }

    set Node(value: TradeModel | null) {
        this._node = value;
        this.raisePropertyChanged("Node");
    }

    get IsLeaf() {
        return this._isleaf;
    }

    set IsLeaf(value: boolean) {
        this._isleaf = value;
        this.raisePropertyChanged("IsLeaf");
    }

    get Children() {
        return this._children;
    }

    set Children(value: TradeViewModel[]) {
        this._children = value;
        this.raisePropertyChanged("Children");
    }

    public static GetRootNode(ModelList: TradeModel[]): TradeViewModel[] {
        const VMList: TradeViewModel[] = [];
        const list = ModelList.filter(x => !x.ParentID);
        for (const item of list) {
            const vm = TradeViewModel.CreateVm(ModelList, item);
            if (vm) {
                VMList.push(vm);
            }
        }

        // 常用交易菜单添加
        // const tradeModel = new TradeModel();
        // tradeModel.Name = "常用交易";
        // const tradeViewModel = new TradeViewModel();
        // tradeViewModel.Node = tradeModel;
        // const commonTradeCodes = ConfigManager.GetInstance().GetConfigValue("CommonTrade", "User").split('|').filter(s => s);
        // for (const code of commonTradeCodes) {
        //     const matchingModels = ModelList.filter(x => x.Code === code);
        //     for (const b of matchingModels) {
        //         const child = new TradeViewModel();
        //         child.Node = b;
        //         child.IsLeaf = true;
        //         child.Children = [];
        //         tradeViewModel.Children.push(child);
        //     }
        // }
        // VMList.push(tradeViewModel);

        return VMList;
    }

    private static CreateVm(models: TradeModel[], current: TradeModel): TradeViewModel | null {
        const result = new TradeViewModel();
        result.Node = current;
        const list = models.filter(x => x.ParentID === result.Node?.GUID);
        if (list.length === 0) {
            if (current.Type === "category") {
                return null;
            }
            result.IsLeaf = true;
            result.Children = [];
        } else {
            result.IsLeaf = false;
            for (const item of list) {
                const child = TradeViewModel.CreateVm(models, item);
                if (child) {
                    child.Parent = result;
                    result.Children.push(child);
                }
            }
            if (result.Children.length === 0) {
                return null;
            }
        }
        return result;
    }
}

export { TradeViewModel, TradeModel };