import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect } from "react";
import { TooltipProvider } from "../../components/ui/tooltip";
import { NavigationPanelItem } from "./NavigationPanelItem";
import { MorePanelsButton } from "./MorePanelsButton";
import { useNavigationPanelLogic } from "../hooks/useNavigationPanelLogic";
import { WorkspaceButton } from "./WorkspaceButton";
import { HomeButton } from "../ui/HomeButton";
import { HelpButton } from "../menubar/toolbar-sections/HelpButton";
import { cn } from "../../lib/utils";
function isTyping(event) {
    const target = event.target;
    if (!target)
        return false;
    const el = target.nodeType === 3 ? target.parentElement : target;
    if (!el)
        return false;
    const tag = el.tagName?.toLowerCase?.() || "";
    if (tag === "input" || tag === "textarea" || tag === "select")
        return true;
    if (el.isContentEditable)
        return true;
    return !!el.closest('input,textarea,select,[contenteditable=""],[contenteditable="true"],[role="textbox"]');
}
const Separator = () => (_jsx("div", { className: "bg-border-default my-1 h-px w-6 shrink-0" }));
function getSortedWorkspaces(workspaces) {
    const sorted = (workspaces ?? [])
        .filter((workspace) => !workspace.hidden && workspace.id !== "home")
        .sort((a, b) => (a.sortOrder ?? 100) - (b.sortOrder ?? 100));
    const settingsIndex = sorted.findIndex((workspace) => workspace.id === "settings");
    if (settingsIndex >= 0) {
        const settings = sorted[settingsIndex];
        if (settings) {
            sorted.splice(settingsIndex, 1);
            sorted.push(settings);
        }
    }
    return sorted;
}
export function WorkspaceRail({ showSidebarIcons }) {
    const { contextValues, handlers, dragOverId, getVisibleItems } = useNavigationPanelLogic();
    useEffect(() => {
        if (!contextValues)
            return;
        const workspaces = getSortedWorkspaces(contextValues.visibleWorkspaces);
        const handleKeyDown = (event) => {
            if (event.altKey &&
                !event.ctrlKey &&
                !event.metaKey &&
                !isTyping(event)) {
                const numMatch = event.key.match(/^[1-9]$/);
                if (numMatch) {
                    const index = parseInt(event.key, 10) - 1;
                    if (index >= 0 && index < workspaces.length) {
                        event.preventDefault();
                        handlers.onWorkspaceClick(workspaces[index]?.id ?? "home");
                    }
                }
            }
        };
        window.addEventListener("keydown", handleKeyDown);
        return () => window.removeEventListener("keydown", handleKeyDown);
    }, [contextValues, handlers]);
    if (!contextValues)
        return null;
    const { visibleWorkspaces, workspaceId, sidebars, showSidebarLabels } = contextValues;
    const leftSidebars = sidebars
        .filter((sidebar) => sidebar.position === "left" && !sidebar.hidden)
        .sort((a, b) => (a.sortOrder ?? 100) - (b.sortOrder ?? 100));
    const visibleItems = getVisibleItems(leftSidebars);
    const workspaces = getSortedWorkspaces(visibleWorkspaces);
    const settingsWorkspace = workspaces.find((workspace) => workspace.id === "settings");
    const hasSidebarItems = showSidebarIcons && leftSidebars.length > 0;
    const showRailLabels = showSidebarLabels && (hasSidebarItems || settingsWorkspace !== undefined);
    const settingsNumberKey = settingsWorkspace
        ? workspaces.indexOf(settingsWorkspace) < 9
            ? `${workspaces.indexOf(settingsWorkspace) + 1}`
            : undefined
        : undefined;
    if (!hasSidebarItems && workspaces.length === 0)
        return null;
    return (_jsx(TooltipProvider, { delayDuration: 500, skipDelayDuration: 0, children: _jsxs("div", { className: cn("border-border-default flex h-full min-h-0 flex-col items-center gap-1.5 border-r bg-white py-2 pt-1.5", showRailLabels ? "w-[68px] px-1.5" : "w-[53px] px-2"), "data-testid": "workspace-rail", children: [_jsx(HomeButton, { className: "rounded-lg p-1 hover:scale-110" }), hasSidebarItems && _jsx(Separator, {}), hasSidebarItems && (_jsxs("div", { className: "scrollbar-hide flex min-h-0 w-full flex-col items-center gap-1 overflow-y-auto", children: [visibleItems.map((sidebar) => (_jsx(NavigationPanelItem, { sidebar: sidebar, contextValues: contextValues, handlers: handlers, dragOverId: dragOverId }, `sidebar-${sidebar.id}`))), _jsx(MorePanelsButton, { panels: leftSidebars, contextValues: contextValues, handlers: handlers, side: "right" })] })), _jsx("div", { className: "flex-1" }), settingsWorkspace && (_jsxs(_Fragment, { children: [_jsx(WorkspaceButton, { active: settingsWorkspace.id === workspaceId, onClick: () => handlers.onWorkspaceClick(settingsWorkspace.id), workspace: settingsWorkspace, numberKey: settingsNumberKey, showSidebarLabels: showRailLabels }), _jsx(Separator, {})] })), _jsx(HelpButton, { className: "text-neutral-grey-50 hover:border-border-default hover:bg-neutral-grey-5 hover:text-neutral-grey-100 h-9 w-9 rounded-md border border-transparent p-0 [&_svg]:h-5 [&_svg]:w-5 [&_svg]:p-0" })] }) }));
}
//# sourceMappingURL=WorkspaceRail.js.map