import { __name, log } from "./chunk-AGHRB4JF.js";
import { clear, defaultConfig_default, getAccDescription, getAccTitle, getConfig, getDiagramTitle, getThemeVariables3, setAccDescription, setAccTitle, setDiagramTitle } from "./chunk-ABZYJK2D.js";
import { selectSvgElement } from "./chunk-EXTU4WIE.js";
import { cleanAndMerge } from "./chunk-S3R3BYOJ.js";
import { populateCommonDb } from "./chunk-4BX2VUAB.js";
import { parse } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
var defaultOptions = {
	showLegend: !0,
	ticks: 5,
	max: null,
	min: 0,
	graticule: "circle"
}, defaultRadarData = {
	axes: [],
	curves: [],
	options: defaultOptions
}, data = structuredClone(defaultRadarData), DEFAULT_RADAR_CONFIG = defaultConfig_default.radar, getConfig2 = /* @__PURE__ */ __name(() => cleanAndMerge({
	...DEFAULT_RADAR_CONFIG,
	...getConfig().radar
}), "getConfig"), getAxes = /* @__PURE__ */ __name(() => data.axes, "getAxes"), getCurves = /* @__PURE__ */ __name(() => data.curves, "getCurves"), getOptions = /* @__PURE__ */ __name(() => data.options, "getOptions"), setAxes = /* @__PURE__ */ __name((c) => {
	data.axes = c.map((c) => ({
		name: c.name,
		label: c.label ?? c.name
	}));
}, "setAxes"), setCurves = /* @__PURE__ */ __name((c) => {
	data.curves = c.map((c) => ({
		name: c.name,
		label: c.label ?? c.name,
		entries: computeCurveEntries(c.entries)
	}));
}, "setCurves"), computeCurveEntries = /* @__PURE__ */ __name((c) => {
	if (c[0].axis == null) return c.map((c) => c.value);
	let k = getAxes();
	if (k.length === 0) throw Error("Axes must be populated before curves for reference entries");
	return k.map((k) => {
		let A = c.find((c) => c.axis?.$refText === k.name);
		if (A === void 0) throw Error("Missing entry for axis " + k.label);
		return A.value;
	});
}, "computeCurveEntries"), db = {
	getAxes,
	getCurves,
	getOptions,
	setAxes,
	setCurves,
	setOptions: /* @__PURE__ */ __name((c) => {
		let k = c.reduce((c, k) => (c[k.name] = k, c), {});
		data.options = {
			showLegend: k.showLegend?.value ?? defaultOptions.showLegend,
			ticks: k.ticks?.value ?? defaultOptions.ticks,
			max: k.max?.value ?? defaultOptions.max,
			min: k.min?.value ?? defaultOptions.min,
			graticule: k.graticule?.value ?? defaultOptions.graticule
		};
	}, "setOptions"),
	getConfig: getConfig2,
	clear: /* @__PURE__ */ __name(() => {
		clear(), data = structuredClone(defaultRadarData);
	}, "clear"),
	setAccTitle,
	getAccTitle,
	setDiagramTitle,
	getDiagramTitle,
	getAccDescription,
	setAccDescription
}, populate = /* @__PURE__ */ __name((c) => {
	populateCommonDb(c, db);
	let { axes: k, curves: A, options: j } = c;
	db.setAxes(k), db.setCurves(A), db.setOptions(j);
}, "populate"), parser = { parse: /* @__PURE__ */ __name(async (c) => {
	let A = await parse("radar", c);
	log.debug(A), populate(A);
}, "parse") }, draw = /* @__PURE__ */ __name((c, k, A, j) => {
	let M = j.db, N = M.getAxes(), P = M.getCurves(), F = M.getOptions(), I = M.getConfig(), L = M.getDiagramTitle(), R = drawFrame(selectSvgElement(k), I), z = F.max ?? Math.max(...P.map((c) => Math.max(...c.entries))), B = F.min, V = Math.min(I.width, I.height) / 2;
	drawGraticule(R, N, V, F.ticks, F.graticule), drawAxes(R, N, V, I), drawCurves(R, N, P, B, z, F.graticule, I), drawLegend(R, P, F.showLegend, I), R.append("text").attr("class", "radarTitle").text(L).attr("x", 0).attr("y", -I.height / 2 - I.marginTop);
}, "draw"), drawFrame = /* @__PURE__ */ __name((c, k) => {
	let A = k.width + k.marginLeft + k.marginRight, j = k.height + k.marginTop + k.marginBottom, M = {
		x: k.marginLeft + k.width / 2,
		y: k.marginTop + k.height / 2
	};
	return c.attr("viewbox", `0 0 ${A} ${j}`).attr("width", A).attr("height", j), c.append("g").attr("transform", `translate(${M.x}, ${M.y})`);
}, "drawFrame"), drawGraticule = /* @__PURE__ */ __name((c, k, A, j, M) => {
	if (M === "circle") for (let k = 0; k < j; k++) {
		let M = A * (k + 1) / j;
		c.append("circle").attr("r", M).attr("class", "radarGraticule");
	}
	else if (M === "polygon") {
		let M = k.length;
		for (let N = 0; N < j; N++) {
			let P = A * (N + 1) / j, F = k.map((c, k) => {
				let A = 2 * k * Math.PI / M - Math.PI / 2;
				return `${P * Math.cos(A)},${P * Math.sin(A)}`;
			}).join(" ");
			c.append("polygon").attr("points", F).attr("class", "radarGraticule");
		}
	}
}, "drawGraticule"), drawAxes = /* @__PURE__ */ __name((c, k, A, j) => {
	let M = k.length;
	for (let N = 0; N < M; N++) {
		let P = k[N].label, F = 2 * N * Math.PI / M - Math.PI / 2;
		c.append("line").attr("x1", 0).attr("y1", 0).attr("x2", A * j.axisScaleFactor * Math.cos(F)).attr("y2", A * j.axisScaleFactor * Math.sin(F)).attr("class", "radarAxisLine"), c.append("text").text(P).attr("x", A * j.axisLabelFactor * Math.cos(F)).attr("y", A * j.axisLabelFactor * Math.sin(F)).attr("class", "radarAxisLabel");
	}
}, "drawAxes");
function drawCurves(c, k, A, j, M, N, P) {
	let F = k.length, I = Math.min(P.width, P.height) / 2;
	A.forEach((k, A) => {
		if (k.entries.length !== F) return;
		let L = k.entries.map((c, k) => {
			let A = 2 * Math.PI * k / F - Math.PI / 2, N = relativeRadius(c, j, M, I);
			return {
				x: N * Math.cos(A),
				y: N * Math.sin(A)
			};
		});
		N === "circle" ? c.append("path").attr("d", closedRoundCurve(L, P.curveTension)).attr("class", `radarCurve-${A}`) : N === "polygon" && c.append("polygon").attr("points", L.map((c) => `${c.x},${c.y}`).join(" ")).attr("class", `radarCurve-${A}`);
	});
}
__name(drawCurves, "drawCurves");
function relativeRadius(c, k, A, j) {
	return j * (Math.min(Math.max(c, k), A) - k) / (A - k);
}
__name(relativeRadius, "relativeRadius");
function closedRoundCurve(c, k) {
	let A = c.length, j = `M${c[0].x},${c[0].y}`;
	for (let M = 0; M < A; M++) {
		let N = c[(M - 1 + A) % A], P = c[M], F = c[(M + 1) % A], I = c[(M + 2) % A], L = {
			x: P.x + (F.x - N.x) * k,
			y: P.y + (F.y - N.y) * k
		}, R = {
			x: F.x - (I.x - P.x) * k,
			y: F.y - (I.y - P.y) * k
		};
		j += ` C${L.x},${L.y} ${R.x},${R.y} ${F.x},${F.y}`;
	}
	return `${j} Z`;
}
__name(closedRoundCurve, "closedRoundCurve");
function drawLegend(c, k, A, j) {
	if (!A) return;
	let M = (j.width / 2 + j.marginRight) * 3 / 4, N = -(j.height / 2 + j.marginTop) * 3 / 4;
	k.forEach((k, A) => {
		let j = c.append("g").attr("transform", `translate(${M}, ${N + A * 20})`);
		j.append("rect").attr("width", 12).attr("height", 12).attr("class", `radarLegendBox-${A}`), j.append("text").attr("x", 16).attr("y", 0).attr("class", "radarLegendText").text(k.label);
	});
}
__name(drawLegend, "drawLegend");
var renderer = { draw }, genIndexStyles = /* @__PURE__ */ __name((c, k) => {
	let A = "";
	for (let j = 0; j < c.THEME_COLOR_LIMIT; j++) {
		let M = c[`cScale${j}`];
		A += `
		.radarCurve-${j} {
			color: ${M};
			fill: ${M};
			fill-opacity: ${k.curveOpacity};
			stroke: ${M};
			stroke-width: ${k.curveStrokeWidth};
		}
		.radarLegendBox-${j} {
			fill: ${M};
			fill-opacity: ${k.curveOpacity};
			stroke: ${M};
		}
		`;
	}
	return A;
}, "genIndexStyles"), buildRadarStyleOptions = /* @__PURE__ */ __name((c) => {
	let k = cleanAndMerge(getThemeVariables3(), getConfig().themeVariables);
	return {
		themeVariables: k,
		radarOptions: cleanAndMerge(k.radar, c)
	};
}, "buildRadarStyleOptions"), diagram = {
	parser,
	db,
	renderer,
	styles: /* @__PURE__ */ __name(({ radar: c } = {}) => {
		let { themeVariables: k, radarOptions: A } = buildRadarStyleOptions(c);
		return `
	.radarTitle {
		font-size: ${k.fontSize};
		color: ${k.titleColor};
		dominant-baseline: hanging;
		text-anchor: middle;
	}
	.radarAxisLine {
		stroke: ${A.axisColor};
		stroke-width: ${A.axisStrokeWidth};
	}
	.radarAxisLabel {
		dominant-baseline: middle;
		text-anchor: middle;
		font-size: ${A.axisLabelFontSize}px;
		color: ${A.axisColor};
	}
	.radarGraticule {
		fill: ${A.graticuleColor};
		fill-opacity: ${A.graticuleOpacity};
		stroke: ${A.graticuleColor};
		stroke-width: ${A.graticuleStrokeWidth};
	}
	.radarLegendText {
		text-anchor: start;
		font-size: ${A.legendFontSize}px;
		dominant-baseline: hanging;
	}
	${genIndexStyles(k, A)}
	`;
	}, "styles")
};
export { diagram };
