"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubmenusContributionPoint = void 0;
exports.isValidSubmenu = isValidSubmenu;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const menu_1 = require("@opensumi/ide-core-browser/lib/extensions/schema/menu");
const next_1 = require("@opensumi/ide-core-browser/lib/menu/next");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_theme_1 = require("@opensumi/ide-theme");
const common_1 = require("../../../common");
const types_1 = require("../../types");
const menu_2 = require("../../vscode/contributes/menu");
function isValidSubmenu(submenu, collector) {
    if (!Array.isArray(submenu)) {
        collector.error((0, ide_core_common_1.formatLocalize)('requirearray'));
        return false;
    }
    for (const item of submenu) {
        if (typeof item.id !== 'string') {
            collector.error((0, ide_core_common_1.formatLocalize)('requirestring', 'id'));
            return false;
        }
        if (item.title && typeof item.title !== 'string') {
            collector.error((0, ide_core_common_1.formatLocalize)('optstring', 'title'));
            return false;
        }
        if (item.when && typeof item.when !== 'string') {
            collector.error((0, ide_core_common_1.formatLocalize)('optstring', 'when'));
            return false;
        }
        if (item.group && typeof item.group !== 'string') {
            collector.error((0, ide_core_common_1.formatLocalize)('optstring', 'group'));
            return false;
        }
    }
    return true;
}
let SubmenusContributionPoint = class SubmenusContributionPoint extends common_1.VSCodeContributePoint {
    contribute() {
        const collector = console;
        for (const contrib of this.contributesMap) {
            const { extensionId, contributes } = contrib;
            const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
            if (!extension) {
                continue;
            }
            for (const menuPosition of Object.keys(contributes)) {
                const menuActions = contributes[menuPosition];
                if (!isValidSubmenu(menuActions, console)) {
                    return;
                }
                const menuId = (0, menu_2.parseMenuId)(menuPosition);
                if ((0, ide_core_common_1.isUndefined)(menuId)) {
                    collector.warn((0, ide_core_common_1.formatLocalize)('menuId.invalid', '`{0}` is not a valid submenu identifier', menuPosition));
                    return;
                }
                for (const item of menuActions) {
                    const [group, order] = (0, menu_2.parseMenuGroup)(item.group);
                    this.addDispose(this.menuRegistry.registerMenuItem(menuId, {
                        submenu: item.id,
                        label: item.title && this.getLocalizeFromNlsJSON(item.title, extensionId),
                        iconClass: this.iconService.fromIcon(extension.path, item.icon, ide_theme_1.IconType.Background),
                        when: item.when,
                        group,
                        order,
                        nativeRole: item.nativeRole,
                    }));
                }
            }
        }
        // menu registration
    }
};
exports.SubmenusContributionPoint = SubmenusContributionPoint;
SubmenusContributionPoint.schema = menu_1.menus.subMenusSchema;
tslib_1.__decorate([
    (0, di_1.Autowired)(next_1.IMenuRegistry),
    tslib_1.__metadata("design:type", next_1.IMenuRegistry)
], SubmenusContributionPoint.prototype, "menuRegistry", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], SubmenusContributionPoint.prototype, "iconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], SubmenusContributionPoint.prototype, "extensionManageService", void 0);
exports.SubmenusContributionPoint = SubmenusContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('submenus'),
    (0, common_1.LifeCycle)(3 /* LifeCyclePhase.Starting */)
], SubmenusContributionPoint);
//# sourceMappingURL=submenu.js.map