import { G as k, K as O, i as S, f as F, W as I, U as E, p as o, a0 as z, H as G, s as y, o as w, F as P, g as R, ah as D, A as W } from "./index-CNUFa5l3.js";
import { t as B } from "./chunk-4BX2VUAB-C_NnimAy-9zhkU6MG.js";
import { r as H } from "./wardley-L42UT6IY-BjAkE_cs-li1xYed9.js";
var h = {
  showLegend: !0,
  ticks: 5,
  max: null,
  min: 0,
  graticule: "circle"
}, M = {
  axes: [],
  curves: [],
  options: h
}, p = structuredClone(M), V = P.radar, j = /* @__PURE__ */ o(() => y({
  ...V,
  ...w().radar
}), "getConfig"), b = /* @__PURE__ */ o(() => p.axes, "getAxes"), _ = /* @__PURE__ */ o(() => p.curves, "getCurves"), K = /* @__PURE__ */ o(() => p.options, "getOptions"), U = /* @__PURE__ */ o((a) => {
  p.axes = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name
  }));
}, "setAxes"), q = /* @__PURE__ */ o((a) => {
  p.curves = a.map((t) => ({
    name: t.name,
    label: t.label ?? t.name,
    entries: J(t.entries)
  }));
}, "setCurves"), J = /* @__PURE__ */ o((a) => {
  if (a[0].axis == null) return a.map((e) => e.value);
  let t = b();
  if (t.length === 0) throw Error("Axes must be populated before curves for reference entries");
  return t.map((e) => {
    let r = a.find((i) => {
      var n;
      return ((n = i.axis) == null ? void 0 : n.$refText) === e.name;
    });
    if (r === void 0) throw Error("Missing entry for axis " + e.label);
    return r.value;
  });
}, "computeCurveEntries"), m = {
  getAxes: b,
  getCurves: _,
  getOptions: K,
  setAxes: U,
  setCurves: q,
  setOptions: /* @__PURE__ */ o((a) => {
    var e, r, i, n, l;
    let t = a.reduce((s, c) => (s[c.name] = c, s), {});
    p.options = {
      showLegend: ((e = t.showLegend) == null ? void 0 : e.value) ?? h.showLegend,
      ticks: ((r = t.ticks) == null ? void 0 : r.value) ?? h.ticks,
      max: ((i = t.max) == null ? void 0 : i.value) ?? h.max,
      min: ((n = t.min) == null ? void 0 : n.value) ?? h.min,
      graticule: ((l = t.graticule) == null ? void 0 : l.value) ?? h.graticule
    };
  }, "setOptions"),
  getConfig: j,
  clear: /* @__PURE__ */ o(() => {
    G(), p = structuredClone(M);
  }, "clear"),
  setAccTitle: E,
  getAccTitle: I,
  setDiagramTitle: F,
  getDiagramTitle: S,
  getAccDescription: O,
  setAccDescription: k
}, N = /* @__PURE__ */ o((a) => {
  B(a, m);
  let { axes: t, curves: e, options: r } = a;
  m.setAxes(t), m.setCurves(e), m.setOptions(r);
}, "populate"), Q = { parse: /* @__PURE__ */ o(async (a) => {
  let t = await H("radar", a);
  R.debug(t), N(t);
}, "parse") }, Z = /* @__PURE__ */ o((a, t, e, r) => {
  let i = r.db, n = i.getAxes(), l = i.getCurves(), s = i.getOptions(), c = i.getConfig(), g = i.getDiagramTitle(), d = X(z(t), c), u = s.max ?? Math.max(...l.map((f) => Math.max(...f.entries))), x = s.min, $ = Math.min(c.width, c.height) / 2;
  Y(d, n, $, s.ticks, s.graticule), tt(d, n, $, c), C(d, n, l, x, u, s.graticule, c), T(d, l, s.showLegend, c), d.append("text").attr("class", "radarTitle").text(g).attr("x", 0).attr("y", -c.height / 2 - c.marginTop);
}, "draw"), X = /* @__PURE__ */ o((a, t) => {
  let 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 W(a, r, e, t.useMaxWidth ?? !0), a.attr("viewBox", `0 0 ${e} ${r}`), a.append("g").attr("transform", `translate(${i.x}, ${i.y})`);
}, "drawFrame"), Y = /* @__PURE__ */ o((a, t, e, r, i) => {
  if (i === "circle") for (let n = 0; n < r; n++) {
    let l = e * (n + 1) / r;
    a.append("circle").attr("r", l).attr("class", "radarGraticule");
  }
  else if (i === "polygon") {
    let n = t.length;
    for (let l = 0; l < r; l++) {
      let s = e * (l + 1) / r, c = t.map((g, d) => {
        let u = 2 * d * Math.PI / n - Math.PI / 2;
        return `${s * Math.cos(u)},${s * Math.sin(u)}`;
      }).join(" ");
      a.append("polygon").attr("points", c).attr("class", "radarGraticule");
    }
  }
}, "drawGraticule"), tt = /* @__PURE__ */ o((a, t, e, r) => {
  let i = t.length;
  for (let n = 0; n < i; n++) {
    let l = 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(l).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, l) {
  let s = t.length, c = Math.min(l.width, l.height) / 2;
  e.forEach((g, d) => {
    if (g.entries.length !== s) return;
    let u = g.entries.map((x, $) => {
      let f = 2 * Math.PI * $ / s - Math.PI / 2, v = L(x, r, i, c);
      return {
        x: v * Math.cos(f),
        y: v * Math.sin(f)
      };
    });
    n === "circle" ? a.append("path").attr("d", A(u, l.curveTension)).attr("class", `radarCurve-${d}`) : n === "polygon" && a.append("polygon").attr("points", u.map((x) => `${x.x},${x.y}`).join(" ")).attr("class", `radarCurve-${d}`);
  });
}
o(C, "drawCurves");
function L(a, t, e, r) {
  return r * (Math.min(Math.max(a, t), e) - t) / (e - t);
}
o(L, "relativeRadius");
function A(a, t) {
  let e = a.length, r = `M${a[0].x},${a[0].y}`;
  for (let i = 0; i < e; i++) {
    let n = a[(i - 1 + e) % e], l = a[i], s = a[(i + 1) % e], c = a[(i + 2) % e], g = {
      x: l.x + (s.x - n.x) * t,
      y: l.y + (s.y - n.y) * t
    }, d = {
      x: s.x - (c.x - l.x) * t,
      y: s.y - (c.y - l.y) * t
    };
    r += ` C${g.x},${g.y} ${d.x},${d.y} ${s.x},${s.y}`;
  }
  return `${r} Z`;
}
o(A, "closedRoundCurve");
function T(a, t, e, r) {
  if (!e) return;
  let i = (r.width / 2 + r.marginRight) * 3 / 4, n = -(r.height / 2 + r.marginTop) * 3 / 4;
  t.forEach((l, s) => {
    let c = a.append("g").attr("transform", `translate(${i}, ${n + s * 20})`);
    c.append("rect").attr("width", 12).attr("height", 12).attr("class", `radarLegendBox-${s}`), c.append("text").attr("x", 16).attr("y", 0).attr("class", "radarLegendText").text(l.label);
  });
}
o(T, "drawLegend");
var et = { draw: Z }, at = /* @__PURE__ */ o((a, t) => {
  let e = "";
  for (let r = 0; r < a.THEME_COLOR_LIMIT; r++) {
    let 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"), rt = /* @__PURE__ */ o((a) => {
  let t = y(D(), w().themeVariables);
  return {
    themeVariables: t,
    radarOptions: y(t.radar, a)
  };
}, "buildRadarStyleOptions"), lt = {
  parser: Q,
  db: m,
  renderer: et,
  styles: /* @__PURE__ */ o(({ radar: a } = {}) => {
    let { themeVariables: t, radarOptions: e } = rt(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;
	}
	${at(t, e)}
	`;
  }, "styles")
};
export {
  lt as diagram
};
