import { m as q } from "./chunk-TMUBEWPD-BRIzn5gH-alZ9Sg3f.js";
import { _ as p, a6 as G, a5 as I, a7 as Y, a8 as Z, aj as H, ak as J, l as R, g as K, aC as Q, aW as U, aY as X, aa as tt, aq as et, aE as at, aZ as y, a_ as nt, a$ as O } from "./index-CMZig9xg.js";
import { D as it } from "./mermaid-parser.core-Ce96PX3Q-CV9ue_xr.js";
import { h as B } from "./arc-CK9P6CkJ-DVgfRhzX.js";
import { h as rt } from "./ordinal-DfAQgscy-BEJTu10r.js";
function lt(t, a) {
  return a < t ? -1 : a > t ? 1 : a >= t ? 0 : NaN;
}
function st(t) {
  return t;
}
function ot() {
  var t = st, a = lt, l = null, h = y(0), g = y(O), w = y(0);
  function r(e) {
    var n, o = (e = nt(e)).length, u, $, x = 0, c = new Array(o), i = new Array(o), f = +h.apply(this, arguments), A = Math.min(O, Math.max(-O, g.apply(this, arguments) - f)), m, v = Math.min(Math.abs(A) / o, w.apply(this, arguments)), C = v * (A < 0 ? -1 : 1), d;
    for (n = 0; n < o; ++n)
      (d = i[c[n] = n] = +t(e[n], n, e)) > 0 && (x += d);
    for (a != null ? c.sort(function(S, D) {
      return a(i[S], i[D]);
    }) : l != null && c.sort(function(S, D) {
      return l(e[S], e[D]);
    }), n = 0, $ = x ? (A - o * C) / x : 0; n < o; ++n, f = m)
      u = c[n], d = i[u], m = f + (d > 0 ? d * $ : 0) + C, i[u] = {
        data: e[u],
        index: n,
        value: d,
        startAngle: f,
        endAngle: m,
        padAngle: v
      };
    return i;
  }
  return r.value = function(e) {
    return arguments.length ? (t = typeof e == "function" ? e : y(+e), r) : t;
  }, r.sortValues = function(e) {
    return arguments.length ? (a = e, l = null, r) : a;
  }, r.sort = function(e) {
    return arguments.length ? (l = e, a = null, r) : l;
  }, r.startAngle = function(e) {
    return arguments.length ? (h = typeof e == "function" ? e : y(+e), r) : h;
  }, r.endAngle = function(e) {
    return arguments.length ? (g = typeof e == "function" ? e : y(+e), r) : g;
  }, r.padAngle = function(e) {
    return arguments.length ? (w = typeof e == "function" ? e : y(+e), r) : w;
  }, r;
}
var ct = at.pie, W = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, M = W.sections, _ = W.showData, pt = structuredClone(ct), ut = /* @__PURE__ */ p(() => structuredClone(pt), "getConfig"), dt = /* @__PURE__ */ p(() => {
  M = /* @__PURE__ */ new Map(), _ = W.showData, et();
}, "clear"), gt = /* @__PURE__ */ p(({ label: t, value: a }) => {
  M.has(t) || (M.set(t, a), R.debug(`added new section: ${t}, with value: ${a}`));
}, "addSection"), ft = /* @__PURE__ */ p(() => M, "getSections"), mt = /* @__PURE__ */ p((t) => {
  _ = t;
}, "setShowData"), ht = /* @__PURE__ */ p(() => _, "getShowData"), L = {
  getConfig: ut,
  clear: dt,
  setDiagramTitle: J,
  getDiagramTitle: H,
  setAccTitle: Z,
  getAccTitle: Y,
  setAccDescription: I,
  getAccDescription: G,
  addSection: gt,
  getSections: ft,
  setShowData: mt,
  getShowData: ht
}, xt = /* @__PURE__ */ p((t, a) => {
  q(t, a), a.setShowData(t.showData), t.sections.map(a.addSection);
}, "populateDb"), St = {
  parse: /* @__PURE__ */ p(async (t) => {
    const a = await it("pie", t);
    R.debug(a), xt(a, L);
  }, "parse")
}, yt = /* @__PURE__ */ p((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"), wt = yt, $t = /* @__PURE__ */ p((t) => {
  const a = [...t.entries()].map((l) => ({
    label: l[0],
    value: l[1]
  })).sort((l, h) => h.value - l.value);
  return ot().value(
    (l) => l.value
  )(a);
}, "createPieArcs"), At = /* @__PURE__ */ p((t, a, l, h) => {
  R.debug(`rendering pie chart
` + t);
  const g = h.db, w = K(), r = Q(g.getConfig(), w.pie), e = 40, n = 18, o = 4, u = 450, $ = u, x = U(a), c = x.append("g");
  c.attr("transform", "translate(" + $ / 2 + "," + u / 2 + ")");
  const { themeVariables: i } = w;
  let [f] = X(i.pieOuterStrokeWidth);
  f ?? (f = 2);
  const A = r.textPosition, m = Math.min($, u) / 2 - e, v = B().innerRadius(0).outerRadius(m), C = B().innerRadius(m * A).outerRadius(m * A);
  c.append("circle").attr("cx", 0).attr("cy", 0).attr("r", m + f / 2).attr("class", "pieOuterCircle");
  const d = g.getSections(), S = $t(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
  ], T = rt(D);
  c.selectAll("mySlices").data(S).enter().append("path").attr("d", v).attr("fill", (s) => T(s.data.label)).attr("class", "pieCircle");
  let F = 0;
  d.forEach((s) => {
    F += s;
  }), c.selectAll("mySlices").data(S).enter().append("text").text((s) => (s.data.value / F * 100).toFixed(0) + "%").attr("transform", (s) => "translate(" + C.centroid(s) + ")").style("text-anchor", "middle").attr("class", "slice"), c.append("text").text(g.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText");
  const z = c.selectAll(".legend").data(T.domain()).enter().append("g").attr("class", "legend").attr("transform", (s, b) => {
    const k = n + o, P = k * T.domain().length / 2, V = 12 * n, j = b * k - P;
    return "translate(" + V + "," + j + ")";
  });
  z.append("rect").attr("width", n).attr("height", n).style("fill", T).style("stroke", T), z.data(S).append("text").attr("x", n + o).attr("y", n - o).text((s) => {
    const { label: b, value: k } = s.data;
    return g.getShowData() ? `${b} [${k}]` : b;
  });
  const N = Math.max(
    ...z.selectAll("text").nodes().map((s) => (s == null ? void 0 : s.getBoundingClientRect().width) ?? 0)
  ), E = $ + e + n + o + N;
  x.attr("viewBox", `0 0 ${E} ${u}`), tt(x, u, E, r.useMaxWidth);
}, "draw"), Dt = { draw: At }, Mt = {
  parser: St,
  db: L,
  renderer: Dt,
  styles: wt
};
export {
  Mt as diagram
};
