"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtIDEWebviewWindow = exports.ExtHostIDEWindow = void 0;
exports.createWindowApiFactory = createWindowApiFactory;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const sumi_1 = require("../../../common/sumi");
class ExtHostIDEWindow {
    constructor(rpcProtocol) {
        this.rpcProtocol = rpcProtocol;
        this._windowMaps = new Map();
        this.proxy = this.rpcProtocol.getProxy(sumi_1.MainThreadSumiAPIIdentifier.MainThreadIDEWindow);
    }
    async createWebviewWindow(webviewId, options, env) {
        if (this._windowMaps.has(webviewId)) {
            const window = this._windowMaps.get(webviewId);
            window === null || window === void 0 ? void 0 : window.show();
            return window;
        }
        const windowInfo = await this.proxy.$createWebviewWindow(webviewId, options, env);
        const window = new ExtIDEWebviewWindow(webviewId, windowInfo, this.proxy, ide_core_common_1.Disposable.create(() => {
            this._windowMaps.delete(webviewId);
        }));
        this._windowMaps.set(webviewId, window);
        return window;
    }
    async $postMessage(webviewId, message) {
        if (this._windowMaps.has(webviewId)) {
            const window = this._windowMaps.get(webviewId);
            window === null || window === void 0 ? void 0 : window.dispatchMessage(message);
        }
    }
    async $dispatchClosed(webviewId) {
        if (this._windowMaps.has(webviewId)) {
            const window = this._windowMaps.get(webviewId);
            window === null || window === void 0 ? void 0 : window.dispatchClosed();
        }
    }
}
exports.ExtHostIDEWindow = ExtHostIDEWindow;
class ExtIDEWebviewWindow {
    constructor(webviewId, info, proxy, dispose) {
        this.webviewId = webviewId;
        this.info = info;
        this.proxy = proxy;
        this._onMessageEmitter = new ide_core_common_1.Emitter();
        this._onClosedEmitter = new ide_core_common_1.Emitter();
        this.disposableCollection = new ide_core_common_1.DisposableCollection();
        this.disposableCollection.push(dispose);
        this.disposableCollection.push(ide_core_common_1.Disposable.create(() => {
            this.proxy.$destroy(this.webviewId);
        }));
    }
    get webContentsId() {
        return this.info.webContentsId;
    }
    get windowId() {
        return this.info.windowId;
    }
    get onMessage() {
        return this._onMessageEmitter.event;
    }
    get onClosed() {
        return this._onClosedEmitter.event;
    }
    async show() {
        return await this.proxy.$show(this.webviewId);
    }
    async hide() {
        return await this.proxy.$hide(this.webviewId);
    }
    async postMessage(message) {
        return await this.proxy.$postMessage(this.webviewId, message);
    }
    async loadUrl(url) {
        return await this.proxy.$loadURL(this.webviewId, url);
    }
    async setSize(size) {
        return await this.proxy.$setSize(this.webviewId, size);
    }
    async setAlwaysOnTop(flag) {
        return await this.proxy.$setAlwaysOnTop(this.webviewId, flag);
    }
    dispose() {
        this.disposableCollection.dispose();
    }
    async dispatchClosed() {
        this._onClosedEmitter.fire();
    }
    async dispatchMessage(message) {
        this._onMessageEmitter.fire(message);
    }
}
exports.ExtIDEWebviewWindow = ExtIDEWebviewWindow;
function createWindowApiFactory(extHostCommands, extHostWindow) {
    return {
        reloadWindow: async () => await extHostCommands.executeCommand('reload_window'),
        createWebviewWindow: async (webviewId, options, env) => await extHostWindow.createWebviewWindow(webviewId, options, env),
    };
}
//# sourceMappingURL=ext.host.window.js.map