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 { localizeWithPath } 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 { VIEWLET_ID } from '../../extensions/common/extensions.js';
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.service.js';
import { IPaneCompositePartService } from '../../../services/panecomposite/browser/panecomposite.service.js';
import { ViewContainerLocation } from '../../../common/views.js';
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';
const _moduleId = "vs/workbench/contrib/format/browser/formatActionsNone";
async function showExtensionQuery(paneCompositeService, query) {
    const viewlet = await paneCompositeService.openPaneComposite(VIEWLET_ID, ViewContainerLocation.Sidebar, true);
    if (viewlet) {
        (viewlet?.getViewPaneContainer()).search(query);
    }
}
registerEditorAction(class FormatDocumentMultipleAction extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.formatDocument.none',
            label: ( localizeWithPath(_moduleId, 0, "Format Document")),
            alias: '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 paneCompositeService = accessor.get(IPaneCompositePartService);
        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(( localizeWithPath(_moduleId, 1, "This file cannot be formatted because it is too large")));
        }
        else {
            const langName = model.getLanguageId();
            const message = ( localizeWithPath(_moduleId, 2, "There is no formatter for '{0}' files installed.", langName));
            const { confirmed } = await dialogService.confirm({
                message,
                primaryButton: ( localizeWithPath(_moduleId, 3, "&&Install Formatter..."))
            });
            if (confirmed) {
                showExtensionQuery(paneCompositeService, `category:formatters ${langName}`);
            }
        }
    }
});
