
import { KeyMod as KeyMod$1, KeyCode } from '../../../../base/common/keyCodes.js';
import { registerEditorAction, EditorAction } from '../../../../editor/browser/editorExtensions.js';
import { EditorContextKeys } from '../../../../editor/common/editorContextKeys.js';
import { localize2, localize } from '../../../../nls.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
import { ICommandService } from '../../../../platform/commands/common/commands.service.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { IExtensionsWorkbenchService } from '../../extensions/common/extensions.service.js';
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.service.js';
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';

registerEditorAction(class FormatDocumentMultipleAction extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.formatDocument.none',
            label: ( localize2(6749, "Format Document")),
            precondition: ( ContextKeyExpr.and(EditorContextKeys.writable, ( EditorContextKeys.hasDocumentFormattingProvider.toNegated()))),
            kbOpts: {
                kbExpr: EditorContextKeys.editorTextFocus,
                primary: KeyMod$1.Shift | KeyMod$1.Alt | KeyCode.KeyF,
                linux: { primary: KeyMod$1.CtrlCmd | KeyMod$1.Shift | KeyCode.KeyI },
                weight: KeybindingWeight.EditorContrib,
            }
        });
    }
    async run(accessor, editor) {
        if (!editor.hasModel()) {
            return;
        }
        const commandService = accessor.get(ICommandService);
        const extensionsWorkbenchService = accessor.get(IExtensionsWorkbenchService);
        const notificationService = accessor.get(INotificationService);
        const dialogService = accessor.get(IDialogService);
        const languageFeaturesService = accessor.get(ILanguageFeaturesService);
        const model = editor.getModel();
        const formatterCount = languageFeaturesService.documentFormattingEditProvider.all(model).length;
        if (formatterCount > 1) {
            return commandService.executeCommand('editor.action.formatDocument.multiple');
        }
        else if (formatterCount === 1) {
            return commandService.executeCommand('editor.action.formatDocument');
        }
        else if (model.isTooLargeForSyncing()) {
            notificationService.warn(( localize(6750, "This file cannot be formatted because it is too large")));
        }
        else {
            const langName = model.getLanguageId();
            const message = ( localize(6751, "There is no formatter for '{0}' files installed.", langName));
            const { confirmed } = await dialogService.confirm({
                message,
                primaryButton: ( localize(6752, "&&Install Formatter..."))
            });
            if (confirmed) {
                extensionsWorkbenchService.openSearch(`category:formatters ${langName}`);
            }
        }
    }
});
