import './media/panelpart.css.js';
import { localize2WithPath, localizeWithPath } from '../../../../nls.js';
import { Action2, MenuId, registerAction2, MenuRegistry } from '../../../../platform/actions/common/actions.js';
import { Categories } from '../../../../platform/action/common/actionCommonCategories.js';
import { IWorkbenchLayoutService, positionToString } from '../../../services/layout/browser/layoutService.js';
import { PanelVisibleContext, PanelAlignmentContext, PanelPositionContext, PanelMaximizedContext, AuxiliaryBarVisibleContext } from '../../../common/contextkeys.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
import { ViewContainerLocationToString, IViewDescriptorService } from '../../../common/views.js';
import { IViewsService } from '../../../services/views/common/viewsService.js';
import { IPaneCompositePartService } from '../../../services/panecomposite/browser/panecomposite.js';
import { INotificationService } from '../../../../platform/notification/common/notification.js';
const maximizeIcon = registerIcon('panel-maximize', Codicon.chevronUp, ( localizeWithPath(
    'vs/workbench/browser/parts/panel/panelActions',
    'maximizeIcon',
    'Icon to maximize a panel.'
)));
const restoreIcon = registerIcon('panel-restore', Codicon.chevronDown, ( localizeWithPath(
    'vs/workbench/browser/parts/panel/panelActions',
    'restoreIcon',
    'Icon to restore a panel.'
)));
const closeIcon = registerIcon('panel-close', Codicon.close, ( localizeWithPath(
    'vs/workbench/browser/parts/panel/panelActions',
    'closeIcon',
    'Icon to close a panel.'
)));
const panelIcon = registerIcon('panel-layout-icon', Codicon.layoutPanel, ( localizeWithPath(
    'vs/workbench/browser/parts/panel/panelActions',
    'togglePanelOffIcon',
    'Icon to toggle the panel off when it is on.'
)));
const panelOffIcon = registerIcon('panel-layout-icon-off', Codicon.layoutPanelOff, ( localizeWithPath(
    'vs/workbench/browser/parts/panel/panelActions',
    'togglePanelOnIcon',
    'Icon to toggle the panel on when it is off.'
)));
class TogglePanelAction extends Action2 {
    static { this.ID = 'workbench.action.togglePanel'; }
    static { this.LABEL = ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'togglePanelVisibility',
        "Toggle Panel Visibility"
    )); }
    constructor() {
        super({
            id: TogglePanelAction.ID,
            title: TogglePanelAction.LABEL,
            toggled: {
                condition: PanelVisibleContext,
                title: ( localizeWithPath('vs/workbench/browser/parts/panel/panelActions', 'toggle panel', "Panel")),
                mnemonicTitle: ( localizeWithPath(
                    'vs/workbench/browser/parts/panel/panelActions',
                    { key: 'toggle panel mnemonic', comment: ['&& denotes a mnemonic'] },
                    "&&Panel"
                )),
            },
            f1: true,
            category: Categories.View,
            keybinding: { primary: 2048  | 40 , weight: 200  },
            menu: [
                {
                    id: MenuId.MenubarAppearanceMenu,
                    group: '2_workbench_layout',
                    order: 5
                }, {
                    id: MenuId.LayoutControlMenuSubmenu,
                    group: '0_workbench_layout',
                    order: 4
                },
            ]
        });
    }
    async run(accessor) {
        const layoutService = accessor.get(IWorkbenchLayoutService);
        layoutService.setPartHidden(layoutService.isVisible("workbench.parts.panel" ), "workbench.parts.panel" );
    }
}
registerAction2(TogglePanelAction);
registerAction2(class extends Action2 {
    static { this.ID = 'workbench.action.focusPanel'; }
    static { this.LABEL = ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'focusPanel',
        "Focus into Panel"
    )); }
    constructor() {
        super({
            id: 'workbench.action.focusPanel',
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'focusPanel',
                "Focus into Panel"
            )),
            category: Categories.View,
            f1: true,
        });
    }
    async run(accessor) {
        const layoutService = accessor.get(IWorkbenchLayoutService);
        const paneCompositeService = accessor.get(IPaneCompositePartService);
        if (!layoutService.isVisible("workbench.parts.panel" )) {
            layoutService.setPartHidden(false, "workbench.parts.panel" );
        }
        const panel = paneCompositeService.getActivePaneComposite(1 );
        panel?.focus();
    }
});
const PositionPanelActionId = {
    LEFT: 'workbench.action.positionPanelLeft',
    RIGHT: 'workbench.action.positionPanelRight',
    BOTTOM: 'workbench.action.positionPanelBottom',
};
const AlignPanelActionId = {
    LEFT: 'workbench.action.alignPanelLeft',
    RIGHT: 'workbench.action.alignPanelRight',
    CENTER: 'workbench.action.alignPanelCenter',
    JUSTIFY: 'workbench.action.alignPanelJustify',
};
function createPanelActionConfig(id, title, shortLabel, value, when) {
    return {
        id,
        title,
        shortLabel,
        value,
        when,
    };
}
function createPositionPanelActionConfig(id, title, shortLabel, position) {
    return createPanelActionConfig(id, title, shortLabel, position, ( PanelPositionContext.notEqualsTo(positionToString(position))));
}
function createAlignmentPanelActionConfig(id, title, shortLabel, alignment) {
    return createPanelActionConfig(id, title, shortLabel, alignment, ( PanelAlignmentContext.notEqualsTo(alignment)));
}
const PositionPanelActionConfigs = [
    createPositionPanelActionConfig(PositionPanelActionId.LEFT, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelLeft',
        "Move Panel Left"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelLeftShort',
        "Left"
    )), 0 ),
    createPositionPanelActionConfig(PositionPanelActionId.RIGHT, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelRight',
        "Move Panel Right"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelRightShort',
        "Right"
    )), 1 ),
    createPositionPanelActionConfig(PositionPanelActionId.BOTTOM, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelBottom',
        "Move Panel To Bottom"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanelBottomShort',
        "Bottom"
    )), 2 ),
];
const AlignPanelActionConfigs = [
    createAlignmentPanelActionConfig(AlignPanelActionId.LEFT, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelLeft',
        "Set Panel Alignment to Left"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelLeftShort',
        "Left"
    )), 'left'),
    createAlignmentPanelActionConfig(AlignPanelActionId.RIGHT, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelRight',
        "Set Panel Alignment to Right"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelRightShort',
        "Right"
    )), 'right'),
    createAlignmentPanelActionConfig(AlignPanelActionId.CENTER, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelCenter',
        "Set Panel Alignment to Center"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelCenterShort',
        "Center"
    )), 'center'),
    createAlignmentPanelActionConfig(AlignPanelActionId.JUSTIFY, ( localize2WithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelJustify',
        "Set Panel Alignment to Justify"
    )), ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanelJustifyShort',
        "Justify"
    )), 'justify'),
];
MenuRegistry.appendMenuItem(MenuId.MenubarAppearanceMenu, {
    submenu: MenuId.PanelPositionMenu,
    title: ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'positionPanel',
        "Panel Position"
    )),
    group: '3_workbench_layout_move',
    order: 4
});
PositionPanelActionConfigs.forEach(positionPanelAction => {
    const { id, title, shortLabel, value, when } = positionPanelAction;
    registerAction2(class extends Action2 {
        constructor() {
            super({
                id,
                title,
                category: Categories.View,
                f1: true
            });
        }
        run(accessor) {
            const layoutService = accessor.get(IWorkbenchLayoutService);
            layoutService.setPanelPosition(value === undefined ? 2  : value);
        }
    });
    MenuRegistry.appendMenuItem(MenuId.PanelPositionMenu, {
        command: {
            id,
            title: shortLabel,
            toggled: ( when.negate())
        },
        order: 5
    });
});
MenuRegistry.appendMenuItem(MenuId.MenubarAppearanceMenu, {
    submenu: MenuId.PanelAlignmentMenu,
    title: ( localizeWithPath(
        'vs/workbench/browser/parts/panel/panelActions',
        'alignPanel',
        "Align Panel"
    )),
    group: '3_workbench_layout_move',
    order: 5
});
AlignPanelActionConfigs.forEach(alignPanelAction => {
    const { id, title, shortLabel, value, when } = alignPanelAction;
    registerAction2(class extends Action2 {
        constructor() {
            super({
                id,
                title,
                category: Categories.View,
                toggled: ( when.negate()),
                f1: true
            });
        }
        run(accessor) {
            const layoutService = accessor.get(IWorkbenchLayoutService);
            layoutService.setPanelAlignment(value === undefined ? 'center' : value);
        }
    });
    MenuRegistry.appendMenuItem(MenuId.PanelAlignmentMenu, {
        command: {
            id,
            title: shortLabel,
            toggled: ( when.negate())
        },
        order: 5
    });
});
class SwitchPanelViewAction extends Action2 {
    constructor(id, title) {
        super({
            id,
            title,
            category: Categories.View,
            f1: true,
        });
    }
    async run(accessor, offset) {
        const paneCompositeService = accessor.get(IPaneCompositePartService);
        const pinnedPanels = paneCompositeService.getVisiblePaneCompositeIds(1 );
        const activePanel = paneCompositeService.getActivePaneComposite(1 );
        if (!activePanel) {
            return;
        }
        let targetPanelId;
        for (let i = 0; i < pinnedPanels.length; i++) {
            if (pinnedPanels[i] === activePanel.getId()) {
                targetPanelId = pinnedPanels[(i + pinnedPanels.length + offset) % pinnedPanels.length];
                break;
            }
        }
        if (typeof targetPanelId === 'string') {
            await paneCompositeService.openPaneComposite(targetPanelId, 1 , true);
        }
    }
}
registerAction2(class extends SwitchPanelViewAction {
    constructor() {
        super('workbench.action.previousPanelView', ( localize2WithPath(
            'vs/workbench/browser/parts/panel/panelActions',
            'previousPanelView',
            "Previous Panel View"
        )));
    }
    run(accessor) {
        return super.run(accessor, -1);
    }
});
registerAction2(class extends SwitchPanelViewAction {
    constructor() {
        super('workbench.action.nextPanelView', ( localize2WithPath(
            'vs/workbench/browser/parts/panel/panelActions',
            'nextPanelView',
            "Next Panel View"
        )));
    }
    run(accessor) {
        return super.run(accessor, 1);
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'workbench.action.toggleMaximizedPanel',
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'toggleMaximizedPanel',
                'Toggle Maximized Panel'
            )),
            tooltip: ( localizeWithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'maximizePanel',
                "Maximize Panel Size"
            )),
            category: Categories.View,
            f1: true,
            icon: maximizeIcon,
            precondition: ( ContextKeyExpr.or(( PanelAlignmentContext.isEqualTo('center')), ( PanelPositionContext.notEqualsTo('bottom')))),
            toggled: { condition: PanelMaximizedContext, icon: restoreIcon, tooltip: ( localizeWithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'minimizePanel',
                "Restore Panel Size"
            )) },
            menu: [{
                    id: MenuId.PanelTitle,
                    group: 'navigation',
                    order: 1,
                    when: ( ContextKeyExpr.or(( PanelAlignmentContext.isEqualTo('center')), ( PanelPositionContext.notEqualsTo('bottom'))))
                }]
        });
    }
    run(accessor) {
        const layoutService = accessor.get(IWorkbenchLayoutService);
        const notificationService = accessor.get(INotificationService);
        if (layoutService.getPanelAlignment() !== 'center' && layoutService.getPanelPosition() === 2 ) {
            notificationService.warn(( localizeWithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'panelMaxNotSupported',
                "Maximizing the panel is only supported when it is center aligned."
            )));
            return;
        }
        if (!layoutService.isVisible("workbench.parts.panel" )) {
            layoutService.setPartHidden(false, "workbench.parts.panel" );
            if (!layoutService.isPanelMaximized()) {
                layoutService.toggleMaximizedPanel();
            }
        }
        else {
            layoutService.toggleMaximizedPanel();
        }
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'workbench.action.closePanel',
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'closePanel',
                'Hide Panel'
            )),
            category: Categories.View,
            icon: closeIcon,
            menu: [{
                    id: MenuId.CommandPalette,
                    when: PanelVisibleContext,
                }, {
                    id: MenuId.PanelTitle,
                    group: 'navigation',
                    order: 2
                }]
        });
    }
    run(accessor) {
        accessor.get(IWorkbenchLayoutService).setPartHidden(true, "workbench.parts.panel" );
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'workbench.action.closeAuxiliaryBar',
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'closeSecondarySideBar',
                'Hide Secondary Side Bar'
            )),
            category: Categories.View,
            icon: closeIcon,
            menu: [{
                    id: MenuId.CommandPalette,
                    when: AuxiliaryBarVisibleContext,
                }, {
                    id: MenuId.AuxiliaryBarTitle,
                    group: 'navigation',
                    order: 2,
                    when: ( ContextKeyExpr.notEquals(
                        `config.${"workbench.activityBar.location" }`,
                        "top"
                    ))
                }, {
                    id: MenuId.AuxiliaryBarHeader,
                    group: 'navigation',
                    when: ( ContextKeyExpr.equals(
                        `config.${"workbench.activityBar.location" }`,
                        "top"
                    ))
                }]
        });
    }
    run(accessor) {
        accessor.get(IWorkbenchLayoutService).setPartHidden(true, "workbench.parts.auxiliarybar" );
    }
});
MenuRegistry.appendMenuItems([
    {
        id: MenuId.LayoutControlMenu,
        item: {
            group: '0_workbench_toggles',
            command: {
                id: TogglePanelAction.ID,
                title: ( localizeWithPath(
                    'vs/workbench/browser/parts/panel/panelActions',
                    'togglePanel',
                    "Toggle Panel"
                )),
                icon: panelOffIcon,
                toggled: { condition: PanelVisibleContext, icon: panelIcon }
            },
            when: ( ContextKeyExpr.or(( ContextKeyExpr.equals('config.workbench.layoutControl.type', 'toggles')), ( ContextKeyExpr.equals('config.workbench.layoutControl.type', 'both')))),
            order: 1
        }
    }, {
        id: MenuId.ViewTitleContext,
        item: {
            group: '3_workbench_layout_move',
            command: {
                id: TogglePanelAction.ID,
                title: ( localize2WithPath('vs/workbench/browser/parts/panel/panelActions', 'hidePanel', 'Hide Panel')),
            },
            when: ( ContextKeyExpr.and(PanelVisibleContext, ( ContextKeyExpr.equals(
                'viewLocation',
                ViewContainerLocationToString(1 )
            )))),
            order: 2
        }
    }
]);
class MoveViewsBetweenPanelsAction extends Action2 {
    constructor(source, destination, desc) {
        super(desc);
        this.source = source;
        this.destination = destination;
    }
    run(accessor, ...args) {
        const viewDescriptorService = accessor.get(IViewDescriptorService);
        const layoutService = accessor.get(IWorkbenchLayoutService);
        const viewsService = accessor.get(IViewsService);
        const srcContainers = viewDescriptorService.getViewContainersByLocation(this.source);
        const destContainers = viewDescriptorService.getViewContainersByLocation(this.destination);
        if (srcContainers.length) {
            const activeViewContainer = viewsService.getVisibleViewContainer(this.source);
            srcContainers.forEach(viewContainer => viewDescriptorService.moveViewContainerToLocation(viewContainer, this.destination, undefined, this.desc.id));
            layoutService.setPartHidden(false, this.destination === 1  ? "workbench.parts.panel"  : "workbench.parts.auxiliarybar" );
            if (activeViewContainer && destContainers.length === 0) {
                viewsService.openViewContainer(activeViewContainer.id, true);
            }
        }
    }
}
class MovePanelToSidePanelAction extends MoveViewsBetweenPanelsAction {
    static { this.ID = 'workbench.action.movePanelToSidePanel'; }
    constructor() {
        super(1 , 2 , {
            id: MovePanelToSidePanelAction.ID,
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'movePanelToSecondarySideBar',
                "Move Panel Views To Secondary Side Bar"
            )),
            category: Categories.View,
            f1: false
        });
    }
}
class MovePanelToSecondarySideBarAction extends MoveViewsBetweenPanelsAction {
    static { this.ID = 'workbench.action.movePanelToSecondarySideBar'; }
    constructor() {
        super(1 , 2 , {
            id: MovePanelToSecondarySideBarAction.ID,
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'movePanelToSecondarySideBar',
                "Move Panel Views To Secondary Side Bar"
            )),
            category: Categories.View,
            f1: true
        });
    }
}
registerAction2(MovePanelToSidePanelAction);
registerAction2(MovePanelToSecondarySideBarAction);
class MoveSidePanelToPanelAction extends MoveViewsBetweenPanelsAction {
    static { this.ID = 'workbench.action.moveSidePanelToPanel'; }
    constructor() {
        super(2 , 1 , {
            id: MoveSidePanelToPanelAction.ID,
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'moveSidePanelToPanel',
                "Move Secondary Side Bar Views To Panel"
            )),
            category: Categories.View,
            f1: false
        });
    }
}
class MoveSecondarySideBarToPanelAction extends MoveViewsBetweenPanelsAction {
    static { this.ID = 'workbench.action.moveSecondarySideBarToPanel'; }
    constructor() {
        super(2 , 1 , {
            id: MoveSecondarySideBarToPanelAction.ID,
            title: ( localize2WithPath(
                'vs/workbench/browser/parts/panel/panelActions',
                'moveSidePanelToPanel',
                "Move Secondary Side Bar Views To Panel"
            )),
            category: Categories.View,
            f1: true
        });
    }
}
registerAction2(MoveSidePanelToPanelAction);
registerAction2(MoveSecondarySideBarToPanelAction);
export { MovePanelToSecondarySideBarAction, MoveSecondarySideBarToPanelAction, TogglePanelAction };
