import { a5 as S, Y as R, aR as H, g as J, s as K, d as ee, e as te, A as ae, z as re, _ as u, l as z, f as ne, Q as ie, T as se, ag as le, aS as oe, i as ce, I as de, R as ue } from "./sdk-entry-DgIWYKoN.js";
import { p as ge } from "./chunk-4BX2VUAB-DPxDR8bx.js";
import { p as pe } from "./mermaid-parser.core-DYTuD9C3.js";
import { d as P } from "./arc-XrcqYq4R.js";
function fe(e, a) {
  return a < e ? -1 : a > e ? 1 : a >= e ? 0 : NaN;
}
function he(e) {
  return e;
}
function me() {
  var e = he, a = fe, f = null, $ = S(0), s = S(R), g = S(0);
  function l(t) {
    var n, o = (t = H(t)).length, p, h, v = 0, c = new Array(o), i = new Array(o), x = +$.apply(this, arguments), w = Math.min(R, Math.max(-R, s.apply(this, arguments) - x)), m, C = Math.min(Math.abs(w) / o, g.apply(this, arguments)), D = C * (w < 0 ? -1 : 1), d;
    for (n = 0; n < o; ++n)
      (d = i[c[n] = n] = +e(t[n], n, t)) > 0 && (v += d);
    for (a != null ? c.sort(function(y, A) {
      return a(i[y], i[A]);
    }) : f != null && c.sort(function(y, A) {
      return f(t[y], t[A]);
    }), n = 0, h = v ? (w - o * D) / v : 0; n < o; ++n, x = m)
      p = c[n], d = i[p], m = x + (d > 0 ? d * h : 0) + D, i[p] = {
        data: t[p],
        index: n,
        value: d,
        startAngle: x,
        endAngle: m,
        padAngle: C
      };
    return i;
  }
  return l.value = function(t) {
    return arguments.length ? (e = typeof t == "function" ? t : S(+t), l) : e;
  }, l.sortValues = function(t) {
    return arguments.length ? (a = t, f = null, l) : a;
  }, l.sort = function(t) {
    return arguments.length ? (f = t, a = null, l) : f;
  }, l.startAngle = function(t) {
    return arguments.length ? ($ = typeof t == "function" ? t : S(+t), l) : $;
  }, l.endAngle = function(t) {
    return arguments.length ? (s = typeof t == "function" ? t : S(+t), l) : s;
  }, l.padAngle = function(t) {
    return arguments.length ? (g = typeof t == "function" ? t : S(+t), l) : g;
  }, l;
}
var ve = ue.pie, W = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, T = W.sections, F = W.showData, xe = structuredClone(ve), Se = /* @__PURE__ */ u(() => structuredClone(xe), "getConfig"), $e = /* @__PURE__ */ u(() => {
  T = /* @__PURE__ */ new Map(), F = W.showData, de();
}, "clear"), we = /* @__PURE__ */ u(({ label: e, value: a }) => {
  if (a < 0)
    throw new Error(
      `"${e}" has invalid value: ${a}. Negative values are not allowed in pie charts. All slice values must be >= 0.`
    );
  T.has(e) || (T.set(e, a), z.debug(`added new section: ${e}, with value: ${a}`));
}, "addSection"), ye = /* @__PURE__ */ u(() => T, "getSections"), Ae = /* @__PURE__ */ u((e) => {
  F = e;
}, "setShowData"), Ce = /* @__PURE__ */ u(() => F, "getShowData"), _ = {
  getConfig: Se,
  clear: $e,
  setDiagramTitle: re,
  getDiagramTitle: ae,
  setAccTitle: te,
  getAccTitle: ee,
  setAccDescription: K,
  getAccDescription: J,
  addSection: we,
  getSections: ye,
  setShowData: Ae,
  getShowData: Ce
}, De = /* @__PURE__ */ u((e, a) => {
  ge(e, a), a.setShowData(e.showData), e.sections.map(a.addSection);
}, "populateDb"), Te = {
  parse: /* @__PURE__ */ u(async (e) => {
    const a = await pe("pie", e);
    z.debug(a), De(a, _);
  }, "parse")
}, ke = /* @__PURE__ */ u((e) => `
  .pieCircle{
    stroke: ${e.pieStrokeColor};
    stroke-width : ${e.pieStrokeWidth};
    opacity : ${e.pieOpacity};
  }
  .pieOuterCircle{
    stroke: ${e.pieOuterStrokeColor};
    stroke-width: ${e.pieOuterStrokeWidth};
    fill: none;
  }
  .pieTitleText {
    text-anchor: middle;
    font-size: ${e.pieTitleTextSize};
    fill: ${e.pieTitleTextColor};
    font-family: ${e.fontFamily};
  }
  .slice {
    font-family: ${e.fontFamily};
    fill: ${e.pieSectionTextColor};
    font-size:${e.pieSectionTextSize};
    // fill: white;
  }
  .legend text {
    fill: ${e.pieLegendTextColor};
    font-family: ${e.fontFamily};
    font-size: ${e.pieLegendTextSize};
  }
`, "getStyles"), be = ke, Ee = /* @__PURE__ */ u((e) => {
  const a = [...e.values()].reduce((s, g) => s + g, 0), f = [...e.entries()].map(([s, g]) => ({ label: s, value: g })).filter((s) => s.value / a * 100 >= 1);
  return me().value((s) => s.value).sort(null)(f);
}, "createPieArcs"), Me = /* @__PURE__ */ u((e, a, f, $) => {
  z.debug(`rendering pie chart
` + e);
  const s = $.db, g = ne(), l = ie(s.getConfig(), g.pie), t = 40, n = 18, o = 4, p = 450, h = p, v = se(a), c = v.append("g");
  c.attr("transform", "translate(" + h / 2 + "," + p / 2 + ")");
  const { themeVariables: i } = g;
  let [x] = le(i.pieOuterStrokeWidth);
  x ??= 2;
  const w = l.textPosition, m = Math.min(h, p) / 2 - t, C = P().innerRadius(0).outerRadius(m), D = P().innerRadius(m * w).outerRadius(m * w);
  c.append("circle").attr("cx", 0).attr("cy", 0).attr("r", m + x / 2).attr("class", "pieOuterCircle");
  const d = s.getSections(), y = Ee(d), A = [
    i.pie1,
    i.pie2,
    i.pie3,
    i.pie4,
    i.pie5,
    i.pie6,
    i.pie7,
    i.pie8,
    i.pie9,
    i.pie10,
    i.pie11,
    i.pie12
  ];
  let k = 0;
  d.forEach((r) => {
    k += r;
  });
  const G = y.filter((r) => (r.data.value / k * 100).toFixed(0) !== "0"), b = oe(A).domain([
    ...d.keys()
  ]);
  c.selectAll("mySlices").data(G).enter().append("path").attr("d", C).attr("fill", (r) => b(r.data.label)).attr("class", "pieCircle"), c.selectAll("mySlices").data(G).enter().append("text").text((r) => (r.data.value / k * 100).toFixed(0) + "%").attr("transform", (r) => "translate(" + D.centroid(r) + ")").style("text-anchor", "middle").attr("class", "slice");
  const V = c.append("text").text(s.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText"), L = [...d.entries()].map(([r, M]) => ({
    label: r,
    value: M
  })), E = c.selectAll(".legend").data(L).enter().append("g").attr("class", "legend").attr("transform", (r, M) => {
    const O = n + o, Y = O * L.length / 2, Z = 12 * n, q = M * O - Y;
    return "translate(" + Z + "," + q + ")";
  });
  E.append("rect").attr("width", n).attr("height", n).style("fill", (r) => b(r.label)).style("stroke", (r) => b(r.label)), E.append("text").attr("x", n + o).attr("y", n - o).text((r) => s.getShowData() ? `${r.label} [${r.value}]` : r.label);
  const U = Math.max(
    ...E.selectAll("text").nodes().map((r) => r?.getBoundingClientRect().width ?? 0)
  ), j = h + t + n + o + U, N = V.node()?.getBoundingClientRect().width ?? 0, Q = h / 2 - N / 2, X = h / 2 + N / 2, B = Math.min(0, Q), I = Math.max(j, X) - B;
  v.attr("viewBox", `${B} 0 ${I} ${p}`), ce(v, p, I, l.useMaxWidth);
}, "draw"), Re = { draw: Me }, Ne = {
  parser: Te,
  db: _,
  renderer: Re,
  styles: be
};
export {
  Ne as diagram
};
