
import { KeyMod as KeyMod$1, KeyCode } from '../../../../../base/common/keyCodes.js';
import '../../../../../base/common/observableInternal/index.js';
import { splitLines } from '../../../../../base/common/strings.js';
import { localize2, localize } from '../../../../../nls.js';
import { MenuId, Action2 } from '../../../../../platform/actions/common/actions.js';
import { IClipboardService } from '../../../../../platform/clipboard/common/clipboardService.service.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyExpr } from '../../../../../platform/contextkey/common/contextkey.js';
import { KeybindingWeight, KeybindingsRegistry } from '../../../../../platform/keybinding/common/keybindingsRegistry.js';
import '../../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../../platform/notification/common/notification.service.js';
import { EditorAction, EditorCommand } from '../../../../browser/editorExtensions.js';
import { EditorContextKeys } from '../../../../common/editorContextKeys.js';
import { Context } from '../../../suggest/browser/suggest.js';
import { showNextInlineSuggestionActionId, showPreviousInlineSuggestionActionId, inlineSuggestCommitId, jumpToNextInlineEditId, hideInlineCompletionId, toggleShowCollapsedId } from './commandIds.js';
import { InlineCompletionContextKeys } from './inlineCompletionContextKeys.js';
import { InlineCompletionsController } from './inlineCompletionsController.js';
import { asyncTransaction, transaction } from '../../../../../base/common/observableInternal/base.js';
import Severity$1 from '../../../../../base/common/severity.js';

