import { m as S } from "./chunk-ANTBXLJU-CgMY7K4E-BDFVHt4k.js";
import { a5 as D, a6 as I, ao as E, an as F, a7 as z, a8 as P, _ as l, aU as R, at as _, aA as y, aB as B, aC as b, l as G, bb as V } from "./index-DeBlJN4f.js";
import { N as j } from "./mermaid-parser.core-DRkTy7co-D3k_lOFL.js";
var x = {
  showLegend: !0,
  ticks: 5,
  max: null,
  min: 0,
  graticule: "circle"
}, w = {
  axes: [],
  curves: [],
  options: x
}, h = structuredClone(w), N = B.radar, W = /* @__PURE__ */ l(() => y({
  ...N,
  ...b().radar
}), "getConfig"), C = /* @__PURE__ */ l(() => h.axes, "getAxes"), U = /* @__PURE__ */ l(() => h.curves, "getCurves"), Z = /* @__PURE__ */ l(() => h.options, "getOptions"), q = /* @__PURE__ */ l((a) => {
  h.axes = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name
  }));
}, "setAxes"), H = /* @__PURE__ */ l((a) => {
  h.curves = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name,
    entries: J(t.entries)
  }));
}, "setCurves"), J = /* @__PURE__ */ l((a) => {
  if (a[0].axis == null)
    return a.map((e) => e.value);
  const t = C();
  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"), K = /* @__PURE__ */ l((a) => {
  var t, e, r, i, n;
  const o = a.reduce(
    (s, c) => (s[c.name] = c, s),
    {}
  );
  h.options = {
    showLegend: ((t = o.showLegend) == null ? void 0 : t.value) ?? x.showLegend,
    ticks: ((e = o.ticks) == null ? void 0 : e.value) ?? x.ticks,
    max: ((r = o.max) == null ? void 0 : r.value) ?? x.max,
    min: ((i = o.min) == null ? void 0 : i.value) ?? x.min,
    graticule: ((n = o.graticule) == null ? void 0 : n.value) ?? x.graticule
  };
}, "setOptions"), Q = /* @__PURE__ */ l(() => {
  _(), h = structuredClone(w);
}, "clear"), $ = {
  getAxes: C,
  getCurves: U,
  getOptions: Z,
  setAxes: q,
  setCurves: H,
  setOptions: K,
  getConfig: W,
  clear: Q,
  setAccTitle: P,
  getAccTitle: z,
  setDiagramTitle: F,
  getDiagramTitle: E,
  getAccDescription: I,
  setAccDescription: D
}, X = /* @__PURE__ */ l((a) => {
  S(a, $);
  const { axes: t, curves: e, options: r } = a;
  $.setAxes(t), $.setCurves(e), $.setOptions(r);
}, "populate"), Y = {
  parse: /* @__PURE__ */ l(async (a) => {
    const t = await j("radar", a);
    G.debug(t), X(t);
  }, "parse")
}, tt = /* @__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 = R(t), g = et(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;
  at(g, n, f, s.ticks, s.graticule), rt(g, n, f, c), M(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"), et = /* @__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 a.attr("viewbox", `0 0 ${e} ${r}`).attr("width", e).attr("height", r), a.append("g").attr("transform", `translate(${i.x}, ${i.y})`);
}, "drawFrame"), at = /* @__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"), rt = /* @__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 M(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 = A(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", T(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(M, "drawCurves");
function A(a, t, e, r) {
  const i = Math.min(Math.max(a, t), e);
  return r * (i - t) / (e - t);
}
l(A, "relativeRadius");
function T(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(T, "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 it = { draw: tt }, st = /* @__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"), nt = /* @__PURE__ */ l((a) => {
  const t = V(), e = b(), r = y(t, e.themeVariables), i = y(r.radar, a);
  return { themeVariables: r, radarOptions: i };
}, "buildRadarStyleOptions"), ot = /* @__PURE__ */ l(({ radar: a } = {}) => {
  const { themeVariables: t, radarOptions: e } = nt(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;
	}
	${st(t, e)}
	`;
}, "styles"), gt = {
  parser: Y,
  db: $,
  renderer: it,
  styles: ot
};
export {
  gt as diagram
};
