import type { ReactNode } from 'react';

import { cn } from '@/lib/utils';

/**
 * The one route heading.
 *
 * Thirteen pages hand-rolled this block and drifted: the `<h1>` was 20px on
 * most, 30px on Growth and Learn, five pages carried two `<h1>`s, and the gap
 * to the first section below ran 16 / 20 / 24 / 40px depending on the file. A
 * document has one title, and a product has one title size.
 *
 * `ask` is a separate slot from `actions` deliberately. "Ask Piggy" is not a
 * page action — it is the same agent on every page, and it sits in the same
 * place on every page so a reader stops hunting for it among the buttons that
 * do differ.
 */
export function PageHeader({
  title,
  description,
  actions,
  ask,
  className,
}: {
  title: ReactNode;
  description?: ReactNode;
  /** Page-specific controls, right-aligned at `sm` and up. */
  actions?: ReactNode;
  /** The `PiggyAskButton` for this page, kept in one consistent slot. */
  ask?: ReactNode;
  className?: string;
}) {
  return (
    <header
      className={cn(
        'flex min-w-0 flex-col gap-3 sm:flex-row sm:items-start sm:justify-between',
        className,
      )}
    >
      <div className="min-w-0">
        <h1 className="min-w-0 break-words text-xl font-semibold tracking-tight sm:text-2xl">
          {title}
        </h1>
        {description ? (
          <p className="mt-1 min-w-0 max-w-2xl text-sm text-muted">{description}</p>
        ) : null}
      </div>
      {actions || ask ? (
        // `flex-wrap` rather than `whitespace-nowrap`: at 393px a two-button
        // row plus the ask button overflows, and a page header is the last
        // place that should be the thing which introduces horizontal scroll.
        <div className="flex min-w-0 shrink-0 flex-wrap items-center gap-2 sm:justify-end">
          {actions}
          {ask}
        </div>
      ) : null}
    </header>
  );
}
