"use strict";
var BrowserViewContributionPoint_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserViewContributionPoint = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const browserViews_1 = require("@opensumi/ide-core-browser/lib/extensions/schema/browserViews");
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 common_1 = require("../../../common");
const components_1 = require("../../components");
const types_1 = require("../../types");
const SUPPORT_LOCATION = ['left', 'right', 'bottom', 'editor', 'toolBar', 'editorSide', 'chat'];
let BrowserViewContributionPoint = BrowserViewContributionPoint_1 = class BrowserViewContributionPoint extends common_1.VSCodeContributePoint {
    constructor() {
        super(...arguments);
        this.disposableCollection = new ide_core_common_1.DisposableCollection();
    }
    contribute() {
        return new Promise((resolve) => {
            this.mainLayoutService.viewReady.promise.then(() => {
                var _a;
                for (const contrib of this.contributesMap) {
                    const { extensionId, contributes } = contrib;
                    const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
                    if (!extension) {
                        continue;
                    }
                    const keys = Object.keys(contributes).filter((key) => !BrowserViewContributionPoint_1.unsupportLocation.includes(key));
                    for (let location of keys) {
                        const views = contributes[location].view.map((view) => ({
                            ...view,
                            component: components_1.ExtensionLoadingView,
                        }));
                        if (!SUPPORT_LOCATION.includes(location)) {
                            if (!this.mainLayoutService.getTabbarHandler(location)) {
                                // 若目标视图不存在，append将fallback到add模式添加到左侧边栏
                                location = 'left';
                            }
                            else {
                                // 走append view逻辑
                                for (const view of views) {
                                    if (view.titleComponentId) {
                                        (0, ide_core_common_1.getDebugLogger)().warn(`custom title component '${view.titleComponentId}' is not allowed for built-in container ${location}!`);
                                    }
                                    const { title, id, priority, component, when, weight } = view;
                                    // 支持指定通过 location 获取 containerId 的方式
                                    const containerId = ((_a = this.mainLayoutService.getTabbarHandler(location)) === null || _a === void 0 ? void 0 : _a.containerId) || location;
                                    const handlerId = this.mainLayoutService.collectViewComponent({
                                        id,
                                        priority,
                                        component,
                                        name: title,
                                        when,
                                        weight,
                                    }, containerId, {}, {
                                        fromExtension: true,
                                    });
                                    this.disposableCollection.push({
                                        dispose: () => {
                                            const handler = this.mainLayoutService.getTabbarHandler(handlerId);
                                            handler.disposeView(id);
                                        },
                                    });
                                }
                                continue;
                            }
                        }
                        for (const view of views) {
                            const { title, icon, iconPath, id, priority, component, expanded, noResize, when, weight, hideTab } = view;
                            const containerId = `${extensionId}:${id}`;
                            const handlerId = this.mainLayoutService.collectTabbarComponent([
                                {
                                    id,
                                    priority,
                                    component,
                                    when,
                                    weight,
                                },
                            ], {
                                iconClass: iconPath ? this.iconService.fromIcon(extension.path, iconPath) : (0, ide_core_browser_1.getIcon)(icon),
                                title: title && this.getLocalizeFromNlsJSON(title, extensionId),
                                priority,
                                expanded,
                                containerId,
                                noResize,
                                fromExtension: true,
                                hideTab,
                            }, location);
                            this.disposableCollection.push({
                                dispose: () => {
                                    const handler = this.mainLayoutService.getTabbarHandler(handlerId);
                                    handler.dispose();
                                },
                            });
                        }
                    }
                }
                resolve();
            });
        });
    }
};
exports.BrowserViewContributionPoint = BrowserViewContributionPoint;
BrowserViewContributionPoint.schema = browserViews_1.browserViews.schema;
// 不支持提前加载的视图
BrowserViewContributionPoint.unsupportLocation = ['bottom', 'editor', 'toolBar', 'chat'];
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IMainLayoutService),
    tslib_1.__metadata("design:type", Object)
], BrowserViewContributionPoint.prototype, "mainLayoutService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], BrowserViewContributionPoint.prototype, "iconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], BrowserViewContributionPoint.prototype, "extensionManageService", void 0);
exports.BrowserViewContributionPoint = BrowserViewContributionPoint = BrowserViewContributionPoint_1 = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('browserViews'),
    (0, common_1.LifeCycle)(3 /* LifeCyclePhase.Starting */)
], BrowserViewContributionPoint);
//# sourceMappingURL=browser-views.js.map