"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtInstanceManagementService = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const common_1 = require("../common");
const extension_1 = require("./extension");
/**
 * 管理 Extension 实例相关数据
 * 负责插件实例的管理
 * - 及插件的激活/禁用等
 */
let ExtInstanceManagementService = class ExtInstanceManagementService extends ide_core_browser_1.Disposable {
    constructor() {
        super();
        this.onDidChangeEmitter = new ide_core_common_1.Emitter();
        this.extensionMap = new Map();
        this._whenReady = this.init();
    }
    async init() {
        this.workspaceStorage = await this.storageProvider(ide_core_browser_1.STORAGE_NAMESPACE.EXTENSIONS);
        this.globalStorage = await this.storageProvider(ide_core_browser_1.STORAGE_NAMESPACE.GLOBAL_EXTENSIONS);
    }
    get whenReady() {
        return this._whenReady;
    }
    get onDidChange() {
        return this.onDidChangeEmitter.event;
    }
    dispose() {
        this.disposeExtensionInstances();
    }
    getExtensionInstances() {
        return Array.from(this.extensionMap.values());
    }
    disposeExtensionInstances() {
        for (const extensionInstance of this.extensionMap.values()) {
            extensionInstance.dispose();
        }
        this.extensionMap = new Map();
    }
    disposeExtensionInstancesByPath(paths) {
        paths.forEach((path) => {
            var _a;
            (_a = this.extensionMap.get(path)) === null || _a === void 0 ? void 0 : _a.dispose();
        });
    }
    resetExtensionInstances() {
        for (const extensionInstance of this.extensionMap.values()) {
            extensionInstance.reset();
        }
    }
    getExtensionInstanceByPath(extensionPath) {
        return this.extensionMap.get(extensionPath);
    }
    getExtensionInstanceByExtId(extensionId) {
        for (const extension of this.extensionMap.values()) {
            if (extension.id === extensionId) {
                return extension;
            }
        }
    }
    deleteExtensionInstanceByPath(extensionPath) {
        this.extensionMap.delete(extensionPath);
        this.onDidChangeEmitter.fire();
    }
    addExtensionInstance(extension) {
        // extension.path 就是 extensionPath
        this.extensionMap.set(extension.path, extension);
        this.onDidChangeEmitter.fire();
    }
    /**
     * 检查插件是否激活
     */
    async checkExtensionEnable(extension) {
        await this.whenReady;
        // 全局默认为启用
        const globalEnableFlag = this.globalStorage.get(extension.extensionId, common_1.EXTENSION_ENABLE.ENABLE);
        // 如果 workspace 未设置则读取全局配置
        return this.workspaceStorage.get(extension.extensionId, globalEnableFlag) === common_1.EXTENSION_ENABLE.ENABLE;
    }
    async createExtensionInstance(extensionPathOrMetaData, isBuiltin, isDevelopment) {
        const extensionMetadata = typeof extensionPathOrMetaData === 'string'
            ? await this.extensionNodeClient.getExtension(extensionPathOrMetaData, (0, ide_core_common_1.getLanguageId)(), {})
            : extensionPathOrMetaData;
        if (!extensionMetadata) {
            return;
        }
        return this.injector.get(extension_1.Extension, [
            extensionMetadata,
            await this.checkExtensionEnable(extensionMetadata),
            isBuiltin ||
                (this.appConfig.extensionDir ? extensionMetadata.realPath.startsWith(this.appConfig.extensionDir) : false),
            !!isDevelopment,
        ]);
    }
    /**
     * 判断插件是否为内置插件
     */
    checkIsBuiltin(extensionMetaData) {
        const extensionCandidate = this.getExtensionCandidateByPath(extensionMetaData.realPath);
        // 1. 通过路径判决是否是内置插件
        // 2. candidate 是否有  isBuiltin 标识符
        const isBuiltin = (this.appConfig.extensionDir ? extensionMetaData.realPath.startsWith(this.appConfig.extensionDir) : false) ||
            (extensionCandidate ? extensionCandidate.isBuiltin : false);
        return isBuiltin;
    }
    /**
     * 判断插件是否为开发模式
     */
    checkIsDevelopment(extensionMetaData) {
        const extensionCandidate = this.getExtensionCandidateByPath(extensionMetaData.realPath);
        const isDevelopment = !!(extensionCandidate === null || extensionCandidate === void 0 ? void 0 : extensionCandidate.isDevelopment);
        return isDevelopment;
    }
    /**
     * @param realPath extension path
     */
    getExtensionCandidateByPath(realPath) {
        return (this.appConfig.extensionCandidate &&
            this.appConfig.extensionCandidate.find((extension) => extension.path === realPath));
    }
};
exports.ExtInstanceManagementService = ExtInstanceManagementService;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.StorageProvider),
    tslib_1.__metadata("design:type", Function)
], ExtInstanceManagementService.prototype, "storageProvider", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.AppConfig),
    tslib_1.__metadata("design:type", Object)
], ExtInstanceManagementService.prototype, "appConfig", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(di_1.INJECTOR_TOKEN),
    tslib_1.__metadata("design:type", di_1.Injector)
], ExtInstanceManagementService.prototype, "injector", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(common_1.ExtensionNodeServiceServerPath),
    tslib_1.__metadata("design:type", Object)
], ExtInstanceManagementService.prototype, "extensionNodeClient", void 0);
exports.ExtInstanceManagementService = ExtInstanceManagementService = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    tslib_1.__metadata("design:paramtypes", [])
], ExtInstanceManagementService);
//# sourceMappingURL=extension-instance-management.js.map