import { reactive, watch } from 'vue';

// 定义一个接口，用于表示属性更改事件处理函数
type PropertyChangedEventHandler = (propertyName: string) => void;

// 定义 NotificationObject 类
export class NotificationObject {
    private _properties: { [key: string]: any };
    public PropertyChanged: PropertyChangedEventHandler | null = null;

    constructor() {
        // 使用 Vue 的 reactive 创建响应式对象
        this._properties = reactive({});
        // 监听属性变化
        watch(
            () => this._properties,
            (newValue, oldValue, onCleanup) => {
                const changedProperties = Object.keys(newValue).filter(
                    (key) => newValue[key] !== oldValue[key]
                );
                changedProperties.forEach((propertyName) => {
                    if (this.PropertyChanged) {
                        this.PropertyChanged(propertyName);
                    }
                });
            },
            { deep: true }
        );
    }

    // 获取属性值
    public getProperty(propertyName: string): any {
        return this._properties[propertyName];
    }

    // 设置属性值
    public setProperty(propertyName: string, value: any): void {
        this._properties[propertyName] = value;
    }

    // 触发属性更改事件
    public RaisePropertyChanged(propertyName: string): void {
        if (this.PropertyChanged) {
            this.PropertyChanged(propertyName);
        }
    }
}