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

/** The design's icon paths (sales-analytics.js `I`), drawn as `ico` / `fill` / `lpi` draw them. */
const PATHS: Record<string, string> = {
  call: 'M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3.1 19.5 19.5 0 0 1-6-6A19.8 19.8 0 0 1 2.1 4.2 2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1.9.4 1.8.7 2.7a2 2 0 0 1-.5 2.1L8 9.8a16 16 0 0 0 6 6l1.3-1.3a2 2 0 0 1 2.1-.4c.9.3 1.8.6 2.7.7a2 2 0 0 1 1.7 2z',
  chat: 'M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.6A8 8 0 1 1 21 12z',
  trophy: 'M8 21h8M12 17v4M7 4h10v5a5 5 0 0 1-10 0zM7 6H4a3 3 0 0 0 3 4M17 6h3a3 3 0 0 1-3 4',
  chart: 'M4 20V10M10 20V4M16 20v-7M22 20H2',
  funnel: 'M3 5h12M5 10h9M7 15h6M17 5v14M14 16l3 3 3-3',
  clock: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM12 7v5l3 2',
  headset:
    'M4 15v-3a8 8 0 0 1 16 0v3M4 15a2 2 0 0 0 2 2h1v-5H6a2 2 0 0 0-2 2zM20 15a2 2 0 0 1-2 2h-1v-5h1a2 2 0 0 1 2 2zM17 17c0 2-2 3-5 3',
  doc: 'M6 3h9l3 3v15H6zM9 9h6M9 13h6M9 17h4',
  task: 'M4 4h16v16H4zM8 12l3 3 5-6',
  team: 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM2 21a7 7 0 0 1 14 0M17 11a3 3 0 1 0 0-6M22 21a6 6 0 0 0-4-5.6',
  calendar: 'M3 5h18v16H3zM3 10h18M8 3v4M16 3v4',
  money: 'M12 2v20M17 6H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6',
  handshake: 'M2 12l4-4 4 2 4-3 4 3 4 2-4 5-3-2-3 3-4-3z',
  loss: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM15 9l-6 6M9 9l6 6',
  gauge: 'M4 18a8 8 0 1 1 16 0M12 18l4-6',
  forecast: 'M4 4h16v16H4zM4 9h16M9 9v11M14 13h3M14 16h3',
  search: 'M11 18a7 7 0 1 0 0-14 7 7 0 0 0 0 14zM20 20l-4-4',
  caret: 'M7 10l5 5 5-5',
  right: 'M9 6l6 6-6 6',
  down: 'M6 9l6 6 6-6',
  back: 'M15 18l-6-6 6-6',
  refresh: 'M20 11a8 8 0 0 0-14.9-3M4 4v4h4M4 13a8 8 0 0 0 14.9 3M20 20v-4h-4',
  flame: 'M12 21c-4 0-6-3-6-6 0-4 4-6 4-10 3 2 4 5 4 7 1-1 2-2 2-4 2 2 2 5 2 7 0 3-2 6-6 6z',
  table: 'M3 5h18v14H3zM3 10h18M3 15h18M9 5v14',
  ext: 'M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6M15 3h6v6M10 14L21 3',
  up: 'M12 6l6 8H6z',
  dn: 'M12 18l-6-8h12z',
  x: 'M18 6L6 18M6 6l12 12',
  sort: 'M8 4v16M4.5 7.5 8 4l3.5 3.5M16 20V4M12.5 16.5 16 20l3.5-3.5',
  info: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM12 11v5M12 8h.01',
  plus: 'M12 5v14M5 12h14',
  pencil: 'M4 20h4L19 9a2.1 2.1 0 0 0-4-4L4 16zM13.5 6.5l4 4',
  trash: 'M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3',
  filter: 'M4 6h16M7 12h10M10 18h4',
  chevu: 'M6 15l6-6 6 6',
  check: 'M5 12.5l4.5 4.5L19 7',
  arrowl: 'M19 12H5M11 6l-6 6 6 6',
  arrowr: 'M5 12h14M13 6l6 6-6 6',
  gear: 'M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z',
};

