
import { registerCss } from '../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import * as auxiliaryBarPart from './media/auxiliaryBarPart.css';
import { localize } from '../../../../nls.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import '../../../../platform/theme/common/colorUtils.js';
import { contrastBorder } from '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { ActiveAuxiliaryContext, AuxiliaryBarFocusContext } from '../../../common/contextkeys.js';
import { SIDE_BAR_BORDER, SIDE_BAR_TITLE_BORDER, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, PANEL_DRAG_AND_DROP_BORDER, ACTIVITY_BAR_TOP_DRAG_AND_DROP_BORDER, ACTIVITY_BAR_BADGE_FOREGROUND, ACTIVITY_BAR_BADGE_BACKGROUND, PANEL_INACTIVE_TITLE_FOREGROUND, ACTIVITY_BAR_TOP_INACTIVE_FOREGROUND, PANEL_ACTIVE_TITLE_FOREGROUND, ACTIVITY_BAR_TOP_FOREGROUND, PANEL_ACTIVE_TITLE_BORDER, ACTIVITY_BAR_TOP_ACTIVE_BORDER } from '../../../common/theme.js';
import { ViewContainerLocation } from '../../../common/views.js';
import { IViewDescriptorService } from '../../../common/views.service.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import { Parts, LayoutSettings, ActivityBarPosition, Position } from '../../../services/layout/browser/layoutService.js';
import { IWorkbenchLayoutService } from '../../../services/layout/browser/layoutService.service.js';
import { HoverPosition } from '../../../../base/browser/ui/hover/hoverWidget.js';
import { Separator, toAction, SubmenuAction } from '../../../../base/common/actions.js';
import { ToggleAuxiliaryBarAction } from './auxiliaryBarActions.js';
import { assertReturnsDefined } from '../../../../base/common/types.js';
import { LayoutPriority } from '../../../../base/browser/ui/splitview/splitview.js';
import { ToggleSidebarPositionAction } from '../../actions/layoutActions.js';
import { ICommandService } from '../../../../platform/commands/common/commands.service.js';
import { AbstractPaneCompositePart, CompositeBarPosition } from '../paneCompositePart.js';
import { ActionsOrientation } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { MenuId } from '../../../../platform/actions/common/actions.js';
import { IMenuService } from '../../../../platform/actions/common/actions.service.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { getContextMenuActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { IHoverService } from '../../../../platform/hover/browser/hover.service.js';
import { VisibleViewContainersTracker } from '../visibleViewContainersTracker.js';
import { Extensions } from '../../panecomposite.js';

