import { Codicon } from '../../../../../base/common/codicons.js';
import { ICodeEditorService } from '../../../../../editor/browser/services/codeEditorService.js';
import { Selection } from '../../../../../editor/common/core/selection.js';
import { localize2WithPath, localizeWithPath } from '../../../../../nls.js';
import { registerAction2, Action2, MenuId } from '../../../../../platform/actions/common/actions.js';
import { CHAT_CATEGORY } from './chatActions.js';
import { IQuickChatService } from '../chat.js';
import { CONTEXT_PROVIDER_EXISTS } from '../../common/chatContextKeys.js';
import { InlineChatController } from '../../../inlineChat/browser/inlineChatController.js';
const ASK_QUICK_QUESTION_ACTION_ID = 'workbench.action.quickchat.toggle';
function registerQuickChatActions() {
    registerAction2(QuickChatGlobalAction);
    registerAction2(class OpenInChatViewAction extends Action2 {
        constructor() {
            super({
                id: 'workbench.action.quickchat.openInChatView',
                title: ( localize2WithPath(
                    'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                    'chat.openInChatView.label',
                    "Open in Chat View"
                )),
                f1: false,
                category: CHAT_CATEGORY,
                icon: Codicon.commentDiscussion,
                menu: {
                    id: MenuId.ChatInputSide,
                    group: 'navigation',
                    order: 10
                }
            });
        }
        run(accessor) {
            const quickChatService = accessor.get(IQuickChatService);
            quickChatService.openInChatView();
        }
    });
    registerAction2(class CloseQuickChatAction extends Action2 {
        constructor() {
            super({
                id: 'workbench.action.quickchat.close',
                title: ( localize2WithPath(
                    'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                    'chat.closeQuickChat.label',
                    "Close Quick Chat"
                )),
                f1: false,
                category: CHAT_CATEGORY,
                icon: Codicon.close,
                menu: {
                    id: MenuId.ChatInputSide,
                    group: 'navigation',
                    order: 20
                }
            });
        }
        run(accessor) {
            const quickChatService = accessor.get(IQuickChatService);
            quickChatService.close();
        }
    });
    registerAction2(class LaunchInlineChatFromQuickChatAction extends Action2 {
        constructor() {
            super({
                id: 'workbench.action.quickchat.launchInlineChat',
                title: ( localize2WithPath(
                    'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                    'chat.launchInlineChat.label',
                    "Launch Inline Chat"
                )),
                f1: false,
                category: CHAT_CATEGORY
            });
        }
        async run(accessor) {
            const quickChatService = accessor.get(IQuickChatService);
            const codeEditorService = accessor.get(ICodeEditorService);
            if (quickChatService.focused) {
                quickChatService.close();
            }
            const codeEditor = codeEditorService.getActiveCodeEditor();
            if (!codeEditor) {
                return;
            }
            const controller = InlineChatController.get(codeEditor);
            if (!controller) {
                return;
            }
            await controller.run();
            controller.focus();
        }
    });
}
class QuickChatGlobalAction extends Action2 {
    constructor() {
        super({
            id: ASK_QUICK_QUESTION_ACTION_ID,
            title: ( localize2WithPath(
                'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                'quickChat',
                'Quick Chat'
            )),
            precondition: CONTEXT_PROVIDER_EXISTS,
            icon: Codicon.commentDiscussion,
            f1: false,
            category: CHAT_CATEGORY,
            keybinding: {
                weight: 200 ,
                primary: 2048  | 1024  | 39 ,
                linux: {
                    primary: 2048  | 1024  | 512  | 39
                }
            },
            metadata: {
                description: ( localizeWithPath(
                    'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                    'toggle.desc',
                    'Toggle the quick chat'
                )),
                args: [{
                        name: 'args',
                        schema: {
                            anyOf: [
                                {
                                    type: 'object',
                                    required: ['query'],
                                    properties: {
                                        query: {
                                            description: ( localizeWithPath(
                                                'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                                                'toggle.query',
                                                "The query to open the quick chat with"
                                            )),
                                            type: 'string'
                                        },
                                        isPartialQuery: {
                                            description: ( localizeWithPath(
                                                'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                                                'toggle.isPartialQuery',
                                                "Whether the query is partial; it will wait for more user input"
                                            )),
                                            type: 'boolean'
                                        }
                                    },
                                },
                                {
                                    type: 'string',
                                    description: ( localizeWithPath(
                                        'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                                        'toggle.query',
                                        "The query to open the quick chat with"
                                    ))
                                }
                            ]
                        }
                    }]
            },
        });
    }
    run(accessor, query) {
        const quickChatService = accessor.get(IQuickChatService);
        let options;
        switch (typeof query) {
            case 'string':
                options = { query };
                break;
            case 'object':
                options = query;
                break;
        }
        if (options?.query) {
            options.selection = ( new Selection(1, options.query.length + 1, 1, options.query.length + 1));
        }
        quickChatService.toggle(undefined, options);
    }
}
function getQuickChatActionForProvider(id, label) {
    return class AskQuickChatAction extends Action2 {
        constructor() {
            super({
                id: `workbench.action.openQuickChat.${id}`,
                category: CHAT_CATEGORY,
                title: ( localize2WithPath(
                    'vs/workbench/contrib/chat/browser/actions/chatQuickInputActions',
                    'interactiveSession.open',
                    "Open Quick Chat ({0})",
                    label
                )),
                f1: true
            });
        }
        run(accessor, query) {
            const quickChatService = accessor.get(IQuickChatService);
            quickChatService.toggle(id, query ? {
                query,
                selection: ( new Selection(1, query.length + 1, 1, query.length + 1))
            } : undefined);
        }
    };
}
export { ASK_QUICK_QUESTION_ACTION_ID, getQuickChatActionForProvider, registerQuickChatActions };
