/**
 * 状态实例核心实现
 */ 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 - 状态值类型
 */ export class StateInstance {
    /**
   * 获取当前状态值
   */ get() {
        if (this.destroyed) {
            throw new Error(`StateInstance ${String(this.id)} has been destroyed`);
        }
        return this.value;
    }
    /**
   * 更新状态值
   */ set(valueOrUpdater) {
        if (this.destroyed) {
            throw new Error(`StateInstance ${String(this.id)} has been destroyed`);
        }
        const previousValue = this.value;
        let newValue;
        // 处理函数式更新
        if (typeof valueOrUpdater === 'function') {
            newValue = valueOrUpdater(this.value);
        } else {
            newValue = valueOrUpdater;
        }
        // 应用中间件
        let processedValue = newValue;
        for (const middleware of this.middleware){
            processedValue = middleware(processedValue, previousValue);
        }
        // 如果值没有变化，跳过更新
        if (processedValue === this.value) {
            return;
        }
        this.value = processedValue;
        if (this.devMode) {
            console.debug(`[StateInstance] Updated: ${String(this.id)}`, {
                previous: previousValue,
                current: this.value
            });
        }
        // 通知所有订阅者
        this.notifySubscribers(this.value, previousValue);
    }
    /**
   * 订阅状态变化
   */ subscribe(subscriber) {
        if (this.destroyed) {
            throw new Error(`StateInstance ${String(this.id)} has been destroyed`);
        }
        this.subscribers.add(subscriber);
        if (this.devMode) {
            console.debug(`[StateInstance] Subscribed: ${String(this.id)}`, {
                subscriberCount: this.subscribers.size
            });
        }
        // 返回取消订阅函数
        return ()=>{
            this.subscribers.delete(subscriber);
            if (this.devMode) {
                console.debug(`[StateInstance] Unsubscribed: ${String(this.id)}`, {
                    subscriberCount: this.subscribers.size
                });
            }
        };
    }
    /**
   * 获取状态实例标识符
   */ getId() {
        return this.id;
    }
    /**
   * 获取状态作用域
   */ getScope() {
        return this.scope;
    }
    /**
   * 销毁状态实例
   */ destroy() {
        if (this.destroyed) {
            return;
        }
        this.destroyed = true;
        this.subscribers.clear();
        this.value = undefined;
        if (this.devMode) {
            console.debug(`[StateInstance] Destroyed: ${String(this.id)}`);
        }
    }
    /**
   * 检查状态实例是否已销毁
   */ isDestroyed() {
        return this.destroyed;
    }
    /**
   * 通知所有订阅者
   */ notifySubscribers(value, previous) {
        // 使用数组副本避免在迭代过程中修改集合
        const subscribers = Array.from(this.subscribers);
        for (const subscriber of subscribers){
            try {
                subscriber(value, previous);
            } catch (error) {
                console.error(`[StateInstance] Error in subscriber for ${String(this.id)}:`, error);
            }
        }
    }
    /**
   * 构造函数
   * 
   * @param initialValue - 初始状态值
   * @param options - 状态实例配置选项
   */ constructor(initialValue, options = {}){
        /**
   * 状态实例标识符
   */ _define_property(this, "id", void 0);
        /**
   * 状态作用域
   */ _define_property(this, "scope", void 0);
        /**
   * 当前状态值
   */ _define_property(this, "value", void 0);
        /**
   * 订阅者集合
   */ _define_property(this, "subscribers", new Set());
        /**
   * 是否已销毁
   */ _define_property(this, "destroyed", false);
        /**
   * 开发模式标志
   */ _define_property(this, "devMode", void 0);
        /**
   * 中间件函数数组
   */ _define_property(this, "middleware", void 0);
        this.value = initialValue;
        this.id = options.id ?? Symbol('StateInstance');
        this.scope = options.scope;
        this.devMode = options.devMode ?? false;
        this.middleware = options.middleware ?? [];
        if (this.devMode) {
            console.debug(`[StateInstance] Created: ${String(this.id)}`, {
                scope: this.scope,
                initialValue: this.value
            });
        }
    }
}

//# sourceMappingURL=StateInstance.js.map