/**
 * B12 Manage views: the parts the three pages share, drawn as the 6 Nov design draws them
 * (view-all.php, default-views.php, recently-deleted.php, view-all.js `ensureDrawer`).
 */
import Link from 'next/link';
import type { ReactNode } from 'react';
import { backHref, manageHref, type ManageParams, type ManageTab } from '../params';

export const Caret = () => (
  <svg
    className="icon iconSize-16 account-btn__caret"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth="1.8"
    strokeLinecap="round"
    strokeLinejoin="round"
    aria-hidden="true"
    focusable="false"
  >
    <path d="m7 10 5 5 5-5" />
  </svg>
);

export const SearchIcon = () => (
  <svg
    className="icon iconSize-18"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth="1.8"
    strokeLinecap="round"
    strokeLinejoin="round"
    aria-hidden="true"
    focusable="false"
  >
    <circle cx="11" cy="11" r="7" />
    <path d="m20 20-3.5-3.5" />
  </svg>
);

/** The dialogs' search icon (view-all.js:94). */
export const SmallSearchIcon = () => (
  <svg viewBox="0 0 24 24" aria-hidden="true">
    <circle cx="11" cy="11" r="7" />
    <path d="m16 16 5 5" />
  </svg>
);

export const SmallCaret = () => (
  <svg viewBox="0 0 16 16" aria-hidden="true">
    <path d="m4 6 4 4 4-4" />
  </svg>
);

/** Each page's own tab labels and title, as drawn (view-all.php:14-20 and the other two). */
const TAB_LABELS: Record<ManageTab, Record<ManageTab, string>> = {
  all: { all: 'All views', default: 'Default Views', deleted: 'Recently Deleted' },
  default: { all: 'All views', default: 'Default views', deleted: 'Recently Deleted' },
  deleted: { all: 'All views', default: 'Default Views', deleted: 'Recently deleted' },
};
const ORDER: ManageTab[] = ['all', 'default', 'deleted'];

/** The top bar and tabs of All views and Recently deleted (`mva-`). */
export function MvaHeader({ tab, params }: { tab: 'all' | 'deleted'; params: ManageParams }) {
  return (
    <>
      <header className="mva-topbar">
        <Link href={backHref(params)} className="mva-back" aria-label="Back">
          {tab === 'all' ? (
            <>
              ‹ <span>Back</span>
            </>
          ) : (
            <>
              <span aria-hidden="true">‹</span>
              <span>Back</span>
            </>
          )}
        </Link>
        <h1>All Views</h1>
        <span className="mva-topbar-spacer" aria-hidden="true" />
      </header>
      <nav
        className={tab === 'deleted' ? 'mva-tabs rdv-tabs' : 'mva-tabs'}
        aria-label="Manage views tabs"
      >
        {ORDER.map((t) => (
          <Link
            key={t}
            href={manageHref(t, params)}
            className={t === tab ? 'mva-tab is-active' : 'mva-tab'}
            aria-current={t === tab ? 'page' : undefined}
          >
            {TAB_LABELS[tab][t]}
          </Link>
        ))}
      </nav>
    </>
  );
}

/** Default views' top bar (with Save) and tabs (`dv-`, default-views.php:12-21). */
export function DvHeader({ params, save }: { params: ManageParams; save: ReactNode }) {
  return (
    <>
      <header className="dv-topbar">
        <Link href={backHref(params)} className="dv-back">
          <svg viewBox="0 0 24 24" aria-hidden="true">
            <path d="m15 18-6-6 6-6" />
          </svg>
          <span>Back</span>
        </Link>
        <h1>Manage Views</h1>
        {save}
      </header>
      <nav className="dv-tabs" aria-label="Manage views tabs">
        {ORDER.map((t) => (
          <Link
            key={t}
            href={manageHref(t, params)}
            className={t === 'default' ? 'dv-tab is-active' : 'dv-tab'}
            aria-current={t === 'default' ? 'page' : undefined}
          >
            {TAB_LABELS.default[t]}
          </Link>
        ))}
      </nav>
    </>
  );
}

/** The design's dialog (view-all.js:16, :27): backdrop, header with ×, body, footer. */
export function MvaDialog({
  title,
  variant,
  onClose,
  footer,
  children,
}: {
  title: string;
  variant?: 'delete' | 'clone' | 'export';
  onClose: () => void;
  footer: ReactNode;
  children: ReactNode;
}) {
  return (
    <>
      <div className="mva-dialog-backdrop" onClick={onClose} />
      <section
        className={variant ? `mva-dialog mva-dialog--${variant}` : 'mva-dialog'}
        role="dialog"
        aria-modal="true"
        aria-labelledby="mvaDialogTitle"
      >
        <header>
          <h2 id="mvaDialogTitle">{title}</h2>
          <button type="button" aria-label="Close" onClick={onClose}>
            ×
          </button>
        </header>
        <div className="mva-dialog-body">{children}</div>
        <footer>{footer}</footer>
      </section>
    </>
  );
}

export const Avatar = ({ text }: { text: string }) => <span className="mva-avatar">{text}</span>;
