"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtHostUrls = void 0;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
class ExtHostUrls {
    constructor(rpcProtocol) {
        this.handles = new Set();
        this.handlers = new Map();
        this._proxy = rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadUrls);
    }
    registerUriHandler(extensionId, handler) {
        if (this.handles.has(extensionId)) {
            throw new Error(`Protocol handler already registered for extension ${extensionId}`);
        }
        const handle = ExtHostUrls.HandlePool++;
        this.handles.add(extensionId);
        this.handlers.set(handle, handler);
        this._proxy.$registerUriHandler(handle, extensionId);
        return (0, ide_core_common_1.toDisposable)(() => {
            this.handles.delete(extensionId);
            this.handlers.delete(handle);
            this._proxy.$unregisterUriHandler(handle);
        });
    }
    $handleExternalUri(handle, uri) {
        const handler = this.handlers.get(handle);
        if (!handler) {
            return Promise.resolve(undefined);
        }
        try {
            handler.handleUri(ide_core_common_1.Uri.revive(uri));
        }
        catch (err) {
            (0, ide_core_common_1.onUnexpectedError)(err);
        }
        return Promise.resolve(undefined);
    }
}
exports.ExtHostUrls = ExtHostUrls;
ExtHostUrls.HandlePool = 0;
//# sourceMappingURL=ext.host.urls.js.map