"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtHostPlainWebview = exports.PlainWebviewHandle = exports.ExtHostWebview = void 0;
exports.createWebviewApi = createWebviewApi;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
const { join } = ide_core_common_1.path;
class ExtHostWebview {
    constructor(rpcProtocol, webviewIdPrefix = 'node') {
        this.rpcProtocol = rpcProtocol;
        this.webviewIdPrefix = webviewIdPrefix;
        this.handles = new Map();
        this._extHostPlainWebviewId = 1;
        this.rpcProtocol = rpcProtocol;
        this._proxy = this.rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadWebview);
    }
    getWebviewHandle(id) {
        if (!this.handles.has(id)) {
            this.handles.set(id, new PlainWebviewHandle(id, this._proxy));
            this._proxy.$connectPlainWebview(id);
        }
        return this.handles.get(id);
    }
    createPlainWebview(title, iconPath) {
        const id = this.webviewIdPrefix + '_webview_' + this._extHostPlainWebviewId++;
        const webview = new ExtHostPlainWebview(id, this._proxy, title, iconPath);
        this.handles.set(id, webview);
        webview.addDispose({
            dispose: () => {
                this.handles.delete(id);
            },
        });
        return webview;
    }
    $acceptMessage(id, message) {
        if (this.handles.has(id)) {
            this.handles.get(id).onMessageEmitter.fire(message);
        }
    }
}
exports.ExtHostWebview = ExtHostWebview;
class PlainWebviewHandle extends ide_core_common_1.Disposable {
    constructor(id, proxy) {
        super();
        this.id = id;
        this.proxy = proxy;
        this.onMessageEmitter = new ide_core_common_1.Emitter();
        this.onMessage = this.onMessageEmitter.event;
    }
    postMessage(message) {
        return this.proxy.$postMessageToPlainWebview(this.id, message);
    }
    async setPartition(value) {
        await this.proxy.$setPlainWebviewPartition(this.id, value);
    }
    async loadUrl(url) {
        this.proxy.$plainWebviewLoadUrl(this.id, url);
    }
}
exports.PlainWebviewHandle = PlainWebviewHandle;
class ExtHostPlainWebview extends PlainWebviewHandle {
    constructor(id, proxy, title, iconPath) {
        super(id, proxy);
        this._ready = this.proxy.$createPlainWebview(id, title, iconPath);
        this.addDispose({
            dispose: () => {
                this.proxy.$disposePlainWebview(id);
            },
        });
    }
    async reveal(groupIndex) {
        await this._ready;
        this.proxy.$revealPlainWebview(this.id, groupIndex);
    }
    async loadUrl(url) {
        await this._ready;
        this.proxy.$plainWebviewLoadUrl(this.id, url);
    }
}
exports.ExtHostPlainWebview = ExtHostPlainWebview;
function createWebviewApi(extension, kaitianExtHostWebview) {
    return {
        getPlainWebviewHandle: (id) => kaitianExtHostWebview.getWebviewHandle(id),
        createPlainWebview: (title, iconPath) => {
            if (iconPath) {
                iconPath = join(extension.realPath, iconPath);
            }
            return kaitianExtHostWebview.createPlainWebview(title, iconPath);
        },
    };
}
//# sourceMappingURL=ext.host.webview.js.map