import { Y as S, P as F, av as K, E as Y, o as Z, p as q, s as H, g as J, c as Q, b as X, _ as g, l as z, v as ee, d as te, F as ae, K as re, a6 as ne, k as ie } from "./mermaid.core-DPTeIFw-.js";
import { p as se } from "./chunk-4BX2VUAB-pnX9CsN3.js";
import { p as le } from "./treemap-KMMF4GRG-D_L55G0b.js";
import { d as I } from "./arc-DD7EWc2o.js";
import { o as oe } from "./ordinal-MUacOSdK.js";
import "./index-RZrnTzzg.js";
import "react";
import "react-dom";
import "./init-DjUOC4st.js";
function ce(e, a) {
  return a < e ? -1 : a > e ? 1 : a >= e ? 0 : NaN;
}
function ue(e) {
  return e;
}
function pe() {
  var e = ue, a = ce, f = null, x = S(0), s = S(F), o = S(0);
  function l(t) {
    var n, c = (t = K(t)).length, u, y, h = 0, p = new Array(c), i = new Array(c), v = +x.apply(this, arguments), w = Math.min(F, Math.max(-F, s.apply(this, arguments) - v)), m, C = Math.min(Math.abs(w) / c, o.apply(this, arguments)), $ = C * (w < 0 ? -1 : 1), d;
    for (n = 0; n < c; ++n)
      (d = i[p[n] = n] = +e(t[n], n, t)) > 0 && (h += d);
    for (a != null ? p.sort(function(A, D) {
      return a(i[A], i[D]);
    }) : f != null && p.sort(function(A, D) {
      return f(t[A], t[D]);
    }), n = 0, y = h ? (w - c * $) / h : 0; n < c; ++n, v = m)
      u = p[n], d = i[u], m = v + (d > 0 ? d * y : 0) + $, i[u] = {
        data: t[u],
        index: n,
        value: d,
        startAngle: v,
        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 ? (x = typeof t == "function" ? t : S(+t), l) : x;
  }, l.endAngle = function(t) {
    return arguments.length ? (s = typeof t == "function" ? t : S(+t), l) : s;
  }, l.padAngle = function(t) {
    return arguments.length ? (o = typeof t == "function" ? t : S(+t), l) : o;
  }, l;
}
var L = Y.pie, G = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1,
  config: L
}, T = G.sections, N = G.showData, ge = structuredClone(L), de = /* @__PURE__ */ g(() => structuredClone(ge), "getConfig"), fe = /* @__PURE__ */ g(() => {
  T = /* @__PURE__ */ new Map(), N = G.showData, ee();
}, "clear"), me = /* @__PURE__ */ g(({ 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"), he = /* @__PURE__ */ g(() => T, "getSections"), ve = /* @__PURE__ */ g((e) => {
  N = e;
}, "setShowData"), Se = /* @__PURE__ */ g(() => N, "getShowData"), _ = {
  getConfig: de,
  clear: fe,
  setDiagramTitle: Z,
  getDiagramTitle: q,
  setAccTitle: H,
  getAccTitle: J,
  setAccDescription: Q,
  getAccDescription: X,
  addSection: me,
  getSections: he,
  setShowData: ve,
  getShowData: Se
}, xe = /* @__PURE__ */ g((e, a) => {
  se(e, a), a.setShowData(e.showData), e.sections.map(a.addSection);
}, "populateDb"), ye = {
  parse: /* @__PURE__ */ g(async (e) => {
    const a = await le("pie", e);
    z.debug(a), xe(a, _);
  }, "parse")
}, we = /* @__PURE__ */ g((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"), Ae = we, De = /* @__PURE__ */ g((e) => {
  const a = [...e.values()].reduce((s, o) => s + o, 0), f = [...e.entries()].map(([s, o]) => ({ label: s, value: o })).filter((s) => s.value / a * 100 >= 1).sort((s, o) => o.value - s.value);
  return pe().value((s) => s.value)(f);
}, "createPieArcs"), Ce = /* @__PURE__ */ g((e, a, f, x) => {
  z.debug(`rendering pie chart
` + e);
  const s = x.db, o = te(), l = ae(s.getConfig(), o.pie), t = 40, n = 18, c = 4, u = 450, y = u, h = re(a), p = h.append("g");
  p.attr("transform", "translate(" + y / 2 + "," + u / 2 + ")");
  const { themeVariables: i } = o;
  let [v] = ne(i.pieOuterStrokeWidth);
  v ?? (v = 2);
  const w = l.textPosition, m = Math.min(y, u) / 2 - t, C = I().innerRadius(0).outerRadius(m), $ = I().innerRadius(m * w).outerRadius(m * w);
  p.append("circle").attr("cx", 0).attr("cy", 0).attr("r", m + v / 2).attr("class", "pieOuterCircle");
  const d = s.getSections(), A = De(d), D = [
    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 E = 0;
  d.forEach((r) => {
    E += r;
  });
  const P = A.filter((r) => (r.data.value / E * 100).toFixed(0) !== "0"), b = oe(D);
  p.selectAll("mySlices").data(P).enter().append("path").attr("d", C).attr("fill", (r) => b(r.data.label)).attr("class", "pieCircle"), p.selectAll("mySlices").data(P).enter().append("text").text((r) => (r.data.value / E * 100).toFixed(0) + "%").attr("transform", (r) => "translate(" + $.centroid(r) + ")").style("text-anchor", "middle").attr("class", "slice"), p.append("text").text(s.getDiagramTitle()).attr("x", 0).attr("y", -(u - 50) / 2).attr("class", "pieTitleText");
  const W = [...d.entries()].map(([r, M]) => ({
    label: r,
    value: M
  })), k = p.selectAll(".legend").data(W).enter().append("g").attr("class", "legend").attr("transform", (r, M) => {
    const R = n + c, V = R * W.length / 2, U = 12 * n, j = M * R - V;
    return "translate(" + U + "," + j + ")";
  });
  k.append("rect").attr("width", n).attr("height", n).style("fill", (r) => b(r.label)).style("stroke", (r) => b(r.label)), k.append("text").attr("x", n + c).attr("y", n - c).text((r) => s.getShowData() ? `${r.label} [${r.value}]` : r.label);
  const B = Math.max(
    ...k.selectAll("text").nodes().map((r) => (r == null ? void 0 : r.getBoundingClientRect().width) ?? 0)
  ), O = y + t + n + c + B;
  h.attr("viewBox", `0 0 ${O} ${u}`), ie(h, u, O, l.useMaxWidth);
}, "draw"), $e = { draw: Ce }, Pe = {
  parser: ye,
  db: _,
  renderer: $e,
  styles: Ae
};
export {
  Pe as diagram
};
