import { __name as e, log as t } from "./chunk-AGHRB4JF.js";
import { clear as n, configureSvgSize as r, defaultConfig_default as i, getAccDescription as a, getAccTitle as o, getConfig as s, getDiagramTitle as c, getThemeVariables3 as l, setAccDescription as u, setAccTitle as d, setDiagramTitle as f } from "./chunk-ICPOFSXX.js";
import p from "../../../../d3-selection/src/select.js";
import { format as m } from "../../../../d3-format/src/defaultLocale.js";
import h from "../../../../d3-hierarchy/src/hierarchy/index.js";
import g from "../../../../d3-hierarchy/src/treemap/index.js";
import _ from "../../../../d3-scale/src/ordinal.js";
import "../../../../d3/src/index.js";
import { selectSvgElement as v } from "./chunk-426QAEUC.js";
import { cleanAndMerge as y } from "./chunk-5PVQY5BW.js";
import { isLabelStyle as b, styles2String as x } from "./chunk-X2U36JSP.js";
import { populateCommonDb as S } from "./chunk-4BX2VUAB.js";
import { parse as C } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
import { setupViewPortForSVG as w } from "./chunk-EDXVE4YY.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/diagram-G4DWMVQ6.mjs
var T = class {
	constructor() {
		this.nodes = [], this.levels = /* @__PURE__ */ new Map(), this.outerNodes = [], this.classes = /* @__PURE__ */ new Map(), this.setAccTitle = d, this.getAccTitle = o, this.setDiagramTitle = f, this.getDiagramTitle = c, this.getAccDescription = a, this.setAccDescription = u;
	}
	static {
		e(this, "TreeMapDB");
	}
	getNodes() {
		return this.nodes;
	}
	getConfig() {
		let e = i, t = s();
		return y({
			...e.treemap,
			...t.treemap ?? {}
		});
	}
	addNode(e, t) {
		this.nodes.push(e), this.levels.set(e, t), t === 0 && (this.outerNodes.push(e), this.root ??= e);
	}
	getRoot() {
		return {
			name: "",
			children: this.outerNodes
		};
	}
	addClass(e, t) {
		let n = this.classes.get(e) ?? {
			id: e,
			styles: [],
			textStyles: []
		}, r = t.replace(/\\,/g, "§§§").replace(/,/g, ";").replace(/§§§/g, ",").split(";");
		r && r.forEach((e) => {
			b(e) && (n?.textStyles ? n.textStyles.push(e) : n.textStyles = [e]), n?.styles ? n.styles.push(e) : n.styles = [e];
		}), this.classes.set(e, n);
	}
	getClasses() {
		return this.classes;
	}
	getStylesForClass(e) {
		return this.classes.get(e)?.styles ?? [];
	}
	clear() {
		n(), this.nodes = [], this.levels = /* @__PURE__ */ new Map(), this.outerNodes = [], this.classes = /* @__PURE__ */ new Map(), this.root = void 0;
	}
};
function E(e) {
	if (!e.length) return [];
	let t = [], n = [];
	return e.forEach((e) => {
		let r = {
			name: e.name,
			children: e.type === "Leaf" ? void 0 : []
		};
		for (r.classSelector = e?.classSelector, e?.cssCompiledStyles && (r.cssCompiledStyles = e.cssCompiledStyles), e.type === "Leaf" && e.value !== void 0 && (r.value = e.value); n.length > 0 && n[n.length - 1].level >= e.level;) n.pop();
		if (n.length === 0) t.push(r);
		else {
			let e = n[n.length - 1].node;
			e.children ? e.children.push(r) : e.children = [r];
		}
		e.type !== "Leaf" && n.push({
			node: r,
			level: e.level
		});
	}), t;
}
e(E, "buildHierarchy");
var D = /* @__PURE__ */ e((t, n) => {
	S(t, n);
	let r = [];
	for (let e of t.TreemapRows ?? []) e.$type === "ClassDefStatement" && n.addClass(e.className ?? "", e.styleText ?? "");
	for (let e of t.TreemapRows ?? []) {
		let t = e.item;
		if (!t) continue;
		let i = e.indent ? parseInt(e.indent) : 0, a = O(t), o = t.classSelector ? n.getStylesForClass(t.classSelector) : [], s = o.length > 0 ? o : void 0, c = {
			level: i,
			name: a,
			type: t.$type,
			value: t.value,
			classSelector: t.classSelector,
			cssCompiledStyles: s
		};
		r.push(c);
	}
	let i = E(r), a = /* @__PURE__ */ e((e, t) => {
		for (let r of e) n.addNode(r, t), r.children && r.children.length > 0 && a(r.children, t + 1);
	}, "addNodesRecursively");
	a(i, 0);
}, "populate"), O = /* @__PURE__ */ e((e) => e.name ? String(e.name) : "", "getItemName"), k = {
	parser: { yy: void 0 },
	parse: /* @__PURE__ */ e(async (e) => {
		try {
			let n = await C("treemap", e);
			t.debug("Treemap AST:", n);
			let r = k.parser?.yy;
			if (!(r instanceof T)) throw 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.");
			D(n, r);
		} catch (e) {
			throw t.error("Error parsing treemap:", e), e;
		}
	}, "parse")
}, A = 10, j = 10, M = 25, N = {
	draw: /* @__PURE__ */ e((n, i, a, o) => {
		let c = o.db, l = c.getConfig(), u = l.padding ?? A, d = c.getDiagramTitle(), f = c.getRoot(), { themeVariables: y } = s();
		if (!f) return;
		let b = d ? 30 : 0, S = v(i), C = l.nodeWidth ? l.nodeWidth * j : 960, T = l.nodeHeight ? l.nodeHeight * j : 500, E = C, D = T + b;
		S.attr("viewBox", `0 0 ${E} ${D}`), r(S, D, E, l.useMaxWidth);
		let O;
		try {
			let t = l.valueFormat || ",";
			if (t === "$0,0") O = /* @__PURE__ */ e((e) => "$" + m(",")(e), "valueFormat");
			else if (t.startsWith("$") && t.includes(",")) {
				let n = /\.\d+/.exec(t), r = n ? n[0] : "";
				O = /* @__PURE__ */ e((e) => "$" + m("," + r)(e), "valueFormat");
			} else if (t.startsWith("$")) {
				let n = t.substring(1);
				O = /* @__PURE__ */ e((e) => "$" + m(n || "")(e), "valueFormat");
			} else O = m(t);
		} catch (e) {
			t.error("Error creating format function:", e), O = m(",");
		}
		let k = _().range([
			"transparent",
			y.cScale0,
			y.cScale1,
			y.cScale2,
			y.cScale3,
			y.cScale4,
			y.cScale5,
			y.cScale6,
			y.cScale7,
			y.cScale8,
			y.cScale9,
			y.cScale10,
			y.cScale11
		]), N = _().range([
			"transparent",
			y.cScalePeer0,
			y.cScalePeer1,
			y.cScalePeer2,
			y.cScalePeer3,
			y.cScalePeer4,
			y.cScalePeer5,
			y.cScalePeer6,
			y.cScalePeer7,
			y.cScalePeer8,
			y.cScalePeer9,
			y.cScalePeer10,
			y.cScalePeer11
		]), P = _().range([
			y.cScaleLabel0,
			y.cScaleLabel1,
			y.cScaleLabel2,
			y.cScaleLabel3,
			y.cScaleLabel4,
			y.cScaleLabel5,
			y.cScaleLabel6,
			y.cScaleLabel7,
			y.cScaleLabel8,
			y.cScaleLabel9,
			y.cScaleLabel10,
			y.cScaleLabel11
		]);
		d && S.append("text").attr("x", E / 2).attr("y", b / 2).attr("class", "treemapTitle").attr("text-anchor", "middle").attr("dominant-baseline", "middle").text(d);
		let F = S.append("g").attr("transform", `translate(0, ${b})`).attr("class", "treemapContainer"), I = h(f).sum((e) => e.value ?? 0).sort((e, t) => (t.value ?? 0) - (e.value ?? 0)), L = g().size([C, T]).paddingTop((e) => e.children && e.children.length > 0 ? M + j : 0).paddingInner(u).paddingLeft((e) => e.children && e.children.length > 0 ? j : 0).paddingRight((e) => e.children && e.children.length > 0 ? j : 0).paddingBottom((e) => e.children && e.children.length > 0 ? j : 0).round(!0)(I), R = L.descendants().filter((e) => e.children && e.children.length > 0), z = F.selectAll(".treemapSection").data(R).enter().append("g").attr("class", "treemapSection").attr("transform", (e) => `translate(${e.x0},${e.y0})`);
		z.append("rect").attr("width", (e) => e.x1 - e.x0).attr("height", M).attr("class", "treemapSectionHeader").attr("fill", "none").attr("fill-opacity", .6).attr("stroke-width", .6).attr("style", (e) => e.depth === 0 ? "display: none;" : ""), z.append("clipPath").attr("id", (e, t) => `clip-section-${i}-${t}`).append("rect").attr("width", (e) => Math.max(0, e.x1 - e.x0 - 12)).attr("height", M), z.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) => k(e.data.name)).attr("fill-opacity", .6).attr("stroke", (e) => N(e.data.name)).attr("stroke-width", 2).attr("stroke-opacity", .4).attr("style", (e) => {
			if (e.depth === 0) return "display: none;";
			let t = x({ cssCompiledStyles: e.data.cssCompiledStyles });
			return t.nodeStyles + ";" + t.borderStyles.join(";");
		}), z.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) => e.depth === 0 ? "display: none;" : "dominant-baseline: middle; font-size: 12px; fill:" + P(e.data.name) + "; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" + x({ cssCompiledStyles: e.data.cssCompiledStyles }).labelStyles.replace("color:", "fill:")).each(function(e) {
			if (e.depth === 0) return;
			let t = p(this), n = e.data.name;
			t.text(n);
			let r = e.x1 - e.x0, i;
			i = l.showValues !== !1 && e.value ? r - 10 - 30 - 10 - 6 : r - 6 - 6;
			let a = Math.max(15, i), o = t.node();
			if (o.getComputedTextLength() > a) {
				let e = n;
				for (; e.length > 0;) {
					if (e = n.substring(0, e.length - 1), e.length === 0) {
						t.text("..."), o.getComputedTextLength() > a && t.text("");
						break;
					}
					if (t.text(e + "..."), o.getComputedTextLength() <= a) break;
				}
			}
		}), l.showValues !== !1 && z.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 ? O(e.value) : "").attr("font-style", "italic").attr("style", (e) => e.depth === 0 ? "display: none;" : "text-anchor: end; dominant-baseline: middle; font-size: 10px; fill:" + P(e.data.name) + "; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" + x({ cssCompiledStyles: e.data.cssCompiledStyles }).labelStyles.replace("color:", "fill:"));
		let B = L.leaves(), V = F.selectAll(".treemapLeafGroup").data(B).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})`);
		V.append("rect").attr("width", (e) => e.x1 - e.x0).attr("height", (e) => e.y1 - e.y0).attr("class", "treemapLeaf").attr("fill", (e) => e.parent ? k(e.parent.data.name) : k(e.data.name)).attr("style", (e) => x({ cssCompiledStyles: e.data.cssCompiledStyles }).nodeStyles).attr("fill-opacity", .3).attr("stroke", (e) => e.parent ? k(e.parent.data.name) : k(e.data.name)).attr("stroke-width", 3), V.append("clipPath").attr("id", (e, t) => `clip-${i}-${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)), V.append("text").attr("class", "treemapLabel").attr("x", (e) => (e.x1 - e.x0) / 2).attr("y", (e) => (e.y1 - e.y0) / 2).attr("style", (e) => "text-anchor: middle; dominant-baseline: middle; font-size: 38px;fill:" + P(e.data.name) + ";" + x({ cssCompiledStyles: e.data.cssCompiledStyles }).labelStyles.replace("color:", "fill:")).attr("clip-path", (e, t) => `url(#clip-${i}-${t})`).text((e) => e.data.name).each(function(e) {
			let t = p(this), n = e.x1 - e.x0, r = e.y1 - e.y0, i = t.node(), a = n - 8, o = r - 8;
			if (a < 10 || o < 10) {
				t.style("display", "none");
				return;
			}
			let s = parseInt(t.style("font-size"), 10), c = .6;
			for (; i.getComputedTextLength() > a && s > 8;) s--, t.style("font-size", `${s}px`);
			let l = Math.max(6, Math.min(28, Math.round(s * c))), u = s + 2 + l;
			for (; u > o && s > 8 && (s--, l = Math.max(6, Math.min(28, Math.round(s * c))), !(l < 6 && s === 8));) t.style("font-size", `${s}px`), u = s + 2 + l;
			t.style("font-size", `${s}px`), (i.getComputedTextLength() > a || s < 8 || o < s) && t.style("display", "none");
		}), l.showValues !== !1 && V.append("text").attr("class", "treemapValue").attr("x", (e) => (e.x1 - e.x0) / 2).attr("y", function(e) {
			return (e.y1 - e.y0) / 2;
		}).attr("style", (e) => "text-anchor: middle; dominant-baseline: hanging; font-size: 28px;fill:" + P(e.data.name) + ";" + x({ cssCompiledStyles: e.data.cssCompiledStyles }).labelStyles.replace("color:", "fill:")).attr("clip-path", (e, t) => `url(#clip-${i}-${t})`).text((e) => e.value ? O(e.value) : "").each(function(e) {
			let t = p(this), n = this.parentNode;
			if (!n) {
				t.style("display", "none");
				return;
			}
			let r = p(n).select(".treemapLabel");
			if (r.empty() || r.style("display") === "none") {
				t.style("display", "none");
				return;
			}
			let i = parseFloat(r.style("font-size")), a = Math.max(6, Math.min(28, Math.round(i * .6)));
			t.style("font-size", `${a}px`);
			let o = (e.y1 - e.y0) / 2 + i / 2 + 2;
			t.attr("y", o);
			let s = e.x1 - e.x0, c = e.y1 - e.y0 - 4, l = s - 8;
			t.node().getComputedTextLength() > l || o + a > c || a < 6 ? t.style("display", "none") : t.style("display", null);
		}), w(S, l.diagramPadding ?? 8, "flowchart", l?.useMaxWidth || !1);
	}, "draw"),
	getClasses: /* @__PURE__ */ e(function(e, t) {
		return t.db.getClasses();
	}, "getClasses")
}, P = {
	sectionStrokeColor: "black",
	sectionStrokeWidth: "1",
	sectionFillColor: "#efefef",
	leafStrokeColor: "black",
	leafStrokeWidth: "1",
	leafFillColor: "#efefef",
	labelFontSize: "12px",
	valueFontSize: "10px",
	titleFontSize: "14px"
}, F = {
	parser: k,
	get db() {
		return new T();
	},
	renderer: N,
	styles: /* @__PURE__ */ e(({ treemap: e } = {}) => {
		let t = y(l(), s().themeVariables), n = y(P, e), r = n.titleColor ?? t.titleColor, i = n.labelColor ?? t.textColor, a = n.valueColor ?? t.textColor;
		return `
  .treemapNode.section {
    stroke: ${n.sectionStrokeColor};
    stroke-width: ${n.sectionStrokeWidth};
    fill: ${n.sectionFillColor};
  }
  .treemapNode.leaf {
    stroke: ${n.leafStrokeColor};
    stroke-width: ${n.leafStrokeWidth};
    fill: ${n.leafFillColor};
  }
  .treemapLabel {
    fill: ${i};
    font-size: ${n.labelFontSize};
  }
  .treemapValue {
    fill: ${a};
    font-size: ${n.valueFontSize};
  }
  .treemapTitle {
    fill: ${r};
    font-size: ${n.titleFontSize};
  }
  `;
	}, "getStyles")
};
//#endregion
export { F as diagram };
