"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadLayout = 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_main_layout_1 = require("@opensumi/ide-main-layout");
const ide_theme_1 = require("@opensumi/ide-theme");
const sumi_1 = require("../../common/sumi");
let MainThreadLayout = class MainThreadLayout extends ide_core_browser_1.Disposable {
    constructor(rpcProtocol) {
        super();
        this.handlerMap = new Map();
        this.proxy = rpcProtocol.getProxy(sumi_1.ExtHostSumiAPIIdentifier.ExtHostLayout);
    }
    $setTitle(id, title) {
        var _a;
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.updateTitle(title);
    }
    $setIcon(id, iconPath) {
        var _a;
        const iconClass = this.iconService.fromIcon('', iconPath, ide_theme_1.IconType.Background, ide_theme_1.IconShape.Square);
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.setIconClass(iconClass);
    }
    $setSize(id, size) {
        var _a;
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.setSize(size);
    }
    $activate(id) {
        var _a;
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.activate();
    }
    $deactivate(id) {
        var _a;
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.deactivate();
    }
    $setBadge(id, badge) {
        var _a;
        (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.setBadge(badge);
    }
    async $setVisible(id, visible) {
        var _a, _b, _c, _d;
        if (visible) {
            (_a = this.getHandler(id)) === null || _a === void 0 ? void 0 : _a.show();
        }
        else {
            if ((_b = this.getHandler(id)) === null || _b === void 0 ? void 0 : _b.isActivated()) {
                (_c = this.getHandler(id)) === null || _c === void 0 ? void 0 : _c.deactivate();
            }
            (_d = this.getHandler(id)) === null || _d === void 0 ? void 0 : _d.hide();
        }
    }
    async $connectTabbar(id) {
        if (!this.handlerMap.has(id)) {
            const handle = this.layoutService.getTabbarHandler(id);
            if (handle) {
                this.bindHandleEvents(handle);
            }
            else {
                const disposer = this.eventBus.on(ide_main_layout_1.TabBarRegistrationEvent, (e) => {
                    if (e.payload.tabBarId === id) {
                        const handler = this.layoutService.getTabbarHandler(id);
                        if (handler) {
                            this.bindHandleEvents(handler);
                        }
                        disposer.dispose();
                    }
                });
                this.addDispose(disposer);
            }
        }
    }
    // 视图可能未注册到layout上，此时调用该方法返回false
    async $isAttached(id) {
        return !!this.layoutService.getTabbarHandler(id);
    }
    bindHandleEvents(handle) {
        this.handlerMap.set(handle.containerId, handle);
        handle.onActivate(() => {
            this.proxy.$acceptMessage(handle.containerId, 'activate');
        });
        handle.onInActivate(() => {
            this.proxy.$acceptMessage(handle.containerId, 'deactivate');
        });
    }
    getHandler(id) {
        const handler = this.layoutService.getTabbarHandler(id);
        if (!handler) {
            this.logger.warn(`Could not find a handler with \`${id}\``);
        }
        return handler;
    }
};
exports.MainThreadLayout = MainThreadLayout;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IMainLayoutService),
    tslib_1.__metadata("design:type", Object)
], MainThreadLayout.prototype, "layoutService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], MainThreadLayout.prototype, "iconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IEventBus),
    tslib_1.__metadata("design:type", Object)
], MainThreadLayout.prototype, "eventBus", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], MainThreadLayout.prototype, "logger", void 0);
exports.MainThreadLayout = MainThreadLayout = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadLayout);
//# sourceMappingURL=main.thread.layout.js.map