/** `ico`: a stroked icon (`sa-i`). */
export function SaIcon({ name, className }: { name: string; className?: string }) {
  return (
    <svg
      className={cn('sa-i', className)}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={2}
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <path d={PATHS[name] ?? 'M0 0'} />
    </svg>
  );
}

/** `fill`: a filled icon (the up / down delta arrows). */
export function SaFill({ name, className }: { name: string; className?: string }) {
  return (
    <svg className={cn('sa-i', className)} viewBox="0 0 24 24" aria-hidden="true">
      <path d={PATHS[name] ?? 'M0 0'} fill="currentColor" />
    </svg>
  );
}

/** `lpi`: the list-page icon (`lp-i`), used in the shared lp-* popovers and dialogs. */
export function LpI({ name, className }: { name: string; className?: string }) {
  return (
    <svg
      className={cn('lp-i', className)}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={1.8}
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <path d={PATHS[name === 'chevd' ? 'down' : name] ?? ''} />
    </svg>
  );
}

/** The caret in the design's buttons (`sa-caret`). */
export function SaCaret() {
  return (
    <svg className="sa-caret" viewBox="0 0 24 24" aria-hidden="true">
      <path d="m7 10 5 5 5-5z" fill="currentColor" />
    </svg>
  );
}

/** The design's "no data" picture (EMPTY_ART). */
export function EmptyArt() {
  return (
    <svg className="sa-empty__art" viewBox="0 0 120 90" aria-hidden="true">
      <path d="M20 70 60 52l40 18-40 18z" fill="currentColor" opacity=".12" />
      <path d="M48 60 42 74M72 60l6 14" stroke="currentColor" strokeDasharray="3 3" opacity=".5" />
      <circle
        cx="62"
        cy="34"
        r="15"
        fill="none"
        stroke="currentColor"
        strokeWidth="3"
        opacity=".55"
      />
      <path
        d="m73 45 12 12"
        stroke="currentColor"
        strokeWidth="5"
        strokeLinecap="round"
        opacity=".55"
      />
    </svg>
  );
}

export const NO_DATA = 'There is no data to show in this time frame. Try changing the date range.';

export type ChartType = 'hbar' | 'vbar' | 'line' | 'area' | 'donut' | 'pie' | 'kpi' | 'table';

/** The chart types the design offers (CTYPES), without Gauge: it shows one series' share. */
export const CHART_TYPES: { key: ChartType; label: string }[] = [
  { key: 'hbar', label: 'Horizontal bar' },
  { key: 'vbar', label: 'Vertical bar' },
  { key: 'line', label: 'Line' },
  { key: 'area', label: 'Area' },
  { key: 'donut', label: 'Donut' },
  { key: 'pie', label: 'Pie' },
  { key: 'kpi', label: 'KPI' },
  { key: 'table', label: 'Table' },
];

