"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadUrls = 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 vscode_1 = require("../../../common/vscode");
const types_1 = require("../../types");
class ExtennsionUrlHandler {
    constructor(proxy, handle, extensionId) {
        this.proxy = proxy;
        this.handle = handle;
        this.extensionId = extensionId;
    }
    async handleURL(uri) {
        // 要执行的 uri 的authority 必须是当前 extension Id
        if (this.extensionId !== uri.authority) {
            return false;
        }
        await this.proxy.$handleExternalUri(this.handle, uri.codeUri);
        return true;
    }
}
let ExtensionOpener = class ExtensionOpener {
    constructor() {
        this.extensionUrlHandlers = new Map();
    }
    async open(uri) {
        let result = false;
        const extensionId = uri.authority;
        const handler = this.extensionUrlHandlers.get(extensionId);
        // 如果没有处理函数，则继续走下一个 opener 逻辑
        if (handler) {
            try {
                result = await handler.handleURL(uri);
            }
            catch (e) {
                this.logger.error(e);
            }
        }
        await this.activationEventService.fireEvent('onUri', extensionId);
        return result;
    }
    handleScheme(scheme) {
        return scheme === this.config.uriScheme;
    }
    registerExtensionHandler(handle, extensionId, proxy) {
        const extensionUrlHandler = new ExtennsionUrlHandler(proxy, handle, extensionId);
        this.extensionUrlHandlers.set(extensionId, extensionUrlHandler);
        return {
            dispose: () => {
                this.extensionUrlHandlers.delete(extensionId);
            },
        };
    }
    dispose() {
        this.extensionUrlHandlers.clear();
    }
};
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], ExtensionOpener.prototype, "logger", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.AppConfig),
    tslib_1.__metadata("design:type", Object)
], ExtensionOpener.prototype, "config", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(),
    tslib_1.__metadata("design:type", types_1.IActivationEventService)
], ExtensionOpener.prototype, "activationEventService", void 0);
ExtensionOpener = tslib_1.__decorate([
    (0, di_1.Injectable)()
], ExtensionOpener);
let MainThreadUrls = class MainThreadUrls {
    constructor(rpcProtocol) {
        this.handlers = new Map();
        this.proxy = rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostUrls);
        this.openerService.registerOpener(this.extensionOpener);
    }
    async $registerUriHandler(handle, extensionId) {
        this.handlers.set(handle, this.extensionOpener.registerExtensionHandler(handle, extensionId, this.proxy));
    }
    async $unregisterUriHandler(handle) {
        const disposer = this.handlers.get(handle);
        if (disposer) {
            disposer.dispose();
            this.handlers.delete(handle);
        }
    }
    dispose() {
        this.handlers.clear();
        this.extensionOpener.dispose();
    }
};
exports.MainThreadUrls = MainThreadUrls;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IOpenerService),
    tslib_1.__metadata("design:type", Object)
], MainThreadUrls.prototype, "openerService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ExtensionOpener),
    tslib_1.__metadata("design:type", ExtensionOpener)
], MainThreadUrls.prototype, "extensionOpener", void 0);
exports.MainThreadUrls = MainThreadUrls = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(ide_connection_1.IRPCProtocol)),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadUrls);
//# sourceMappingURL=main.thread.urls.js.map