import { selectSvgElement as ne } from "./chunk-EXTU4WIE.js";
import { setupViewPortForSVG as oe } from "./chunk-QN33PNHL.js";
import { styles2String as L, isLabelStyle as ie } from "./chunk-ATLVNIR6.js";
import { populateCommonDb as ce } from "./chunk-4BX2VUAB.js";
import { cleanAndMerge as U } from "./chunk-S3R3BYOJ.js";
import { getConfig as q, configureSvgSize as pe, setAccTitle as de, getAccTitle as he, setDiagramTitle as me, getDiagramTitle as ue, getAccDescription as fe, setAccDescription as ye, defaultConfig_default as ge, clear as Se } from "./chunk-ABZYJK2D.js";
import { __name as h, log as I } from "./chunk-AGHRB4JF.js";
import { parse as xe } 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 { format as D } from "../../../../../../d3-format@3.1.2/node_modules/d3-format/src/defaultLocale.js";
import B from "../../../../../../d3-scale@4.0.2/node_modules/d3-scale/src/ordinal.js";
import be from "../../../../../../d3-hierarchy@3.1.2/node_modules/d3-hierarchy/src/hierarchy/index.js";
import ve from "../../../../../../d3-hierarchy@3.1.2/node_modules/d3-hierarchy/src/treemap/index.js";
import _ from "../../../../../../d3-selection@3.0.0/node_modules/d3-selection/src/select.js";
var F, J = (F = class {
  constructor() {
    this.nodes = [], this.levels = /* @__PURE__ */ new Map(), this.outerNodes = [], this.classes = /* @__PURE__ */ new Map(), this.setAccTitle = de, this.getAccTitle = he, this.setDiagramTitle = me, this.getDiagramTitle = ue, this.getAccDescription = fe, this.setAccDescription = ye;
  }
  getNodes() {
    return this.nodes;
  }
  getConfig() {
    const a = ge, o = q();
    return U({
      ...a.treemap,
      ...o.treemap ?? {}
    });
  }
  addNode(a, o) {
    this.nodes.push(a), this.levels.set(a, o), o === 0 && (this.outerNodes.push(a), this.root ?? (this.root = a));
  }
  getRoot() {
    return { name: "", children: this.outerNodes };
  }
  addClass(a, o) {
    const s = this.classes.get(a) ?? { id: a, styles: [], textStyles: [] }, c = o.replace(/\\,/g, "§§§").replace(/,/g, ";").replace(/§§§/g, ",").split(";");
    c && c.forEach((r) => {
      ie(r) && (s != null && s.textStyles ? s.textStyles.push(r) : s.textStyles = [r]), s != null && s.styles ? s.styles.push(r) : s.styles = [r];
    }), this.classes.set(a, s);
  }
  getClasses() {
    return this.classes;
  }
  getStylesForClass(a) {
    var o;
    return ((o = this.classes.get(a)) == null ? void 0 : o.styles) ?? [];
  }
  clear() {
    Se(), this.nodes = [], this.levels = /* @__PURE__ */ new Map(), this.outerNodes = [], this.classes = /* @__PURE__ */ new Map(), this.root = void 0;
  }
}, h(F, "TreeMapDB"), F);
function K(p) {
  if (!p.length)
    return [];
  const a = [], o = [];
  return p.forEach((s) => {
    const c = {
      name: s.name,
      children: s.type === "Leaf" ? void 0 : []
    };
    for (c.classSelector = s == null ? void 0 : s.classSelector, s != null && s.cssCompiledStyles && (c.cssCompiledStyles = [s.cssCompiledStyles]), s.type === "Leaf" && s.value !== void 0 && (c.value = s.value); o.length > 0 && o[o.length - 1].level >= s.level; )
      o.pop();
    if (o.length === 0)
      a.push(c);
    else {
      const r = o[o.length - 1].node;
      r.children ? r.children.push(c) : r.children = [c];
    }
    s.type !== "Leaf" && o.push({ node: c, level: s.level });
  }), a;
}
h(K, "buildHierarchy");
var Ce = /* @__PURE__ */ h((p, a) => {
  ce(p, a);
  const o = [];
  for (const r of p.TreemapRows ?? [])
    r.$type === "ClassDefStatement" && a.addClass(r.className ?? "", r.styleText ?? "");
  for (const r of p.TreemapRows ?? []) {
    const d = r.item;
    if (!d)
      continue;
    const u = r.indent ? parseInt(r.indent) : 0, V = we(d), l = d.classSelector ? a.getStylesForClass(d.classSelector) : [], z = l.length > 0 ? l.join(";") : void 0, b = {
      level: u,
      name: V,
      type: d.$type,
      value: d.value,
      classSelector: d.classSelector,
      cssCompiledStyles: z
    };
    o.push(b);
  }
  const s = K(o), c = /* @__PURE__ */ h((r, d) => {
    for (const u of r)
      a.addNode(u, d), u.children && u.children.length > 0 && c(u.children, d + 1);
  }, "addNodesRecursively");
  c(s, 0);
}, "populate"), we = /* @__PURE__ */ h((p) => p.name ? String(p.name) : "", "getItemName"), Q = {
  // @ts-expect-error - TreeMapDB is not assignable to DiagramDB
  parser: { yy: void 0 },
  parse: /* @__PURE__ */ h(async (p) => {
    var a;
    try {
      const s = await xe("treemap", p);
      I.debug("Treemap AST:", s);
      const c = (a = Q.parser) == null ? void 0 : a.yy;
      if (!(c instanceof J))
        throw new Error(
          "parser.parser?.yy was not a TreemapDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues."
        );
      Ce(s, c);
    } catch (o) {
      throw I.error("Error parsing treemap:", o), o;
    }
  }, "parse")
}, Te = 10, $ = 10, M = 25, Le = /* @__PURE__ */ h((p, a, o, s) => {
  const c = s.db, r = c.getConfig(), d = r.padding ?? Te, u = c.getDiagramTitle(), V = c.getRoot(), { themeVariables: l } = q();
  if (!V)
    return;
  const z = u ? 30 : 0, b = ne(a), G = r.nodeWidth ? r.nodeWidth * $ : 960, O = r.nodeHeight ? r.nodeHeight * $ : 500, H = G, X = O + z;
  b.attr("viewBox", `0 0 ${H} ${X}`), pe(b, X, H, r.useMaxWidth);
  let v;
  try {
    const e = r.valueFormat || ",";
    if (e === "$0,0")
      v = /* @__PURE__ */ h((t) => "$" + D(",")(t), "valueFormat");
    else if (e.startsWith("$") && e.includes(",")) {
      const t = /\.\d+/.exec(e), n = t ? t[0] : "";
      v = /* @__PURE__ */ h((m) => "$" + D("," + n)(m), "valueFormat");
    } else if (e.startsWith("$")) {
      const t = e.substring(1);
      v = /* @__PURE__ */ h((n) => "$" + D(t || "")(n), "valueFormat");
    } else
      v = D(e);
  } catch (e) {
    I.error("Error creating format function:", e), v = D(",");
  }
  const N = B().range([
    "transparent",
    l.cScale0,
    l.cScale1,
    l.cScale2,
    l.cScale3,
    l.cScale4,
    l.cScale5,
    l.cScale6,
    l.cScale7,
    l.cScale8,
    l.cScale9,
    l.cScale10,
    l.cScale11
  ]), Z = B().range([
    "transparent",
    l.cScalePeer0,
    l.cScalePeer1,
    l.cScalePeer2,
    l.cScalePeer3,
    l.cScalePeer4,
    l.cScalePeer5,
    l.cScalePeer6,
    l.cScalePeer7,
    l.cScalePeer8,
    l.cScalePeer9,
    l.cScalePeer10,
    l.cScalePeer11
  ]), W = B().range([
    l.cScaleLabel0,
    l.cScaleLabel1,
    l.cScaleLabel2,
    l.cScaleLabel3,
    l.cScaleLabel4,
    l.cScaleLabel5,
    l.cScaleLabel6,
    l.cScaleLabel7,
    l.cScaleLabel8,
    l.cScaleLabel9,
    l.cScaleLabel10,
    l.cScaleLabel11
  ]);
  u && b.append("text").attr("x", H / 2).attr("y", z / 2).attr("class", "treemapTitle").attr("text-anchor", "middle").attr("dominant-baseline", "middle").text(u);
  const Y = b.append("g").attr("transform", `translate(0, ${z})`).attr("class", "treemapContainer"), ee = be(V).sum((e) => e.value ?? 0).sort((e, t) => (t.value ?? 0) - (e.value ?? 0)), j = ve().size([G, O]).paddingTop(
    (e) => e.children && e.children.length > 0 ? M + $ : 0
  ).paddingInner(d).paddingLeft((e) => e.children && e.children.length > 0 ? $ : 0).paddingRight((e) => e.children && e.children.length > 0 ? $ : 0).paddingBottom((e) => e.children && e.children.length > 0 ? $ : 0).round(!0)(ee), te = j.descendants().filter((e) => e.children && e.children.length > 0), A = Y.selectAll(".treemapSection").data(te).enter().append("g").attr("class", "treemapSection").attr("transform", (e) => `translate(${e.x0},${e.y0})`);
  A.append("rect").attr("width", (e) => e.x1 - e.x0).attr("height", M).attr("class", "treemapSectionHeader").attr("fill", "none").attr("fill-opacity", 0.6).attr("stroke-width", 0.6).attr("style", (e) => e.depth === 0 ? "display: none;" : ""), A.append("clipPath").attr("id", (e, t) => `clip-section-${a}-${t}`).append("rect").attr("width", (e) => Math.max(0, e.x1 - e.x0 - 12)).attr("height", M), A.append("rect").attr("width", (e) => e.x1 - e.x0).attr("height", (e) => e.y1 - e.y0).attr("class", (e, t) => `treemapSection section${t}`).attr("fill", (e) => N(e.data.name)).attr("fill-opacity", 0.6).attr("stroke", (e) => Z(e.data.name)).attr("stroke-width", 2).attr("stroke-opacity", 0.4).attr("style", (e) => {
    if (e.depth === 0)
      return "display: none;";
    const t = L({ cssCompiledStyles: e.data.cssCompiledStyles });
    return t.nodeStyles + ";" + t.borderStyles.join(";");
  }), A.append("text").attr("class", "treemapSectionLabel").attr("x", 6).attr("y", M / 2).attr("dominant-baseline", "middle").text((e) => e.depth === 0 ? "" : e.data.name).attr("font-weight", "bold").attr("style", (e) => {
    if (e.depth === 0)
      return "display: none;";
    const t = "dominant-baseline: middle; font-size: 12px; fill:" + W(e.data.name) + "; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;", n = L({ cssCompiledStyles: e.data.cssCompiledStyles });
    return t + n.labelStyles.replace("color:", "fill:");
  }).each(function(e) {
    if (e.depth === 0)
      return;
    const t = _(this), n = e.data.name;
    t.text(n);
    const m = e.x1 - e.x0, y = 6;
    let g;
    r.showValues !== !1 && e.value ? g = m - 10 - 30 - 10 - y : g = m - y - 6;
    const S = Math.max(15, g), i = t.node();
    if (i.getComputedTextLength() > S) {
      let f = n;
      for (; f.length > 0; ) {
        if (f = n.substring(0, f.length - 1), f.length === 0) {
          t.text("..."), i.getComputedTextLength() > S && t.text("");
          break;
        }
        if (t.text(f + "..."), i.getComputedTextLength() <= S)
          break;
      }
    }
  }), r.showValues !== !1 && A.append("text").attr("class", "treemapSectionValue").attr("x", (e) => e.x1 - e.x0 - 10).attr("y", M / 2).attr("text-anchor", "end").attr("dominant-baseline", "middle").text((e) => e.value ? v(e.value) : "").attr("font-style", "italic").attr("style", (e) => {
    if (e.depth === 0)
      return "display: none;";
    const t = "text-anchor: end; dominant-baseline: middle; font-size: 10px; fill:" + W(e.data.name) + "; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;", n = L({ cssCompiledStyles: e.data.cssCompiledStyles });
    return t + n.labelStyles.replace("color:", "fill:");
  });
  const ae = j.leaves(), E = Y.selectAll(".treemapLeafGroup").data(ae).enter().append("g").attr("class", (e, t) => `treemapNode treemapLeafGroup leaf${t}${e.data.classSelector ? ` ${e.data.classSelector}` : ""}x`).attr("transform", (e) => `translate(${e.x0},${e.y0})`);
  E.append("rect").attr("width", (e) => e.x1 - e.x0).attr("height", (e) => e.y1 - e.y0).attr("class", "treemapLeaf").attr("fill", (e) => e.parent ? N(e.parent.data.name) : N(e.data.name)).attr("style", (e) => L({ cssCompiledStyles: e.data.cssCompiledStyles }).nodeStyles).attr("fill-opacity", 0.3).attr("stroke", (e) => e.parent ? N(e.parent.data.name) : N(e.data.name)).attr("stroke-width", 3), E.append("clipPath").attr("id", (e, t) => `clip-${a}-${t}`).append("rect").attr("width", (e) => Math.max(0, e.x1 - e.x0 - 4)).attr("height", (e) => Math.max(0, e.y1 - e.y0 - 4)), E.append("text").attr("class", "treemapLabel").attr("x", (e) => (e.x1 - e.x0) / 2).attr("y", (e) => (e.y1 - e.y0) / 2).attr("style", (e) => {
    const t = "text-anchor: middle; dominant-baseline: middle; font-size: 38px;fill:" + W(e.data.name) + ";", n = L({ cssCompiledStyles: e.data.cssCompiledStyles });
    return t + n.labelStyles.replace("color:", "fill:");
  }).attr("clip-path", (e, t) => `url(#clip-${a}-${t})`).text((e) => e.data.name).each(function(e) {
    const t = _(this), n = e.x1 - e.x0, m = e.y1 - e.y0, y = t.node(), g = 4, T = n - 2 * g, S = m - 2 * g;
    if (T < 10 || S < 10) {
      t.style("display", "none");
      return;
    }
    let i = parseInt(t.style("font-size"), 10);
    const C = 8, x = 28, f = 0.6, w = 6, k = 2;
    for (; y.getComputedTextLength() > T && i > C; )
      i--, t.style("font-size", `${i}px`);
    let P = Math.max(
      w,
      Math.min(x, Math.round(i * f))
    ), R = i + k + P;
    for (; R > S && i > C && (i--, P = Math.max(
      w,
      Math.min(x, Math.round(i * f))
    ), !(P < w && i === C)); )
      t.style("font-size", `${i}px`), R = i + k + P;
    t.style("font-size", `${i}px`), (y.getComputedTextLength() > T || i < C || S < i) && t.style("display", "none");
  }), r.showValues !== !1 && E.append("text").attr("class", "treemapValue").attr("x", (t) => (t.x1 - t.x0) / 2).attr("y", function(t) {
    return (t.y1 - t.y0) / 2;
  }).attr("style", (t) => {
    const n = "text-anchor: middle; dominant-baseline: hanging; font-size: 28px;fill:" + W(t.data.name) + ";", m = L({ cssCompiledStyles: t.data.cssCompiledStyles });
    return n + m.labelStyles.replace("color:", "fill:");
  }).attr("clip-path", (t, n) => `url(#clip-${a}-${n})`).text((t) => t.value ? v(t.value) : "").each(function(t) {
    const n = _(this), m = this.parentNode;
    if (!m) {
      n.style("display", "none");
      return;
    }
    const y = _(m).select(".treemapLabel");
    if (y.empty() || y.style("display") === "none") {
      n.style("display", "none");
      return;
    }
    const g = parseFloat(y.style("font-size")), T = 28, S = 0.6, i = 6, C = 2, x = Math.max(
      i,
      Math.min(T, Math.round(g * S))
    );
    n.style("font-size", `${x}px`);
    const w = (t.y1 - t.y0) / 2 + g / 2 + C;
    n.attr("y", w);
    const k = t.x1 - t.x0, se = t.y1 - t.y0 - 4, re = k - 8;
    n.node().getComputedTextLength() > re || w + x > se || x < i ? n.style("display", "none") : n.style("display", null);
  });
  const le = r.diagramPadding ?? 8;
  oe(b, le, "flowchart", (r == null ? void 0 : r.useMaxWidth) || !1);
}, "draw"), $e = /* @__PURE__ */ h(function(p, a) {
  return a.db.getClasses();
}, "getClasses"), Fe = { draw: Le, getClasses: $e }, ze = {
  sectionStrokeColor: "black",
  sectionStrokeWidth: "1",
  sectionFillColor: "#efefef",
  leafStrokeColor: "black",
  leafStrokeWidth: "1",
  leafFillColor: "#efefef",
  labelColor: "black",
  labelFontSize: "12px",
  valueFontSize: "10px",
  valueColor: "black",
  titleColor: "black",
  titleFontSize: "14px"
}, Ne = /* @__PURE__ */ h(({
  treemap: p
} = {}) => {
  const a = U(ze, p);
  return `
  .treemapNode.section {
    stroke: ${a.sectionStrokeColor};
    stroke-width: ${a.sectionStrokeWidth};
    fill: ${a.sectionFillColor};
  }
  .treemapNode.leaf {
    stroke: ${a.leafStrokeColor};
    stroke-width: ${a.leafStrokeWidth};
    fill: ${a.leafFillColor};
  }
  .treemapLabel {
    fill: ${a.labelColor};
    font-size: ${a.labelFontSize};
  }
  .treemapValue {
    fill: ${a.valueColor};
    font-size: ${a.valueFontSize};
  }
  .treemapTitle {
    fill: ${a.titleColor};
    font-size: ${a.titleFontSize};
  }
  `;
}, "getStyles"), Ae = Ne, Ue = {
  parser: Q,
  get db() {
    return new J();
  },
  renderer: Fe,
  styles: Ae
};
export {
  Ue as diagram
};
