import { selectSvgElement as U } from "./chunk-EXTU4WIE.js";
import { populateCommonDb as V } from "./chunk-4BX2VUAB.js";
import { cleanAndMerge as Z, parseFontSize as j } from "./chunk-S3R3BYOJ.js";
import { getAccDescription as q, setAccDescription as H, getAccTitle as J, setAccTitle as K, getDiagramTitle as Q, setDiagramTitle as X, getConfig2 as Y, configureSvgSize as ee, clear as te, defaultConfig_default as ae } from "./chunk-ABZYJK2D.js";
import { __name as o, log as w } from "./chunk-AGHRB4JF.js";
import { parse as re } from "../../../../../../@mermaid-js_parser@0.6.3/node_modules/@mermaid-js/parser/dist/mermaid-parser.core.js";
import "../../../../../../d3-transition@3.0.1_d3-selection@3.0.0/node_modules/d3-transition/src/selection/index.js";
import "../../../../../../d3-zoom@3.0.0/node_modules/d3-zoom/src/transform.js";
import G from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/arc.js";
import ie from "../../../../../../d3-scale@4.0.2/node_modules/d3-scale/src/ordinal.js";
import oe from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/pie.js";
var le = ae.pie, C = {
  sections: /* @__PURE__ */ new Map(),
  showData: !1
}, g = C.sections, D = C.showData, ne = structuredClone(le), se = /* @__PURE__ */ o(() => structuredClone(ne), "getConfig"), ce = /* @__PURE__ */ o(() => {
  g = /* @__PURE__ */ new Map(), D = C.showData, te();
}, "clear"), pe = /* @__PURE__ */ o(({ 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.`
    );
  g.has(e) || (g.set(e, a), w.debug(`added new section: ${e}, with value: ${a}`));
}, "addSection"), de = /* @__PURE__ */ o(() => g, "getSections"), ge = /* @__PURE__ */ o((e) => {
  D = e;
}, "setShowData"), fe = /* @__PURE__ */ o(() => D, "getShowData"), W = {
  getConfig: se,
  clear: ce,
  setDiagramTitle: X,
  getDiagramTitle: Q,
  setAccTitle: K,
  getAccTitle: J,
  setAccDescription: H,
  getAccDescription: q,
  addSection: pe,
  getSections: de,
  setShowData: ge,
  getShowData: fe
}, me = /* @__PURE__ */ o((e, a) => {
  V(e, a), a.setShowData(e.showData), e.sections.map(a.addSection);
}, "populateDb"), ue = {
  parse: /* @__PURE__ */ o(async (e) => {
    const a = await re("pie", e);
    w.debug(a), me(a, W);
  }, "parse")
}, he = /* @__PURE__ */ o((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"), ve = he, Se = /* @__PURE__ */ o((e) => {
  const a = [...e.values()].reduce((r, l) => r + l, 0), $ = [...e.entries()].map(([r, l]) => ({ label: r, value: l })).filter((r) => r.value / a * 100 >= 1).sort((r, l) => l.value - r.value);
  return oe().value((r) => r.value)($);
}, "createPieArcs"), xe = /* @__PURE__ */ o((e, a, $, y) => {
  w.debug(`rendering pie chart
` + e);
  const r = y.db, l = Y(), T = Z(r.getConfig(), l.pie), A = 40, n = 18, p = 4, c = 450, f = c, m = U(a), s = m.append("g");
  s.attr("transform", "translate(" + f / 2 + "," + c / 2 + ")");
  const { themeVariables: i } = l;
  let [b] = j(i.pieOuterStrokeWidth);
  b ?? (b = 2);
  const E = T.textPosition, d = Math.min(f, c) / 2 - A, M = G().innerRadius(0).outerRadius(d), O = G().innerRadius(d * E).outerRadius(d * E);
  s.append("circle").attr("cx", 0).attr("cy", 0).attr("r", d + b / 2).attr("class", "pieOuterCircle");
  const u = r.getSections(), P = Se(u), R = [
    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 h = 0;
  u.forEach((t) => {
    h += t;
  });
  const _ = P.filter((t) => (t.data.value / h * 100).toFixed(0) !== "0"), v = ie(R);
  s.selectAll("mySlices").data(_).enter().append("path").attr("d", M).attr("fill", (t) => v(t.data.label)).attr("class", "pieCircle"), s.selectAll("mySlices").data(_).enter().append("text").text((t) => (t.data.value / h * 100).toFixed(0) + "%").attr("transform", (t) => "translate(" + O.centroid(t) + ")").style("text-anchor", "middle").attr("class", "slice"), s.append("text").text(r.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText");
  const k = [...u.entries()].map(([t, x]) => ({
    label: t,
    value: x
  })), S = s.selectAll(".legend").data(k).enter().append("g").attr("class", "legend").attr("transform", (t, x) => {
    const F = n + p, L = F * k.length / 2, N = 12 * n, B = x * F - L;
    return "translate(" + N + "," + B + ")";
  });
  S.append("rect").attr("width", n).attr("height", n).style("fill", (t) => v(t.label)).style("stroke", (t) => v(t.label)), S.append("text").attr("x", n + p).attr("y", n - p).text((t) => r.getShowData() ? `${t.label} [${t.value}]` : t.label);
  const I = Math.max(
    ...S.selectAll("text").nodes().map((t) => (t == null ? void 0 : t.getBoundingClientRect().width) ?? 0)
  ), z = f + A + n + p + I;
  m.attr("viewBox", `0 0 ${z} ${c}`), ee(m, c, z, T.useMaxWidth);
}, "draw"), we = { draw: xe }, Fe = {
  parser: ue,
  db: W,
  renderer: we,
  styles: ve
};
export {
  Fe as diagram
};
