import { Action, SubmenuAction, Separator } from '../../../../base/common/actions.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { Schemas } from '../../../../base/common/network.js';
import { localizeWithPath, localize2WithPath } from '../../../../nls.js';
import { MenuRegistry, MenuId } from '../../../../platform/actions/common/actions.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { TerminalSettingId, TerminalLocation } from '../../../../platform/terminal/common/terminal.js';
import { ResourceContextKey } from '../../../common/contextkeys.js';
import { TaskExecutionSupportedContext } from '../../tasks/common/taskService.js';
import { TerminalCommandId, TERMINAL_VIEW_ID } from '../common/terminal.js';
import { TerminalContextKeyStrings, TerminalContextKeys } from '../common/terminalContextKey.js';
import { terminalStrings } from '../common/terminalStrings.js';
import { ACTIVE_GROUP, SIDE_GROUP } from '../../../services/editor/common/editorService.js';
const _moduleId = "vs/workbench/contrib/terminal/browser/terminalMenus";
var ContextMenuGroup;
( ((function(ContextMenuGroup) {
    ContextMenuGroup["Create"] = "1_create";
    ContextMenuGroup["Edit"] = "3_edit";
    ContextMenuGroup["Clear"] = "5_clear";
    ContextMenuGroup["Kill"] = "7_kill";
    ContextMenuGroup["Config"] = "9_config";
})(ContextMenuGroup || (ContextMenuGroup = {}))));
var TerminalMenuBarGroup;
( ((function(TerminalMenuBarGroup) {
    TerminalMenuBarGroup["Create"] = "1_create";
    TerminalMenuBarGroup["Run"] = "3_run";
    TerminalMenuBarGroup["Manage"] = "5_manage";
    TerminalMenuBarGroup["Configure"] = "7_configure";
})(TerminalMenuBarGroup || (TerminalMenuBarGroup = {}))));
function setupTerminalMenus() {
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.MenubarTerminalMenu,
            item: {
                group: TerminalMenuBarGroup.Create,
                command: {
                    id: TerminalCommandId.New,
                    title: ( localizeWithPath(_moduleId, 0, "&&New Terminal"))
                },
                order: 1
            }
        },
        {
            id: MenuId.MenubarTerminalMenu,
            item: {
                group: TerminalMenuBarGroup.Create,
                command: {
                    id: TerminalCommandId.Split,
                    title: ( localizeWithPath(_moduleId, 1, "&&Split Terminal")),
                    precondition: ( (ContextKeyExpr.has(TerminalContextKeyStrings.IsOpen)))
                },
                order: 2,
                when: TerminalContextKeys.processSupported
            }
        },
        {
            id: MenuId.MenubarTerminalMenu,
            item: {
                group: TerminalMenuBarGroup.Run,
                command: {
                    id: TerminalCommandId.RunActiveFile,
                    title: ( localizeWithPath(_moduleId, 2, "Run &&Active File"))
                },
                order: 3,
                when: TerminalContextKeys.processSupported
            }
        },
        {
            id: MenuId.MenubarTerminalMenu,
            item: {
                group: TerminalMenuBarGroup.Run,
                command: {
                    id: TerminalCommandId.RunSelectedText,
                    title: ( localizeWithPath(_moduleId, 3, "Run &&Selected Text"))
                },
                order: 4,
                when: TerminalContextKeys.processSupported
            }
        }
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                group: ContextMenuGroup.Create,
                command: {
                    id: TerminalCommandId.Split,
                    title: terminalStrings.split.value
                }
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.New,
                    title: terminalStrings.new
                },
                group: ContextMenuGroup.Create
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.KillViewOrEditor,
                    title: terminalStrings.kill.value,
                },
                group: ContextMenuGroup.Kill
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.CopySelection,
                    title: ( localizeWithPath(_moduleId, 4, "Copy"))
                },
                group: ContextMenuGroup.Edit,
                order: 1
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.CopySelectionAsHtml,
                    title: ( localizeWithPath(_moduleId, 5, "Copy as HTML"))
                },
                group: ContextMenuGroup.Edit,
                order: 2
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.Paste,
                    title: ( localizeWithPath(_moduleId, 6, "Paste"))
                },
                group: ContextMenuGroup.Edit,
                order: 3
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.Clear,
                    title: ( localizeWithPath(_moduleId, 7, "Clear"))
                },
                group: ContextMenuGroup.Clear,
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.SizeToContentWidth,
                    title: terminalStrings.toggleSizeToContentWidth
                },
                group: ContextMenuGroup.Config
            }
        },
        {
            id: MenuId.TerminalInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.SelectAll,
                    title: ( localizeWithPath(_moduleId, 8, "Select All")),
                },
                group: ContextMenuGroup.Edit,
                order: 3
            }
        },
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                group: ContextMenuGroup.Create,
                command: {
                    id: TerminalCommandId.Split,
                    title: terminalStrings.split.value
                }
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.New,
                    title: terminalStrings.new
                },
                group: ContextMenuGroup.Create
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.KillEditor,
                    title: terminalStrings.kill.value
                },
                group: ContextMenuGroup.Kill
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.CopySelection,
                    title: ( localizeWithPath(_moduleId, 4, "Copy"))
                },
                group: ContextMenuGroup.Edit,
                order: 1
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.CopySelectionAsHtml,
                    title: ( localizeWithPath(_moduleId, 5, "Copy as HTML"))
                },
                group: ContextMenuGroup.Edit,
                order: 2
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.Paste,
                    title: ( localizeWithPath(_moduleId, 6, "Paste"))
                },
                group: ContextMenuGroup.Edit,
                order: 3
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.Clear,
                    title: ( localizeWithPath(_moduleId, 7, "Clear"))
                },
                group: ContextMenuGroup.Clear,
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.SelectAll,
                    title: ( localizeWithPath(_moduleId, 8, "Select All")),
                },
                group: ContextMenuGroup.Edit,
                order: 3
            }
        },
        {
            id: MenuId.TerminalEditorInstanceContext,
            item: {
                command: {
                    id: TerminalCommandId.SizeToContentWidth,
                    title: terminalStrings.toggleSizeToContentWidth
                },
                group: ContextMenuGroup.Config
            }
        }
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.TerminalTabEmptyAreaContext,
            item: {
                command: {
                    id: TerminalCommandId.NewWithProfile,
                    title: ( localizeWithPath(_moduleId, 9, "New Terminal With Profile..."))
                },
                group: ContextMenuGroup.Create
            }
        },
        {
            id: MenuId.TerminalTabEmptyAreaContext,
            item: {
                command: {
                    id: TerminalCommandId.New,
                    title: terminalStrings.new
                },
                group: ContextMenuGroup.Create
            }
        }
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.TerminalNewDropdownContext,
            item: {
                command: {
                    id: TerminalCommandId.SelectDefaultProfile,
                    title: ( localize2WithPath(_moduleId, 10, 'Select Default Profile')),
                },
                group: '3_configure'
            }
        },
        {
            id: MenuId.TerminalNewDropdownContext,
            item: {
                command: {
                    id: TerminalCommandId.ConfigureTerminalSettings,
                    title: ( localizeWithPath(_moduleId, 11, "Configure Terminal Settings"))
                },
                group: '3_configure'
            }
        },
        {
            id: MenuId.TerminalNewDropdownContext,
            item: {
                command: {
                    id: 'workbench.action.tasks.runTask',
                    title: ( localizeWithPath(_moduleId, 12, "Run Task..."))
                },
                when: TaskExecutionSupportedContext,
                group: '4_tasks',
                order: 1
            },
        },
        {
            id: MenuId.TerminalNewDropdownContext,
            item: {
                command: {
                    id: 'workbench.action.tasks.configureTaskRunner',
                    title: ( localizeWithPath(_moduleId, 13, "Configure Tasks..."))
                },
                when: TaskExecutionSupportedContext,
                group: '4_tasks',
                order: 2
            },
        }
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.SwitchTerminal,
                    title: ( localize2WithPath(_moduleId, 14, 'Switch Terminal'))
                },
                group: 'navigation',
                order: 0,
                when: ( (ContextKeyExpr.and(
                     (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID)),
                    ContextKeyExpr.not(`config.${TerminalSettingId.TabsEnabled}`)
                ))),
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.Focus,
                    title: terminalStrings.focus
                },
                alt: {
                    id: TerminalCommandId.Split,
                    title: terminalStrings.split.value,
                    icon: Codicon.splitHorizontal
                },
                group: 'navigation',
                order: 0,
                when: ( (ContextKeyExpr.and(
                     (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID)),
                     (ContextKeyExpr.has(`config.${TerminalSettingId.TabsEnabled}`)),
                     (ContextKeyExpr.or( (ContextKeyExpr.and(
                         (ContextKeyExpr.equals(`config.${TerminalSettingId.TabsShowActiveTerminal}`, 'singleTerminal')),
                         (ContextKeyExpr.equals(TerminalContextKeyStrings.GroupCount, 1))
                    )),  (ContextKeyExpr.and( (ContextKeyExpr.equals(
                        `config.${TerminalSettingId.TabsShowActiveTerminal}`,
                        'singleTerminalOrNarrow'
                    )),  (ContextKeyExpr.or(
                         (ContextKeyExpr.equals(TerminalContextKeyStrings.GroupCount, 1)),
                         (ContextKeyExpr.has(TerminalContextKeyStrings.TabsNarrow))
                    )))),  (ContextKeyExpr.and(
                         (ContextKeyExpr.equals(`config.${TerminalSettingId.TabsShowActiveTerminal}`, 'singleGroup')),
                         (ContextKeyExpr.equals(TerminalContextKeyStrings.GroupCount, 1))
                    )),  (ContextKeyExpr.equals(`config.${TerminalSettingId.TabsShowActiveTerminal}`, 'always'))))
                ))),
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.Split,
                    title: terminalStrings.split,
                    icon: Codicon.splitHorizontal
                },
                group: 'navigation',
                order: 2,
                when: TerminalContextKeys.shouldShowViewInlineActions
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.Kill,
                    title: terminalStrings.kill,
                    icon: Codicon.trash
                },
                group: 'navigation',
                order: 3,
                when: TerminalContextKeys.shouldShowViewInlineActions
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.New,
                    title: terminalStrings.new,
                    icon: Codicon.plus
                },
                alt: {
                    id: TerminalCommandId.Split,
                    title: terminalStrings.split.value,
                    icon: Codicon.splitHorizontal
                },
                group: 'navigation',
                order: 0,
                when: ( (ContextKeyExpr.and(
                     (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID)),
                     (ContextKeyExpr.or(
                        TerminalContextKeys.webExtensionContributedProfile,
                        TerminalContextKeys.processSupported
                    ))
                )))
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.Clear,
                    title: ( localizeWithPath(_moduleId, 15, "Clear Terminal")),
                    icon: Codicon.clearAll
                },
                group: 'navigation',
                order: 4,
                when: ( (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID))),
                isHiddenByDefault: true
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.RunActiveFile,
                    title: ( localizeWithPath(_moduleId, 16, "Run Active File")),
                    icon: Codicon.run
                },
                group: 'navigation',
                order: 5,
                when: ( (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID))),
                isHiddenByDefault: true
            }
        },
        {
            id: MenuId.ViewTitle,
            item: {
                command: {
                    id: TerminalCommandId.RunSelectedText,
                    title: ( localizeWithPath(_moduleId, 17, "Run Selected Text")),
                    icon: Codicon.selection
                },
                group: 'navigation',
                order: 6,
                when: ( (ContextKeyExpr.equals('view', TERMINAL_VIEW_ID))),
                isHiddenByDefault: true
            }
        },
    ]);
    MenuRegistry.appendMenuItems([
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.SplitActiveTab,
                    title: terminalStrings.split.value,
                },
                group: ContextMenuGroup.Create,
                order: 1
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.MoveToEditor,
                    title: terminalStrings.moveToEditor.value
                },
                group: ContextMenuGroup.Create,
                order: 2
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.MoveIntoNewWindow,
                    title: terminalStrings.moveIntoNewWindow.value
                },
                group: ContextMenuGroup.Create,
                order: 2
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.RenameActiveTab,
                    title: ( localizeWithPath(_moduleId, 18, "Rename..."))
                },
                group: ContextMenuGroup.Edit
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.ChangeIconActiveTab,
                    title: ( localizeWithPath(_moduleId, 19, "Change Icon..."))
                },
                group: ContextMenuGroup.Edit
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.ChangeColorActiveTab,
                    title: ( localizeWithPath(_moduleId, 20, "Change Color..."))
                },
                group: ContextMenuGroup.Edit
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.SizeToContentWidth,
                    title: terminalStrings.toggleSizeToContentWidth
                },
                group: ContextMenuGroup.Edit
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.JoinActiveTab,
                    title: ( localizeWithPath(_moduleId, 21, "Join Terminals"))
                },
                when: ( (TerminalContextKeys.tabsSingularSelection.toNegated())),
                group: ContextMenuGroup.Config
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.Unsplit,
                    title: terminalStrings.unsplit.value
                },
                when: ( (ContextKeyExpr.and(
                    TerminalContextKeys.tabsSingularSelection,
                    TerminalContextKeys.splitTerminal
                ))),
                group: ContextMenuGroup.Config
            }
        },
        {
            id: MenuId.TerminalTabContext,
            item: {
                command: {
                    id: TerminalCommandId.KillActiveTab,
                    title: terminalStrings.kill.value
                },
                group: ContextMenuGroup.Kill,
            }
        }
    ]);
    MenuRegistry.appendMenuItem(MenuId.EditorTitleContext, {
        command: {
            id: TerminalCommandId.MoveToTerminalPanel,
            title: terminalStrings.moveToTerminalPanel
        },
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal))),
        group: '2_files'
    });
    MenuRegistry.appendMenuItem(MenuId.EditorTitleContext, {
        command: {
            id: TerminalCommandId.Rename,
            title: terminalStrings.rename
        },
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal))),
        group: '2_files'
    });
    MenuRegistry.appendMenuItem(MenuId.EditorTitleContext, {
        command: {
            id: TerminalCommandId.ChangeColor,
            title: terminalStrings.changeColor
        },
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal))),
        group: '2_files'
    });
    MenuRegistry.appendMenuItem(MenuId.EditorTitleContext, {
        command: {
            id: TerminalCommandId.ChangeIcon,
            title: terminalStrings.changeIcon
        },
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal))),
        group: '2_files'
    });
    MenuRegistry.appendMenuItem(MenuId.EditorTitleContext, {
        command: {
            id: TerminalCommandId.SizeToContentWidth,
            title: terminalStrings.toggleSizeToContentWidth
        },
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal))),
        group: '2_files'
    });
    MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
        command: {
            id: TerminalCommandId.CreateTerminalEditorSameGroup,
            title: terminalStrings.new,
            icon: Codicon.plus
        },
        alt: {
            id: TerminalCommandId.Split,
            title: terminalStrings.split.value,
            icon: Codicon.splitHorizontal
        },
        group: 'navigation',
        order: 0,
        when: ( (ResourceContextKey.Scheme.isEqualTo(Schemas.vscodeTerminal)))
    });
}
function getTerminalActionBarArgs(location, profiles, defaultProfileName, contributedProfiles, terminalService, dropdownMenu) {
    let dropdownActions = [];
    let submenuActions = [];
    profiles = profiles.filter(e => !e.isAutoDetected);
    const splitLocation = (location === TerminalLocation.Editor || (typeof location === 'object' && 'viewColumn' in location && location.viewColumn === ACTIVE_GROUP)) ? { viewColumn: SIDE_GROUP } : { splitActiveTerminal: true };
    for (const p of profiles) {
        const isDefault = p.profileName === defaultProfileName;
        const options = { config: p, location };
        const splitOptions = { config: p, location: splitLocation };
        const sanitizedProfileName = p.profileName.replace(/[\n\r\t]/g, '');
        dropdownActions.push(( (new Action(TerminalCommandId.NewWithProfile, isDefault ? ( localizeWithPath(_moduleId, 22, "{0} (Default)", sanitizedProfileName)) : sanitizedProfileName, undefined, true, async () => {
            const instance = await terminalService.createTerminal(options);
            terminalService.setActiveInstance(instance);
            await terminalService.focusActiveInstance();
        }))));
        submenuActions.push(( (new Action(TerminalCommandId.Split, isDefault ? ( localizeWithPath(_moduleId, 22, "{0} (Default)", sanitizedProfileName)) : sanitizedProfileName, undefined, true, async () => {
            const instance = await terminalService.createTerminal(splitOptions);
            terminalService.setActiveInstance(instance);
            await terminalService.focusActiveInstance();
        }))));
    }
    for (const contributed of contributedProfiles) {
        const isDefault = contributed.title === defaultProfileName;
        const title = isDefault ? ( localizeWithPath(_moduleId, 22, "{0} (Default)", contributed.title.replace(/[\n\r\t]/g, ''))) : contributed.title.replace(/[\n\r\t]/g, '');
        dropdownActions.push(( (new Action(
            'contributed',
            title,
            undefined,
            true,
            () => terminalService.createTerminal({
                config: {
                    extensionIdentifier: contributed.extensionIdentifier,
                    id: contributed.id,
                    title
                },
                location
            })
        ))));
        submenuActions.push(( (new Action(
            'contributed-split',
            title,
            undefined,
            true,
            () => terminalService.createTerminal({
                config: {
                    extensionIdentifier: contributed.extensionIdentifier,
                    id: contributed.id,
                    title
                },
                location: splitLocation
            })
        ))));
    }
    const defaultProfileAction = dropdownActions.find(d => d.label.endsWith('(Default)'));
    if (defaultProfileAction) {
        dropdownActions = dropdownActions.filter(d => d !== defaultProfileAction).sort((a, b) => a.label.localeCompare(b.label));
        dropdownActions.unshift(defaultProfileAction);
    }
    if (dropdownActions.length > 0) {
        dropdownActions.push(( (new SubmenuAction('split.profile', ( localizeWithPath(_moduleId, 23, 'Split Terminal')), submenuActions))));
        dropdownActions.push(( (new Separator())));
    }
    const actions = dropdownMenu.getActions();
    dropdownActions.push(...Separator.join(...( (actions.map(a => a[1])))));
    const defaultSubmenuProfileAction = submenuActions.find(d => d.label.endsWith('(Default)'));
    if (defaultSubmenuProfileAction) {
        submenuActions = submenuActions.filter(d => d !== defaultSubmenuProfileAction).sort((a, b) => a.label.localeCompare(b.label));
        submenuActions.unshift(defaultSubmenuProfileAction);
    }
    const dropdownAction = ( (new Action('refresh profiles', ( localizeWithPath(_moduleId, 24, 'Launch Profile...')), 'codicon-chevron-down', true)));
    return { dropdownAction, dropdownMenuActions: dropdownActions, className: `terminal-tab-actions-${terminalService.resolveLocation(location)}` };
}
export { TerminalMenuBarGroup, getTerminalActionBarArgs, setupTerminalMenus };
