"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionManagementService = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_file_service_1 = require("@opensumi/ide-file-service");
const common_1 = require("../common");
const contributes_1 = require("./sumi/contributes");
const types_1 = require("./types");
const contributes_2 = require("./vscode/contributes");
/**
 * 为插件市场面板提供数据/交互
 */
let ExtensionManagementService = class ExtensionManagementService extends ide_core_common_1.WithEventBus {
    /**
     * 通过 extensionPath 获取插件实例序列化数据及从 node 层获取的 extraMetadata
     */
    async getExtensionProps(extensionPath, extraMetaData) {
        const extensionMetaData = await this.extensionNodeClient.getExtension(extensionPath, (0, ide_core_common_1.getLanguageId)(), extraMetaData);
        if (extensionMetaData) {
            const extension = this.getExtensionByPath(extensionPath);
            if (extension) {
                return {
                    ...extension.toJSON(),
                    extraMetadata: extensionMetaData.extraMetadata,
                };
            }
        }
    }
    /**
     * 通过 extensionPath 获取插件实例
     */
    getExtensionByPath(extensionPath) {
        return this.extInstanceManagementService.getExtensionInstances().find((ext) => extensionPath === ext.path);
    }
    /**
     * 通过 extension id 获取插件实例
     */
    getExtensionByExtId(extensionId) {
        return this.extInstanceManagementService.getExtensionInstances().find((ext) => extensionId === ext.id);
    }
    async postChangedExtension(_upgrade, path, _oldExtensionPath) {
        const { upgrade, extensionPath, oldExtensionPath, isBuiltin } = typeof _upgrade === 'boolean'
            ? {
                upgrade: _upgrade,
                extensionPath: path,
                oldExtensionPath: _oldExtensionPath,
                isBuiltin: false,
            }
            : _upgrade;
        // 如果已经加载了一个 id 一样的插件，则不激活当前插件
        const sameExtension = this.extInstanceManagementService.getExtensionInstanceByPath(extensionPath);
        if (sameExtension) {
            this.logger.warn(`Extension ${sameExtension.id} already exists, skip activate`);
            return;
        }
        const extensionInstance = await this.extInstanceManagementService.createExtensionInstance(extensionPath, !!isBuiltin);
        if (extensionInstance) {
            if (upgrade) {
                this.disableExtension(oldExtensionPath);
            }
            return await this.enableExtension(extensionInstance);
        }
    }
    /**
     * 启用插件
     */
    async postEnableExtension(extensionPath) {
        const extensionInstance = await this.extInstanceManagementService.getExtensionInstanceByPath(extensionPath);
        if (extensionInstance) {
            return await this.enableExtension(extensionInstance);
        }
    }
    /**
     * 禁用插件
     */
    async postDisableExtension(extensionPath) {
        return this.disableExtension(extensionPath);
    }
    /**
     * 卸载插件
     */
    async postUninstallExtension(extensionPath) {
        return this.uninstallExtension(extensionPath);
    }
    /**
     * @deprecated 建议直接用 AbstractExtInstanceManagementService#getExtensionInstances 后自行 map#toJSON 即可
     */
    getAllExtensionJson() {
        // 备注: 这里的 getAllExtensions 跟 extensionMap 没有关系
        // 且 extensionMap 只在 start/restart 时设值
        // await this.getAllExtensions();
        return this.extInstanceManagementService.getExtensionInstances().map((extension) => extension.toJSON());
    }
    /**
     * 通过 extensionPath 来禁用插件
     */
    disableExtension(extensionPath) {
        const extension = this.extInstanceManagementService.getExtensionInstanceByPath(extensionPath);
        if (extension) {
            extension.disable();
            this.extInstanceManagementService.deleteExtensionInstanceByPath(extensionPath);
        }
    }
    /**
     * 激活插件实例
     */
    async enableExtension(extension) {
        this.extInstanceManagementService.addExtensionInstance(extension);
        extension.enable();
        await extension.initialize();
        this.eventBus.fire(new types_1.ExtensionDidEnabledEvent(extension.toJSON()));
        this.sumiContributesService.register(extension.id, extension.packageJSON.sumiContributes || {});
        this.contributesService.register(extension.id, extension.contributes);
        this.sumiContributesService.initialize();
        this.contributesService.initialize();
    }
    /**
     * 删除插件文件
     */
    async removeExtension(extensionPath) {
        try {
            await this.fileService.delete(ide_core_common_1.URI.file(extensionPath).toString());
            return true;
        }
        catch (err) {
            this.logger.error(err);
            return false;
        }
    }
    /**
     * 通过 extensionPath 来卸载插件
     */
    async uninstallExtension(extensionPath) {
        const oldExtension = this.extInstanceManagementService.getExtensionInstanceByPath(extensionPath);
        if (oldExtension) {
            oldExtension.dispose();
            this.extInstanceManagementService.deleteExtensionInstanceByPath(extensionPath);
        }
        await this.removeExtension(extensionPath);
        this.eventBus.fire(new types_1.ExtensionDidUninstalledEvent());
    }
};
exports.ExtensionManagementService = ExtensionManagementService;
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], ExtensionManagementService.prototype, "extInstanceManagementService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(common_1.ExtensionNodeServiceServerPath),
    tslib_1.__metadata("design:type", Object)
], ExtensionManagementService.prototype, "extensionNodeClient", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(contributes_2.VSCodeContributesServiceToken),
    tslib_1.__metadata("design:type", contributes_2.VSCodeContributesService)
], ExtensionManagementService.prototype, "contributesService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(contributes_1.SumiContributionsServiceToken),
    tslib_1.__metadata("design:type", contributes_1.SumiContributionsService)
], ExtensionManagementService.prototype, "sumiContributesService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_file_service_1.IFileServiceClient),
    tslib_1.__metadata("design:type", Object)
], ExtensionManagementService.prototype, "fileService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], ExtensionManagementService.prototype, "logger", void 0);
exports.ExtensionManagementService = ExtensionManagementService = tslib_1.__decorate([
    (0, di_1.Injectable)()
], ExtensionManagementService);
//# sourceMappingURL=extension-management.service.js.map