import { transaction, asyncTransaction } from '../../../../base/common/observableInternal/base.js';
import '../../../../base/common/observableInternal/derived.js';
import '../../../../base/common/observableInternal/autorun.js';
import '../../../../base/common/observableInternal/utils.js';
import '../../../../base/common/cancellation.js';
import { EditorAction } from '../../../browser/editorExtensions.js';
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
import { showNextInlineSuggestionActionId, showPreviousInlineSuggestionActionId, inlineSuggestCommitId } from './commandIds.js';
import { InlineCompletionContextKeys } from './inlineCompletionContextKeys.js';
import { InlineCompletionsController } from './inlineCompletionsController.js';
import { Context } from '../../suggest/browser/suggest.js';
import { localizeWithPath } from '../../../../nls.js';
import { Action2, MenuId } from '../../../../platform/actions/common/actions.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
class ShowNextInlineSuggestionAction extends EditorAction {
    static { this.ID = showNextInlineSuggestionActionId; }
    constructor() {
        super({
            id: ShowNextInlineSuggestionAction.ID,
            label: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.showNext',
                "Show Next Inline Suggestion"
            )),
            alias: 'Show Next Inline Suggestion',
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100,
                primary: 512  | 94 ,
            },
        });
    }
    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: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.showPrevious',
                "Show Previous Inline Suggestion"
            )),
            alias: 'Show Previous Inline Suggestion',
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100,
                primary: 512  | 92 ,
            },
        });
    }
    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: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.trigger',
                "Trigger Inline Suggestion"
            )),
            alias: '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 AcceptNextWordOfInlineCompletion extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.inlineSuggest.acceptNextWord',
            label: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.acceptNextWord',
                "Accept Next Word Of Inline Suggestion"
            )),
            alias: 'Accept Next Word Of Inline Suggestion',
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100  + 1,
                primary: 2048  | 17 ,
                kbExpr: ( ContextKeyExpr.and(
                    EditorContextKeys.writable,
                    InlineCompletionContextKeys.inlineSuggestionVisible
                )),
            },
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localizeWithPath(
                        'vs/editor/contrib/inlineCompletions/browser/commands',
                        'acceptWord',
                        '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: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.acceptNextLine',
                "Accept Next Line Of Inline Suggestion"
            )),
            alias: 'Accept Next Line Of Inline Suggestion',
            precondition: ( ContextKeyExpr.and(
                EditorContextKeys.writable,
                InlineCompletionContextKeys.inlineSuggestionVisible
            )),
            kbOpts: {
                weight: 100  + 1,
            },
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localizeWithPath(
                        'vs/editor/contrib/inlineCompletions/browser/commands',
                        'acceptLine',
                        '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: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.accept',
                "Accept Inline Suggestion"
            )),
            alias: 'Accept Inline Suggestion',
            precondition: InlineCompletionContextKeys.inlineSuggestionVisible,
            menuOpts: [{
                    menuId: MenuId.InlineSuggestionToolbar,
                    title: ( localizeWithPath('vs/editor/contrib/inlineCompletions/browser/commands', 'accept', "Accept")),
                    group: 'primary',
                    order: 1,
                }],
            kbOpts: {
                primary: 2 ,
                weight: 200,
                kbExpr: ( ContextKeyExpr.and(InlineCompletionContextKeys.inlineSuggestionVisible, ( EditorContextKeys.tabMovesFocus.toNegated()), InlineCompletionContextKeys.inlineSuggestionHasIndentationLessThanTabSize, ( Context.Visible.toNegated()), ( EditorContextKeys.hoverFocused.toNegated()))),
            }
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        if (controller) {
            controller.model.get()?.accept(controller.editor);
            controller.editor.focus();
        }
    }
}
class HideInlineCompletion extends EditorAction {
    static { this.ID = 'editor.action.inlineSuggest.hide'; }
    constructor() {
        super({
            id: HideInlineCompletion.ID,
            label: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.hide',
                "Hide Inline Suggestion"
            )),
            alias: 'Hide Inline Suggestion',
            precondition: InlineCompletionContextKeys.inlineSuggestionVisible,
            kbOpts: {
                weight: 100,
                primary: 9 ,
            }
        });
    }
    async run(accessor, editor) {
        const controller = InlineCompletionsController.get(editor);
        transaction(tx => {
            controller?.model.get()?.stop(tx);
        });
    }
}
class ToggleAlwaysShowInlineSuggestionToolbar extends Action2 {
    static { this.ID = 'editor.action.inlineSuggest.toggleAlwaysShowToolbar'; }
    constructor() {
        super({
            id: ToggleAlwaysShowInlineSuggestionToolbar.ID,
            title: ( localizeWithPath(
                'vs/editor/contrib/inlineCompletions/browser/commands',
                'action.inlineSuggest.alwaysShowToolbar',
                "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, editor) {
        const configService = accessor.get(IConfigurationService);
        const currentValue = configService.getValue('editor.inlineSuggest.showToolbar');
        const newValue = currentValue === 'always' ? 'onHover' : 'always';
        configService.updateValue('editor.inlineSuggest.showToolbar', newValue);
    }
}
export { AcceptInlineCompletion, AcceptNextLineOfInlineCompletion, AcceptNextWordOfInlineCompletion, HideInlineCompletion, ShowNextInlineSuggestionAction, ShowPreviousInlineSuggestionAction, ToggleAlwaysShowInlineSuggestionToolbar, TriggerInlineSuggestionAction };
