import { K as G, G as H, W as K, U, i as V, f as q, p as u, g as F, Z as I, s as J, a0 as Q, a1 as _, A as X, H as Y, F as tt, a2 as m, a3 as et, a4 as k } from "./index-CNUFa5l3.js";
import { s as at } from "./ordinal-D9ksOc8y-CrGveXkB.js";
import { x as E } from "./arc-DZdQjcG8-O7SaDQo1.js";
import { t as rt } from "./chunk-4BX2VUAB-C_NnimAy-9zhkU6MG.js";
import { r as nt } from "./wardley-L42UT6IY-BjAkE_cs-li1xYed9.js";
function it(t, a) {
  return a < t ? -1 : a > t ? 1 : a >= t ? 0 : NaN;
}
function lt(t) {
  return t;
}
function st() {
  var t = lt, a = it, c = null, s = m(0), o = m(k), x = m(0);
  function l(e) {
    var i, n = (e = et(e)).length, g, v, A = 0, y = Array(n), p = Array(n), w = +s.apply(this, arguments), b = Math.min(k, Math.max(-k, o.apply(this, arguments) - w)), f, T = Math.min(Math.abs(b) / n, x.apply(this, arguments)), S = T * (b < 0 ? -1 : 1), d;
    for (i = 0; i < n; ++i) (d = p[y[i] = i] = +t(e[i], i, e)) > 0 && (A += d);
    for (a == null ? c != null && y.sort(function($, h) {
      return c(e[$], e[h]);
    }) : y.sort(function($, h) {
      return a(p[$], p[h]);
    }), i = 0, v = A ? (b - n * S) / A : 0; i < n; ++i, w = f) g = y[i], d = p[g], f = w + (d > 0 ? d * v : 0) + S, p[g] = {
      data: e[g],
      index: i,
      value: d,
      startAngle: w,
      endAngle: f,
      padAngle: T
    };
    return p;
  }
  return l.value = function(e) {
    return arguments.length ? (t = typeof e == "function" ? e : m(+e), l) : t;
  }, l.sortValues = function(e) {
    return arguments.length ? (a = e, c = null, l) : a;
  }, l.sort = function(e) {
    return arguments.length ? (c = e, a = null, l) : c;
  }, l.startAngle = function(e) {
    return arguments.length ? (s = typeof e == "function" ? e : m(+e), l) : s;
  }, l.endAngle = function(e) {
    return arguments.length ? (o = typeof e == "function" ? e : m(+e), l) : o;
  }, l.padAngle = function(e) {
    return arguments.length ? (x = typeof e == "function" ? e : m(+e), l) : x;
  }, l;
}
var ot = tt.pie, O = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, C = O.sections, M = O.showData, pt = structuredClone(ot), B = {
  getConfig: /* @__PURE__ */ u(() => structuredClone(pt), "getConfig"),
  clear: /* @__PURE__ */ u(() => {
    C = /* @__PURE__ */ new Map(), M = O.showData, Y();
  }, "clear"),
  setDiagramTitle: q,
  getDiagramTitle: V,
  setAccTitle: U,
  getAccTitle: K,
  setAccDescription: H,
  getAccDescription: G,
  addSection: /* @__PURE__ */ u(({ label: t, value: a }) => {
    if (a < 0) throw Error(`"${t}" has invalid value: ${a}. Negative values are not allowed in pie charts. All slice values must be >= 0.`);
    C.has(t) || (C.set(t, a), F.debug(`added new section: ${t}, with value: ${a}`));
  }, "addSection"),
  getSections: /* @__PURE__ */ u(() => C, "getSections"),
  setShowData: /* @__PURE__ */ u((t) => {
    M = t;
  }, "setShowData"),
  getShowData: /* @__PURE__ */ u(() => M, "getShowData")
}, ut = /* @__PURE__ */ u((t, a) => {
  rt(t, a), a.setShowData(t.showData), t.sections.map(a.addSection);
}, "populateDb"), ct = { parse: /* @__PURE__ */ u(async (t) => {
  let a = await nt("pie", t);
  F.debug(a), ut(a, B);
}, "parse") }, dt = /* @__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"), gt = /* @__PURE__ */ u((t) => {
  let a = [...t.values()].reduce((s, o) => s + o, 0), c = [...t.entries()].map(([s, o]) => ({
    label: s,
    value: o
  })).filter((s) => s.value / a * 100 >= 1);
  return st().value((s) => s.value).sort(null)(c);
}, "createPieArcs"), wt = {
  parser: ct,
  db: B,
  renderer: { draw: /* @__PURE__ */ u((t, a, c, s) => {
    var N;
    F.debug(`rendering pie chart
` + t);
    let o = s.db, x = I(), l = J(o.getConfig(), x.pie), e = Q(a), i = e.append("g");
    i.attr("transform", "translate(225,225)");
    let { themeVariables: n } = x, [g] = _(n.pieOuterStrokeWidth);
    g ?? (g = 2);
    let v = l.textPosition, A = E().innerRadius(0).outerRadius(185), y = E().innerRadius(185 * v).outerRadius(185 * v);
    i.append("circle").attr("cx", 0).attr("cy", 0).attr("r", 185 + g / 2).attr("class", "pieOuterCircle");
    let p = o.getSections(), w = gt(p), b = [
      n.pie1,
      n.pie2,
      n.pie3,
      n.pie4,
      n.pie5,
      n.pie6,
      n.pie7,
      n.pie8,
      n.pie9,
      n.pie10,
      n.pie11,
      n.pie12
    ], f = 0;
    p.forEach((r) => {
      f += r;
    });
    let T = w.filter((r) => (r.data.value / f * 100).toFixed(0) !== "0"), S = at(b).domain([...p.keys()]);
    i.selectAll("mySlices").data(T).enter().append("path").attr("d", A).attr("fill", (r) => S(r.data.label)).attr("class", "pieCircle"), i.selectAll("mySlices").data(T).enter().append("text").text((r) => (r.data.value / f * 100).toFixed(0) + "%").attr("transform", (r) => "translate(" + y.centroid(r) + ")").style("text-anchor", "middle").attr("class", "slice");
    let d = i.append("text").text(o.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText"), $ = [...p.entries()].map(([r, D]) => ({
      label: r,
      value: D
    })), h = i.selectAll(".legend").data($).enter().append("g").attr("class", "legend").attr("transform", (r, D) => {
      let j = 22 * $.length / 2;
      return "translate(216," + (D * 22 - j) + ")";
    });
    h.append("rect").attr("width", 18).attr("height", 18).style("fill", (r) => S(r.label)).style("stroke", (r) => S(r.label)), h.append("text").attr("x", 22).attr("y", 14).text((r) => o.getShowData() ? `${r.label} [${r.value}]` : r.label);
    let L = 512 + Math.max(...h.selectAll("text").nodes().map((r) => (r == null ? void 0 : r.getBoundingClientRect().width) ?? 0)), z = ((N = d.node()) == null ? void 0 : N.getBoundingClientRect().width) ?? 0, P = 450 / 2 - z / 2, Z = 450 / 2 + z / 2, R = Math.min(0, P), W = Math.max(L, Z) - R;
    e.attr("viewBox", `${R} 0 ${W} 450`), X(e, 450, W, l.useMaxWidth);
  }, "draw") },
  styles: dt
};
export {
  wt as diagram
};
