
import { registerCss } from '../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import * as sidebarpart from './media/sidebarpart.css';
import './sidebarActions.js';
import { Parts, LayoutSettings, ActivityBarPosition, Position } from '../../../services/layout/browser/layoutService.js';
import { IWorkbenchLayoutService } from '../../../services/layout/browser/layoutService.service.js';
import { ActiveViewletContext, SidebarFocusContext } from '../../../common/contextkeys.js';
import { StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.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 { SIDE_BAR_BORDER, SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_TITLE_BORDER, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, SIDE_BAR_DRAG_AND_DROP_BACKGROUND, ACTIVITY_BAR_TOP_DRAG_AND_DROP_BORDER, ACTIVITY_BAR_BADGE_FOREGROUND, ACTIVITY_BAR_BADGE_BACKGROUND, ACTIVITY_BAR_TOP_INACTIVE_FOREGROUND, ACTIVITY_BAR_TOP_FOREGROUND, ACTIVITY_BAR_TOP_ACTIVE_BORDER } from '../../../common/theme.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import '../../../../base/browser/ui/contextview/contextview.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import '../../../../base/browser/ui/grid/grid.js';
import { assertReturnsDefined } from '../../../../base/common/types.js';
import { ViewContainerLocation } from '../../../common/views.js';
import { IViewDescriptorService } from '../../../common/views.service.js';
import { AbstractPaneCompositePart, CompositeBarPosition } from '../paneCompositePart.js';
import { ActivitybarPart, ActivityBarCompositeBar } from '../activitybar/activitybarPart.js';
import { ActionsOrientation } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { HoverPosition } from '../../../../base/browser/ui/hover/hoverWidget.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { MenuId, registerAction2, Action2 } from '../../../../platform/actions/common/actions.js';
import { IMenuService } from '../../../../platform/actions/common/actions.service.js';
import { Separator } from '../../../../base/common/actions.js';
import { ToggleActivityBarVisibilityActionId } from '../../actions/layoutActions.js';
import { localize2 } from '../../../../nls.js';
import { IHoverService } from '../../../../platform/hover/browser/hover.service.js';
import { VisibleViewContainersTracker } from '../visibleViewContainersTracker.js';
import { Extensions } from '../../panecomposite.js';
import { LayoutPriority } from '../../../../base/browser/ui/splitview/splitview.js';
import '../../../../base/browser/ui/grid/gridview.js';
import { AnchorAlignment } from '../../../../base/common/layout.js';

var SidebarPart_1;
registerCss(sidebarpart);
let SidebarPart = class SidebarPart extends AbstractPaneCompositePart {
    static {
        SidebarPart_1 = this;
    }
    static {
        this.activeViewletSettingsKey = "workbench.sidebar.activeviewletid";
    }
    get snap() {
        return true;
    }
    get preferredWidth() {
        const viewlet = this.getActivePaneComposite();
        if (!viewlet) {
            return undefined;
        }
        const width = viewlet.getOptimalWidth();
        if (typeof width !== "number") {
            return undefined;
        }
        return Math.max(width, 300);
    }
    constructor(
        notificationService,
        storageService,
        contextMenuService,
        layoutService,
        keybindingService,
        hoverService,
        instantiationService,
        themeService,
        viewDescriptorService,
        contextKeyService,
        extensionService,
        configurationService,
        menuService
    ) {
        super(Parts.SIDEBAR_PART, {
            hasTitle: true,
            trailingSeparator: false,
            borderWidth: () => (this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder)) ? 1 : 0
        }, SidebarPart_1.activeViewletSettingsKey, ActiveViewletContext.bindTo(contextKeyService), SidebarFocusContext.bindTo(contextKeyService), "sideBar", "viewlet", SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_TITLE_BORDER, ViewContainerLocation.Sidebar, Extensions.Viewlets, MenuId.SidebarTitle, notificationService, storageService, contextMenuService, layoutService, keybindingService, hoverService, instantiationService, themeService, viewDescriptorService, contextKeyService, extensionService, menuService);
        this.configurationService = configurationService;
        this.minimumWidth = 170;
        this.maximumWidth = Number.POSITIVE_INFINITY;
        this.minimumHeight = 0;
        this.maximumHeight = Number.POSITIVE_INFINITY;
        this.priority = LayoutPriority.Low;
        this.activityBarPart = this._register(
            this.instantiationService.createInstance(ActivitybarPart, this.location, this)
        );
        this.visibleViewContainersTracker = this._register(
            instantiationService.createInstance(VisibleViewContainersTracker, ViewContainerLocation.Sidebar)
        );
        this._register(
            this.visibleViewContainersTracker.onDidChange(e => this.onDidChangeAutoHideViewContainers(e))
        );
        this.rememberActivityBarVisiblePosition();
        this._register(configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration(LayoutSettings.ACTIVITY_BAR_LOCATION)) {
                this.onDidChangeActivityBarLocation();
            }
            if (e.affectsConfiguration(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE)) {
                this.onDidChangeActivityBarLocation();
            }
        }));
        this.registerActions();
    }
    onDidChangeAutoHideViewContainers(e) {
        const activityBarPosition = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION);
        const autoHide = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE);
        if (autoHide && (activityBarPosition === ActivityBarPosition.TOP || activityBarPosition === ActivityBarPosition.BOTTOM)) {
            const visibleBefore = e.before > 1;
            const visibleAfter = e.after > 1;
            if (visibleBefore !== visibleAfter) {
                this.onDidChangeActivityBarLocation();
            }
        }
    }
    onDidChangeActivityBarLocation() {
        this.activityBarPart.hide();
        this.updateCompositeBar();
        const id = this.getActiveComposite()?.getId();
        if (id) {
            this.onTitleAreaUpdate(id);
        }
        if (this.shouldShowActivityBar()) {
            this.activityBarPart.show();
        }
        this.rememberActivityBarVisiblePosition();
    }
    updateStyles() {
        super.updateStyles();
        const container = assertReturnsDefined(this.getContainer());
        container.style.backgroundColor = this.getColor(SIDE_BAR_BACKGROUND) || "";
        container.style.color = this.getColor(SIDE_BAR_FOREGROUND) || "";
        const borderColor = this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder);
        const isPositionLeft = this.layoutService.getSideBarPosition() === Position.LEFT;
        container.style.borderRightWidth = borderColor && isPositionLeft ? "1px" : "";
        container.style.borderRightStyle = borderColor && isPositionLeft ? "solid" : "";
        container.style.borderRightColor = isPositionLeft ? borderColor || "" : "";
        container.style.borderLeftWidth = borderColor && !isPositionLeft ? "1px" : "";
        container.style.borderLeftStyle = borderColor && !isPositionLeft ? "solid" : "";
        container.style.borderLeftColor = !isPositionLeft ? borderColor || "" : "";
        container.style.outlineColor = this.getColor(SIDE_BAR_DRAG_AND_DROP_BACKGROUND) ?? "";
    }
    layout(width, height, top, left) {
        if (!this.layoutService.isVisible(Parts.SIDEBAR_PART)) {
            return;
        }
        super.layout(width, height, top, left);
    }
    getTitleAreaDropDownAnchorAlignment() {
        return this.layoutService.getSideBarPosition() === Position.LEFT ? AnchorAlignment.LEFT : AnchorAlignment.RIGHT;
    }
    createCompositeBar() {
        return this.instantiationService.createInstance(
            ActivityBarCompositeBar,
            ViewContainerLocation.Sidebar,
            this.getCompositeBarOptions(),
            this.partId,
            this,
            false
        );
    }
    getCompositeBarOptions() {
        return {
            partContainerClass: "sidebar",
            pinnedViewContainersKey: ActivitybarPart.pinnedViewContainersKey,
            placeholderViewContainersKey: ActivitybarPart.placeholderViewContainersKey,
            viewContainersWorkspaceStateKey: ActivitybarPart.viewContainersWorkspaceStateKey,
            icon: true,
            orientation: ActionsOrientation.HORIZONTAL,
            recomputeSizes: true,
            activityHoverOptions: {
                position: () => this.getCompositeBarPosition() === CompositeBarPosition.BOTTOM ? HoverPosition.ABOVE : HoverPosition.BELOW
            },
            fillExtraContextMenuActions: actions => {
                if (this.getCompositeBarPosition() === CompositeBarPosition.TITLE) {
                    const viewsSubmenuAction = this.getViewsSubmenuAction();
                    if (viewsSubmenuAction) {
                        actions.push(( new Separator()));
                        actions.push(viewsSubmenuAction);
                    }
                }
            },
            compositeSize: 0,
            iconSize: 16,
            overflowActionSize: 30,
            colors: theme => ({
                activeBackgroundColor: theme.getColor(SIDE_BAR_BACKGROUND),
                inactiveBackgroundColor: theme.getColor(SIDE_BAR_BACKGROUND),
                activeBorderBottomColor: theme.getColor(ACTIVITY_BAR_TOP_ACTIVE_BORDER),
                activeForegroundColor: theme.getColor(ACTIVITY_BAR_TOP_FOREGROUND),
                inactiveForegroundColor: theme.getColor(ACTIVITY_BAR_TOP_INACTIVE_FOREGROUND),
                badgeBackground: theme.getColor(ACTIVITY_BAR_BADGE_BACKGROUND),
                badgeForeground: theme.getColor(ACTIVITY_BAR_BADGE_FOREGROUND),
                dragAndDropBorder: theme.getColor(ACTIVITY_BAR_TOP_DRAG_AND_DROP_BORDER)
            }),
            compact: true
        };
    }
    shouldShowCompositeBar() {
        const activityBarPosition = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION);
        if (activityBarPosition !== ActivityBarPosition.TOP && activityBarPosition !== ActivityBarPosition.BOTTOM) {
            return false;
        }
        const autoHide = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_AUTO_HIDE);
        if (autoHide) {
            const visibleCount = this.visibleViewContainersTracker.visibleCount;
            if (visibleCount <= 1) {
                return false;
            }
        }
        return true;
    }
    shouldShowActivityBar() {
        if (this.shouldShowCompositeBar()) {
            return false;
        }
        return this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION) !== ActivityBarPosition.HIDDEN;
    }
    getCompositeBarPosition() {
        const activityBarPosition = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION);
        switch (activityBarPosition) {
        case ActivityBarPosition.TOP:
            return CompositeBarPosition.TOP;
        case ActivityBarPosition.BOTTOM:
            return CompositeBarPosition.BOTTOM;
        case ActivityBarPosition.HIDDEN:
        case ActivityBarPosition.DEFAULT:
        default:
            return CompositeBarPosition.TITLE;
        }
    }
    rememberActivityBarVisiblePosition() {
        const activityBarPosition = this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION);
        if (activityBarPosition !== ActivityBarPosition.HIDDEN) {
            this.storageService.store(
                LayoutSettings.ACTIVITY_BAR_LOCATION,
                activityBarPosition,
                StorageScope.PROFILE,
                StorageTarget.USER
            );
        }
    }
    getRememberedActivityBarVisiblePosition() {
        const activityBarPosition = this.storageService.get(LayoutSettings.ACTIVITY_BAR_LOCATION, StorageScope.PROFILE);
        switch (activityBarPosition) {
        case ActivityBarPosition.TOP:
            return ActivityBarPosition.TOP;
        case ActivityBarPosition.BOTTOM:
            return ActivityBarPosition.BOTTOM;
        default:
            return ActivityBarPosition.DEFAULT;
        }
    }
    getPinnedPaneCompositeIds() {
        return this.shouldShowCompositeBar() ? super.getPinnedPaneCompositeIds() : this.activityBarPart.getPinnedPaneCompositeIds();
    }
    getVisiblePaneCompositeIds() {
        return this.shouldShowCompositeBar() ? super.getVisiblePaneCompositeIds() : this.activityBarPart.getVisiblePaneCompositeIds();
    }
    getPaneCompositeIds() {
        return this.shouldShowCompositeBar() ? super.getPaneCompositeIds() : this.activityBarPart.getPaneCompositeIds();
    }
    async focusActivityBar() {
        if (this.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION) === ActivityBarPosition.HIDDEN) {
            await this.configurationService.updateValue(
                LayoutSettings.ACTIVITY_BAR_LOCATION,
                this.getRememberedActivityBarVisiblePosition()
            );
            this.onDidChangeActivityBarLocation();
        }
        if (this.shouldShowCompositeBar()) {
            this.focusCompositeBar();
        } else {
            if (!this.layoutService.isVisible(Parts.ACTIVITYBAR_PART)) {
                this.layoutService.setPartHidden(false, Parts.ACTIVITYBAR_PART);
            }
            this.activityBarPart.show(true);
        }
    }
    registerActions() {
        const that = this;
        this._register(registerAction2(class extends Action2 {
            constructor() {
                super({
                    id: ToggleActivityBarVisibilityActionId,
                    title: ( localize2(3844, "Toggle Activity Bar Visibility"))
                });
            }
            run() {
                const value = that.configurationService.getValue(LayoutSettings.ACTIVITY_BAR_LOCATION) === ActivityBarPosition.HIDDEN ? that.getRememberedActivityBarVisiblePosition() : ActivityBarPosition.HIDDEN;
                return that.configurationService.updateValue(LayoutSettings.ACTIVITY_BAR_LOCATION, value);
            }
        }));
    }
    toJSON() {
        return {
            type: Parts.SIDEBAR_PART
        };
    }
};
SidebarPart = SidebarPart_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, IConfigurationService)), ( __param(12, IMenuService))], SidebarPart));

export { SidebarPart };
