import "./chunk-TCCFYFTB-d3HOqL2I.js";
import "./main-CThhXnXU.js";
import "./purify.es-DT70lfR0.js";
import { n as ordinal } from "./ordinal-B43ZeR68.js";
import { r as constant_default } from "./path-BGaWgPKg.js";
import { p as tau } from "./math-DFcdCCU8.js";
import { t as arc_default } from "./arc-DfkSnvZm.js";
import { t as array_default } from "./array-tvvEqPy7.js";
import { i as cleanAndMerge, p as parseFontSize } from "./chunk-S3R3BYOJ-mQeCz5CE.js";
import { i as log, r as __name } from "./src-Bf2iLOlr.js";
import { B as setAccTitle, C as getDiagramTitle, U as setDiagramTitle, _ as getAccDescription, a as clear, b as getConfig2, c as configureSvgSize, d as defaultConfig_default, v as getAccTitle, z as setAccDescription } from "./chunk-ABZYJK2D-D0cLy8Bb.js";
import { t as selectSvgElement } from "./chunk-EXTU4WIE-BKNXdLmD.js";
import "./dist-BGdYVvOu.js";
import "./chunk-UMXZTB3W-97iS1iEl.js";
import "./chunk-4F5CHEZ2-BZq7Kom7.js";
import "./chunk-FRFDVMJY-BSBUAX7r.js";
import "./chunk-SJTYNZTY-CEG4F0pB.js";
import "./chunk-PL6DKKU2-B0MTXvyc.js";
import "./chunk-TQ3KTPDO-DiCtqVSi.js";
import "./chunk-B2363JML-D9-XOau1.js";
import { t as populateCommonDb } from "./chunk-4BX2VUAB-CzXltWHN.js";
import { t as parse } from "./mermaid-parser.core-DreccfmS.js";
function descending_default(t, o) {
  return o < t ? -1 : o > t ? 1 : o >= t ? 0 : NaN;
}
function identity_default(t) {
  return t;
}
function pie_default() {
  var t = identity_default, c = descending_default, u = null, d = constant_default(0), f = constant_default(tau), p = constant_default(0);
  function m(o) {
    var m2, h = (o = array_default(o)).length, g, _, v = 0, y = Array(h), b = Array(h), x = +d.apply(this, arguments), S = Math.min(tau, Math.max(-tau, f.apply(this, arguments) - x)), C, w = Math.min(Math.abs(S) / h, p.apply(this, arguments)), T = w * (S < 0 ? -1 : 1), E;
    for (m2 = 0; m2 < h; ++m2) (E = b[y[m2] = m2] = +t(o[m2], m2, o)) > 0 && (v += E);
    for (c == null ? u != null && y.sort(function(t2, s) {
      return u(o[t2], o[s]);
    }) : y.sort(function(t2, o2) {
      return c(b[t2], b[o2]);
    }), m2 = 0, _ = v ? (S - h * T) / v : 0; m2 < h; ++m2, x = C) g = y[m2], E = b[g], C = x + (E > 0 ? E * _ : 0) + T, b[g] = {
      data: o[g],
      index: m2,
      value: E,
      startAngle: x,
      endAngle: C,
      padAngle: w
    };
    return b;
  }
  return m.value = function(s) {
    return arguments.length ? (t = typeof s == "function" ? s : constant_default(+s), m) : t;
  }, m.sortValues = function(t2) {
    return arguments.length ? (c = t2, u = null, m) : c;
  }, m.sort = function(t2) {
    return arguments.length ? (u = t2, c = null, m) : u;
  }, m.startAngle = function(t2) {
    return arguments.length ? (d = typeof t2 == "function" ? t2 : constant_default(+t2), m) : d;
  }, m.endAngle = function(t2) {
    return arguments.length ? (f = typeof t2 == "function" ? t2 : constant_default(+t2), m) : f;
  }, m.padAngle = function(t2) {
    return arguments.length ? (p = typeof t2 == "function" ? t2 : constant_default(+t2), m) : p;
  }, m;
}
var DEFAULT_PIE_CONFIG = defaultConfig_default.pie, DEFAULT_PIE_DB = {
  sections: /* @__PURE__ */ new Map(),
  showData: false,
  config: DEFAULT_PIE_CONFIG
}, sections = DEFAULT_PIE_DB.sections, showData = DEFAULT_PIE_DB.showData, config = structuredClone(DEFAULT_PIE_CONFIG), db = {
  getConfig: /* @__PURE__ */ __name(() => structuredClone(config), "getConfig"),
  clear: /* @__PURE__ */ __name(() => {
    sections = /* @__PURE__ */ new Map(), showData = DEFAULT_PIE_DB.showData, clear();
  }, "clear"),
  setDiagramTitle,
  getDiagramTitle,
  setAccTitle,
  getAccTitle,
  setAccDescription,
  getAccDescription,
  addSection: /* @__PURE__ */ __name(({ label: t, value: o }) => {
    if (o < 0) throw Error(`"${t}" has invalid value: ${o}. Negative values are not allowed in pie charts. All slice values must be >= 0.`);
    sections.has(t) || (sections.set(t, o), log.debug(`added new section: ${t}, with value: ${o}`));
  }, "addSection"),
  getSections: /* @__PURE__ */ __name(() => sections, "getSections"),
  setShowData: /* @__PURE__ */ __name((t) => {
    showData = t;
  }, "setShowData"),
  getShowData: /* @__PURE__ */ __name(() => showData, "getShowData")
}, populateDb = /* @__PURE__ */ __name((t, o) => {
  populateCommonDb(t, o), o.setShowData(t.showData), t.sections.map(o.addSection);
}, "populateDb"), parser = { parse: /* @__PURE__ */ __name(async (t) => {
  let o = await parse("pie", t);
  log.debug(o), populateDb(o, db);
}, "parse") }, pieStyles_default = /* @__PURE__ */ __name((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"), createPieArcs = /* @__PURE__ */ __name((t) => {
  let o = [...t.values()].reduce((t2, o2) => t2 + o2, 0), s = [...t.entries()].map(([t2, o2]) => ({
    label: t2,
    value: o2
  })).filter((t2) => t2.value / o * 100 >= 1).sort((t2, o2) => o2.value - t2.value);
  return pie_default().value((t2) => t2.value)(s);
}, "createPieArcs"), diagram = {
  parser,
  db,
  renderer: { draw: /* @__PURE__ */ __name((o, s, l, p) => {
    log.debug("rendering pie chart\n" + o);
    let m = p.db, h = getConfig2(), g = cleanAndMerge(m.getConfig(), h.pie), _ = selectSvgElement(s), v = _.append("g");
    v.attr("transform", "translate(225,225)");
    let { themeVariables: x } = h, [S] = parseFontSize(x.pieOuterStrokeWidth);
    S ?? (S = 2);
    let C = g.textPosition, T = arc_default().innerRadius(0).outerRadius(185), E = arc_default().innerRadius(185 * C).outerRadius(185 * C);
    v.append("circle").attr("cx", 0).attr("cy", 0).attr("r", 185 + S / 2).attr("class", "pieOuterCircle");
    let D = m.getSections(), O = createPieArcs(D), k = [
      x.pie1,
      x.pie2,
      x.pie3,
      x.pie4,
      x.pie5,
      x.pie6,
      x.pie7,
      x.pie8,
      x.pie9,
      x.pie10,
      x.pie11,
      x.pie12
    ], A = 0;
    D.forEach((t) => {
      A += t;
    });
    let j = O.filter((t) => (t.data.value / A * 100).toFixed(0) !== "0"), M = ordinal(k);
    v.selectAll("mySlices").data(j).enter().append("path").attr("d", T).attr("fill", (t) => M(t.data.label)).attr("class", "pieCircle"), v.selectAll("mySlices").data(j).enter().append("text").text((t) => (t.data.value / A * 100).toFixed(0) + "%").attr("transform", (t) => "translate(" + E.centroid(t) + ")").style("text-anchor", "middle").attr("class", "slice"), v.append("text").text(m.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText");
    let N = [...D.entries()].map(([t, o2]) => ({
      label: t,
      value: o2
    })), P = v.selectAll(".legend").data(N).enter().append("g").attr("class", "legend").attr("transform", (t, o2) => {
      let s2 = 22 * N.length / 2;
      return "translate(216," + (o2 * 22 - s2) + ")";
    });
    P.append("rect").attr("width", 18).attr("height", 18).style("fill", (t) => M(t.label)).style("stroke", (t) => M(t.label)), P.append("text").attr("x", 22).attr("y", 14).text((t) => m.getShowData() ? `${t.label} [${t.value}]` : t.label);
    let F = 512 + Math.max(...P.selectAll("text").nodes().map((t) => (t == null ? void 0 : t.getBoundingClientRect().width) ?? 0));
    _.attr("viewBox", `0 0 ${F} 450`), configureSvgSize(_, 450, F, g.useMaxWidth);
  }, "draw") },
  styles: pieStyles_default
};
export {
  diagram
};
