import { localizeWithPath } from '../../../../../nls.js';
import { format } from '../../../../../base/common/strings.js';
import { IKeybindingService } from '../../../../../platform/keybinding/common/keybinding.js';
import { IChatWidgetService } from '../chat.js';
import { IAccessibleViewService } from '../../../accessibility/browser/accessibleView.js';
import { AccessibleDiffViewerNext } from '../../../../../editor/browser/widget/diffEditor/commands.js';
import { INLINE_CHAT_ID } from '../../../inlineChat/common/inlineChat.js';
function getAccessibilityHelpText(accessor, type) {
    const keybindingService = accessor.get(IKeybindingService);
    const content = [];
    const openAccessibleViewKeybinding = keybindingService.lookupKeybinding('editor.action.accessibleView')?.getAriaLabel();
    if (type === 'panelChat') {
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.overview',
            'The chat view is comprised of an input box and a request/response list. The input box is used to make requests and the list is used to display responses.'
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.requestHistory',
            'In the input box, use up and down arrows to navigate your request history. Edit input and use enter or the submit button to run a new request.'
        )));
        content.push(openAccessibleViewKeybinding ? ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.inspectResponse',
            'In the input box, inspect the last response in the accessible view {0}',
            openAccessibleViewKeybinding
        )) : ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.inspectResponseNoKb',
            'With the input box focused, inspect the last response in the accessible view via the Open Accessible View command, which is currently not triggerable by a keybinding.'
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.followUp',
            'In the input box, navigate to the suggested follow up question (Shift+Tab) and press Enter to run it.'
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'chat.announcement',
            'Chat responses will be announced as they come in. A response will indicate the number of code blocks, if any, and then the rest of the response.'
        )));
        content.push(descriptionForCommand('chat.action.focus', ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.focus',
            'To focus the chat request/response list, which can be navigated with up and down arrows, invoke the Focus Chat command ({0}).'
        )), ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.focusNoKb',
            'To focus the chat request/response list, which can be navigated with up and down arrows, invoke The Focus Chat List command, which is currently not triggerable by a keybinding.'
        )), keybindingService));
        content.push(descriptionForCommand('workbench.action.chat.focusInput', ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.focusInput',
            'To focus the input box for chat requests, invoke the Focus Chat Input command ({0}).'
        )), ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.interactiveSession.focusInputNoKb',
            'To focus the input box for chat requests, invoke the Focus Chat Input command, which is currently not triggerable by a keybinding.'
        )), keybindingService));
        content.push(descriptionForCommand('workbench.action.chat.nextCodeBlock', ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.nextCodeBlock',
            'To focus the next code block within a response, invoke the Chat: Next Code Block command ({0}).'
        )), ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.nextCodeBlockNoKb',
            'To focus the next code block within a response, invoke the Chat: Next Code Block command, which is currently not triggerable by a keybinding.'
        )), keybindingService));
        content.push(descriptionForCommand('workbench.action.chat.nextFileTree', ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.nextFileTree',
            'To focus the next file tree within a response, invoke the Chat: Next File Tree command ({0}).'
        )), ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.nextFileTreeNoKb',
            'To focus the next file tree within a response, invoke the Chat: Next File Tree command, which is currently not triggerable by a keybinding.'
        )), keybindingService));
        content.push(descriptionForCommand('workbench.action.chat.clear', ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.clear',
            'To clear the request/response list, invoke the Chat Clear command ({0}).'
        )), ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'workbench.action.chat.clearNoKb',
            'To clear the request/response list, invoke the Chat Clear command, which is currently not triggerable by a keybinding.'
        )), keybindingService));
    }
    else {
        const startChatKeybinding = keybindingService.lookupKeybinding('inlineChat.start')?.getAriaLabel();
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.overview',
            "Inline chat occurs within a code editor and takes into account the current selection. It is useful for making changes to the current editor. For example, fixing diagnostics, documenting or refactoring code. Keep in mind that AI generated code may be incorrect."
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.access',
            "It can be activated via code actions or directly using the command: Inline Chat: Start Inline Chat ({0}).",
            startChatKeybinding
        )));
        const upHistoryKeybinding = keybindingService.lookupKeybinding('inlineChat.previousFromHistory')?.getAriaLabel();
        const downHistoryKeybinding = keybindingService.lookupKeybinding('inlineChat.nextFromHistory')?.getAriaLabel();
        if (upHistoryKeybinding && downHistoryKeybinding) {
            content.push(( localizeWithPath(
                'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
                'inlineChat.requestHistory',
                'In the input box, use {0} and {1} to navigate your request history. Edit input and use enter or the submit button to run a new request.',
                upHistoryKeybinding,
                downHistoryKeybinding
            )));
        }
        content.push(openAccessibleViewKeybinding ? ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.inspectResponse',
            'In the input box, inspect the response in the accessible view {0}.',
            openAccessibleViewKeybinding
        )) : ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.inspectResponseNoKb',
            'With the input box focused, inspect the response in the accessible view via the Open Accessible View command, which is currently not triggerable by a keybinding.'
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.contextActions',
            "Context menu actions may run a request prefixed with a /. Type / to discover such ready-made commands."
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.fix',
            "If a fix action is invoked, a response will indicate the problem with the current code. A diff editor will be rendered and can be reached by tabbing."
        )));
        const diffReviewKeybinding = keybindingService.lookupKeybinding(AccessibleDiffViewerNext.id)?.getAriaLabel();
        content.push(diffReviewKeybinding ? ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.diff',
            "Once in the diff editor, enter review mode with ({0}). Use up and down arrows to navigate lines with the proposed changes.",
            diffReviewKeybinding
        )) : ( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.diffNoKb',
            "Tab again to enter the Diff editor with the changes and enter review mode with the Go to Next Difference Command. Use Up/DownArrow to navigate lines with the proposed changes."
        )));
        content.push(( localizeWithPath(
            'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
            'inlineChat.toolbar',
            "Use tab to reach conditional parts like commands, status, message responses and more."
        )));
    }
    content.push(( localizeWithPath(
        'vs/workbench/contrib/chat/browser/actions/chatAccessibilityHelp',
        'chat.signals',
        "Accessibility Signals can be changed via settings with a prefix of signals.chat. By default, if a request takes more than 4 seconds, you will hear a sound indicating that progress is still occurring."
    )));
    return content.join('\n\n');
}
function descriptionForCommand(commandId, msg, noKbMsg, keybindingService) {
    const kb = keybindingService.lookupKeybinding(commandId);
    if (kb) {
        return format(msg, kb.getAriaLabel());
    }
    return format(noKbMsg, commandId);
}
async function runAccessibilityHelpAction(accessor, editor, type) {
    const widgetService = accessor.get(IChatWidgetService);
    const accessibleViewService = accessor.get(IAccessibleViewService);
    const inputEditor = type === 'panelChat' ? widgetService.lastFocusedWidget?.inputEditor : editor;
    if (!inputEditor) {
        return;
    }
    const domNode = inputEditor.getDomNode() ?? undefined;
    if (!domNode) {
        return;
    }
    const cachedPosition = inputEditor.getPosition();
    inputEditor.getSupportedActions();
    const helpText = getAccessibilityHelpText(accessor, type);
    accessibleViewService.show({
        id: type === 'panelChat' ? "panelChat"  : "inlineChat" ,
        verbositySettingKey: type === 'panelChat' ? "accessibility.verbosity.panelChat"  : "accessibility.verbosity.inlineChat" ,
        provideContent: () => helpText,
        onClose: () => {
            if (type === 'panelChat' && cachedPosition) {
                inputEditor.setPosition(cachedPosition);
                inputEditor.focus();
            }
            else if (type === 'inlineChat') {
                const ctrl = editor?.getContribution(INLINE_CHAT_ID);
                ctrl?.focus();
            }
        },
        options: { type: "help"  }
    });
}
export { getAccessibilityHelpText, runAccessibilityHelpAction };
