"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAPIFactory = createAPIFactory;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const common_1 = require("../../../common");
const sumi_1 = require("../../../common/sumi");
const vscode_1 = require("../../../common/vscode");
const ext_host_chat_impl_1 = require("./ext.host.chat.impl");
const ext_host_command_1 = require("./ext.host.command");
const ext_host_common_1 = require("./ext.host.common");
const ext_host_layout_1 = require("./ext.host.layout");
const ext_host_lifecycle_1 = require("./ext.host.lifecycle");
const ext_host_theme_1 = require("./ext.host.theme");
const ext_host_toolbar_1 = require("./ext.host.toolbar");
const ext_host_webview_1 = require("./ext.host.webview");
const ext_host_window_1 = require("./ext.host.window");
function createAPIFactory(rpcProtocol, extensionService, type, reporterEmitter, sumiApiExtenders = {}) {
    if (type === 'worker') {
        rpcProtocol.set(common_1.WorkerHostAPIIdentifier.ExtWorkerHostExtensionService, extensionService);
    }
    const extHostCommands = rpcProtocol.get(vscode_1.ExtHostAPIIdentifier.ExtHostCommands);
    const extHostEditors = rpcProtocol.get(vscode_1.ExtHostAPIIdentifier.ExtHostEditors);
    const extHostWebview = rpcProtocol.set(vscode_1.ExtHostAPIIdentifier.SumiExtHostWebview, new ext_host_webview_1.ExtHostWebview(rpcProtocol));
    const extHostLifeCycle = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostLifeCycle, new ext_host_lifecycle_1.ExtHostLifeCycle(rpcProtocol));
    const extHostLayout = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostLayout, new ext_host_layout_1.ExtHostLayout(rpcProtocol));
    const extHostTheme = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostTheme, new ext_host_theme_1.ExtHostTheme(rpcProtocol));
    const extHostCommon = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostCommon, new ext_host_common_1.ExtHostCommon(rpcProtocol));
    const extHostToolbar = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostToolbar, new ext_host_toolbar_1.ExtHostToolbarActionService(extHostCommands, extHostCommon, rpcProtocol, extensionService.logger));
    const extHostWindow = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostIDEWindow, new ext_host_window_1.ExtHostIDEWindow(rpcProtocol));
    const extHostChatAgents = rpcProtocol.set(sumi_1.ExtHostSumiAPIIdentifier.ExtHostChatAgents, new ext_host_chat_impl_1.ExtHostChatAgents(rpcProtocol));
    const externalSumiApis = Object.keys(sumiApiExtenders).reduce((acc, key) => {
        const SumiApiExtender = sumiApiExtenders[key];
        const extender = new SumiApiExtender(rpcProtocol);
        let rpcService;
        // register external api rpc protocol
        if ((0, ide_core_common_1.isFunction)(extender.createRPCService)) {
            const [identifier, service] = extender.createRPCService();
            rpcService = service;
            rpcProtocol.set(identifier, service);
        }
        acc[key] = extender.createApiFactory(rpcService);
        return acc;
    }, {});
    return (extension) => {
        const reporter = new ide_core_common_1.ReporterService(reporterEmitter, {
            extensionId: extension.extensionId,
            extensionVersion: extension.packageJSON.version,
            host: type === 'worker' ? ide_core_common_1.REPORT_HOST.WORKER : ide_core_common_1.REPORT_HOST.EXTENSION,
        });
        return {
            layout: (0, ext_host_layout_1.createLayoutAPIFactory)(extHostCommands, extHostLayout, extension),
            ideWindow: (0, ext_host_window_1.createWindowApiFactory)(extHostCommands, extHostWindow),
            webview: (0, ext_host_webview_1.createWebviewApi)(extension, extHostWebview),
            lifecycle: (0, ext_host_lifecycle_1.createLifeCycleApi)(extHostCommands, extHostLifeCycle),
            theme: (0, ext_host_theme_1.createThemeApi)(extHostTheme),
            event: (0, ext_host_common_1.createEventAPIFactory)(extHostCommands, extHostCommon, extension),
            reporter,
            commands: (0, ext_host_command_1.createCommandsApiFactory)(extHostCommands, extHostEditors, extension),
            toolbar: (0, ext_host_toolbar_1.createToolbarAPIFactory)(extension, extHostToolbar),
            chat: (0, ext_host_chat_impl_1.createChatApiFactory)(extension, extHostChatAgents),
            ...externalSumiApis,
        };
    };
}
//# sourceMappingURL=ext.host.api.impl.js.map