"use client";
import * as React from 'react';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsxs, jsx } from 'react/jsx-runtime';

// src/lib/chart-palette.ts
var CHART_PALETTE = [
  "hsl(var(--chart-1, var(--primary)))",
  "hsl(var(--chart-2, var(--primary)))",
  "hsl(var(--chart-3, var(--primary)))",
  "hsl(var(--chart-4, var(--primary)))",
  "hsl(var(--chart-5, var(--primary)))",
  "hsl(var(--chart-6, var(--primary)))"
];
function pickChartHue(index) {
  const safe = (index % CHART_PALETTE.length + CHART_PALETTE.length) % CHART_PALETTE.length;
  return CHART_PALETTE[safe];
}
function cn(...inputs) {
  return twMerge(clsx(inputs));
}
function Venn({
  sets,
  size = 360,
  onSetClick,
  className,
  ...rest
}) {
  const circles = React.useMemo(() => layout(sets, size), [sets, size]);
  const desc = `Venn diagram with ${sets.length} set${sets.length === 1 ? "" : "s"}: ${sets.map((s) => s.label).join(", ") || "(empty)"}`;
  const warnedRef = React.useRef(false);
  const nodeEnv = globalThis.process?.env?.NODE_ENV;
  if (nodeEnv !== "production" && !warnedRef.current && (sets.length === 0 || sets.length > 3)) {
    warnedRef.current = true;
    console.warn(
      `[hex-core/Venn] Got ${sets.length} sets \u2014 Venn supports 2 or 3. Use TreeMap or Matrix for higher-arity overlaps.`
    );
  }
  if (sets.length === 0 || sets.length > 3) {
    return /* @__PURE__ */ jsxs(
      "svg",
      {
        ...rest,
        "data-hex-venn": true,
        "data-set-count": sets.length,
        role: "img",
        width: size,
        height: size,
        viewBox: `0 0 ${size} ${size}`,
        className: cn("block", className),
        children: [
          /* @__PURE__ */ jsx("title", { children: "Venn diagram" }),
          /* @__PURE__ */ jsx("desc", { children: desc }),
          /* @__PURE__ */ jsx(
            "text",
            {
              x: size / 2,
              y: size / 2,
              textAnchor: "middle",
              dy: "0.35em",
              fontSize: 12,
              fill: "hsl(var(--muted-foreground))",
              children: sets.length === 0 ? "No sets" : `Venn supports 2\u20133 sets (got ${sets.length})`
            }
          )
        ]
      }
    );
  }
  return /* @__PURE__ */ jsxs(
    "svg",
    {
      ...rest,
      "data-hex-venn": true,
      "data-set-count": sets.length,
      role: "img",
      width: size,
      height: size,
      viewBox: `0 0 ${size} ${size}`,
      className: cn("block", className),
      children: [
        /* @__PURE__ */ jsx("title", { children: "Venn diagram" }),
        /* @__PURE__ */ jsx("desc", { children: desc }),
        /* @__PURE__ */ jsx("g", { "data-hex-venn-sets": true, children: circles.map((c) => {
          const interactive = Boolean(onSetClick);
          const handleActivate = () => onSetClick?.(c.set);
          return /* @__PURE__ */ jsx(
            "g",
            {
              "data-hex-venn-set": true,
              "data-depth": c.depth,
              className: interactive ? "focus-visible:outline-none focus-visible:outline-2 focus-visible:outline-ring" : void 0,
              role: interactive ? "button" : void 0,
              tabIndex: interactive ? 0 : void 0,
              "aria-label": interactive ? c.set.label : void 0,
              style: interactive ? { cursor: "pointer" } : void 0,
              onClick: interactive ? handleActivate : void 0,
              onKeyDown: interactive ? (e) => activateOnKey(e, handleActivate) : void 0,
              children: /* @__PURE__ */ jsx(
                "circle",
                {
                  cx: c.cx,
                  cy: c.cy,
                  r: c.r,
                  fill: pickChartHue(c.depth),
                  fillOpacity: 0.45,
                  stroke: "hsl(var(--background))",
                  strokeWidth: 1.5
                }
              )
            },
            c.set.id
          );
        }) }),
        /* @__PURE__ */ jsx("g", { "data-hex-venn-labels": true, children: circles.map((c) => /* @__PURE__ */ jsx(
          "text",
          {
            "data-hex-venn-label": true,
            x: c.labelX,
            y: c.labelY,
            textAnchor: "middle",
            dy: "0.35em",
            fontSize: 12,
            fontWeight: 600,
            fill: "hsl(var(--foreground))",
            style: { paintOrder: "stroke", pointerEvents: "none" },
            stroke: "hsl(var(--background))",
            strokeWidth: 3,
            strokeLinejoin: "round",
            children: `${c.set.label}${c.set.value != null ? ` (${c.set.value})` : ""}`
          },
          `label-${c.set.id}`
        )) })
      ]
    }
  );
}
function layout(sets, size) {
  if (sets.length === 0 || sets.length > 3) return [];
  const cx = size / 2;
  const cy = size / 2;
  const r = size * 0.22;
  const sep = size * 0.12;
  if (sets.length === 1) {
    return [{ set: sets[0], cx, cy, r, labelX: cx, labelY: cy - r - 8, depth: 0 }];
  }
  if (sets.length === 2) {
    return sets.map((s, i) => {
      const dx = i === 0 ? -sep : sep;
      return {
        set: s,
        cx: cx + dx,
        cy,
        r,
        labelX: cx + dx + (i === 0 ? -r * 0.6 : r * 0.6),
        labelY: cy - r - 8,
        depth: i
      };
    });
  }
  const verticalOffset = sep * 0.866;
  const triangle = [
    { dx: -sep, dy: verticalOffset },
    // bottom-left
    { dx: sep, dy: verticalOffset },
    // bottom-right
    { dx: 0, dy: -verticalOffset }
    // top
  ];
  const LABEL_PUSH = 1.7;
  const TOP_LABEL_NUDGE = -8;
  const BOTTOM_LABEL_NUDGE = 16;
  return sets.map((s, i) => {
    const { dx, dy } = triangle[i] ?? triangle[0];
    const isTop = i === 2;
    return {
      set: s,
      cx: cx + dx,
      cy: cy + dy,
      r,
      labelX: cx + dx * LABEL_PUSH,
      labelY: cy + dy * LABEL_PUSH + (isTop ? TOP_LABEL_NUDGE : BOTTOM_LABEL_NUDGE),
      depth: i
    };
  });
}
function activateOnKey(e, fn) {
  if (e.key === "Enter" || e.key === " ") {
    e.preventDefault();
    fn();
  }
}

export { Venn };
//# sourceMappingURL=venn.js.map
//# sourceMappingURL=venn.js.map