"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KaitianBrowserCommand = void 0;
exports.createBrowserCommandsApiFactory = createBrowserCommandsApiFactory;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const command_1 = require("@opensumi/ide-monaco/lib/browser/contrib/command");
const vscode_1 = require("../../common/vscode");
let KaitianBrowserCommand = class KaitianBrowserCommand {
    constructor(rpcProtocol) {
        this.rpcProtocol = rpcProtocol;
        if (this.rpcProtocol) {
            this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostCommands);
        }
    }
    /**
     * @param id commandid
     * @param extension 插件
     * @param args 参数
     * **允许前端调用的** command 都会注册到 commandRegistry
     * commandRegistry 不存在的 command，表示没有注册或该 command 是通过 extHost 配置的内置命令
     * command 在 commandRegistry 已注册，则优先通过 extHostCommand 调用，这样即可在插件进程执行鉴权逻辑
     * 否则仅在前端鉴权后执行，对于没有后端或没有 extHost 的情况下，满足执行前端命令的需求
     */
    async executeCommand(id, extension, ...args) {
        const extensionInfo = {
            id: extension.id,
            extensionId: extension.extensionId,
            isBuiltin: extension.isBuiltin,
        };
        const command = this.commandRegistry.getCommand(id);
        if (this.proxy && command) {
            return this.proxy.$executeCommandWithExtensionInfo(id, extensionInfo, ...args);
        }
        const isPermitted = this.commandRegistry.isPermittedCommand(id, extensionInfo, ...args);
        if (!isPermitted) {
            throw new Error(`Extension ${extensionInfo.id} has not permit to execute ${id}`);
        }
        try {
            return await this.monacoCommandService.executeCommand(id, ...args);
        }
        catch (e) {
            this.logger.error(e);
            throw e;
        }
    }
};
exports.KaitianBrowserCommand = KaitianBrowserCommand;
tslib_1.__decorate([
    (0, di_1.Autowired)(command_1.ICommandServiceToken),
    tslib_1.__metadata("design:type", Object)
], KaitianBrowserCommand.prototype, "monacoCommandService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.CommandRegistry),
    tslib_1.__metadata("design:type", Object)
], KaitianBrowserCommand.prototype, "commandRegistry", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], KaitianBrowserCommand.prototype, "logger", void 0);
exports.KaitianBrowserCommand = KaitianBrowserCommand = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    tslib_1.__param(0, (0, di_1.Optional)()),
    tslib_1.__metadata("design:paramtypes", [Object])
], KaitianBrowserCommand);
function createBrowserCommandsApiFactory(injector, extension, rpcProtocol) {
    const commands = injector.get(KaitianBrowserCommand, [rpcProtocol]);
    return {
        executeCommand(command, ...args) {
            return commands.executeCommand(command, extension, ...args);
        },
    };
}
//# sourceMappingURL=commands.js.map