"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadIDEWindow = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_webview_1 = require("@opensumi/ide-webview");
const sumi_1 = require("../../common/sumi");
// 与 MainThreadWindow 做一下区分，用于拓展 sumi 下的ideWindow API
let MainThreadIDEWindow = class MainThreadIDEWindow extends ide_core_browser_1.Disposable {
    constructor(rpcProtocol) {
        super();
        this.rpcProtocol = rpcProtocol;
        // 当前仅支持单个浮动窗口管理及通信
        this._plainWebviewWindowMap = new Map();
        this._proxy = this.rpcProtocol.getProxy(sumi_1.ExtHostSumiAPIIdentifier.ExtHostIDEWindow);
        // 这段逻辑主要是为了在窗口刷新前清理新建webview窗口带来的副作用，可能不是最佳方案
        // 尝试全局卸载过插件副作用，但如主题这类配置的清理，会导致在刷新前做多余的事情
        // 表现为，刷新时主题被重置，后刷新，与预期表现不符
        window.onbeforeunload = () => {
            this.dispose();
        };
    }
    async $createWebviewWindow(webviewId, options, env) {
        let window;
        if (this._plainWebviewWindowMap.has(webviewId)) {
            window = this._plainWebviewWindowMap.get(webviewId);
        }
        else {
            window = this.webviewService.createWebviewWindow(options, env);
            if (window) {
                this.disposables.push(window.onMessage((event) => {
                    this._proxy.$postMessage(webviewId, event);
                }));
                this.disposables.push(window.onClosed((event) => {
                    this._proxy.$dispatchClosed(webviewId);
                    // 清理关闭的窗口模拟器
                    this.$destroy(webviewId);
                }));
                this.disposables.push(window);
                this._plainWebviewWindowMap.set(webviewId, window);
            }
        }
        await window.ready;
        return {
            windowId: window.windowId,
            webContentsId: window.webContentsId,
        };
    }
    async $show(webviewId) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.show();
        }
    }
    async $hide(webviewId) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.hide();
        }
    }
    async $setSize(webviewId, size) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.setSize(size);
        }
    }
    async $postMessage(webviewId, message) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.postMessage(message);
        }
    }
    async $setAlwaysOnTop(webviewId, flag) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.setAlwaysOnTop(flag);
        }
    }
    async $loadURL(webviewId, url) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.loadURL(url);
        }
    }
    async $destroy(webviewId) {
        if (this._plainWebviewWindowMap.has(webviewId)) {
            const window = this._plainWebviewWindowMap.get(webviewId);
            window === null || window === void 0 ? void 0 : window.dispose();
            this._plainWebviewWindowMap.delete(webviewId);
        }
    }
};
exports.MainThreadIDEWindow = MainThreadIDEWindow;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_webview_1.IWebviewService),
    tslib_1.__metadata("design:type", Object)
], MainThreadIDEWindow.prototype, "webviewService", void 0);
exports.MainThreadIDEWindow = MainThreadIDEWindow = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadIDEWindow);
//# sourceMappingURL=main.thread.window.js.map