import { m as Y } from "./chunk-ANTBXLJU-CgMY7K4E-BDFVHt4k.js";
import { a6 as Z, a5 as j, a7 as q, a8 as H, ao as I, an as J, _ as p, l as O, g as K, aA as Q, aU as ee, aW as te, aa as ae, at as ne, aB as ie, aX as y, aY as re, aZ as F } from "./index-DeBlJN4f.js";
import { N as le } from "./mermaid-parser.core-DRkTy7co-D3k_lOFL.js";
import { h as L } from "./arc-B-ukyQgl-BIkl0oly.js";
import { h as se } from "./ordinal-DfAQgscy-BEJTu10r.js";
function oe(e, a) {
  return a < e ? -1 : a > e ? 1 : a >= e ? 0 : NaN;
}
function ce(e) {
  return e;
}
function ue() {
  var e = ce, a = oe, f = null, s = y(0), o = y(F), w = y(0);
  function l(t) {
    var i, c = (t = re(t)).length, d, v, x = 0, u = new Array(c), r = new Array(c), h = +s.apply(this, arguments), S = Math.min(F, Math.max(-F, o.apply(this, arguments) - h)), m, D = Math.min(Math.abs(S) / c, w.apply(this, arguments)), T = D * (S < 0 ? -1 : 1), g;
    for (i = 0; i < c; ++i)
      (g = r[u[i] = i] = +e(t[i], i, t)) > 0 && (x += g);
    for (a != null ? u.sort(function($, A) {
      return a(r[$], r[A]);
    }) : f != null && u.sort(function($, A) {
      return f(t[$], t[A]);
    }), i = 0, v = x ? (S - c * T) / x : 0; i < c; ++i, h = m)
      d = u[i], g = r[d], m = h + (g > 0 ? g * v : 0) + T, r[d] = {
        data: t[d],
        index: i,
        value: g,
        startAngle: h,
        endAngle: m,
        padAngle: D
      };
    return r;
  }
  return l.value = function(t) {
    return arguments.length ? (e = typeof t == "function" ? t : y(+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 ? (s = typeof t == "function" ? t : y(+t), l) : s;
  }, l.endAngle = function(t) {
    return arguments.length ? (o = typeof t == "function" ? t : y(+t), l) : o;
  }, l.padAngle = function(t) {
    return arguments.length ? (w = typeof t == "function" ? t : y(+t), l) : w;
  }, l;
}
var pe = ie.pie, N = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, C = N.sections, R = N.showData, de = structuredClone(pe), ge = /* @__PURE__ */ p(() => structuredClone(de), "getConfig"), fe = /* @__PURE__ */ p(() => {
  C = /* @__PURE__ */ new Map(), R = N.showData, ne();
}, "clear"), he = /* @__PURE__ */ p(({ 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.`
    );
  C.has(e) || (C.set(e, a), O.debug(`added new section: ${e}, with value: ${a}`));
}, "addSection"), me = /* @__PURE__ */ p(() => C, "getSections"), xe = /* @__PURE__ */ p((e) => {
  R = e;
}, "setShowData"), ye = /* @__PURE__ */ p(() => R, "getShowData"), P = {
  getConfig: ge,
  clear: fe,
  setDiagramTitle: J,
  getDiagramTitle: I,
  setAccTitle: H,
  getAccTitle: q,
  setAccDescription: j,
  getAccDescription: Z,
  addSection: he,
  getSections: me,
  setShowData: xe,
  getShowData: ye
}, we = /* @__PURE__ */ p((e, a) => {
  Y(e, a), a.setShowData(e.showData), e.sections.map(a.addSection);
}, "populateDb"), ve = {
  parse: /* @__PURE__ */ p(async (e) => {
    const a = await le("pie", e);
    O.debug(a), we(a, P);
  }, "parse")
}, Se = /* @__PURE__ */ p((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"), $e = Se, Ae = /* @__PURE__ */ p((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 ue().value((s) => s.value)(f);
}, "createPieArcs"), De = /* @__PURE__ */ p((e, a, f, s) => {
  O.debug(`rendering pie chart
` + e);
  const o = s.db, w = K(), l = Q(o.getConfig(), w.pie), t = 40, i = 18, c = 4, d = 450, v = d, x = ee(a), u = x.append("g");
  u.attr("transform", "translate(" + v / 2 + "," + d / 2 + ")");
  const { themeVariables: r } = w;
  let [h] = te(r.pieOuterStrokeWidth);
  h ?? (h = 2);
  const S = l.textPosition, m = Math.min(v, d) / 2 - t, D = L().innerRadius(0).outerRadius(m), T = L().innerRadius(m * S).outerRadius(m * S);
  u.append("circle").attr("cx", 0).attr("cy", 0).attr("r", m + h / 2).attr("class", "pieOuterCircle");
  const g = o.getSections(), $ = Ae(g), A = [
    r.pie1,
    r.pie2,
    r.pie3,
    r.pie4,
    r.pie5,
    r.pie6,
    r.pie7,
    r.pie8,
    r.pie9,
    r.pie10,
    r.pie11,
    r.pie12
  ];
  let b = 0;
  g.forEach((n) => {
    b += n;
  });
  const W = $.filter((n) => (n.data.value / b * 100).toFixed(0) !== "0"), k = se(A);
  u.selectAll("mySlices").data(W).enter().append("path").attr("d", D).attr("fill", (n) => k(n.data.label)).attr("class", "pieCircle"), u.selectAll("mySlices").data(W).enter().append("text").text((n) => (n.data.value / b * 100).toFixed(0) + "%").attr("transform", (n) => "translate(" + T.centroid(n) + ")").style("text-anchor", "middle").attr("class", "slice"), u.append("text").text(o.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText");
  const _ = [...g.entries()].map(([n, z]) => ({
    label: n,
    value: z
  })), M = u.selectAll(".legend").data(_).enter().append("g").attr("class", "legend").attr("transform", (n, z) => {
    const E = i + c, U = E * _.length / 2, G = 12 * i, X = z * E - U;
    return "translate(" + G + "," + X + ")";
  });
  M.append("rect").attr("width", i).attr("height", i).style("fill", (n) => k(n.label)).style("stroke", (n) => k(n.label)), M.append("text").attr("x", i + c).attr("y", i - c).text((n) => o.getShowData() ? `${n.label} [${n.value}]` : n.label);
  const V = Math.max(
    ...M.selectAll("text").nodes().map((n) => (n == null ? void 0 : n.getBoundingClientRect().width) ?? 0)
  ), B = v + t + i + c + V;
  x.attr("viewBox", `0 0 ${B} ${d}`), ae(x, d, B, l.useMaxWidth);
}, "draw"), Te = { draw: De }, Fe = {
  parser: ve,
  db: P,
  renderer: Te,
  styles: $e
};
export {
  Fe as diagram
};
