d5-mermaid 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,4081 @@
1
+ 'use strict';
2
+
3
+ // src/d5-domain/detector.ts
4
+ function detectDomain(text) {
5
+ return text.trimStart().startsWith("d5-domain");
6
+ }
7
+
8
+ // src/shared/direction.ts
9
+ function normalizeDirection(value) {
10
+ const d = value.trim().toUpperCase();
11
+ const normalized = d === "TD" ? "TB" : d;
12
+ return normalized === "LR" || normalized === "RL" || normalized === "TB" || normalized === "BT" ? normalized : null;
13
+ }
14
+ var DIRECTION_RE = /^\s*direction\s+(LR|RL|TB|TD|BT)\s*$/i;
15
+
16
+ // src/d5-domain/db.ts
17
+ var DEFAULT_DIRECTION = "TB";
18
+ var D5DomainDb = class {
19
+ domain;
20
+ subdomains = [];
21
+ relationships = [];
22
+ direction = DEFAULT_DIRECTION;
23
+ title;
24
+ accTitle;
25
+ accDescription;
26
+ getDomain() {
27
+ return this.domain;
28
+ }
29
+ setDomain(id, label) {
30
+ this.domain = { id, label };
31
+ }
32
+ getSubdomains() {
33
+ return this.subdomains;
34
+ }
35
+ addSubdomain(id, label, type) {
36
+ this.subdomains.push({ id, label, type });
37
+ }
38
+ getRelationships() {
39
+ return this.relationships;
40
+ }
41
+ addRelationship(source, target, label) {
42
+ this.relationships.push({ source, target, label });
43
+ }
44
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
45
+ setDirection(dir) {
46
+ const normalized = normalizeDirection(dir);
47
+ if (normalized) this.direction = normalized;
48
+ }
49
+ getDirection() {
50
+ return this.direction;
51
+ }
52
+ clear() {
53
+ this.domain = void 0;
54
+ this.subdomains = [];
55
+ this.relationships = [];
56
+ this.direction = DEFAULT_DIRECTION;
57
+ this.title = void 0;
58
+ this.accTitle = void 0;
59
+ this.accDescription = void 0;
60
+ }
61
+ setTitle(title) {
62
+ this.title = title;
63
+ }
64
+ getTitle() {
65
+ return this.title;
66
+ }
67
+ // Mermaid DiagramDB interface methods
68
+ setDiagramTitle(title) {
69
+ this.setTitle(title);
70
+ }
71
+ getDiagramTitle() {
72
+ return this.title ?? "";
73
+ }
74
+ setAccTitle(title) {
75
+ this.accTitle = title;
76
+ }
77
+ getAccTitle() {
78
+ return this.accTitle ?? "";
79
+ }
80
+ setAccDescription(desc) {
81
+ this.accDescription = desc;
82
+ }
83
+ getAccDescription() {
84
+ return this.accDescription ?? "";
85
+ }
86
+ };
87
+
88
+ // src/d5-domain/parser.ts
89
+ var TITLE_RE = /^\s*title\s+(.+)$/;
90
+ var DOMAIN_RE = /^\s*Domain\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)\s*\{/;
91
+ var SUBDOMAIN_RE = /^\s*Subdomain\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*(\w+)\s*\)/;
92
+ var REL_RE = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*(?:,\s*"([^"]+)")?\s*\)/;
93
+ var COMMENT_LINE_RE = /^\s*%%/;
94
+ function stripInlineComment(line) {
95
+ const idx = line.indexOf("%%");
96
+ return idx === -1 ? line : line.slice(0, idx);
97
+ }
98
+ function parse(text, db) {
99
+ const lines = text.split("\n");
100
+ for (const raw of lines) {
101
+ if (COMMENT_LINE_RE.test(raw)) continue;
102
+ const line = stripInlineComment(raw).trim();
103
+ if (line === "" || line === "d5-domain") continue;
104
+ let m;
105
+ if (m = line.match(DIRECTION_RE)) {
106
+ db.setDirection(m[1]);
107
+ } else if (m = line.match(TITLE_RE)) {
108
+ db.setTitle(m[1].trim());
109
+ } else if (m = line.match(DOMAIN_RE)) {
110
+ db.setDomain(m[1], m[2]);
111
+ } else if (m = line.match(SUBDOMAIN_RE)) {
112
+ db.addSubdomain(m[1], m[2], m[3]);
113
+ } else if (m = line.match(REL_RE)) {
114
+ db.addRelationship(m[1], m[2], m[3] ?? "");
115
+ }
116
+ }
117
+ }
118
+
119
+ // node_modules/@dagrejs/dagre/dist/dagre.esm.js
120
+ var ge = Object.defineProperty;
121
+ var hn = (e, n, t) => n in e ? ge(e, n, { enumerable: true, configurable: true, writable: true, value: t }) : e[n] = t;
122
+ var fn = (e, n) => {
123
+ for (var t in n) ge(e, t, { get: n[t], enumerable: true });
124
+ };
125
+ var pe = (e, n, t) => hn(e, n + "" , t);
126
+ var z = {};
127
+ fn(z, { Graph: () => p, alg: () => R, json: () => ye, version: () => pn });
128
+ var bn = Object.defineProperty;
129
+ var Le = (e, n) => {
130
+ for (var t in n) bn(e, t, { get: n[t], enumerable: true });
131
+ };
132
+ var p = class {
133
+ constructor(e) {
134
+ this._isDirected = true, this._isMultigraph = false, this._isCompound = false, this._nodes = {}, this._in = {}, this._preds = {}, this._out = {}, this._sucs = {}, this._edgeObjs = {}, this._edgeLabels = {}, this._nodeCount = 0, this._edgeCount = 0, this._defaultNodeLabelFn = () => {
135
+ }, this._defaultEdgeLabelFn = () => {
136
+ }, e && (this._isDirected = "directed" in e ? e.directed : true, this._isMultigraph = "multigraph" in e ? e.multigraph : false, this._isCompound = "compound" in e ? e.compound : false), this._isCompound && (this._parent = {}, this._children = {}, this._children["\0"] = {});
137
+ }
138
+ isDirected() {
139
+ return this._isDirected;
140
+ }
141
+ isMultigraph() {
142
+ return this._isMultigraph;
143
+ }
144
+ isCompound() {
145
+ return this._isCompound;
146
+ }
147
+ setGraph(e) {
148
+ return this._label = e, this;
149
+ }
150
+ graph() {
151
+ return this._label;
152
+ }
153
+ setDefaultNodeLabel(e) {
154
+ return typeof e != "function" ? this._defaultNodeLabelFn = () => e : this._defaultNodeLabelFn = e, this;
155
+ }
156
+ nodeCount() {
157
+ return this._nodeCount;
158
+ }
159
+ nodes() {
160
+ return Object.keys(this._nodes);
161
+ }
162
+ sources() {
163
+ return this.nodes().filter((e) => Object.keys(this._in[e]).length === 0);
164
+ }
165
+ sinks() {
166
+ return this.nodes().filter((e) => Object.keys(this._out[e]).length === 0);
167
+ }
168
+ setNodes(e, n) {
169
+ return e.forEach((t) => {
170
+ n !== void 0 ? this.setNode(t, n) : this.setNode(t);
171
+ }), this;
172
+ }
173
+ setNode(e, n) {
174
+ return e in this._nodes ? (arguments.length > 1 && (this._nodes[e] = n), this) : (this._nodes[e] = arguments.length > 1 ? n : this._defaultNodeLabelFn(e), this._isCompound && (this._parent[e] = "\0", this._children[e] = {}, this._children["\0"][e] = true), this._in[e] = {}, this._preds[e] = {}, this._out[e] = {}, this._sucs[e] = {}, ++this._nodeCount, this);
175
+ }
176
+ node(e) {
177
+ return this._nodes[e];
178
+ }
179
+ hasNode(e) {
180
+ return e in this._nodes;
181
+ }
182
+ removeNode(e) {
183
+ if (e in this._nodes) {
184
+ let n = (t) => this.removeEdge(this._edgeObjs[t]);
185
+ delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], this.children(e).forEach((t) => {
186
+ this.setParent(t);
187
+ }), delete this._children[e]), Object.keys(this._in[e]).forEach(n), delete this._in[e], delete this._preds[e], Object.keys(this._out[e]).forEach(n), delete this._out[e], delete this._sucs[e], --this._nodeCount;
188
+ }
189
+ return this;
190
+ }
191
+ setParent(e, n) {
192
+ if (!this._isCompound) throw new Error("Cannot set parent in a non-compound graph");
193
+ if (n === void 0) n = "\0";
194
+ else {
195
+ n += "";
196
+ for (let t = n; t !== void 0; t = this.parent(t)) if (t === e) throw new Error("Setting " + n + " as parent of " + e + " would create a cycle");
197
+ this.setNode(n);
198
+ }
199
+ return this.setNode(e), this._removeFromParentsChildList(e), this._parent[e] = n, this._children[n][e] = true, this;
200
+ }
201
+ parent(e) {
202
+ if (this._isCompound) {
203
+ let n = this._parent[e];
204
+ if (n !== "\0") return n;
205
+ }
206
+ }
207
+ children(e = "\0") {
208
+ if (this._isCompound) {
209
+ let n = this._children[e];
210
+ if (n) return Object.keys(n);
211
+ } else {
212
+ if (e === "\0") return this.nodes();
213
+ if (this.hasNode(e)) return [];
214
+ }
215
+ return [];
216
+ }
217
+ predecessors(e) {
218
+ let n = this._preds[e];
219
+ if (n) return Object.keys(n);
220
+ }
221
+ successors(e) {
222
+ let n = this._sucs[e];
223
+ if (n) return Object.keys(n);
224
+ }
225
+ neighbors(e) {
226
+ let n = this.predecessors(e);
227
+ if (n) {
228
+ let t = new Set(n);
229
+ for (let r of this.successors(e)) t.add(r);
230
+ return Array.from(t.values());
231
+ }
232
+ }
233
+ isLeaf(e) {
234
+ let n;
235
+ return this.isDirected() ? n = this.successors(e) : n = this.neighbors(e), n.length === 0;
236
+ }
237
+ filterNodes(e) {
238
+ let n = new this.constructor({ directed: this._isDirected, multigraph: this._isMultigraph, compound: this._isCompound });
239
+ n.setGraph(this.graph()), Object.entries(this._nodes).forEach(([o, i]) => {
240
+ e(o) && n.setNode(o, i);
241
+ }), Object.values(this._edgeObjs).forEach((o) => {
242
+ n.hasNode(o.v) && n.hasNode(o.w) && n.setEdge(o, this.edge(o));
243
+ });
244
+ let t = {}, r = (o) => {
245
+ let i = this.parent(o);
246
+ return !i || n.hasNode(i) ? (t[o] = i != null ? i : void 0, i != null ? i : void 0) : i in t ? t[i] : r(i);
247
+ };
248
+ return this._isCompound && n.nodes().forEach((o) => n.setParent(o, r(o))), n;
249
+ }
250
+ setDefaultEdgeLabel(e) {
251
+ return typeof e != "function" ? this._defaultEdgeLabelFn = () => e : this._defaultEdgeLabelFn = e, this;
252
+ }
253
+ edgeCount() {
254
+ return this._edgeCount;
255
+ }
256
+ edges() {
257
+ return Object.values(this._edgeObjs);
258
+ }
259
+ setPath(e, n) {
260
+ return e.reduce((t, r) => (n !== void 0 ? this.setEdge(t, r, n) : this.setEdge(t, r), r)), this;
261
+ }
262
+ setEdge(e, n, t, r) {
263
+ let o, i, s, a, d = false;
264
+ typeof e == "object" && e !== null && "v" in e ? (o = e.v, i = e.w, s = e.name, arguments.length === 2 && (a = n, d = true)) : (o = e, i = n, s = r, arguments.length > 2 && (a = t, d = true)), o = "" + o, i = "" + i, s !== void 0 && (s = "" + s);
265
+ let l = C(this._isDirected, o, i, s);
266
+ if (l in this._edgeLabels) return d && (this._edgeLabels[l] = a), this;
267
+ if (s !== void 0 && !this._isMultigraph) throw new Error("Cannot set a named edge when isMultigraph = false");
268
+ this.setNode(o), this.setNode(i), this._edgeLabels[l] = d ? a : this._defaultEdgeLabelFn(o, i, s);
269
+ let u = gn(this._isDirected, o, i, s);
270
+ return o = u.v, i = u.w, Object.freeze(u), this._edgeObjs[l] = u, me(this._preds[i], o), me(this._sucs[o], i), this._in[i][l] = u, this._out[o][l] = u, this._edgeCount++, this;
271
+ }
272
+ edge(e, n, t) {
273
+ let r = arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t);
274
+ return this._edgeLabels[r];
275
+ }
276
+ edgeAsObj(e, n, t) {
277
+ let r = arguments.length === 1 ? this.edge(e) : this.edge(e, n, t);
278
+ return typeof r != "object" ? { label: r } : r;
279
+ }
280
+ hasEdge(e, n, t) {
281
+ return (arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t)) in this._edgeLabels;
282
+ }
283
+ removeEdge(e, n, t) {
284
+ let r = arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t), o = this._edgeObjs[r];
285
+ if (o) {
286
+ let i = o.v, s = o.w;
287
+ delete this._edgeLabels[r], delete this._edgeObjs[r], Ee(this._preds[s], i), Ee(this._sucs[i], s), delete this._in[s][r], delete this._out[i][r], this._edgeCount--;
288
+ }
289
+ return this;
290
+ }
291
+ inEdges(e, n) {
292
+ return this.isDirected() ? this.filterEdges(this._in[e], e, n) : this.nodeEdges(e, n);
293
+ }
294
+ outEdges(e, n) {
295
+ return this.isDirected() ? this.filterEdges(this._out[e], e, n) : this.nodeEdges(e, n);
296
+ }
297
+ nodeEdges(e, n) {
298
+ if (e in this._nodes) return this.filterEdges({ ...this._in[e], ...this._out[e] }, e, n);
299
+ }
300
+ _removeFromParentsChildList(e) {
301
+ delete this._children[this._parent[e]][e];
302
+ }
303
+ filterEdges(e, n, t) {
304
+ if (!e) return;
305
+ let r = Object.values(e);
306
+ return t ? r.filter((o) => o.v === n && o.w === t || o.v === t && o.w === n) : r;
307
+ }
308
+ };
309
+ function me(e, n) {
310
+ e[n] ? e[n]++ : e[n] = 1;
311
+ }
312
+ function Ee(e, n) {
313
+ e[n] !== void 0 && !--e[n] && delete e[n];
314
+ }
315
+ function C(e, n, t, r) {
316
+ let o = "" + n, i = "" + t;
317
+ if (!e && o > i) {
318
+ let s = o;
319
+ o = i, i = s;
320
+ }
321
+ return o + "" + i + "" + (r === void 0 ? "\0" : r);
322
+ }
323
+ function gn(e, n, t, r) {
324
+ let o = "" + n, i = "" + t;
325
+ if (!e && o > i) {
326
+ let a = o;
327
+ o = i, i = a;
328
+ }
329
+ let s = { v: o, w: i };
330
+ return r && (s.name = r), s;
331
+ }
332
+ function Y(e, n) {
333
+ return C(e, n.v, n.w, n.name);
334
+ }
335
+ var pn = "4.0.1";
336
+ var ye = {};
337
+ Le(ye, { read: () => yn, write: () => mn });
338
+ function mn(e) {
339
+ let n = { options: { directed: e.isDirected(), multigraph: e.isMultigraph(), compound: e.isCompound() }, nodes: En(e), edges: Ln(e) }, t = e.graph();
340
+ return t !== void 0 && (n.value = structuredClone(t)), n;
341
+ }
342
+ function En(e) {
343
+ return e.nodes().map((n) => {
344
+ let t = e.node(n), r = e.parent(n), o = { v: n };
345
+ return t !== void 0 && (o.value = t), r !== void 0 && (o.parent = r), o;
346
+ });
347
+ }
348
+ function Ln(e) {
349
+ return e.edges().map((n) => {
350
+ let t = e.edge(n), r = { v: n.v, w: n.w };
351
+ return n.name !== void 0 && (r.name = n.name), t !== void 0 && (r.value = t), r;
352
+ });
353
+ }
354
+ function yn(e) {
355
+ let n = new p(e.options);
356
+ return e.value !== void 0 && n.setGraph(e.value), e.nodes.forEach((t) => {
357
+ n.setNode(t.v, t.value), t.parent && n.setParent(t.v, t.parent);
358
+ }), e.edges.forEach((t) => {
359
+ n.setEdge({ v: t.v, w: t.w, name: t.name }, t.value);
360
+ }), n;
361
+ }
362
+ var R = {};
363
+ Le(R, { CycleException: () => D, bellmanFord: () => we, components: () => Gn, dijkstra: () => F, dijkstraAll: () => _n, findCycles: () => xn, floydWarshall: () => On, isAcyclic: () => Cn, postorder: () => Pn, preorder: () => Mn, prim: () => jn, shortestPaths: () => Sn, tarjan: () => Ge, topsort: () => ke });
364
+ var wn = () => 1;
365
+ function we(e, n, t, r) {
366
+ return Nn(e, String(n), t || wn, r || function(o) {
367
+ return e.outEdges(o);
368
+ });
369
+ }
370
+ function Nn(e, n, t, r) {
371
+ let o = {}, i, s = 0, a = e.nodes(), d = function(c) {
372
+ let h = t(c);
373
+ o[c.v].distance + h < o[c.w].distance && (o[c.w] = { distance: o[c.v].distance + h, predecessor: c.v }, i = true);
374
+ }, l = function() {
375
+ a.forEach(function(c) {
376
+ r(c).forEach(function(h) {
377
+ let f = h.v === c ? h.v : h.w, g = f === h.v ? h.w : h.v;
378
+ d({ v: f, w: g });
379
+ });
380
+ });
381
+ };
382
+ a.forEach(function(c) {
383
+ let h = c === n ? 0 : Number.POSITIVE_INFINITY;
384
+ o[c] = { distance: h, predecessor: "" };
385
+ });
386
+ let u = a.length;
387
+ for (let c = 1; c < u && (i = false, s++, l(), !!i); c++) ;
388
+ if (s === u - 1 && (i = false, l(), i)) throw new Error("The graph contains a negative weight cycle");
389
+ return o;
390
+ }
391
+ function Gn(e) {
392
+ let n = {}, t = [], r;
393
+ function o(i) {
394
+ i in n || (n[i] = true, r.push(i), e.successors(i).forEach(o), e.predecessors(i).forEach(o));
395
+ }
396
+ return e.nodes().forEach(function(i) {
397
+ r = [], o(i), r.length && t.push(r);
398
+ }), t;
399
+ }
400
+ var Ne = class {
401
+ constructor() {
402
+ this._arr = [], this._keyIndices = {};
403
+ }
404
+ size() {
405
+ return this._arr.length;
406
+ }
407
+ keys() {
408
+ return this._arr.map((e) => e.key);
409
+ }
410
+ has(e) {
411
+ return e in this._keyIndices;
412
+ }
413
+ priority(e) {
414
+ let n = this._keyIndices[e];
415
+ if (n !== void 0) return this._arr[n].priority;
416
+ }
417
+ min() {
418
+ if (this.size() === 0) throw new Error("Queue underflow");
419
+ return this._arr[0].key;
420
+ }
421
+ add(e, n) {
422
+ let t = this._keyIndices, r = String(e);
423
+ if (!(r in t)) {
424
+ let o = this._arr, i = o.length;
425
+ return t[r] = i, o.push({ key: r, priority: n }), this._decrease(i), true;
426
+ }
427
+ return false;
428
+ }
429
+ removeMin() {
430
+ this._swap(0, this._arr.length - 1);
431
+ let e = this._arr.pop();
432
+ return delete this._keyIndices[e.key], this._heapify(0), e.key;
433
+ }
434
+ decrease(e, n) {
435
+ let t = this._keyIndices[e];
436
+ if (t === void 0) throw new Error(`Key not found: ${e}`);
437
+ let r = this._arr[t].priority;
438
+ if (n > r) throw new Error(`New priority is greater than current priority. Key: ${e} Old: ${r} New: ${n}`);
439
+ this._arr[t].priority = n, this._decrease(t);
440
+ }
441
+ _heapify(e) {
442
+ let n = this._arr, t = 2 * e, r = t + 1, o = e;
443
+ t < n.length && (o = n[t].priority < n[o].priority ? t : o, r < n.length && (o = n[r].priority < n[o].priority ? r : o), o !== e && (this._swap(e, o), this._heapify(o)));
444
+ }
445
+ _decrease(e) {
446
+ let n = this._arr, t = n[e].priority, r;
447
+ for (; e !== 0 && (r = e >> 1, !(n[r].priority < t)); ) this._swap(e, r), e = r;
448
+ }
449
+ _swap(e, n) {
450
+ let t = this._arr, r = this._keyIndices, o = t[e], i = t[n];
451
+ t[e] = i, t[n] = o, r[i.key] = e, r[o.key] = n;
452
+ }
453
+ };
454
+ var kn = () => 1;
455
+ function F(e, n, t, r) {
456
+ let o = function(i) {
457
+ return e.outEdges(i);
458
+ };
459
+ return vn(e, String(n), t || kn, r || o);
460
+ }
461
+ function vn(e, n, t, r) {
462
+ let o = {}, i = new Ne(), s, a, d = function(l) {
463
+ let u = l.v !== s ? l.v : l.w, c = o[u], h = t(l), f = a.distance + h;
464
+ if (h < 0) throw new Error("dijkstra does not allow negative edge weights. Bad edge: " + l + " Weight: " + h);
465
+ f < c.distance && (c.distance = f, c.predecessor = s, i.decrease(u, f));
466
+ };
467
+ for (e.nodes().forEach(function(l) {
468
+ let u = l === n ? 0 : Number.POSITIVE_INFINITY;
469
+ o[l] = { distance: u, predecessor: "" }, i.add(l, u);
470
+ }); i.size() > 0 && (s = i.removeMin(), a = o[s], a.distance !== Number.POSITIVE_INFINITY); ) r(s).forEach(d);
471
+ return o;
472
+ }
473
+ function _n(e, n, t) {
474
+ return e.nodes().reduce(function(r, o) {
475
+ return r[o] = F(e, o, n, t), r;
476
+ }, {});
477
+ }
478
+ function Ge(e) {
479
+ let n = 0, t = [], r = {}, o = [];
480
+ function i(s) {
481
+ let a = r[s] = { onStack: true, lowlink: n, index: n++ };
482
+ if (t.push(s), e.successors(s).forEach(function(d) {
483
+ d in r ? r[d].onStack && (a.lowlink = Math.min(a.lowlink, r[d].index)) : (i(d), a.lowlink = Math.min(a.lowlink, r[d].lowlink));
484
+ }), a.lowlink === a.index) {
485
+ let d = [], l;
486
+ do
487
+ l = t.pop(), r[l].onStack = false, d.push(l);
488
+ while (s !== l);
489
+ o.push(d);
490
+ }
491
+ }
492
+ return e.nodes().forEach(function(s) {
493
+ s in r || i(s);
494
+ }), o;
495
+ }
496
+ function xn(e) {
497
+ return Ge(e).filter(function(n) {
498
+ return n.length > 1 || n.length === 1 && e.hasEdge(n[0], n[0]);
499
+ });
500
+ }
501
+ var Tn = () => 1;
502
+ function On(e, n, t) {
503
+ return In(e, n || Tn, t || function(r) {
504
+ return e.outEdges(r);
505
+ });
506
+ }
507
+ function In(e, n, t) {
508
+ let r = {}, o = e.nodes();
509
+ return o.forEach(function(i) {
510
+ r[i] = {}, r[i][i] = { distance: 0, predecessor: "" }, o.forEach(function(s) {
511
+ i !== s && (r[i][s] = { distance: Number.POSITIVE_INFINITY, predecessor: "" });
512
+ }), t(i).forEach(function(s) {
513
+ let a = s.v === i ? s.w : s.v, d = n(s);
514
+ r[i][a] = { distance: d, predecessor: i };
515
+ });
516
+ }), o.forEach(function(i) {
517
+ let s = r[i];
518
+ o.forEach(function(a) {
519
+ let d = r[a];
520
+ o.forEach(function(l) {
521
+ let u = d[i], c = s[l], h = d[l], f = u.distance + c.distance;
522
+ f < h.distance && (h.distance = f, h.predecessor = c.predecessor);
523
+ });
524
+ });
525
+ }), r;
526
+ }
527
+ var D = class extends Error {
528
+ constructor(...e) {
529
+ super(...e);
530
+ }
531
+ };
532
+ function ke(e) {
533
+ let n = {}, t = {}, r = [];
534
+ function o(i) {
535
+ if (i in t) throw new D();
536
+ i in n || (t[i] = true, n[i] = true, e.predecessors(i).forEach(o), delete t[i], r.push(i));
537
+ }
538
+ if (e.sinks().forEach(o), Object.keys(n).length !== e.nodeCount()) throw new D();
539
+ return r;
540
+ }
541
+ function Cn(e) {
542
+ try {
543
+ ke(e);
544
+ } catch (n) {
545
+ if (n instanceof D) return false;
546
+ throw n;
547
+ }
548
+ return true;
549
+ }
550
+ function Rn(e, n, t, r, o) {
551
+ Array.isArray(n) || (n = [n]);
552
+ let i = ((a) => {
553
+ var d;
554
+ return (d = e.isDirected() ? e.successors(a) : e.neighbors(a)) != null ? d : [];
555
+ }), s = {};
556
+ return n.forEach(function(a) {
557
+ if (!e.hasNode(a)) throw new Error("Graph does not have node: " + a);
558
+ o = ve(e, a, t === "post", s, i, r, o);
559
+ }), o;
560
+ }
561
+ function ve(e, n, t, r, o, i, s) {
562
+ return n in r || (r[n] = true, t || (s = i(s, n)), o(n).forEach(function(a) {
563
+ s = ve(e, a, t, r, o, i, s);
564
+ }), t && (s = i(s, n))), s;
565
+ }
566
+ function _e(e, n, t) {
567
+ return Rn(e, n, t, function(r, o) {
568
+ return r.push(o), r;
569
+ }, []);
570
+ }
571
+ function Pn(e, n) {
572
+ return _e(e, n, "post");
573
+ }
574
+ function Mn(e, n) {
575
+ return _e(e, n, "pre");
576
+ }
577
+ function jn(e, n) {
578
+ let t = new p(), r = {}, o = new Ne(), i;
579
+ function s(d) {
580
+ let l = d.v === i ? d.w : d.v, u = o.priority(l);
581
+ if (u !== void 0) {
582
+ let c = n(d);
583
+ c < u && (r[l] = i, o.decrease(l, c));
584
+ }
585
+ }
586
+ if (e.nodeCount() === 0) return t;
587
+ e.nodes().forEach(function(d) {
588
+ o.add(d, Number.POSITIVE_INFINITY), t.setNode(d);
589
+ }), o.decrease(e.nodes()[0], 0);
590
+ let a = false;
591
+ for (; o.size() > 0; ) {
592
+ if (i = o.removeMin(), i in r) t.setEdge(i, r[i]);
593
+ else {
594
+ if (a) throw new Error("Input graph is not connected: " + e);
595
+ a = true;
596
+ }
597
+ e.nodeEdges(i).forEach(s);
598
+ }
599
+ return t;
600
+ }
601
+ function Sn(e, n, t, r) {
602
+ return Fn(e, n, t, r != null ? r : ((o) => {
603
+ let i = e.outEdges(o);
604
+ return i != null ? i : [];
605
+ }));
606
+ }
607
+ function Fn(e, n, t, r) {
608
+ if (t === void 0) return F(e, n, t, r);
609
+ let o = false, i = e.nodes();
610
+ for (let s = 0; s < i.length; s++) {
611
+ let a = r(i[s]);
612
+ for (let d = 0; d < a.length; d++) {
613
+ let l = a[d], u = l.v === i[s] ? l.v : l.w, c = u === l.v ? l.w : l.v;
614
+ t({ v: u, w: c }) < 0 && (o = true);
615
+ }
616
+ if (o) return we(e, n, t, r);
617
+ }
618
+ return F(e, n, t, r);
619
+ }
620
+ function w(e, n, t, r) {
621
+ let o = r;
622
+ for (; e.hasNode(o); ) o = j(r);
623
+ return t.dummy = n, e.setNode(o, t), o;
624
+ }
625
+ function xe(e) {
626
+ let n = new p().setGraph(e.graph());
627
+ return e.nodes().forEach((t) => n.setNode(t, e.node(t))), e.edges().forEach((t) => {
628
+ let r = n.edge(t.v, t.w) || { weight: 0, minlen: 1 }, o = e.edge(t);
629
+ n.setEdge(t.v, t.w, { weight: r.weight + o.weight, minlen: Math.max(r.minlen, o.minlen) });
630
+ }), n;
631
+ }
632
+ function A(e) {
633
+ let n = new p({ multigraph: e.isMultigraph() }).setGraph(e.graph());
634
+ return e.nodes().forEach((t) => {
635
+ e.children(t).length || n.setNode(t, e.node(t));
636
+ }), e.edges().forEach((t) => {
637
+ n.setEdge(t, e.edge(t));
638
+ }), n;
639
+ }
640
+ function H(e, n) {
641
+ let t = e.x, r = e.y, o = n.x - t, i = n.y - r, s = e.width / 2, a = e.height / 2;
642
+ if (!o && !i) throw new Error("Not possible to find intersection inside of the rectangle");
643
+ let d, l;
644
+ return Math.abs(i) * s > Math.abs(o) * a ? (i < 0 && (a = -a), d = a * o / i, l = a) : (o < 0 && (s = -s), d = s, l = s * i / o), { x: t + d, y: r + l };
645
+ }
646
+ function N(e) {
647
+ let n = k(X(e) + 1).map(() => []);
648
+ return e.nodes().forEach((t) => {
649
+ let r = e.node(t), o = r.rank;
650
+ o !== void 0 && (n[o] || (n[o] = []), n[o][r.order] = t);
651
+ }), n;
652
+ }
653
+ function Te(e) {
654
+ let n = e.nodes().map((r) => {
655
+ let o = e.node(r).rank;
656
+ return o === void 0 ? Number.MAX_VALUE : o;
657
+ }), t = L(Math.min, n);
658
+ e.nodes().forEach((r) => {
659
+ let o = e.node(r);
660
+ Object.hasOwn(o, "rank") && (o.rank -= t);
661
+ });
662
+ }
663
+ function Oe(e) {
664
+ let n = e.nodes().map((s) => e.node(s).rank).filter((s) => s !== void 0), t = L(Math.min, n), r = [];
665
+ e.nodes().forEach((s) => {
666
+ let a = e.node(s).rank - t;
667
+ r[a] || (r[a] = []), r[a].push(s);
668
+ });
669
+ let o = 0, i = e.graph().nodeRankFactor;
670
+ Array.from(r).forEach((s, a) => {
671
+ s === void 0 && a % i !== 0 ? --o : s !== void 0 && o && s.forEach((d) => e.node(d).rank += o);
672
+ });
673
+ }
674
+ function q(e, n, t, r) {
675
+ let o = { width: 0, height: 0 };
676
+ return arguments.length >= 4 && (o.rank = t, o.order = r), w(e, "border", o, n);
677
+ }
678
+ function Dn(e, n = Ie) {
679
+ let t = [];
680
+ for (let r = 0; r < e.length; r += n) {
681
+ let o = e.slice(r, r + n);
682
+ t.push(o);
683
+ }
684
+ return t;
685
+ }
686
+ var Ie = 65535;
687
+ function L(e, n) {
688
+ if (n.length > Ie) {
689
+ let t = Dn(n);
690
+ return e(...t.map((r) => e(...r)));
691
+ } else return e(...n);
692
+ }
693
+ function X(e) {
694
+ let t = e.nodes().map((r) => {
695
+ let o = e.node(r).rank;
696
+ return o === void 0 ? Number.MIN_VALUE : o;
697
+ });
698
+ return L(Math.max, t);
699
+ }
700
+ function Ce(e, n) {
701
+ let t = { lhs: [], rhs: [] };
702
+ return e.forEach((r) => {
703
+ n(r) ? t.lhs.push(r) : t.rhs.push(r);
704
+ }), t;
705
+ }
706
+ function P(e, n) {
707
+ let t = Date.now();
708
+ try {
709
+ return n();
710
+ } finally {
711
+ console.log(e + " time: " + (Date.now() - t) + "ms");
712
+ }
713
+ }
714
+ function M(e, n) {
715
+ return n();
716
+ }
717
+ var An = 0;
718
+ function j(e) {
719
+ let n = ++An;
720
+ return e + ("" + n);
721
+ }
722
+ function k(e, n, t = 1) {
723
+ n == null && (n = e, e = 0);
724
+ let r = (i) => i < n;
725
+ t < 0 && (r = (i) => n < i);
726
+ let o = [];
727
+ for (let i = e; r(i); i += t) o.push(i);
728
+ return o;
729
+ }
730
+ function T(e, n) {
731
+ let t = {};
732
+ for (let r of n) e[r] !== void 0 && (t[r] = e[r]);
733
+ return t;
734
+ }
735
+ function O(e, n) {
736
+ let t;
737
+ return typeof n == "string" ? t = (r) => r[n] : t = n, Object.entries(e).reduce((r, [o, i]) => (r[o] = t(i, o), r), {});
738
+ }
739
+ function Re(e, n) {
740
+ return e.reduce((t, r, o) => (t[r] = n[o], t), {});
741
+ }
742
+ var _ = "\0";
743
+ var U = "3.0.0";
744
+ var K = class {
745
+ constructor() {
746
+ pe(this, "_sentinel");
747
+ let n = {};
748
+ n._next = n._prev = n, this._sentinel = n;
749
+ }
750
+ dequeue() {
751
+ let n = this._sentinel, t = n._prev;
752
+ if (t !== n) return Pe(t), t;
753
+ }
754
+ enqueue(n) {
755
+ let t = this._sentinel;
756
+ n._prev && n._next && Pe(n), n._next = t._next, t._next._prev = n, t._next = n, n._prev = t;
757
+ }
758
+ toString() {
759
+ let n = [], t = this._sentinel, r = t._prev;
760
+ for (; r !== t; ) n.push(JSON.stringify(r, Vn)), r = r._prev;
761
+ return "[" + n.join(", ") + "]";
762
+ }
763
+ };
764
+ function Pe(e) {
765
+ e._prev._next = e._next, e._next._prev = e._prev, delete e._next, delete e._prev;
766
+ }
767
+ function Vn(e, n) {
768
+ if (e !== "_next" && e !== "_prev") return n;
769
+ }
770
+ var Me = K;
771
+ var Wn = () => 1;
772
+ function Q(e, n) {
773
+ if (e.nodeCount() <= 1) return [];
774
+ let t = Yn(e, n || Wn);
775
+ return Bn(t.graph, t.buckets, t.zeroIdx).flatMap((o) => e.outEdges(o.v, o.w) || []);
776
+ }
777
+ function Bn(e, n, t) {
778
+ var a;
779
+ let r = [], o = n[n.length - 1], i = n[0], s;
780
+ for (; e.nodeCount(); ) {
781
+ for (; s = i.dequeue(); ) $(e, n, t, s);
782
+ for (; s = o.dequeue(); ) $(e, n, t, s);
783
+ if (e.nodeCount()) {
784
+ for (let d = n.length - 2; d > 0; --d) if (s = (a = n[d]) == null ? void 0 : a.dequeue(), s) {
785
+ r = r.concat($(e, n, t, s, true) || []);
786
+ break;
787
+ }
788
+ }
789
+ }
790
+ return r;
791
+ }
792
+ function $(e, n, t, r, o) {
793
+ let i = [], s = o ? i : void 0;
794
+ return (e.inEdges(r.v) || []).forEach((a) => {
795
+ let d = e.edge(a), l = e.node(a.v);
796
+ o && i.push({ v: a.v, w: a.w }), l.out -= d, J(n, t, l);
797
+ }), (e.outEdges(r.v) || []).forEach((a) => {
798
+ let d = e.edge(a), l = a.w, u = e.node(l);
799
+ u.in -= d, J(n, t, u);
800
+ }), e.removeNode(r.v), s;
801
+ }
802
+ function Yn(e, n) {
803
+ let t = new p(), r = 0, o = 0;
804
+ e.nodes().forEach((a) => {
805
+ t.setNode(a, { v: a, in: 0, out: 0 });
806
+ }), e.edges().forEach((a) => {
807
+ let d = t.edge(a.v, a.w) || 0, l = n(a), u = d + l;
808
+ t.setEdge(a.v, a.w, u);
809
+ let c = t.node(a.v), h = t.node(a.w);
810
+ o = Math.max(o, c.out += l), r = Math.max(r, h.in += l);
811
+ });
812
+ let i = zn(o + r + 3).map(() => new Me()), s = r + 1;
813
+ return t.nodes().forEach((a) => {
814
+ J(i, s, t.node(a));
815
+ }), { graph: t, buckets: i, zeroIdx: s };
816
+ }
817
+ function J(e, n, t) {
818
+ var r, o, i;
819
+ t.out ? t.in ? (i = e[t.out - t.in + n]) == null || i.enqueue(t) : (o = e[e.length - 1]) == null || o.enqueue(t) : (r = e[0]) == null || r.enqueue(t);
820
+ }
821
+ function zn(e) {
822
+ let n = [];
823
+ for (let t = 0; t < e; t++) n.push(t);
824
+ return n;
825
+ }
826
+ function je(e) {
827
+ (e.graph().acyclicer === "greedy" ? Q(e, t(e)) : Hn(e)).forEach((r) => {
828
+ let o = e.edge(r);
829
+ e.removeEdge(r), o.forwardName = r.name, o.reversed = true, e.setEdge(r.w, r.v, o, j("rev"));
830
+ });
831
+ function t(r) {
832
+ return (o) => r.edge(o).weight;
833
+ }
834
+ }
835
+ function Hn(e) {
836
+ let n = [], t = {}, r = {};
837
+ function o(i) {
838
+ Object.hasOwn(r, i) || (r[i] = true, t[i] = true, e.outEdges(i).forEach((s) => {
839
+ Object.hasOwn(t, s.w) ? n.push(s) : o(s.w);
840
+ }), delete t[i]);
841
+ }
842
+ return e.nodes().forEach(o), n;
843
+ }
844
+ function Se(e) {
845
+ e.edges().forEach((n) => {
846
+ let t = e.edge(n);
847
+ if (t.reversed) {
848
+ e.removeEdge(n);
849
+ let r = t.forwardName;
850
+ delete t.reversed, delete t.forwardName, e.setEdge(n.w, n.v, t, r);
851
+ }
852
+ });
853
+ }
854
+ function Fe(e) {
855
+ e.graph().dummyChains = [], e.edges().forEach((n) => Xn(e, n));
856
+ }
857
+ function Xn(e, n) {
858
+ let t = n.v, r = e.node(t).rank, o = n.w, i = e.node(o).rank, s = n.name, a = e.edge(n), d = a.labelRank;
859
+ if (i === r + 1) return;
860
+ e.removeEdge(n);
861
+ let l, u, c;
862
+ for (c = 0, ++r; r < i; ++c, ++r) a.points = [], u = { width: 0, height: 0, edgeLabel: a, edgeObj: n, rank: r }, l = w(e, "edge", u, "_d"), r === d && (u.width = a.width, u.height = a.height, u.dummy = "edge-label", u.labelpos = a.labelpos), e.setEdge(t, l, { weight: a.weight }, s), c === 0 && e.graph().dummyChains.push(l), t = l;
863
+ e.setEdge(t, o, { weight: a.weight }, s);
864
+ }
865
+ function De(e) {
866
+ e.graph().dummyChains.forEach((n) => {
867
+ let t = e.node(n), r = t.edgeLabel, o;
868
+ for (e.setEdge(t.edgeObj, r); t.dummy; ) o = e.successors(n)[0], e.removeNode(n), r.points.push({ x: t.x, y: t.y }), t.dummy === "edge-label" && (r.x = t.x, r.y = t.y, r.width = t.width, r.height = t.height), n = o, t = e.node(n);
869
+ });
870
+ }
871
+ function S(e) {
872
+ let n = {};
873
+ function t(r) {
874
+ let o = e.node(r);
875
+ if (Object.hasOwn(n, r)) return o.rank;
876
+ n[r] = true;
877
+ let i = e.outEdges(r), s = i ? i.map((d) => d == null ? Number.POSITIVE_INFINITY : t(d.w) - e.edge(d).minlen) : [], a = L(Math.min, s);
878
+ return a === Number.POSITIVE_INFINITY && (a = 0), o.rank = a;
879
+ }
880
+ e.sources().forEach(t);
881
+ }
882
+ function v(e, n) {
883
+ return e.node(n.w).rank - e.node(n.v).rank - e.edge(n).minlen;
884
+ }
885
+ var V = Kn;
886
+ function Kn(e) {
887
+ let n = new p({ directed: false }), t = e.nodes();
888
+ if (t.length === 0) throw new Error("Graph must have at least one node");
889
+ let r = t[0], o = e.nodeCount();
890
+ n.setNode(r, {});
891
+ let i, s;
892
+ for (; $n(n, e) < o && (i = Jn(n, e), !!i); ) s = n.hasNode(i.v) ? v(e, i) : -v(e, i), Qn(n, e, s);
893
+ return n;
894
+ }
895
+ function $n(e, n) {
896
+ function t(r) {
897
+ let o = n.nodeEdges(r);
898
+ o && o.forEach((i) => {
899
+ let s = i.v, a = r === s ? i.w : s;
900
+ !e.hasNode(a) && !v(n, i) && (e.setNode(a, {}), e.setEdge(r, a, {}), t(a));
901
+ });
902
+ }
903
+ return e.nodes().forEach(t), e.nodeCount();
904
+ }
905
+ function Jn(e, n) {
906
+ return n.edges().reduce((r, o) => {
907
+ let i = Number.POSITIVE_INFINITY;
908
+ return e.hasNode(o.v) !== e.hasNode(o.w) && (i = v(n, o)), i < r[0] ? [i, o] : r;
909
+ }, [Number.POSITIVE_INFINITY, null])[1];
910
+ }
911
+ function Qn(e, n, t) {
912
+ e.nodes().forEach((r) => n.node(r).rank += t);
913
+ }
914
+ var { preorder: Zn, postorder: et } = R;
915
+ var Ve = x;
916
+ x.initLowLimValues = ee;
917
+ x.initCutValues = Z;
918
+ x.calcCutValue = We;
919
+ x.leaveEdge = Ye;
920
+ x.enterEdge = ze;
921
+ x.exchangeEdges = He;
922
+ function x(e) {
923
+ e = xe(e), S(e);
924
+ let n = V(e);
925
+ ee(n), Z(n, e);
926
+ let t, r;
927
+ for (; t = Ye(n); ) r = ze(n, e, t), He(n, e, t, r);
928
+ }
929
+ function Z(e, n) {
930
+ let t = et(e, e.nodes());
931
+ t = t.slice(0, t.length - 1), t.forEach((r) => nt(e, n, r));
932
+ }
933
+ function nt(e, n, t) {
934
+ let o = e.node(t).parent, i = e.edge(t, o);
935
+ i.cutvalue = We(e, n, t);
936
+ }
937
+ function We(e, n, t) {
938
+ let o = e.node(t).parent, i = true, s = n.edge(t, o), a = 0;
939
+ s || (i = false, s = n.edge(o, t)), a = s.weight;
940
+ let d = n.nodeEdges(t);
941
+ return d && d.forEach((l) => {
942
+ let u = l.v === t, c = u ? l.w : l.v;
943
+ if (c !== o) {
944
+ let h = u === i, f = n.edge(l).weight;
945
+ if (a += h ? f : -f, rt(e, t, c)) {
946
+ let b = e.edge(t, c).cutvalue;
947
+ a += h ? -b : b;
948
+ }
949
+ }
950
+ }), a;
951
+ }
952
+ function ee(e, n) {
953
+ arguments.length < 2 && (n = e.nodes()[0]), Be(e, {}, 1, n);
954
+ }
955
+ function Be(e, n, t, r, o) {
956
+ let i = t, s = e.node(r);
957
+ n[r] = true;
958
+ let a = e.neighbors(r);
959
+ return a && a.forEach((d) => {
960
+ Object.hasOwn(n, d) || (t = Be(e, n, t, d, r));
961
+ }), s.low = i, s.lim = t++, o ? s.parent = o : delete s.parent, t;
962
+ }
963
+ function Ye(e) {
964
+ return e.edges().find((n) => e.edge(n).cutvalue < 0);
965
+ }
966
+ function ze(e, n, t) {
967
+ let r = t.v, o = t.w;
968
+ n.hasEdge(r, o) || (r = t.w, o = t.v);
969
+ let i = e.node(r), s = e.node(o), a = i, d = false;
970
+ return i.lim > s.lim && (a = s, d = true), n.edges().filter((u) => d === Ae(e, e.node(u.v), a) && d !== Ae(e, e.node(u.w), a)).reduce((u, c) => v(n, c) < v(n, u) ? c : u);
971
+ }
972
+ function He(e, n, t, r) {
973
+ let o = t.v, i = t.w;
974
+ e.removeEdge(o, i), e.setEdge(r.v, r.w, {}), ee(e), Z(e, n), tt(e, n);
975
+ }
976
+ function tt(e, n) {
977
+ let t = e.nodes().find((o) => !e.node(o).parent);
978
+ if (!t) return;
979
+ let r = Zn(e, [t]);
980
+ r = r.slice(1), r.forEach((o) => {
981
+ let s = e.node(o).parent, a = n.edge(o, s), d = false;
982
+ a || (a = n.edge(s, o), d = true), n.node(o).rank = n.node(s).rank + (d ? a.minlen : -a.minlen);
983
+ });
984
+ }
985
+ function rt(e, n, t) {
986
+ return e.hasEdge(n, t);
987
+ }
988
+ function Ae(e, n, t) {
989
+ return t.low <= n.lim && n.lim <= t.lim;
990
+ }
991
+ var Xe = ot;
992
+ function ot(e) {
993
+ let n = e.graph().ranker;
994
+ if (typeof n == "function") return n(e);
995
+ switch (n) {
996
+ case "network-simplex":
997
+ qe(e);
998
+ break;
999
+ case "tight-tree":
1000
+ st(e);
1001
+ break;
1002
+ case "longest-path":
1003
+ it(e);
1004
+ break;
1005
+ case "none":
1006
+ break;
1007
+ default:
1008
+ qe(e);
1009
+ }
1010
+ }
1011
+ var it = S;
1012
+ function st(e) {
1013
+ S(e), V(e);
1014
+ }
1015
+ function qe(e) {
1016
+ Ve(e);
1017
+ }
1018
+ var Ue = at;
1019
+ function at(e) {
1020
+ let n = lt(e);
1021
+ e.graph().dummyChains.forEach((t) => {
1022
+ let r = e.node(t), o = r.edgeObj, i = dt(e, n, o.v, o.w), s = i.path, a = i.lca, d = 0, l = s[d], u = true;
1023
+ for (; t !== o.w; ) {
1024
+ if (r = e.node(t), u) {
1025
+ for (; (l = s[d]) !== a && e.node(l).maxRank < r.rank; ) d++;
1026
+ l === a && (u = false);
1027
+ }
1028
+ if (!u) {
1029
+ for (; d < s.length - 1 && e.node(s[d + 1]).minRank <= r.rank; ) d++;
1030
+ l = s[d];
1031
+ }
1032
+ l !== void 0 && e.setParent(t, l), t = e.successors(t)[0];
1033
+ }
1034
+ });
1035
+ }
1036
+ function dt(e, n, t, r) {
1037
+ let o = [], i = [], s = Math.min(n[t].low, n[r].low), a = Math.max(n[t].lim, n[r].lim), d;
1038
+ d = t;
1039
+ do
1040
+ d = e.parent(d), o.push(d);
1041
+ while (d && (n[d].low > s || a > n[d].lim));
1042
+ let l = d, u = r;
1043
+ for (; (u = e.parent(u)) !== l; ) i.push(u);
1044
+ return { path: o.concat(i.reverse()), lca: l };
1045
+ }
1046
+ function lt(e) {
1047
+ let n = {}, t = 0;
1048
+ function r(o) {
1049
+ let i = t;
1050
+ e.children(o).forEach(r), n[o] = { low: i, lim: t++ };
1051
+ }
1052
+ return e.children(_).forEach(r), n;
1053
+ }
1054
+ function Ke(e) {
1055
+ let n = w(e, "root", {}, "_root"), t = ut(e), r = Object.values(t), o = L(Math.max, r) - 1, i = 2 * o + 1;
1056
+ e.graph().nestingRoot = n, e.edges().forEach((a) => e.edge(a).minlen *= i);
1057
+ let s = ct(e) + 1;
1058
+ e.children(_).forEach((a) => $e(e, n, i, s, o, t, a)), e.graph().nodeRankFactor = i;
1059
+ }
1060
+ function $e(e, n, t, r, o, i, s) {
1061
+ var c;
1062
+ let a = e.children(s);
1063
+ if (!a.length) {
1064
+ s !== n && e.setEdge(n, s, { weight: 0, minlen: t });
1065
+ return;
1066
+ }
1067
+ let d = q(e, "_bt"), l = q(e, "_bb"), u = e.node(s);
1068
+ e.setParent(d, s), u.borderTop = d, e.setParent(l, s), u.borderBottom = l, a.forEach((h) => {
1069
+ var y;
1070
+ $e(e, n, t, r, o, i, h);
1071
+ let f = e.node(h), g = f.borderTop ? f.borderTop : h, b = f.borderBottom ? f.borderBottom : h, m = f.borderTop ? r : 2 * r, E = g !== b ? 1 : o - ((y = i[s]) != null ? y : 0) + 1;
1072
+ e.setEdge(d, g, { weight: m, minlen: E, nestingEdge: true }), e.setEdge(b, l, { weight: m, minlen: E, nestingEdge: true });
1073
+ }), e.parent(s) || e.setEdge(n, d, { weight: 0, minlen: o + ((c = i[s]) != null ? c : 0) });
1074
+ }
1075
+ function ut(e) {
1076
+ let n = {};
1077
+ function t(r, o) {
1078
+ let i = e.children(r);
1079
+ i && i.length && i.forEach((s) => t(s, o + 1)), n[r] = o;
1080
+ }
1081
+ return e.children(_).forEach((r) => t(r, 1)), n;
1082
+ }
1083
+ function ct(e) {
1084
+ return e.edges().reduce((n, t) => n + e.edge(t).weight, 0);
1085
+ }
1086
+ function Je(e) {
1087
+ let n = e.graph();
1088
+ e.removeNode(n.nestingRoot), delete n.nestingRoot, e.edges().forEach((t) => {
1089
+ e.edge(t).nestingEdge && e.removeEdge(t);
1090
+ });
1091
+ }
1092
+ var Ze = ft;
1093
+ function ft(e) {
1094
+ function n(t) {
1095
+ let r = e.children(t), o = e.node(t);
1096
+ if (r.length && r.forEach(n), Object.hasOwn(o, "minRank")) {
1097
+ o.borderLeft = [], o.borderRight = [];
1098
+ for (let i = o.minRank, s = o.maxRank + 1; i < s; ++i) Qe(e, "borderLeft", "_bl", t, o, i), Qe(e, "borderRight", "_br", t, o, i);
1099
+ }
1100
+ }
1101
+ e.children(_).forEach(n);
1102
+ }
1103
+ function Qe(e, n, t, r, o, i) {
1104
+ let s = { width: 0, height: 0, rank: i, borderType: n }, a = o[n][i - 1], d = w(e, "border", s, t);
1105
+ o[n][i] = d, e.setParent(d, r), a && e.setEdge(a, d, { weight: 1 });
1106
+ }
1107
+ function nn(e) {
1108
+ var t;
1109
+ let n = (t = e.graph().rankdir) == null ? void 0 : t.toLowerCase();
1110
+ (n === "lr" || n === "rl") && rn(e);
1111
+ }
1112
+ function tn(e) {
1113
+ var t;
1114
+ let n = (t = e.graph().rankdir) == null ? void 0 : t.toLowerCase();
1115
+ (n === "bt" || n === "rl") && bt(e), (n === "lr" || n === "rl") && (gt(e), rn(e));
1116
+ }
1117
+ function rn(e) {
1118
+ e.nodes().forEach((n) => en(e.node(n))), e.edges().forEach((n) => en(e.edge(n)));
1119
+ }
1120
+ function en(e) {
1121
+ let n = e.width;
1122
+ e.width = e.height, e.height = n;
1123
+ }
1124
+ function bt(e) {
1125
+ e.nodes().forEach((n) => ne(e.node(n))), e.edges().forEach((n) => {
1126
+ var r;
1127
+ let t = e.edge(n);
1128
+ (r = t.points) == null || r.forEach(ne), Object.hasOwn(t, "y") && ne(t);
1129
+ });
1130
+ }
1131
+ function ne(e) {
1132
+ e.y = -e.y;
1133
+ }
1134
+ function gt(e) {
1135
+ e.nodes().forEach((n) => te(e.node(n))), e.edges().forEach((n) => {
1136
+ var r;
1137
+ let t = e.edge(n);
1138
+ (r = t.points) == null || r.forEach(te), Object.hasOwn(t, "x") && te(t);
1139
+ });
1140
+ }
1141
+ function te(e) {
1142
+ let n = e.x;
1143
+ e.x = e.y, e.y = n;
1144
+ }
1145
+ function re(e) {
1146
+ let n = {}, t = e.nodes().filter((d) => !e.children(d).length), r = t.map((d) => e.node(d).rank), o = L(Math.max, r), i = k(o + 1).map(() => []);
1147
+ function s(d) {
1148
+ if (n[d]) return;
1149
+ n[d] = true;
1150
+ let l = e.node(d);
1151
+ i[l.rank].push(d);
1152
+ let u = e.successors(d);
1153
+ u && u.forEach(s);
1154
+ }
1155
+ return t.sort((d, l) => e.node(d).rank - e.node(l).rank).forEach(s), i;
1156
+ }
1157
+ function oe(e, n) {
1158
+ let t = 0;
1159
+ for (let r = 1; r < n.length; ++r) t += mt(e, n[r - 1], n[r]);
1160
+ return t;
1161
+ }
1162
+ function mt(e, n, t) {
1163
+ let r = Re(t, t.map((l, u) => u)), o = n.flatMap((l) => {
1164
+ let u = e.outEdges(l);
1165
+ return u ? u.map((c) => ({ pos: r[c.w], weight: e.edge(c).weight })).sort((c, h) => c.pos - h.pos) : [];
1166
+ }), i = 1;
1167
+ for (; i < t.length; ) i <<= 1;
1168
+ let s = 2 * i - 1;
1169
+ i -= 1;
1170
+ let a = new Array(s).fill(0), d = 0;
1171
+ return o.forEach((l) => {
1172
+ let u = l.pos + i;
1173
+ a[u] += l.weight;
1174
+ let c = 0;
1175
+ for (; u > 0; ) u % 2 && (c += a[u + 1]), u = u - 1 >> 1, a[u] += l.weight;
1176
+ d += l.weight * c;
1177
+ }), d;
1178
+ }
1179
+ function ie(e, n = []) {
1180
+ return n.map((t) => {
1181
+ let r = e.inEdges(t);
1182
+ if (!r || !r.length) return { v: t };
1183
+ {
1184
+ let o = r.reduce((i, s) => {
1185
+ let a = e.edge(s), d = e.node(s.v);
1186
+ return { sum: i.sum + a.weight * d.order, weight: i.weight + a.weight };
1187
+ }, { sum: 0, weight: 0 });
1188
+ return { v: t, barycenter: o.sum / o.weight, weight: o.weight };
1189
+ }
1190
+ });
1191
+ }
1192
+ function se(e, n) {
1193
+ let t = {};
1194
+ e.forEach((o, i) => {
1195
+ let s = { indegree: 0, in: [], out: [], vs: [o.v], i };
1196
+ o.barycenter !== void 0 && (s.barycenter = o.barycenter, s.weight = o.weight), t[o.v] = s;
1197
+ }), n.edges().forEach((o) => {
1198
+ let i = t[o.v], s = t[o.w];
1199
+ i !== void 0 && s !== void 0 && (s.indegree++, i.out.push(s));
1200
+ });
1201
+ let r = Object.values(t).filter((o) => !o.indegree);
1202
+ return Et(r);
1203
+ }
1204
+ function Et(e) {
1205
+ let n = [];
1206
+ function t(o) {
1207
+ return (i) => {
1208
+ i.merged || (i.barycenter === void 0 || o.barycenter === void 0 || i.barycenter >= o.barycenter) && Lt(o, i);
1209
+ };
1210
+ }
1211
+ function r(o) {
1212
+ return (i) => {
1213
+ i.in.push(o), --i.indegree === 0 && e.push(i);
1214
+ };
1215
+ }
1216
+ for (; e.length; ) {
1217
+ let o = e.pop();
1218
+ n.push(o), o.in.reverse().forEach(t(o)), o.out.forEach(r(o));
1219
+ }
1220
+ return n.filter((o) => !o.merged).map((o) => T(o, ["vs", "i", "barycenter", "weight"]));
1221
+ }
1222
+ function Lt(e, n) {
1223
+ let t = 0, r = 0;
1224
+ e.weight && (t += e.barycenter * e.weight, r += e.weight), n.weight && (t += n.barycenter * n.weight, r += n.weight), e.vs = n.vs.concat(e.vs), e.barycenter = t / r, e.weight = r, e.i = Math.min(n.i, e.i), n.merged = true;
1225
+ }
1226
+ function ae(e, n) {
1227
+ let t = Ce(e, (u) => Object.hasOwn(u, "barycenter")), r = t.lhs, o = t.rhs.sort((u, c) => c.i - u.i), i = [], s = 0, a = 0, d = 0;
1228
+ r.sort(yt(!!n)), d = on(i, o, d), r.forEach((u) => {
1229
+ d += u.vs.length, i.push(u.vs), s += u.barycenter * u.weight, a += u.weight, d = on(i, o, d);
1230
+ });
1231
+ let l = { vs: i.flat(1) };
1232
+ return a && (l.barycenter = s / a, l.weight = a), l;
1233
+ }
1234
+ function on(e, n, t) {
1235
+ let r;
1236
+ for (; n.length && (r = n[n.length - 1]).i <= t; ) n.pop(), e.push(r.vs), t++;
1237
+ return t;
1238
+ }
1239
+ function yt(e) {
1240
+ return (n, t) => n.barycenter < t.barycenter ? -1 : n.barycenter > t.barycenter ? 1 : e ? t.i - n.i : n.i - t.i;
1241
+ }
1242
+ function W(e, n, t, r) {
1243
+ let o = e.children(n), i = e.node(n), s = i ? i.borderLeft : void 0, a = i ? i.borderRight : void 0, d = {};
1244
+ s && (o = o.filter((h) => h !== s && h !== a));
1245
+ let l = ie(e, o);
1246
+ l.forEach((h) => {
1247
+ if (e.children(h.v).length) {
1248
+ let f = W(e, h.v, t, r);
1249
+ d[h.v] = f, Object.hasOwn(f, "barycenter") && Nt(h, f);
1250
+ }
1251
+ });
1252
+ let u = se(l, t);
1253
+ wt(u, d);
1254
+ let c = ae(u, r);
1255
+ if (s && a) {
1256
+ c.vs = [s, c.vs, a].flat(1);
1257
+ let h = e.predecessors(s);
1258
+ if (h && h.length) {
1259
+ let f = e.node(h[0]), g = e.predecessors(a), b = e.node(g[0]);
1260
+ Object.hasOwn(c, "barycenter") || (c.barycenter = 0, c.weight = 0), c.barycenter = (c.barycenter * c.weight + f.order + b.order) / (c.weight + 2), c.weight += 2;
1261
+ }
1262
+ }
1263
+ return c;
1264
+ }
1265
+ function wt(e, n) {
1266
+ e.forEach((t) => {
1267
+ t.vs = t.vs.flatMap((r) => n[r] ? n[r].vs : r);
1268
+ });
1269
+ }
1270
+ function Nt(e, n) {
1271
+ e.barycenter !== void 0 ? (e.barycenter = (e.barycenter * e.weight + n.barycenter * n.weight) / (e.weight + n.weight), e.weight += n.weight) : (e.barycenter = n.barycenter, e.weight = n.weight);
1272
+ }
1273
+ function de(e, n, t, r) {
1274
+ r || (r = e.nodes());
1275
+ let o = Gt(e), i = new p({ compound: true }).setGraph({ root: o }).setDefaultNodeLabel((s) => e.node(s));
1276
+ return r.forEach((s) => {
1277
+ let a = e.node(s), d = e.parent(s);
1278
+ if (a.rank === n || a.minRank <= n && n <= a.maxRank) {
1279
+ i.setNode(s), i.setParent(s, d || o);
1280
+ let l = e[t](s);
1281
+ l && l.forEach((u) => {
1282
+ let c = u.v === s ? u.w : u.v, h = i.edge(c, s), f = h !== void 0 ? h.weight : 0;
1283
+ i.setEdge(c, s, { weight: e.edge(u).weight + f });
1284
+ }), Object.hasOwn(a, "minRank") && i.setNode(s, { borderLeft: a.borderLeft[n], borderRight: a.borderRight[n] });
1285
+ }
1286
+ }), i;
1287
+ }
1288
+ function Gt(e) {
1289
+ let n;
1290
+ for (; e.hasNode(n = j("_root")); ) ;
1291
+ return n;
1292
+ }
1293
+ function le(e, n, t) {
1294
+ let r = {}, o;
1295
+ t.forEach((i) => {
1296
+ let s = e.parent(i), a, d;
1297
+ for (; s; ) {
1298
+ if (a = e.parent(s), a ? (d = r[a], r[a] = s) : (d = o, o = s), d && d !== s) {
1299
+ n.setEdge(d, s);
1300
+ return;
1301
+ }
1302
+ s = a;
1303
+ }
1304
+ });
1305
+ }
1306
+ function B(e, n = {}) {
1307
+ if (typeof n.customOrder == "function") {
1308
+ n.customOrder(e, B);
1309
+ return;
1310
+ }
1311
+ let t = X(e), r = sn(e, k(1, t + 1), "inEdges"), o = sn(e, k(t - 1, -1, -1), "outEdges"), i = re(e);
1312
+ if (an(e, i), n.disableOptimalOrderHeuristic) return;
1313
+ let s = Number.POSITIVE_INFINITY, a, d = n.constraints || [];
1314
+ for (let l = 0, u = 0; u < 4; ++l, ++u) {
1315
+ kt(l % 2 ? r : o, l % 4 >= 2, d), i = N(e);
1316
+ let c = oe(e, i);
1317
+ c < s ? (u = 0, a = Object.assign({}, i), s = c) : c === s && (a = structuredClone(i));
1318
+ }
1319
+ an(e, a);
1320
+ }
1321
+ function sn(e, n, t) {
1322
+ let r = /* @__PURE__ */ new Map(), o = (i, s) => {
1323
+ r.has(i) || r.set(i, []), r.get(i).push(s);
1324
+ };
1325
+ for (let i of e.nodes()) {
1326
+ let s = e.node(i);
1327
+ if (typeof s.rank == "number" && o(s.rank, i), typeof s.minRank == "number" && typeof s.maxRank == "number") for (let a = s.minRank; a <= s.maxRank; a++) a !== s.rank && o(a, i);
1328
+ }
1329
+ return n.map(function(i) {
1330
+ return de(e, i, t, r.get(i) || []);
1331
+ });
1332
+ }
1333
+ function kt(e, n, t) {
1334
+ let r = new p();
1335
+ e.forEach(function(o) {
1336
+ t.forEach((a) => r.setEdge(a.left, a.right));
1337
+ let i = o.graph().root, s = W(o, i, r, n);
1338
+ s.vs.forEach((a, d) => o.node(a).order = d), le(o, r, s.vs);
1339
+ });
1340
+ }
1341
+ function an(e, n) {
1342
+ Object.values(n).forEach((t) => t.forEach((r, o) => e.node(r).order = o));
1343
+ }
1344
+ function vt(e, n) {
1345
+ let t = {};
1346
+ function r(o, i) {
1347
+ let s = 0, a = 0, d = o.length, l = i[i.length - 1];
1348
+ return i.forEach((u, c) => {
1349
+ let h = xt(e, u), f = h ? e.node(h).order : d;
1350
+ (h || u === l) && (i.slice(a, c + 1).forEach((g) => {
1351
+ let b = e.predecessors(g);
1352
+ b && b.forEach((m) => {
1353
+ let E = e.node(m), y = E.order;
1354
+ (y < s || f < y) && !(E.dummy && e.node(g).dummy) && dn(t, m, g);
1355
+ });
1356
+ }), a = c + 1, s = f);
1357
+ }), i;
1358
+ }
1359
+ return n.length && n.reduce(r), t;
1360
+ }
1361
+ function _t(e, n) {
1362
+ let t = {};
1363
+ function r(i, s, a, d, l) {
1364
+ k(s, a).forEach((u) => {
1365
+ let c = i[u];
1366
+ if (c !== void 0 && e.node(c).dummy) {
1367
+ let h = e.predecessors(c);
1368
+ h && h.forEach((f) => {
1369
+ if (f === void 0) return;
1370
+ let g = e.node(f);
1371
+ g.dummy && (g.order < d || g.order > l) && dn(t, f, c);
1372
+ });
1373
+ }
1374
+ });
1375
+ }
1376
+ function o(i, s) {
1377
+ let a = -1, d = -1, l = 0;
1378
+ return s.forEach((u, c) => {
1379
+ if (e.node(u).dummy === "border") {
1380
+ let h = e.predecessors(u);
1381
+ if (h && h.length) {
1382
+ let f = h[0];
1383
+ if (f === void 0) return;
1384
+ d = e.node(f).order, r(s, l, c, a, d), l = c, a = d;
1385
+ }
1386
+ }
1387
+ r(s, l, s.length, d, i.length);
1388
+ }), s;
1389
+ }
1390
+ return n.length && n.reduce(o), t;
1391
+ }
1392
+ function xt(e, n) {
1393
+ if (e.node(n).dummy) {
1394
+ let t = e.predecessors(n);
1395
+ if (t) return t.find((r) => e.node(r).dummy);
1396
+ }
1397
+ }
1398
+ function dn(e, n, t) {
1399
+ if (n > t) {
1400
+ let o = n;
1401
+ n = t, t = o;
1402
+ }
1403
+ let r = e[n];
1404
+ r || (e[n] = r = {}), r[t] = true;
1405
+ }
1406
+ function Tt(e, n, t) {
1407
+ if (n > t) {
1408
+ let o = n;
1409
+ n = t, t = o;
1410
+ }
1411
+ let r = e[n];
1412
+ return r !== void 0 && Object.hasOwn(r, t);
1413
+ }
1414
+ function Ot(e, n, t, r) {
1415
+ let o = {}, i = {}, s = {};
1416
+ return n.forEach((a) => {
1417
+ a.forEach((d, l) => {
1418
+ o[d] = d, i[d] = d, s[d] = l;
1419
+ });
1420
+ }), n.forEach((a) => {
1421
+ let d = -1;
1422
+ a.forEach((l) => {
1423
+ let u = r(l);
1424
+ if (u && u.length) {
1425
+ let c = u.sort((f, g) => {
1426
+ let b = s[f], m = s[g];
1427
+ return (b !== void 0 ? b : 0) - (m !== void 0 ? m : 0);
1428
+ }), h = (c.length - 1) / 2;
1429
+ for (let f = Math.floor(h), g = Math.ceil(h); f <= g; ++f) {
1430
+ let b = c[f];
1431
+ if (b === void 0) continue;
1432
+ let m = s[b];
1433
+ if (m !== void 0 && i[l] === l && d < m && !Tt(t, l, b)) {
1434
+ let E = o[b];
1435
+ E !== void 0 && (i[b] = l, i[l] = o[l] = E, d = m);
1436
+ }
1437
+ }
1438
+ }
1439
+ });
1440
+ }), { root: o, align: i };
1441
+ }
1442
+ function It(e, n, t, r, o = false) {
1443
+ let i = {}, s = Ct(e, n, t, o), a = o ? "borderLeft" : "borderRight";
1444
+ function d(f, g) {
1445
+ let b = s.nodes().slice(), m = {}, E = b.pop();
1446
+ for (; E; ) {
1447
+ if (m[E]) f(E);
1448
+ else {
1449
+ m[E] = true, b.push(E);
1450
+ for (let y of g(E)) b.push(y);
1451
+ }
1452
+ E = b.pop();
1453
+ }
1454
+ }
1455
+ function l(f) {
1456
+ let g = s.inEdges(f);
1457
+ g ? i[f] = g.reduce((b, m) => {
1458
+ var I;
1459
+ let E = (I = i[m.v]) != null ? I : 0, y = s.edge(m);
1460
+ return Math.max(b, E + (y !== void 0 ? y : 0));
1461
+ }, 0) : i[f] = 0;
1462
+ }
1463
+ function u(f) {
1464
+ let g = s.outEdges(f), b = Number.POSITIVE_INFINITY;
1465
+ g && (b = g.reduce((E, y) => {
1466
+ let I = i[y.w], be = s.edge(y);
1467
+ return Math.min(E, (I !== void 0 ? I : 0) - (be !== void 0 ? be : 0));
1468
+ }, Number.POSITIVE_INFINITY));
1469
+ let m = e.node(f);
1470
+ b !== Number.POSITIVE_INFINITY && m.borderType !== a && (i[f] = Math.max(i[f] !== void 0 ? i[f] : 0, b));
1471
+ }
1472
+ function c(f) {
1473
+ return s.predecessors(f) || [];
1474
+ }
1475
+ function h(f) {
1476
+ return s.successors(f) || [];
1477
+ }
1478
+ return d(l, c), d(u, h), Object.keys(r).forEach((f) => {
1479
+ var b;
1480
+ let g = t[f];
1481
+ g !== void 0 && (i[f] = (b = i[g]) != null ? b : 0);
1482
+ }), i;
1483
+ }
1484
+ function Ct(e, n, t, r) {
1485
+ let o = new p(), i = e.graph(), s = jt(i.nodesep, i.edgesep, r);
1486
+ return n.forEach((a) => {
1487
+ let d;
1488
+ a.forEach((l) => {
1489
+ let u = t[l];
1490
+ if (u !== void 0) {
1491
+ if (o.setNode(u), d !== void 0) {
1492
+ let c = t[d];
1493
+ if (c !== void 0) {
1494
+ let h = o.edge(c, u);
1495
+ o.setEdge(c, u, Math.max(s(e, l, d), h || 0));
1496
+ }
1497
+ }
1498
+ d = l;
1499
+ }
1500
+ });
1501
+ }), o;
1502
+ }
1503
+ function Rt(e, n) {
1504
+ return Object.values(n).reduce((t, r) => {
1505
+ let o = Number.NEGATIVE_INFINITY, i = Number.POSITIVE_INFINITY;
1506
+ Object.entries(r).forEach(([a, d]) => {
1507
+ let l = St(e, a) / 2;
1508
+ o = Math.max(d + l, o), i = Math.min(d - l, i);
1509
+ });
1510
+ let s = o - i;
1511
+ return s < t[0] && (t = [s, r]), t;
1512
+ }, [Number.POSITIVE_INFINITY, null])[1];
1513
+ }
1514
+ function Pt(e, n) {
1515
+ let t = Object.values(n), r = L(Math.min, t), o = L(Math.max, t);
1516
+ ["u", "d"].forEach((i) => {
1517
+ ["l", "r"].forEach((s) => {
1518
+ let a = i + s, d = e[a];
1519
+ if (!d || d === n) return;
1520
+ let l = Object.values(d), u = r - L(Math.min, l);
1521
+ s !== "l" && (u = o - L(Math.max, l)), u && (e[a] = O(d, (c) => c + u));
1522
+ });
1523
+ });
1524
+ }
1525
+ function Mt(e, n = void 0) {
1526
+ let t = e.ul;
1527
+ return t ? O(t, (r, o) => {
1528
+ var s, a;
1529
+ if (n) {
1530
+ let d = n.toLowerCase(), l = e[d];
1531
+ if (l && l[o] !== void 0) return l[o];
1532
+ }
1533
+ let i = Object.values(e).map((d) => {
1534
+ let l = d[o];
1535
+ return l !== void 0 ? l : 0;
1536
+ }).sort((d, l) => d - l);
1537
+ return (((s = i[1]) != null ? s : 0) + ((a = i[2]) != null ? a : 0)) / 2;
1538
+ }) : {};
1539
+ }
1540
+ function ln(e) {
1541
+ let n = N(e), t = Object.assign(vt(e, n), _t(e, n)), r = {}, o;
1542
+ ["u", "d"].forEach((s) => {
1543
+ o = s === "u" ? n : Object.values(n).reverse(), ["l", "r"].forEach((a) => {
1544
+ a === "r" && (o = o.map((c) => Object.values(c).reverse()));
1545
+ let l = Ot(e, o, t, (c) => (s === "u" ? e.predecessors(c) : e.successors(c)) || []), u = It(e, o, l.root, l.align, a === "r");
1546
+ a === "r" && (u = O(u, (c) => -c)), r[s + a] = u;
1547
+ });
1548
+ });
1549
+ let i = Rt(e, r);
1550
+ return Pt(r, i), Mt(r, e.graph().align);
1551
+ }
1552
+ function jt(e, n, t) {
1553
+ return (r, o, i) => {
1554
+ let s = r.node(o), a = r.node(i), d = 0, l;
1555
+ if (d += s.width / 2, Object.hasOwn(s, "labelpos")) switch (s.labelpos.toLowerCase()) {
1556
+ case "l":
1557
+ l = -s.width / 2;
1558
+ break;
1559
+ case "r":
1560
+ l = s.width / 2;
1561
+ break;
1562
+ }
1563
+ if (l && (d += t ? l : -l), l = void 0, d += (s.dummy ? n : e) / 2, d += (a.dummy ? n : e) / 2, d += a.width / 2, Object.hasOwn(a, "labelpos")) switch (a.labelpos.toLowerCase()) {
1564
+ case "l":
1565
+ l = a.width / 2;
1566
+ break;
1567
+ case "r":
1568
+ l = -a.width / 2;
1569
+ break;
1570
+ }
1571
+ return l && (d += t ? l : -l), d;
1572
+ };
1573
+ }
1574
+ function St(e, n) {
1575
+ return e.node(n).width;
1576
+ }
1577
+ function un(e) {
1578
+ e = A(e), Ft(e), Object.entries(ln(e)).forEach(([n, t]) => e.node(n).x = t);
1579
+ }
1580
+ function Ft(e) {
1581
+ let n = N(e), t = e.graph(), r = t.ranksep, o = t.rankalign, i = 0;
1582
+ n.forEach((s) => {
1583
+ let a = s.reduce((d, l) => {
1584
+ var c;
1585
+ let u = (c = e.node(l).height) != null ? c : 0;
1586
+ return d > u ? d : u;
1587
+ }, 0);
1588
+ s.forEach((d) => {
1589
+ let l = e.node(d);
1590
+ o === "top" ? l.y = i + l.height / 2 : o === "bottom" ? l.y = i + a - l.height / 2 : l.y = i + a / 2;
1591
+ }), i += a + r;
1592
+ });
1593
+ }
1594
+ function he(e, n = {}) {
1595
+ let t = n.debugTiming ? P : M;
1596
+ return t("layout", () => {
1597
+ let r = t(" buildLayoutGraph", () => Xt(e));
1598
+ return t(" runLayout", () => Dt(r, t, n)), t(" updateInputGraph", () => At(e, r)), r;
1599
+ });
1600
+ }
1601
+ function Dt(e, n, t) {
1602
+ n(" makeSpaceForEdgeLabels", () => Ut(e)), n(" removeSelfEdges", () => rr(e)), n(" acyclic", () => je(e)), n(" nestingGraph.run", () => Ke(e)), n(" rank", () => Xe(A(e))), n(" injectEdgeLabelProxies", () => Kt(e)), n(" removeEmptyRanks", () => Oe(e)), n(" nestingGraph.cleanup", () => Je(e)), n(" normalizeRanks", () => Te(e)), n(" assignRankMinMax", () => $t(e)), n(" removeEdgeLabelProxies", () => Jt(e)), n(" normalize.run", () => Fe(e)), n(" parentDummyChains", () => Ue(e)), n(" addBorderSegments", () => Ze(e)), n(" order", () => B(e, t)), n(" insertSelfEdges", () => or(e)), n(" adjustCoordinateSystem", () => nn(e)), n(" position", () => un(e)), n(" positionSelfEdges", () => ir(e)), n(" removeBorderNodes", () => tr(e)), n(" normalize.undo", () => De(e)), n(" fixupEdgeLabelCoords", () => er(e)), n(" undoCoordinateSystem", () => tn(e)), n(" translateGraph", () => Qt(e)), n(" assignNodeIntersects", () => Zt(e)), n(" reversePoints", () => nr(e)), n(" acyclic.undo", () => Se(e));
1603
+ }
1604
+ function At(e, n) {
1605
+ e.nodes().forEach((t) => {
1606
+ let r = e.node(t), o = n.node(t);
1607
+ r && (r.x = o.x, r.y = o.y, r.order = o.order, r.rank = o.rank, n.children(t).length && (r.width = o.width, r.height = o.height));
1608
+ }), e.edges().forEach((t) => {
1609
+ let r = e.edge(t), o = n.edge(t);
1610
+ r.points = o.points, Object.hasOwn(o, "x") && (r.x = o.x, r.y = o.y);
1611
+ }), e.graph().width = n.graph().width, e.graph().height = n.graph().height;
1612
+ }
1613
+ var Vt = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
1614
+ var Wt = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "TB", rankalign: "center" };
1615
+ var Bt = ["acyclicer", "ranker", "rankdir", "align", "rankalign"];
1616
+ var Yt = ["width", "height", "rank"];
1617
+ var cn = { width: 0, height: 0 };
1618
+ var zt = ["minlen", "weight", "width", "height", "labeloffset"];
1619
+ var Ht = { minlen: 1, weight: 1, width: 0, height: 0, labeloffset: 10, labelpos: "r" };
1620
+ var qt = ["labelpos"];
1621
+ function Xt(e) {
1622
+ let n = new p({ multigraph: true, compound: true }), t = ce(e.graph());
1623
+ return n.setGraph(Object.assign({}, Wt, ue(t, Vt), T(t, Bt))), e.nodes().forEach((r) => {
1624
+ let o = ce(e.node(r)), i = ue(o, Yt);
1625
+ Object.keys(cn).forEach((a) => {
1626
+ i[a] === void 0 && (i[a] = cn[a]);
1627
+ }), n.setNode(r, i);
1628
+ let s = e.parent(r);
1629
+ s !== void 0 && n.setParent(r, s);
1630
+ }), e.edges().forEach((r) => {
1631
+ let o = ce(e.edge(r));
1632
+ n.setEdge(r, Object.assign({}, Ht, ue(o, zt), T(o, qt)));
1633
+ }), n;
1634
+ }
1635
+ function Ut(e) {
1636
+ let n = e.graph();
1637
+ n.ranksep /= 2, e.edges().forEach((t) => {
1638
+ let r = e.edge(t);
1639
+ r.minlen *= 2, r.labelpos.toLowerCase() !== "c" && (n.rankdir === "TB" || n.rankdir === "BT" ? r.width += r.labeloffset : r.height += r.labeloffset);
1640
+ });
1641
+ }
1642
+ function Kt(e) {
1643
+ e.edges().forEach((n) => {
1644
+ let t = e.edge(n);
1645
+ if (t.width && t.height) {
1646
+ let r = e.node(n.v), i = { rank: (e.node(n.w).rank - r.rank) / 2 + r.rank, e: n };
1647
+ w(e, "edge-proxy", i, "_ep");
1648
+ }
1649
+ });
1650
+ }
1651
+ function $t(e) {
1652
+ let n = 0;
1653
+ e.nodes().forEach((t) => {
1654
+ let r = e.node(t);
1655
+ r.borderTop && (r.minRank = e.node(r.borderTop).rank, r.maxRank = e.node(r.borderBottom).rank, n = Math.max(n, r.maxRank));
1656
+ }), e.graph().maxRank = n;
1657
+ }
1658
+ function Jt(e) {
1659
+ e.nodes().forEach((n) => {
1660
+ let t = e.node(n);
1661
+ if (t.dummy === "edge-proxy") {
1662
+ let r = t;
1663
+ e.edge(r.e).labelRank = t.rank, e.removeNode(n);
1664
+ }
1665
+ });
1666
+ }
1667
+ function Qt(e) {
1668
+ let n = Number.POSITIVE_INFINITY, t = 0, r = Number.POSITIVE_INFINITY, o = 0, i = e.graph(), s = i.marginx || 0, a = i.marginy || 0;
1669
+ function d(l) {
1670
+ let u = l.x, c = l.y, h = l.width, f = l.height;
1671
+ n = Math.min(n, u - h / 2), t = Math.max(t, u + h / 2), r = Math.min(r, c - f / 2), o = Math.max(o, c + f / 2);
1672
+ }
1673
+ e.nodes().forEach((l) => d(e.node(l))), e.edges().forEach((l) => {
1674
+ let u = e.edge(l);
1675
+ Object.hasOwn(u, "x") && d(u);
1676
+ }), n -= s, r -= a, e.nodes().forEach((l) => {
1677
+ let u = e.node(l);
1678
+ u.x -= n, u.y -= r;
1679
+ }), e.edges().forEach((l) => {
1680
+ let u = e.edge(l);
1681
+ u.points.forEach((c) => {
1682
+ c.x -= n, c.y -= r;
1683
+ }), Object.hasOwn(u, "x") && (u.x -= n), Object.hasOwn(u, "y") && (u.y -= r);
1684
+ }), i.width = t - n + s, i.height = o - r + a;
1685
+ }
1686
+ function Zt(e) {
1687
+ e.edges().forEach((n) => {
1688
+ let t = e.edge(n), r = e.node(n.v), o = e.node(n.w), i, s;
1689
+ t.points ? (i = t.points[0], s = t.points[t.points.length - 1]) : (t.points = [], i = o, s = r), t.points.unshift(H(r, i)), t.points.push(H(o, s));
1690
+ });
1691
+ }
1692
+ function er(e) {
1693
+ e.edges().forEach((n) => {
1694
+ let t = e.edge(n);
1695
+ if (Object.hasOwn(t, "x")) switch ((t.labelpos === "l" || t.labelpos === "r") && (t.width -= t.labeloffset), t.labelpos) {
1696
+ case "l":
1697
+ t.x -= t.width / 2 + t.labeloffset;
1698
+ break;
1699
+ case "r":
1700
+ t.x += t.width / 2 + t.labeloffset;
1701
+ break;
1702
+ }
1703
+ });
1704
+ }
1705
+ function nr(e) {
1706
+ e.edges().forEach((n) => {
1707
+ let t = e.edge(n);
1708
+ t.reversed && t.points.reverse();
1709
+ });
1710
+ }
1711
+ function tr(e) {
1712
+ e.nodes().forEach((n) => {
1713
+ if (e.children(n).length) {
1714
+ let t = e.node(n), r = e.node(t.borderTop), o = e.node(t.borderBottom), i = e.node(t.borderLeft[t.borderLeft.length - 1]), s = e.node(t.borderRight[t.borderRight.length - 1]);
1715
+ t.width = Math.abs(s.x - i.x), t.height = Math.abs(o.y - r.y), t.x = i.x + t.width / 2, t.y = r.y + t.height / 2;
1716
+ }
1717
+ }), e.nodes().forEach((n) => {
1718
+ e.node(n).dummy === "border" && e.removeNode(n);
1719
+ });
1720
+ }
1721
+ function rr(e) {
1722
+ e.edges().forEach((n) => {
1723
+ if (n.v === n.w) {
1724
+ let t = e.node(n.v);
1725
+ t.selfEdges || (t.selfEdges = []), t.selfEdges.push({ e: n, label: e.edge(n) }), e.removeEdge(n);
1726
+ }
1727
+ });
1728
+ }
1729
+ function or(e) {
1730
+ N(e).forEach((t) => {
1731
+ let r = 0;
1732
+ t.forEach((o, i) => {
1733
+ let s = e.node(o);
1734
+ s.order = i + r, (s.selfEdges || []).forEach((a) => {
1735
+ w(e, "selfedge", { width: a.label.width, height: a.label.height, rank: s.rank, order: i + ++r, e: a.e, label: a.label }, "_se");
1736
+ }), delete s.selfEdges;
1737
+ });
1738
+ });
1739
+ }
1740
+ function ir(e) {
1741
+ e.nodes().forEach((n) => {
1742
+ let t = e.node(n);
1743
+ if (t.dummy === "selfedge") {
1744
+ let r = t, o = e.node(r.e.v), i = o.x + o.width / 2, s = o.y, a = t.x - i, d = o.height / 2;
1745
+ e.setEdge(r.e, r.label), e.removeNode(n), r.label.points = [{ x: i + 2 * a / 3, y: s - d }, { x: i + 5 * a / 6, y: s - d }, { x: i + a, y: s }, { x: i + 5 * a / 6, y: s + d }, { x: i + 2 * a / 3, y: s + d }], r.label.x = t.x, r.label.y = t.y;
1746
+ }
1747
+ });
1748
+ }
1749
+ function ue(e, n) {
1750
+ return O(T(e, n), Number);
1751
+ }
1752
+ function ce(e) {
1753
+ let n = {};
1754
+ return e && Object.entries(e).forEach(([t, r]) => {
1755
+ typeof t == "string" && (t = t.toLowerCase()), n[t] = r;
1756
+ }), n;
1757
+ }
1758
+ function fe(e) {
1759
+ let n = N(e), t = new p({ compound: true, multigraph: true }).setGraph({});
1760
+ return e.nodes().forEach((r) => {
1761
+ t.setNode(r, { label: r }), t.setParent(r, "layer" + e.node(r).rank);
1762
+ }), e.edges().forEach((r) => t.setEdge(r.v, r.w, {}, r.name)), n.forEach((r, o) => {
1763
+ let i = "layer" + o;
1764
+ t.setNode(i, { rank: "same" }), r.reduce((s, a) => (t.setEdge(s, a, { style: "invis" }), a));
1765
+ }), t;
1766
+ }
1767
+ var sr = { graphlib: z, version: U, layout: he, debug: fe, util: { time: P, notime: M } };
1768
+ var To = sr;
1769
+
1770
+ // src/shared/text.ts
1771
+ var UI_FONT_FAMILY = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
1772
+ var NARROW = new Set("ijltfI.,:;'|!()[]{}-/\\ ".split(""));
1773
+ var WIDE = new Set("mwMW@%".split(""));
1774
+ var canvasCtx;
1775
+ function getCtx() {
1776
+ if (canvasCtx !== void 0) return canvasCtx;
1777
+ const isJsdom = typeof navigator !== "undefined" && /jsdom/i.test(navigator.userAgent || "");
1778
+ if (isJsdom) {
1779
+ canvasCtx = null;
1780
+ return canvasCtx;
1781
+ }
1782
+ try {
1783
+ const c = typeof document !== "undefined" ? document.createElement("canvas") : null;
1784
+ const ctx = c ? c.getContext("2d") : null;
1785
+ canvasCtx = ctx && typeof ctx.measureText === "function" && ctx.measureText("M").width > 0 ? ctx : null;
1786
+ } catch {
1787
+ canvasCtx = null;
1788
+ }
1789
+ return canvasCtx;
1790
+ }
1791
+ function isBold(weight) {
1792
+ return weight === "bold" || typeof weight === "number" && weight >= 600;
1793
+ }
1794
+ var cache = /* @__PURE__ */ new Map();
1795
+ function measureText(text, font) {
1796
+ if (!text) return 0;
1797
+ const weight = font.weight ?? "normal";
1798
+ const family = font.family ?? UI_FONT_FAMILY;
1799
+ const key = `${font.size}|${weight}|${family}|${text}`;
1800
+ const hit = cache.get(key);
1801
+ if (hit !== void 0) return hit;
1802
+ let width;
1803
+ const ctx = getCtx();
1804
+ if (ctx) {
1805
+ ctx.font = `${isBold(weight) ? "700" : "400"} ${font.size}px ${family}`;
1806
+ width = ctx.measureText(text).width;
1807
+ } else {
1808
+ const boldFactor = isBold(weight) ? 1.05 : 1;
1809
+ let units = 0;
1810
+ for (const ch of text) {
1811
+ if (NARROW.has(ch)) units += 0.32;
1812
+ else if (WIDE.has(ch)) units += 0.87;
1813
+ else if (ch >= "A" && ch <= "Z") units += 0.7;
1814
+ else if (ch >= "0" && ch <= "9") units += 0.56;
1815
+ else units += 0.53;
1816
+ }
1817
+ width = units * font.size * boldFactor;
1818
+ }
1819
+ cache.set(key, width);
1820
+ return width;
1821
+ }
1822
+ function lineHeight(fontSize) {
1823
+ return Math.round(fontSize * 1.3);
1824
+ }
1825
+ function wrapText(text, maxWidth, font) {
1826
+ const words = text.split(/\s+/).filter(Boolean);
1827
+ if (words.length === 0) return [];
1828
+ const lines = [];
1829
+ let line = "";
1830
+ const pushHardBroken = (word) => {
1831
+ const chunks = hardBreak(word, maxWidth, font);
1832
+ for (let i = 0; i < chunks.length - 1; i++) lines.push(chunks[i]);
1833
+ line = chunks[chunks.length - 1] ?? "";
1834
+ };
1835
+ for (const word of words) {
1836
+ if (!line) {
1837
+ if (measureText(word, font) > maxWidth) pushHardBroken(word);
1838
+ else line = word;
1839
+ continue;
1840
+ }
1841
+ const candidate = `${line} ${word}`;
1842
+ if (measureText(candidate, font) <= maxWidth) {
1843
+ line = candidate;
1844
+ } else {
1845
+ lines.push(line);
1846
+ if (measureText(word, font) > maxWidth) pushHardBroken(word);
1847
+ else line = word;
1848
+ }
1849
+ }
1850
+ if (line) lines.push(line);
1851
+ return lines;
1852
+ }
1853
+ function hardBreak(word, maxWidth, font) {
1854
+ const out = [];
1855
+ let cur = "";
1856
+ for (const ch of word) {
1857
+ if (cur && measureText(cur + ch, font) > maxWidth) {
1858
+ out.push(cur);
1859
+ cur = ch;
1860
+ } else {
1861
+ cur += ch;
1862
+ }
1863
+ }
1864
+ if (cur) out.push(cur);
1865
+ return out.length ? out : [word];
1866
+ }
1867
+
1868
+ // src/shared/edge-label.ts
1869
+ var SVG_NS = "http://www.w3.org/2000/svg";
1870
+ var PAD_X = 7;
1871
+ var PAD_Y = 3;
1872
+ var DEFAULT_MAX_WIDTH = 150;
1873
+ var DEFAULT_FONT_SIZE = 10;
1874
+ function layout(text, maxWidth, fontSize) {
1875
+ const font = { size: fontSize, weight: 600 };
1876
+ const lines = wrapText(text, maxWidth, font);
1877
+ const textW = lines.reduce((m, l) => Math.max(m, measureText(l, font)), 1);
1878
+ const lh = lineHeight(fontSize);
1879
+ return {
1880
+ lines,
1881
+ size: { w: Math.ceil(textW + PAD_X * 2), h: Math.ceil(lines.length * lh + PAD_Y * 2) }
1882
+ };
1883
+ }
1884
+ function edgeLabelSize(text, maxWidth = DEFAULT_MAX_WIDTH, fontSize = DEFAULT_FONT_SIZE) {
1885
+ return layout(text, maxWidth, fontSize).size;
1886
+ }
1887
+ function createEdgeLabel(opts) {
1888
+ const fontSize = opts.fontSize ?? DEFAULT_FONT_SIZE;
1889
+ const maxWidth = opts.maxWidth ?? DEFAULT_MAX_WIDTH;
1890
+ const { lines, size } = layout(opts.text, maxWidth, fontSize);
1891
+ const lh = lineHeight(fontSize);
1892
+ const g = document.createElementNS(SVG_NS, "g");
1893
+ g.setAttribute("class", opts.className ?? "d5-edge-label");
1894
+ const rect = document.createElementNS(SVG_NS, "rect");
1895
+ rect.setAttribute("x", String(opts.x - size.w / 2));
1896
+ rect.setAttribute("y", String(opts.y - size.h / 2));
1897
+ rect.setAttribute("width", String(size.w));
1898
+ rect.setAttribute("height", String(size.h));
1899
+ rect.setAttribute("rx", "4");
1900
+ rect.setAttribute("fill", "white");
1901
+ rect.setAttribute("fill-opacity", "0.92");
1902
+ rect.setAttribute("stroke", "#cbd5e1");
1903
+ rect.setAttribute("stroke-width", "1");
1904
+ g.appendChild(rect);
1905
+ const firstBaseline = opts.y - size.h / 2 + PAD_Y + fontSize * 0.82;
1906
+ lines.forEach((ln2, i) => {
1907
+ const t = document.createElementNS(SVG_NS, "text");
1908
+ t.setAttribute("x", String(opts.x));
1909
+ t.setAttribute("y", String(firstBaseline + i * lh));
1910
+ t.setAttribute("text-anchor", "middle");
1911
+ t.setAttribute("font-size", String(fontSize));
1912
+ t.setAttribute("font-weight", "600");
1913
+ t.setAttribute("fill", opts.fill ?? "#475569");
1914
+ t.textContent = ln2;
1915
+ g.appendChild(t);
1916
+ });
1917
+ return g;
1918
+ }
1919
+
1920
+ // src/shared/shape.ts
1921
+ function boxWidth(entries, padX, min, max = Infinity) {
1922
+ const widest = entries.reduce((m, e) => Math.max(m, measureText(e.text, e.font)), 0);
1923
+ return Math.round(Math.max(min, Math.min(max, widest + padX * 2)));
1924
+ }
1925
+ function gridDimensions(count, cellWidth, gap, targetWidth) {
1926
+ if (count <= 0) return { cols: 0, rows: 0 };
1927
+ const fit = Math.max(1, Math.floor((targetWidth + gap) / (cellWidth + gap)));
1928
+ const cols = Math.min(count, fit);
1929
+ const rows = Math.ceil(count / cols);
1930
+ return { cols, rows };
1931
+ }
1932
+
1933
+ // src/d5-domain/renderer.ts
1934
+ var REL_LABEL_MAX_WIDTH = 150;
1935
+ var SVG_NS2 = "http://www.w3.org/2000/svg";
1936
+ var SUBDOMAIN_MIN_WIDTH = 180;
1937
+ var SUBDOMAIN_MAX_WIDTH = 320;
1938
+ var SUBDOMAIN_HEIGHT = 70;
1939
+ var SUBDOMAIN_LABEL_PAD_X = 18;
1940
+ var DOMAIN_PADDING = 30;
1941
+ var TITLE_HEIGHT = 40;
1942
+ var DOMAIN_HEADER = 36;
1943
+ var ARROW_MARKER_SIZE = 8;
1944
+ var SUBDOMAIN_COLORS = {
1945
+ core: { fill: "#dbeafe", stroke: "#3b82f6" },
1946
+ supporting: { fill: "#fef9c3", stroke: "#ca8a04" },
1947
+ generic: { fill: "#e5e7eb", stroke: "#6b7280" }
1948
+ };
1949
+ var LEGEND_ITEMS = [
1950
+ { type: "core", label: "Core" },
1951
+ { type: "supporting", label: "Supporting" },
1952
+ { type: "generic", label: "Generic" }
1953
+ ];
1954
+ function addArrowMarker(svg) {
1955
+ let defs = svg.querySelector("defs");
1956
+ if (!defs) {
1957
+ defs = document.createElementNS(SVG_NS2, "defs");
1958
+ svg.prepend(defs);
1959
+ }
1960
+ const marker = document.createElementNS(SVG_NS2, "marker");
1961
+ marker.setAttribute("id", "d5-arrowhead");
1962
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE));
1963
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE));
1964
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE));
1965
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE / 2));
1966
+ marker.setAttribute("orient", "auto");
1967
+ const path = document.createElementNS(SVG_NS2, "path");
1968
+ path.setAttribute("d", `M0,0 L${ARROW_MARKER_SIZE},${ARROW_MARKER_SIZE / 2} L0,${ARROW_MARKER_SIZE}`);
1969
+ path.setAttribute("fill", "#64748b");
1970
+ marker.appendChild(path);
1971
+ defs.appendChild(marker);
1972
+ }
1973
+ function generateCurvePath(points) {
1974
+ if (!points || points.length === 0) return "";
1975
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
1976
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
1977
+ let d = `M ${points[0].x} ${points[0].y}`;
1978
+ for (let i = 1; i < points.length - 1; i++) {
1979
+ const p1 = points[i];
1980
+ const p2 = points[i + 1];
1981
+ const midX = (p1.x + p2.x) / 2;
1982
+ const midY = (p1.y + p2.y) / 2;
1983
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
1984
+ }
1985
+ const last = points[points.length - 1];
1986
+ d += ` L ${last.x} ${last.y}`;
1987
+ return d;
1988
+ }
1989
+ function render(db, container) {
1990
+ addArrowMarker(container);
1991
+ const subdomains = db.getSubdomains();
1992
+ const domain = db.getDomain();
1993
+ const relationships = db.getRelationships();
1994
+ const direction = db.getDirection();
1995
+ const horizontal = direction === "LR" || direction === "RL";
1996
+ const g = new To.graphlib.Graph();
1997
+ g.setGraph({
1998
+ rankdir: direction,
1999
+ nodesep: horizontal ? 46 : 74,
2000
+ ranksep: horizontal ? 92 : 58,
2001
+ edgesep: 28,
2002
+ acyclicer: "greedy",
2003
+ ranker: "network-simplex",
2004
+ marginx: 0,
2005
+ marginy: 0
2006
+ });
2007
+ g.setDefaultEdgeLabel(() => ({}));
2008
+ subdomains.forEach((sd) => {
2009
+ const w2 = boxWidth(
2010
+ [{ text: sd.label, font: { size: 13, weight: 600 } }],
2011
+ SUBDOMAIN_LABEL_PAD_X,
2012
+ SUBDOMAIN_MIN_WIDTH,
2013
+ SUBDOMAIN_MAX_WIDTH
2014
+ );
2015
+ g.setNode(sd.id, { width: w2, height: SUBDOMAIN_HEIGHT });
2016
+ });
2017
+ relationships.forEach((rel) => {
2018
+ const edgeCfg = { minlen: 1 };
2019
+ if (rel.label) {
2020
+ const { w: w2, h } = edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH);
2021
+ edgeCfg.width = w2;
2022
+ edgeCfg.height = h;
2023
+ edgeCfg.labelpos = "c";
2024
+ }
2025
+ g.setEdge(rel.source, rel.target, edgeCfg);
2026
+ });
2027
+ To.layout(g);
2028
+ const graphW = g.graph().width || 0;
2029
+ const graphH = g.graph().height || 0;
2030
+ const domainX = DOMAIN_PADDING;
2031
+ const domainY = (db.getTitle() ? TITLE_HEIGHT : 0) + DOMAIN_PADDING;
2032
+ let domainW = graphW + DOMAIN_PADDING * 2;
2033
+ if (!domain && subdomains.length === 0) domainW = 400;
2034
+ const domainH = (domain ? DOMAIN_HEADER : 0) + graphH + DOMAIN_PADDING * 2;
2035
+ const totalW = domainX + domainW + DOMAIN_PADDING;
2036
+ const legendH = 30;
2037
+ const totalH = domainY + domainH + legendH + DOMAIN_PADDING;
2038
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
2039
+ container.setAttribute("height", String(totalH));
2040
+ container.style.maxWidth = `${totalW}px`;
2041
+ const title = db.getTitle();
2042
+ if (title) {
2043
+ const titleEl = document.createElementNS(SVG_NS2, "text");
2044
+ titleEl.setAttribute("class", "d5-title");
2045
+ titleEl.setAttribute("x", String(totalW / 2));
2046
+ titleEl.setAttribute("y", "24");
2047
+ titleEl.setAttribute("text-anchor", "middle");
2048
+ titleEl.setAttribute("font-size", "18");
2049
+ titleEl.setAttribute("font-weight", "bold");
2050
+ titleEl.setAttribute("fill", "#1e293b");
2051
+ titleEl.textContent = title;
2052
+ container.appendChild(titleEl);
2053
+ }
2054
+ const domainGroup = document.createElementNS(SVG_NS2, "g");
2055
+ domainGroup.setAttribute("class", "d5-domain");
2056
+ const domainRect = document.createElementNS(SVG_NS2, "rect");
2057
+ domainRect.setAttribute("x", String(domainX));
2058
+ domainRect.setAttribute("y", String(domainY));
2059
+ domainRect.setAttribute("width", String(domainW));
2060
+ domainRect.setAttribute("height", String(domainH));
2061
+ domainRect.setAttribute("rx", "12");
2062
+ domainRect.setAttribute("fill", "#f8fafc");
2063
+ domainRect.setAttribute("stroke", "#94a3b8");
2064
+ domainRect.setAttribute("stroke-width", "2");
2065
+ domainRect.setAttribute("stroke-dasharray", "8 4");
2066
+ domainGroup.appendChild(domainRect);
2067
+ if (domain) {
2068
+ const domainLabel = document.createElementNS(SVG_NS2, "text");
2069
+ domainLabel.setAttribute("x", String(domainX + 16));
2070
+ domainLabel.setAttribute("y", String(domainY + 24));
2071
+ domainLabel.setAttribute("font-size", "14");
2072
+ domainLabel.setAttribute("font-weight", "600");
2073
+ domainLabel.setAttribute("fill", "#475569");
2074
+ domainLabel.textContent = domain.label;
2075
+ domainGroup.appendChild(domainLabel);
2076
+ }
2077
+ container.appendChild(domainGroup);
2078
+ const graphStartX = domainX + DOMAIN_PADDING;
2079
+ const graphStartY = domainY + (domain ? DOMAIN_HEADER : 0) + DOMAIN_PADDING;
2080
+ subdomains.forEach((sd) => {
2081
+ const node = g.node(sd.id);
2082
+ if (!node) return;
2083
+ const w2 = node.width;
2084
+ const h = node.height;
2085
+ const x2 = graphStartX + node.x - w2 / 2;
2086
+ const y = graphStartY + node.y - h / 2;
2087
+ const cx = x2 + w2 / 2;
2088
+ const cy = y + h / 2;
2089
+ const colors = SUBDOMAIN_COLORS[sd.type];
2090
+ const group = document.createElementNS(SVG_NS2, "g");
2091
+ group.setAttribute("class", `d5-subdomain d5-subdomain-${sd.type}`);
2092
+ const rect = document.createElementNS(SVG_NS2, "rect");
2093
+ rect.setAttribute("x", String(x2));
2094
+ rect.setAttribute("y", String(y));
2095
+ rect.setAttribute("width", String(w2));
2096
+ rect.setAttribute("height", String(h));
2097
+ rect.setAttribute("rx", "8");
2098
+ rect.setAttribute("fill", colors.fill);
2099
+ rect.setAttribute("stroke", colors.stroke);
2100
+ rect.setAttribute("stroke-width", "2");
2101
+ group.appendChild(rect);
2102
+ const labelText = document.createElementNS(SVG_NS2, "text");
2103
+ labelText.setAttribute("x", String(cx));
2104
+ labelText.setAttribute("y", String(cy - 4));
2105
+ labelText.setAttribute("text-anchor", "middle");
2106
+ labelText.setAttribute("font-size", "13");
2107
+ labelText.setAttribute("font-weight", "600");
2108
+ labelText.setAttribute("fill", "#1e293b");
2109
+ labelText.textContent = sd.label;
2110
+ group.appendChild(labelText);
2111
+ const typeText = document.createElementNS(SVG_NS2, "text");
2112
+ typeText.setAttribute("x", String(cx));
2113
+ typeText.setAttribute("y", String(cy + 16));
2114
+ typeText.setAttribute("text-anchor", "middle");
2115
+ typeText.setAttribute("font-size", "11");
2116
+ typeText.setAttribute("fill", colors.stroke);
2117
+ typeText.textContent = sd.type;
2118
+ group.appendChild(typeText);
2119
+ container.appendChild(group);
2120
+ });
2121
+ relationships.forEach((rel) => {
2122
+ const edge = g.edge(rel.source, rel.target);
2123
+ if (!edge || !edge.points || edge.points.length === 0) return;
2124
+ const shiftedPoints = edge.points.map((p2) => ({
2125
+ x: graphStartX + p2.x,
2126
+ y: graphStartY + p2.y
2127
+ }));
2128
+ const srcNode = g.node(rel.source);
2129
+ const tgtNode = g.node(rel.target);
2130
+ const isBackEdge = !!srcNode && !!tgtNode && tgtNode.y < srcNode.y - 1;
2131
+ const group = document.createElementNS(SVG_NS2, "g");
2132
+ group.setAttribute("class", isBackEdge ? "d5-rel d5-rel-back" : "d5-rel");
2133
+ const pathString = generateCurvePath(shiftedPoints);
2134
+ const path = document.createElementNS(SVG_NS2, "path");
2135
+ path.setAttribute("d", pathString);
2136
+ path.setAttribute("fill", "none");
2137
+ path.setAttribute("stroke", isBackEdge ? "#94a3b8" : "#64748b");
2138
+ path.setAttribute("stroke-width", "1.5");
2139
+ if (isBackEdge) path.setAttribute("stroke-dasharray", "6 4");
2140
+ path.setAttribute("marker-end", "url(#d5-arrowhead)");
2141
+ group.appendChild(path);
2142
+ if (rel.label) {
2143
+ let lx;
2144
+ let ly;
2145
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
2146
+ lx = graphStartX + edge.x;
2147
+ ly = graphStartY + edge.y;
2148
+ } else {
2149
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
2150
+ lx = mid.x;
2151
+ ly = mid.y;
2152
+ }
2153
+ group.appendChild(
2154
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH })
2155
+ );
2156
+ }
2157
+ container.appendChild(group);
2158
+ });
2159
+ const legendY = domainY + domainH + 16;
2160
+ const legendGroup = document.createElementNS(SVG_NS2, "g");
2161
+ legendGroup.setAttribute("class", "d5-legend");
2162
+ let legendX = domainX;
2163
+ LEGEND_ITEMS.forEach((item) => {
2164
+ const colors = SUBDOMAIN_COLORS[item.type];
2165
+ const swatch = document.createElementNS(SVG_NS2, "rect");
2166
+ swatch.setAttribute("x", String(legendX));
2167
+ swatch.setAttribute("y", String(legendY));
2168
+ swatch.setAttribute("width", "14");
2169
+ swatch.setAttribute("height", "14");
2170
+ swatch.setAttribute("rx", "3");
2171
+ swatch.setAttribute("fill", colors.fill);
2172
+ swatch.setAttribute("stroke", colors.stroke);
2173
+ swatch.setAttribute("stroke-width", "1.5");
2174
+ legendGroup.appendChild(swatch);
2175
+ const label = document.createElementNS(SVG_NS2, "text");
2176
+ label.setAttribute("x", String(legendX + 20));
2177
+ label.setAttribute("y", String(legendY + 11));
2178
+ label.setAttribute("font-size", "11");
2179
+ label.setAttribute("fill", "#64748b");
2180
+ label.textContent = item.label;
2181
+ legendGroup.appendChild(label);
2182
+ legendX += 90;
2183
+ });
2184
+ container.appendChild(legendGroup);
2185
+ }
2186
+
2187
+ // src/d5-subdomain/detector.ts
2188
+ function detectSubdomain(text) {
2189
+ return text.trimStart().startsWith("d5-subdomain");
2190
+ }
2191
+
2192
+ // src/d5-subdomain/db.ts
2193
+ var DEFAULT_DIRECTION2 = "LR";
2194
+ var D5SubdomainDb = class {
2195
+ subdomains = [];
2196
+ boundedContexts = [];
2197
+ relationships = [];
2198
+ direction = DEFAULT_DIRECTION2;
2199
+ title;
2200
+ accTitle;
2201
+ accDescription;
2202
+ setTitle(title) {
2203
+ this.title = title;
2204
+ }
2205
+ getTitle() {
2206
+ return this.title;
2207
+ }
2208
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
2209
+ setDirection(dir) {
2210
+ const normalized = normalizeDirection(dir);
2211
+ if (normalized) this.direction = normalized;
2212
+ }
2213
+ getDirection() {
2214
+ return this.direction;
2215
+ }
2216
+ addSubdomain(id, label, type) {
2217
+ this.subdomains.push({ id, label, type });
2218
+ }
2219
+ getSubdomains() {
2220
+ return this.subdomains;
2221
+ }
2222
+ addBoundedContext(id, label, subdomainId, team) {
2223
+ this.boundedContexts.push({ id, label, subdomainId, team });
2224
+ }
2225
+ getBoundedContexts() {
2226
+ return this.boundedContexts;
2227
+ }
2228
+ addRelationship(source, target, label) {
2229
+ this.relationships.push({ source, target, label });
2230
+ }
2231
+ getRelationships() {
2232
+ return this.relationships;
2233
+ }
2234
+ clear() {
2235
+ this.subdomains = [];
2236
+ this.boundedContexts = [];
2237
+ this.relationships = [];
2238
+ this.direction = DEFAULT_DIRECTION2;
2239
+ this.title = void 0;
2240
+ this.accTitle = void 0;
2241
+ this.accDescription = void 0;
2242
+ }
2243
+ // Mermaid DiagramDB interface methods
2244
+ setDiagramTitle(title) {
2245
+ this.setTitle(title);
2246
+ }
2247
+ getDiagramTitle() {
2248
+ return this.title ?? "";
2249
+ }
2250
+ setAccTitle(title) {
2251
+ this.accTitle = title;
2252
+ }
2253
+ getAccTitle() {
2254
+ return this.accTitle ?? "";
2255
+ }
2256
+ setAccDescription(desc) {
2257
+ this.accDescription = desc;
2258
+ }
2259
+ getAccDescription() {
2260
+ return this.accDescription ?? "";
2261
+ }
2262
+ };
2263
+
2264
+ // src/d5-subdomain/parser.ts
2265
+ var TITLE_RE2 = /^\s*title\s+(.+)$/;
2266
+ var SUBDOMAIN_RE2 = /^\s*Subdomain\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*(\w+)\s*\)\s*\{/;
2267
+ var BC_RE = /^\s*BoundedContext\(\s*(\w+)\s*,\s*"([^"]+)"\s*(?:,\s*team:\s*"([^"]+)")?\s*\)/;
2268
+ var REL_RE2 = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2269
+ var CLOSE_BRACE_RE = /^\s*\}/;
2270
+ var COMMENT_LINE_RE2 = /^\s*%%/;
2271
+ function stripInlineComment2(line) {
2272
+ const idx = line.indexOf("%%");
2273
+ return idx === -1 ? line : line.slice(0, idx);
2274
+ }
2275
+ function parse2(text, db) {
2276
+ const lines = text.split("\n");
2277
+ let currentSubdomainId;
2278
+ for (const raw of lines) {
2279
+ if (COMMENT_LINE_RE2.test(raw)) continue;
2280
+ const line = stripInlineComment2(raw).trim();
2281
+ if (line === "" || line === "d5-subdomain") continue;
2282
+ let m;
2283
+ if (m = line.match(DIRECTION_RE)) {
2284
+ db.setDirection(m[1]);
2285
+ } else if (m = line.match(TITLE_RE2)) {
2286
+ db.setTitle(m[1].trim());
2287
+ } else if (m = line.match(SUBDOMAIN_RE2)) {
2288
+ db.addSubdomain(m[1], m[2], m[3]);
2289
+ currentSubdomainId = m[1];
2290
+ } else if (m = line.match(BC_RE)) {
2291
+ const team = m[3] || void 0;
2292
+ db.addBoundedContext(m[1], m[2], currentSubdomainId, team);
2293
+ } else if (m = line.match(REL_RE2)) {
2294
+ db.addRelationship(m[1], m[2], m[3]);
2295
+ } else if (CLOSE_BRACE_RE.test(line)) {
2296
+ currentSubdomainId = void 0;
2297
+ }
2298
+ }
2299
+ }
2300
+
2301
+ // src/shared/context-relationship.ts
2302
+ var TABLE = [
2303
+ {
2304
+ keys: ["partnership", "p"],
2305
+ pattern: { name: "Partnership", badge: "P", kind: "symmetric", thick: true }
2306
+ },
2307
+ {
2308
+ keys: ["sharedkernel", "sk"],
2309
+ pattern: { name: "Shared Kernel", badge: "SK", kind: "symmetric", doubleStroke: true }
2310
+ },
2311
+ {
2312
+ keys: ["customersupplier", "customersupplier", "cs", "cusup"],
2313
+ pattern: {
2314
+ name: "Customer-Supplier",
2315
+ badge: "C/S",
2316
+ kind: "directional",
2317
+ upstreamRole: "S",
2318
+ downstreamRole: "C"
2319
+ }
2320
+ },
2321
+ {
2322
+ keys: ["conformist", "cf"],
2323
+ pattern: {
2324
+ name: "Conformist",
2325
+ badge: "CF",
2326
+ kind: "directional",
2327
+ upstreamRole: "U",
2328
+ downstreamRole: "D"
2329
+ }
2330
+ },
2331
+ {
2332
+ keys: ["anticorruptionlayer", "anticorruption", "acl"],
2333
+ pattern: {
2334
+ name: "Anti-Corruption Layer",
2335
+ badge: "ACL",
2336
+ kind: "directional",
2337
+ upstreamRole: "U",
2338
+ downstreamRole: "D",
2339
+ aclGate: true
2340
+ }
2341
+ },
2342
+ {
2343
+ keys: ["openhostservice", "ohs"],
2344
+ pattern: {
2345
+ name: "Open Host Service",
2346
+ badge: "OHS",
2347
+ kind: "directional",
2348
+ upstreamRole: "U",
2349
+ downstreamRole: "D",
2350
+ ohsSocket: true
2351
+ }
2352
+ },
2353
+ {
2354
+ keys: ["publishedlanguage", "pl"],
2355
+ pattern: {
2356
+ name: "Published Language",
2357
+ badge: "PL",
2358
+ kind: "directional",
2359
+ upstreamRole: "U",
2360
+ downstreamRole: "D"
2361
+ }
2362
+ },
2363
+ {
2364
+ keys: ["separateways", "sw"],
2365
+ pattern: { name: "Separate Ways", badge: "", kind: "none", dashed: true }
2366
+ },
2367
+ {
2368
+ keys: ["bigballofmud", "bbom", "bbm"],
2369
+ pattern: {
2370
+ name: "Big Ball of Mud",
2371
+ badge: "BBoM",
2372
+ kind: "directional",
2373
+ upstreamRole: "U",
2374
+ downstreamRole: "D"
2375
+ }
2376
+ }
2377
+ ];
2378
+ function normalize(label) {
2379
+ return label.trim().toLowerCase().replace(/[\s_/()-]+/g, "");
2380
+ }
2381
+ function classifyRelationship(label) {
2382
+ const norm = normalize(label);
2383
+ for (const { keys, pattern } of TABLE) {
2384
+ if (keys.includes(norm)) return pattern;
2385
+ }
2386
+ return null;
2387
+ }
2388
+
2389
+ // src/d5-subdomain/renderer.ts
2390
+ var REL_LABEL_MAX_WIDTH2 = 150;
2391
+ var REL_STROKE = "#64748b";
2392
+ var SVG_NS3 = "http://www.w3.org/2000/svg";
2393
+ function pointAlong(from, to, dist) {
2394
+ const dx = to.x - from.x;
2395
+ const dy = to.y - from.y;
2396
+ const len = Math.hypot(dx, dy) || 1;
2397
+ return { x: from.x + dx / len * dist, y: from.y + dy / len * dist };
2398
+ }
2399
+ function angleDeg(from, to) {
2400
+ return Math.atan2(to.y - from.y, to.x - from.x) * 180 / Math.PI;
2401
+ }
2402
+ function roleMarker(cx, cy, text) {
2403
+ const g = document.createElementNS(SVG_NS3, "g");
2404
+ g.setAttribute("class", "d5-rel-role");
2405
+ const c = document.createElementNS(SVG_NS3, "circle");
2406
+ c.setAttribute("cx", String(cx));
2407
+ c.setAttribute("cy", String(cy));
2408
+ c.setAttribute("r", "8");
2409
+ c.setAttribute("fill", "white");
2410
+ c.setAttribute("stroke", REL_STROKE);
2411
+ c.setAttribute("stroke-width", "1.25");
2412
+ g.appendChild(c);
2413
+ const t = document.createElementNS(SVG_NS3, "text");
2414
+ t.setAttribute("x", String(cx));
2415
+ t.setAttribute("y", String(cy + 3));
2416
+ t.setAttribute("text-anchor", "middle");
2417
+ t.setAttribute("font-size", "9");
2418
+ t.setAttribute("font-weight", "bold");
2419
+ t.setAttribute("fill", "#475569");
2420
+ t.textContent = text;
2421
+ g.appendChild(t);
2422
+ return g;
2423
+ }
2424
+ function patternBadge(cx, cy, text) {
2425
+ const g = document.createElementNS(SVG_NS3, "g");
2426
+ g.setAttribute("class", "d5-rel-badge");
2427
+ const w2 = Math.ceil(measureText(text, { size: 10, weight: 700 }) + 12);
2428
+ const h = 18;
2429
+ const rect = document.createElementNS(SVG_NS3, "rect");
2430
+ rect.setAttribute("x", String(cx - w2 / 2));
2431
+ rect.setAttribute("y", String(cy - h / 2));
2432
+ rect.setAttribute("width", String(w2));
2433
+ rect.setAttribute("height", String(h));
2434
+ rect.setAttribute("rx", "4");
2435
+ rect.setAttribute("fill", "#eef2ff");
2436
+ rect.setAttribute("stroke", "#c7d2fe");
2437
+ rect.setAttribute("stroke-width", "1");
2438
+ g.appendChild(rect);
2439
+ const t = document.createElementNS(SVG_NS3, "text");
2440
+ t.setAttribute("x", String(cx));
2441
+ t.setAttribute("y", String(cy + 3.5));
2442
+ t.setAttribute("text-anchor", "middle");
2443
+ t.setAttribute("font-size", "10");
2444
+ t.setAttribute("font-weight", "700");
2445
+ t.setAttribute("fill", "#4338ca");
2446
+ t.textContent = text;
2447
+ g.appendChild(t);
2448
+ return g;
2449
+ }
2450
+ function aclGate(cx, cy, deg) {
2451
+ const g = document.createElementNS(SVG_NS3, "g");
2452
+ g.setAttribute("class", "d5-rel-acl");
2453
+ g.setAttribute("transform", `translate(${cx} ${cy}) rotate(${deg})`);
2454
+ const rect = document.createElementNS(SVG_NS3, "rect");
2455
+ rect.setAttribute("x", "-9");
2456
+ rect.setAttribute("y", "-7");
2457
+ rect.setAttribute("width", "18");
2458
+ rect.setAttribute("height", "14");
2459
+ rect.setAttribute("rx", "2");
2460
+ rect.setAttribute("fill", "white");
2461
+ rect.setAttribute("stroke", REL_STROKE);
2462
+ rect.setAttribute("stroke-width", "1.5");
2463
+ g.appendChild(rect);
2464
+ for (const dx of [-3, 3]) {
2465
+ const tick = document.createElementNS(SVG_NS3, "line");
2466
+ tick.setAttribute("x1", String(dx));
2467
+ tick.setAttribute("y1", "-7");
2468
+ tick.setAttribute("x2", String(dx));
2469
+ tick.setAttribute("y2", "7");
2470
+ tick.setAttribute("stroke", REL_STROKE);
2471
+ tick.setAttribute("stroke-width", "1");
2472
+ g.appendChild(tick);
2473
+ }
2474
+ return g;
2475
+ }
2476
+ function ohsSocket(cx, cy) {
2477
+ const g = document.createElementNS(SVG_NS3, "g");
2478
+ g.setAttribute("class", "d5-rel-ohs");
2479
+ const c = document.createElementNS(SVG_NS3, "circle");
2480
+ c.setAttribute("cx", String(cx));
2481
+ c.setAttribute("cy", String(cy));
2482
+ c.setAttribute("r", "5");
2483
+ c.setAttribute("fill", "white");
2484
+ c.setAttribute("stroke", REL_STROKE);
2485
+ c.setAttribute("stroke-width", "1.5");
2486
+ g.appendChild(c);
2487
+ return g;
2488
+ }
2489
+ var BC_RX = 80;
2490
+ var BC_RY = 28;
2491
+ var BC_LABEL_WRAP_W = 150;
2492
+ var TITLE_HEIGHT2 = 40;
2493
+ var MARGIN = 30;
2494
+ var ARROW_MARKER_SIZE2 = 8;
2495
+ var BC_LABEL_FONT = 12;
2496
+ var BC_TEAM_FONT = 10;
2497
+ var LEGEND_H = 30;
2498
+ var SUBDOMAIN_LABEL_H = 24;
2499
+ var SUBDOMAIN_LABEL_FONT = 13;
2500
+ var CLUSTER_PADDING_X = 20;
2501
+ var CLUSTER_PADDING_Y_TOP = SUBDOMAIN_LABEL_H + 12;
2502
+ var CLUSTER_PADDING_Y_BOTTOM = 16;
2503
+ var NODESEP = 46;
2504
+ var RANKSEP = 96;
2505
+ var SUBDOMAIN_COLORS2 = {
2506
+ core: { fill: "#dbeafe", stroke: "#3b82f6" },
2507
+ supporting: { fill: "#fef9c3", stroke: "#ca8a04" },
2508
+ generic: { fill: "#e5e7eb", stroke: "#6b7280" }
2509
+ };
2510
+ var LEGEND_ITEMS2 = [
2511
+ { type: "core", label: "Core" },
2512
+ { type: "supporting", label: "Supporting" },
2513
+ { type: "generic", label: "Generic" }
2514
+ ];
2515
+ function addArrowMarker2(svg) {
2516
+ let defs = svg.querySelector("defs");
2517
+ if (!defs) {
2518
+ defs = document.createElementNS(SVG_NS3, "defs");
2519
+ svg.prepend(defs);
2520
+ }
2521
+ const marker = document.createElementNS(SVG_NS3, "marker");
2522
+ marker.setAttribute("id", "d5-arrowhead");
2523
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE2));
2524
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE2));
2525
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE2));
2526
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE2 / 2));
2527
+ marker.setAttribute("orient", "auto");
2528
+ const path = document.createElementNS(SVG_NS3, "path");
2529
+ path.setAttribute(
2530
+ "d",
2531
+ `M0,0 L${ARROW_MARKER_SIZE2},${ARROW_MARKER_SIZE2 / 2} L0,${ARROW_MARKER_SIZE2}`
2532
+ );
2533
+ path.setAttribute("fill", "#64748b");
2534
+ marker.appendChild(path);
2535
+ defs.appendChild(marker);
2536
+ }
2537
+ function generateCurvePath2(points) {
2538
+ if (!points || points.length === 0) return "";
2539
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
2540
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
2541
+ let d = `M ${points[0].x} ${points[0].y}`;
2542
+ for (let i = 1; i < points.length - 1; i++) {
2543
+ const p1 = points[i];
2544
+ const p2 = points[i + 1];
2545
+ const midX = (p1.x + p2.x) / 2;
2546
+ const midY = (p1.y + p2.y) / 2;
2547
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
2548
+ }
2549
+ const last = points[points.length - 1];
2550
+ d += ` L ${last.x} ${last.y}`;
2551
+ return d;
2552
+ }
2553
+ function render2(db, container) {
2554
+ addArrowMarker2(container);
2555
+ const title = db.getTitle();
2556
+ const subdomains = db.getSubdomains();
2557
+ const boundedContexts = db.getBoundedContexts();
2558
+ const relationships = db.getRelationships();
2559
+ const direction = db.getDirection();
2560
+ const horizontal = direction === "LR" || direction === "RL";
2561
+ const g = new To.graphlib.Graph({ compound: true });
2562
+ g.setGraph({
2563
+ rankdir: direction,
2564
+ nodesep: NODESEP,
2565
+ ranksep: horizontal ? RANKSEP : 64,
2566
+ edgesep: 30,
2567
+ acyclicer: "greedy",
2568
+ ranker: "network-simplex",
2569
+ marginx: 0,
2570
+ marginy: 0
2571
+ });
2572
+ g.setDefaultEdgeLabel(() => ({}));
2573
+ const subdomainIds = new Set(subdomains.map((s) => s.id));
2574
+ const bcBoxes = /* @__PURE__ */ new Map();
2575
+ boundedContexts.forEach((bc) => {
2576
+ const labelLines = wrapText(bc.label, BC_LABEL_WRAP_W, {
2577
+ size: BC_LABEL_FONT,
2578
+ weight: 600
2579
+ });
2580
+ const widest = labelLines.reduce(
2581
+ (m, l) => Math.max(m, measureText(l, { size: BC_LABEL_FONT, weight: 600 })),
2582
+ 0
2583
+ );
2584
+ const teamW = bc.team ? measureText(bc.team, { size: BC_TEAM_FONT }) : 0;
2585
+ const contentW = Math.max(widest, teamW);
2586
+ const contentH = labelLines.length * lineHeight(BC_LABEL_FONT) + (bc.team ? lineHeight(BC_TEAM_FONT) : 0);
2587
+ const rx = Math.round(Math.max(BC_RX, contentW / 1.55 + 16));
2588
+ const ry = Math.round(Math.max(BC_RY, contentH / 1.5 + 12));
2589
+ bcBoxes.set(bc.id, { rx, ry, labelLines });
2590
+ });
2591
+ subdomains.forEach((sd) => {
2592
+ const labelW = measureText(sd.label, {
2593
+ size: SUBDOMAIN_LABEL_FONT,
2594
+ weight: 600
2595
+ });
2596
+ const padX = Math.max(CLUSTER_PADDING_X, Math.ceil((labelW + 28 - 2 * BC_RX) / 2));
2597
+ g.setNode(sd.id, {
2598
+ label: sd.label,
2599
+ clusterLabelPos: "top",
2600
+ paddingTop: CLUSTER_PADDING_Y_TOP,
2601
+ paddingBottom: CLUSTER_PADDING_Y_BOTTOM,
2602
+ paddingLeft: padX,
2603
+ paddingRight: padX
2604
+ });
2605
+ });
2606
+ boundedContexts.forEach((bc) => {
2607
+ const box = bcBoxes.get(bc.id);
2608
+ g.setNode(bc.id, { width: box.rx * 2, height: box.ry * 2 });
2609
+ if (bc.subdomainId && subdomainIds.has(bc.subdomainId)) {
2610
+ g.setParent(bc.id, bc.subdomainId);
2611
+ }
2612
+ });
2613
+ relationships.forEach((rel) => {
2614
+ const edgeCfg = { minlen: 1 };
2615
+ if (rel.label) {
2616
+ const pattern = classifyRelationship(rel.label);
2617
+ const { w: w2, h } = pattern ? { w: pattern.badge ? Math.ceil(measureText(pattern.badge, { size: 10, weight: 700 }) + 16) : 8, h: 20 } : edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH2);
2618
+ edgeCfg.width = w2;
2619
+ edgeCfg.height = h;
2620
+ edgeCfg.labelpos = "c";
2621
+ }
2622
+ g.setEdge(rel.source, rel.target, edgeCfg);
2623
+ });
2624
+ To.layout(g);
2625
+ const graphW = g.graph().width || 0;
2626
+ const graphH = g.graph().height || 0;
2627
+ const usedPatterns = [];
2628
+ for (const rel of relationships) {
2629
+ const p2 = rel.label ? classifyRelationship(rel.label) : null;
2630
+ if (p2 && !usedPatterns.some((u) => u.name === p2.name)) usedPatterns.push(p2);
2631
+ }
2632
+ const gridStartX = MARGIN;
2633
+ const gridStartY = MARGIN + (title ? TITLE_HEIGHT2 : 0);
2634
+ const totalW = gridStartX + graphW + MARGIN;
2635
+ const totalH = gridStartY + graphH + LEGEND_H * (usedPatterns.length > 0 ? 2 : 1) + MARGIN;
2636
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
2637
+ container.setAttribute("height", String(totalH));
2638
+ container.style.maxWidth = `${totalW}px`;
2639
+ if (title) {
2640
+ const titleEl = document.createElementNS(SVG_NS3, "text");
2641
+ titleEl.setAttribute("class", "d5-title");
2642
+ titleEl.setAttribute("x", String(totalW / 2));
2643
+ titleEl.setAttribute("y", "24");
2644
+ titleEl.setAttribute("text-anchor", "middle");
2645
+ titleEl.setAttribute("font-size", "18");
2646
+ titleEl.setAttribute("font-weight", "bold");
2647
+ titleEl.setAttribute("fill", "#1e293b");
2648
+ titleEl.textContent = title;
2649
+ container.appendChild(titleEl);
2650
+ }
2651
+ subdomains.forEach((sd) => {
2652
+ const node = g.node(sd.id);
2653
+ if (!node) return;
2654
+ const colors = SUBDOMAIN_COLORS2[sd.type];
2655
+ const w2 = node.width;
2656
+ const h = node.height;
2657
+ const x2 = gridStartX + node.x - w2 / 2;
2658
+ const y = gridStartY + node.y - h / 2;
2659
+ const group = document.createElementNS(SVG_NS3, "g");
2660
+ group.setAttribute("class", `d5-subdomain d5-subdomain-${sd.type}`);
2661
+ const rect = document.createElementNS(SVG_NS3, "rect");
2662
+ rect.setAttribute("x", String(x2));
2663
+ rect.setAttribute("y", String(y));
2664
+ rect.setAttribute("width", String(w2));
2665
+ rect.setAttribute("height", String(h));
2666
+ rect.setAttribute("rx", "12");
2667
+ rect.setAttribute("fill", colors.fill);
2668
+ rect.setAttribute("stroke", colors.stroke);
2669
+ rect.setAttribute("stroke-width", "2");
2670
+ rect.setAttribute("stroke-dasharray", "8 4");
2671
+ group.appendChild(rect);
2672
+ const labelText = document.createElementNS(SVG_NS3, "text");
2673
+ labelText.setAttribute("x", String(x2 + w2 / 2));
2674
+ labelText.setAttribute("y", String(y + 20));
2675
+ labelText.setAttribute("text-anchor", "middle");
2676
+ labelText.setAttribute("font-size", "13");
2677
+ labelText.setAttribute("font-weight", "600");
2678
+ labelText.setAttribute("fill", "#1e293b");
2679
+ labelText.textContent = sd.label;
2680
+ group.appendChild(labelText);
2681
+ container.appendChild(group);
2682
+ });
2683
+ boundedContexts.forEach((bc) => {
2684
+ const node = g.node(bc.id);
2685
+ if (!node) return;
2686
+ const sd = subdomains.find((s) => s.id === bc.subdomainId);
2687
+ const colors = sd ? SUBDOMAIN_COLORS2[sd.type] : { stroke: "#64748b" };
2688
+ const cx = gridStartX + node.x;
2689
+ const cy = gridStartY + node.y;
2690
+ const box = bcBoxes.get(bc.id);
2691
+ const bcGroup = document.createElementNS(SVG_NS3, "g");
2692
+ bcGroup.setAttribute("class", "d5-bounded-context");
2693
+ const ellipse = document.createElementNS(SVG_NS3, "ellipse");
2694
+ ellipse.setAttribute("cx", String(cx));
2695
+ ellipse.setAttribute("cy", String(cy));
2696
+ ellipse.setAttribute("rx", String(box.rx));
2697
+ ellipse.setAttribute("ry", String(box.ry));
2698
+ ellipse.setAttribute("fill", "white");
2699
+ ellipse.setAttribute("stroke", colors.stroke);
2700
+ ellipse.setAttribute("stroke-width", "2");
2701
+ bcGroup.appendChild(ellipse);
2702
+ const labelLH = lineHeight(BC_LABEL_FONT);
2703
+ const teamLH = bc.team ? lineHeight(BC_TEAM_FONT) : 0;
2704
+ const blockH = box.labelLines.length * labelLH + teamLH;
2705
+ let ty = cy - blockH / 2 + BC_LABEL_FONT * 0.85;
2706
+ box.labelLines.forEach((ln2) => {
2707
+ const bcLabel = document.createElementNS(SVG_NS3, "text");
2708
+ bcLabel.setAttribute("x", String(cx));
2709
+ bcLabel.setAttribute("y", String(ty));
2710
+ bcLabel.setAttribute("text-anchor", "middle");
2711
+ bcLabel.setAttribute("font-size", String(BC_LABEL_FONT));
2712
+ bcLabel.setAttribute("font-weight", "600");
2713
+ bcLabel.setAttribute("fill", "#1e293b");
2714
+ bcLabel.textContent = ln2;
2715
+ bcGroup.appendChild(bcLabel);
2716
+ ty += labelLH;
2717
+ });
2718
+ if (bc.team) {
2719
+ const teamLabel = document.createElementNS(SVG_NS3, "text");
2720
+ teamLabel.setAttribute("x", String(cx));
2721
+ teamLabel.setAttribute("y", String(ty + 2));
2722
+ teamLabel.setAttribute("text-anchor", "middle");
2723
+ teamLabel.setAttribute("font-size", String(BC_TEAM_FONT));
2724
+ teamLabel.setAttribute("font-style", "italic");
2725
+ teamLabel.setAttribute("fill", "#64748b");
2726
+ teamLabel.textContent = bc.team;
2727
+ bcGroup.appendChild(teamLabel);
2728
+ }
2729
+ container.appendChild(bcGroup);
2730
+ });
2731
+ relationships.forEach((rel) => {
2732
+ const edge = g.edge(rel.source, rel.target);
2733
+ if (!edge || !edge.points || edge.points.length === 0) return;
2734
+ const shiftedPoints = edge.points.map((p2) => ({
2735
+ x: gridStartX + p2.x,
2736
+ y: gridStartY + p2.y
2737
+ }));
2738
+ const pattern = rel.label ? classifyRelationship(rel.label) : null;
2739
+ const symmetric = !pattern || pattern.kind !== "directional";
2740
+ const group = document.createElementNS(SVG_NS3, "g");
2741
+ group.setAttribute("class", pattern ? `d5-rel d5-rel-${pattern.badge || "sw"}` : "d5-rel");
2742
+ if (pattern) {
2743
+ const title2 = document.createElementNS(SVG_NS3, "title");
2744
+ title2.textContent = pattern.name;
2745
+ group.appendChild(title2);
2746
+ }
2747
+ const strokeW = pattern?.thick ? 3 : pattern?.doubleStroke ? 4 : 1.5;
2748
+ const path = document.createElementNS(SVG_NS3, "path");
2749
+ path.setAttribute("d", generateCurvePath2(shiftedPoints));
2750
+ path.setAttribute("fill", "none");
2751
+ path.setAttribute("stroke", REL_STROKE);
2752
+ path.setAttribute("stroke-width", String(strokeW));
2753
+ if (pattern?.dashed) path.setAttribute("stroke-dasharray", "7 5");
2754
+ if (!symmetric) path.setAttribute("marker-end", "url(#d5-arrowhead)");
2755
+ group.appendChild(path);
2756
+ if (pattern?.doubleStroke) {
2757
+ const inner = document.createElementNS(SVG_NS3, "path");
2758
+ inner.setAttribute("d", generateCurvePath2(shiftedPoints));
2759
+ inner.setAttribute("fill", "none");
2760
+ inner.setAttribute("stroke", "white");
2761
+ inner.setAttribute("stroke-width", "1.5");
2762
+ group.appendChild(inner);
2763
+ }
2764
+ if (pattern && pattern.kind === "directional" && shiftedPoints.length >= 2) {
2765
+ const p0 = shiftedPoints[0];
2766
+ const p1 = shiftedPoints[1];
2767
+ const pN = shiftedPoints[shiftedPoints.length - 1];
2768
+ const pN1 = shiftedPoints[shiftedPoints.length - 2];
2769
+ if (pattern.upstreamRole) {
2770
+ const m = pointAlong(p0, p1, 16);
2771
+ group.appendChild(roleMarker(m.x, m.y, pattern.upstreamRole));
2772
+ }
2773
+ if (pattern.downstreamRole) {
2774
+ const m = pointAlong(pN, pN1, 20);
2775
+ group.appendChild(roleMarker(m.x, m.y, pattern.downstreamRole));
2776
+ }
2777
+ if (pattern.aclGate) {
2778
+ const m = pointAlong(pN, pN1, 40);
2779
+ group.appendChild(aclGate(m.x, m.y, angleDeg(pN1, pN)));
2780
+ }
2781
+ if (pattern.ohsSocket) {
2782
+ const m = pointAlong(p0, p1, 34);
2783
+ group.appendChild(ohsSocket(m.x, m.y));
2784
+ }
2785
+ }
2786
+ if (rel.label) {
2787
+ let lx;
2788
+ let ly;
2789
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
2790
+ lx = gridStartX + edge.x;
2791
+ ly = gridStartY + edge.y;
2792
+ } else {
2793
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
2794
+ lx = mid.x;
2795
+ ly = mid.y;
2796
+ }
2797
+ if (pattern && pattern.badge) {
2798
+ group.appendChild(patternBadge(lx, ly, pattern.badge));
2799
+ } else if (!pattern) {
2800
+ group.appendChild(
2801
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH2 })
2802
+ );
2803
+ }
2804
+ }
2805
+ container.appendChild(group);
2806
+ });
2807
+ const legendGroup = document.createElementNS(SVG_NS3, "g");
2808
+ legendGroup.setAttribute("class", "d5-legend");
2809
+ const legendY = totalH - LEGEND_H * (usedPatterns.length > 0 ? 2 : 1);
2810
+ let legendX = MARGIN;
2811
+ LEGEND_ITEMS2.forEach((item) => {
2812
+ const colors = SUBDOMAIN_COLORS2[item.type];
2813
+ const swatch = document.createElementNS(SVG_NS3, "rect");
2814
+ swatch.setAttribute("x", String(legendX));
2815
+ swatch.setAttribute("y", String(legendY));
2816
+ swatch.setAttribute("width", "14");
2817
+ swatch.setAttribute("height", "14");
2818
+ swatch.setAttribute("rx", "3");
2819
+ swatch.setAttribute("fill", colors.fill);
2820
+ swatch.setAttribute("stroke", colors.stroke);
2821
+ swatch.setAttribute("stroke-width", "1.5");
2822
+ legendGroup.appendChild(swatch);
2823
+ const label = document.createElementNS(SVG_NS3, "text");
2824
+ label.setAttribute("x", String(legendX + 20));
2825
+ label.setAttribute("y", String(legendY + 11));
2826
+ label.setAttribute("font-size", "11");
2827
+ label.setAttribute("fill", "#64748b");
2828
+ label.textContent = item.label;
2829
+ legendGroup.appendChild(label);
2830
+ legendX += 90;
2831
+ });
2832
+ if (usedPatterns.length > 0) {
2833
+ let px = MARGIN;
2834
+ const py = legendY + LEGEND_H;
2835
+ usedPatterns.forEach((p2) => {
2836
+ if (p2.badge) {
2837
+ legendGroup.appendChild(patternBadge(px + 15, py + 7, p2.badge));
2838
+ px += 34;
2839
+ } else {
2840
+ const dash = document.createElementNS(SVG_NS3, "line");
2841
+ dash.setAttribute("x1", String(px));
2842
+ dash.setAttribute("y1", String(py + 7));
2843
+ dash.setAttribute("x2", String(px + 22));
2844
+ dash.setAttribute("y2", String(py + 7));
2845
+ dash.setAttribute("stroke", REL_STROKE);
2846
+ dash.setAttribute("stroke-dasharray", "4 3");
2847
+ legendGroup.appendChild(dash);
2848
+ px += 28;
2849
+ }
2850
+ const label = document.createElementNS(SVG_NS3, "text");
2851
+ label.setAttribute("x", String(px + 4));
2852
+ label.setAttribute("y", String(py + 11));
2853
+ label.setAttribute("font-size", "11");
2854
+ label.setAttribute("fill", "#64748b");
2855
+ label.textContent = p2.name;
2856
+ legendGroup.appendChild(label);
2857
+ px += measureText(p2.name, { size: 11 }) + 22;
2858
+ });
2859
+ }
2860
+ container.appendChild(legendGroup);
2861
+ }
2862
+
2863
+ // src/d5-context/detector.ts
2864
+ function detectContext(text) {
2865
+ return text.trimStart().startsWith("d5-context");
2866
+ }
2867
+
2868
+ // src/d5-context/db.ts
2869
+ var DEFAULT_DIRECTION3 = "TB";
2870
+ var D5ContextDb = class {
2871
+ boundedContext;
2872
+ aggregates = [];
2873
+ readModels = [];
2874
+ terms = [];
2875
+ relationships = [];
2876
+ events = [];
2877
+ policies = [];
2878
+ direction = DEFAULT_DIRECTION3;
2879
+ title;
2880
+ accTitle;
2881
+ accDescription;
2882
+ setTitle(title) {
2883
+ this.title = title;
2884
+ }
2885
+ getTitle() {
2886
+ return this.title;
2887
+ }
2888
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
2889
+ setDirection(dir) {
2890
+ const normalized = normalizeDirection(dir);
2891
+ if (normalized) this.direction = normalized;
2892
+ }
2893
+ getDirection() {
2894
+ return this.direction;
2895
+ }
2896
+ setBoundedContext(id, label, team) {
2897
+ this.boundedContext = { id, label, team };
2898
+ }
2899
+ getBoundedContext() {
2900
+ return this.boundedContext;
2901
+ }
2902
+ addAggregate(id, label, root, fields) {
2903
+ this.aggregates.push({ id, label, root, fields });
2904
+ }
2905
+ getAggregates() {
2906
+ return this.aggregates;
2907
+ }
2908
+ addReadModel(id, label) {
2909
+ this.readModels.push({ id, label });
2910
+ }
2911
+ getReadModels() {
2912
+ return this.readModels;
2913
+ }
2914
+ addTerm(term, definition) {
2915
+ this.terms.push({ term, definition });
2916
+ }
2917
+ getTerms() {
2918
+ return this.terms;
2919
+ }
2920
+ addRelationship(source, target, label) {
2921
+ this.relationships.push({ source, target, label });
2922
+ }
2923
+ getRelationships() {
2924
+ return this.relationships;
2925
+ }
2926
+ addEvent(source, target, name) {
2927
+ this.events.push({ source, target, name });
2928
+ }
2929
+ getEvents() {
2930
+ return this.events;
2931
+ }
2932
+ addPolicy(source, target, rule) {
2933
+ this.policies.push({ source, target, rule });
2934
+ }
2935
+ getPolicies() {
2936
+ return this.policies;
2937
+ }
2938
+ clear() {
2939
+ this.boundedContext = void 0;
2940
+ this.aggregates = [];
2941
+ this.readModels = [];
2942
+ this.terms = [];
2943
+ this.relationships = [];
2944
+ this.events = [];
2945
+ this.policies = [];
2946
+ this.direction = DEFAULT_DIRECTION3;
2947
+ this.title = void 0;
2948
+ this.accTitle = void 0;
2949
+ this.accDescription = void 0;
2950
+ }
2951
+ // Mermaid DiagramDB interface methods
2952
+ setDiagramTitle(title) {
2953
+ this.setTitle(title);
2954
+ }
2955
+ getDiagramTitle() {
2956
+ return this.title ?? "";
2957
+ }
2958
+ setAccTitle(title) {
2959
+ this.accTitle = title;
2960
+ }
2961
+ getAccTitle() {
2962
+ return this.accTitle ?? "";
2963
+ }
2964
+ setAccDescription(desc) {
2965
+ this.accDescription = desc;
2966
+ }
2967
+ getAccDescription() {
2968
+ return this.accDescription ?? "";
2969
+ }
2970
+ };
2971
+
2972
+ // src/d5-context/parser.ts
2973
+ var TITLE_RE3 = /^\s*title\s+(.+)$/;
2974
+ var BC_RE2 = /^\s*BoundedContext\(\s*(\w+)\s*,\s*"([^"]+)"\s*(?:,\s*team:\s*"([^"]+)")?\s*\)\s*\{?/;
2975
+ var LANGUAGE_RE = /^\s*Language\s*\{/;
2976
+ var TERM_RE = /^\s*Term\(\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\)/;
2977
+ var AGGREGATE_RE = /^\s*Aggregate\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*root:\s*"([^"]+)"(?:\s*,\s*fields:\s*"([^"]+)")?\s*\)/;
2978
+ var READMODEL_RE = /^\s*ReadModel\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2979
+ var REL_RE3 = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2980
+ var EVENT_RE = /^\s*Event\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2981
+ var POLICY_RE = /^\s*Policy\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2982
+ var COMMENT_LINE_RE3 = /^\s*%%/;
2983
+ function stripInlineComment3(line) {
2984
+ const idx = line.indexOf("%%");
2985
+ return idx === -1 ? line : line.slice(0, idx);
2986
+ }
2987
+ function parse3(text, db) {
2988
+ const lines = text.split("\n");
2989
+ for (const raw of lines) {
2990
+ if (COMMENT_LINE_RE3.test(raw)) continue;
2991
+ const line = stripInlineComment3(raw).trim();
2992
+ if (line === "" || line === "d5-context") continue;
2993
+ let m;
2994
+ if (m = line.match(DIRECTION_RE)) {
2995
+ db.setDirection(m[1]);
2996
+ } else if (m = line.match(TITLE_RE3)) {
2997
+ db.setTitle(m[1].trim());
2998
+ } else if (m = line.match(BC_RE2)) {
2999
+ const team = m[3] || void 0;
3000
+ db.setBoundedContext(m[1], m[2], team);
3001
+ } else if (LANGUAGE_RE.test(line)) ; else if (m = line.match(TERM_RE)) {
3002
+ db.addTerm(m[1], m[2]);
3003
+ } else if (m = line.match(AGGREGATE_RE)) {
3004
+ const fields = m[4] ? m[4].split(",").map((s) => s.trim()) : void 0;
3005
+ db.addAggregate(m[1], m[2], m[3], fields);
3006
+ } else if (m = line.match(READMODEL_RE)) {
3007
+ db.addReadModel(m[1], m[2]);
3008
+ } else if (m = line.match(REL_RE3)) {
3009
+ db.addRelationship(m[1], m[2], m[3]);
3010
+ } else if (m = line.match(EVENT_RE)) {
3011
+ db.addEvent(m[1], m[2], m[3]);
3012
+ } else if (m = line.match(POLICY_RE)) {
3013
+ db.addPolicy(m[1], m[2], m[3]);
3014
+ }
3015
+ }
3016
+ }
3017
+
3018
+ // src/d5-context/renderer.ts
3019
+ var REL_LABEL_MAX_WIDTH3 = 150;
3020
+ var SVG_NS4 = "http://www.w3.org/2000/svg";
3021
+ var AGGREGATE_MIN_WIDTH = 180;
3022
+ var AGGREGATE_MAX_WIDTH = 340;
3023
+ var AGGREGATE_MIN_HEIGHT = 76;
3024
+ var AGGREGATE_FIELD_HEIGHT = 20;
3025
+ var READMODEL_HEIGHT = 74;
3026
+ var GRID_GAP = 60;
3027
+ var MARGIN2 = 30;
3028
+ var TITLE_HEIGHT3 = 34;
3029
+ var BC_PADDING = 30;
3030
+ var BC_HEADER = 36;
3031
+ var ARROW_MARKER_SIZE3 = 8;
3032
+ var NOTE_TEXT_W = 300;
3033
+ var NOTE_PAD = 14;
3034
+ var NOTE_WIDTH = NOTE_TEXT_W + NOTE_PAD * 2;
3035
+ var NOTE_HEADER_H = 30;
3036
+ var NOTE_TERM_GAP = 12;
3037
+ var NOTE_TERM_FONT = { size: 12, weight: 600 };
3038
+ var NOTE_DEF_FONT = { size: 11 };
3039
+ var EVENT_STROKE = "#d97706";
3040
+ var POLICY_STROKE = "#7c3aed";
3041
+ var POLICY_MAX_W = 172;
3042
+ function addArrowMarker3(svg) {
3043
+ let defs = svg.querySelector("defs");
3044
+ if (!defs) {
3045
+ defs = document.createElementNS(SVG_NS4, "defs");
3046
+ svg.prepend(defs);
3047
+ }
3048
+ for (const [id, fill] of [
3049
+ ["d5-arrowhead", "#64748b"],
3050
+ ["d5-event-arrowhead", EVENT_STROKE],
3051
+ ["d5-policy-arrowhead", POLICY_STROKE]
3052
+ ]) {
3053
+ const marker = document.createElementNS(SVG_NS4, "marker");
3054
+ marker.setAttribute("id", id);
3055
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE3));
3056
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE3));
3057
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE3));
3058
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE3 / 2));
3059
+ marker.setAttribute("orient", "auto");
3060
+ const path = document.createElementNS(SVG_NS4, "path");
3061
+ path.setAttribute(
3062
+ "d",
3063
+ `M0,0 L${ARROW_MARKER_SIZE3},${ARROW_MARKER_SIZE3 / 2} L0,${ARROW_MARKER_SIZE3}`
3064
+ );
3065
+ path.setAttribute("fill", fill);
3066
+ marker.appendChild(path);
3067
+ defs.appendChild(marker);
3068
+ }
3069
+ }
3070
+ function eventRibbon(cx, cy, name) {
3071
+ const g = document.createElementNS(SVG_NS4, "g");
3072
+ g.setAttribute("class", "d5-event-label");
3073
+ const padX = 8;
3074
+ const point = 7;
3075
+ const w2 = Math.ceil(measureText(name, { size: 10, weight: 600 }) + padX * 2 + point);
3076
+ const h = 18;
3077
+ const x2 = cx - w2 / 2;
3078
+ const y = cy - h / 2;
3079
+ const tag = document.createElementNS(SVG_NS4, "path");
3080
+ tag.setAttribute(
3081
+ "d",
3082
+ `M${x2},${y} L${x2 + w2 - point},${y} L${x2 + w2},${cy} L${x2 + w2 - point},${y + h} L${x2},${y + h} Z`
3083
+ );
3084
+ tag.setAttribute("fill", "#fef3c7");
3085
+ tag.setAttribute("stroke", EVENT_STROKE);
3086
+ tag.setAttribute("stroke-width", "1");
3087
+ g.appendChild(tag);
3088
+ const t = document.createElementNS(SVG_NS4, "text");
3089
+ t.setAttribute("x", String(x2 + (w2 - point) / 2));
3090
+ t.setAttribute("y", String(cy + 3.5));
3091
+ t.setAttribute("text-anchor", "middle");
3092
+ t.setAttribute("font-size", "10");
3093
+ t.setAttribute("font-weight", "600");
3094
+ t.setAttribute("fill", "#92400e");
3095
+ t.textContent = name;
3096
+ g.appendChild(t);
3097
+ return g;
3098
+ }
3099
+ function eventRibbonSize(name) {
3100
+ return { w: Math.ceil(measureText(name, { size: 10, weight: 600 }) + 23), h: 20 };
3101
+ }
3102
+ var POLICY_FONT = { size: 10, weight: 600 };
3103
+ var POLICY_PAD_X = 9;
3104
+ var POLICY_PAD_Y = 4;
3105
+ function policyLayout(rule) {
3106
+ const lines = wrapText(rule, POLICY_MAX_W - POLICY_PAD_X * 2, POLICY_FONT);
3107
+ const textW = lines.reduce((m, l) => Math.max(m, measureText(l, POLICY_FONT)), 1);
3108
+ return {
3109
+ lines,
3110
+ w: Math.ceil(textW + POLICY_PAD_X * 2),
3111
+ h: Math.ceil(lines.length * lineHeight(POLICY_FONT.size) + POLICY_PAD_Y * 2)
3112
+ };
3113
+ }
3114
+ function policyTag(cx, cy, rule) {
3115
+ const { lines, w: w2, h } = policyLayout(rule);
3116
+ const g = document.createElementNS(SVG_NS4, "g");
3117
+ g.setAttribute("class", "d5-policy-label");
3118
+ const rect = document.createElementNS(SVG_NS4, "rect");
3119
+ rect.setAttribute("x", String(cx - w2 / 2));
3120
+ rect.setAttribute("y", String(cy - h / 2));
3121
+ rect.setAttribute("width", String(w2));
3122
+ rect.setAttribute("height", String(h));
3123
+ rect.setAttribute("rx", "4");
3124
+ rect.setAttribute("fill", "#ede9fe");
3125
+ rect.setAttribute("stroke", POLICY_STROKE);
3126
+ rect.setAttribute("stroke-width", "1");
3127
+ g.appendChild(rect);
3128
+ const lh = lineHeight(POLICY_FONT.size);
3129
+ let ty = cy - h / 2 + POLICY_PAD_Y + POLICY_FONT.size * 0.82;
3130
+ for (const ln2 of lines) {
3131
+ const t = document.createElementNS(SVG_NS4, "text");
3132
+ t.setAttribute("x", String(cx));
3133
+ t.setAttribute("y", String(ty));
3134
+ t.setAttribute("text-anchor", "middle");
3135
+ t.setAttribute("font-size", String(POLICY_FONT.size));
3136
+ t.setAttribute("font-weight", "600");
3137
+ t.setAttribute("fill", "#5b21b6");
3138
+ t.textContent = ln2;
3139
+ g.appendChild(t);
3140
+ ty += lh;
3141
+ }
3142
+ return g;
3143
+ }
3144
+ function generateCurvePath3(points) {
3145
+ if (!points || points.length === 0) return "";
3146
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
3147
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
3148
+ let d = `M ${points[0].x} ${points[0].y}`;
3149
+ for (let i = 1; i < points.length - 1; i++) {
3150
+ const p1 = points[i];
3151
+ const p2 = points[i + 1];
3152
+ const midX = (p1.x + p2.x) / 2;
3153
+ const midY = (p1.y + p2.y) / 2;
3154
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
3155
+ }
3156
+ const last = points[points.length - 1];
3157
+ d += ` L ${last.x} ${last.y}`;
3158
+ return d;
3159
+ }
3160
+ function render3(db, container) {
3161
+ addArrowMarker3(container);
3162
+ const title = db.getTitle();
3163
+ const bc = db.getBoundedContext();
3164
+ const aggregates = db.getAggregates();
3165
+ const terms = db.getTerms();
3166
+ const relationships = db.getRelationships();
3167
+ const events = db.getEvents();
3168
+ const policies = db.getPolicies();
3169
+ const readModels = db.getReadModels();
3170
+ const direction = db.getDirection();
3171
+ const g = new To.graphlib.Graph({ multigraph: true });
3172
+ g.setGraph({
3173
+ rankdir: direction,
3174
+ nodesep: GRID_GAP,
3175
+ ranksep: GRID_GAP,
3176
+ edgesep: 20,
3177
+ marginx: 0,
3178
+ marginy: 0
3179
+ });
3180
+ g.setDefaultEdgeLabel(() => ({}));
3181
+ aggregates.forEach((agg) => {
3182
+ const h = AGGREGATE_MIN_HEIGHT + (agg.fields?.length ? agg.fields.length * AGGREGATE_FIELD_HEIGHT + 10 : 0);
3183
+ const candidates = [
3184
+ measureText(agg.label, { size: 13, weight: 600 }) + 28,
3185
+ measureText(`Root: ${agg.root}`, { size: 11 }) + 28,
3186
+ ...(agg.fields ?? []).map((f) => measureText(f, { size: 12 }) + 44)
3187
+ ];
3188
+ const w2 = Math.round(
3189
+ Math.min(AGGREGATE_MAX_WIDTH, Math.max(AGGREGATE_MIN_WIDTH, ...candidates))
3190
+ );
3191
+ g.setNode(agg.id, { width: w2, height: h });
3192
+ });
3193
+ readModels.forEach((rm) => {
3194
+ const w2 = Math.round(
3195
+ Math.min(
3196
+ AGGREGATE_MAX_WIDTH,
3197
+ Math.max(AGGREGATE_MIN_WIDTH, measureText(rm.label, { size: 13, weight: 600 }) + 28)
3198
+ )
3199
+ );
3200
+ g.setNode(rm.id, { width: w2, height: READMODEL_HEIGHT });
3201
+ });
3202
+ relationships.forEach((rel) => {
3203
+ const edgeCfg = { minlen: 1 };
3204
+ if (rel.label) {
3205
+ const { w: w2, h } = edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH3);
3206
+ edgeCfg.width = w2;
3207
+ edgeCfg.height = h;
3208
+ edgeCfg.labelpos = "c";
3209
+ }
3210
+ g.setEdge(rel.source, rel.target, edgeCfg);
3211
+ });
3212
+ events.forEach((ev, i) => {
3213
+ const { w: w2, h } = eventRibbonSize(ev.name);
3214
+ g.setEdge(ev.source, ev.target, { minlen: 1, width: w2, height: h, labelpos: "c" }, `evt${i}`);
3215
+ });
3216
+ policies.forEach((p2, i) => {
3217
+ const { w: w2, h } = policyLayout(p2.rule);
3218
+ g.setEdge(p2.source, p2.target, { minlen: 1, width: w2, height: h, labelpos: "c" }, `pol${i}`);
3219
+ });
3220
+ To.layout(g);
3221
+ let graphW = g.graph().width || 0;
3222
+ let graphH = g.graph().height || 0;
3223
+ const hasTerms = terms.length > 0;
3224
+ const termBlocks = terms.map((t) => ({
3225
+ nameLines: wrapText(`${t.term}`, NOTE_TEXT_W, NOTE_TERM_FONT),
3226
+ defLines: wrapText(t.definition, NOTE_TEXT_W, NOTE_DEF_FONT)
3227
+ }));
3228
+ let noteH = 0;
3229
+ if (hasTerms) {
3230
+ noteH = NOTE_HEADER_H + NOTE_PAD;
3231
+ for (const b of termBlocks) {
3232
+ noteH += b.nameLines.length * lineHeight(NOTE_TERM_FONT.size) + 2 + b.defLines.length * lineHeight(NOTE_DEF_FONT.size) + NOTE_TERM_GAP;
3233
+ }
3234
+ noteH += NOTE_PAD - NOTE_TERM_GAP;
3235
+ }
3236
+ const innerGridW = graphW + (hasTerms ? (graphW > 0 ? GRID_GAP : 0) + NOTE_WIDTH : 0);
3237
+ const innerGridH = Math.max(graphH, noteH);
3238
+ const gridYOffset = Math.max(0, (innerGridH - graphH) / 2);
3239
+ let bcX = MARGIN2;
3240
+ const bcY = MARGIN2 + (title ? TITLE_HEIGHT3 : 0);
3241
+ let bcW = innerGridW + BC_PADDING * 2;
3242
+ if (!bc && !hasTerms && aggregates.length === 0) bcW = 400;
3243
+ const bcH = (bc ? BC_HEADER : 0) + innerGridH + BC_PADDING * 2;
3244
+ const totalW = bcX + bcW + MARGIN2;
3245
+ const totalH = bcY + bcH + MARGIN2;
3246
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
3247
+ container.setAttribute("height", String(totalH));
3248
+ container.style.maxWidth = `${totalW}px`;
3249
+ if (title) {
3250
+ const titleEl = document.createElementNS(SVG_NS4, "text");
3251
+ titleEl.setAttribute("class", "d5-title");
3252
+ titleEl.setAttribute("x", String(totalW / 2));
3253
+ titleEl.setAttribute("y", "24");
3254
+ titleEl.setAttribute("text-anchor", "middle");
3255
+ titleEl.setAttribute("font-size", "18");
3256
+ titleEl.setAttribute("font-weight", "bold");
3257
+ titleEl.setAttribute("fill", "#1e293b");
3258
+ titleEl.textContent = title;
3259
+ container.appendChild(titleEl);
3260
+ }
3261
+ const bcGroup = document.createElementNS(SVG_NS4, "g");
3262
+ bcGroup.setAttribute("class", "d5-bounded-context");
3263
+ const bcRect = document.createElementNS(SVG_NS4, "rect");
3264
+ bcRect.setAttribute("x", String(bcX));
3265
+ bcRect.setAttribute("y", String(bcY));
3266
+ bcRect.setAttribute("width", String(bcW));
3267
+ bcRect.setAttribute("height", String(bcH));
3268
+ bcRect.setAttribute("rx", "12");
3269
+ bcRect.setAttribute("fill", "#f8fafc");
3270
+ bcRect.setAttribute("stroke", "#94a3b8");
3271
+ bcRect.setAttribute("stroke-width", "2");
3272
+ bcRect.setAttribute("stroke-dasharray", "8 4");
3273
+ bcGroup.appendChild(bcRect);
3274
+ if (bc) {
3275
+ const bcLabel = document.createElementNS(SVG_NS4, "text");
3276
+ bcLabel.setAttribute("x", String(bcX + 16));
3277
+ bcLabel.setAttribute("y", String(bcY + 24));
3278
+ bcLabel.setAttribute("font-size", "14");
3279
+ bcLabel.setAttribute("font-weight", "600");
3280
+ bcLabel.setAttribute("fill", "#475569");
3281
+ bcLabel.textContent = bc.label;
3282
+ bcGroup.appendChild(bcLabel);
3283
+ if (bc.team) {
3284
+ const teamLabel = document.createElementNS(SVG_NS4, "text");
3285
+ teamLabel.setAttribute("x", String(bcX + bcW - 16));
3286
+ teamLabel.setAttribute("y", String(bcY + 24));
3287
+ teamLabel.setAttribute("font-size", "12");
3288
+ teamLabel.setAttribute("font-style", "italic");
3289
+ teamLabel.setAttribute("fill", "#64748b");
3290
+ teamLabel.setAttribute("text-anchor", "end");
3291
+ teamLabel.textContent = `Team: ${bc.team}`;
3292
+ bcGroup.appendChild(teamLabel);
3293
+ }
3294
+ }
3295
+ container.appendChild(bcGroup);
3296
+ const graphStartX = bcX + BC_PADDING;
3297
+ const graphStartY = bcY + (bc ? BC_HEADER : 0) + BC_PADDING;
3298
+ const aggAreaY = graphStartY + gridYOffset;
3299
+ if (hasTerms) {
3300
+ const noteX = graphStartX + (graphW > 0 ? graphW + GRID_GAP : 0);
3301
+ const noteY = graphStartY;
3302
+ const noteGroup = document.createElementNS(SVG_NS4, "g");
3303
+ noteGroup.setAttribute("class", "d5-language-note");
3304
+ const noteRect = document.createElementNS(SVG_NS4, "rect");
3305
+ noteRect.setAttribute("x", String(noteX));
3306
+ noteRect.setAttribute("y", String(noteY));
3307
+ noteRect.setAttribute("width", String(NOTE_WIDTH));
3308
+ noteRect.setAttribute("height", String(noteH));
3309
+ noteRect.setAttribute("rx", "4");
3310
+ noteRect.setAttribute("fill", "#fef9c3");
3311
+ noteRect.setAttribute("stroke", "#ca8a04");
3312
+ noteRect.setAttribute("stroke-width", "1.5");
3313
+ noteGroup.appendChild(noteRect);
3314
+ const noteTop = document.createElementNS(SVG_NS4, "path");
3315
+ noteTop.setAttribute(
3316
+ "d",
3317
+ `M${noteX},${noteY + NOTE_HEADER_H} L${noteX},${noteY + 4} Q${noteX},${noteY} ${noteX + 4},${noteY} L${noteX + NOTE_WIDTH - 4},${noteY} Q${noteX + NOTE_WIDTH},${noteY} ${noteX + NOTE_WIDTH},${noteY + 4} L${noteX + NOTE_WIDTH},${noteY + NOTE_HEADER_H} Z`
3318
+ );
3319
+ noteTop.setAttribute("fill", "#fde047");
3320
+ noteGroup.appendChild(noteTop);
3321
+ const noteTitle = document.createElementNS(SVG_NS4, "text");
3322
+ noteTitle.setAttribute("x", String(noteX + NOTE_WIDTH / 2));
3323
+ noteTitle.setAttribute("y", String(noteY + 20));
3324
+ noteTitle.setAttribute("text-anchor", "middle");
3325
+ noteTitle.setAttribute("font-size", "12");
3326
+ noteTitle.setAttribute("font-weight", "bold");
3327
+ noteTitle.setAttribute("fill", "#854d0e");
3328
+ noteTitle.textContent = "Ubiquitous Language";
3329
+ noteGroup.appendChild(noteTitle);
3330
+ const textX = noteX + NOTE_PAD;
3331
+ let ty = noteY + NOTE_HEADER_H + NOTE_PAD + NOTE_TERM_FONT.size * 0.85;
3332
+ const nameLH = lineHeight(NOTE_TERM_FONT.size);
3333
+ const defLH = lineHeight(NOTE_DEF_FONT.size);
3334
+ termBlocks.forEach((block) => {
3335
+ block.nameLines.forEach((ln2, i) => {
3336
+ const el = document.createElementNS(SVG_NS4, "text");
3337
+ el.setAttribute("x", String(textX));
3338
+ el.setAttribute("y", String(ty));
3339
+ el.setAttribute("font-size", String(NOTE_TERM_FONT.size));
3340
+ el.setAttribute("font-weight", "bold");
3341
+ el.setAttribute("fill", "#713f12");
3342
+ el.textContent = i === block.nameLines.length - 1 ? `${ln2}:` : ln2;
3343
+ noteGroup.appendChild(el);
3344
+ ty += nameLH;
3345
+ });
3346
+ ty += 2;
3347
+ block.defLines.forEach((ln2) => {
3348
+ const el = document.createElementNS(SVG_NS4, "text");
3349
+ el.setAttribute("x", String(textX));
3350
+ el.setAttribute("y", String(ty));
3351
+ el.setAttribute("font-size", String(NOTE_DEF_FONT.size));
3352
+ el.setAttribute("fill", "#854d0e");
3353
+ el.textContent = ln2;
3354
+ noteGroup.appendChild(el);
3355
+ ty += defLH;
3356
+ });
3357
+ ty += NOTE_TERM_GAP;
3358
+ });
3359
+ container.appendChild(noteGroup);
3360
+ }
3361
+ aggregates.forEach((agg) => {
3362
+ const node = g.node(agg.id);
3363
+ if (!node) return;
3364
+ const w2 = node.width;
3365
+ const h = node.height;
3366
+ const x2 = graphStartX + node.x - w2 / 2;
3367
+ const y = aggAreaY + node.y - h / 2;
3368
+ const cx = x2 + w2 / 2;
3369
+ const group = document.createElementNS(SVG_NS4, "g");
3370
+ group.setAttribute("class", "d5-aggregate");
3371
+ const rect = document.createElementNS(SVG_NS4, "rect");
3372
+ rect.setAttribute("x", String(x2));
3373
+ rect.setAttribute("y", String(y));
3374
+ rect.setAttribute("width", String(w2));
3375
+ rect.setAttribute("height", String(h));
3376
+ rect.setAttribute("rx", "8");
3377
+ rect.setAttribute("fill", "white");
3378
+ rect.setAttribute("stroke", "#3b82f6");
3379
+ rect.setAttribute("stroke-width", "2");
3380
+ group.appendChild(rect);
3381
+ const headerH = 34;
3382
+ const header = document.createElementNS(SVG_NS4, "path");
3383
+ header.setAttribute(
3384
+ "d",
3385
+ `M${x2},${y + headerH} L${x2},${y + 8} Q${x2},${y} ${x2 + 8},${y} L${x2 + w2 - 8},${y} Q${x2 + w2},${y} ${x2 + w2},${y + 8} L${x2 + w2},${y + headerH} Z`
3386
+ );
3387
+ header.setAttribute("fill", "#dbeafe");
3388
+ group.appendChild(header);
3389
+ const line = document.createElementNS(SVG_NS4, "line");
3390
+ line.setAttribute("x1", String(x2));
3391
+ line.setAttribute("y1", String(y + headerH));
3392
+ line.setAttribute("x2", String(x2 + w2));
3393
+ line.setAttribute("y2", String(y + headerH));
3394
+ line.setAttribute("stroke", "#3b82f6");
3395
+ line.setAttribute("stroke-width", "1.5");
3396
+ group.appendChild(line);
3397
+ const labelText = document.createElementNS(SVG_NS4, "text");
3398
+ labelText.setAttribute("x", String(cx));
3399
+ labelText.setAttribute("y", String(y + 20));
3400
+ labelText.setAttribute("text-anchor", "middle");
3401
+ labelText.setAttribute("font-size", "13");
3402
+ labelText.setAttribute("font-weight", "bold");
3403
+ labelText.setAttribute("fill", "#1e293b");
3404
+ labelText.textContent = agg.label;
3405
+ group.appendChild(labelText);
3406
+ const rootText = document.createElementNS(SVG_NS4, "text");
3407
+ rootText.setAttribute("x", String(cx));
3408
+ rootText.setAttribute("y", String(y + headerH + 20));
3409
+ rootText.setAttribute("text-anchor", "middle");
3410
+ rootText.setAttribute("font-size", "11");
3411
+ rootText.setAttribute("font-style", "italic");
3412
+ rootText.setAttribute("fill", "#3b82f6");
3413
+ rootText.textContent = `Root: ${agg.root}`;
3414
+ group.appendChild(rootText);
3415
+ if (agg.fields && agg.fields.length > 0) {
3416
+ const sep = document.createElementNS(SVG_NS4, "line");
3417
+ sep.setAttribute("x1", String(x2 + 10));
3418
+ sep.setAttribute("y1", String(y + headerH + 32));
3419
+ sep.setAttribute("x2", String(x2 + w2 - 10));
3420
+ sep.setAttribute("y2", String(y + headerH + 32));
3421
+ sep.setAttribute("stroke", "#cbd5e1");
3422
+ sep.setAttribute("stroke-dasharray", "4 4");
3423
+ group.appendChild(sep);
3424
+ let fy = y + headerH + 50;
3425
+ agg.fields.forEach((field) => {
3426
+ const bullet = document.createElementNS(SVG_NS4, "circle");
3427
+ bullet.setAttribute("cx", String(x2 + 20));
3428
+ bullet.setAttribute("cy", String(fy - 4));
3429
+ bullet.setAttribute("r", "3");
3430
+ bullet.setAttribute("fill", "#94a3b8");
3431
+ group.appendChild(bullet);
3432
+ const fieldText = document.createElementNS(SVG_NS4, "text");
3433
+ fieldText.setAttribute("x", String(x2 + 30));
3434
+ fieldText.setAttribute("y", String(fy));
3435
+ fieldText.setAttribute("font-size", "12");
3436
+ fieldText.setAttribute("fill", "#475569");
3437
+ fieldText.textContent = field;
3438
+ group.appendChild(fieldText);
3439
+ fy += AGGREGATE_FIELD_HEIGHT;
3440
+ });
3441
+ }
3442
+ container.appendChild(group);
3443
+ });
3444
+ readModels.forEach((rm) => {
3445
+ const node = g.node(rm.id);
3446
+ if (!node) return;
3447
+ const w2 = node.width;
3448
+ const h = node.height;
3449
+ const x2 = graphStartX + node.x - w2 / 2;
3450
+ const y = aggAreaY + node.y - h / 2;
3451
+ const cx = x2 + w2 / 2;
3452
+ const group = document.createElementNS(SVG_NS4, "g");
3453
+ group.setAttribute("class", "d5-read-model");
3454
+ const rect = document.createElementNS(SVG_NS4, "rect");
3455
+ rect.setAttribute("x", String(x2));
3456
+ rect.setAttribute("y", String(y));
3457
+ rect.setAttribute("width", String(w2));
3458
+ rect.setAttribute("height", String(h));
3459
+ rect.setAttribute("rx", "6");
3460
+ rect.setAttribute("fill", "white");
3461
+ rect.setAttribute("stroke", "#64748b");
3462
+ rect.setAttribute("stroke-width", "2");
3463
+ group.appendChild(rect);
3464
+ const headerH = 30;
3465
+ const header = document.createElementNS(SVG_NS4, "path");
3466
+ header.setAttribute(
3467
+ "d",
3468
+ `M${x2},${y + headerH} L${x2},${y + 6} Q${x2},${y} ${x2 + 6},${y} L${x2 + w2 - 6},${y} Q${x2 + w2},${y} ${x2 + w2},${y + 6} L${x2 + w2},${y + headerH} Z`
3469
+ );
3470
+ header.setAttribute("fill", "#f1f5f9");
3471
+ group.appendChild(header);
3472
+ const hLine = document.createElementNS(SVG_NS4, "line");
3473
+ hLine.setAttribute("x1", String(x2));
3474
+ hLine.setAttribute("y1", String(y + headerH));
3475
+ hLine.setAttribute("x2", String(x2 + w2));
3476
+ hLine.setAttribute("y2", String(y + headerH));
3477
+ hLine.setAttribute("stroke", "#64748b");
3478
+ hLine.setAttribute("stroke-width", "1");
3479
+ group.appendChild(hLine);
3480
+ const labelText = document.createElementNS(SVG_NS4, "text");
3481
+ labelText.setAttribute("x", String(cx));
3482
+ labelText.setAttribute("y", String(y + 19));
3483
+ labelText.setAttribute("text-anchor", "middle");
3484
+ labelText.setAttribute("font-size", "13");
3485
+ labelText.setAttribute("font-weight", "bold");
3486
+ labelText.setAttribute("fill", "#1e293b");
3487
+ labelText.textContent = rm.label;
3488
+ group.appendChild(labelText);
3489
+ const typeText = document.createElementNS(SVG_NS4, "text");
3490
+ typeText.setAttribute("x", String(cx));
3491
+ typeText.setAttribute("y", String(y + headerH + 16));
3492
+ typeText.setAttribute("text-anchor", "middle");
3493
+ typeText.setAttribute("font-size", "10");
3494
+ typeText.setAttribute("font-style", "italic");
3495
+ typeText.setAttribute("fill", "#64748b");
3496
+ typeText.textContent = "read model";
3497
+ group.appendChild(typeText);
3498
+ for (let r = 0; r < 2; r++) {
3499
+ const row = document.createElementNS(SVG_NS4, "line");
3500
+ row.setAttribute("x1", String(x2 + 12));
3501
+ row.setAttribute("y1", String(y + headerH + 30 + r * 9));
3502
+ row.setAttribute("x2", String(x2 + w2 - 12));
3503
+ row.setAttribute("y2", String(y + headerH + 30 + r * 9));
3504
+ row.setAttribute("stroke", "#e2e8f0");
3505
+ row.setAttribute("stroke-width", "1");
3506
+ group.appendChild(row);
3507
+ }
3508
+ container.appendChild(group);
3509
+ });
3510
+ relationships.forEach((rel) => {
3511
+ const edge = g.edge(rel.source, rel.target);
3512
+ if (!edge || !edge.points || edge.points.length === 0) return;
3513
+ const shiftedPoints = edge.points.map((p2) => ({
3514
+ x: graphStartX + p2.x,
3515
+ y: aggAreaY + p2.y
3516
+ }));
3517
+ const group = document.createElementNS(SVG_NS4, "g");
3518
+ group.setAttribute("class", "d5-rel");
3519
+ const pathString = generateCurvePath3(shiftedPoints);
3520
+ const path = document.createElementNS(SVG_NS4, "path");
3521
+ path.setAttribute("d", pathString);
3522
+ path.setAttribute("fill", "none");
3523
+ path.setAttribute("stroke", "#64748b");
3524
+ path.setAttribute("stroke-width", "1.5");
3525
+ path.setAttribute("marker-end", "url(#d5-arrowhead)");
3526
+ group.appendChild(path);
3527
+ if (rel.label) {
3528
+ let lx;
3529
+ let ly;
3530
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3531
+ lx = graphStartX + edge.x;
3532
+ ly = aggAreaY + edge.y;
3533
+ } else {
3534
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3535
+ lx = mid.x;
3536
+ ly = mid.y;
3537
+ }
3538
+ group.appendChild(
3539
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH3 })
3540
+ );
3541
+ }
3542
+ container.appendChild(group);
3543
+ });
3544
+ events.forEach((ev, i) => {
3545
+ const edge = g.edge(ev.source, ev.target, `evt${i}`);
3546
+ if (!edge || !edge.points || edge.points.length === 0) return;
3547
+ const shiftedPoints = edge.points.map((p2) => ({
3548
+ x: graphStartX + p2.x,
3549
+ y: aggAreaY + p2.y
3550
+ }));
3551
+ const group = document.createElementNS(SVG_NS4, "g");
3552
+ group.setAttribute("class", "d5-event");
3553
+ const titleEl = document.createElementNS(SVG_NS4, "title");
3554
+ titleEl.textContent = `${ev.name} \u2014 emitted by ${ev.source}, handled by ${ev.target}`;
3555
+ group.appendChild(titleEl);
3556
+ const path = document.createElementNS(SVG_NS4, "path");
3557
+ path.setAttribute("d", generateCurvePath3(shiftedPoints));
3558
+ path.setAttribute("fill", "none");
3559
+ path.setAttribute("stroke", EVENT_STROKE);
3560
+ path.setAttribute("stroke-width", "1.5");
3561
+ path.setAttribute("stroke-dasharray", "6 4");
3562
+ path.setAttribute("marker-end", "url(#d5-event-arrowhead)");
3563
+ group.appendChild(path);
3564
+ let lx;
3565
+ let ly;
3566
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3567
+ lx = graphStartX + edge.x;
3568
+ ly = aggAreaY + edge.y;
3569
+ } else {
3570
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3571
+ lx = mid.x;
3572
+ ly = mid.y;
3573
+ }
3574
+ group.appendChild(eventRibbon(lx, ly, ev.name));
3575
+ container.appendChild(group);
3576
+ });
3577
+ policies.forEach((p2, i) => {
3578
+ const edge = g.edge(p2.source, p2.target, `pol${i}`);
3579
+ if (!edge || !edge.points || edge.points.length === 0) return;
3580
+ const shiftedPoints = edge.points.map((pt) => ({
3581
+ x: graphStartX + pt.x,
3582
+ y: aggAreaY + pt.y
3583
+ }));
3584
+ const group = document.createElementNS(SVG_NS4, "g");
3585
+ group.setAttribute("class", "d5-policy");
3586
+ const titleEl = document.createElementNS(SVG_NS4, "title");
3587
+ titleEl.textContent = `Policy \u2014 triggered via ${p2.source}, acts on ${p2.target}`;
3588
+ group.appendChild(titleEl);
3589
+ const path = document.createElementNS(SVG_NS4, "path");
3590
+ path.setAttribute("d", generateCurvePath3(shiftedPoints));
3591
+ path.setAttribute("fill", "none");
3592
+ path.setAttribute("stroke", POLICY_STROKE);
3593
+ path.setAttribute("stroke-width", "1.5");
3594
+ path.setAttribute("stroke-dasharray", "3 3");
3595
+ path.setAttribute("marker-end", "url(#d5-policy-arrowhead)");
3596
+ group.appendChild(path);
3597
+ let lx;
3598
+ let ly;
3599
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3600
+ lx = graphStartX + edge.x;
3601
+ ly = aggAreaY + edge.y;
3602
+ } else {
3603
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3604
+ lx = mid.x;
3605
+ ly = mid.y;
3606
+ }
3607
+ group.appendChild(policyTag(lx, ly, p2.rule));
3608
+ container.appendChild(group);
3609
+ });
3610
+ }
3611
+
3612
+ // src/d5-aggregate/detector.ts
3613
+ function detectAggregate(text) {
3614
+ return text.trimStart().startsWith("d5-aggregate");
3615
+ }
3616
+
3617
+ // src/d5-aggregate/db.ts
3618
+ var D5AggregateDb = class {
3619
+ aggregate;
3620
+ entities = [];
3621
+ valueObjects = [];
3622
+ invariants = [];
3623
+ title;
3624
+ accTitle;
3625
+ accDescription;
3626
+ getAggregate() {
3627
+ return this.aggregate;
3628
+ }
3629
+ setAggregate(id, label, root) {
3630
+ this.aggregate = { id, label, root };
3631
+ }
3632
+ getEntities() {
3633
+ return this.entities;
3634
+ }
3635
+ addEntity(id, label) {
3636
+ this.entities.push({ id, label });
3637
+ }
3638
+ getValueObjects() {
3639
+ return this.valueObjects;
3640
+ }
3641
+ addValueObject(id, label) {
3642
+ this.valueObjects.push({ id, label });
3643
+ }
3644
+ getInvariants() {
3645
+ return this.invariants;
3646
+ }
3647
+ addInvariant(text, name) {
3648
+ this.invariants.push({ name: name || void 0, text });
3649
+ }
3650
+ clear() {
3651
+ this.aggregate = void 0;
3652
+ this.entities = [];
3653
+ this.valueObjects = [];
3654
+ this.invariants = [];
3655
+ this.title = void 0;
3656
+ this.accTitle = void 0;
3657
+ this.accDescription = void 0;
3658
+ }
3659
+ setTitle(title) {
3660
+ this.title = title;
3661
+ }
3662
+ getTitle() {
3663
+ return this.title;
3664
+ }
3665
+ // Mermaid DiagramDB interface methods
3666
+ setDiagramTitle(title) {
3667
+ this.setTitle(title);
3668
+ }
3669
+ getDiagramTitle() {
3670
+ return this.title ?? "";
3671
+ }
3672
+ setAccTitle(title) {
3673
+ this.accTitle = title;
3674
+ }
3675
+ getAccTitle() {
3676
+ return this.accTitle ?? "";
3677
+ }
3678
+ setAccDescription(desc) {
3679
+ this.accDescription = desc;
3680
+ }
3681
+ getAccDescription() {
3682
+ return this.accDescription ?? "";
3683
+ }
3684
+ };
3685
+
3686
+ // src/d5-aggregate/parser.ts
3687
+ var TITLE_RE4 = /^\s*title\s+(.+)$/;
3688
+ var AGGREGATE_RE2 = /^\s*Aggregate\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*root:\s*"([^"]+)"\s*\)\s*\{/;
3689
+ var ENTITY_RE = /^\s*Entity\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
3690
+ var VALUE_OBJECT_RE = /^\s*ValueObject\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
3691
+ var INVARIANTS_RE = /^\s*Invariants\s*\{/;
3692
+ var INVARIANT_RE = /^\s*Invariant\(\s*"([^"]+)"\s*(?:,\s*"([^"]+)")?\s*\)/;
3693
+ var COMMENT_LINE_RE4 = /^\s*%%/;
3694
+ function stripInlineComment4(line) {
3695
+ const idx = line.indexOf("%%");
3696
+ return idx === -1 ? line : line.slice(0, idx);
3697
+ }
3698
+ function parse4(text, db) {
3699
+ const lines = text.split("\n");
3700
+ for (const raw of lines) {
3701
+ if (COMMENT_LINE_RE4.test(raw)) continue;
3702
+ const line = stripInlineComment4(raw).trim();
3703
+ if (line === "" || line === "d5-aggregate") continue;
3704
+ let m;
3705
+ if (m = line.match(TITLE_RE4)) {
3706
+ db.setTitle(m[1].trim());
3707
+ } else if (m = line.match(AGGREGATE_RE2)) {
3708
+ db.setAggregate(m[1], m[2], m[3]);
3709
+ } else if (INVARIANTS_RE.test(line)) ; else if (m = line.match(INVARIANT_RE)) {
3710
+ if (m[2] !== void 0) db.addInvariant(m[2], m[1]);
3711
+ else db.addInvariant(m[1]);
3712
+ } else if (m = line.match(ENTITY_RE)) {
3713
+ db.addEntity(m[1], m[2]);
3714
+ } else if (m = line.match(VALUE_OBJECT_RE)) {
3715
+ db.addValueObject(m[1], m[2]);
3716
+ }
3717
+ }
3718
+ }
3719
+
3720
+ // src/d5-aggregate/renderer.ts
3721
+ var SVG_NS5 = "http://www.w3.org/2000/svg";
3722
+ var MARGIN3 = 30;
3723
+ var TITLE_HEIGHT4 = 40;
3724
+ var AGG_PADDING = 28;
3725
+ var AGG_HEADER = 36;
3726
+ var ROOT_H = 58;
3727
+ var ROOT_MIN_W = 320;
3728
+ var CELL_MIN_W = 150;
3729
+ var CELL_MAX_W = 240;
3730
+ var CELL_GAP = 18;
3731
+ var CELL_PAD_X = 16;
3732
+ var CELL_PAD_Y = 9;
3733
+ var LABEL_FONT = { size: 13, weight: 600 };
3734
+ var TYPE_FONT_SIZE = 10;
3735
+ var TARGET_GRID_W = 980;
3736
+ var ROOT_TO_GRID_GAP = 24;
3737
+ var INV_FONT = 12;
3738
+ var INV_TITLE_FONT = 12;
3739
+ var INV_PAD = 16;
3740
+ var INV_BULLET_INDENT = 16;
3741
+ var INV_GAP_ABOVE = 22;
3742
+ var INV_TITLE_GAP = 12;
3743
+ var INV_ITEM_GAP = 9;
3744
+ var INV_BAND_BOTTOM_PAD = 12;
3745
+ var INV_MAX_TEXT_COL = 620;
3746
+ function render4(db, container) {
3747
+ const title = db.getTitle();
3748
+ const aggregate = db.getAggregate();
3749
+ const entities = db.getEntities();
3750
+ const valueObjects = db.getValueObjects();
3751
+ const invariants = db.getInvariants();
3752
+ const rootId = aggregate ? entities.find((e) => e.label === aggregate.root)?.id : void 0;
3753
+ const wrapW = CELL_MAX_W - CELL_PAD_X * 2;
3754
+ const members = [
3755
+ ...entities.filter((e) => e.id !== rootId).map((e) => ({ ...e, kind: "entity" })),
3756
+ ...valueObjects.map((v2) => ({ ...v2, kind: "value-object" }))
3757
+ ].map((m) => ({ ...m, lines: wrapText(m.label, wrapW, LABEL_FONT) }));
3758
+ const widestLabel = members.reduce(
3759
+ (max, m) => Math.max(max, ...m.lines.map((l) => measureText(l, LABEL_FONT))),
3760
+ 0
3761
+ );
3762
+ const cellW = Math.round(
3763
+ Math.min(CELL_MAX_W, Math.max(CELL_MIN_W, widestLabel + CELL_PAD_X * 2))
3764
+ );
3765
+ const maxLines = members.reduce((max, m) => Math.max(max, m.lines.length), 1);
3766
+ const cellH = CELL_PAD_Y * 2 + maxLines * lineHeight(LABEL_FONT.size) + 4 + TYPE_FONT_SIZE;
3767
+ const { cols, rows } = gridDimensions(members.length, cellW, CELL_GAP, TARGET_GRID_W);
3768
+ const gridW = cols > 0 ? cols * cellW + (cols - 1) * CELL_GAP : 0;
3769
+ const gridH = rows > 0 ? rows * cellH + (rows - 1) * CELL_GAP : 0;
3770
+ const contentW = Math.max(gridW, rootId ? ROOT_MIN_W : 0, 320);
3771
+ const invWrapW = Math.min(contentW, INV_MAX_TEXT_COL) - INV_PAD * 2 - INV_BULLET_INDENT;
3772
+ const invItemLines = [];
3773
+ let invBandH = 0;
3774
+ if (invariants.length > 0) {
3775
+ invBandH = INV_GAP_ABOVE + lineHeight(INV_TITLE_FONT) + INV_TITLE_GAP;
3776
+ for (const inv of invariants) {
3777
+ const full = inv.name ? `${inv.name} \u2014 ${inv.text}` : inv.text;
3778
+ const lines = wrapText(full, invWrapW, { size: INV_FONT });
3779
+ invItemLines.push(lines);
3780
+ invBandH += lines.length * lineHeight(INV_FONT) + INV_ITEM_GAP;
3781
+ }
3782
+ invBandH += INV_BAND_BOTTOM_PAD - INV_ITEM_GAP;
3783
+ }
3784
+ const aggX = MARGIN3;
3785
+ const aggY = MARGIN3 + (title ? TITLE_HEIGHT4 : 0);
3786
+ const aggW = contentW + AGG_PADDING * 2;
3787
+ const bodyTop = aggY + AGG_HEADER + AGG_PADDING;
3788
+ const rootBottom = rootId ? bodyTop + ROOT_H + ROOT_TO_GRID_GAP : bodyTop;
3789
+ const gridBottom = rootBottom + gridH;
3790
+ const aggH = gridBottom + invBandH + AGG_PADDING - aggY;
3791
+ const legendH = 30;
3792
+ const totalW = aggX + aggW + MARGIN3;
3793
+ const totalH = aggY + aggH + legendH + MARGIN3;
3794
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
3795
+ container.setAttribute("height", String(totalH));
3796
+ container.style.maxWidth = `${totalW}px`;
3797
+ if (title) {
3798
+ const titleEl = document.createElementNS(SVG_NS5, "text");
3799
+ titleEl.setAttribute("class", "d5-title");
3800
+ titleEl.setAttribute("x", String(totalW / 2));
3801
+ titleEl.setAttribute("y", "24");
3802
+ titleEl.setAttribute("text-anchor", "middle");
3803
+ titleEl.setAttribute("font-size", "18");
3804
+ titleEl.setAttribute("font-weight", "bold");
3805
+ titleEl.setAttribute("fill", "#1e293b");
3806
+ titleEl.textContent = title;
3807
+ container.appendChild(titleEl);
3808
+ }
3809
+ if (aggregate) {
3810
+ const aggGroup = document.createElementNS(SVG_NS5, "g");
3811
+ aggGroup.setAttribute("class", "d5-aggregate");
3812
+ const aggRect = document.createElementNS(SVG_NS5, "rect");
3813
+ aggRect.setAttribute("x", String(aggX));
3814
+ aggRect.setAttribute("y", String(aggY));
3815
+ aggRect.setAttribute("width", String(aggW));
3816
+ aggRect.setAttribute("height", String(aggH));
3817
+ aggRect.setAttribute("rx", "12");
3818
+ aggRect.setAttribute("fill", "#f8fafc");
3819
+ aggRect.setAttribute("stroke", "#94a3b8");
3820
+ aggRect.setAttribute("stroke-width", "2");
3821
+ aggRect.setAttribute("stroke-dasharray", "8 4");
3822
+ aggGroup.appendChild(aggRect);
3823
+ const aggLabel = document.createElementNS(SVG_NS5, "text");
3824
+ aggLabel.setAttribute("x", String(aggX + 16));
3825
+ aggLabel.setAttribute("y", String(aggY + 24));
3826
+ aggLabel.setAttribute("font-size", "14");
3827
+ aggLabel.setAttribute("font-weight", "600");
3828
+ aggLabel.setAttribute("fill", "#475569");
3829
+ aggLabel.textContent = `Aggregate: ${aggregate.label}`;
3830
+ aggGroup.appendChild(aggLabel);
3831
+ container.appendChild(aggGroup);
3832
+ }
3833
+ const contentX = aggX + AGG_PADDING;
3834
+ if (rootId && aggregate) {
3835
+ const g = document.createElementNS(SVG_NS5, "g");
3836
+ g.setAttribute("class", "d5-entity d5-aggregate-root");
3837
+ const rect = document.createElementNS(SVG_NS5, "rect");
3838
+ rect.setAttribute("x", String(contentX));
3839
+ rect.setAttribute("y", String(bodyTop));
3840
+ rect.setAttribute("width", String(contentW));
3841
+ rect.setAttribute("height", String(ROOT_H));
3842
+ rect.setAttribute("rx", "8");
3843
+ rect.setAttribute("fill", "#eff6ff");
3844
+ rect.setAttribute("stroke", "#3b82f6");
3845
+ rect.setAttribute("stroke-width", "3");
3846
+ g.appendChild(rect);
3847
+ const cx = contentX + contentW / 2;
3848
+ const label = document.createElementNS(SVG_NS5, "text");
3849
+ label.setAttribute("x", String(cx));
3850
+ label.setAttribute("y", String(bodyTop + 25));
3851
+ label.setAttribute("text-anchor", "middle");
3852
+ label.setAttribute("font-size", "15");
3853
+ label.setAttribute("font-weight", "bold");
3854
+ label.setAttribute("fill", "#1e293b");
3855
+ label.textContent = aggregate.root;
3856
+ g.appendChild(label);
3857
+ const type = document.createElementNS(SVG_NS5, "text");
3858
+ type.setAttribute("x", String(cx));
3859
+ type.setAttribute("y", String(bodyTop + 43));
3860
+ type.setAttribute("text-anchor", "middle");
3861
+ type.setAttribute("font-size", String(TYPE_FONT_SIZE));
3862
+ type.setAttribute("font-style", "italic");
3863
+ type.setAttribute("fill", "#3b82f6");
3864
+ type.textContent = "Aggregate Root";
3865
+ g.appendChild(type);
3866
+ container.appendChild(g);
3867
+ }
3868
+ const gridX = contentX + (contentW - gridW) / 2;
3869
+ members.forEach((m, i) => {
3870
+ const col = i % cols;
3871
+ const row = Math.floor(i / cols);
3872
+ const x2 = gridX + col * (cellW + CELL_GAP);
3873
+ const y = rootBottom + row * (cellH + CELL_GAP);
3874
+ const cx = x2 + cellW / 2;
3875
+ const isEntity = m.kind === "entity";
3876
+ const g = document.createElementNS(SVG_NS5, "g");
3877
+ g.setAttribute("class", isEntity ? "d5-entity" : "d5-value-object");
3878
+ const rect = document.createElementNS(SVG_NS5, "rect");
3879
+ rect.setAttribute("x", String(x2));
3880
+ rect.setAttribute("y", String(y));
3881
+ rect.setAttribute("width", String(cellW));
3882
+ rect.setAttribute("height", String(cellH));
3883
+ rect.setAttribute("rx", String(isEntity ? 6 : cellH / 2));
3884
+ rect.setAttribute("fill", "white");
3885
+ rect.setAttribute("stroke", isEntity ? "#3b82f6" : "#10b981");
3886
+ rect.setAttribute("stroke-width", "2");
3887
+ g.appendChild(rect);
3888
+ const lh = lineHeight(LABEL_FONT.size);
3889
+ const blockH = m.lines.length * lh + 4 + TYPE_FONT_SIZE;
3890
+ let ty = y + (cellH - blockH) / 2 + LABEL_FONT.size * 0.85;
3891
+ m.lines.forEach((ln2) => {
3892
+ const t = document.createElementNS(SVG_NS5, "text");
3893
+ t.setAttribute("x", String(cx));
3894
+ t.setAttribute("y", String(ty));
3895
+ t.setAttribute("text-anchor", "middle");
3896
+ t.setAttribute("font-size", String(LABEL_FONT.size));
3897
+ t.setAttribute("font-weight", "600");
3898
+ t.setAttribute("fill", "#1e293b");
3899
+ t.textContent = ln2;
3900
+ g.appendChild(t);
3901
+ ty += lh;
3902
+ });
3903
+ const type = document.createElementNS(SVG_NS5, "text");
3904
+ type.setAttribute("x", String(cx));
3905
+ type.setAttribute("y", String(ty + 2));
3906
+ type.setAttribute("text-anchor", "middle");
3907
+ type.setAttribute("font-size", String(TYPE_FONT_SIZE));
3908
+ type.setAttribute("font-style", "italic");
3909
+ type.setAttribute("fill", isEntity ? "#3b82f6" : "#10b981");
3910
+ type.textContent = isEntity ? "Entity" : "Value Object";
3911
+ g.appendChild(type);
3912
+ container.appendChild(g);
3913
+ });
3914
+ if (invariants.length > 0) {
3915
+ const bandX = aggX + INV_PAD;
3916
+ const bandRight = aggX + aggW - INV_PAD;
3917
+ let y = gridBottom + INV_GAP_ABOVE;
3918
+ const invGroup = document.createElementNS(SVG_NS5, "g");
3919
+ invGroup.setAttribute("class", "d5-invariants");
3920
+ const backing = document.createElementNS(SVG_NS5, "rect");
3921
+ backing.setAttribute("x", String(bandX - 8));
3922
+ backing.setAttribute("y", String(y - 12));
3923
+ backing.setAttribute("width", String(bandRight - bandX + 16));
3924
+ backing.setAttribute("height", String(invBandH - INV_GAP_ABOVE + 12));
3925
+ backing.setAttribute("rx", "6");
3926
+ backing.setAttribute("fill", "#fffbeb");
3927
+ backing.setAttribute("stroke", "#fcd34d");
3928
+ backing.setAttribute("stroke-width", "1");
3929
+ backing.setAttribute("stroke-dasharray", "4 3");
3930
+ invGroup.appendChild(backing);
3931
+ const heading = document.createElementNS(SVG_NS5, "text");
3932
+ heading.setAttribute("x", String(bandX));
3933
+ heading.setAttribute("y", String(y + INV_TITLE_FONT * 0.9));
3934
+ heading.setAttribute("font-size", String(INV_TITLE_FONT));
3935
+ heading.setAttribute("font-weight", "bold");
3936
+ heading.setAttribute("fill", "#92400e");
3937
+ heading.textContent = aggregate?.root ? `Invariants \u2014 enforced by root: ${aggregate.root}` : "Invariants";
3938
+ invGroup.appendChild(heading);
3939
+ y += lineHeight(INV_TITLE_FONT) + INV_TITLE_GAP;
3940
+ const lh = lineHeight(INV_FONT);
3941
+ invItemLines.forEach((lines) => {
3942
+ const ms = 6;
3943
+ const marker = document.createElementNS(SVG_NS5, "rect");
3944
+ marker.setAttribute("x", String(bandX + 1));
3945
+ marker.setAttribute("y", String(y - ms));
3946
+ marker.setAttribute("width", String(ms));
3947
+ marker.setAttribute("height", String(ms));
3948
+ marker.setAttribute("transform", `rotate(45 ${bandX + 1 + ms / 2} ${y - ms + ms / 2})`);
3949
+ marker.setAttribute("fill", "#f59e0b");
3950
+ invGroup.appendChild(marker);
3951
+ lines.forEach((ln2, i) => {
3952
+ const t = document.createElementNS(SVG_NS5, "text");
3953
+ t.setAttribute("x", String(bandX + INV_BULLET_INDENT));
3954
+ t.setAttribute("y", String(y + i * lh));
3955
+ t.setAttribute("font-size", String(INV_FONT));
3956
+ t.setAttribute("fill", "#78350f");
3957
+ t.textContent = ln2;
3958
+ invGroup.appendChild(t);
3959
+ });
3960
+ y += lines.length * lh + INV_ITEM_GAP;
3961
+ });
3962
+ container.appendChild(invGroup);
3963
+ }
3964
+ const legendY = aggY + aggH + 16;
3965
+ const legendGroup = document.createElementNS(SVG_NS5, "g");
3966
+ legendGroup.setAttribute("class", "d5-legend");
3967
+ const legendItems = [
3968
+ { label: "Aggregate Root", stroke: "#3b82f6", width: 3, rx: 3 },
3969
+ { label: "Entity", stroke: "#3b82f6", width: 2, rx: 3 },
3970
+ { label: "Value Object", stroke: "#10b981", width: 2, rx: 7 }
3971
+ ];
3972
+ let legendX = aggX;
3973
+ legendItems.forEach((item) => {
3974
+ const swatch = document.createElementNS(SVG_NS5, "rect");
3975
+ swatch.setAttribute("x", String(legendX));
3976
+ swatch.setAttribute("y", String(legendY));
3977
+ swatch.setAttribute("width", "14");
3978
+ swatch.setAttribute("height", "14");
3979
+ swatch.setAttribute("rx", String(item.rx));
3980
+ swatch.setAttribute("fill", "white");
3981
+ swatch.setAttribute("stroke", item.stroke);
3982
+ swatch.setAttribute("stroke-width", String(item.width));
3983
+ legendGroup.appendChild(swatch);
3984
+ const label = document.createElementNS(SVG_NS5, "text");
3985
+ label.setAttribute("x", String(legendX + 20));
3986
+ label.setAttribute("y", String(legendY + 11));
3987
+ label.setAttribute("font-size", "11");
3988
+ label.setAttribute("fill", "#64748b");
3989
+ label.textContent = item.label;
3990
+ legendGroup.appendChild(label);
3991
+ legendX += 26 + measureText(item.label, { size: 11 }) + 22;
3992
+ });
3993
+ container.appendChild(legendGroup);
3994
+ }
3995
+
3996
+ // src/index.ts
3997
+ var SVG_NS6 = "http://www.w3.org/2000/svg";
3998
+ function createRenderer(db, renderFn) {
3999
+ return {
4000
+ draw(_text, id, _version, _diagramObject) {
4001
+ let svgElement = document.getElementById(id);
4002
+ if (!svgElement) {
4003
+ svgElement = document.createElementNS(SVG_NS6, "svg");
4004
+ svgElement.setAttribute("id", id);
4005
+ document.body.appendChild(svgElement);
4006
+ }
4007
+ renderFn(db, svgElement);
4008
+ }
4009
+ };
4010
+ }
4011
+ function createParser(db, parseFn) {
4012
+ return {
4013
+ parse(text) {
4014
+ db.clear();
4015
+ parseFn(text, db);
4016
+ }
4017
+ };
4018
+ }
4019
+ var domainDb = new D5DomainDb();
4020
+ var subdomainDb = new D5SubdomainDb();
4021
+ var contextDb = new D5ContextDb();
4022
+ var aggregateDb = new D5AggregateDb();
4023
+ var d5Diagrams = [
4024
+ {
4025
+ id: "d5-domain",
4026
+ detector: (text, _config) => detectDomain(text),
4027
+ loader: async () => ({
4028
+ id: "d5-domain",
4029
+ diagram: {
4030
+ db: domainDb,
4031
+ renderer: createRenderer(domainDb, render),
4032
+ parser: createParser(domainDb, parse)
4033
+ }
4034
+ })
4035
+ },
4036
+ {
4037
+ id: "d5-subdomain",
4038
+ detector: (text, _config) => detectSubdomain(text),
4039
+ loader: async () => ({
4040
+ id: "d5-subdomain",
4041
+ diagram: {
4042
+ db: subdomainDb,
4043
+ renderer: createRenderer(subdomainDb, render2),
4044
+ parser: createParser(subdomainDb, parse2)
4045
+ }
4046
+ })
4047
+ },
4048
+ {
4049
+ id: "d5-context",
4050
+ detector: (text, _config) => detectContext(text),
4051
+ loader: async () => ({
4052
+ id: "d5-context",
4053
+ diagram: {
4054
+ db: contextDb,
4055
+ renderer: createRenderer(contextDb, render3),
4056
+ parser: createParser(contextDb, parse3)
4057
+ }
4058
+ })
4059
+ },
4060
+ {
4061
+ id: "d5-aggregate",
4062
+ detector: (text, _config) => detectAggregate(text),
4063
+ loader: async () => ({
4064
+ id: "d5-aggregate",
4065
+ diagram: {
4066
+ db: aggregateDb,
4067
+ renderer: createRenderer(aggregateDb, render4),
4068
+ parser: createParser(aggregateDb, parse4)
4069
+ }
4070
+ })
4071
+ }
4072
+ ];
4073
+ /*! Bundled license information:
4074
+
4075
+ @dagrejs/dagre/dist/dagre.esm.js:
4076
+ (*! For license information please see dagre.esm.js.LEGAL.txt *)
4077
+ */
4078
+
4079
+ exports.d5Diagrams = d5Diagrams;
4080
+ //# sourceMappingURL=index.cjs.map
4081
+ //# sourceMappingURL=index.cjs.map