/**
 * 派生状态实例实现
 */ function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
/**
 * 派生状态实例
 * 
 * 派生状态不直接存储值，而是基于依赖状态自动计算
 * 
 * @template T - 派生状态值类型
 * @template D - 依赖状态实例数组类型
 */ export class DerivedStateInstance {
    /**
   * 获取当前状态值
   */ get() {
        if (this.destroyed) {
            throw new Error(`DerivedStateInstance ${String(this.id)} has been destroyed`);
        }
        return this.value;
    }
    /**
   * 派生状态不支持直接设置值
   */ set(_valueOrUpdater) {
        throw new Error('Derived state cannot be set directly. Modify the dependency states instead.');
    }
    /**
   * 订阅状态变化
   */ subscribe(subscriber) {
        if (this.destroyed) {
            throw new Error(`DerivedStateInstance ${String(this.id)} has been destroyed`);
        }
        this.subscribers.add(subscriber);
        // 返回取消订阅函数
        return ()=>{
            this.subscribers.delete(subscriber);
        };
    }
    /**
   * 获取状态实例标识符
   */ getId() {
        return this.id;
    }
    /**
   * 获取状态作用域
   */ getScope() {
        return this.scope;
    }
    /**
   * 获取依赖的状态实例
   */ getDependencies() {
        return this.config.dependencies;
    }
    /**
   * 手动重新计算派生状态
   */ recompute() {
        if (this.destroyed || this.recomputing) {
            return;
        }
        this.recomputing = true;
        try {
            // 获取所有依赖状态的当前值
            const dependencyValues = this.config.dependencies.map((dep)=>dep.get());
            // 计算新的派生值
            const newValue = this.config.derive(dependencyValues);
            // 检查值是否变化
            const hasChanged = this.config.equalityCheck ? !this.config.equalityCheck(this.value, newValue) : this.value !== newValue;
            if (hasChanged) {
                const previousValue = this.value;
                this.value = newValue;
                // 通知所有订阅者
                this.notifySubscribers(this.value, previousValue);
            }
        } catch (error) {
            console.error(`[DerivedStateInstance] Error recomputing ${String(this.id)}:`, error);
        } finally{
            this.recomputing = false;
        }
    }
    /**
   * 销毁状态实例
   */ destroy() {
        if (this.destroyed) {
            return;
        }
        this.destroyed = true;
        // 取消所有依赖订阅
        for (const unsubscribe of this.dependencyUnsubscribes){
            unsubscribe();
        }
        this.dependencyUnsubscribes.length = 0;
        // 清理订阅者
        this.subscribers.clear();
    }
    /**
   * 检查状态实例是否已销毁
   */ isDestroyed() {
        return this.destroyed;
    }
    /**
   * 设置依赖状态订阅
   */ setupDependencies() {
        for (const dependency of this.config.dependencies){
            const unsubscribe = dependency.subscribe(()=>{
                // 当依赖状态变化时，重新计算派生状态
                this.recompute();
            });
            this.dependencyUnsubscribes.push(unsubscribe);
        }
    }
    /**
   * 通知所有订阅者
   */ notifySubscribers(value, previous) {
        const subscribers = Array.from(this.subscribers);
        for (const subscriber of subscribers){
            try {
                subscriber(value, previous);
            } catch (error) {
                console.error(`[DerivedStateInstance] Error in subscriber for ${String(this.id)}:`, error);
            }
        }
    }
    /**
   * 构造函数
   * 
   * @param config - 派生状态配置
   * @param options - 状态实例配置选项
   */ constructor(config, options = {}){
        /**
   * 状态实例标识符
   */ _define_property(this, "id", void 0);
        /**
   * 状态作用域
   */ _define_property(this, "scope", void 0);
        /**
   * 派生状态配置
   */ _define_property(this, "config", void 0);
        /**
   * 当前派生值
   */ _define_property(this, "value", void 0);
        /**
   * 订阅者集合
   */ _define_property(this, "subscribers", new Set());
        /**
   * 依赖状态的取消订阅函数数组
   */ _define_property(this, "dependencyUnsubscribes", []);
        /**
   * 是否已销毁
   */ _define_property(this, "destroyed", false);
        /**
   * 是否正在重新计算
   */ _define_property(this, "recomputing", false);
        this.config = config;
        this.id = options.id ?? Symbol('DerivedStateInstance');
        this.scope = options.scope;
        // 订阅所有依赖状态的变化
        this.setupDependencies();
        // 初始计算
        this.recompute();
    }
}

//# sourceMappingURL=DerivedStateInstance.js.map