import { __name } from "./chunk-AGHRB4JF.js";
import { clear, common_default, defaultConfig2, getAccDescription, getAccTitle, getConfig2, getDiagramTitle, setAccDescription, setAccTitle, setDiagramTitle, setupGraphViewbox } from "./chunk-ABZYJK2D.js";
import select_default from "../../../../d3-selection/src/select.js";
import ordinal from "../../../../d3-scale/src/ordinal.js";
import Tableau10_default from "../../../../d3-scale-chromatic/src/categorical/Tableau10.js";
import "../../../../d3/src/index.js";
import { center, justify, left, right } from "../../../../d3-sankey/src/align.js";
import Sankey from "../../../../d3-sankey/src/sankey.js";
import sankeyLinkHorizontal_default from "../../../../d3-sankey/src/sankeyLinkHorizontal.js";
var parser = (function() {
	var i = /* @__PURE__ */ __name(function(e, i, a, o) {
		for (a ||= {}, o = e.length; o--; a[e[o]] = i);
		return a;
	}, "o"), a = [1, 9], o = [1, 10], s = [
		1,
		5,
		10,
		12
	], c = {
		trace: /* @__PURE__ */ __name(function() {}, "trace"),
		yy: {},
		symbols_: {
			error: 2,
			start: 3,
			SANKEY: 4,
			NEWLINE: 5,
			csv: 6,
			opt_eof: 7,
			record: 8,
			csv_tail: 9,
			EOF: 10,
			"field[source]": 11,
			COMMA: 12,
			"field[target]": 13,
			"field[value]": 14,
			field: 15,
			escaped: 16,
			non_escaped: 17,
			DQUOTE: 18,
			ESCAPED_TEXT: 19,
			NON_ESCAPED_TEXT: 20,
			$accept: 0,
			$end: 1
		},
		terminals_: {
			2: "error",
			4: "SANKEY",
			5: "NEWLINE",
			10: "EOF",
			11: "field[source]",
			12: "COMMA",
			13: "field[target]",
			14: "field[value]",
			18: "DQUOTE",
			19: "ESCAPED_TEXT",
			20: "NON_ESCAPED_TEXT"
		},
		productions_: [
			0,
			[3, 4],
			[6, 2],
			[9, 2],
			[9, 0],
			[7, 1],
			[7, 0],
			[8, 5],
			[15, 1],
			[15, 1],
			[16, 3],
			[17, 1]
		],
		performAction: /* @__PURE__ */ __name(function(e, i, a, o, s, c, l) {
			var u = c.length - 1;
			switch (s) {
				case 7:
					let e = o.findOrCreateNode(c[u - 4].trim().replaceAll("\"\"", "\"")), i = o.findOrCreateNode(c[u - 2].trim().replaceAll("\"\"", "\"")), a = parseFloat(c[u].trim());
					o.addLink(e, i, a);
					break;
				case 8:
				case 9:
				case 11:
					this.$ = c[u];
					break;
				case 10:
					this.$ = c[u - 1];
					break;
			}
		}, "anonymous"),
		table: [
			{
				3: 1,
				4: [1, 2]
			},
			{ 1: [3] },
			{ 5: [1, 3] },
			{
				6: 4,
				8: 5,
				15: 6,
				16: 7,
				17: 8,
				18: a,
				20: o
			},
			{
				1: [2, 6],
				7: 11,
				10: [1, 12]
			},
			i(o, [2, 4], {
				9: 13,
				5: [1, 14]
			}),
			{ 12: [1, 15] },
			i(s, [2, 8]),
			i(s, [2, 9]),
			{ 19: [1, 16] },
			i(s, [2, 11]),
			{ 1: [2, 1] },
			{ 1: [2, 5] },
			i(o, [2, 2]),
			{
				6: 17,
				8: 5,
				15: 6,
				16: 7,
				17: 8,
				18: a,
				20: o
			},
			{
				15: 18,
				16: 7,
				17: 8,
				18: a,
				20: o
			},
			{ 18: [1, 19] },
			i(o, [2, 3]),
			{ 12: [1, 20] },
			i(s, [2, 10]),
			{
				15: 21,
				16: 7,
				17: 8,
				18: a,
				20: o
			},
			i([
				1,
				5,
				10
			], [2, 7])
		],
		defaultActions: {
			11: [2, 1],
			12: [2, 5]
		},
		parseError: /* @__PURE__ */ __name(function(e, i) {
			if (i.recoverable) this.trace(e);
			else {
				var a = Error(e);
				throw a.hash = i, a;
			}
		}, "parseError"),
		parse: /* @__PURE__ */ __name(function(i) {
			var a = this, o = [0], s = [], c = [null], l = [], u = this.table, d = "", f = 0, p = 0, m = 0, h = 2, g = 1, _ = l.slice.call(arguments, 1), v = Object.create(this.lexer), y = { yy: {} };
			for (var b in this.yy) Object.prototype.hasOwnProperty.call(this.yy, b) && (y.yy[b] = this.yy[b]);
			v.setInput(i, y.yy), y.yy.lexer = v, y.yy.parser = this, v.yylloc === void 0 && (v.yylloc = {});
			var x = v.yylloc;
			l.push(x);
			var S = v.options && v.options.ranges;
			typeof y.yy.parseError == "function" ? this.parseError = y.yy.parseError : this.parseError = Object.getPrototypeOf(this).parseError;
			function C(e) {
				o.length -= 2 * e, c.length -= e, l.length -= e;
			}
			__name(C, "popStack");
			function w() {
				var e = s.pop() || v.lex() || g;
				return typeof e != "number" && (e instanceof Array && (s = e, e = s.pop()), e = a.symbols_[e] || e), e;
			}
			__name(w, "lex");
			for (var T, E, D, O, k, A = {}, j, M, N, P;;) {
				if (D = o[o.length - 1], this.defaultActions[D] ? O = this.defaultActions[D] : (T ??= w(), O = u[D] && u[D][T]), O === void 0 || !O.length || !O[0]) {
					var F = "";
					for (j in P = [], u[D]) this.terminals_[j] && j > h && P.push("'" + this.terminals_[j] + "'");
					F = v.showPosition ? "Parse error on line " + (f + 1) + ":\n" + v.showPosition() + "\nExpecting " + P.join(", ") + ", got '" + (this.terminals_[T] || T) + "'" : "Parse error on line " + (f + 1) + ": Unexpected " + (T == g ? "end of input" : "'" + (this.terminals_[T] || T) + "'"), this.parseError(F, {
						text: v.match,
						token: this.terminals_[T] || T,
						line: v.yylineno,
						loc: x,
						expected: P
					});
				}
				if (O[0] instanceof Array && O.length > 1) throw Error("Parse Error: multiple actions possible at state: " + D + ", token: " + T);
				switch (O[0]) {
					case 1:
						o.push(T), c.push(v.yytext), l.push(v.yylloc), o.push(O[1]), T = null, E ? (T = E, E = null) : (p = v.yyleng, d = v.yytext, f = v.yylineno, x = v.yylloc, m > 0 && m--);
						break;
					case 2:
						if (M = this.productions_[O[1]][1], A.$ = c[c.length - M], A._$ = {
							first_line: l[l.length - (M || 1)].first_line,
							last_line: l[l.length - 1].last_line,
							first_column: l[l.length - (M || 1)].first_column,
							last_column: l[l.length - 1].last_column
						}, S && (A._$.range = [l[l.length - (M || 1)].range[0], l[l.length - 1].range[1]]), k = this.performAction.apply(A, [
							d,
							p,
							f,
							y.yy,
							O[1],
							c,
							l
						].concat(_)), k !== void 0) return k;
						M && (o = o.slice(0, -1 * M * 2), c = c.slice(0, -1 * M), l = l.slice(0, -1 * M)), o.push(this.productions_[O[1]][0]), c.push(A.$), l.push(A._$), N = u[o[o.length - 2]][o[o.length - 1]], o.push(N);
						break;
					case 3: return !0;
				}
			}
			return !0;
		}, "parse")
	};
	c.lexer = /* @__PURE__ */ (function() {
		return {
			EOF: 1,
			parseError: /* @__PURE__ */ __name(function(e, i) {
				if (this.yy.parser) this.yy.parser.parseError(e, i);
				else throw Error(e);
			}, "parseError"),
			setInput: /* @__PURE__ */ __name(function(e, i) {
				return this.yy = i || this.yy || {}, this._input = e, this._more = this._backtrack = this.done = !1, this.yylineno = this.yyleng = 0, this.yytext = this.matched = this.match = "", this.conditionStack = ["INITIAL"], this.yylloc = {
					first_line: 1,
					first_column: 0,
					last_line: 1,
					last_column: 0
				}, this.options.ranges && (this.yylloc.range = [0, 0]), this.offset = 0, this;
			}, "setInput"),
			input: /* @__PURE__ */ __name(function() {
				var e = this._input[0];
				return this.yytext += e, this.yyleng++, this.offset++, this.match += e, this.matched += e, e.match(/(?:\r\n?|\n).*/g) ? (this.yylineno++, this.yylloc.last_line++) : this.yylloc.last_column++, this.options.ranges && this.yylloc.range[1]++, this._input = this._input.slice(1), e;
			}, "input"),
			unput: /* @__PURE__ */ __name(function(e) {
				var i = e.length, a = e.split(/(?:\r\n?|\n)/g);
				this._input = e + this._input, this.yytext = this.yytext.substr(0, this.yytext.length - i), this.offset -= i;
				var o = this.match.split(/(?:\r\n?|\n)/g);
				this.match = this.match.substr(0, this.match.length - 1), this.matched = this.matched.substr(0, this.matched.length - 1), a.length - 1 && (this.yylineno -= a.length - 1);
				var s = this.yylloc.range;
				return this.yylloc = {
					first_line: this.yylloc.first_line,
					last_line: this.yylineno + 1,
					first_column: this.yylloc.first_column,
					last_column: a ? (a.length === o.length ? this.yylloc.first_column : 0) + o[o.length - a.length].length - a[0].length : this.yylloc.first_column - i
				}, this.options.ranges && (this.yylloc.range = [s[0], s[0] + this.yyleng - i]), this.yyleng = this.yytext.length, this;
			}, "unput"),
			more: /* @__PURE__ */ __name(function() {
				return this._more = !0, this;
			}, "more"),
			reject: /* @__PURE__ */ __name(function() {
				if (this.options.backtrack_lexer) this._backtrack = !0;
				else return this.parseError("Lexical error on line " + (this.yylineno + 1) + ". You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).\n" + this.showPosition(), {
					text: "",
					token: null,
					line: this.yylineno
				});
				return this;
			}, "reject"),
			less: /* @__PURE__ */ __name(function(e) {
				this.unput(this.match.slice(e));
			}, "less"),
			pastInput: /* @__PURE__ */ __name(function() {
				var e = this.matched.substr(0, this.matched.length - this.match.length);
				return (e.length > 20 ? "..." : "") + e.substr(-20).replace(/\n/g, "");
			}, "pastInput"),
			upcomingInput: /* @__PURE__ */ __name(function() {
				var e = this.match;
				return e.length < 20 && (e += this._input.substr(0, 20 - e.length)), (e.substr(0, 20) + (e.length > 20 ? "..." : "")).replace(/\n/g, "");
			}, "upcomingInput"),
			showPosition: /* @__PURE__ */ __name(function() {
				var e = this.pastInput(), i = Array(e.length + 1).join("-");
				return e + this.upcomingInput() + "\n" + i + "^";
			}, "showPosition"),
			test_match: /* @__PURE__ */ __name(function(e, i) {
				var a, o, s;
				if (this.options.backtrack_lexer && (s = {
					yylineno: this.yylineno,
					yylloc: {
						first_line: this.yylloc.first_line,
						last_line: this.last_line,
						first_column: this.yylloc.first_column,
						last_column: this.yylloc.last_column
					},
					yytext: this.yytext,
					match: this.match,
					matches: this.matches,
					matched: this.matched,
					yyleng: this.yyleng,
					offset: this.offset,
					_more: this._more,
					_input: this._input,
					yy: this.yy,
					conditionStack: this.conditionStack.slice(0),
					done: this.done
				}, this.options.ranges && (s.yylloc.range = this.yylloc.range.slice(0))), o = e[0].match(/(?:\r\n?|\n).*/g), o && (this.yylineno += o.length), this.yylloc = {
					first_line: this.yylloc.last_line,
					last_line: this.yylineno + 1,
					first_column: this.yylloc.last_column,
					last_column: o ? o[o.length - 1].length - o[o.length - 1].match(/\r?\n?/)[0].length : this.yylloc.last_column + e[0].length
				}, this.yytext += e[0], this.match += e[0], this.matches = e, this.yyleng = this.yytext.length, this.options.ranges && (this.yylloc.range = [this.offset, this.offset += this.yyleng]), this._more = !1, this._backtrack = !1, this._input = this._input.slice(e[0].length), this.matched += e[0], a = this.performAction.call(this, this.yy, this, i, this.conditionStack[this.conditionStack.length - 1]), this.done && this._input && (this.done = !1), a) return a;
				if (this._backtrack) {
					for (var c in s) this[c] = s[c];
					return !1;
				}
				return !1;
			}, "test_match"),
			next: /* @__PURE__ */ __name(function() {
				if (this.done) return this.EOF;
				this._input || (this.done = !0);
				var e, i, a, o;
				this._more || (this.yytext = "", this.match = "");
				for (var s = this._currentRules(), c = 0; c < s.length; c++) if (a = this._input.match(this.rules[s[c]]), a && (!i || a[0].length > i[0].length)) {
					if (i = a, o = c, this.options.backtrack_lexer) {
						if (e = this.test_match(a, s[c]), e !== !1) return e;
						if (this._backtrack) {
							i = !1;
							continue;
						} else return !1;
					} else if (!this.options.flex) break;
				}
				return i ? (e = this.test_match(i, s[o]), e === !1 ? !1 : e) : this._input === "" ? this.EOF : this.parseError("Lexical error on line " + (this.yylineno + 1) + ". Unrecognized text.\n" + this.showPosition(), {
					text: "",
					token: null,
					line: this.yylineno
				});
			}, "next"),
			lex: /* @__PURE__ */ __name(function() {
				return this.next() || this.lex();
			}, "lex"),
			begin: /* @__PURE__ */ __name(function(e) {
				this.conditionStack.push(e);
			}, "begin"),
			popState: /* @__PURE__ */ __name(function() {
				return this.conditionStack.length - 1 > 0 ? this.conditionStack.pop() : this.conditionStack[0];
			}, "popState"),
			_currentRules: /* @__PURE__ */ __name(function() {
				return this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1] ? this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules : this.conditions.INITIAL.rules;
			}, "_currentRules"),
			topState: /* @__PURE__ */ __name(function(e) {
				return e = this.conditionStack.length - 1 - Math.abs(e || 0), e >= 0 ? this.conditionStack[e] : "INITIAL";
			}, "topState"),
			pushState: /* @__PURE__ */ __name(function(e) {
				this.begin(e);
			}, "pushState"),
			stateStackSize: /* @__PURE__ */ __name(function() {
				return this.conditionStack.length;
			}, "stateStackSize"),
			options: { "case-insensitive": !0 },
			performAction: /* @__PURE__ */ __name(function(e, i, a, o) {
				switch (a) {
					case 0: return this.pushState("csv"), 4;
					case 1: return this.pushState("csv"), 4;
					case 2: return 10;
					case 3: return 5;
					case 4: return 12;
					case 5: return this.pushState("escaped_text"), 18;
					case 6: return 20;
					case 7: return this.popState("escaped_text"), 18;
					case 8: return 19;
				}
			}, "anonymous"),
			rules: [
				/^(?:sankey-beta\b)/i,
				/^(?:sankey\b)/i,
				/^(?:$)/i,
				/^(?:((\u000D\u000A)|(\u000A)))/i,
				/^(?:(\u002C))/i,
				/^(?:(\u0022))/i,
				/^(?:([\u0020-\u0021\u0023-\u002B\u002D-\u007E])*)/i,
				/^(?:(\u0022)(?!(\u0022)))/i,
				/^(?:(([\u0020-\u0021\u0023-\u002B\u002D-\u007E])|(\u002C)|(\u000D)|(\u000A)|(\u0022)(\u0022))*)/i
			],
			conditions: {
				csv: {
					rules: [
						2,
						3,
						4,
						5,
						6,
						7,
						8
					],
					inclusive: !1
				},
				escaped_text: {
					rules: [7, 8],
					inclusive: !1
				},
				INITIAL: {
					rules: [
						0,
						1,
						2,
						3,
						4,
						5,
						6,
						7,
						8
					],
					inclusive: !0
				}
			}
		};
	})();
	function l() {
		this.yy = {};
	}
	return __name(l, "Parser"), l.prototype = c, c.Parser = l, new l();
})();
parser.parser = parser;
var sankey_default = parser, links = [], nodes = [], nodesMap = /* @__PURE__ */ new Map(), clear2 = /* @__PURE__ */ __name(() => {
	links = [], nodes = [], nodesMap = /* @__PURE__ */ new Map(), clear();
}, "clear"), SankeyLink = class {
	constructor(e, i, a = 0) {
		this.source = e, this.target = i, this.value = a;
	}
	static #e = __name(this, "SankeyLink");
}, addLink = /* @__PURE__ */ __name((e, i, a) => {
	links.push(new SankeyLink(e, i, a));
}, "addLink"), SankeyNode = class {
	constructor(e) {
		this.ID = e;
	}
	static #e = __name(this, "SankeyNode");
}, sankeyDB_default = {
	nodesMap,
	getConfig: /* @__PURE__ */ __name(() => getConfig2().sankey, "getConfig"),
	getNodes: /* @__PURE__ */ __name(() => nodes, "getNodes"),
	getLinks: /* @__PURE__ */ __name(() => links, "getLinks"),
	getGraph: /* @__PURE__ */ __name(() => ({
		nodes: nodes.map((e) => ({ id: e.ID })),
		links: links.map((e) => ({
			source: e.source.ID,
			target: e.target.ID,
			value: e.value
		}))
	}), "getGraph"),
	addLink,
	findOrCreateNode: /* @__PURE__ */ __name((e) => {
		e = common_default.sanitizeText(e, getConfig2());
		let i = nodesMap.get(e);
		return i === void 0 && (i = new SankeyNode(e), nodesMap.set(e, i), nodes.push(i)), i;
	}, "findOrCreateNode"),
	getAccTitle,
	setAccTitle,
	getAccDescription,
	setAccDescription,
	getDiagramTitle,
	setDiagramTitle,
	clear: clear2
}, Uid = class i {
	static #e = __name(this, "Uid");
	static #t = this.count = 0;
	static next(e) {
		return new i(e + ++i.count);
	}
	constructor(e) {
		this.id = e, this.href = `#${e}`;
	}
	toString() {
		return "url(" + this.href + ")";
	}
}, alignmentsMap = {
	left,
	right,
	center,
	justify
}, sankeyRenderer_default = { draw: /* @__PURE__ */ __name(function(i, a, s, c) {
	let { securityLevel: u, sankey: d } = getConfig2(), f = defaultConfig2.sankey, p;
	u === "sandbox" && (p = select_default("#i" + a));
	let v = select_default(u === "sandbox" ? p.nodes()[0].contentDocument.body : "body"), y = u === "sandbox" ? v.select(`[id="${a}"]`) : select_default(`[id="${a}"]`), b = d?.width ?? f.width, x = d?.height ?? f.width, w = d?.useMaxWidth ?? f.useMaxWidth, T = d?.nodeAlignment ?? f.nodeAlignment, E = d?.prefix ?? f.prefix, D = d?.suffix ?? f.suffix, O = d?.showValues ?? f.showValues, k = c.db.getGraph(), A = alignmentsMap[T];
	Sankey().nodeId((e) => e.id).nodeWidth(10).nodePadding(10 + (O ? 15 : 0)).nodeAlign(A).extent([[0, 0], [b, x]])(k);
	let j = ordinal(Tableau10_default);
	y.append("g").attr("class", "nodes").selectAll(".node").data(k.nodes).join("g").attr("class", "node").attr("id", (e) => (e.uid = Uid.next("node-")).id).attr("transform", function(e) {
		return "translate(" + e.x0 + "," + e.y0 + ")";
	}).attr("x", (e) => e.x0).attr("y", (e) => e.y0).append("rect").attr("height", (e) => e.y1 - e.y0).attr("width", (e) => e.x1 - e.x0).attr("fill", (e) => j(e.id));
	let M = /* @__PURE__ */ __name(({ id: e, value: i }) => O ? `${e}
${E}${Math.round(i * 100) / 100}${D}` : e, "getText");
	y.append("g").attr("class", "node-labels").attr("font-size", 14).selectAll("text").data(k.nodes).join("text").attr("x", (e) => e.x0 < b / 2 ? e.x1 + 6 : e.x0 - 6).attr("y", (e) => (e.y1 + e.y0) / 2).attr("dy", `${O ? "0" : "0.35"}em`).attr("text-anchor", (e) => e.x0 < b / 2 ? "start" : "end").text(M);
	let N = y.append("g").attr("class", "links").attr("fill", "none").attr("stroke-opacity", .5).selectAll(".link").data(k.links).join("g").attr("class", "link").style("mix-blend-mode", "multiply"), I = d?.linkColor ?? "gradient";
	if (I === "gradient") {
		let e = N.append("linearGradient").attr("id", (e) => (e.uid = Uid.next("linearGradient-")).id).attr("gradientUnits", "userSpaceOnUse").attr("x1", (e) => e.source.x1).attr("x2", (e) => e.target.x0);
		e.append("stop").attr("offset", "0%").attr("stop-color", (e) => j(e.source.id)), e.append("stop").attr("offset", "100%").attr("stop-color", (e) => j(e.target.id));
	}
	let L;
	switch (I) {
		case "gradient":
			L = /* @__PURE__ */ __name((e) => e.uid, "coloring");
			break;
		case "source":
			L = /* @__PURE__ */ __name((e) => j(e.source.id), "coloring");
			break;
		case "target":
			L = /* @__PURE__ */ __name((e) => j(e.target.id), "coloring");
			break;
		default: L = I;
	}
	N.append("path").attr("d", sankeyLinkHorizontal_default()).attr("stroke", L).attr("stroke-width", (e) => Math.max(1, e.width)), setupGraphViewbox(void 0, y, 0, w);
}, "draw") }, prepareTextForParsing = /* @__PURE__ */ __name((e) => e.replaceAll(/^[^\S\n\r]+|[^\S\n\r]+$/g, "").replaceAll(/([\n\r])+/g, "\n").trim(), "prepareTextForParsing"), styles_default = /* @__PURE__ */ __name((e) => `.label {
      font-family: ${e.fontFamily};
    }`, "getStyles"), originalParse = sankey_default.parse.bind(sankey_default);
sankey_default.parse = (e) => originalParse(prepareTextForParsing(e));
var diagram = {
	styles: styles_default,
	parser: sankey_default,
	db: sankeyDB_default,
	renderer: sankeyRenderer_default
};
export { diagram };
