"use client";

import type { ButtonHTMLAttributes, ReactNode } from "react";
import { FOCUS_RING } from "./focus-ring";
import { TAP_TARGET } from "./tap-target";

// The controls in a page or drawer head come in exactly three types:
//
// 1. Primary   — `<Button variant="primary" size="header">`: the head's one
//                main action (filled). At most one per head.
// 2. Secondary — `<Button variant="secondary" size="header">`: every other
//                labelled action (a pill in the header chrome).
// 3. Icon      — `<HeaderIconButton>` / `HEADER_ICON_BUTTON`: a 40 px circle in
//                the same chrome — back, sidebar, "…", filter, refresh, the
//                phone form of a labelled action.
//
// A primary action without a label (a sheet head's ✓ Confirm) is the filled
// 40 px circle `HEADER_PRIMARY_ICON_BUTTON`.
//
// All of them are 40 px tall, so a head's controls line up. See
// docs/components.md → "Page head buttons".

/**
 * The header chrome: surface fill, shadow and a hairline ring, darkening on
 * hover, with the keyboard focus ring. Shared by the secondary pill and the
 * icon circle.
 */
export const HEADER_CHROME =
  `bg-surface-base text-foreground-default shadow ring-1 ring-surface-stroke-muted transition-colors hover:bg-surface-base-muted active:bg-surface-base-muted disabled:opacity-50 disabled:hover:bg-surface-base ${FOCUS_RING}`;

/**
 * Class string of a header icon button (type 3), for a control that renders
 * its own element — e.g. an `ActionMenu`'s `triggerClassName`.
 */
export const HEADER_ICON_BUTTON = `${TAP_TARGET} flex size-10 shrink-0 items-center justify-center rounded-full ${HEADER_CHROME}`;

/**
 * Class string of a head's primary action in icon form — a filled 40 px
 * circle, e.g. the ✓ Confirm in a sheet head. Same fill as
 * `<Button variant="primary">`; at most one per head.
 */
export const HEADER_PRIMARY_ICON_BUTTON = `${TAP_TARGET} flex size-10 shrink-0 items-center justify-center rounded-full bg-foreground-red-orange text-white shadow transition-opacity hover:opacity-90 active:opacity-75 disabled:opacity-50 ${FOCUS_RING}`;

/**
 * Class string of a header secondary pill (type 2), for a control that renders
 * its own element — e.g. an `ActionMenu` trigger with a label. Matches
 * `<Button variant="secondary" size="header">`.
 */
export const HEADER_PILL = `clickable-small inline-flex h-10 shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-full px-4 ${HEADER_CHROME}`;

/**
 * A 40 px round icon-only button in the header chrome (type 3 above).
 *
 * @param label - Accessible name (already translated); also the tooltip.
 * @param icon - The glyph, sized `size-5` by the caller.
 */
export function HeaderIconButton({
  label,
  icon,
  className,
  type = "button",
  ...rest
}: Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type" | "children"> & {
  label: string;
  icon: ReactNode;
  type?: "button" | "submit";
}) {
  return (
    <button
      type={type}
      aria-label={label}
      title={label}
      className={`${HEADER_ICON_BUTTON} ${className ?? ""}`}
      {...rest}
    >
      {icon}
    </button>
  );
}
