import Link from "next/link";
import type { ReactNode } from "react";

/*
 * One signed-in Hub page in the dashboard design: breadcrumb, page heading and content.
 * `data-db-root` carries what public/js/dashboard.js reads (data-page and the API endpoints), and the
 * #mz-account JSON holds the user's sites and merchants for it.
 */
export function DbPage({ page, title, crumb = title, sub, actions, data, endpoints = {}, children }: {
  page: string;
  title: string;
  crumb?: string;
  sub?: string;
  actions?: ReactNode;
  data?: object;
  endpoints?: { stats?: string; sites?: string; check?: string };
  children: ReactNode;
}) {
  return (
    <main id="db-main" className="db-main" data-db-root="" data-page={page} data-stats-endpoint={endpoints.stats} data-sites-endpoint={endpoints.sites} data-check-endpoint={endpoints.check}>
      <div className="db-wrap">
        <nav className="db-crumbs" aria-label="Breadcrumb">
          <ol>
            <li><Link href="/hub">Home</Link></li>
            <li><span aria-current="page">{crumb}</span></li>
          </ol>
        </nav>

        <div className="db-pagehead">
          {sub ? (
            <div>
              <h1>{title}</h1>
              <p className="db-pagehead__sub">{sub}</p>
            </div>
          ) : <h1>{title}</h1>}
          {actions}
        </div>

        {children}
      </div>
      {/* "<" is escaped so the JSON can't close the script element */}
      <script type="application/json" id="mz-account" dangerouslySetInnerHTML={{ __html: JSON.stringify(data ?? {}).replace(/</g, "\\u003c") }} />
    </main>
  );
}

export const CopyIcon = () => (
  <svg className="db-icon" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2" /><path d="M5 15V5a2 2 0 0 1 2-2h8" /></svg>
);
export const CloseIcon = () => (
  <svg className="db-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18" /></svg>
);
export const Caret = () => (
  <svg className="db-icon db-caret" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6" /></svg>
);
