"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewsWelcomeContributionPoint = exports.ViewIdentifierMap = exports.ViewsWelcomeExtensionPointFields = 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_debug_1 = require("@opensumi/ide-debug");
const ide_file_tree_next_1 = require("@opensumi/ide-file-tree-next");
const ide_main_layout_1 = require("@opensumi/ide-main-layout");
const ide_scm_1 = require("@opensumi/ide-scm");
const contextkey_1 = require("@opensumi/monaco-editor-core/esm/vs/platform/contextkey/common/contextkey");
const common_1 = require("../../../common");
var ViewsWelcomeExtensionPointFields;
(function (ViewsWelcomeExtensionPointFields) {
    ViewsWelcomeExtensionPointFields["view"] = "view";
    ViewsWelcomeExtensionPointFields["contents"] = "contents";
    ViewsWelcomeExtensionPointFields["when"] = "when";
    ViewsWelcomeExtensionPointFields["group"] = "group";
    ViewsWelcomeExtensionPointFields["enablement"] = "enablement";
})(ViewsWelcomeExtensionPointFields || (exports.ViewsWelcomeExtensionPointFields = ViewsWelcomeExtensionPointFields = {}));
exports.ViewIdentifierMap = {
    explorer: ide_file_tree_next_1.FILE_EXPLORER_WELCOME_ID,
    debug: ide_debug_1.DEBUG_WELCOME_ID,
    scm: ide_scm_1.SCM_WELCOME_ID,
};
let ViewsWelcomeContributionPoint = class ViewsWelcomeContributionPoint extends common_1.VSCodeContributePoint {
    constructor() {
        super(...arguments);
        this.disposableCollection = new ide_core_browser_1.DisposableCollection();
    }
    contribute() {
        var _a;
        const welcomesByViewId = new Map();
        for (const contrib of this.contributesMap) {
            const { extensionId, contributes } = contrib;
            for (const welcome of contributes) {
                const { group, order } = parseGroupAndOrder(welcome);
                const precondition = contextkey_1.ContextKeyExpr.deserialize(welcome.enablement);
                const id = (_a = exports.ViewIdentifierMap[welcome.view]) !== null && _a !== void 0 ? _a : welcome.view;
                let viewContentMap = welcomesByViewId.get(id);
                if (!viewContentMap) {
                    viewContentMap = new Map();
                    welcomesByViewId.set(id, viewContentMap);
                }
                viewContentMap.set(welcome, {
                    content: this.getLocalizeFromNlsJSON(welcome.contents, extensionId),
                    when: contextkey_1.ContextKeyExpr.deserialize(welcome.when),
                    precondition,
                    group,
                    order,
                });
            }
        }
        for (const [id, viewContentMap] of welcomesByViewId) {
            const disposables = this.viewsRegistry.registerViewWelcomeContent2(id, viewContentMap);
            for (const [, disposable] of disposables) {
                this.disposableCollection.push(disposable);
            }
        }
    }
    dispose() {
        this.disposableCollection.dispose();
    }
};
exports.ViewsWelcomeContributionPoint = ViewsWelcomeContributionPoint;
ViewsWelcomeContributionPoint.schema = {
    type: 'array',
    description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome', 'Contributed views welcome content. Welcome content will be rendered in tree based views whenever they have no meaningful content to display, ie. the File Explorer when no folder is open. Such content is useful as in-product documentation to drive users to use certain features before they are available. A good example would be a `Clone Repository` button in the File Explorer welcome view.'),
    items: {
        type: 'object',
        description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view', 'Contributed welcome content for a specific view.'),
        required: [ViewsWelcomeExtensionPointFields.view, ViewsWelcomeExtensionPointFields.contents],
        properties: {
            [ViewsWelcomeExtensionPointFields.view]: {
                anyOf: [
                    {
                        type: 'string',
                        description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.view', 'Target view identifier for this welcome content. Only tree based views are supported.'),
                    },
                    {
                        type: 'string',
                        description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.view', 'Target view identifier for this welcome content. Only tree based views are supported.'),
                        enum: Object.keys(exports.ViewIdentifierMap),
                    },
                ],
            },
            [ViewsWelcomeExtensionPointFields.contents]: {
                type: 'string',
                description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.contents', 'Welcome content to be displayed. The format of the contents is a subset of Markdown, with support for links only.'),
            },
            [ViewsWelcomeExtensionPointFields.when]: {
                type: 'string',
                description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.when', 'Condition when the welcome content should be displayed.'),
            },
            [ViewsWelcomeExtensionPointFields.group]: {
                type: 'string',
                description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.group', 'Group to which this welcome content belongs.'),
            },
            [ViewsWelcomeExtensionPointFields.enablement]: {
                type: 'string',
                description: (0, ide_core_browser_1.localize)('contributes.viewsWelcome.view.enablement', 'Condition when the welcome content buttons and command links should be enabled.'),
            },
        },
    },
};
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IMainLayoutService),
    tslib_1.__metadata("design:type", Object)
], ViewsWelcomeContributionPoint.prototype, "mainlayoutService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IViewsRegistry),
    tslib_1.__metadata("design:type", Object)
], ViewsWelcomeContributionPoint.prototype, "viewsRegistry", void 0);
exports.ViewsWelcomeContributionPoint = ViewsWelcomeContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('viewsWelcome'),
    (0, common_1.LifeCycle)(3 /* LifeCyclePhase.Starting */)
], ViewsWelcomeContributionPoint);
function parseGroupAndOrder(welcome) {
    let group;
    let order;
    if (welcome.group) {
        const idx = welcome.group.lastIndexOf('@');
        if (idx > 0) {
            group = welcome.group.substr(0, idx);
            order = Number(welcome.group.substr(idx + 1)) || undefined;
        }
        else {
            group = welcome.group;
        }
    }
    return { group, order };
}
//# sourceMappingURL=views-welcome.js.map