import { a8 as Z, a7 as Q, a9 as tt, aa as et, ap as at, ao as nt, _ as u, l as O, g as it, aD as rt, aU as lt, aV as st, ab as ot, av as ct, aE as ut, aW as $, aX as pt, aY as F } from "./index-Bh-wTBlD.js";
import { m as dt } from "./chunk-4BX2VUAB-CkAjNgA_-Bs9A5Kfv.js";
import { M as gt } from "./mermaid-parser.core-DtLRLmJt-Bs8fzU82.js";
import { h as P } from "./arc-Da5qnJCa-D1fJq9Ga.js";
import { h as ft } from "./ordinal-DfAQgscy-BEJTu10r.js";
function ht(t, a) {
  return a < t ? -1 : a > t ? 1 : a >= t ? 0 : NaN;
}
function mt(t) {
  return t;
}
function xt() {
  var t = mt, a = ht, f = null, o = $(0), p = $(F), h = $(0);
  function r(e) {
    var l, s = (e = pt(e)).length, d, m, g = 0, x = new Array(s), c = new Array(s), i = +o.apply(this, arguments), v = Math.min(F, Math.max(-F, p.apply(this, arguments) - i)), A, S = Math.min(Math.abs(v) / s, h.apply(this, arguments)), T = S * (v < 0 ? -1 : 1), w;
    for (l = 0; l < s; ++l)
      (w = c[x[l] = l] = +t(e[l], l, e)) > 0 && (g += w);
    for (a != null ? x.sort(function(y, D) {
      return a(c[y], c[D]);
    }) : f != null && x.sort(function(y, D) {
      return f(e[y], e[D]);
    }), l = 0, m = g ? (v - s * T) / g : 0; l < s; ++l, i = A)
      d = x[l], w = c[d], A = i + (w > 0 ? w * m : 0) + T, c[d] = {
        data: e[d],
        index: l,
        value: w,
        startAngle: i,
        endAngle: A,
        padAngle: S
      };
    return c;
  }
  return r.value = function(e) {
    return arguments.length ? (t = typeof e == "function" ? e : $(+e), r) : t;
  }, r.sortValues = function(e) {
    return arguments.length ? (a = e, f = null, r) : a;
  }, r.sort = function(e) {
    return arguments.length ? (f = e, a = null, r) : f;
  }, r.startAngle = function(e) {
    return arguments.length ? (o = typeof e == "function" ? e : $(+e), r) : o;
  }, r.endAngle = function(e) {
    return arguments.length ? (p = typeof e == "function" ? e : $(+e), r) : p;
  }, r.padAngle = function(e) {
    return arguments.length ? (h = typeof e == "function" ? e : $(+e), r) : h;
  }, r;
}
var wt = ut.pie, R = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, b = R.sections, E = R.showData, yt = structuredClone(wt), vt = /* @__PURE__ */ u(() => structuredClone(yt), "getConfig"), St = /* @__PURE__ */ u(() => {
  b = /* @__PURE__ */ new Map(), E = R.showData, ct();
}, "clear"), $t = /* @__PURE__ */ u(({ label: t, value: a }) => {
  if (a < 0)
    throw new Error(
      `"${t}" has invalid value: ${a}. Negative values are not allowed in pie charts. All slice values must be >= 0.`
    );
  b.has(t) || (b.set(t, a), O.debug(`added new section: ${t}, with value: ${a}`));
}, "addSection"), At = /* @__PURE__ */ u(() => b, "getSections"), Dt = /* @__PURE__ */ u((t) => {
  E = t;
}, "setShowData"), Tt = /* @__PURE__ */ u(() => E, "getShowData"), I = {
  getConfig: vt,
  clear: St,
  setDiagramTitle: nt,
  getDiagramTitle: at,
  setAccTitle: et,
  getAccTitle: tt,
  setAccDescription: Q,
  getAccDescription: Z,
  addSection: $t,
  getSections: At,
  setShowData: Dt,
  getShowData: Tt
}, bt = /* @__PURE__ */ u((t, a) => {
  dt(t, a), a.setShowData(t.showData), t.sections.map(a.addSection);
}, "populateDb"), Ct = {
  parse: /* @__PURE__ */ u(async (t) => {
    const a = await gt("pie", t);
    O.debug(a), bt(a, I);
  }, "parse")
}, Mt = /* @__PURE__ */ u((t) => `
  .pieCircle{
    stroke: ${t.pieStrokeColor};
    stroke-width : ${t.pieStrokeWidth};
    opacity : ${t.pieOpacity};
  }
  .pieOuterCircle{
    stroke: ${t.pieOuterStrokeColor};
    stroke-width: ${t.pieOuterStrokeWidth};
    fill: none;
  }
  .pieTitleText {
    text-anchor: middle;
    font-size: ${t.pieTitleTextSize};
    fill: ${t.pieTitleTextColor};
    font-family: ${t.fontFamily};
  }
  .slice {
    font-family: ${t.fontFamily};
    fill: ${t.pieSectionTextColor};
    font-size:${t.pieSectionTextSize};
    // fill: white;
  }
  .legend text {
    fill: ${t.pieLegendTextColor};
    font-family: ${t.fontFamily};
    font-size: ${t.pieLegendTextSize};
  }
`, "getStyles"), kt = Mt, zt = /* @__PURE__ */ u((t) => {
  const a = [...t.values()].reduce((o, p) => o + p, 0), f = [...t.entries()].map(([o, p]) => ({ label: o, value: p })).filter((o) => o.value / a * 100 >= 1);
  return xt().value((o) => o.value).sort(null)(f);
}, "createPieArcs"), Ft = /* @__PURE__ */ u((t, a, f, o) => {
  var p;
  O.debug(`rendering pie chart
` + t);
  const h = o.db, r = it(), e = rt(h.getConfig(), r.pie), l = 40, s = 18, d = 4, m = 450, g = m, x = lt(a), c = x.append("g");
  c.attr("transform", "translate(" + g / 2 + "," + m / 2 + ")");
  const { themeVariables: i } = r;
  let [v] = st(i.pieOuterStrokeWidth);
  v ?? (v = 2);
  const A = e.textPosition, S = Math.min(g, m) / 2 - l, T = P().innerRadius(0).outerRadius(S), w = P().innerRadius(S * A).outerRadius(S * A);
  c.append("circle").attr("cx", 0).attr("cy", 0).attr("r", S + v / 2).attr("class", "pieOuterCircle");
  const y = h.getSections(), D = zt(y), U = [
    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 C = 0;
  y.forEach((n) => {
    C += n;
  });
  const W = D.filter((n) => (n.data.value / C * 100).toFixed(0) !== "0"), M = ft(U).domain([
    ...y.keys()
  ]);
  c.selectAll("mySlices").data(W).enter().append("path").attr("d", T).attr("fill", (n) => M(n.data.label)).attr("class", "pieCircle"), c.selectAll("mySlices").data(W).enter().append("text").text((n) => (n.data.value / C * 100).toFixed(0) + "%").attr("transform", (n) => "translate(" + w.centroid(n) + ")").style("text-anchor", "middle").attr("class", "slice");
  const X = c.append("text").text(h.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText"), B = [...y.entries()].map(([n, z]) => ({
    label: n,
    value: z
  })), k = c.selectAll(".legend").data(B).enter().append("g").attr("class", "legend").attr("transform", (n, z) => {
    const L = s + d, J = L * B.length / 2, K = 12 * s, Y = z * L - J;
    return "translate(" + K + "," + Y + ")";
  });
  k.append("rect").attr("width", s).attr("height", s).style("fill", (n) => M(n.label)).style("stroke", (n) => M(n.label)), k.append("text").attr("x", s + d).attr("y", s - d).text((n) => h.getShowData() ? `${n.label} [${n.value}]` : n.label);
  const j = Math.max(
    ...k.selectAll("text").nodes().map((n) => (n == null ? void 0 : n.getBoundingClientRect().width) ?? 0)
  ), q = g + l + s + d + j, N = ((p = X.node()) == null ? void 0 : p.getBoundingClientRect().width) ?? 0, G = g / 2 - N / 2, H = g / 2 + N / 2, V = Math.min(0, G), _ = Math.max(q, H) - V;
  x.attr("viewBox", `${V} 0 ${_} ${m}`), ot(x, m, _, e.useMaxWidth);
}, "draw"), Ot = { draw: Ft }, Vt = {
  parser: Ct,
  db: I,
  renderer: Ot,
  styles: kt
};
export {
  Vt as diagram
};
