/**
 * 应用实例管理模块
 * 支持多实例隔离
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get AppInstance () {
        return AppInstance;
    },
    get AppInstanceManager () {
        return AppInstanceManager;
    },
    get getAppInstanceManager () {
        return getAppInstanceManager;
    }
});
const _renderApp = require("./renderApp");
const _performanceTracker = require("./performanceTracker");
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 AppInstance = class AppInstance {
    /**
   * 初始化应用实例
   * 
   * @param container - React应用的挂载容器
   * @param services - 服务实例
   */ initialize(container, services) {
        if (this.initialized) {
            throw new Error(`应用实例 ${this.id} 已经初始化`);
        }
        if (this.destroyed) {
            throw new Error(`应用实例 ${this.id} 已销毁，无法重新初始化`);
        }
        this.services = services;
        this.renderer.initialize(container, this.options.rootOptions);
        this.performanceTracker = new _performanceTracker.PerformanceTracker(services.monitoring);
        this.performanceTracker.collectWebVitals();
        this.initialized = true;
    }
    /**
   * 获取渲染器
   */ getRenderer() {
        if (!this.initialized) {
            throw new Error(`应用实例 ${this.id} 未初始化`);
        }
        return this.renderer;
    }
    /**
   * 获取服务实例
   */ getServices() {
        if (!this.services) {
            throw new Error(`应用实例 ${this.id} 未初始化`);
        }
        return this.services;
    }
    /**
   * 获取性能追踪器
   */ getPerformanceTracker() {
        return this.performanceTracker;
    }
    /**
   * 获取事件总线
   */ getEventBus() {
        if (!this.services) {
            return null;
        }
        return this.services.eventBus;
    }
    /**
   * 获取启动配置
   */ getOptions() {
        return this.options;
    }
    /**
   * 是否已初始化
   */ isInitialized() {
        return this.initialized;
    }
    /**
   * 是否已销毁
   */ isDestroyed() {
        return this.destroyed;
    }
    /**
   * 销毁应用实例
   */ destroy() {
        if (this.destroyed) {
            return;
        }
        this.renderer.destroy();
        this.services = null;
        this.performanceTracker = null;
        this.destroyed = true;
    }
    constructor(id, options){
        /**
   * 实例ID
   */ _define_property(this, "id", void 0);
        /**
   * 应用渲染器
   */ _define_property(this, "renderer", void 0);
        /**
   * 服务实例
   */ _define_property(this, "services", null);
        /**
   * 性能追踪器
   */ _define_property(this, "performanceTracker", null);
        /**
   * 启动配置
   */ _define_property(this, "options", void 0);
        /**
   * 是否已初始化
   */ _define_property(this, "initialized", false);
        /**
   * 是否已销毁
   */ _define_property(this, "destroyed", false);
        this.id = id;
        this.options = options;
        this.renderer = new _renderApp.AppRenderer();
    }
};
let AppInstanceManager = class AppInstanceManager {
    /**
   * 获取单例实例
   */ static getInstance() {
        if (!AppInstanceManager.instance) {
            AppInstanceManager.instance = new AppInstanceManager();
        }
        return AppInstanceManager.instance;
    }
    /**
   * 创建应用实例
   * 
   * @param id - 实例ID，如果不提供则自动生成
   * @param options - 启动配置选项
   * @returns 应用实例
   */ createInstance(id, options = {}) {
        const instanceId = id || `app-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
        if (this.instances.has(instanceId)) {
            throw new Error(`应用实例 ${instanceId} 已存在`);
        }
        const instance = new AppInstance(instanceId, options);
        this.instances.set(instanceId, instance);
        // 如果没有默认实例，设置为默认实例
        if (!this.defaultInstanceId) {
            this.defaultInstanceId = instanceId;
        }
        return instance;
    }
    /**
   * 获取应用实例
   * 
   * @param id - 实例ID，如果不提供则返回默认实例
   * @returns 应用实例或undefined
   */ getInstance(id) {
        const instanceId = id || this.defaultInstanceId;
        if (!instanceId) {
            return undefined;
        }
        return this.instances.get(instanceId);
    }
    /**
   * 销毁应用实例
   * 
   * @param id - 实例ID，如果不提供则销毁默认实例
   */ destroyInstance(id) {
        const instanceId = id || this.defaultInstanceId;
        if (!instanceId) {
            return;
        }
        const instance = this.instances.get(instanceId);
        if (instance) {
            instance.destroy();
            this.instances.delete(instanceId);
            // 如果销毁的是默认实例，重新设置默认实例
            if (this.defaultInstanceId === instanceId) {
                const remainingInstances = Array.from(this.instances.keys());
                this.defaultInstanceId = remainingInstances.length > 0 ? remainingInstances[0] : null;
            }
        }
    }
    /**
   * 获取所有实例
   */ getAllInstances() {
        return Array.from(this.instances.values());
    }
    /**
   * 设置默认实例
   * 
   * @param id - 实例ID
   */ setDefaultInstance(id) {
        if (!this.instances.has(id)) {
            throw new Error(`应用实例 ${id} 不存在`);
        }
        this.defaultInstanceId = id;
    }
    /**
   * 获取默认实例ID
   */ getDefaultInstanceId() {
        return this.defaultInstanceId;
    }
    /**
   * 清空所有实例
   */ clear() {
        for (const instance of this.instances.values()){
            instance.destroy();
        }
        this.instances.clear();
        this.defaultInstanceId = null;
    }
    constructor(){
        _define_property(this, "instances", new Map());
        _define_property(this, "defaultInstanceId", null);
    // 私有构造函数
    }
};
_define_property(AppInstanceManager, "instance", null);
function getAppInstanceManager() {
    return AppInstanceManager.getInstance();
}

//# sourceMappingURL=AppInstance.js.map