import { __name, log } from "./chunk-AGHRB4JF.js";
import { clear, configureSvgSize, defaultConfig_default, getAccDescription, getAccTitle, getConfig2, getDiagramTitle, setAccDescription, setAccTitle, setDiagramTitle } from "./chunk-ABZYJK2D.js";
import ordinal from "../../../../d3-scale/src/ordinal.js";
import arc_default from "../../../../d3-shape/src/arc.js";
import pie_default from "../../../../d3-shape/src/pie.js";
import "../../../../d3/src/index.js";
import { selectSvgElement } from "./chunk-EXTU4WIE.js";
import { cleanAndMerge, parseFontSize } from "./chunk-S3R3BYOJ.js";
import { populateCommonDb } from "./chunk-4BX2VUAB.js";
import { parse } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
var DEFAULT_PIE_CONFIG = defaultConfig_default.pie, DEFAULT_PIE_DB = {
	sections: /* @__PURE__ */ new Map(),
	showData: !1,
	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: e, value: u }) => {
		if (u < 0) throw Error(`"${e}" has invalid value: ${u}. Negative values are not allowed in pie charts. All slice values must be >= 0.`);
		sections.has(e) || (sections.set(e, u), log.debug(`added new section: ${e}, with value: ${u}`));
	}, "addSection"),
	getSections: /* @__PURE__ */ __name(() => sections, "getSections"),
	setShowData: /* @__PURE__ */ __name((e) => {
		showData = e;
	}, "setShowData"),
	getShowData: /* @__PURE__ */ __name(() => showData, "getShowData")
}, populateDb = /* @__PURE__ */ __name((e, l) => {
	populateCommonDb(e, l), l.setShowData(e.showData), e.sections.map(l.addSection);
}, "populateDb"), parser = { parse: /* @__PURE__ */ __name(async (e) => {
	let u = await parse("pie", e);
	log.debug(u), populateDb(u, db);
}, "parse") }, pieStyles_default = /* @__PURE__ */ __name((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"), createPieArcs = /* @__PURE__ */ __name((e) => {
	let l = [...e.values()].reduce((e, l) => e + l, 0), u = [...e.entries()].map(([e, l]) => ({
		label: e,
		value: l
	})).filter((e) => e.value / l * 100 >= 1).sort((e, l) => l.value - e.value);
	return pie_default().value((e) => e.value)(u);
}, "createPieArcs"), diagram = {
	parser,
	db,
	renderer: { draw: /* @__PURE__ */ __name((e, u, d, f) => {
		log.debug("rendering pie chart\n" + e);
		let p = f.db, m = getConfig2(), h = cleanAndMerge(p.getConfig(), m.pie), g = selectSvgElement(u), _ = g.append("g");
		_.attr("transform", "translate(225,225)");
		let { themeVariables: v } = m, [y] = parseFontSize(v.pieOuterStrokeWidth);
		y ??= 2;
		let b = h.textPosition, x = arc_default().innerRadius(0).outerRadius(185), S = arc_default().innerRadius(185 * b).outerRadius(185 * b);
		_.append("circle").attr("cx", 0).attr("cy", 0).attr("r", 185 + y / 2).attr("class", "pieOuterCircle");
		let C = p.getSections(), w = createPieArcs(C), T = [
			v.pie1,
			v.pie2,
			v.pie3,
			v.pie4,
			v.pie5,
			v.pie6,
			v.pie7,
			v.pie8,
			v.pie9,
			v.pie10,
			v.pie11,
			v.pie12
		], E = 0;
		C.forEach((e) => {
			E += e;
		});
		let D = w.filter((e) => (e.data.value / E * 100).toFixed(0) !== "0"), O = ordinal(T);
		_.selectAll("mySlices").data(D).enter().append("path").attr("d", x).attr("fill", (e) => O(e.data.label)).attr("class", "pieCircle"), _.selectAll("mySlices").data(D).enter().append("text").text((e) => (e.data.value / E * 100).toFixed(0) + "%").attr("transform", (e) => "translate(" + S.centroid(e) + ")").style("text-anchor", "middle").attr("class", "slice"), _.append("text").text(p.getDiagramTitle()).attr("x", 0).attr("y", -400 / 2).attr("class", "pieTitleText");
		let k = [...C.entries()].map(([e, l]) => ({
			label: e,
			value: l
		})), A = _.selectAll(".legend").data(k).enter().append("g").attr("class", "legend").attr("transform", (e, l) => {
			let u = 22 * k.length / 2;
			return "translate(216," + (l * 22 - u) + ")";
		});
		A.append("rect").attr("width", 18).attr("height", 18).style("fill", (e) => O(e.label)).style("stroke", (e) => O(e.label)), A.append("text").attr("x", 22).attr("y", 14).text((e) => p.getShowData() ? `${e.label} [${e.value}]` : e.label);
		let j = 512 + Math.max(...A.selectAll("text").nodes().map((e) => e?.getBoundingClientRect().width ?? 0));
		g.attr("viewBox", `0 0 ${j} 450`), configureSvgSize(g, 450, j, h.useMaxWidth);
	}, "draw") },
	styles: pieStyles_default
};
export { diagram };