class ShowNextInlineSuggestionAction extends EditorAction {
    static { this.ID = showNextInlineSuggestionActionId; }
    constructor() {
        super({
            id: ShowNextInlineSuggestionAction.ID,
            label: ( localize2(1202, "Show Next Inline Suggestion")),
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100,
                primary: KeyMod$1.Alt | KeyCode.BracketRight,
            },
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        controller?.model.get()?.next();
    }
}
class ShowPreviousInlineSuggestionAction extends EditorAction {
    static { this.ID = showPreviousInlineSuggestionActionId; }
    constructor() {
        super({
            id: ShowPreviousInlineSuggestionAction.ID,
            label: ( localize2(1203, "Show Previous Inline Suggestion")),
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100,
                primary: KeyMod$1.Alt | KeyCode.BracketLeft,
            },
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        controller?.model.get()?.previous();
    }
}
class TriggerInlineSuggestionAction extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.trigger',
            label: ( localize2(1204, "Trigger Inline Suggestion")),
            precondition: EditorContextKeys.writable
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        await asyncTransaction(async (tx) => {
            await controller?.model.get()?.triggerExplicitly(tx);
            controller?.playAccessibilitySignal(tx);
        });
    }
}
class ExplicitTriggerInlineEditAction extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.triggerInlineEditExplicit',
            label: ( localize2(1205, "Trigger Next Edit Suggestion")),
            precondition: EditorContextKeys.writable,
        });
    }
    async run(accessor, editor) {
        const notificationService = accessor.get(INotificationService);
        const controller = InlineCompletionsController.get(editor);
        await controller?.model.get()?.triggerExplicitly(undefined, true);
        if (!controller?.model.get()?.inlineEditAvailable.get()) {
            notificationService.notify({
                severity: Severity$1.Info,
                message: ( localize(1206, "No inline edit is available."))
            });
        }
    }
}
class TriggerInlineEditAction extends EditorCommand {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.triggerInlineEdit',
            precondition: EditorContextKeys.writable,
        });
    }
    async runEditorCommand(accessor, editor, args) {
        const controller = InlineCompletionsController.get(editor);
        await controller?.model.get()?.trigger(undefined, { onlyFetchInlineEdits: true });
    }
}
class AcceptNextWordOfInlineCompletion extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.acceptNextWord',
            label: ( localize2(1207, "Accept Next Word Of Inline Suggestion")),
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: KeybindingWeight.EditorContrib + 1,
                primary: KeyMod$1.CtrlCmd | KeyCode.RightArrow,
                kbExpr: ( ContextKeyExpr.and(
                    EditorContextKeys.writable,
                    InlineCompletionContextKeys.inlineSuggestionVisible
                )),
            },
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localize(1208, 'Accept Word')),
                    group: 'primary',
                    order: 2,
                }],
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        await controller?.model.get()?.acceptNextWord(controller.editor);
    }
}
class AcceptNextLineOfInlineCompletion extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.acceptNextLine',
            label: ( localize2(1209, "Accept Next Line Of Inline Suggestion")),
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: KeybindingWeight.EditorContrib + 1,
            },
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localize(1210, 'Accept Line')),
                    group: 'secondary',
                    order: 2,
                }],
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        await controller?.model.get()?.acceptNextLine(controller.editor);
    }
}
class AcceptInlineCompletion extends EditorAction {
    constructor() {
        super({
            id: inlineSuggestCommitId,
            label: ( localize2(1211, "Accept Inline Suggestion")),
            precondition: ( ContextKeyExpr.or(
                InlineCompletionContextKeys.inlineSuggestionVisible,
                InlineCompletionContextKeys.inlineEditVisible
            )),
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localize(1212, "Accept")),
                    group: 'primary',
                    order: 2,
                }, {
                    menuId: MenuId.InlineEditsActions,
                    title: ( localize(1212, "Accept")),
                    group: 'primary',
                    order: 2,
                }],
            kbOpts: [
                {
                    primary: KeyCode.Tab,
                    weight: 200,
                    kbExpr: ( ContextKeyExpr.or(( ContextKeyExpr.and(InlineCompletionContextKeys.inlineSuggestionVisible, ( EditorContextKeys.tabMovesFocus.toNegated()), ( Context.Visible.toNegated()), ( EditorContextKeys.hoverFocused.toNegated()), InlineCompletionContextKeys.inlineSuggestionHasIndentationLessThanTabSize)), ( ContextKeyExpr.and(InlineCompletionContextKeys.inlineEditVisible, ( EditorContextKeys.tabMovesFocus.toNegated()), ( Context.Visible.toNegated()), ( EditorContextKeys.hoverFocused.toNegated()), InlineCompletionContextKeys.tabShouldAcceptInlineEdit)))),
                }
            ],
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.getInFocusedEditorOrParent(accessor);
        if (controller) {
            controller.model.get()?.accept(controller.editor);
            controller.editor.focus();
        }
    }
}
KeybindingsRegistry.registerKeybindingRule({
    id: inlineSuggestCommitId,
    weight: 202,
    primary: KeyCode.Tab,
    when: ( ContextKeyExpr.and(InlineCompletionContextKeys.inInlineEditsPreviewEditor))
});
class JumpToNextInlineEdit extends EditorAction {
    constructor() {
        super({
            id: jumpToNextInlineEditId,
            label: ( localize2(1213, "Jump to next inline edit")),
            precondition: InlineCompletionContextKeys.inlineEditVisible,
            menuOpts: [{
                    menuId: MenuId.InlineEditsActions,
                    title: ( localize(1214, "Jump")),
                    group: 'primary',
                    order: 1,
                    when: ( InlineCompletionContextKeys.cursorAtInlineEdit.toNegated()),
                }],
            kbOpts: {
                primary: KeyCode.Tab,
                weight: 201,
                kbExpr: ( ContextKeyExpr.and(InlineCompletionContextKeys.inlineEditVisible, ( EditorContextKeys.tabMovesFocus.toNegated()), ( Context.Visible.toNegated()), ( EditorContextKeys.hoverFocused.toNegated()), InlineCompletionContextKeys.tabShouldJumpToInlineEdit)),
            }
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        if (controller) {
            controller.jump();
        }
    }
}
class AcceptNextInlineEditPart extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.acceptNextInlineEditPart',
            label: ( localize2(1215, "Accept Next Inline Edit Part")),
            precondition: ( ContextKeyExpr.and(EditorContextKeys.writable, InlineCompletionContextKeys.inlineEditVisible)),
            kbOpts: {
                weight: KeybindingWeight.EditorContrib + 1,
                kbExpr: ( ContextKeyExpr.and(EditorContextKeys.writable, InlineCompletionContextKeys.inlineEditVisible)),
            },
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        await controller?.model.get()?.acceptNextInlineEditPart(controller.editor);
    }
}
class HideInlineCompletion extends EditorAction {
    static { this.ID = hideInlineCompletionId; }
    constructor() {
        super({
            id: HideInlineCompletion.ID,
            label: ( localize2(1216, "Hide Inline Suggestion")),
            precondition: ( ContextKeyExpr.or(
                InlineCompletionContextKeys.inlineSuggestionVisible,
                InlineCompletionContextKeys.inlineEditVisible
            )),
            kbOpts: {
                weight: KeybindingWeight.EditorContrib + 90,
                primary: KeyCode.Escape,
            },
            menuOpts: [{
                    menuId: MenuId.InlineEditsActions,
                    title: ( localize(1217, "Reject")),
                    group: 'primary',
                    order: 3,
                }]
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.getInFocusedEditorOrParent(accessor);
        transaction(tx => {
            controller?.model.get()?.stop('explicitCancel', tx);
        });
        controller?.editor.focus();
    }
}
class ToggleInlineCompletionShowCollapsed extends EditorAction {
    static { this.ID = toggleShowCollapsedId; }
    constructor() {
        super({
            id: ToggleInlineCompletionShowCollapsed.ID,
            label: ( localize2(1218, "Toggle Inline Suggestions Show Collapsed")),
            precondition: ContextKeyExpr.true(),
        });
    }
    async run(accessor, editor) {
        const configurationService = accessor.get(IConfigurationService);
        const showCollapsed = configurationService.getValue('editor.inlineSuggest.edits.showCollapsed');
        configurationService.updateValue('editor.inlineSuggest.edits.showCollapsed', !showCollapsed);
    }
}
KeybindingsRegistry.registerKeybindingRule({
    id: HideInlineCompletion.ID,
    weight: -1,
    primary: KeyCode.Escape,
    secondary: [KeyMod$1.Shift | KeyCode.Escape],
    when: ( ContextKeyExpr.and(InlineCompletionContextKeys.inInlineEditsPreviewEditor))
});
class ToggleAlwaysShowInlineSuggestionToolbar extends Action2 {
    static { this.ID = 'editor.action.inlineSuggest.toggleAlwaysShowToolbar'; }
    constructor() {
        super({
            id: ToggleAlwaysShowInlineSuggestionToolbar.ID,
            title: ( localize(1219, "Always Show Toolbar")),
            f1: false,
            precondition: undefined,
            menu: [{
                    id: MenuId.InlineSuggestionToolbar,
                    group: 'secondary',
                    order: 10,
                }],
            toggled: ( ContextKeyExpr.equals('config.editor.inlineSuggest.showToolbar', 'always'))
        });
    }
    async run(accessor) {
        const configService = accessor.get(IConfigurationService);
        const currentValue = configService.getValue('editor.inlineSuggest.showToolbar');
        const newValue = currentValue === 'always' ? 'onHover' : 'always';
        configService.updateValue('editor.inlineSuggest.showToolbar', newValue);
    }
}
class DevExtractReproSample extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.dev.extractRepro',
            label: ( localize(1220, "Developer: Extract Inline Suggest State")),
            alias: 'Developer: Inline Suggest Extract Repro',
            precondition: InlineCompletionContextKeys.inlineEditVisible,
        });
    }
    async run(accessor, editor) {
        const clipboardService = accessor.get(IClipboardService);
        const controller = InlineCompletionsController.get(editor);
        const m = controller?.model.get();
        if (!m) {
            return;
        }
        const repro = m.extractReproSample();
        const inlineCompletionLines = splitLines(JSON.stringify({ inlineCompletion: repro.inlineCompletion }, null, 4));
        const json = ( inlineCompletionLines.map(l => '// ' + l)).join('\n');
        const reproStr = `${repro.documentValue}\n\n// <json>\n${json}\n// </json>\n`;
        await clipboardService.writeText(reproStr);
        return { reproCase: reproStr };
    }
}

export { AcceptInlineCompletion, AcceptNextInlineEditPart, AcceptNextLineOfInlineCompletion, AcceptNextWordOfInlineCompletion, DevExtractReproSample, ExplicitTriggerInlineEditAction, HideInlineCompletion, JumpToNextInlineEdit, ShowNextInlineSuggestionAction, ShowPreviousInlineSuggestionAction, ToggleAlwaysShowInlineSuggestionToolbar, ToggleInlineCompletionShowCollapsed, TriggerInlineEditAction, TriggerInlineSuggestionAction };