/** The coloured chart-type pictures (design `chartTypeIcon`). */
export function ChartTypeIcon({ type }: { type: ChartType }) {
  const b = 'var(--brand)';
  const te = 'var(--teal, #0e7c86)';
  const ind = 'var(--indigo, #444ce7)';
  const ac = 'var(--accent, #f28f23)';
  let body: React.ReactNode;
  switch (type) {
    case 'hbar':
      body = (
        <>
          <rect fill={b} x="0" y="0" width="80" height="14" rx="2" />
          <rect fill={te} x="0" y="20" width="62" height="14" rx="2" />
          <rect fill={ind} x="0" y="40" width="49" height="14" rx="2" />
          <rect fill={ac} x="0" y="60" width="30" height="14" rx="2" />
        </>
      );
      break;
    case 'vbar':
      body = (
        <>
          <rect fill={b} x="0" y="0" height="80" width="14" rx="2" />
          <rect fill={te} x="20" y="18" height="62" width="14" rx="2" />
          <rect fill={ind} x="40" y="31" height="49" width="14" rx="2" />
          <rect fill={ac} x="60" y="50" height="30" width="14" rx="2" />
        </>
      );
      break;
    case 'line':
      body = (
        <>
          <polyline
            fill="none"
            stroke={b}
            strokeWidth="6"
            strokeLinecap="round"
            strokeLinejoin="round"
            points="4,73 52,8 76,45"
          />
          <polyline
            fill="none"
            stroke={te}
            strokeWidth="6"
            strokeLinecap="round"
            strokeLinejoin="round"
            points="4,39 26,9 50,41 76,6"
          />
        </>
      );
      break;
    case 'area':
      body = (
        <>
          <polygon fill={b} points="0,71.6 28.8,24.9 80,0 80,25.8 29.2,41" opacity=".9" />
          <polygon
            fill={te}
            points="0,76.2 30.2,44.7 80,29.4 80,48 59,49.6 31,56.2"
            opacity=".85"
          />
          <polygon fill={ind} points="0,80 32,59.8 59.2,53.6 80,51.4 80,80" opacity=".85" />
        </>
      );
      break;
    case 'donut':
      body = (
        <>
          <circle
            cx="40"
            cy="40"
            r="28"
            fill="none"
            stroke={b}
            strokeWidth="15"
            strokeDasharray="35 141"
            transform="rotate(-90 40 40)"
          />
          <circle
            cx="40"
            cy="40"
            r="28"
            fill="none"
            stroke={te}
            strokeWidth="15"
            strokeDasharray="48 128"
            strokeDashoffset="-37"
            transform="rotate(-90 40 40)"
          />
          <circle
            cx="40"
            cy="40"
            r="28"
            fill="none"
            stroke={ind}
            strokeWidth="15"
            strokeDasharray="42 134"
            strokeDashoffset="-87"
            transform="rotate(-90 40 40)"
          />
          <circle
            cx="40"
            cy="40"
            r="28"
            fill="none"
            stroke={ac}
            strokeWidth="15"
            strokeDasharray="48 128"
            strokeDashoffset="-132"
            transform="rotate(-90 40 40)"
          />
        </>
      );
      break;
    case 'pie':
      body = (
        <>
          <path
            fill={b}
            d="M40 40L27.6 72.3C9.8 65.4 .9 45.4 7.7 27.6 12.9 14.1 25.5 5.4 40 5.4V40Z"
          />
          <path fill={te} d="M40 40l34 6c-3.3 18.8-21.2 31.4-40 28-2.3-.4-4.2-.9-6.4-1.8L40 40Z" />
          <path fill={ind} d="M40 40V5.4c19.1 0 34.6 15.5 34.6 34.6 0 2.3-.1 3.7-.5 6L40 40Z" />
        </>
      );
      break;
    case 'kpi':
      body = (
        <>
          <circle fill="none" stroke={b} strokeWidth="6" cx="39" cy="39" r="36" />
          <path
            fill={b}
            d="M37.4 56.7V34.9H29v-5.1c1.2 0 2.3-.1 3.4-.3 1.1-.2 2.1-.6 3-1.1s1.6-1.2 2.2-2.1c.6-.9 1-1.9 1.2-3.2h5.4v33.6h-6.8Z"
          />
        </>
      );
      break;
    default:
      body = (
        <>
          {[15, 31, 47, 63].map((y) => (
            <rect key={`l${y}`} fill={b} x="0" y={y} width="13" height="13" rx="1.5" />
          ))}
          {[15, 31, 47, 63].map((x) => (
            <rect key={`t${x}`} fill={te} x={x} y="0" width="13" height="13" rx="1.5" />
          ))}
          <path fill={ind} d="M15 15h61v61H15z" />
          <path
            fill="white"
            d="M28 15h3v61h-3zM44 15h3v61h-3zM60 15h3v61h-3zM15 28h61v3H15zM15 44h61v3H15zM15 60h61v3H15z"
          />
        </>
      );
  }
  return (
    <svg
      className="sa-chart-type-icon"
      width="24"
      height="24"
      viewBox="0 0 80 80"
      aria-hidden="true"
      focusable="false"
    >
      {body}
    </svg>
  );
}
