// @design-system: layout/DesktopTopBar

/**
 * DesktopTopBar - glass sticky horizontal navigation bar for desktop (lg+).
 *
 * Shown only on screens at lg breakpoint and above via `hidden lg:flex`.
 * Floats over page content with backdrop-blur glass effect.
 * Mobile TopBar and BottomNav remain unchanged; AppShell hides them on lg+.
 *
 * @example
 * <DesktopTopBar
 *   items={customerNavItems}
 *   mode="customer"
 *   logoSlot={<MDLogo className="text-mode-customer-700" />}
 *   endSlot={<LanguageToggle />}
 * />
 */

import { type ReactNode, useSyncExternalStore } from 'react';
import { cn } from '@/lib/cn';
import { useT } from '@/lib/i18n/react';
import {
  getNavSpeedServerSnapshot,
  getNavSpeedSnapshot,
  subscribeNavSpeed,
} from '@/lib/nav/nav-speed';

export interface DesktopTopBarItem {
  href: string;
  /** Icon node - use <Icon> from icons/. */
  icon: ReactNode;
  /** Accessible label (also rendered as visible text). */
  label: string;
  /** Whether this item is the currently active route. */
  active?: boolean;
}

export interface DesktopTopBarProps {
  /** Navigation link items. */
  items: DesktopTopBarItem[];
  /** Visual/interaction mode - drives active indicator color. */
  mode?: 'customer' | 'vendor';
  /**
   * Slot at the logical start (inline-start).
   * Typically the <MDLogo />.
   */
  logoSlot?: ReactNode;
  /**
   * Slot at the logical end (inline-end).
   * Typically <LanguageToggle /> + ⌘K hint + auth chip.
   */
  endSlot?: ReactNode;
  className?: string;
}

export function DesktopTopBar({
  items,
  mode = 'customer',
  logoSlot,
  endSlot,
  className,
}: DesktopTopBarProps) {
  const t = useT('nav');
  const isVendor = mode === 'vendor';
  const navSpeed = useSyncExternalStore(
    subscribeNavSpeed,
    getNavSpeedSnapshot,
    getNavSpeedServerSnapshot,
  );

  return (
    <header
      data-testid="smart-top-bar"
      data-nav-topbar-visible={navSpeed.topBarVisible ? 'true' : 'false'}
    >
      {/* hidden on mobile - only rendered at lg+ */}
      <nav
        aria-label={t('desktop_nav')}
        className={cn(
          'sticky top-0 z-[var(--z-sticky)]',
          'hidden lg:flex',
          'h-[var(--height-desktop-topnav)] w-full',
          'pt-[var(--safe-area-top)]',
          'ps-[var(--safe-area-start)]',
          'pe-[var(--safe-area-end)]',
          'bg-surface-app shadow-sm',
          'border-border-default border-b',
          'transition-transform duration-[var(--duration-base)] ease-[var(--ease-emphasized)]',
          navSpeed.topBarVisible ? 'translate-y-0' : '-translate-y-full',
          className,
        )}
      >
        {/* Inner container — max-w-7xl centered, matches page content gutter */}
        <div className="mx-auto flex h-full w-full max-w-7xl items-center justify-between px-6">
          {/* Logo slot - inline-start */}
          {logoSlot && <div className="me-8 shrink-0">{logoSlot}</div>}

          {/* Navigation items - center */}
          <ul className="flex min-w-0 flex-1 items-center justify-center gap-1">
            {items.map((item) => (
              <li key={item.href}>
                <a
                  href={item.href}
                  aria-current={item.active ? 'page' : undefined}
                  className={cn(
                    'inline-flex shrink-0 items-center gap-2',
                    'whitespace-nowrap',
                    'px-3 py-2',
                    'rounded-md',
                    'text-sm font-medium',
                    'transition-colors duration-[var(--duration-fast)] ease-[var(--ease-out)]',
                    'focus-visible:outline-mode-customer-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2',
                    'relative',
                    item.active
                      ? 'text-mode-customer-700 font-semibold'
                      : 'text-text-primary hover:text-mode-customer-700 hover:bg-mode-customer-50',
                  )}
                >
                  <span className="size-[var(--spacing-4)] shrink-0" aria-hidden="true">
                    {item.icon}
                  </span>
                  <span>{item.label}</span>
                  {/* Active indicator: 2px underline in secondary color */}
                  {item.active && (
                    <span
                      className={cn(
                        'absolute inset-x-0 bottom-0 h-(--size-hairline-indicator) rounded-full',
                        isVendor ? 'bg-mode-vendor-500' : 'bg-mode-customer-500',
                      )}
                      aria-hidden="true"
                    />
                  )}
                </a>
              </li>
            ))}
          </ul>

          {/* End slot - inline-end */}
          {endSlot && <div className="ms-8 flex shrink-0 items-center gap-3">{endSlot}</div>}
        </div>
      </nav>
    </header>
  );
}
