import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useSyncExternalStore } from "react";
import { Button } from "../../components/ui/button";
import { RotateCcw, Route, Settings, SquarePen } from "lucide-react";
import { subscribeTaskBoardNav, getTaskBoardNavSnapshot, } from "../taskBoardNavStore";
import { useEditContext } from "../../editor/client/editContext";
import { SimpleIconButton } from "../../editor/ui/SimpleIconButton";
import { TaskBoardBreadcrumb } from "./TaskBoardBreadcrumb";
import { TaskboardPersistentLogPanel } from "./TaskboardPersistentLogPanel";
import { TaskBoardProjectSelector } from "./TaskBoardProjectSelector";
export function TaskBoardTitlebar() {
    const editContext = useEditContext();
    const nav = useSyncExternalStore(subscribeTaskBoardNav, getTaskBoardNavSnapshot, getTaskBoardNavSnapshot);
    const showEditorPanel = editContext?.showAgentsWorkspaceEditor ?? false;
    const breadcrumbItems = useMemo(() => {
        if (!nav.selectedProjectId) {
            return [];
        }
        const projectById = new Map(nav.projectOptions.map((project) => [project.projectId, project]));
        const items = [];
        const visited = new Set();
        let currentProjectId = nav.selectedProjectId;
        while (currentProjectId && !visited.has(currentProjectId)) {
            visited.add(currentProjectId);
            const project = projectById.get(currentProjectId);
            const isCurrent = currentProjectId === nav.selectedProjectId;
            const description = (isCurrent ? nav.projectSubtitle : (project?.description ?? "")).trim();
            items.unshift({
                projectId: currentProjectId,
                title: isCurrent
                    ? nav.projectTitle
                    : (project?.title ?? currentProjectId),
                subtitle: isCurrent
                    ? [description, nav.permissionLabel].filter(Boolean).join(" · ") ||
                        undefined
                    : description || undefined,
            });
            currentProjectId =
                project?.parentProjectId && projectById.has(project.parentProjectId)
                    ? project.parentProjectId
                    : null;
        }
        return items;
    }, [
        nav.permissionLabel,
        nav.projectOptions,
        nav.projectSubtitle,
        nav.projectTitle,
        nav.selectedProjectId,
    ]);
    const currentBreadcrumbItem = breadcrumbItems[breadcrumbItems.length - 1];
    if (editContext?.isMobile && !nav.hasSelectedProject) {
        return null;
    }
    return (_jsxs("div", { className: "flex w-full items-center justify-between gap-4 pr-2 pl-2", children: [_jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [_jsx(TaskBoardBreadcrumb, { rootSubtitle: "Parhelia taskboard", items: breadcrumbItems, rootContent: !nav.hasSelectedProject ? (_jsx(TaskBoardProjectSelector, { variant: "breadcrumb", label: "Tasks", subtitle: "Parhelia taskboard" })) : undefined, currentItemContent: currentBreadcrumbItem ? (_jsx(TaskBoardProjectSelector, { variant: "breadcrumb", label: currentBreadcrumbItem.title, subtitle: currentBreadcrumbItem.subtitle })) : undefined, onRootClick: () => nav.onSelectProject?.(null), onItemClick: (projectId) => nav.onSelectProject?.(projectId) }), nav.hasSelectedProject ? (_jsxs(_Fragment, { children: [_jsx("div", { className: "bg-neutral-grey-10 mx-1 h-8 w-px" }), _jsx(Button, { variant: "outline", size: "icon", onClick: () => {
                                    if (nav.selectedProjectId) {
                                        nav.onOpenProjectSettings?.(nav.selectedProjectId);
                                    }
                                }, disabled: !nav.canManageProject, className: "h-8 w-8", title: "Project settings", "data-testid": "taskboard-project-settings-button", children: _jsx(Settings, { className: "text-neutral-grey-50 h-4 w-4" }) }), _jsx(Button, { variant: "outline", size: "icon", onClick: () => {
                                    if (nav.selectedProjectId) {
                                        nav.onRestartProject?.(nav.selectedProjectId, nav.projectTitle);
                                    }
                                }, disabled: !nav.canManageProject || !nav.onRestartProject, className: "h-8 w-8", title: "Restart project (delete all tasks and subprojects, recreate from template)", "data-testid": "taskboard-project-restart-button", children: _jsx(RotateCcw, { className: "text-neutral-grey-50 h-4 w-4" }) }), nav.onToggleWizardMode && (_jsx(SimpleIconButton, { icon: _jsx(Route, { className: "h-4 w-4" }), label: nav.isWizardMode ? "Exit Wizard mode" : "Wizard mode", className: "bg-background hover:bg-accent hover:text-accent-foreground text-neutral-grey-50 h-8 w-8 rounded border", "data-testid": "taskboard-wizard-switcher", selected: nav.isWizardMode, onClick: () => nav.onToggleWizardMode?.(!nav.isWizardMode) })), nav.selectedProjectId ? (_jsx(TaskboardPersistentLogPanel, { scope: "project", projectId: nav.selectedProjectId, title: "Project Activity Log", emptyMessage: "No project-level orchestration entries yet.", refreshKey: nav.selectedProjectId, testId: "taskboard-project-dashboard-log-panel" })) : null] })) : null] }), !editContext?.isMobile && (_jsx("div", { className: "flex items-center gap-2", children: _jsx(SimpleIconButton, { icon: _jsx(SquarePen, { className: "h-5 w-5", strokeWidth: 1 }), label: showEditorPanel ? "Hide Editor" : "Show Editor", size: "large", "data-testid": "taskboard-editor-panel-toggle", selected: showEditorPanel, onClick: () => editContext?.setShowAgentsWorkspaceEditor(!showEditorPanel) }) }))] }));
}
//# sourceMappingURL=TaskBoardTitlebar.js.map