'use client';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useState, useEffect } from 'react';
import { Plus } from 'lucide-react';
import { Sidebar } from './Sidebar.js';
import { Header } from './Header.js';
import { Breadcrumbs } from '../components/Breadcrumbs.js';
import { AdminShell } from './primitives/AdminShell.js';
import { Toaster } from 'sonner';
/**
 * Layout — thin shell that wires Sidebar/Header/Breadcrumbs into the
 * `<AdminShell>` primitive.
 *
 * `AdminShell` (in `./primitives/AdminShell.tsx`) owns the actual layout
 * algorithm (CSS Grid on desktop, fixed slide-in overlay on mobile,
 * JS-driven `matchMedia` breakpoint) so that every layout decision lives
 * in one place. Per-product chrome (Sidebar, Header, Breadcrumbs)
 * composes on top of it.
 *
 * DO NOT re-implement layout logic in this file — extend `AdminShell`
 * instead. The previous flex + `fixed`↔`static` toggle caused recurring
 * sidebar-overlap bugs; the grid-based primitive is the contract.
 */
export function Layout({ config, session, currentPath, onNavigate, onLogout, children, }) {
    const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
    const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false);
    useEffect(() => {
        setMobileSidebarOpen(false);
    }, [currentPath]);
    const pageActions = useMemo(() => computePageActions({ currentPath, config, onNavigate }), [currentPath, config, onNavigate]);
    return (_jsxs(_Fragment, { children: [_jsx(Toaster, { position: "bottom-right" }), _jsx(AdminShell, { mobileSidebarOpen: mobileSidebarOpen, onMobileSidebarClose: () => setMobileSidebarOpen(false), sidebar: _jsx(Sidebar, { collapsed: sidebarCollapsed, onToggleCollapse: () => setSidebarCollapsed(!sidebarCollapsed), currentPath: currentPath, onNavigate: onNavigate, config: config }), header: _jsx(Header, { onToggleSidebar: () => setMobileSidebarOpen(!mobileSidebarOpen), session: session, onNavigate: onNavigate, onLogout: onLogout, pageActions: pageActions }), breadcrumbs: _jsx(Breadcrumbs, { currentPath: currentPath, onNavigate: onNavigate }), children: _jsx("div", { className: "h-full", children: children }) })] }));
}
/**
 * Per-route primary actions that render on the right of the top bar.
 *
 * Centralised here so the top bar's actions row stays consistent
 * across view changes — no flicker on navigation, no per-view header
 * reimplementation. Views that need view-local actions (e.g. a
 * "Save draft" button on the post editor) continue to render them
 * inside their own hero; this slot is for the highest-level
 * primary/secondary pair that mirrors the reference dashboard.
 *
 * Resolution rules:
 *   - Dashboard / Posts / Pages → nothing here; those views render their
 *     hero CTAs inside the page header per the design
 *   - Other list views → just "+ New <Type>"
 *   - Editor views → no top-bar action (editor renders its own toolbar)
 */
function computePageActions({ currentPath, config, onNavigate, }) {
    const collections = collectionList(config);
    // Dashboard, /posts, and /pages render their own hero CTAs in the page
    // header (design `.page-hdr` → `.hdr-r`), so the top bar stays empty there.
    const isDashboard = currentPath === '/' || currentPath === '/dashboard';
    if (isDashboard || currentPath === '/posts' || currentPath === '/pages') {
        return null;
    }
    if (currentPath === '/posts/types') {
        return (_jsxs("button", { type: "button", onClick: () => onNavigate('/posts/types/new'), className: "bg-primary text-primary-foreground hover:bg-primary/90 focus-visible:ring-ring inline-flex items-center gap-1.5 rounded-lg px-3.5 py-1.5 text-sm font-medium shadow-sm transition-colors focus:outline-none focus-visible:ring-2", children: [_jsx(Plus, { className: "h-3.5 w-3.5", "aria-hidden": true }), "New Post Type"] }));
    }
    // List views — surface "+ New X" so authors can keep creating without
    // hunting for a per-list button. Skip on editor views; those have their
    // own toolbar.
    for (const c of collections) {
        if (currentPath === `/${c.slug}`) {
            return (_jsxs("button", { type: "button", onClick: () => onNavigate(`/${c.slug}/new`), className: "bg-primary text-primary-foreground hover:bg-primary/90 focus-visible:ring-ring inline-flex items-center gap-1.5 rounded-lg px-3.5 py-1.5 text-sm font-medium shadow-sm transition-colors focus:outline-none focus-visible:ring-2", children: [_jsx(Plus, { className: "h-3.5 w-3.5", "aria-hidden": true }), "New ", c.labels?.singular ?? c.slug] }));
        }
    }
    return null;
}
function collectionList(config) {
    if (!config?.collections)
        return [];
    const raw = config.collections;
    const list = Array.isArray(raw) ? raw : Object.values(raw);
    return list.filter((c) => !c?.admin?.hidden);
}
//# sourceMappingURL=Layout.js.map