"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TabbarBrowserContributionRunner = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_main_layout_1 = require("@opensumi/ide-main-layout");
const ide_theme_1 = require("@opensumi/ide-theme");
const types_1 = require("../types");
const SUPPORT_LOCATION = ['left', 'right', 'bottom', 'editor', 'toolBar'];
let TabbarBrowserContributionRunner = class TabbarBrowserContributionRunner extends types_1.AbstractSumiBrowserContributionRunner {
    run(params) {
        const disposer = new ide_core_common_1.Disposable();
        if (this.contribution.left) {
            this.contribution.left.view.forEach((view) => {
                var _a;
                disposer.addDispose(this.registerTabBar(view, params, (_a = this.contribution.left) === null || _a === void 0 ? void 0 : _a.type, 'left'));
            });
        }
        if (this.contribution.right) {
            this.contribution.right.view.forEach((view) => {
                var _a;
                disposer.addDispose(this.registerTabBar(view, params, (_a = this.contribution.right) === null || _a === void 0 ? void 0 : _a.type, 'right'));
            });
        }
        if (this.contribution.bottom) {
            this.contribution.bottom.view.forEach((view) => {
                var _a;
                disposer.addDispose(this.registerTabBar(view, params, (_a = this.contribution.bottom) === null || _a === void 0 ? void 0 : _a.type, 'bottom'));
            });
        }
        Object.keys(this.contribution).forEach((location) => {
            var _a;
            if (SUPPORT_LOCATION.indexOf(location) === -1) {
                ((_a = this.contribution[location]) === null || _a === void 0 ? void 0 : _a.view).forEach((view) => {
                    disposer.addDispose(this.registerTabBar(view, params, 'replace'));
                });
            }
        });
        return disposer;
    }
    registerTabBar(view, runtimeParams, kind = 'add', position) {
        const { extendProtocol, extendService } = runtimeParams.getExtensionExtendService(this.extension, view.id);
        const containerId = `${this.extension.id}:${view.id}`;
        const initialProps = {
            kaitianExtendService: extendService,
            kaitianExtendSet: extendProtocol,
            sumiExtendService: extendService,
            sumiExtendSet: extendProtocol,
        };
        this.layoutService.viewReady.promise.then(() => {
            var _a;
            if (kind === 'add') {
                const { component, titleComponent } = view;
                this.layoutService.collectTabbarComponent([
                    {
                        id: containerId,
                    },
                ], {
                    ...view,
                    iconClass: view.icon
                        ? (0, ide_core_browser_1.getIcon)(view.icon)
                        : view.iconPath
                            ? this.iconService.fromIcon(this.extension.path, view.iconPath)
                            : '',
                    containerId,
                    component,
                    titleComponent,
                    titleProps: initialProps,
                    initialProps,
                    fromExtension: true,
                }, position);
            }
            else {
                this.layoutService.replaceViewComponent(view, initialProps);
                view.titleComponent &&
                    ((_a = this.layoutService.getTabbarHandler(containerId)) === null || _a === void 0 ? void 0 : _a.setTitleComponent(view.titleComponent, initialProps));
            }
        });
        return {
            dispose: () => {
                const componentHandler = this.layoutService.getTabbarHandler(containerId);
                if (componentHandler) {
                    componentHandler.dispose();
                }
            },
        };
    }
};
exports.TabbarBrowserContributionRunner = TabbarBrowserContributionRunner;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IMainLayoutService),
    tslib_1.__metadata("design:type", Object)
], TabbarBrowserContributionRunner.prototype, "layoutService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], TabbarBrowserContributionRunner.prototype, "iconService", void 0);
exports.TabbarBrowserContributionRunner = TabbarBrowserContributionRunner = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true })
], TabbarBrowserContributionRunner);
//# sourceMappingURL=tabbar.js.map