/**
 * 状态注册表实现
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "StateRegistry", {
    enumerable: true,
    get: function() {
        return StateRegistry;
    }
});
const _StateScope = require("./StateScope");
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;
}
let StateRegistry = class StateRegistry {
    /**
   * 注册状态实例
   */ register(instance) {
        const id = instance.getId();
        if (this.instances.has(id)) {
            console.warn(`[StateRegistry] Instance with id ${String(id)} already exists, replacing...`);
        }
        this.instances.set(id, instance);
        // 如果实例有作用域，注册到作用域管理器
        const scope = instance.getScope();
        if (scope) {
            this.scopeManager.register(instance, scope);
        }
    }
    /**
   * 注销状态实例
   */ unregister(id) {
        const instance = this.instances.get(id);
        if (!instance) {
            return;
        }
        // 从作用域管理器注销
        this.scopeManager.unregister(instance);
        // 销毁实例
        instance.destroy();
        // 从注册表移除
        this.instances.delete(id);
    }
    /**
   * 根据标识符获取状态实例
   */ getInstance(id) {
        return this.instances.get(id);
    }
    /**
   * 根据作用域获取所有状态实例
   */ getInstancesByScope(scope) {
        return this.scopeManager.getInstances(scope);
    }
    /**
   * 获取所有状态实例
   */ getAllInstances() {
        return Array.from(this.instances.values());
    }
    /**
   * 销毁指定作用域的所有状态实例
   */ destroyScope(scope) {
        const instances = this.scopeManager.getInstances(scope);
        // 销毁所有实例
        for (const instance of instances){
            instance.destroy();
            this.instances.delete(instance.getId());
        }
        // 从作用域管理器清理
        this.scopeManager.destroyScope(scope);
    }
    /**
   * 销毁所有状态实例
   */ destroyAll() {
        // 销毁所有实例
        for (const instance of this.instances.values()){
            instance.destroy();
        }
        // 清理所有映射
        this.instances.clear();
        this.scopeManager.destroyAll();
    }
    /**
   * 获取注册表中的实例数量
   */ size() {
        return this.instances.size;
    }
    /**
   * 构造函数
   */ constructor(){
        /**
   * 状态实例映射表（ID -> Instance）
   */ _define_property(this, "instances", new Map());
        /**
   * 作用域管理器
   */ _define_property(this, "scopeManager", void 0);
        this.scopeManager = new _StateScope.StateScopeManager();
    }
};

//# sourceMappingURL=StateRegistry.js.map