var AuxiliaryBarPart_1;
registerCss(auxiliaryBarPart);
let AuxiliaryBarPart = class AuxiliaryBarPart extends AbstractPaneCompositePart {
    static {
        AuxiliaryBarPart_1 = this;
    }
    static {
        this.activeViewSettingsKey = "workbench.auxiliarybar.activepanelid";
    }
    static {
        this.pinnedViewsKey = "workbench.auxiliarybar.pinnedPanels";
    }
    static {
        this.placeholdeViewContainersKey = "workbench.auxiliarybar.placeholderPanels";
    }
    static {
        this.viewContainersWorkspaceStateKey = "workbench.auxiliarybar.viewContainersWorkspaceState";
    }
    get preferredHeight() {
        return this.layoutService.mainContainerDimension.height * 0.4;
    }
    get preferredWidth() {
        const activeComposite = this.getActivePaneComposite();
        if (!activeComposite) {
            return undefined;
        }
        const width = activeComposite.getOptimalWidth();
        if (typeof width !== "number") {
            return undefined;
        }
        return Math.max(width, 300);
    }
    constructor(
        notificationService,
        storageService,
        contextMenuService,
        layoutService,
        keybindingService,
        hoverService,
        instantiationService,
        themeService,
        viewDescriptorService,
        contextKeyService,
        extensionService,
        commandService,
        menuService,
        configurationService
    ) {
        super(Parts.AUXILIARYBAR_PART, {
            hasTitle: true,
            trailingSeparator: true,
            borderWidth: () => (this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder)) ? 1 : 0
        }, AuxiliaryBarPart_1.activeViewSettingsKey, ActiveAuxiliaryContext.bindTo(contextKeyService), AuxiliaryBarFocusContext.bindTo(contextKeyService), "auxiliarybar", "auxiliarybar", undefined, SIDE_BAR_TITLE_BORDER, ViewContainerLocation.AuxiliaryBar, Extensions.Auxiliary, MenuId.AuxiliaryBarTitle, undefined, notificationService, storageService, contextMenuService, layoutService, keybindingService, hoverService, instantiationService, themeService, viewDescriptorService, contextKeyService, extensionService, menuService);
        this.commandService = commandService;
        this.configurationService = configurationService;
        this.minimumWidth = 170;
        this.maximumWidth = Number.POSITIVE_INFINITY;
        this.minimumHeight = 0;
        this.maximumHeight = Number.POSITIVE_INFINITY;
        this.priority = LayoutPriority.Low;
        this.visibleViewContainersTracker = this._register(
            instantiationService.createInstance(VisibleViewContainersTracker, ViewContainerLocation.AuxiliaryBar)
        );
        this._register(
            this.visibleViewContainersTracker.onDidChange(e => this.onDidChangeAutoHideViewContainers(e))
        );
        this.configuration = this.resolveConfiguration();
        this._register(configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration(LayoutSettings.ACTIVITY_BAR_LOCATION)) {
                this.configuration = this.resolveConfiguration();
                this.onDidChangeActivityBarLocation();
            } else if (e.affectsConfiguration("workbench.secondarySideBar.showLabels")) {
                this.configuration = this.resolveConfiguration();
                this.updateCompositeBar(true);
            } else if (e.affectsConfiguration(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE)) {
                this.onDidChangeActivityBarLocation();
            }
        }));
    }
    onDidChangeAutoHideViewContainers(e) {
        const autoHide = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE);
        if (autoHide && (this.configuration.position === ActivityBarPosition.TOP || this.configuration.position === ActivityBarPosition.BOTTOM)) {
            const visibleBefore = e.before > 1;
            const visibleAfter = e.after > 1;
            if (visibleBefore !== visibleAfter) {
                this.onDidChangeActivityBarLocation();
            }
        }
    }
    resolveConfiguration() {
        const position = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION);
        const canShowLabels = position !== ActivityBarPosition.TOP && position !== ActivityBarPosition.BOTTOM;
        const showLabels = canShowLabels && this.configurationService.getValue("workbench.secondarySideBar.showLabels") !== false;
        return {
            position,
            canShowLabels,
            showLabels
        };
    }
    onDidChangeActivityBarLocation() {
        this.updateCompositeBar();
        const id = this.getActiveComposite()?.getId();
        if (id) {
            this.onTitleAreaUpdate(id);
        }
    }
    updateStyles() {
        super.updateStyles();
        const container = assertReturnsDefined(this.getContainer());
        container.style.backgroundColor = this.getColor(SIDE_BAR_BACKGROUND) || "";
        const borderColor = this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder);
        const isPositionLeft = this.layoutService.getSideBarPosition() === Position.RIGHT;
        container.style.color = this.getColor(SIDE_BAR_FOREGROUND) || "";
        container.style.borderLeftColor = borderColor ?? "";
        container.style.borderRightColor = borderColor ?? "";
        container.style.borderLeftStyle = borderColor && !isPositionLeft ? "solid" : "none";
        container.style.borderRightStyle = borderColor && isPositionLeft ? "solid" : "none";
        container.style.borderLeftWidth = borderColor && !isPositionLeft ? "1px" : "0px";
        container.style.borderRightWidth = borderColor && isPositionLeft ? "1px" : "0px";
    }
    getCompositeBarOptions() {
        const $this = this;
        return {
            partContainerClass: "auxiliarybar",
            pinnedViewContainersKey: AuxiliaryBarPart_1.pinnedViewsKey,
            placeholderViewContainersKey: AuxiliaryBarPart_1.placeholdeViewContainersKey,
            viewContainersWorkspaceStateKey: AuxiliaryBarPart_1.viewContainersWorkspaceStateKey,
            icon: !this.configuration.showLabels,
            orientation: ActionsOrientation.HORIZONTAL,
            recomputeSizes: true,
            activityHoverOptions: {
                position: () => this.getCompositeBarPosition() === CompositeBarPosition.BOTTOM ? HoverPosition.ABOVE : HoverPosition.BELOW
            },
            fillExtraContextMenuActions: actions => this.fillExtraContextMenuActions(actions),
            compositeSize: 0,
            iconSize: 16,
            get overflowActionSize() {
                return $this.getCompositeBarPosition() === CompositeBarPosition.TITLE ? 40 : 30;
            },
            colors: theme => ({
                activeBackgroundColor: theme.getColor(SIDE_BAR_BACKGROUND),
                inactiveBackgroundColor: theme.getColor(SIDE_BAR_BACKGROUND),
                get activeBorderBottomColor() {
                    return $this.getCompositeBarPosition() === CompositeBarPosition.TITLE ? theme.getColor(PANEL_ACTIVE_TITLE_BORDER) : theme.getColor(ACTIVITY_BAR_TOP_ACTIVE_BORDER);
                },
                get activeForegroundColor() {
                    return $this.getCompositeBarPosition() === CompositeBarPosition.TITLE ? theme.getColor(PANEL_ACTIVE_TITLE_FOREGROUND) : theme.getColor(ACTIVITY_BAR_TOP_FOREGROUND);
                },
                get inactiveForegroundColor() {
                    return $this.getCompositeBarPosition() === CompositeBarPosition.TITLE ? theme.getColor(PANEL_INACTIVE_TITLE_FOREGROUND) : theme.getColor(ACTIVITY_BAR_TOP_INACTIVE_FOREGROUND);
                },
                badgeBackground: theme.getColor(ACTIVITY_BAR_BADGE_BACKGROUND),
                badgeForeground: theme.getColor(ACTIVITY_BAR_BADGE_FOREGROUND),
                get dragAndDropBorder() {
                    return $this.getCompositeBarPosition() === CompositeBarPosition.TITLE ? theme.getColor(PANEL_DRAG_AND_DROP_BORDER) : theme.getColor(ACTIVITY_BAR_TOP_DRAG_AND_DROP_BORDER);
                }
            }),
            compact: true
        };
    }
    fillExtraContextMenuActions(actions) {
        const currentPositionRight = this.layoutService.getSideBarPosition() === Position.LEFT;
        if (this.getCompositeBarPosition() === CompositeBarPosition.TITLE) {
            const viewsSubmenuAction = this.getViewsSubmenuAction();
            if (viewsSubmenuAction) {
                actions.push(( new Separator()));
                actions.push(viewsSubmenuAction);
            }
        }
        const activityBarPositionMenu = this.menuService.getMenuActions(MenuId.ActivityBarPositionMenu, this.contextKeyService, {
            shouldForwardArgs: true,
            renderShortTitle: true
        });
        const positionActions = getContextMenuActions(activityBarPositionMenu).secondary;
        const toggleShowLabelsAction = toAction({
            id: "workbench.action.auxiliarybar.toggleShowLabels",
            label: this.configuration.showLabels ? ( localize(3085, "Show Icons")) : ( localize(3086, "Show Labels")),
            enabled: this.configuration.canShowLabels,
            run: () => this.configurationService.updateValue("workbench.secondarySideBar.showLabels", !this.configuration.showLabels)
        });
        actions.push(...[( new Separator()), ( new SubmenuAction("workbench.action.panel.position", ( localize(3087, "Activity Bar Position")), positionActions)), toAction({
            id: ToggleSidebarPositionAction.ID,
            label: currentPositionRight ? ( localize(3088, "Move Secondary Side Bar Left")) : ( localize(3089, "Move Secondary Side Bar Right")),
            run: () => this.commandService.executeCommand(ToggleSidebarPositionAction.ID)
        }), toggleShowLabelsAction, toAction({
            id: ToggleAuxiliaryBarAction.ID,
            label: ( localize(3090, "Hide Secondary Side Bar")),
            run: () => this.commandService.executeCommand(ToggleAuxiliaryBarAction.ID)
        })]);
    }
    shouldShowCompositeBar() {
        if (this.configuration.position === ActivityBarPosition.HIDDEN) {
            return false;
        }
        if (this.configuration.position === ActivityBarPosition.TOP || this.configuration.position === ActivityBarPosition.BOTTOM) {
            const autoHide = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE);
            if (autoHide) {
                const visibleCount = this.visibleViewContainersTracker.visibleCount;
                if (visibleCount <= 1) {
                    return false;
                }
            }
        }
        return true;
    }
    getCompositeBarPosition() {
        switch (this.configuration.position) {
        case ActivityBarPosition.TOP:
            return CompositeBarPosition.TOP;
        case ActivityBarPosition.BOTTOM:
            return CompositeBarPosition.BOTTOM;
        case ActivityBarPosition.HIDDEN:
            return CompositeBarPosition.TITLE;
        case ActivityBarPosition.DEFAULT:
            return CompositeBarPosition.TITLE;
        default:
            return CompositeBarPosition.TITLE;
        }
    }
    toJSON() {
        return {
            type: Parts.AUXILIARYBAR_PART
        };
    }
};
AuxiliaryBarPart = AuxiliaryBarPart_1 = ( __decorate([( __param(0, INotificationService)), ( __param(1, IStorageService)), ( __param(2, IContextMenuService)), ( __param(3, IWorkbenchLayoutService)), ( __param(4, IKeybindingService)), ( __param(5, IHoverService)), ( __param(6, IInstantiationService)), ( __param(7, IThemeService)), ( __param(8, IViewDescriptorService)), ( __param(9, IContextKeyService)), ( __param(10, IExtensionService)), ( __param(11, ICommandService)), ( __param(12, IMenuService)), ( __param(13, IConfigurationService))], AuxiliaryBarPart));

export { AuxiliaryBarPart };
