import { a7 as S, a8 as D, ap as E, ao as I, a9 as z, aa as F, _ as l, aU as P, av as R, aD as y, aE as _, aB as M, l as B, ba as G, ab as V } from "./index-Bh-wTBlD.js";
import { m as W } from "./chunk-4BX2VUAB-CkAjNgA_-Bs9A5Kfv.js";
import { M as j } from "./mermaid-parser.core-DtLRLmJt-Bs8fzU82.js";
var h = {
  showLegend: !0,
  ticks: 5,
  max: null,
  min: 0,
  graticule: "circle"
}, b = {
  axes: [],
  curves: [],
  options: h
}, x = structuredClone(b), U = _.radar, Z = /* @__PURE__ */ l(() => y({
  ...U,
  ...M().radar
}), "getConfig"), w = /* @__PURE__ */ l(() => x.axes, "getAxes"), q = /* @__PURE__ */ l(() => x.curves, "getCurves"), H = /* @__PURE__ */ l(() => x.options, "getOptions"), J = /* @__PURE__ */ l((a) => {
  x.axes = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name
  }));
}, "setAxes"), K = /* @__PURE__ */ l((a) => {
  x.curves = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name,
    entries: N(t.entries)
  }));
}, "setCurves"), N = /* @__PURE__ */ l((a) => {
  if (a[0].axis == null)
    return a.map((e) => e.value);
  const t = w();
  if (t.length === 0)
    throw new Error("Axes must be populated before curves for reference entries");
  return t.map((e) => {
    const r = a.find((i) => {
      var n;
      return ((n = i.axis) == null ? void 0 : n.$refText) === e.name;
    });
    if (r === void 0)
      throw new Error("Missing entry for axis " + e.label);
    return r.value;
  });
}, "computeCurveEntries"), Q = /* @__PURE__ */ l((a) => {
  var t, e, r, i, n;
  const o = a.reduce(
    (s, c) => (s[c.name] = c, s),
    {}
  );
  x.options = {
    showLegend: ((t = o.showLegend) == null ? void 0 : t.value) ?? h.showLegend,
    ticks: ((e = o.ticks) == null ? void 0 : e.value) ?? h.ticks,
    max: ((r = o.max) == null ? void 0 : r.value) ?? h.max,
    min: ((i = o.min) == null ? void 0 : i.value) ?? h.min,
    graticule: ((n = o.graticule) == null ? void 0 : n.value) ?? h.graticule
  };
}, "setOptions"), X = /* @__PURE__ */ l(() => {
  R(), x = structuredClone(b);
}, "clear"), $ = {
  getAxes: w,
  getCurves: q,
  getOptions: H,
  setAxes: J,
  setCurves: K,
  setOptions: Q,
  getConfig: Z,
  clear: X,
  setAccTitle: F,
  getAccTitle: z,
  setDiagramTitle: I,
  getDiagramTitle: E,
  getAccDescription: D,
  setAccDescription: S
}, Y = /* @__PURE__ */ l((a) => {
  W(a, $);
  const { axes: t, curves: e, options: r } = a;
  $.setAxes(t), $.setCurves(e), $.setOptions(r);
}, "populate"), tt = {
  parse: /* @__PURE__ */ l(async (a) => {
    const t = await j("radar", a);
    B.debug(t), Y(t);
  }, "parse")
}, et = /* @__PURE__ */ l((a, t, e, r) => {
  const i = r.db, n = i.getAxes(), o = i.getCurves(), s = i.getOptions(), c = i.getConfig(), d = i.getDiagramTitle(), u = P(t), g = at(u, c), p = s.max ?? Math.max(...o.map((v) => Math.max(...v.entries))), m = s.min, f = Math.min(c.width, c.height) / 2;
  rt(g, n, f, s.ticks, s.graticule), it(g, n, f, c), C(g, n, o, m, p, s.graticule, c), L(g, o, s.showLegend, c), g.append("text").attr("class", "radarTitle").text(d).attr("x", 0).attr("y", -c.height / 2 - c.marginTop);
}, "draw"), at = /* @__PURE__ */ l((a, t) => {
  const e = t.width + t.marginLeft + t.marginRight, r = t.height + t.marginTop + t.marginBottom, i = {
    x: t.marginLeft + t.width / 2,
    y: t.marginTop + t.height / 2
  };
  return V(a, r, e, t.useMaxWidth ?? !0), a.attr("viewBox", `0 0 ${e} ${r}`), a.append("g").attr("transform", `translate(${i.x}, ${i.y})`);
}, "drawFrame"), rt = /* @__PURE__ */ l((a, t, e, r, i) => {
  if (i === "circle")
    for (let n = 0; n < r; n++) {
      const o = e * (n + 1) / r;
      a.append("circle").attr("r", o).attr("class", "radarGraticule");
    }
  else if (i === "polygon") {
    const n = t.length;
    for (let o = 0; o < r; o++) {
      const s = e * (o + 1) / r, c = t.map((d, u) => {
        const g = 2 * u * Math.PI / n - Math.PI / 2, p = s * Math.cos(g), m = s * Math.sin(g);
        return `${p},${m}`;
      }).join(" ");
      a.append("polygon").attr("points", c).attr("class", "radarGraticule");
    }
  }
}, "drawGraticule"), it = /* @__PURE__ */ l((a, t, e, r) => {
  const i = t.length;
  for (let n = 0; n < i; n++) {
    const o = t[n].label, s = 2 * n * Math.PI / i - Math.PI / 2;
    a.append("line").attr("x1", 0).attr("y1", 0).attr("x2", e * r.axisScaleFactor * Math.cos(s)).attr("y2", e * r.axisScaleFactor * Math.sin(s)).attr("class", "radarAxisLine"), a.append("text").text(o).attr("x", e * r.axisLabelFactor * Math.cos(s)).attr("y", e * r.axisLabelFactor * Math.sin(s)).attr("class", "radarAxisLabel");
  }
}, "drawAxes");
function C(a, t, e, r, i, n, o) {
  const s = t.length, c = Math.min(o.width, o.height) / 2;
  e.forEach((d, u) => {
    if (d.entries.length !== s)
      return;
    const g = d.entries.map((p, m) => {
      const f = 2 * Math.PI * m / s - Math.PI / 2, v = T(p, r, i, c), k = v * Math.cos(f), O = v * Math.sin(f);
      return { x: k, y: O };
    });
    n === "circle" ? a.append("path").attr("d", A(g, o.curveTension)).attr("class", `radarCurve-${u}`) : n === "polygon" && a.append("polygon").attr("points", g.map((p) => `${p.x},${p.y}`).join(" ")).attr("class", `radarCurve-${u}`);
  });
}
l(C, "drawCurves");
function T(a, t, e, r) {
  const i = Math.min(Math.max(a, t), e);
  return r * (i - t) / (e - t);
}
l(T, "relativeRadius");
function A(a, t) {
  const e = a.length;
  let r = `M${a[0].x},${a[0].y}`;
  for (let i = 0; i < e; i++) {
    const n = a[(i - 1 + e) % e], o = a[i], s = a[(i + 1) % e], c = a[(i + 2) % e], d = {
      x: o.x + (s.x - n.x) * t,
      y: o.y + (s.y - n.y) * t
    }, u = {
      x: s.x - (c.x - o.x) * t,
      y: s.y - (c.y - o.y) * t
    };
    r += ` C${d.x},${d.y} ${u.x},${u.y} ${s.x},${s.y}`;
  }
  return `${r} Z`;
}
l(A, "closedRoundCurve");
function L(a, t, e, r) {
  if (!e)
    return;
  const i = (r.width / 2 + r.marginRight) * 3 / 4, n = -(r.height / 2 + r.marginTop) * 3 / 4, o = 20;
  t.forEach((s, c) => {
    const d = a.append("g").attr("transform", `translate(${i}, ${n + c * o})`);
    d.append("rect").attr("width", 12).attr("height", 12).attr("class", `radarLegendBox-${c}`), d.append("text").attr("x", 16).attr("y", 0).attr("class", "radarLegendText").text(s.label);
  });
}
l(L, "drawLegend");
var st = { draw: et }, nt = /* @__PURE__ */ l((a, t) => {
  let e = "";
  for (let r = 0; r < a.THEME_COLOR_LIMIT; r++) {
    const i = a[`cScale${r}`];
    e += `
		.radarCurve-${r} {
			color: ${i};
			fill: ${i};
			fill-opacity: ${t.curveOpacity};
			stroke: ${i};
			stroke-width: ${t.curveStrokeWidth};
		}
		.radarLegendBox-${r} {
			fill: ${i};
			fill-opacity: ${t.curveOpacity};
			stroke: ${i};
		}
		`;
  }
  return e;
}, "genIndexStyles"), ot = /* @__PURE__ */ l((a) => {
  const t = G(), e = M(), r = y(t, e.themeVariables), i = y(r.radar, a);
  return { themeVariables: r, radarOptions: i };
}, "buildRadarStyleOptions"), lt = /* @__PURE__ */ l(({ radar: a } = {}) => {
  const { themeVariables: t, radarOptions: e } = ot(a);
  return `
	.radarTitle {
		font-size: ${t.fontSize};
		color: ${t.titleColor};
		dominant-baseline: hanging;
		text-anchor: middle;
	}
	.radarAxisLine {
		stroke: ${e.axisColor};
		stroke-width: ${e.axisStrokeWidth};
	}
	.radarAxisLabel {
		dominant-baseline: middle;
		text-anchor: middle;
		font-size: ${e.axisLabelFontSize}px;
		color: ${e.axisColor};
	}
	.radarGraticule {
		fill: ${e.graticuleColor};
		fill-opacity: ${e.graticuleOpacity};
		stroke: ${e.graticuleColor};
		stroke-width: ${e.graticuleStrokeWidth};
	}
	.radarLegendText {
		text-anchor: start;
		font-size: ${e.legendFontSize}px;
		dominant-baseline: hanging;
	}
	${nt(t, e)}
	`;
}, "styles"), ut = {
  parser: tt,
  db: $,
  renderer: st,
  styles: lt
};
export {
  ut as diagram
};
