
import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';
import { localize } from '../../../nls.js';
import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
import { isProposedApiEnabled } from '../../services/extensions/common/extensions.js';
import { ExtensionsRegistry } from '../../services/extensions/common/extensionsRegistry.js';
import { isAccessibilityInformation } from '../../../platform/accessibility/common/accessibility.js';
import { Iterable } from '../../../base/common/iterator.js';
import { ExtensionIdentifier } from '../../../platform/extensions/common/extensions.js';
import { asStatusBarItemIdentifier } from '../common/extHostTypes.js';
import { registerWorkbenchContribution2, WorkbenchPhase } from '../../common/contributions.js';
import { StatusBarUpdateKind, IExtensionStatusBarItemService } from './statusBarService.js';

function isUserFriendlyStatusItemEntry(candidate) {
    const obj = candidate;
    return (typeof obj.id === "string" && obj.id.length > 0) && typeof obj.name === "string" && typeof obj.text === "string" && (obj.alignment === "left" || obj.alignment === "right") && (obj.command === undefined || typeof obj.command === "string") && (obj.tooltip === undefined || typeof obj.tooltip === "string") && (obj.priority === undefined || typeof obj.priority === "number") && (obj.accessibilityInformation === undefined || isAccessibilityInformation(obj.accessibilityInformation));
}
const statusBarItemSchema = {
    type: "object",
    required: ["id", "text", "alignment", "name"],
    properties: {
        id: {
            type: "string",
            markdownDescription: ( localize(
                2620,
                "The identifier of the status bar entry. Must be unique within the extension. The same value must be used when calling the `vscode.window.createStatusBarItem(id, ...)`-API"
            ))
        },
        name: {
            type: "string",
            description: ( localize(
                2621,
                "The name of the entry, like 'Python Language Indicator', 'Git Status' etc. Try to keep the length of the name short, yet descriptive enough that users can understand what the status bar item is about."
            ))
        },
        text: {
            type: "string",
            description: ( localize(
                2622,
                "The text to show for the entry. You can embed icons in the text by leveraging the `$(<name>)`-syntax, like 'Hello {0}!'",
                "$(globe)"
            ))
        },
        tooltip: {
            type: "string",
            description: ( localize(2623, "The tooltip text for the entry."))
        },
        command: {
            type: "string",
            description: ( localize(2624, "The command to execute when the status bar entry is clicked."))
        },
        alignment: {
            type: "string",
            enum: ["left", "right"],
            description: ( localize(2625, "The alignment of the status bar entry."))
        },
        priority: {
            type: "number",
            description: ( localize(
                2626,
                "The priority of the status bar entry. Higher value means the item should be shown more to the left."
            ))
        },
        accessibilityInformation: {
            type: "object",
            description: ( localize(
                2627,
                "Defines the role and aria label to be used when the status bar entry is focused."
            )),
            required: ["label"],
            properties: {
                role: {
                    type: "string",
                    description: ( localize(
                        2628,
                        "The role of the status bar entry which defines how a screen reader interacts with it. More about aria roles can be found here https://w3c.github.io/aria/#widget_roles"
                    ))
                },
                label: {
                    type: "string",
                    description: ( localize(
                        2629,
                        "The aria label of the status bar entry. Defaults to the entry's text."
                    ))
                }
            }
        }
    }
};
const statusBarItemsSchema = {
    description: ( localize(2630, "Contributes items to the status bar.")),
    oneOf: [statusBarItemSchema, {
        type: "array",
        items: statusBarItemSchema
    }]
};
const statusBarItemsExtensionPoint = ExtensionsRegistry.registerExtensionPoint({
    extensionPoint: "statusBarItems",
    jsonSchema: statusBarItemsSchema
});
let StatusBarItemsExtensionPoint = class StatusBarItemsExtensionPoint {
    constructor(statusBarItemsService) {
        const contributions = ( new DisposableStore());
        statusBarItemsExtensionPoint.setHandler(extensions => {
            contributions.clear();
            for (const entry of extensions) {
                if (!isProposedApiEnabled(entry.description)) {
                    entry.collector.error(`The ${statusBarItemsExtensionPoint.name} is proposed API`);
                    continue;
                }
                const {
                    value,
                    collector
                } = entry;
                for (const candidate of Iterable.wrap(value)) {
                    if (!isUserFriendlyStatusItemEntry(candidate)) {
                        collector.error(( localize(2631, "Invalid status bar item contribution.")));
                        continue;
                    }
                    const fullItemId = asStatusBarItemIdentifier(entry.description.identifier, candidate.id);
                    const kind = statusBarItemsService.setOrUpdateEntry(
                        fullItemId,
                        fullItemId,
                        ExtensionIdentifier.toKey(entry.description.identifier),
                        candidate.name ?? entry.description.displayName ?? entry.description.name,
                        candidate.text,
                        candidate.tooltip,
                        candidate.command ? {
                            id: candidate.command,
                            title: candidate.name
                        } : undefined,
                        undefined,
                        undefined,
                        candidate.alignment === "left",
                        candidate.priority,
                        candidate.accessibilityInformation
                    );
                    if (kind === StatusBarUpdateKind.DidDefine) {
                        contributions.add(toDisposable(() => statusBarItemsService.unsetEntry(fullItemId)));
                    }
                }
            }
        });
    }
};
StatusBarItemsExtensionPoint = ( __decorate([( __param(0, IExtensionStatusBarItemService))], StatusBarItemsExtensionPoint));
let StatusBarItemsExtensionPointWorkbenchContribution = class StatusBarItemsExtensionPointWorkbenchContribution {
    static {
        this.ID = "workbench.contrib.statusBarItemsExtensionPoint";
    }
    constructor(instantiationService) {
        this.instantiationService = instantiationService;
        this.instantiationService.createInstance(StatusBarItemsExtensionPoint);
    }
};
StatusBarItemsExtensionPointWorkbenchContribution = ( __decorate([( __param(0, IInstantiationService))], StatusBarItemsExtensionPointWorkbenchContribution));
registerWorkbenchContribution2(
    StatusBarItemsExtensionPointWorkbenchContribution.ID,
    StatusBarItemsExtensionPointWorkbenchContribution,
    WorkbenchPhase.BlockStartup
);

export { StatusBarItemsExtensionPoint };
