// Download-the-menu button.
//
// The PDF is supplied by the client and dropped at the path in
// CORNER_MENU_PDF_URL. Until that file exists the button renders nothing: a
// download control that 404s is worse than no control, and this way the button
// appears by itself the moment the PDF lands, with no code change. The probe is
// a HEAD request, so it costs no bandwidth and the browser caches the result.
//
// Note that fetch() cannot reach a file:// sibling, so opening the site straight
// off disk always reports "no PDF". Serve the folder over HTTP to see the
// button (`npx serve .` from the repo root, then browse to the ui_kits path).
function MenuPdfButton({ t, block = false, style }) {
  const { Button } = window.TheCornerDesignSystem_28aa27;
  const url = window.CORNER_MENU_PDF_URL;
  const [ready, setReady] = React.useState(false);

  React.useEffect(() => {
    let live = true;
    fetch(url, { method: 'HEAD' })
      .then((r) => { if (live && r.ok) setReady(true); })
      .catch(() => { /* absent, offline, or file:// — leave the button hidden */ });
    return () => { live = false; };
  }, [url]);

  if (!ready) return null;

  return (
    <a
      href={url}
      download
      style={{ display: block ? 'block' : 'inline-block', textDecoration: 'none', ...style }}
    >
      <Button variant="secondary" size="md" icon={<DownloadIcon />} style={block ? { width: '100%' } : undefined}>
        {t.menu.pdf}
      </Button>
    </a>
  );
}
window.MenuPdfButton = MenuPdfButton;

// Same 2px stroke weight as the rest of the system's icons (see readme ICONOGRAPHY).
function DownloadIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
         strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M12 3v12" />
      <path d="m7 11 5 5 5-5" />
      <path d="M4 20h16" />
    </svg>
  );
}
window.DownloadIcon = DownloadIcon;
