/**
 * 状态管理器主类
 * 
 * 这是状态管理器的核心入口，提供统一的状态管理 API
 */ 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;
}
import { AdapterFactory } from "./adapters/AdapterFactory";
import { StateRegistry } from "./core/StateRegistry";
import { DerivedStateInstance } from "./core/DerivedStateInstance";
/**
 * 状态管理器实现
 */ export class StateManager {
    /**
   * 创建状态实例
   */ createState(initialValue, options) {
        if (this.destroyed) {
            throw new Error('StateManager has been destroyed');
        }
        // 合并默认作用域
        const mergedOptions = {
            ...options,
            scope: options?.scope ?? this.defaultScope,
            devMode: options?.devMode ?? this.devMode
        };
        // 使用适配器创建状态实例
        const instance = this.adapter.createInstance(initialValue, mergedOptions);
        // 如果启用了注册表，注册实例
        if (this.registry) {
            // 类型转换：注册表接受 IStateInstance<unknown>，但实际类型是 IStateInstance<T>
            this.registry.register(instance);
        }
        return instance;
    }
    /**
   * 创建派生状态
   */ createDerivedState(config, options) {
        if (this.destroyed) {
            throw new Error('StateManager has been destroyed');
        }
        const mergedOptions = {
            ...options,
            scope: options?.scope ?? this.defaultScope
        };
        const instance = new DerivedStateInstance(config, mergedOptions);
        // 如果启用了注册表，注册实例
        if (this.registry) {
            this.registry.register(instance);
        }
        return instance;
    }
    /**
   * 获取状态实例（如果启用了注册表）
   */ getState(id) {
        if (!this.registry) {
            throw new Error('Registry is not enabled. Enable it in StateManagerOptions.');
        }
        return this.registry.getInstance(id);
    }
    /**
   * 根据作用域获取所有状态实例
   */ getStatesByScope(scope) {
        if (!this.registry) {
            throw new Error('Registry is not enabled. Enable it in StateManagerOptions.');
        }
        return this.registry.getInstancesByScope(scope);
    }
    /**
   * 获取所有状态实例
   */ getAllStates() {
        if (!this.registry) {
            throw new Error('Registry is not enabled. Enable it in StateManagerOptions.');
        }
        return this.registry.getAllInstances();
    }
    /**
   * 销毁指定作用域的所有状态实例
   */ destroyScope(scope) {
        if (!this.registry) {
            throw new Error('Registry is not enabled. Enable it in StateManagerOptions.');
        }
        this.registry.destroyScope(scope);
    }
    /**
   * 销毁状态管理器
   */ destroy() {
        if (this.destroyed) {
            return;
        }
        this.destroyed = true;
        // 销毁注册表中的所有实例
        if (this.registry) {
            this.registry.destroyAll();
        }
        // 销毁适配器
        this.adapter.destroy();
        if (this.devMode) {
            console.debug('[StateManager] Destroyed');
        }
    }
    /**
   * 检查状态管理器是否已销毁
   */ isDestroyed() {
        return this.destroyed;
    }
    /**
   * 获取当前使用的适配器
   */ getAdapter() {
        return this.adapter;
    }
    /**
   * 获取注册表（如果启用）
   */ getRegistry() {
        return this.registry;
    }
    /**
   * 构造函数
   * 
   * @param options - 状态管理器配置选项
   */ constructor(options = {}){
        /**
   * 默认适配器
   */ _define_property(this, "adapter", void 0);
        /**
   * 状态注册表（如果启用）
   */ _define_property(this, "registry", void 0);
        /**
   * 默认作用域
   */ _define_property(this, "defaultScope", void 0);
        /**
   * 是否启用开发模式
   */ _define_property(this, "devMode", void 0);
        /**
   * 是否已销毁
   */ _define_property(this, "destroyed", false);
        this.adapter = options.defaultAdapter ?? AdapterFactory.getDefaultAdapter();
        this.defaultScope = options.defaultScope;
        this.devMode = options.devMode ?? false;
        this.registry = options.enableRegistry !== false ? new StateRegistry() : null;
        if (this.devMode) {
            console.debug('[StateManager] Initialized', {
                adapter: this.adapter.getName(),
                registryEnabled: !!this.registry,
                defaultScope: this.defaultScope
            });
        }
    }
}

//# sourceMappingURL=StateManager.js.map