Renders the Mermaid diagrams Claude Code writes or reads as elegant cards in the transcript: colored Unicode art in any terminal, real SVG on desktop.

mermaid-render is a Claude Code plugin that turns every Mermaid diagram Claude writes in a reply, reads, writes, edits or prints in a file or a command's output, into a rendered diagram right in the transcript: a colored Unicode card in any terminal, a real SVG in the Claude Code desktop app and VS Code.
No more reading raw flowchart LR source. Flowcharts, sequence diagrams, state diagrams, class diagrams and ER diagrams show up as pictures.

In Claude Code, run:
/plugin marketplace add ejklock/claude-mermaid-render
/plugin install mermaid-render@mermaid-render
Or in one line:
/plugin install mermaid-render --marketplace ejklock/claude-mermaid-render
Answer y to add the marketplace and pick a scope (user scope is the default). The plugin is active at once, with no restart.
Requirements: Claude Code 2.1.292 or newer (function-hook plugins). Node.js 20+ for the SVG renderer (desktop, VS Code, and the terminal image mode). rsvg-convert only for the image mode (brew install librsvg / apt install librsvg2-bin).
| Where | When | How it looks |
|---|---|---|
| Claude's replies | A closed ```mermaid fence in the answer | Prose stays Markdown; each diagram becomes a titled card |
| Tool rows | Read or Write of a .mmd / .mermaid file, or a .md file with mermaid fences | The diagrams appear under the tool row |
| Tool rows | A finished Bash run whose output holds a closed mermaid fence, or that prints a whole .mmd / .mermaid file (cat, sed -n) | The command's own row and output stay; each diagram appears as a card under it |
| Tool rows | A finished Edit of a .md file, for each mermaid fence the edit touched; of a .mmd / .mermaid file, the whole updated diagram | The edit's own diff stays; the updated diagrams appear as cards under it |
| Any terminal | Default unicode mode | Box-drawing art: bold labels, soft lines, accent-colored arrows |
| Ghostty, kitty, WezTerm | image mode | The real SVG, drawn as a picture (kitty graphics protocol) |
| Desktop app, VS Code | Always | Native SVG |
Details that keep it pleasant:
image mode the picture fills the card width and is shrunk to fit the terminal height.The desktop SVG and the terminal image mode use the diagram themes of beautiful-mermaid:
Flowchart, tokyo-night | Sequence, tokyo-night | State, catppuccin-mocha |
|---|---|---|
![]() | ![]() | ![]() |
Open /plugin, select mermaid-render, then Configure:
| Option | Values | Default |
|---|---|---|
terminalRender | unicode (any terminal), image (kitty graphics terminals) | unicode |
theme | tokyo-night, tokyo-night-storm, catppuccin-mocha, dracula, nord, one-dark, github-dark, zinc-dark, solarized-dark, github-light, catppuccin-latte, zinc-light | tokyo-night |
language | en, pt-BR (card titles, tool row counts and notices; any other value gives English) | en |
Inside tmux or another multiplexer that does not pass graphics through, keep unicode.
After installing, ask Claude:
Draw the checkout flow of an online store as a Mermaid flowchart.
Or read one of the demo files in this repository:
Read demo/checkout.mmd and demo/architecture.md
flowchart / graph, sequenceDiagram, stateDiagram / stateDiagram-v2, classDiagram, erDiagram. Other types (pie, gantt, mindmap, ...) show a short notice and their source.
The plugin is a Claude Code function-hook module (hooks/register.tsx). It hooks ui.render for AssistantMessage, ToolUse and ToolGroup, finds mermaid fences with a CommonMark-style parser (hooks/fences.ts) and draws a TSX tree per diagram.
hooks/vendor/mermaid.js, about 84 KB).renderer/svg.mjs runs it under Node. It flattens CSS variables to plain colors, so any SVG renderer (including rsvg-convert) draws it right.npm ci
npm run build # rebuilds hooks/vendor/mermaid.js and renderer/svg.mjs
npm run validate # claude plugin validate .
npm test # claude plugin test .
npm run screenshots # regenerates docs/images (needs rsvg-convert)
claude --plugin-dir . # try the working copy in a session
The images in docs/images are generated by scripts/screenshots.mjs from the plugin's own renderers. The terminal picture draws the plugin's real Unicode output inside a terminal frame.
MIT. Bundled third-party code is listed in THIRD_PARTY_NOTICES.md.
hooks/register.tsx 350 lines1import type { ElementTable, Register, RenderElement, RenderSurface } from 'claude-code'
2
3import { header, renderUnicode } from './diagram'
4import type { Failure, Glyph, Unicode } from './diagram'
5import { applyEdit, diagramsInFile, diagramsInOutput, diagramsTouched, isDiagramFile, splitMermaid } from './fences'
6import type { Segment } from './fences'
7import { countLine, messagesFor } from './messages'
8import type { Messages } from './messages'
9
10type Svg = { isRendered: true; svg: string; width: number; height: number }
11type Png = { path: string; width: number; height: number }
12/** What the hook fetched through $ for one source, before anything is drawn. */
13type Prepared = { svg?: Svg | Failure; png?: Png }
14type Draw = { ui: ElementTable; surface: RenderSurface; width: number; rows: number; prepared: Map<string, Prepared>; messages: Messages }
15type FileDiagrams = { path: string; lines: number; sources: string[] }
16
17const GLYPH_COLOR: Record<Glyph, string> = { label: 'text', line: 'inactive', arrow: 'claude' }
18const SVG_LIMIT = 131072
19const CACHE_LIMIT = 200
20// The card's border and padding.
21const CARD_CHROME = 4
22
23const unicodeCache = new Map<string, Unicode | Failure>()
24const svgCache = new Map<string, Svg | Failure>()
25const pngCache = new Map<string, Png | undefined>()
26
27export const register: Register = (on, options) => {
28 const terminalRender = String(options.terminalRender ?? 'unicode')
29 const theme = String(options.theme ?? 'tokyo-night')
30 const messages = messagesFor(options.language)
31
32 // A run of reads folds into one count line; unfold the finished ones that
33 // read or wrote a diagram, so each row below can draw it.
34 on('ui.render', { component: 'ToolGroup' }, ($, e, next) => {
35 const hasDiagram = e.props.calls.some(call => {
36 if (fileDiagrams(call.tool, call.output) !== undefined) return true
37 if (call.isRunning || call.isErrored || call.isInterrupted) return false
38 if (call.tool === 'Bash') return bashDiagrams(call.input, call.output).length > 0
39
40 return call.tool === 'Edit' && editDiagrams(call.output).length > 0
41 })
42
43 return !e.props.isActive && !e.props.isExpanded && hasDiagram
44 ? next({ ...e, props: { ...e.props, isExpanded: true } })
45 : next(e)
46 })
47
48 on('ui.render', async ($, e, next) => {
49 let segments: Segment[] = []
50 let file: FileDiagrams | undefined
51 let indent = 0
52 // Sources of a Bash or Edit row, drawn under the engine's own row.
53 let kept: string[] = []
54 if (e.component === 'AssistantMessage' && e.props.isSummary !== true) {
55 segments = splitMermaid(e.props.text)
56 indent = 2
57 } else if (e.component === 'ToolUse' && !e.props.isRunning && !e.props.isErrored) {
58 const isInterrupted = e.props.isInterrupted === true
59 file = fileDiagrams(e.props.tool, e.props.output)
60 segments = (file?.sources ?? []).map(source => ({ kind: 'mermaid', source }))
61 indent = 7
62 if (file === undefined && !isInterrupted) {
63 kept = e.props.tool === 'Bash' ? bashDiagrams(e.props.input, e.props.output) : e.props.tool === 'Edit' ? editDiagrams(e.props.output) : []
64 segments = kept.map(source => ({ kind: 'mermaid', source }))
65 }
66 }
67 const sources = segments.flatMap(segment => (segment.kind === 'mermaid' ? [segment.source] : []))
68 if (sources.length === 0) return next(e)
69
70 const wantsSvg = e.surface !== 'terminal' || terminalRender === 'image'
71 const prepared = new Map<string, Prepared>()
72 for (const source of wantsSvg ? sources : []) {
73 const key = `${theme}|${source}`
74 let svg = svgCache.get(key)
75 if (svg === undefined) {
76 // SVG layout needs ELK, too large to import: renderer/svg.mjs runs it under node.
77 try {
78 const ran = await $.process.run(['/usr/bin/env', 'node', `${$.plugin.root}/renderer/svg.mjs`], {
79 stdin: JSON.stringify({ source, theme }),
80 timeoutMs: 15000,
81 })
82 svg = ran.exitCode === 0 && !ran.isStdoutTruncated ? parseSvg(ran.stdout) : failure(source, ran.stderr)
83 } catch (error) {
84 svg = failure(source, error instanceof Error ? error.message : String(error))
85 }
86 remember(svgCache, key, svg)
87 }
88
89 let png: Png | undefined
90 if (e.surface === 'terminal' && svg.isRendered) {
91 if (!pngCache.has(key)) {
92 // The terminal reads the PNG itself, so no pixel crosses $.
93 const dir = `${((await $.env.get('TMPDIR')) ?? '/tmp').replace(/\/$/, '')}/mermaid-render`
94 const name = `${dir}/${hash(key)}`
95 let isRasterized = false
96 try {
97 await $.process.run(['mkdir', '-p', dir])
98 await $.fs.write(`${name}.svg`, svg.svg)
99 const ran = await $.process.run(
100 ['/usr/bin/env', 'rsvg-convert', '--zoom', '2', '--output', `${name}.png`, `${name}.svg`],
101 { timeoutMs: 15000 },
102 )
103 isRasterized = ran.exitCode === 0
104 } catch {
105 isRasterized = false
106 }
107 const evicted = remember(pngCache, key, isRasterized ? { path: `${name}.png`, width: svg.width, height: svg.height } : undefined)
108 // Only the PNG is the cache; the SVG is scratch, and a failed run may leave a half-written PNG.
109 const stale = [`${name}.svg`, ...(isRasterized ? [] : [`${name}.png`]), ...(evicted === undefined ? [] : [evicted.path])]
110 await $.process.run(['rm', '-f', ...stale]).catch(() => undefined)
111 }
112 png = pngCache.get(key)
113 }
114 prepared.set(source, { svg, png })
115 }
116
117 const ui = $.ui.resolve(e)
118 const draw: Draw = {
119 ui,
120 surface: e.surface,
121 width: (e.viewport?.columns ?? 100) - indent - CARD_CHROME,
122 rows: e.viewport?.rows ?? 40,
123 prepared,
124 messages,
125 }
126 if (e.component === 'ToolUse' && file !== undefined) return toolRow(draw, e.props.tool, file)
127 if (kept.length > 0) return underEngineRow(draw, await next(e), kept)
128
129 return reply(draw, segments, e.component === 'AssistantMessage' && e.props.isFirstOfReply)
130 })
131}
132
133function reply(draw: Draw, segments: Segment[], isFirstOfReply: boolean): RenderElement {
134 const { Box, Text, Markdown } = draw.ui
135
136 return (
137 <Box flexDirection="row">
138 <Box width={2} flexShrink={0}>
139 <Text>{isFirstOfReply ? '⏺' : ' '}</Text>
140 </Box>
141 <Box flexDirection="column" flexGrow={1} gap={1}>
142 {segments.map((segment, index) =>
143 segment.kind === 'markdown' ? <Markdown key={`md-${index}`} text={segment.text} /> : card(draw, segment.source, index),
144 )}
145 </Box>
146 </Box>
147 )
148}
149
150function underEngineRow(draw: Draw, engineRow: RenderElement, sources: string[]): RenderElement {
151 const { Box } = draw.ui
152
153 return (
154 <Box flexDirection="column">
155 {engineRow}
156 <Box flexDirection="column" marginLeft={5} marginTop={1} gap={1}>
157 {sources.map((source, index) => card(draw, source, index))}
158 </Box>
159 </Box>
160 )
161}
162
163function toolRow(draw: Draw, tool: string, file: FileDiagrams): RenderElement {
164 const { Box, Text } = draw.ui
165
166 return (
167 <Box flexDirection="column">
168 <Text>
169 <Text color="success">⏺ </Text>
170 <Text bold>{tool}</Text>
171 <Text>({file.path})</Text>
172 </Text>
173 <Text dimColor>{countLine(draw.messages, file.lines, file.sources.length)}</Text>
174 <Box flexDirection="column" marginLeft={5} marginTop={1} gap={1}>
175 {file.sources.map((source, index) => card(draw, source, index))}
176 </Box>
177 </Box>
178 )
179}
180
181/** The diagrams a finished, clean foreground Bash run printed. */
182function bashDiagrams(input: unknown, output: unknown): string[] {
183 const command = (input as { command?: unknown } | undefined)?.command
184 const result = output as { stdout?: unknown; interrupted?: unknown; isImage?: unknown; backgroundTaskId?: unknown } | undefined
185 const stdout = result?.stdout
186 const isClean = result?.interrupted !== true && result?.isImage !== true && result?.backgroundTaskId === undefined
187 if (typeof command !== 'string' || typeof stdout !== 'string' || !isClean) return []
188
189 return diagramsInOutput(command, stdout)
190}
191
192/** The diagrams a finished Edit left in its file: the whole of a .mmd, else the fences the edit touched. */
193function editDiagrams(output: unknown): string[] {
194 const result = output as
195 | { filePath?: unknown; oldString?: unknown; newString?: unknown; originalFile?: unknown; replaceAll?: unknown }
196 | undefined
197 const { filePath, oldString, newString, originalFile } = result ?? {}
198 if (typeof filePath !== 'string' || typeof oldString !== 'string' || typeof newString !== 'string') return []
199 const original = originalFile === null ? null : typeof originalFile === 'string' ? originalFile : undefined
200 if (original === undefined) return []
201
202 const edited = applyEdit(original, oldString, newString, result?.replaceAll === true)
203 if (isDiagramFile(filePath)) return edited.content.trim() === '' ? [] : [edited.content]
204
205 return diagramsTouched(edited.content, edited.spans)
206}
207
208/** The diagrams a finished Read or Write handled, with the file they came from. */
209function fileDiagrams(tool: string, output: unknown): FileDiagrams | undefined {
210 const result = output as { type?: string; file?: { filePath: string; content: string }; filePath?: string; content?: string } | undefined
211 const file =
212 tool === 'Read' && result?.type === 'text' && result.file !== undefined
213 ? { path: result.file.filePath, content: result.file.content }
214 : tool === 'Write' && typeof result?.filePath === 'string' && typeof result.content === 'string'
215 ? { path: result.filePath, content: result.content }
216 : undefined
217 if (file === undefined) return undefined
218 const sources = diagramsInFile(file.path, file.content)
219
220 return sources.length === 0 ? undefined : { path: file.path, lines: file.content.split('\n').length, sources }
221}
222
223function card(draw: Draw, source: string, index: number): RenderElement {
224 const { Box, Text, Code } = draw.ui
225 const width = Math.max(20, draw.width)
226 const body = cardBody(draw, source, width)
227 const { kind, direction } = header(source)
228 const isFailed = 'reason' in body
229
230 return (
231 <Box
232 key={`mermaid-${index}`}
233 flexDirection="column"
234 borderStyle="round"
235 borderColor={isFailed ? 'warning' : 'claude'}
236 borderDimColor={!isFailed}
237 paddingX={1}
238 >
239 <Box flexDirection="row" justifyContent="space-between">
240 <Text>
241 <Text color={isFailed ? 'warning' : 'claude'}>{isFailed ? '▲ ' : '◆ '}</Text>
242 <Text bold>{draw.messages.kinds[kind] ?? kind}</Text>
243 {direction !== undefined && <Text dimColor>{` · ${direction}`}</Text>}
244 </Text>
245 <Text dimColor>mermaid</Text>
246 </Box>
247 {'reason' in body ? (
248 <Box flexDirection="column" marginTop={1}>
249 <Text color="warning">{draw.messages.couldNotDraw(body.reason)}</Text>
250 <Code source={source} language="mermaid" />
251 </Box>
252 ) : (
253 <Box flexDirection="column" marginTop={1}>
254 {body.element}
255 </Box>
256 )}
257 </Box>
258 )
259}
260
261/** The picture a surface draws best, falling back to box-drawing art. */
262function cardBody(draw: Draw, source: string, width: number): { element: RenderElement } | { reason: string } {
263 const { ui } = draw
264 const { svg, png } = draw.prepared.get(source) ?? {}
265 const alt = draw.messages.alt(header(source).kind)
266
267 if (draw.surface !== 'terminal' && svg?.isRendered && svg.svg.length <= SVG_LIMIT && 'Svg' in ui) {
268 const { Svg } = ui
269 return { element: <Svg source={svg.svg} alt={alt} /> }
270 }
271
272 if (draw.surface === 'terminal' && png !== undefined && 'Image' in ui) {
273 const { Image } = ui
274 const { columns, rows } = fitImage(png, width, draw.rows)
275 return { element: <Image source={{ file: png.path, format: 'png' }} columns={columns} rows={rows} alt={alt} /> }
276 }
277
278 let art = unicodeCache.get(`${width}|${source}`)
279 if (art === undefined) {
280 art = renderUnicode(source, width)
281 remember(unicodeCache, `${width}|${source}`, art)
282 }
283 if (!art.isRendered) return { reason: art.reason }
284
285 return { element: unicodeArt(ui, art, width, draw.messages) }
286}
287
288/** Fills the card width, shrinking to the terminal height; a cell is about twice as tall as it is wide. */
289function fitImage(png: { width: number; height: number }, width: number, viewportRows: number): { columns: number; rows: number } {
290 const cap = Math.min(255, Math.max(3, viewportRows - 8))
291 const columns = Math.min(255, width)
292 const rows = Math.max(1, Math.round((columns * png.height) / png.width / 2))
293 if (rows <= cap) return { columns, rows }
294
295 return { columns: Math.min(columns, Math.max(1, Math.round((cap * 2 * png.width) / png.height))), rows: cap }
296}
297
298function unicodeArt(ui: ElementTable, art: Unicode, width: number, messages: Messages): RenderElement {
299 const { Box, Text } = ui
300
301 return (
302 <Box flexDirection="column">
303 {art.lines.map(runs => (
304 <Text wrap="truncate-end">
305 {runs.map(run => (
306 <Text color={GLYPH_COLOR[run.glyph]} bold={run.glyph === 'label'}>
307 {run.text}
308 </Text>
309 ))}
310 </Text>
311 ))}
312 {art.isTurned && <Text dimColor>{`\n${messages.turned}`}</Text>}
313 {art.width > width && <Text dimColor>{`\n${messages.tooWide(art.width)}`}</Text>}
314 </Box>
315 )
316}
317
318function parseSvg(svg: string): Svg {
319 const [, width = '640', height = '360'] = /viewBox="[\d.]+ [\d.]+ ([\d.]+) ([\d.]+)"/.exec(svg) ?? []
320
321 return { isRendered: true, svg, width: Number(width), height: Number(height) }
322}
323
324function failure(source: string, reason: string): Failure {
325 return { isRendered: false, kind: header(source).kind, reason: reason.trim() }
326}
327
328/** Stores the value and returns the entry it pushed out, if any. */
329function remember<T>(cache: Map<string, T>, key: string, value: T): T | undefined {
330 let evicted: T | undefined
331 if (cache.size >= CACHE_LIMIT) {
332 const oldest = cache.keys().next().value as string
333 evicted = cache.get(oldest)
334 cache.delete(oldest)
335 }
336 cache.set(key, value)
337
338 return evicted
339}
340
341function hash(text: string): string {
342 let value = 0x811c9dc5
343 for (let index = 0; index < text.length; index += 1) {
344 value ^= text.charCodeAt(index)
345 value = Math.imul(value, 0x01000193)
346 }
347
348 return (value >>> 0).toString(16)
349}
350hooks/diagram.ts 92 lines1import { renderMermaidASCII } from './vendor/mermaid.js'
2
3export type Glyph = 'label' | 'line' | 'arrow'
4export type Run = { glyph: Glyph; text: string }
5
6export type Unicode = {
7 isRendered: true
8 kind: string
9 direction?: string
10 /** Set when a horizontal flowchart was laid out vertically to fit. */
11 isTurned: boolean
12 lines: Run[][]
13 width: number
14}
15
16export type Failure = { isRendered: false; kind: string; reason: string }
17
18const COMPACT = { colorMode: 'none', paddingX: 4, paddingY: 2, boxBorderPadding: 0 } as const
19const ARROWS = new Set('►▶◀◄▲▼△▽▷◁→←↑↓')
20const LINES = /[─-╿◇◆○●]/
21
22/**
23 * The header Mermaid itself reads: the first line that is neither blank, a
24 * %% comment nor front matter. Its first word is the kind, its second the
25 * direction of a flowchart.
26 */
27export function header(source: string): { kind: string; direction?: string } {
28 const lines = source.split('\n').map(line => line.trim())
29 let index = 0
30 if (lines[0] === '---') index = lines.indexOf('---', 1) + 1
31 const first = lines.slice(index).find(line => line !== '' && !line.startsWith('%%')) ?? ''
32 const [kind = 'diagram', direction] = first.split(/\s+/)
33 const isFlow = kind === 'flowchart' || kind === 'graph'
34
35 return { kind, direction: isFlow && direction ? direction.toUpperCase() : undefined }
36}
37
38/** Box-drawing art cut into colored runs, laid out vertically when too wide. */
39export function renderUnicode(source: string, columns: number): Unicode | Failure {
40 const { kind, direction } = header(source)
41 try {
42 let art = tidy(renderMermaidASCII(source, COMPACT))
43 let isTurned = false
44 if (widest(art) > columns && (direction === 'LR' || direction === 'RL')) {
45 const turned = tidy(renderMermaidASCII(turnVertical(source), COMPACT))
46 if (widest(turned) < widest(art)) {
47 art = turned
48 isTurned = true
49 }
50 }
51
52 return { isRendered: true, kind, direction, isTurned, lines: art.map(toRuns), width: widest(art) }
53 } catch (error) {
54 return { isRendered: false, kind, reason: firstLine(error) }
55 }
56}
57
58function turnVertical(source: string): string {
59 return source.replace(/^(\s*(?:flowchart|graph)\s+)(LR|RL)\b/im, '$1TD')
60}
61
62function tidy(text: string): string[] {
63 const lines = text.split('\n').map(line => line.replace(/\s+$/, ''))
64 while (lines.length > 0 && lines[0] === '') lines.shift()
65 while (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
66
67 return lines
68}
69
70function widest(lines: string[]): number {
71 return lines.reduce((most, line) => Math.max(most, [...line].length), 0)
72}
73
74function toRuns(line: string): Run[] {
75 const runs: Run[] = []
76 for (const char of line) {
77 const glyph: Glyph = ARROWS.has(char) ? 'arrow' : LINES.test(char) || char === '╌' ? 'line' : 'label'
78 const last = runs[runs.length - 1]
79 // A space joins whatever run it sits in, so labels stay one run each.
80 if (last !== undefined && (last.glyph === glyph || char === ' ')) last.text += char
81 else runs.push({ glyph, text: char })
82 }
83
84 return runs
85}
86
87function firstLine(error: unknown): string {
88 const message = error instanceof Error ? error.message : String(error)
89
90 return message.split('\n')[0] ?? message
91}
92hooks/fences.ts 136 lines1import { header } from './diagram'
2
3export type Segment =
4 | { kind: 'markdown'; text: string }
5 | { kind: 'mermaid'; source: string }
6
7const OPENER = /^ {0,3}(`{3,}|~{3,})(.*)$/
8
9/**
10 * Splits markdown into prose and mermaid fences, CommonMark style: the closer
11 * repeats the opener's character at least as long, and a mermaid fence shown
12 * inside another fence is documentation, left as prose. An unclosed mermaid
13 * fence (a reply still streaming) stays prose until it closes.
14 */
15export function splitMermaid(markdown: string): Segment[] {
16 const { lines, fences } = scanFences(markdown)
17 const segments: Segment[] = []
18 let proseFrom = 0
19
20 const flushProse = (to: number) => {
21 const text = lines.slice(proseFrom, to).join('\n').replace(/^\n+|\s+$/g, '')
22 if (text !== '') segments.push({ kind: 'markdown', text })
23 }
24
25 for (const fence of fences) {
26 flushProse(fence.opener)
27 segments.push({ kind: 'mermaid', source: fence.source })
28 proseFrom = fence.closer + 1
29 }
30 flushProse(lines.length)
31
32 return segments
33}
34
35/** A closed top-level mermaid fence: the lines of its opener and closer, and what sits between. */
36type Fence = { opener: number; closer: number; source: string }
37
38function scanFences(markdown: string): { lines: string[]; fences: Fence[] } {
39 const lines = markdown.split('\n').map(line => (line.endsWith('\r') ? line.slice(0, -1) : line))
40 const fences: Fence[] = []
41 let index = 0
42
43 while (index < lines.length) {
44 const opener = OPENER.exec(lines[index] ?? '')
45 if (opener === null) {
46 index += 1
47 continue
48 }
49 const closer = closingLine(lines, index + 1, opener[1] ?? '```')
50 const isMermaid = (opener[2] ?? '').trim().split(/\s+/)[0]?.toLowerCase() === 'mermaid'
51 if (closer !== -1 && isMermaid) fences.push({ opener: index, closer, source: lines.slice(index + 1, closer).join('\n') })
52 index = closer === -1 ? lines.length : closer + 1
53 }
54
55 return { lines, fences }
56}
57
58function closingLine(lines: string[], from: number, fence: string): number {
59 const pattern = new RegExp(`^ {0,3}\\${fence[0]}{${fence.length},}\\s*$`)
60 for (let index = from; index < lines.length; index += 1) {
61 if (pattern.test(lines[index] ?? '')) return index
62 }
63
64 return -1
65}
66
67/** The mermaid sources a file holds: the whole of a .mmd, the fences of anything else. */
68export function diagramsInFile(path: string, content: string): string[] {
69 if (isDiagramFile(path)) return content.trim() === '' ? [] : [content]
70
71 return splitMermaid(content).flatMap(segment => (segment.kind === 'mermaid' ? [segment.source] : []))
72}
73
74export function isDiagramFile(path: string): boolean {
75 return /\.(mmd|mermaid)$/i.test(path)
76}
77
78const DIAGRAM_KINDS = new Set([
79 'flowchart', 'flowchart-elk', 'graph', 'sequenceDiagram', 'stateDiagram', 'stateDiagram-v2', 'classDiagram', 'erDiagram',
80 'pie', 'gantt', 'journey', 'gitGraph', 'mindmap', 'timeline', 'quadrantChart', 'requirementDiagram', 'sankey-beta',
81 'xychart-beta', 'block-beta', 'architecture-beta', 'packet-beta', 'kanban', 'C4Context',
82])
83
84/** The mermaid sources a finished shell command printed: its fences, else the whole of a .mmd/.mermaid file it named. */
85export function diagramsInOutput(command: string, stdout: string): string[] {
86 const fenced = splitMermaid(stdout).flatMap(segment => (segment.kind === 'mermaid' ? [segment.source] : []))
87 if (fenced.length > 0) return fenced
88
89 const namesDiagramFile = /\.(mmd|mermaid)(?=$|[\s'"`;|&<>)])/i.test(command)
90 const source = stdout.replace(/\r\n/g, '\n')
91
92 return namesDiagramFile && DIAGRAM_KINDS.has(header(source).kind) ? [source] : []
93}
94
95/** A character range of the edited file that the edit wrote. */
96export type Span = { start: number; end: number }
97
98/** The file an edit leaves behind and the ranges it wrote. */
99export function applyEdit(original: string | null, oldString: string, newString: string, replaceAll: boolean): { content: string; spans: Span[] } {
100 if (original === null) return { content: newString, spans: [{ start: 0, end: newString.length }] }
101 if (oldString === '') return { content: original, spans: [] }
102
103 let content = ''
104 let from = 0
105 const spans: Span[] = []
106 for (let at = original.indexOf(oldString); at !== -1; at = replaceAll ? original.indexOf(oldString, from) : -1) {
107 content += original.slice(from, at)
108 spans.push({ start: content.length, end: content.length + newString.length })
109 content += newString
110 from = at + oldString.length
111 }
112
113 return { content: content + original.slice(from), spans }
114}
115
116/** The mermaid fences of a markdown text whose lines, opener to closer, overlap a written span. */
117export function diagramsTouched(markdown: string, spans: Span[]): string[] {
118 const starts: number[] = []
119 let offset = 0
120 for (const line of markdown.split('\n')) {
121 starts.push(offset)
122 offset += line.length + 1
123 }
124 starts.push(offset)
125 const { fences } = scanFences(markdown)
126
127 return fences.flatMap(fence => {
128 const start = starts[fence.opener] ?? 0
129 // The raw closer line, \r included: scanFences strips the \r from its lines.
130 const end = (starts[fence.closer + 1] ?? 0) - 1
131 const isTouched = spans.some(span => span.start < end && span.end > start)
132
133 return isTouched ? [fence.source] : []
134 })
135}
136hooks/messages.ts 63 lines1type Plural = { one: string; other: string }
2
3export type Messages = {
4 kinds: Record<string, string>
5 lines: Plural
6 diagrams: Plural
7 couldNotDraw: (reason: string) => string
8 turned: string
9 tooWide: (columns: number) => string
10 alt: (kind: string) => string
11 plural: Intl.PluralRules
12}
13
14const en: Messages = {
15 kinds: {
16 flowchart: 'Flowchart',
17 graph: 'Flowchart',
18 sequenceDiagram: 'Sequence',
19 stateDiagram: 'State',
20 'stateDiagram-v2': 'State',
21 classDiagram: 'Class',
22 erDiagram: 'Entity relationship',
23 },
24 lines: { one: 'line', other: 'lines' },
25 diagrams: { one: 'Mermaid diagram', other: 'Mermaid diagrams' },
26 couldNotDraw: reason => `Could not draw: ${reason}`,
27 turned: '↕ laid out vertically to fit the terminal width',
28 tooWide: columns => `↔ the diagram is ${columns} columns wide; widen the terminal to see all of it`,
29 alt: kind => `${kind} diagram`,
30 plural: new Intl.PluralRules('en'),
31}
32
33const ptBR: Messages = {
34 kinds: {
35 flowchart: 'Fluxograma',
36 graph: 'Fluxograma',
37 sequenceDiagram: 'Sequência',
38 stateDiagram: 'Estados',
39 'stateDiagram-v2': 'Estados',
40 classDiagram: 'Classes',
41 erDiagram: 'Entidade-relacionamento',
42 },
43 lines: { one: 'linha', other: 'linhas' },
44 diagrams: { one: 'diagrama Mermaid', other: 'diagramas Mermaid' },
45 couldNotDraw: reason => `Não foi possível desenhar: ${reason}`,
46 turned: '↕ disposto na vertical para caber na largura do terminal',
47 tooWide: columns => `↔ o diagrama tem ${columns} colunas; alargue o terminal para vê-lo inteiro`,
48 alt: kind => `diagrama ${kind}`,
49 plural: new Intl.PluralRules('pt-BR'),
50}
51
52/** Only the exact value `pt-BR` selects Portuguese; anything else, or nothing, is English. */
53export function messagesFor(language: unknown): Messages {
54 return language === 'pt-BR' ? ptBR : en
55}
56
57/** The count line of a tool row, with each language's plural form. */
58export function countLine(messages: Messages, lines: number, diagrams: number): string {
59 const word = (count: number, forms: Plural) => forms[messages.plural.select(count) === 'one' ? 'one' : 'other']
60
61 return ` ⎿ ${lines} ${word(lines, messages.lines)} · ${diagrams} ${word(diagrams, messages.diagrams)}`
62}
63hooks/vendor/mermaid.js 13 lines1var oe;var pe=(oe=String.fromCodePoint)!==null&&oe!==void 0?oe:t=>{let e="";return t>65535&&(t-=65536,e+=String.fromCharCode(t>>>10&1023|55296),t=56320|t&1023),e+=String.fromCharCode(t),e};var re;(function(t){t[t.VALUE_LENGTH=49152]="VALUE_LENGTH",t[t.FLAG13=8192]="FLAG13",t[t.BRANCH_LENGTH=8064]="BRANCH_LENGTH",t[t.JUMP_TABLE=127]="JUMP_TABLE"})(re||(re={}));var ye;(function(t){t[t.NUM=35]="NUM",t[t.SEMI=59]="SEMI",t[t.EQUALS=61]="EQUALS",t[t.ZERO=48]="ZERO",t[t.NINE=57]="NINE",t[t.LOWER_A=97]="LOWER_A",t[t.LOWER_F=102]="LOWER_F",t[t.LOWER_X=120]="LOWER_X",t[t.LOWER_Z=122]="LOWER_Z",t[t.UPPER_A=65]="UPPER_A",t[t.UPPER_F=70]="UPPER_F",t[t.UPPER_Z=90]="UPPER_Z"})(ye||(ye={}));var be;(function(t){t[t.EntityStart=0]="EntityStart",t[t.NumericStart=1]="NumericStart",t[t.NumericDecimal=2]="NumericDecimal",t[t.NumericHex=3]="NumericHex",t[t.NamedEntity=4]="NamedEntity"})(be||(be={}));var jt;(function(t){t[t.Legacy=0]="Legacy",t[t.Strict=1]="Strict",t[t.Attribute=2]="Attribute"})(jt||(jt={}));var yn=String.prototype.codePointAt==null?(t,e)=>(t.charCodeAt(e)&64512)===55296?(t.charCodeAt(e)-55296)*1024+t.charCodeAt(e+1)-56320+65536:t.charCodeAt(e):(t,e)=>t.codePointAt(e);var we;(function(t){t[t.XML=0]="XML",t[t.HTML=1]="HTML"})(we||(we={}));var Ce;(function(t){t[t.UTF8=0]="UTF8",t[t.ASCII=1]="ASCII",t[t.Extensive=2]="Extensive",t[t.Attribute=3]="Attribute",t[t.Text=4]="Text"})(Ce||(Ce={}));function lt(t){return(t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t).replace(/<br\s*\/?>/gi,`
2`).replace(/\\n/g,`
3`).replace(/<\/?(?:sub|sup|small|mark)\s*>/gi,"").replace(/\*\*(.+?)\*\*/g,"<b>$1</b>").replace(/(?<!\*)\*([^\s*](?:[^*]*[^\s*])?)\*(?!\*)/g,"<i>$1</i>").replace(/~~(.+?)~~/g,"<s>$1</s>")}function Mn(t){let e=t.split(`
4`).map(s=>s.trim()).filter(s=>s.length>0&&!s.startsWith("%%"));if(e.length===0)throw new Error("Empty mermaid diagram");let n=e[0];return/^stateDiagram(-v2)?\s*$/i.test(n)?$n(e):kn(e)}function kn(t){let e=t[0].match(/^(?:graph|flowchart)\s+(TD|TB|LR|BT|RL)\s*$/i);if(!e)throw new Error(`Invalid mermaid header: "${t[0]}". Expected "graph TD", "flowchart LR", "stateDiagram-v2", etc.`);let s={direction:e[1].toUpperCase(),nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map},i=[];for(let o=1;o<t.length;o++){let r=t[o],c=r.match(/^classDef\s+(\w+)\s+(.+)$/);if(c){let h=c[1],g=c[2],f=qt(g);s.classDefs.set(h,f);continue}let a=r.match(/^class\s+([\w,-]+)\s+(\w+)$/);if(a){let h=a[1].split(",").map(f=>f.trim()),g=a[2];for(let f of h)s.classAssignments.set(f,g);continue}let A=r.match(/^style\s+([\w,-]+)\s+(.+)$/);if(A){let h=A[1].split(",").map(f=>f.trim()),g=qt(A[2]);for(let f of h)s.nodeStyles.set(f,{...s.nodeStyles.get(f),...g});continue}let l=r.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(l){let h=l[1].trim(),g=qt(l[2]);if(h==="default")s.linkStyles.set("default",{...s.linkStyles.get("default"),...g});else{let f=h.split(",").map(p=>parseInt(p.trim(),10));for(let p of f)isNaN(p)||s.linkStyles.set(p,{...s.linkStyles.get(p),...g})}continue}let u=r.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(u&&i.length>0){i[i.length-1].direction=u[1].toUpperCase();continue}let d=r.match(/^subgraph\s+(.+)$/);if(d){let h=d[1].trim(),g=h.match(/^([\w-]+)\s*\[(.+)\]$/),f,p;g?(f=g[1],p=lt(g[2])):(p=lt(h),f=h.replace(/\s+/g,"_").replace(/[^\w]/g,""));let I={id:f,label:p,nodeIds:[],children:[]};i.push(I);continue}if(r==="end"){let h=i.pop();h&&(i.length>0?i[i.length-1].children.push(h):s.subgraphs.push(h));continue}Yn(r,s,i)}return s}function $n(t){let e={direction:"TD",nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map},n=[],s=new Set,i=0,o=0;for(let r=1;r<t.length;r++){let c=t[r],a=c.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(a){n.length>0?n[n.length-1].direction=a[1].toUpperCase():e.direction=a[1].toUpperCase();continue}let A=c.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(A){let g=A[1].trim(),f=qt(A[2]);if(g==="default")e.linkStyles.set("default",{...e.linkStyles.get("default"),...f});else{let p=g.split(",").map(I=>parseInt(I.trim(),10));for(let I of p)isNaN(I)||e.linkStyles.set(I,{...e.linkStyles.get(I),...f})}continue}let l=c.match(/^state\s+(?:"([^"]+)"\s+as\s+)?([\w\p{L}]+)\s*\{$/u);if(l){let g=l[1]??l[2],f=l[2],p={id:f,label:g,nodeIds:[],children:[]};n.push(p),s.add(f),e.nodes.delete(f);continue}if(c==="}"){let g=n.pop();g&&(n.length>0?n[n.length-1].children.push(g):e.subgraphs.push(g));continue}let u=c.match(/^state\s+"([^"]+)"\s+as\s+([\w\p{L}]+)\s*$/u);if(u){let g=lt(u[1]),f=u[2];Xt(e,n,{id:f,label:g,shape:"rounded"});continue}let d=c.match(/^(\[\*\]|[\w\p{L}-]+)\s*(-->)\s*(\[\*\]|[\w\p{L}-]+)(?:\s*:\s*(.+))?$/u);if(d){let g=d[1],f=d[3],p=d[4]?.trim(),I=p?lt(p):void 0;g==="[*]"?(i++,g=`_start${i>1?i:""}`,Xt(e,n,{id:g,label:"",shape:"state-start"})):s.has(g)||Be(e,n,g),f==="[*]"?(o++,f=`_end${o>1?o:""}`,Xt(e,n,{id:f,label:"",shape:"state-end"})):s.has(f)||Be(e,n,f),e.edges.push({source:g,target:f,label:I,style:"solid",hasArrowStart:!1,hasArrowEnd:!0});continue}let h=c.match(/^([\w\p{L}-]+)\s*:\s*(.+)$/u);if(h){let g=h[1],f=lt(h[2].trim());Xt(e,n,{id:g,label:f,shape:"rounded"});continue}}return e}function Xt(t,e,n){if(!t.nodes.has(n.id)&&t.nodes.set(n.id,n),e.length>0){let i=e[e.length-1];i.nodeIds.includes(n.id)||i.nodeIds.push(n.id)}}function Be(t,e,n){if(!t.nodes.has(n))Xt(t,e,{id:n,label:n,shape:"rounded"});else if(e.length>0){let s=e[e.length-1];s.nodeIds.includes(n)||s.nodeIds.push(n)}}function qt(t){let e=t.replace(/;\s*$/,""),n={};for(let s of e.split(",")){let i=s.indexOf(":");if(i>0){let o=s.slice(0,i).trim(),r=s.slice(i+1).trim();o&&r&&(n[o]=r)}}return n}var Dn=/^(<)?(-->|-.->|==>|---|-\.-|===)(?:\|([^|]*)\|)?/,vn=/^(<)?(--|-\.|==)\s+(.+?)\s+(-->|---|\.\->|-\.\-|==>|===)/,Sn=[{regex:/^([\w-]+)\(\(\((.+?)\)\)\)/,shape:"doublecircle"},{regex:/^([\w-]+)\(\[(.+?)\]\)/,shape:"stadium"},{regex:/^([\w-]+)\(\((.+?)\)\)/,shape:"circle"},{regex:/^([\w-]+)\[\[(.+?)\]\]/,shape:"subroutine"},{regex:/^([\w-]+)\[\((.+?)\)\]/,shape:"cylinder"},{regex:/^([\w-]+)\[\/(.+?)\\\]/,shape:"trapezoid"},{regex:/^([\w-]+)\[\\(.+?)\/\]/,shape:"trapezoid-alt"},{regex:/^([\w-]+)>(.+?)\]/,shape:"asymmetric"},{regex:/^([\w-]+)\{\{(.+?)\}\}/,shape:"hexagon"},{regex:/^([\w-]+)\[(.+?)\]/,shape:"rectangle"},{regex:/^([\w-]+)\((.+?)\)/,shape:"rounded"},{regex:/^([\w-]+)\{(.+?)\}/,shape:"diamond"}],En=/^([\w-]+)/,Wn=/^:::([\w][\w-]*)/;function Yn(t,e,n){let s=t.trim(),i=Me(s,e,n);if(!i||i.ids.length===0)return;s=i.remaining.trim();let o=i.ids;for(;s.length>0;){let r,c,a,A,l=s.match(Dn);if(l){r=!!l[1];let d=l[2],h=l[3]?.trim();A=h?lt(h):void 0,s=s.slice(l[0].length).trim(),c=Qn(d),a=d.endsWith(">")}else{let d=s.match(vn);if(!d)break;r=!!d[1];let h=d[3].trim();A=h?lt(h):void 0;let g=d[2],f=d[4];s=s.slice(d[0].length).trim(),c=Hn(g,f),a=f.endsWith(">")}let u=Me(s,e,n);if(!u||u.ids.length===0)break;s=u.remaining.trim();for(let d of o)for(let h of u.ids)e.edges.push({source:d,target:h,label:A,style:c,hasArrowStart:r,hasArrowEnd:a});o=u.ids}}function Me(t,e,n){let s=ke(t,e,n);if(!s)return null;let i=[s.id],o=s.remaining.trim();for(;o.startsWith("&");){o=o.slice(1).trim();let r=ke(o,e,n);if(!r)break;i.push(r.id),o=r.remaining.trim()}return{ids:i,remaining:o}}function ke(t,e,n){let s=null,i=t;for(let{regex:r,shape:c}of Sn){let a=t.match(r);if(a){s=a[1];let A=lt(a[2]);$e(e,n,{id:s,label:A,shape:c}),i=t.slice(a[0].length);break}}if(s===null){let r=t.match(En);r&&(s=r[1],e.nodes.has(s)||$e(e,n,{id:s,label:s,shape:"rectangle"}),i=t.slice(r[0].length))}if(s===null)return null;let o=i.match(Wn);return o&&(e.classAssignments.set(s,o[1]),i=i.slice(o[0].length)),{id:s,remaining:i}}function $e(t,e,n){!t.nodes.has(n.id)&&t.nodes.set(n.id,n),Fn(e,n.id)}function Fn(t,e){if(t.length>0){let n=t[t.length-1];n.nodeIds.includes(e)||n.nodeIds.push(e)}}function Qn(t){return t==="-.->"||t==="-.-"?"dotted":t==="==>"||t==="==="?"thick":"solid"}function Hn(t,e){return t==="-."||e===".->"||e==="-.-"?"dotted":t==="=="||e==="==>"||e==="==="?"thick":"solid"}var T={x:1,y:0},J={x:1,y:2},U={x:0,y:1},X={x:2,y:1},wt={x:2,y:0},yt={x:0,y:0},Bt={x:2,y:2},Ct={x:0,y:2},Ht={x:1,y:1};function De(t,e){return t.x===e.x&&t.y===e.y}function fe(t,e){return t.x===e.x&&t.y===e.y}function Ot(t,e){return{x:t.x+e.x,y:t.y+e.y}}function Wt(t){return`${t.x},${t.y}`}var Ln={name:"",styles:{}},Ne={fg:"#27272a",border:"#a1a1aa",line:"#71717a",arrow:"#52525b",corner:"#71717a",junction:"#a1a1aa"};function Gn(){let t=globalThis.process;if(t){if(!t.stdout?.isTTY)return"none";let e=t.env?.COLORTERM?.toLowerCase()??"",n=t.env?.TERM?.toLowerCase()??"";return e==="truecolor"||e==="24bit"?"truecolor":n.includes("256color")||n.includes("256")?"ansi256":n&&n!=="dumb"?"ansi16":"none"}return typeof document<"u"?"html":"none"}function xe(t){let e=t.replace("#","");return e.length===3?{r:parseInt(e[0]+e[0],16),g:parseInt(e[1]+e[1],16),b:parseInt(e[2]+e[2],16)}:{r:parseInt(e.substring(0,2),16),g:parseInt(e.substring(2,4),16),b:parseInt(e.substring(4,6),16)}}var ee="\x1B[",_t=`${ee}0m`;function Xe(t){let{r:e,g:n,b:s}=xe(t);return`${ee}38;2;${e};${n};${s}m`}function Rn(t,e,n){let s=(t+e+n)/3;if(Math.max(Math.abs(t-s),Math.abs(e-s),Math.abs(n-s))<10){let A=Math.round(s/255*23);return 232+Math.min(23,Math.max(0,A))}let o=A=>A<48?0:A<115?1:Math.min(5,Math.floor((A-35)/40)),r=o(t),c=o(e),a=o(n);return 16+36*r+6*c+a}function Ze(t){let{r:e,g:n,b:s}=xe(t),i=Rn(e,n,s);return`${ee}38;5;${i}m`}function Te(t){let{r:e,g:n,b:s}=xe(t),i=.299*e+.587*n+.114*s,o=i>100?0:60,r;return e>180&&n<100&&s<100?r=31:n>180&&e<100&&s<100?r=32:e>150&&n>150&&s<100?r=33:s>180&&e<100&&n<100?r=34:e>150&&s>150&&n<100?r=35:n>150&&s>150&&e<100?r=36:i>200?r=37:i<50?r=30:r=37,`${ee}${r+o}m`}function Ue(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function Pe(t,e){return`<span style="color:${t}">${Ue(e)}</span>`}function je(t,e){switch(t){case"text":return e.fg;case"border":return e.border;case"line":return e.line;case"arrow":return e.arrow;case"corner":return e.corner??e.line;case"junction":return e.junction??e.border;default:return e.fg}}function ae(t,e,n){if(n==="none")return"";let s=je(t,e);switch(n){case"truecolor":return Xe(s);case"ansi256":return Ze(s);case"ansi16":return Te(s);default:return""}}function Kn(t,e,n,s){if(s==="none")return t.join("");if(s==="html")return Jn(t,e,n);let i="",o=null,r="";for(let c=0;c<t.length;c++){let a=t[c],A=e[c]??null;if(a===" "){r.length>0&&(o!==null?i+=ae(o,n,s)+r+_t:i+=r,r="",o=null),i+=a;continue}if(A===o){r+=a;continue}r.length>0&&(o!==null?i+=ae(o,n,s)+r+_t:i+=r),r=a,o=A}return r.length>0&&o!==null?i+=ae(o,n,s)+r+_t:r.length>0&&(i+=r),i}function Jn(t,e,n){let s="",i=null,o="",r=()=>{o.length!==0&&(i!==null?s+=Pe(je(i,n),o):s+=Ue(o),o="",i=null)};for(let c=0;c<t.length;c++){let a=t[c],A=e[c]??null;if(a===" "){r(),s+=" ";continue}if(A===i){o+=a;continue}r(),o=a,i=A}return r(),s}function Ae(t,e,n){if(n==="none"||t.length===0)return t;if(n==="html")return Pe(e,t);let s;switch(n){case"truecolor":s=Xe(e);break;case"ansi256":s=Ze(e);break;case"ansi16":s=Te(e);break;default:return t}return`${s}${t}${_t}`}function dt(t,e){let n=[];for(let s=0;s<=t;s++){let i=[];for(let o=0;o<=e;o++)i.push(" ");n.push(i)}return n}function ut(t){let[e,n]=Tt(t);return dt(e,n)}function Ut(t,e){let n=[];for(let s=0;s<=t;s++){let i=[];for(let o=0;o<=e;o++)i.push(null);n.push(i)}return n}function Lt(t,e,n){let s=t.length-1,i=(t[0]?.length??1)-1,o=Math.max(e,s),r=Math.max(n,i),c=Ut(o,r);for(let a=0;a<c.length;a++)for(let A=0;A<c[0].length;A++)a<t.length&&A<t[0].length&&(c[a][A]=t[a][A]);return t.length=0,t.push(...c),t}function Pt(t,e,n,s){(e>=t.length||n>=(t[0]?.length??0))&&Lt(t,e,n),t[e][n]=s}function Tt(t){return[t.length-1,(t[0]?.length??1)-1]}function Ft(t,e,n){let[s,i]=Tt(t),o=Math.max(e,s),r=Math.max(n,i),c=dt(o,r);for(let a=0;a<c.length;a++)for(let A=0;A<c[0].length;A++)a<t.length&&A<t[0].length&&(c[a][A]=t[a][A]);return t.length=0,t.push(...c),t}var Nn=new Set(["\u2500","\u2502","\u250C","\u2510","\u2514","\u2518","\u251C","\u2524","\u252C","\u2534","\u253C","\u2574","\u2575","\u2576","\u2577"]);function ve(t){return Nn.has(t)}function Se(t){return/^[a-zA-Z0-9]$/.test(t)}var Xn={"\u2500":{"\u2502":"\u253C","\u250C":"\u252C","\u2510":"\u252C","\u2514":"\u2534","\u2518":"\u2534","\u251C":"\u253C","\u2524":"\u253C","\u252C":"\u252C","\u2534":"\u2534"},"\u2502":{"\u2500":"\u253C","\u250C":"\u251C","\u2510":"\u2524","\u2514":"\u251C","\u2518":"\u2524","\u251C":"\u251C","\u2524":"\u2524","\u252C":"\u253C","\u2534":"\u253C"},"\u250C":{"\u2500":"\u252C","\u2502":"\u251C","\u2510":"\u252C","\u2514":"\u251C","\u2518":"\u253C","\u251C":"\u251C","\u2524":"\u253C","\u252C":"\u252C","\u2534":"\u253C"},"\u2510":{"\u2500":"\u252C","\u2502":"\u2524","\u250C":"\u252C","\u2514":"\u253C","\u2518":"\u2524","\u251C":"\u253C","\u2524":"\u2524","\u252C":"\u252C","\u2534":"\u253C"},"\u2514":{"\u2500":"\u2534","\u2502":"\u251C","\u250C":"\u251C","\u2510":"\u253C","\u2518":"\u2534","\u251C":"\u251C","\u2524":"\u253C","\u252C":"\u253C","\u2534":"\u2534"},"\u2518":{"\u2500":"\u2534","\u2502":"\u2524","\u250C":"\u253C","\u2510":"\u2524","\u2514":"\u2534","\u251C":"\u253C","\u2524":"\u2524","\u252C":"\u253C","\u2534":"\u2534"},"\u251C":{"\u2500":"\u253C","\u2502":"\u251C","\u250C":"\u251C","\u2510":"\u253C","\u2514":"\u251C","\u2518":"\u253C","\u2524":"\u253C","\u252C":"\u253C","\u2534":"\u253C"},"\u2524":{"\u2500":"\u253C","\u2502":"\u2524","\u250C":"\u253C","\u2510":"\u2524","\u2514":"\u253C","\u2518":"\u2524","\u251C":"\u253C","\u252C":"\u253C","\u2534":"\u253C"},"\u252C":{"\u2500":"\u252C","\u2502":"\u253C","\u250C":"\u252C","\u2510":"\u252C","\u2514":"\u253C","\u2518":"\u253C","\u251C":"\u253C","\u2524":"\u253C","\u2534":"\u253C"},"\u2534":{"\u2500":"\u2534","\u2502":"\u253C","\u250C":"\u253C","\u2510":"\u253C","\u2514":"\u2534","\u2518":"\u2534","\u251C":"\u253C","\u2524":"\u253C","\u252C":"\u253C"}};function Zn(t,e){return Xn[t]?.[e]??t}function It(t,e,n,...s){let[i,o]=Tt(t);for(let c of s){let[a,A]=Tt(c);i=Math.max(i,a+e.x),o=Math.max(o,A+e.y)}let r=dt(i,o);for(let c=0;c<=i;c++)for(let a=0;a<=o;a++)c<t.length&&a<t[0].length&&(r[c][a]=t[c][a]);for(let c of s)for(let a=0;a<c.length;a++)for(let A=0;A<c[0].length;A++){let l=c[a][A];if(l!==" "){let u=a+e.x,d=A+e.y,h=r[u][d];!n&&ve(l)&&ve(h)?r[u][d]=Zn(h,l):Se(h)&&Se(l)||(r[u][d]=l)}}return r}function ne(t,e){let[n,s]=Tt(t),i=[],o=e?.roleCanvas,r=e?.colorMode??"none",c=e?.theme??Ne;for(let a=0;a<=s;a++)if(r==="none"||!o){let A="";for(let l=0;l<=n;l++)A+=t[l][a];i.push(A)}else{let A=[],l=[];for(let u=0;u<=n;u++)A.push(t[u][a]),l.push(o[u]?.[a]??null);i.push(Kn(A,l,c,r))}return i.join(`
5`)}var Tn={"\u25B2":"\u25BC","\u25BC":"\u25B2","\u25E4":"\u25E3","\u25E3":"\u25E4","\u25E5":"\u25E2","\u25E2":"\u25E5","^":"v",v:"^","\u250C":"\u2514","\u2514":"\u250C","\u2510":"\u2518","\u2518":"\u2510","\u252C":"\u2534","\u2534":"\u252C","\u2575":"\u2577","\u2577":"\u2575"};function Un(t){for(let e of t)e.reverse();for(let e of t)for(let n=0;n<e.length;n++){let s=Tn[e[n]];s&&(e[n]=s)}return t}function Pn(t){for(let e of t)e.reverse();return t}function jn(t,e,n,s=!1){Ft(t,e.x+n.length,e.y);for(let i=0;i<n.length;i++){let o=e.x+i,r=t[o][e.y];(s||r===" ")&&(t[o][e.y]=n[i])}}function On(t,e,n){let s=0,i=0;for(let o of e.values())s+=o;for(let o of n.values())i+=o;Ft(t,s-1,i-1)}function Vn(t,e,n){let s=0,i=0;for(let o of e.values())s+=o;for(let o of n.values())i+=o;Lt(t,s-1,i-1)}function zn(t,e){let n=new Map,s=0;for(let[c,a]of t.nodes){let A={name:c,displayLabel:a.label,shape:a.shape,index:s,gridCoord:null,drawingCoord:null,drawing:null,drawn:!1,styleClassName:"",styleClass:Ln};n.set(c,A),s++}let i=[...n.values()],o=[];for(let c of t.edges){let a=n.get(c.source),A=n.get(c.target);!a||!A||o.push({from:a,to:A,text:c.label??"",path:[],labelLine:[],startDir:{x:0,y:0},endDir:{x:0,y:0},style:c.style,hasArrowStart:c.hasArrowStart,hasArrowEnd:c.hasArrowEnd})}let r=[];for(let c of t.subgraphs)Oe(c,null,n,r);qn(t.subgraphs,r,n,t);for(let[c,a]of t.classAssignments){let A=n.get(c),l=t.classDefs.get(a);A&&l&&(A.styleClassName=a,A.styleClass={name:a,styles:l})}return{nodes:i,edges:o,canvas:dt(0,0),roleCanvas:Ut(0,0),grid:new Map,columnWidth:new Map,rowHeight:new Map,subgraphs:r,config:e,offsetX:0,offsetY:0,bundles:[]}}function Oe(t,e,n,s){let i;t.direction&&(i=t.direction==="LR"||t.direction==="RL"?"LR":"TD");let o={name:t.label,nodes:[],parent:e,children:[],minX:0,minY:0,maxX:0,maxY:0,direction:i};for(let r of t.nodeIds){let c=n.get(r);c&&o.nodes.push(c)}s.push(o);for(let r of t.children){let c=Oe(r,o,n,s);o.children.push(c);for(let a of c.nodes)o.nodes.includes(a)||o.nodes.push(a)}return o}function qn(t,e,n,s){let i=new Map;to(t,e,i);let o=new Map;function r(c){let a=i.get(c);if(a){for(let A of c.children)r(A);for(let A of c.nodeIds)o.has(A)||o.set(A,a)}}for(let c of t)r(c);for(let c of e)c.nodes=c.nodes.filter(a=>{let A;for(let[u,d]of n)if(d===a){A=u;break}if(!A)return!1;let l=o.get(A);return l?_n(c,l):!0})}function _n(t,e){let n=e;for(;n!==null;){if(n===t)return!0;n=n.parent}return!1}function to(t,e,n){let s=[];function i(o){for(let r of o)s.push(r),i(r.children)}i(t);for(let o=0;o<s.length&&o<e.length;o++)n.set(s[o],e[o])}var eo=class{items=[];get length(){return this.items.length}push(t){this.items.push(t),this.bubbleUp(this.items.length-1)}pop(){if(this.items.length===0)return;let t=this.items[0],e=this.items.pop();return this.items.length>0&&(this.items[0]=e,this.sinkDown(0)),t}bubbleUp(t){for(;t>0;){let e=t-1>>1;if(this.items[t].priority<this.items[e].priority)[this.items[t],this.items[e]]=[this.items[e],this.items[t]],t=e;else break}}sinkDown(t){let e=this.items.length;for(;;){let n=t,s=2*t+1,i=2*t+2;if(s<e&&this.items[s].priority<this.items[n].priority&&(n=s),i<e&&this.items[i].priority<this.items[n].priority&&(n=i),n!==t)[this.items[t],this.items[n]]=[this.items[n],this.items[t]],t=n;else break}}};function no(t,e){let n=Math.abs(t.x-e.x),s=Math.abs(t.y-e.y);return n===0||s===0?n+s:n+s+1}var oo=[{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}];function ro(t,e){return e.x<0||e.y<0?!1:!t.has(Wt(e))}function Kt(t,e,n){let s=new eo;s.push({coord:e,priority:0});let i=new Map;i.set(Wt(e),0);let o=new Map;for(o.set(Wt(e),null);s.length>0;){let r=s.pop().coord;if(De(r,n)){let a=[],A=r;for(;A!==null;)a.unshift(A),A=o.get(Wt(A))??null;return a}let c=i.get(Wt(r));for(let a of oo){let A={x:r.x+a.x,y:r.y+a.y};if(!ro(t,A)&&!De(A,n))continue;let l=c+1,u=Wt(A),d=i.get(u);if(d===void 0||l<d){i.set(u,l);let h=l+no(A,n);s.push({coord:A,priority:h}),o.set(u,r)}}}return null}function Yt(t){if(t.length<=2)return t;let e=new Set,n=t[0],s=t[1];for(let i=2;i<t.length;i++){let o=t[i],r=s.x-n.x,c=s.y-n.y,a=o.x-s.x,A=o.y-s.y;r===a&&c===A&&e.add(i-1),n=s,s=o}return t.filter((i,o)=>!e.has(o))}function Vt(t){return t===T?J:t===J?T:t===U?X:t===X?U:t===wt?Ct:t===yt?Bt:t===Bt?yt:t===Ct?wt:Ht}function x(t,e){return t.x===e.x&&t.y===e.y}function ft(t,e){return t.x===e.x?t.y<e.y?J:T:t.y===e.y?t.x<e.x?X:U:t.x<e.x?t.y<e.y?Bt:wt:t.y<e.y?Ct:yt}function so(t){return t==="LR"?[X,J,J,X]:[J,X,X,J]}function io(t,e){if(t.from===t.to)return so(e);let n=ft(t.from.gridCoord,t.to.gridCoord),s,i,o,r,c=e==="LR"?x(n,U)||x(n,yt)||x(n,Ct):x(n,T)||x(n,yt)||x(n,wt);return x(n,Bt)?e==="LR"?(s=J,i=U,o=X,r=T):(s=X,i=T,o=J,r=U):x(n,wt)?e==="LR"?(s=T,i=U,o=X,r=J):(s=X,i=J,o=T,r=U):x(n,Ct)?e==="LR"?(s=J,i=J,o=U,r=T):(s=U,i=T,o=J,r=X):x(n,yt)?e==="LR"?(s=J,i=J,o=U,r=J):(s=X,i=X,o=T,r=X):c?e==="LR"&&x(n,U)?(s=J,i=J,o=U,r=X):e==="TD"&&x(n,T)?(s=X,i=X,o=T,r=J):(s=n,i=Vt(n),o=n,r=Vt(n)):(s=n,i=Vt(n),o=n,r=Vt(n)),[s,i,o,r]}function co(t,e){let n=xt(t,e.from),s=xt(t,e.to),i=n&&n===s&&n.direction?n.direction:t.config.graphDirection,[o,r,c,a]=io(e,i),A=Ot(e.from.gridCoord,o),l=Ot(e.to.gridCoord,r),u=Kt(t.grid,A,l),d=Ot(e.from.gridCoord,c),h=Ot(e.to.gridCoord,a),g=Kt(t.grid,d,h);if(u!==null&&g!==null){u=Yt(u),g=Yt(g),u.length<=g.length?(e.startDir=o,e.endDir=r,e.path=u):(e.startDir=c,e.endDir=a,e.path=g);return}if(u!==null){e.startDir=o,e.endDir=r,e.path=Yt(u);return}if(g!==null){e.startDir=c,e.endDir=a,e.path=Yt(g);return}e.startDir=o,e.endDir=r,e.path=[A,l]}function Ee(t,e){if(e.text.length===0)return;let n=e.text.length,s=e.path.length,i=t.config.graphDirection==="TD",o=[];for(let d=1;d<s;d++){let h=e.path[d-1],g=e.path[d],f=[h,g],p=ao(t,f),I=h.x===g.x;o.push({line:f,width:p,index:d,isVertical:I})}let r=o.filter(d=>d.width>=n&&d.index>1),c;if(r.length>0)r.sort((d,h)=>h.index-d.index),c=r[0].line;else{let d=o.filter(h=>h.width>=n);d.length>0?(d.sort((h,g)=>g.index-h.index),c=d[0].line):(o.sort((h,g)=>g.width-h.width),c=o[0]?.line??[e.path[0],e.path[1]])}let a=Math.min(c[0].x,c[1].x),A=Math.max(c[0].x,c[1].x),l=a+Math.floor((A-a)/2),u=t.columnWidth.get(l)??0;t.columnWidth.set(l,Math.max(u,n+2)),e.labelLine=[c[0],c[1]]}function ao(t,e){let n=0,s=Math.min(e[0].x,e[1].x),i=Math.max(e[0].x,e[1].x);for(let o=s;o<=i;o++)n+=t.columnWidth.get(o)??0;return n}function Ao(t){if(t.config.graphDirection!=="TD")return[];let e=[],n=new Set,s=new Map;for(let o of t.edges){if(o.from===o.to)continue;let r=s.get(o.to)??[];r.push(o),s.set(o.to,r)}for(let[o,r]of s){if(r.length<2||!We(r,t)||r.some(a=>n.has(a)))continue;let c={type:"fan-in",edges:[...r],sharedNode:o,otherNodes:r.map(a=>a.from),junctionPoint:null,sharedPath:[],junctionDir:Ht,sharedNodeDir:Ht};for(let a of r)a.bundle=c,n.add(a);e.push(c)}let i=new Map;for(let o of t.edges){if(o.from===o.to||n.has(o))continue;let r=i.get(o.from)??[];r.push(o),i.set(o.from,r)}for(let[o,r]of i){if(r.length<2||!We(r,t))continue;let c={type:"fan-out",edges:[...r],sharedNode:o,otherNodes:r.map(a=>a.to),junctionPoint:null,sharedPath:[],junctionDir:Ht,sharedNodeDir:Ht};for(let a of r)a.bundle=c,n.add(a);e.push(c)}return e}function We(t,e){if(t.length<2)return!1;let n=t[0].style,s=xt(e,t[0].from),i=xt(e,t[0].to);for(let o of t){if(o.style!==n||o.text.length>0)return!1;let r=xt(e,o.from),c=xt(e,o.to);if(r!==s||c!==i||r!==c)return!1}return!0}function lo(t,e){let n=t.config.graphDirection,s=e.sharedNode.gridCoord,i=e.otherNodes.map(o=>o.gridCoord);if(e.type==="fan-in"){let o=Math.min(...i.map(A=>A.x)),r=Math.max(...i.map(A=>A.x)),c=Math.min(...i.map(A=>A.y)),a=Math.max(...i.map(A=>A.y));if(n==="TD"){let A=s.y-1,l=Math.floor((o+r)/2)+1;return{x:s.x+1,y:A}}else{let A=s.x-1,l=s.y+1;return{x:A,y:l}}}else{let o=Math.min(...i.map(A=>A.x)),r=Math.max(...i.map(A=>A.x)),c=Math.min(...i.map(A=>A.y)),a=Math.max(...i.map(A=>A.y));if(n==="TD"){let A=s.y+3;return{x:s.x+1,y:A}}else{let A=s.x+3,l=s.y+1;return{x:A,y:l}}}}function ho(t,e){let n=t.config.graphDirection;e.junctionPoint=lo(t,e);let s=e.junctionPoint;if(e.type==="fan-in"){e.junctionDir=n==="TD"?T:U,e.sharedNodeDir=n==="TD"?J:X;let i=e.sharedNode.gridCoord,o=n==="TD"?{x:i.x+1,y:i.y}:{x:i.x,y:i.y+1},r=Kt(t.grid,s,o);e.sharedPath=r?Yt(r):[s,o];for(let c of e.edges){let a=c.from.gridCoord,A=n==="TD"?{x:a.x+1,y:a.y+2}:{x:a.x+2,y:a.y+1},l=Kt(t.grid,A,s);c.pathToJunction=l?Yt(l):[A,s],c.startDir=n==="TD"?J:X,c.endDir=n==="TD"?T:U,c.path=[...c.pathToJunction,...e.sharedPath.slice(1)]}}else{e.junctionDir=n==="TD"?J:X,e.sharedNodeDir=n==="TD"?T:U;let i=e.sharedNode.gridCoord,o=n==="TD"?{x:i.x+1,y:i.y+2}:{x:i.x+2,y:i.y+1},r=Kt(t.grid,o,s);e.sharedPath=r?Yt(r):[o,s];for(let c of e.edges){let a=c.to.gridCoord,A=n==="TD"?{x:a.x+1,y:a.y}:{x:a.x,y:a.y+1},l=Kt(t.grid,s,A);c.pathToJunction=l?Yt(l):[s,A],c.startDir=n==="TD"?J:X,c.endDir=n==="TD"?T:U,c.path=[...e.sharedPath,...c.pathToJunction.slice(1)]}}}function uo(t){for(let e of t.bundles)ho(t,e)}function ct(t){return t.split(`
6`)}function le(t){let e=ct(t);return Math.max(...e.map(n=>n.length),0)}function Ye(t){return ct(t).length}var Fe={rectangle:{unicode:{tl:"\u250C",tr:"\u2510",bl:"\u2514",br:"\u2518"},ascii:{tl:"+",tr:"+",bl:"+",br:"+"}},rounded:{unicode:{tl:"\u256D",tr:"\u256E",bl:"\u2570",br:"\u256F"},ascii:{tl:".",tr:".",bl:"'",br:"'"}},circle:{unicode:{tl:"\u25EF",tr:"\u25EF",bl:"\u25EF",br:"\u25EF"},ascii:{tl:"o",tr:"o",bl:"o",br:"o"}},doublecircle:{unicode:{tl:"\u25CE",tr:"\u25CE",bl:"\u25CE",br:"\u25CE"},ascii:{tl:"@",tr:"@",bl:"@",br:"@"}},diamond:{unicode:{tl:"\u25C7",tr:"\u25C7",bl:"\u25C7",br:"\u25C7"},ascii:{tl:"<",tr:">",bl:"<",br:">"}},hexagon:{unicode:{tl:"\u231C",tr:"\u231D",bl:"\u231E",br:"\u231F"},ascii:{tl:"*",tr:"*",bl:"*",br:"*"}},stadium:{unicode:{tl:"(",tr:")",bl:"(",br:")"},ascii:{tl:"(",tr:")",bl:"(",br:")"}},subroutine:{unicode:{tl:"\u255F",tr:"\u2562",bl:"\u255F",br:"\u2562"},ascii:{tl:"|",tr:"|",bl:"|",br:"|"}},cylinder:{unicode:{tl:"\u256D",tr:"\u256E",bl:"\u2570",br:"\u256F"},ascii:{tl:".",tr:".",bl:"'",br:"'"}},asymmetric:{unicode:{tl:"\u25B7",tr:"\u2510",bl:"\u25B7",br:"\u2518"},ascii:{tl:">",tr:"+",bl:">",br:"+"}},trapezoid:{unicode:{tl:"/",tr:"\\",bl:"\u2514",br:"\u2518"},ascii:{tl:"/",tr:"\\",bl:"+",br:"+"}},"trapezoid-alt":{unicode:{tl:"\u250C",tr:"\u2510",bl:"\\",br:"/"},ascii:{tl:"+",tr:"+",bl:"\\",br:"/"}},"state-start":{unicode:{tl:"\u25CF",tr:"\u25CF",bl:"\u25CF",br:"\u25CF"},ascii:{tl:"*",tr:"*",bl:"*",br:"*"}},"state-end":{unicode:{tl:"\u25C9",tr:"\u25C9",bl:"\u25C9",br:"\u25C9"},ascii:{tl:"@",tr:"@",bl:"@",br:"@"}}};function kt(t,e){let n=Fe[t]??Fe.rectangle;return e?n.ascii:n.unicode}function Dt(t,e){let n=ct(t),s=Math.max(...n.map(l=>l.length),0),i=n.length,o=2*e.padding+s,r=o+2,c=i+2*e.padding,a=c%2===0?c+1:c,A=a+2;return{width:r,height:A,labelArea:{x:1+e.padding,y:1+e.padding,width:s,height:i},gridColumns:[1,o,1],gridRows:[1,a,1]}}function vt(t,e,n,s){let{width:i,height:o}=e,r=dt(i-1,o-1),c={x:0,y:0},a={x:i-1,y:o-1},A=s?"-":"\u2500",l=s?"|":"\u2502";for(let p=c.x+1;p<a.x;p++)r[p][c.y]=A,r[p][a.y]=A;for(let p=c.y+1;p<a.y;p++)r[c.x][p]=l,r[a.x][p]=l;r[c.x][c.y]=n.tl,r[a.x][c.y]=n.tr,r[c.x][a.y]=n.bl,r[a.x][a.y]=n.br;let u=ct(t),d=i-1,h=o-1,f=Math.floor(h/2)-Math.floor((u.length-1)/2);for(let p=0;p<u.length;p++){let I=u[p],H=Math.floor(d/2)-Math.ceil(I.length/2)+1;for(let B=0;B<I.length;B++){let R=H+B,D=f+p;R>=0&&R<r.length&&D>=0&&D<r[0].length&&(r[R][D]=I[B])}}return r}function bt(t,e,n){let{width:s,height:i}=e,o=n.x+Math.floor(s/2),r=n.y+Math.floor(i/2);return x(t,T)?{x:o,y:n.y}:x(t,J)?{x:o,y:n.y+i-1}:x(t,U)?{x:n.x,y:r}:x(t,X)?{x:n.x+s-1,y:r}:x(t,yt)?{x:n.x,y:n.y}:x(t,wt)?{x:n.x+s-1,y:n.y}:x(t,Ct)?{x:n.x,y:n.y+i-1}:x(t,Bt)?{x:n.x+s-1,y:n.y+i-1}:{x:o,y:r}}var Ve={getDimensions:Dt,render(t,e,n){let s=kt("rectangle",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},go={getDimensions:Dt,render(t,e,n){let s=kt("diamond",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},fo={getDimensions:Dt,render(t,e,n){let s=kt("circle",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},xo={getDimensions(t,e){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(t,e,n){let{width:s,height:i}=e,o=dt(s-1,i-1),r=Math.floor(s/2);return n.useAscii?(o[0][0]=".",o[1][0]="-",o[2][0]="-",o[3][0]="-",o[4][0]=".",o[0][1]="|",o[r][1]="*",o[4][1]="|",o[0][2]="'",o[1][2]="-",o[2][2]="-",o[3][2]="-",o[4][2]="'"):(o[0][0]="\u256D",o[1][0]="\u2500",o[2][0]="\u2500",o[3][0]="\u2500",o[4][0]="\u256E",o[0][1]="\u2502",o[r][1]="\u25CF",o[4][1]="\u2502",o[0][2]="\u2570",o[1][2]="\u2500",o[2][2]="\u2500",o[3][2]="\u2500",o[4][2]="\u256F"),o},getAttachmentPoint(t,e,n){let{width:s,height:i}=e,o=n.x+Math.floor(s/2),r=n.y+Math.floor(i/2);return x(t,T)?{x:o,y:n.y}:x(t,J)?{x:o,y:n.y+i-1}:x(t,U)?{x:n.x,y:r}:x(t,X)?{x:n.x+s-1,y:r}:{x:o,y:r}}},mo={getDimensions(t,e){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(t,e,n){let{width:s,height:i}=e,o=dt(s-1,i-1),r=Math.floor(s/2);return n.useAscii?(o[0][0]="#",o[1][0]="=",o[2][0]="=",o[3][0]="=",o[4][0]="#",o[0][1]="#",o[r][1]="*",o[4][1]="#",o[0][2]="#",o[1][2]="=",o[2][2]="=",o[3][2]="=",o[4][2]="#"):(o[0][0]="\u2554",o[1][0]="\u2550",o[2][0]="\u2550",o[3][0]="\u2550",o[4][0]="\u2557",o[0][1]="\u2551",o[r][1]="\u25CE",o[4][1]="\u2551",o[0][2]="\u255A",o[1][2]="\u2550",o[2][2]="\u2550",o[3][2]="\u2550",o[4][2]="\u255D"),o},getAttachmentPoint(t,e,n){let{width:s,height:i}=e,o=n.x+Math.floor(s/2),r=n.y+Math.floor(i/2);return x(t,T)?{x:o,y:n.y}:x(t,J)?{x:o,y:n.y+i-1}:x(t,U)?{x:n.x,y:r}:x(t,X)?{x:n.x+s-1,y:r}:{x:o,y:r}}},po={getDimensions:Dt,render(t,e,n){let s=kt("rounded",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},yo={getDimensions(t,e){let n=ct(t),s=Math.max(...n.map(A=>A.length),0),i=n.length,o=2*e.padding+s,r=o+4,c=i+2*e.padding,a=Math.max(c+2,3);return{width:r,height:a,labelArea:{x:2+e.padding,y:1+e.padding,width:s,height:i},gridColumns:[2,o,2],gridRows:[1,c,1]}},render(t,e,n){let{width:s,height:i}=e,o=dt(s-1,i-1),r=Math.floor(i/2),c=n.useAscii?"-":"\u2500";if(i===3)o[0][r]="(",o[s-1][r]=")";else if(n.useAscii){for(let l=0;l<i;l++)o[0][l]="(",o[s-1][l]=")";for(let l=1;l<s-1;l++)o[l][0]=c,o[l][i-1]=c}else{o[0][0]="\u256D";for(let l=1;l<s-1;l++)o[l][0]=c;o[s-1][0]="\u256E";for(let l=1;l<i-1;l++)o[0][l]="\u2502",o[s-1][l]="\u2502";o[0][i-1]="\u2570";for(let l=1;l<s-1;l++)o[l][i-1]=c;o[s-1][i-1]="\u256F"}let a=ct(t),A=r-Math.floor((a.length-1)/2);for(let l=0;l<a.length;l++){let u=a[l],d=Math.floor(s/2)-Math.floor(u.length/2);for(let h=0;h<u.length;h++){let g=d+h,f=A+l;g>0&&g<s-1&&f>=0&&f<i&&(o[g][f]=u[h])}}return o},getAttachmentPoint:bt},bo={getDimensions:Dt,render(t,e,n){let s=kt("hexagon",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},wo={getDimensions(t,e){let n=ct(t),s=Math.max(...n.map(A=>A.length),0),i=n.length,o=2*e.padding+s,r=o+4,c=i+2*e.padding,a=c+2;return{width:r,height:a,labelArea:{x:2+e.padding,y:1+e.padding,width:s,height:i},gridColumns:[2,o,2],gridRows:[1,c,1]}},render(t,e,n){let{width:s,height:i}=e,o=dt(s-1,i-1),r=n.useAscii?"-":"\u2500",c=n.useAscii?"|":"\u2502";o[0][0]=n.useAscii?"+":"\u250C",o[1][0]=n.useAscii?"+":"\u252C";for(let u=2;u<s-2;u++)o[u][0]=r;o[s-2][0]=n.useAscii?"+":"\u252C",o[s-1][0]=n.useAscii?"+":"\u2510";for(let u=1;u<i-1;u++)o[0][u]=c,o[1][u]=c,o[s-2][u]=c,o[s-1][u]=c;o[0][i-1]=n.useAscii?"+":"\u2514",o[1][i-1]=n.useAscii?"+":"\u2534";for(let u=2;u<s-2;u++)o[u][i-1]=r;o[s-2][i-1]=n.useAscii?"+":"\u2534",o[s-1][i-1]=n.useAscii?"+":"\u2518";let a=ct(t),l=Math.floor(i/2)-Math.floor((a.length-1)/2);for(let u=0;u<a.length;u++){let d=a[u],h=Math.floor(s/2)-Math.floor(d.length/2);for(let g=0;g<d.length;g++){let f=h+g,p=l+u;f>1&&f<s-2&&p>0&&p<i-1&&(o[f][p]=d[g])}}return o},getAttachmentPoint:bt},Co={getDimensions:Dt,render(t,e,n){let s=kt("doublecircle",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},Io={getDimensions(t,e){let n=ct(t),s=Math.max(...n.map(A=>A.length),0),i=n.length,o=2*e.padding+s,r=o+2,c=i+2*e.padding+2,a=c+2;return{width:r,height:a,labelArea:{x:1+e.padding,y:2+e.padding,width:s,height:i},gridColumns:[1,o,1],gridRows:[2,c-2,2]}},render(t,e,n){let{width:s,height:i}=e,o=dt(s-1,i-1),r=n.useAscii?"-":"\u2500",c=n.useAscii?"|":"\u2502";o[0][0]=n.useAscii?".":"\u256D";for(let u=1;u<s-1;u++)o[u][0]=r;o[s-1][0]=n.useAscii?".":"\u256E",o[0][1]=c;for(let u=1;u<s-1;u++)o[u][1]=r;o[s-1][1]=c;for(let u=2;u<i-2;u++)o[0][u]=c,o[s-1][u]=c;o[0][i-2]=c;for(let u=1;u<s-1;u++)o[u][i-2]=r;o[s-1][i-2]=c,o[0][i-1]=n.useAscii?"'":"\u2570";for(let u=1;u<s-1;u++)o[u][i-1]=r;o[s-1][i-1]=n.useAscii?"'":"\u256F";let a=ct(t),l=Math.floor(i/2)-Math.floor((a.length-1)/2);for(let u=0;u<a.length;u++){let d=a[u],h=Math.floor(s/2)-Math.floor(d.length/2);for(let g=0;g<d.length;g++){let f=h+g,p=l+u;f>0&&f<s-1&&p>1&&p<i-2&&(o[f][p]=d[g])}}return o},getAttachmentPoint:bt},Bo={getDimensions:Dt,render(t,e,n){let s=kt("asymmetric",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},Mo={getDimensions:Dt,render(t,e,n){let s=kt("trapezoid",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},ko={getDimensions:Dt,render(t,e,n){let s=kt("trapezoid-alt",n.useAscii);return vt(t,e,s,n.useAscii)},getAttachmentPoint:bt},$o=new Map([["rectangle",Ve],["rounded",po],["diamond",go],["stadium",yo],["circle",fo],["subroutine",wo],["doublecircle",Co],["hexagon",bo],["cylinder",Io],["asymmetric",Bo],["trapezoid",Mo],["trapezoid-alt",ko],["state-start",xo],["state-end",mo]]);function ze(t){return $o.get(t)??Ve}function Do(t,e,n){return ze(t).getDimensions(e,n)}function vo(t,e,n,s){return ze(t).getAttachmentPoint(e,n,s)}function So(t,e){return Eo(t,e)}function Eo(t,e){let n=t.gridCoord,s=e.config.useAscii,i=0;for(let B=0;B<2;B++)i+=e.columnWidth.get(n.x+B)??0;let o=0;for(let B=0;B<2;B++)o+=e.rowHeight.get(n.y+B)??0;let r={x:0,y:0},c={x:i,y:o},a=dt(Math.max(r.x,c.x),Math.max(r.y,c.y)),A=kt(t.shape,s),l=t.shape==="state-end",u=s?l?"=":"-":l?"\u2550":"\u2500",d=s?l?"\u2016":"|":l?"\u2551":"\u2502",g=l?s?{tl:"#",tr:"#",bl:"#",br:"#"}:{tl:"\u2554",tr:"\u2557",bl:"\u255A",br:"\u255D"}:A;for(let B=r.x+1;B<c.x;B++)a[B][r.y]=u;for(let B=r.x+1;B<c.x;B++)a[B][c.y]=u;for(let B=r.y+1;B<c.y;B++)a[r.x][B]=d;for(let B=r.y+1;B<c.y;B++)a[c.x][B]=d;a[r.x][r.y]=g.tl,a[c.x][r.y]=g.tr,a[r.x][c.y]=g.bl,a[c.x][c.y]=g.br;let f=t.displayLabel,p=ct(f),H=r.y+Math.floor(o/2)-Math.floor((p.length-1)/2);for(let B=0;B<p.length;B++){let R=p[B],D=r.x+Math.floor(i/2)-Math.ceil(R.length/2)+1;for(let j=0;j<R.length;j++)D+j>=0&&D+j<a.length&&H+B>=0&&H+B<a[0].length&&(a[D+j][H+B]=R[j])}return a}function Wo(t,e){return So(t,e)}function qe(t,e,n=1){let s=0;for(let B of t)for(let R of B)s=Math.max(s,R.length);let o=s+2*n+2,r=0;for(let B of t)r+=Math.max(B.length,1);let c=t.length-1,a=r+c+2,A=e?"-":"\u2500",l=e?"|":"\u2502",u=e?"+":"\u250C",d=e?"+":"\u2510",h=e?"+":"\u2514",g=e?"+":"\u2518",f=e?"+":"\u251C",p=e?"+":"\u2524",I=dt(o-1,a-1);I[0][0]=u;for(let B=1;B<o-1;B++)I[B][0]=A;I[o-1][0]=d,I[0][a-1]=h;for(let B=1;B<o-1;B++)I[B][a-1]=A;I[o-1][a-1]=g;for(let B=1;B<a-1;B++)I[0][B]=l,I[o-1][B]=l;let H=1;for(let B=0;B<t.length;B++){let R=t[B],D=R.length>0?R:[""];for(let j of D){let ht=1+n;for(let L=0;L<j.length;L++)I[ht+L][H]=j[L];H++}if(B<t.length-1){I[0][H]=f;for(let j=1;j<o-1;j++)I[j][H]=A;I[o-1][H]=p,H++}}return I}var Yo={solid:{h:{unicode:"\u2500",ascii:"-"},v:{unicode:"\u2502",ascii:"|"}},dotted:{h:{unicode:"\u2504",ascii:"."},v:{unicode:"\u2506",ascii:":"}},thick:{h:{unicode:"\u2501",ascii:"="},v:{unicode:"\u2503",ascii:"\u2016"}}};function me(t,e,n,s,i,o,r="solid"){let c=ft(e,n),a=[],A=Yo[r],l=o?A.h.ascii:A.h.unicode,u=o?A.v.ascii:A.v.unicode;if(x(c,T))for(let d=e.y-s;d>=n.y-i;d--)a.push({x:e.x,y:d}),t[e.x][d]=u;else if(x(c,J))for(let d=e.y+s;d<=n.y+i;d++)a.push({x:e.x,y:d}),t[e.x][d]=u;else if(x(c,U))for(let d=e.x-s;d>=n.x-i;d--)a.push({x:d,y:e.y}),t[d][e.y]=l;else if(x(c,X))for(let d=e.x+s;d<=n.x+i;d++)a.push({x:d,y:e.y}),t[d][e.y]=l;else if(x(c,yt)){for(let d=e.x-s;d>=n.x;d--)a.push({x:d,y:e.y}),t[d][e.y]=l;for(let d=e.y-1;d>=n.y-i;d--)a.push({x:n.x,y:d}),t[n.x][d]=u}else if(x(c,wt)){for(let d=e.x+s;d<=n.x;d++)a.push({x:d,y:e.y}),t[d][e.y]=l;for(let d=e.y-1;d>=n.y-i;d--)a.push({x:n.x,y:d}),t[n.x][d]=u}else if(x(c,Ct)){for(let d=e.x-s;d>=n.x;d--)a.push({x:d,y:e.y}),t[d][e.y]=l;for(let d=e.y+1;d<=n.y+i;d++)a.push({x:n.x,y:d}),t[n.x][d]=u}else if(x(c,Bt))if(n.x-e.x<=1)for(let h=e.y+s;h<=n.y+i;h++)a.push({x:e.x,y:h}),t[e.x][h]=u;else{for(let h=e.x+s;h<=n.x;h++)a.push({x:h,y:e.y}),t[h][e.y]=l;for(let h=e.y+1;h<=n.y+i;h++)a.push({x:n.x,y:h}),t[n.x][h]=u}return a}function Fo(t,e){if(e.path.length===0){let l=ut(t.canvas);return[l,l,l,l,l,l]}let n=Ro(t,e),[s,i,o]=Ho(t,e.path,e.style),r=Lo(t,e.path,i[0],e.from.shape),c;e.hasArrowEnd?c=Qe(t,i[i.length-1],o[o.length-1]):c=ut(t.canvas);let a;if(e.hasArrowStart&&i.length>0){let u=i[0][0],d=Qo(o[0]),h={x:u.x,y:u.y};x(o[0],X)?h.x=u.x-1:x(o[0],U)?h.x=u.x+1:x(o[0],J)?h.y=u.y-1:x(o[0],T)&&(h.y=u.y+1),a=Qe(t,[u,h],d)}else a=ut(t.canvas);let A=Go(t,e.path);return[s,r,c,a,A,n]}function Qo(t){return x(t,T)?J:x(t,J)?T:x(t,U)?X:x(t,X)?U:x(t,yt)?Bt:x(t,wt)?Ct:x(t,Ct)?wt:x(t,Bt)?yt:Ht}function Ho(t,e,n="solid"){let s=ut(t.canvas),i=e[0],o=[],r=[];for(let c=1;c<e.length;c++){let a=e[c],A=Mt(t,i),l=Mt(t,a);if(fe(A,l)){i=a;continue}let u=ft(i,a),d=me(s,A,l,1,-1,t.config.useAscii,n);d.length===0&&d.push(A),o.push(d),r.push(u),i=a}return[s,o,r]}function Lo(t,e,n,s){let i=ut(t.canvas);if(t.config.useAscii||s==="state-start"||s==="state-end")return i;let o=n[0],r=ft(e[0],e[1]);return x(r,T)?i[o.x][o.y+1]="\u2534":x(r,J)?i[o.x][o.y-1]="\u252C":x(r,U)?i[o.x+1][o.y]="\u2524":x(r,X)&&(i[o.x-1][o.y]="\u251C"),i}function Qe(t,e,n){let s=ut(t.canvas);if(e.length===0)return s;let i=e[0],o=e[e.length-1],r=ft(i,o);(e.length===1||x(r,Ht))&&(r=n);let c;return t.config.useAscii?x(r,T)?c="^":x(r,J)?c="v":x(r,U)?c="<":x(r,X)?c=">":x(n,T)?c="^":x(n,J)?c="v":x(n,U)?c="<":x(n,X)?c=">":c="*":x(r,T)?c="\u25B2":x(r,J)?c="\u25BC":x(r,U)?c="\u25C4":x(r,X)?c="\u25BA":x(r,wt)?c="\u25E5":x(r,yt)?c="\u25E4":x(r,Bt)?c="\u25E2":x(r,Ct)?c="\u25E3":x(n,T)?c="\u25B2":x(n,J)?c="\u25BC":x(n,U)?c="\u25C4":x(n,X)?c="\u25BA":x(n,wt)?c="\u25E5":x(n,yt)?c="\u25E4":x(n,Bt)?c="\u25E2":x(n,Ct)?c="\u25E3":c="\u25CF",s[o.x][o.y]=c,s}function Go(t,e){let n=ut(t.canvas);for(let s=1;s<e.length-1;s++){let i=e[s],o=Mt(t,i),r=ft(e[s-1],i),c=ft(i,e[s+1]),a;t.config.useAscii?a="+":x(r,X)&&x(c,J)||x(r,T)&&x(c,U)?a="\u2510":x(r,X)&&x(c,T)||x(r,J)&&x(c,U)?a="\u2518":x(r,U)&&x(c,J)||x(r,T)&&x(c,X)?a="\u250C":x(r,U)&&x(c,T)||x(r,J)&&x(c,X)?a="\u2514":a="+",n[o.x][o.y]=a}return n}function Ro(t,e){let n=ut(t.canvas);if(e.text.length===0)return n;let s=zo(t,e.labelLine),i;if(e.path.length>=2){let o=e.path[0].y,r=e.path[e.path.length-1].y;r<o?i=!0:r>o&&(i=!1)}return Ko(n,s,e.text,i),n}function Ko(t,e,n,s){if(e.length<2)return;let i=Math.min(e[0].x,e[1].x),o=Math.max(e[0].x,e[1].x),r=Math.min(e[0].y,e[1].y),c=Math.max(e[0].y,e[1].y),a=i+Math.floor((o-i)/2),A=r+Math.floor((c-r)/2);if(s!==void 0&&i===o){let d=c-r,h=Math.max(1,Math.floor(d/4));s?A=A+h:A=A-h}let l=ct(n),u=A-Math.floor((l.length-1)/2);for(let d=0;d<l.length;d++){let h=l[d],g=a-Math.floor(h.length/2);jn(t,{x:g,y:u+d},h)}}function Jt(t,e,n){let s=e.gridCoord,i=0;for(let a=0;a<2;a++)i+=t.columnWidth.get(s.x+a)??0;let o=0;for(let a=0;a<2;a++)o+=t.rowHeight.get(s.y+a)??0;let r={width:i+1,height:o+1,labelArea:{x:0,y:0,width:0,height:0},gridColumns:[0,0,0],gridRows:[0,0,0]},c=e.drawingCoord;return vo(e.shape,n,r,c)}function Jo(t,e,n){let s=ut(t.canvas);if(!e.pathToJunction||e.pathToJunction.length===0)return[s,s,s,s,s,s];let i=ut(t.canvas),o=t.config.useAscii,r=e.pathToJunction.map((l,u)=>n.type==="fan-in"&&u===0?Jt(t,e.from,e.startDir):n.type==="fan-out"&&u===e.pathToJunction.length-1?Jt(t,e.to,e.endDir):Mt(t,l));for(let l=1;l<r.length;l++){let u=r[l-1],d=r[l];fe(u,d)||me(i,u,d,1,-1,o,e.style)}let c=ut(t.canvas);for(let l=1;l<e.pathToJunction.length-1;l++){let u=e.pathToJunction[l],d=Mt(t,u),h=ft(e.pathToJunction[l-1],u),g=ft(u,e.pathToJunction[l+1]),f;o?f="+":x(h,X)&&x(g,J)||x(h,T)&&x(g,U)?f="\u2510":x(h,X)&&x(g,T)||x(h,J)&&x(g,U)?f="\u2518":x(h,U)&&x(g,J)||x(h,T)&&x(g,X)?f="\u250C":x(h,U)&&x(g,T)||x(h,J)&&x(g,X)?f="\u2514":f="+",c[d.x][d.y]=f}let a=ut(t.canvas);if(n.type==="fan-in"&&e.pathToJunction.length>=2){let l=r[0],u=ft(e.pathToJunction[0],e.pathToJunction[1]);o||(x(u,T)?a[l.x][l.y]="\u2534":x(u,J)?a[l.x][l.y]="\u252C":x(u,U)?a[l.x][l.y]="\u2524":x(u,X)&&(a[l.x][l.y]="\u251C"))}let A=ut(t.canvas);return[i,a,s,s,c,A]}function No(t,e){let n=ut(t.canvas),s=ut(t.canvas);if(e.sharedPath.length<2)return[n,s];let i=t.config.useAscii,o=e.edges[0]?.style??"solid",r=t.config.graphDirection,c=e.sharedPath.map((a,A)=>{if(e.type==="fan-in"&&A===e.sharedPath.length-1){let l=r==="TD"?T:U;return Jt(t,e.sharedNode,l)}if(e.type==="fan-out"&&A===0){let l=r==="TD"?J:X;return Jt(t,e.sharedNode,l)}return Mt(t,a)});for(let a=1;a<c.length;a++){let A=c[a-1],l=c[a];fe(A,l)||me(n,A,l,1,-1,i,o)}for(let a=1;a<e.sharedPath.length-1;a++){let A=e.sharedPath[a],l=Mt(t,A),u=ft(e.sharedPath[a-1],A),d=ft(A,e.sharedPath[a+1]),h;i?h="+":x(u,X)&&x(d,J)||x(u,T)&&x(d,U)?h="\u2510":x(u,X)&&x(d,T)||x(u,J)&&x(d,U)?h="\u2518":x(u,U)&&x(d,J)||x(u,T)&&x(d,X)?h="\u250C":x(u,U)&&x(d,T)||x(u,J)&&x(d,X)?h="\u2514":h="+",s[l.x][l.y]=h}return[n,s]}function Xo(t,e){let n=ut(t.canvas);if(e.sharedPath.length<2)return n;let s=e.sharedPath.length-1,i=e.sharedPath[s-1],o=e.sharedPath[s],r=ft(i,o),c=t.config.graphDirection,a=c==="TD"?T:U,A=Jt(t,e.sharedNode,a);c==="TD"?A.y-=1:A.x-=1;let l;return t.config.useAscii?x(r,T)?l="^":x(r,J)?l="v":x(r,U)?l="<":x(r,X)?l=">":l="v":x(r,T)?l="\u25B2":x(r,J)?l="\u25BC":x(r,U)?l="\u25C4":x(r,X)?l="\u25BA":l="\u25BC",n[A.x][A.y]=l,n}function Zo(t,e){let n=ut(t.canvas);if(!e.pathToJunction||e.pathToJunction.length<2)return n;let s=e.pathToJunction.length-1,i=e.pathToJunction[s-1],o=e.pathToJunction[s],r=ft(i,o),c=t.config.graphDirection,a=c==="TD"?T:U,A=Jt(t,e.to,a);c==="TD"?A.y-=1:A.x-=1;let l;return t.config.useAscii?x(r,T)?l="^":x(r,J)?l="v":x(r,U)?l="<":x(r,X)?l=">":l="v":x(r,T)?l="\u25B2":x(r,J)?l="\u25BC":x(r,U)?l="\u25C4":x(r,X)?l="\u25BA":l="\u25BC",n[A.x][A.y]=l,n}function To(t,e){let n=ut(t.canvas);if(!e.junctionPoint)return n;let s=Mt(t,e.junctionPoint),i=t.config.useAscii,o=!1,r=!1,c=!1,a=!1;if(e.sharedPath.length>=2){let l=e.type==="fan-in"?0:e.sharedPath.length-1,u=e.type==="fan-in"?1:e.sharedPath.length-2,d=ft(e.sharedPath[l],e.sharedPath[u]);x(d,J)?r=!0:x(d,T)?o=!0:x(d,X)?a=!0:x(d,U)&&(c=!0)}for(let l of e.edges)if(l.pathToJunction&&l.pathToJunction.length>=2){let u=e.type==="fan-in"?l.pathToJunction.length-1:0,d=e.type==="fan-in"?l.pathToJunction.length-2:1,h=ft(l.pathToJunction[d],l.pathToJunction[u]);x(h,J)?o=!0:x(h,T)?r=!0:x(h,X)?c=!0:x(h,U)&&(a=!0)}let A;return i?A="+":o&&r&&c&&a?A="\u253C":r&&c&&a&&!o?A="\u252C":o&&c&&a&&!r?A="\u2534":o&&r&&a&&!c?A="\u251C":o&&r&&c&&!a?A="\u2524":c&&a?A="\u2500":o&&r?A="\u2502":r&&a?A="\u250C":r&&c?A="\u2510":o&&a?A="\u2514":o&&c?A="\u2518":A="\u253C",n[s.x][s.y]=A,n}function Uo(t,e){let n=t.maxX-t.minX,s=t.maxY-t.minY;if(n<=0||s<=0)return dt(0,0);let i={x:0,y:0},o={x:n,y:s},r=dt(n,s);if(e.config.useAscii){for(let c=i.x+1;c<o.x;c++)r[c][i.y]="-";for(let c=i.x+1;c<o.x;c++)r[c][o.y]="-";for(let c=i.y+1;c<o.y;c++)r[i.x][c]="|";for(let c=i.y+1;c<o.y;c++)r[o.x][c]="|";r[i.x][i.y]="+",r[o.x][i.y]="+",r[i.x][o.y]="+",r[o.x][o.y]="+"}else{for(let c=i.x+1;c<o.x;c++)r[c][i.y]="\u2500";for(let c=i.x+1;c<o.x;c++)r[c][o.y]="\u2500";for(let c=i.y+1;c<o.y;c++)r[i.x][c]="\u2502";for(let c=i.y+1;c<o.y;c++)r[o.x][c]="\u2502";r[i.x][i.y]="\u250C",r[o.x][i.y]="\u2510",r[i.x][o.y]="\u2514",r[o.x][o.y]="\u2518"}return r}function Po(t,e){let n=t.maxX-t.minX,s=t.maxY-t.minY;if(n<=0||s<=0)return[dt(0,0),{x:0,y:0}];let i=dt(n,s),o=ct(t.name);for(let r=0;r<o.length;r++){let c=o[r],a=1+r,A=Math.floor(n/2)-Math.floor(c.length/2);A<1&&(A=1);for(let l=0;l<c.length;l++)A+l<n&&a<s&&(i[A+l][a]=c[l])}return[i,{x:t.minX,y:t.minY}]}function jo(t){function e(s){return s.parent===null?0:1+e(s.parent)}let n=[...t];return n.sort((s,i)=>e(s)-e(i)),n}function ge(t,e,n,s){for(let i=0;i<e.length;i++)for(let o=0;o<(e[0]?.length??0);o++){let r=e[i]?.[o];if(r&&r!==" "){let c=i+n.x,a=o+n.y;c>=0&&a>=0&&Pt(t,c,a,s)}}}function Qt(t,e,n,s){for(let i of e)ge(t,i,n,s)}function Oo(t,e,n){let s=i=>/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|.':]$/.test(i);for(let i=0;i<e.length;i++)for(let o=0;o<(e[0]?.length??0);o++){let r=e[i]?.[o];if(r&&r!==" "){let c=i+n.x,a=o+n.y;c>=0&&a>=0&&Pt(t,c,a,s(r)?"border":"text")}}}function Vo(t){let e=t.config.useAscii,n={x:0,y:0},s=jo(t.subgraphs);for(let d of s){let h=Uo(d,t),g={x:d.minX,y:d.minY};t.canvas=It(t.canvas,g,e,h),ge(t.roleCanvas,h,g,"border")}for(let d of t.nodes)!d.drawn&&d.drawingCoord&&d.drawing&&(t.canvas=It(t.canvas,d.drawingCoord,e,d.drawing),Oo(t.roleCanvas,d.drawing,d.drawingCoord),d.drawn=!0);let i=[],o=[],r=[],c=[],a=[],A=[],l=[],u=new Set;for(let d of t.edges)if(d.bundle&&d.pathToJunction){let h=d.bundle,[g,f,,,p,I]=Jo(t,d,h);if(i.push(g),o.push(p),a.push(f),A.push(I),!u.has(h)){u.add(h);let[H,B]=No(t,h);if(i.push(H),o.push(B),h.type==="fan-in"){let D=Xo(t,h);r.push(D)}let R=To(t,h);l.push(R)}if(h.type==="fan-out"&&d.hasArrowEnd){let H=Zo(t,d);r.push(H)}}else{let[h,g,f,p,I,H]=Fo(t,d);i.push(h),o.push(I),r.push(f),c.push(p),a.push(g),A.push(H)}t.canvas=It(t.canvas,n,e,...i),Qt(t.roleCanvas,i,n,"line"),t.canvas=It(t.canvas,n,e,...o),Qt(t.roleCanvas,o,n,"corner"),t.canvas=It(t.canvas,n,e,...l),Qt(t.roleCanvas,l,n,"junction"),t.canvas=It(t.canvas,n,e,...r),Qt(t.roleCanvas,r,n,"arrow"),t.canvas=It(t.canvas,n,e,...a),Qt(t.roleCanvas,a,n,"junction"),t.canvas=It(t.canvas,n,e,...c),Qt(t.roleCanvas,c,n,"arrow"),t.canvas=It(t.canvas,n,e,...A),Qt(t.roleCanvas,A,n,"text");for(let d of t.subgraphs){if(d.nodes.length===0)continue;let[h,g]=Po(d,t);t.canvas=It(t.canvas,g,e,h),ge(t.roleCanvas,h,g,"text")}return t.canvas}function Mt(t,e,n){let s=n?{x:e.x+n.x,y:e.y+n.y}:e,i=0;for(let a=0;a<s.x;a++)i+=t.columnWidth.get(a)??0;let o=0;for(let a=0;a<s.y;a++)o+=t.rowHeight.get(a)??0;let r=t.columnWidth.get(s.x)??0,c=t.rowHeight.get(s.y)??0;return{x:i+Math.floor(r/2)+t.offsetX,y:o+Math.floor(c/2)+t.offsetY}}function zo(t,e){return e.map(n=>Mt(t,n))}function Zt(t,e,n,s){let i=s??tr(t,e);if(t.grid.has(Wt(n)))return i==="LR"?Zt(t,e,{x:n.x,y:n.y+4},i):Zt(t,e,{x:n.x+4,y:n.y},i);for(let o=0;o<3;o++)for(let r=0;r<3;r++){let c={x:n.x+o,y:n.y+r};t.grid.set(Wt(c),e)}return e.gridCoord=n,n}function qo(t,e){let n=e.gridCoord,s=t.config.boxBorderPadding,i=Do(e.shape,e.displayLabel,{useAscii:t.config.useAscii,padding:s}),o=i.gridColumns,r=i.gridRows;for(let c=0;c<o.length;c++){let a=n.x+c,A=t.columnWidth.get(a)??0;t.columnWidth.set(a,Math.max(A,o[c]))}for(let c=0;c<r.length;c++){let a=n.y+c,A=t.rowHeight.get(a)??0;t.rowHeight.set(a,Math.max(A,r[c]))}if(n.x>0){let c=t.columnWidth.get(n.x-1)??0;t.columnWidth.set(n.x-1,Math.max(c,t.config.paddingX))}if(n.y>0){let c=t.config.paddingY;er(t,e)&&(c+=4);let a=t.rowHeight.get(n.y-1)??0;t.rowHeight.set(n.y-1,Math.max(a,c))}}function He(t,e){for(let n of e)t.columnWidth.has(n.x)||t.columnWidth.set(n.x,Math.floor(t.config.paddingX/2)),t.rowHeight.has(n.y)||t.rowHeight.set(n.y,Math.floor(t.config.paddingY/2))}function de(t,e){return t.subgraphs.some(n=>n.nodes.includes(e))}function xt(t,e){let n=null;for(let s of t.subgraphs)s.nodes.includes(e)&&(!n||_o(n,s))&&(n=s);return n}function _o(t,e){let n=e;for(;n!==null;){if(n===t)return!0;n=n.parent}return!1}function tr(t,e){let n=xt(t,e);return n?.direction?n.direction:t.config.graphDirection}function er(t,e){let n=xt(t,e);if(!n)return!1;let s=!1;for(let i of t.edges)if(i.to===e&&xt(t,i.from)!==n){s=!0;break}if(!s)return!1;for(let i of n.nodes){if(i===e||!i.gridCoord)continue;let o=!1;for(let r of t.edges)if(r.to===i&&xt(t,r.from)!==n){o=!0;break}if(o&&i.gridCoord.y<e.gridCoord.y)return!1}return!0}function _e(t,e){if(e.nodes.length===0)return;let n=1e6,s=1e6,i=-1e6,o=-1e6;for(let a of e.children)_e(t,a),a.nodes.length>0&&(n=Math.min(n,a.minX),s=Math.min(s,a.minY),i=Math.max(i,a.maxX),o=Math.max(o,a.maxY));for(let a of e.nodes){if(!a.drawingCoord||!a.drawing)continue;let A=a.drawingCoord.x,l=a.drawingCoord.y,u=A+a.drawing.length-1,d=l+a.drawing[0].length-1;n=Math.min(n,A),s=Math.min(s,l),i=Math.max(i,u),o=Math.max(o,d)}let r=2,c=2;e.minX=n-r,e.minY=s-r-c,e.maxX=i+r,e.maxY=o+r}function nr(t){let n=t.subgraphs.filter(s=>s.parent===null&&s.nodes.length>0);for(let s=0;s<n.length;s++)for(let i=s+1;i<n.length;i++){let o=n[s],r=n[i];o.minX<r.maxX&&o.maxX>r.minX&&(o.maxY>=r.minY-1&&o.minY<r.minY?r.minY=o.maxY+1+1:r.maxY>=o.minY-1&&r.minY<o.minY&&(o.minY=r.maxY+1+1)),o.minY<r.maxY&&o.maxY>r.minY&&(o.maxX>=r.minX-1&&o.minX<r.minX?r.minX=o.maxX+1+1:r.maxX>=o.minX-1&&r.minX<o.minX&&(o.minX=r.maxX+1+1))}}function or(t){for(let e of t.subgraphs)_e(t,e);nr(t)}function rr(t){if(t.subgraphs.length===0)return;let e=0,n=0;for(let o of t.subgraphs)e=Math.min(e,o.minX),n=Math.min(n,o.minY);let s=-e,i=-n;if(!(s===0&&i===0)){t.offsetX=s,t.offsetY=i;for(let o of t.subgraphs)o.minX+=s,o.minY+=i,o.maxX+=s,o.maxY+=i;for(let o of t.nodes)o.drawingCoord&&(o.drawingCoord.x+=s,o.drawingCoord.y+=i)}}function sr(t){let e=t.config.graphDirection,n=new Array(100).fill(0),s=new Set,i=[];for(let d of t.nodes){s.has(d.name)||i.push(d),s.add(d.name);for(let h of he(t,d))s.add(h.name)}let o=i.filter(d=>{let h=xt(t,d);if(!h)return!0;for(let g of t.edges)if(g.to===d&&xt(t,g.from)!==h)return!1;return!0}),r=!1,c=!1;for(let d of o)de(t,d)?he(t,d).length>0&&(c=!0):r=!0;let a=e==="LR"&&r&&c,A,l=[];a?(A=o.filter(d=>!de(t,d)),l=o.filter(d=>de(t,d))):A=o;for(let d of A){let h=e==="LR"?{x:0,y:n[0]}:{x:n[0],y:0};Zt(t,t.nodes[d.index],h),n[0]=n[0]+4}if(a&&l.length>0)for(let h of l){let g=e==="LR"?{x:4,y:n[4]}:{x:n[4],y:4};Zt(t,t.nodes[h.index],g),n[4]=n[4]+4}let u=A.length+l.length;for(;u<t.nodes.length;){let d=u;for(let h of t.nodes){if(h.gridCoord===null)continue;let g=h.gridCoord;for(let f of he(t,h)){if(f.gridCoord!==null)continue;let p=xt(t,h),I=xt(t,f),H=p&&p===I&&p.direction?p.direction:t.config.graphDirection,B=H==="LR"?g.x+4:g.y+4,R;H!==t.config.graphDirection?R=H==="LR"?g.y:g.x:R=n[B];let D=H==="LR"?{x:B,y:R}:{x:R,y:B};Zt(t,t.nodes[f.index],D,H),H===t.config.graphDirection&&(n[B]=R+4),u++}}if(u===d)break}for(let d of t.nodes)qo(t,d);t.bundles=Ao(t),uo(t);for(let d of t.edges){if(d.bundle&&d.path.length>0){He(t,d.path),Ee(t,d);continue}co(t,d),He(t,d.path),Ee(t,d)}for(let d of t.nodes)d.drawingCoord=Mt(t,d.gridCoord),d.drawing=Wo(d,t);On(t.canvas,t.columnWidth,t.rowHeight),Vn(t.roleCanvas,t.columnWidth,t.rowHeight),or(t),rr(t)}function ir(t,e){return t.edges.filter(n=>n.from.name===e.name)}function he(t,e){return ir(t,e).map(n=>n.to)}function cr(t){let e={actors:[],messages:[],blocks:[],notes:[]},n=new Set,s=[];for(let i=1;i<t.length;i++){let o=t[i],r=o.match(/^(participant|actor)\s+(\S+?)(?:\s+as\s+(.+))?$/);if(r){let d=r[1],h=r[2],g=r[3]?.trim()??h,f=lt(g);n.has(h)||(n.add(h),e.actors.push({id:h,label:f,type:d}));continue}let c=o.match(/^Note\s+(left of|right of|over)\s+([^:]+):\s*(.+)$/i);if(c){let d=c[1].toLowerCase(),h=c[2].trim(),g=lt(c[3].trim()),f=h.split(",").map(I=>I.trim());for(let I of f)Nt(e,n,I);let p="over";d==="left of"?p="left":d==="right of"&&(p="right"),e.notes.push({actorIds:f,text:g,position:p,afterIndex:e.messages.length-1});continue}let a=o.match(/^(loop|alt|opt|par|critical|break|rect)\s*(.*)$/);if(a){let d=a[1],h=a[2]?.trim()??"",g=lt(h);s.push({type:d,label:g,startIndex:e.messages.length,dividers:[]});continue}let A=o.match(/^(else|and)\s*(.*)$/);if(A&&s.length>0){let d=A[2]?.trim()??"",h=lt(d);s[s.length-1].dividers.push({index:e.messages.length,label:h});continue}if(o==="end"&&s.length>0){let d=s.pop();e.blocks.push({type:d.type,label:d.label,startIndex:d.startIndex,endIndex:Math.max(e.messages.length-1,d.startIndex),dividers:d.dividers});continue}let l=o.match(/^(\S+?)\s*(--?>?>|--?[)x]|--?>>|--?>)\s*([+-]?)(\S+?)\s*:\s*(.+)$/);if(l){let d=l[1],h=l[2],g=l[3],f=l[4],p=lt(l[5].trim());Nt(e,n,d),Nt(e,n,f);let I=h.startsWith("--")?"dashed":"solid",H=h.includes(">>")||h.includes("x")?"filled":"open",B={from:d,to:f,label:p,lineStyle:I,arrowHead:H};g==="+"&&(B.activate=!0),g==="-"&&(B.deactivate=!0),e.messages.push(B);continue}let u=o.match(/^(\S+?)\s*(->>|-->>|-\)|--\)|-x|--x|->|-->)\s*([+-]?)(\S+?)\s*:\s*(.+)$/);if(u){let d=u[1],h=u[2],g=u[3],f=u[4],p=lt(u[5].trim());Nt(e,n,d),Nt(e,n,f);let I=h.startsWith("--")?"dashed":"solid",H=h.includes(">>")||h.includes("x")?"filled":"open",B={from:d,to:f,label:p,lineStyle:I,arrowHead:H};g==="+"&&(B.activate=!0),g==="-"&&(B.deactivate=!0),e.messages.push(B);continue}}return e}function Nt(t,e,n){e.has(n)||(e.add(n),t.actors.push({id:n,label:n,type:"participant"}))}function ar(t,e,n,s){let i=t.split(`
7`).map(m=>m.trim()).filter(m=>m.length>0&&!m.startsWith("%%")),o=cr(i);if(o.actors.length===0)return"";let r=e.useAscii,c=r?"-":"\u2500",a=r?"|":"\u2502",A=r?"+":"\u250C",l=r?"+":"\u2510",u=r?"+":"\u2514",d=r?"+":"\u2518",h=r?"+":"\u252C",g=r?"+":"\u2534",f=r?"+":"\u251C",p=r?"+":"\u2524",I=new Map;o.actors.forEach((m,b)=>I.set(m.id,b));let H=1,R=o.actors.map(m=>le(m.label)+2*H+2).map(m=>Math.ceil(m/2)),D=o.actors.map(m=>Ye(m.label)+2),j=Math.max(...D,3),ht=new Array(Math.max(o.actors.length-1,0)).fill(0);for(let m of o.messages){let b=I.get(m.from),S=I.get(m.to);if(b===S)continue;let v=Math.min(b,S),Q=Math.max(b,S),k=le(m.label)+4,P=Q-v,Y=Math.ceil(k/P);for(let M=v;M<Q;M++)ht[M]=Math.max(ht[M],Y)}let L=[R[0]];for(let m=1;m<o.actors.length;m++){let b=Math.max(R[m-1]+R[m]+2,ht[m-1]+2,10);L[m]=L[m-1]+b}let nt=[],W=[],z=new Map,y=new Map,O=new Map,V=[],q=j;for(let m=0;m<o.messages.length;m++){for(let Q=0;Q<o.blocks.length;Q++)o.blocks[Q].startIndex===m&&(q+=2,z.set(Q,q-1));for(let Q=0;Q<o.blocks.length;Q++)for(let k=0;k<o.blocks[Q].dividers.length;k++)o.blocks[Q].dividers[k].index===m&&(q+=1,O.set(`${Q}:${k}`,q),q+=1);q+=1;let b=o.messages[m],S=b.from===b.to,v=Ye(b.label);S?(W[m]=q+1,nt[m]=q,q+=2+v):(W[m]=q,nt[m]=q+v,q+=v+1);for(let Q=0;Q<o.notes.length;Q++)if(o.notes[Q].afterIndex===m){q+=1;let k=o.notes[Q],P=ct(k.text),Y=Math.max(...P.map(K=>K.length))+4,M=P.length+2,Z=I.get(k.actorIds[0])??0,$;if(k.position==="left")$=L[Z]-Y-1;else if(k.position==="right")$=L[Z]+2;else if(k.actorIds.length>=2){let K=I.get(k.actorIds[1])??Z;$=Math.floor((L[Z]+L[K])/2)-Math.floor(Y/2)}else $=L[Z]-Math.floor(Y/2);$=Math.max(0,$),V.push({x:$,y:q,width:Y,height:M,lines:P}),q+=M}for(let Q=0;Q<o.blocks.length;Q++)o.blocks[Q].endIndex===m&&(q+=1,y.set(Q,q),q+=1)}q+=1;let E=q,_=E+j,st=L[L.length-1]??0,it=R[R.length-1]??0,w=st+it+2;for(let m=0;m<o.messages.length;m++){let b=o.messages[m];if(b.from===b.to){let S=I.get(b.from),v=L[S]+6+2+b.label.length;w=Math.max(w,v+1)}}for(let m of V)w=Math.max(w,m.x+m.width+1);let G=dt(w,_-1),F=Ut(w,_-1);function C(m,b,S,v){m>=0&&m<G.length&&b>=0&&b<(G[0]?.length??0)&&(G[m][b]=S,Pt(F,m,b,v))}function N(m,b,S){let v=ct(S),Q=le(S),k=Q+2*H+2,P=v.length+2,Y=m-Math.floor(k/2);C(Y,b,A,"border");for(let Z=1;Z<k-1;Z++)C(Y+Z,b,c,"border");C(Y+k-1,b,l,"border");for(let Z=0;Z<v.length;Z++){let $=b+1+Z;C(Y,$,a,"border"),C(Y+k-1,$,a,"border");let K=v[Z],rt=Y+1+H+Math.floor((Q-K.length)/2);for(let et=0;et<K.length;et++)C(rt+et,$,K[et],"text")}let M=b+P-1;C(Y,M,u,"border");for(let Z=1;Z<k-1;Z++)C(Y+Z,M,c,"border");C(Y+k-1,M,d,"border")}for(let m=0;m<o.actors.length;m++){let b=L[m];for(let S=j;S<=E;S++)C(b,S,a,"line")}for(let m=0;m<o.actors.length;m++){let b=o.actors[m];N(L[m],0,b.label),N(L[m],E,b.label),r||(C(L[m],j-1,h,"junction"),C(L[m],E,g,"junction"))}for(let m=0;m<o.messages.length;m++){let b=o.messages[m],S=I.get(b.from),v=I.get(b.to),Q=L[S],k=L[v],P=S===v,Y=b.lineStyle==="dashed",M=b.arrowHead==="filled",Z=Y?r?".":"\u254C":c;if(P){let $=nt[m],K=Math.max(4,4);C(Q,$,f,"junction");for(let ot=Q+1;ot<Q+K;ot++)C(ot,$,Z,"line");C(Q+K,$,r?"+":"\u2510","corner"),C(Q+K,$+1,a,"line");let rt=Q+K+2;for(let ot=0;ot<b.label.length;ot++)rt+ot<w&&C(rt+ot,$+1,b.label[ot],"text");let et=M?r?"<":"\u25C0":r?"<":"\u25C1";C(Q,$+2,et,"arrow");for(let ot=Q+1;ot<Q+K;ot++)C(ot,$+2,Z,"line");C(Q+K,$+2,r?"+":"\u2518","corner")}else{let $=W[m],K=nt[m],rt=Q<k,et=Math.floor((Q+k)/2),ot=ct(b.label);for(let at=0;at<ot.length;at++){let At=ot[at],mt=et-Math.floor(At.length/2),St=$+at;for(let pt=0;pt<At.length;pt++){let Gt=mt+pt;Gt>=0&&Gt<w&&C(Gt,St,At[pt],"text")}}if(rt){for(let At=Q+1;At<k;At++)C(At,K,Z,"line");C(k,K,M?r?">":"\u25B6":r?">":"\u25B7","arrow")}else{for(let At=k+1;At<Q;At++)C(At,K,Z,"line");C(k,K,M?r?"<":"\u25C0":r?"<":"\u25C1","arrow")}}}for(let m=0;m<o.blocks.length;m++){let b=o.blocks[m],S=z.get(m),v=y.get(m);if(S===void 0||v===void 0)continue;let Q=w,k=0;for(let $=b.startIndex;$<=b.endIndex&&!($>=o.messages.length);$++){let K=o.messages[$],rt=I.get(K.from)??0,et=I.get(K.to)??0;Q=Math.min(Q,L[Math.min(rt,et)]),k=Math.max(k,L[Math.max(rt,et)])}let P=Math.max(0,Q-4),Y=Math.min(w-1,k+4);C(P,S,A,"border");for(let $=P+1;$<Y;$++)C($,S,c,"border");C(Y,S,l,"border");let M=b.label?`${b.type} [${b.label}]`:b.type,Z=ct(M);for(let $=0;$<Z.length&&S+$<v;$++){let K=Z[$];for(let rt=0;rt<K.length&&P+1+rt<Y;rt++)C(P+1+rt,S+$,K[rt],"text")}C(P,v,u,"border");for(let $=P+1;$<Y;$++)C($,v,c,"border");C(Y,v,d,"border");for(let $=S+1;$<v;$++)C(P,$,a,"border"),C(Y,$,a,"border");for(let $=0;$<b.dividers.length;$++){let K=O.get(`${m}:${$}`);if(K===void 0)continue;let rt=tt();C(P,K,f,"junction");for(let ot=P+1;ot<Y;ot++)C(ot,K,rt,"line");C(Y,K,p,"junction");let et=b.dividers[$].label;if(et){let ot=`[${et}]`;for(let at=0;at<ot.length&&P+1+at<Y;at++)C(P+1+at,K,ot[at],"text")}}}for(let m of V){Ft(G,m.x+m.width,m.y+m.height),Lt(F,m.x+m.width,m.y+m.height),C(m.x,m.y,A,"border");for(let S=1;S<m.width-1;S++)C(m.x+S,m.y,c,"border");C(m.x+m.width-1,m.y,l,"border");for(let S=0;S<m.lines.length;S++){let v=m.y+1+S;C(m.x,v,a,"border"),C(m.x+m.width-1,v,a,"border");for(let Q=0;Q<m.lines[S].length;Q++)C(m.x+2+Q,v,m.lines[S][Q],"text")}let b=m.y+m.height-1;C(m.x,b,u,"border");for(let S=1;S<m.width-1;S++)C(m.x+S,b,c,"border");C(m.x+m.width-1,b,d,"border")}return ne(G,{roleCanvas:F,colorMode:n,theme:s});function tt(){return r?"-":"\u254C"}}function Ar(t){let e={classes:[],relationships:[],namespaces:[]},n=new Map,s=null,i=null,o=0;for(let r=1;r<t.length;r++){let c=t[r];if(i&&o>0){if(c==="}"){o--,o===0&&(i=null);continue}let g=c.match(/^<<(\w+)>>$/);if(g){i.annotation=g[1];continue}let f=Le(c);f&&(f.isMethod?i.methods.push(f.member):i.attributes.push(f.member));continue}let a=c.match(/^namespace\s+(\S+)\s*\{$/);if(a){s={name:a[1],classIds:[]};continue}if(c==="}"&&s){e.namespaces.push(s),s=null;continue}let A=c.match(/^class\s+(\S+?)(?:\s*~(\w+)~)?\s*\{$/);if(A){let g=A[1],f=A[2],p=Rt(n,g);f&&(p.label=`${g}<${f}>`),i=p,o=1,s&&s.classIds.push(g);continue}let l=c.match(/^class\s+(\S+?)(?:\s*~(\w+)~)?\s*$/);if(l){let g=l[1],f=l[2],p=Rt(n,g);f&&(p.label=`${g}<${f}>`),s&&s.classIds.push(g);continue}let u=c.match(/^class\s+(\S+?)\s*\{\s*<<(\w+)>>\s*\}$/);if(u){let g=Rt(n,u[1]);g.annotation=u[2];continue}let d=c.match(/^(\S+?)\s*:\s*(.+)$/);if(d){let g=d[2];if(!g.match(/<\|--|--|\*--|o--|-->|\.\.>|\.\.\|>/)){let f=Rt(n,d[1]),p=Le(g);p&&(p.isMethod?f.methods.push(p.member):f.attributes.push(p.member));continue}}let h=lr(c);if(h){Rt(n,h.from),Rt(n,h.to),e.relationships.push(h);continue}}return e.classes=[...n.values()],e}function Rt(t,e){let n=t.get(e);return n||(n={id:e,label:e,attributes:[],methods:[]},t.set(e,n)),n}function Le(t){let e=t.trim().replace(/;$/,"");if(!e)return null;let n="",s=e;/^[+\-#~]/.test(s)&&(n=s[0],s=s.slice(1).trim());let i=s.match(/^(.+?)\(([^)]*)\)(?:\s*(.+))?$/);if(i){let l=i[1].trim(),u=i[2]?.trim()||void 0,d=i[3]?.trim(),h=l.endsWith("$")||s.includes("$"),g=l.endsWith("*")||s.includes("*");return{member:{visibility:n,name:l.replace(/[$*]$/,""),type:d||void 0,isStatic:h,isAbstract:g,isMethod:!0,params:u},isMethod:!0}}let o=s.split(/\s+/),r,c;o.length>=2?(c=o[0],r=o.slice(1).join(" ")):r=o[0]??s;let a=r.endsWith("$"),A=r.endsWith("*");return{member:{visibility:n,name:r.replace(/[$*]$/,""),type:c||void 0,isStatic:a,isAbstract:A,isMethod:!1},isMethod:!1}}function lr(t){let e=t.match(/^(\S+?)\s+(?:"([^"]*?)"\s+)?(<\|--|<\|\.\.|\*--|o--|-->|--\*|--o|--\|>|\.\.>|\.\.\|>|<--|<\.\.?|--)\s+(?:"([^"]*?)"\s+)?(\S+?)(?:\s*:\s*(.+))?$/);if(!e)return null;let n=e[1],s=e[2],i=s?lt(s):void 0,o=e[3].trim(),r=e[4],c=r?lt(r):void 0,a=e[5],A=e[6]?.trim(),l=A?lt(A):void 0,u=dr(o);return u?{from:n,to:a,type:u.type,markerAt:u.markerAt,label:l,fromCardinality:i,toCardinality:c}:null}function dr(t){switch(t.trim()){case"<|--":return{type:"inheritance",markerAt:"from"};case"--|>":return{type:"inheritance",markerAt:"to"};case"<|..":return{type:"realization",markerAt:"from"};case"..|>":return{type:"realization",markerAt:"to"};case"*--":return{type:"composition",markerAt:"from"};case"--*":return{type:"composition",markerAt:"to"};case"o--":return{type:"aggregation",markerAt:"from"};case"--o":return{type:"aggregation",markerAt:"to"};case"-->":return{type:"association",markerAt:"to"};case"<--":return{type:"association",markerAt:"from"};case"..>":return{type:"dependency",markerAt:"to"};case"<..":return{type:"dependency",markerAt:"from"};case"--":return{type:"association",markerAt:"to"};default:return null}}function hr(t){return/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|]$/.test(t)?"border":"text"}function Ge(t){let e=t.visibility||"",n=t.type?`: ${t.type}`:"";return`${e}${t.name}${n}`}function ur(t){let e=[];t.annotation&&e.push(`<<${t.annotation}>>`);let n=ct(t.label);e.push(...n);let s=t.attributes.map(Ge),i=t.methods.map(Ge);return s.length===0&&i.length===0?[e]:i.length===0?[e,s]:[e,s,i]}function gr(t,e){return{type:t,markerAt:e,dashed:t==="dependency"||t==="realization"}}function Et(t,e,n){switch(t){case"inheritance":case"realization":return n==="down"?e?"^":"\u25B3":n==="up"?e?"v":"\u25BD":n==="left"?e?">":"\u25C1":e?"<":"\u25B7";case"composition":return e?"*":"\u25C6";case"aggregation":return e?"o":"\u25C7";case"association":case"dependency":return n==="down"?e?"v":"\u25BC":n==="up"?e?"^":"\u25B2":n==="left"?e?"<":"\u25C0":e?">":"\u25B6"}}function fr(t,e,n,s){let i=t.split(`
8`).map(w=>w.trim()).filter(w=>w.length>0&&!w.startsWith("%%")),o=Ar(i);if(o.classes.length===0)return"";let r=e.useAscii,c=4,a=3,A=new Map,l=new Map,u=new Map;for(let w of o.classes){let G=ur(w);A.set(w.id,G);let F=0;for(let m of G)for(let b of m)F=Math.max(F,b.length);let C=F+4,N=0;for(let m of G)N+=Math.max(m.length,1);let tt=N+(G.length-1)+2;l.set(w.id,C),u.set(w.id,tt)}let d=new Map;for(let w of o.classes)d.set(w.id,w);let h=new Map,g=new Map;for(let w of o.relationships){let G=w.type==="inheritance"||w.type==="realization",F=G&&w.markerAt==="to"?w.to:w.from,C=G&&w.markerAt==="to"?w.from:w.to;h.has(C)||h.set(C,new Set),h.get(C).add(F),g.has(F)||g.set(F,new Set),g.get(F).add(C)}let f=new Map,I=o.classes.filter(w=>!h.has(w.id)||h.get(w.id).size===0).map(w=>w.id);for(let w of I)f.set(w,0);let H=o.classes.length-1,B=0;for(;B<I.length;){let w=I[B++],G=g.get(w);if(G)for(let F of G){let C=(f.get(w)??0)+1;C>H||(!f.has(F)||f.get(F)<C)&&(f.set(F,C),I.push(F))}}for(let w of o.classes)f.has(w.id)||f.set(w.id,0);let R=Math.max(...f.values(),0),D=Array.from({length:R+1},()=>[]);for(let w of o.classes)D[f.get(w.id)].push(w.id);let j=new Map,ht=0;for(let w=0;w<=R;w++){let G=D[w];if(G.length===0)continue;let F=0,C=0;for(let N of G){let tt=d.get(N),m=l.get(N),b=u.get(N);j.set(N,{cls:tt,sections:A.get(N),x:F,y:ht,width:m,height:b}),F+=m+c,C=Math.max(C,b)}ht+=C+a}let L=0,nt=0;for(let w of j.values())L=Math.max(L,w.x+w.width),nt=Math.max(nt,w.y+w.height);L+=4,nt+=2;let W=dt(L-1,nt-1),z=Ut(L-1,nt-1);function y(w,G,F,C){w>=0&&w<W.length&&G>=0&&G<(W[0]?.length??0)&&(W[w][G]=F,Pt(z,w,G,C))}for(let w of j.values()){let G=qe(w.sections,r);for(let F=0;F<G.length;F++)for(let C=0;C<G[0].length;C++){let N=G[F][C];if(N!==" "){let tt=w.x+F,m=w.y+C;tt<L&&m<nt&&y(tt,m,N,hr(N))}}}let O=[];for(let w of j.values())O.push({x1:w.x,x2:w.x+w.width-1,y1:w.y,y2:w.y+w.height-1});function V(w,G,F){for(let[C,N]of j.entries())if(!F?.has(C)&&w>=N.x&&w<=N.x+N.width-1&&G>=N.y&&G<=N.y+N.height-1)return!0;return!1}function q(w,G,F,C){let N=!0;for(let tt=Math.min(G,F);tt<=Math.max(G,F);tt++)if(V(w,tt,C)){N=!1;break}if(N)return w;for(let tt=1;tt<L+10;tt++){let m=w+tt;N=!0;for(let S=Math.min(G,F);S<=Math.max(G,F);S++)if(V(m,S,C)){N=!1;break}if(N)return m;let b=w-tt;if(b>=0){N=!0;for(let S=Math.min(G,F);S<=Math.max(G,F);S++)if(V(b,S,C)){N=!1;break}if(N)return b}}return L+2}let E=r?"-":"\u2500",_=r?"|":"\u2502",st=r?".":"\u254C",it=r?":":"\u250A";for(let w of o.relationships){let G=j.get(w.from),F=j.get(w.to);if(!G||!F)continue;let C=gr(w.type,w.markerAt),N=C.dashed?st:E,tt=C.dashed?it:_,m=new Set([w.from,w.to]),b=G.x+Math.floor(G.width/2),S=G.y+G.height-1,v=F.x+Math.floor(F.width/2),Q=F.y;if(S<Q){let k=q(b,S+1,Q-1,m),P=k!==b;if(k>=L&&Ft(W,k+2,nt),P){let Y=S+1,M=Q-1,Z=Math.min(b,k),$=Math.max(b,k);for(let K=Z;K<=$;K++)y(K,Y,N,"line");!r&&Y<(W[0]?.length??0)&&(b<k?(y(b,Y,"\u2514","corner"),y(k,Y,"\u2510","corner")):(y(b,Y,"\u2518","corner"),y(k,Y,"\u250C","corner")));for(let K=Y+1;K<=M;K++)y(k,K,tt,"line");if(k!==v){let K=Math.min(k,v),rt=Math.max(k,v);for(let et=K;et<=rt;et++)y(et,M,N,"line");!r&&M<(W[0]?.length??0)&&(k<v?(y(k,M,"\u2514","corner"),y(v,M,"\u2510","corner")):(y(k,M,"\u2518","corner"),y(v,M,"\u250C","corner")))}if(C.markerAt==="to"){let K=Et(C.type,r,"down");y(v,M,K,"arrow")}if(C.markerAt==="from"){let K=Et(C.type,r,"down");y(b,S+1,K,"arrow")}}else{let Y=S+Math.floor((Q-S)/2);for(let M=S+1;M<=Y;M++)y(b,M,tt,"line");if(b!==v&&Y<(W[0]?.length??0)){let M=Math.min(b,v),Z=Math.max(b,v);for(let $=M;$<=Z;$++)y($,Y,N,"line");r||(y(b,Y,b<v?"\u2514":"\u2518","corner"),y(v,Y,b<v?"\u2510":"\u250C","corner"))}for(let M=Y+1;M<Q;M++)y(v,M,tt,"line");C.markerAt==="to"&&y(v,Q-1,Et(C.type,r,"down"),"arrow"),C.markerAt==="from"&&y(b,S+1,Et(C.type,r,"down"),"arrow")}}else if(F.y+F.height-1<G.y){let k=G.y,P=F.y+F.height-1,Y=P+Math.floor((k-P)/2);for(let M=k-1;M>=Y;M--)y(b,M,tt,"line");if(b!==v){let M=Math.min(b,v),Z=Math.max(b,v);for(let $=M;$<=Z;$++)y($,Y,N,"line");!r&&Y>=0&&Y<nt&&(y(b,Y,b<v?"\u250C":"\u2510","corner"),y(v,Y,b<v?"\u2518":"\u2514","corner"))}for(let M=Y-1;M>P;M--)y(v,M,tt,"line");if(C.markerAt==="from"){let M=Et(C.type,r,"up"),Z=k-1;for(let $=0;$<M.length;$++)y(b-Math.floor(M.length/2)+$,Z,M[$],"arrow")}if(C.markerAt==="to"){let Z=C.type==="inheritance"||C.type==="realization"?"down":"up",$=Et(C.type,r,Z),K=P+1;for(let rt=0;rt<$.length;rt++)y(v-Math.floor($.length/2)+rt,K,$[rt],"arrow")}}else{let k=Math.max(S,F.y+F.height-1)+2;Ft(W,L,k+1),Lt(z,L,k+1);for(let M=S+1;M<=k;M++)y(b,M,tt,"line");let P=Math.min(b,v),Y=Math.max(b,v);for(let M=P;M<=Y;M++)y(M,k,N,"line");for(let M=k-1;M>=F.y+F.height;M--)y(v,M,tt,"line");if(C.markerAt==="from"){let M=Et(C.type,r,"down"),Z=S+1;for(let $=0;$<M.length;$++)y(b-Math.floor(M.length/2)+$,Z,M[$],"arrow")}if(C.markerAt==="to"){let M=Et(C.type,r,"up"),Z=F.y+F.height;for(let $=0;$<M.length;$++)y(v-Math.floor(M.length/2)+$,Z,M[$],"arrow")}}if(w.label){let k=ct(w.label),P=Math.max(...k.map(et=>et.length))+2,Y,M;if(S<Q)Y=Math.floor((S+1+Q-1)/2),M=Math.floor((b+v)/2);else if(F.y+F.height-1<G.y){let et=F.y+F.height-1;Y=Math.floor((et+1+G.y-1)/2),M=Math.floor((b+v)/2)}else Y=Math.max(S,F.y+F.height-1)+2,M=Math.floor((b+v)/2);let Z=Y,$=Math.floor(k.length/2),K=!1;for(let et=0;et<k.length;et++){let ot=Z-$+et,at=M-Math.floor(P/2),At=Math.max(0,at);for(let mt=At;mt<At+P;mt++)if(V(mt,ot,m)){K=!0;break}if(K)break}if(K){let et=S+1,ot=Q-1;for(let at=et;at<=ot;at++){let At=!0,mt=M-Math.floor(P/2),St=Math.max(0,mt);for(let pt=St;pt<St+P;pt++)if(V(pt,at,m)){At=!1;break}if(At){Z=at;break}}}let rt=Z-$;for(let et=0;et<k.length;et++){let ot=` ${k[et]} `,at=M-Math.floor(ot.length/2),At=Math.max(0,at),mt=rt+et,St=At+ot.length;St>0&&mt>=0&&(Ft(W,Math.max(St,1),Math.max(mt+1,1)),Lt(z,Math.max(St,1),Math.max(mt+1,1)));for(let pt=0;pt<ot.length;pt++){let Gt=At+pt;Gt>=0&&mt>=0&&y(Gt,mt,ot[pt],"text")}}}}return ne(W,{roleCanvas:z,colorMode:n,theme:s})}function xr(t){let e={entities:[],relationships:[]},n=new Map,s=null;for(let i=1;i<t.length;i++){let o=t[i];if(s){if(o==="}"){s=null;continue}let a=mr(o);a&&s.attributes.push(a);continue}let r=o.match(/^(\S+)\s*\{$/);if(r){let a=r[1];s=ue(n,a);continue}let c=pr(o);if(c){ue(n,c.entity1),ue(n,c.entity2),e.relationships.push(c);continue}}return e.entities=[...n.values()],e}function ue(t,e){let n=t.get(e);return n||(n={id:e,label:e,attributes:[]},t.set(e,n)),n}function mr(t){let e=t.match(/^(\S+)\s+(\S+)(?:\s+(.+))?$/);if(!e)return null;let n=e[1],s=e[2],i=e[3]?.trim()??"",o=[],r,c=i.match(/"([^"]*)"/);c&&(r=lt(c[1]));let a=i.replace(/"[^"]*"/,"").trim();for(let A of a.split(/\s+/)){let l=A.toUpperCase();(l==="PK"||l==="FK"||l==="UK")&&o.push(l)}return{type:n,name:s,keys:o,comment:r}}function pr(t){let e=t.match(/^(\S+)\s+([|o}{]+(?:--|\.\.)[|o}{]+)\s+(\S+)\s*:\s*(.+)$/);if(!e)return null;let n=e[1],s=e[2],i=e[3],o=e[4].trim().replace(/^["']|["']$/g,""),r=lt(o),c=s.match(/^([|o}{]+)(--|\.\.?)([|o}{]+)$/);if(!c)return null;let a=c[1],A=c[2],l=c[3],u=Re(a),d=Re(l),h=A==="--";return!u||!d?null:{entity1:n,entity2:i,cardinality1:u,cardinality2:d,label:r,identifying:h}}function Re(t){let e=t.split("").sort().join("");return e==="||"?"one":e==="o|"?"zero-one":e==="|}"||e==="{|"?"many":e==="{o"||e==="o{"?"zero-many":null}function yr(t){return/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|]$/.test(t)?"border":"text"}function br(t){return`${t.keys.length>0?t.keys.join(",")+" ":" "}${t.type} ${t.name}`}function wr(t){let e=ct(t.label),n=t.attributes.map(br);return n.length===0?[e]:[e,n]}function zt(t,e,n=!1){if(e)switch(t){case"one":return"|";case"zero-one":return"o|";case"many":return n?"<":">";case"zero-many":return n?"o<":">o"}else switch(t){case"one":return"\u2502";case"zero-one":return"\u25CB\u2502";case"many":return n?"\u255F":"\u2562";case"zero-many":return n?"\u25CB\u255F":"\u2562\u25CB"}}function Cr(t){let e=new Set,n=[],s=new Map;for(let o of t.entities)s.set(o.id,new Set);for(let o of t.relationships)s.get(o.entity1)?.add(o.entity2),s.get(o.entity2)?.add(o.entity1);function i(o,r){let c=[o];for(;c.length>0;){let a=c.pop();if(!e.has(a)){e.add(a),r.add(a);for(let A of s.get(a)??[])e.has(A)||c.push(A)}}}for(let o of t.entities)if(!e.has(o.id)){let r=new Set;i(o.id,r),r.size>0&&n.push(r)}return n}function Ir(t,e,n,s){let i=t.split(`
9`).map(W=>W.trim()).filter(W=>W.length>0&&!W.startsWith("%%")),o=xr(i);if(o.entities.length===0)return"";let r=e.useAscii,c=6,a=4,A=6,l=new Map,u=new Map,d=new Map,h=new Map;for(let W of o.entities){h.set(W.id,W);let z=wr(W);l.set(W.id,z);let y=0;for(let E of z)for(let _ of E)y=Math.max(y,_.length);let O=y+4,V=0;for(let E of z)V+=Math.max(E.length,1);let q=V+(z.length-1)+2;u.set(W.id,O),d.set(W.id,q)}let g=Cr(o),f=new Map,p=0;for(let W of g){let z=o.entities.filter(_=>W.has(_.id)),y=Math.max(2,Math.ceil(Math.sqrt(z.length))),O=0,V=0,q=0,E=p;for(let _ of z){let st=u.get(_.id),it=d.get(_.id);q>=y&&(p+=V+a,O=0,V=0,q=0),f.set(_.id,{entity:_,sections:l.get(_.id),x:O,y:p,width:st,height:it}),O+=st+c,V=Math.max(V,it),q++}p+=V+A}let I=0,H=0;for(let W of f.values())I=Math.max(I,W.x+W.width),H=Math.max(H,W.y+W.height);I+=4,H+=2;let B=dt(I-1,H-1),R=Ut(I-1,H-1);function D(W,z,y,O){W>=0&&W<B.length&&z>=0&&z<(B[0]?.length??0)&&(B[W][z]=y,Pt(R,W,z,O))}for(let W of f.values()){let z=qe(W.sections,r);for(let y=0;y<z.length;y++)for(let O=0;O<z[0].length;O++){let V=z[y][O];if(V!==" "){let q=W.x+y,E=W.y+O;q<I&&E<H&&D(q,E,V,yr(V))}}}let j=r?"-":"\u2500",ht=r?"|":"\u2502",L=r?".":"\u254C",nt=r?":":"\u250A";for(let W of o.relationships){let z=f.get(W.entity1),y=f.get(W.entity2);if(!z||!y)continue;let O=W.identifying?j:L,V=W.identifying?ht:nt,q=z.x+Math.floor(z.width/2),E=z.y+Math.floor(z.height/2),_=y.x+Math.floor(y.width/2),st=y.y+Math.floor(y.height/2);if(Math.abs(E-st)<Math.max(z.height,y.height)){let[w,G]=q<_?[z,y]:[y,z],[F,C]=q<_?[W.cardinality1,W.cardinality2]:[W.cardinality2,W.cardinality1],N=w.x+w.width,tt=G.x-1,m=w.y+Math.floor(w.height/2);for(let v=N;v<=tt;v++)D(v,m,O,"line");let b=zt(F,r,!1);for(let v=0;v<b.length;v++)D(N+v,m,b[v],"arrow");let S=zt(C,r,!0);for(let v=0;v<S.length;v++)D(tt-S.length+1+v,m,S[v],"arrow");if(W.label){let v=ct(W.label),Q=Math.floor((N+tt)/2);for(let k=0;k<v.length;k++){let P=v[k],Y=Math.max(N,Q-Math.floor(P.length/2)),M=m+1+k;Ft(B,Math.max(Y+P.length,1),Math.max(M+1,1)),Lt(R,Math.max(Y+P.length,1),Math.max(M+1,1));for(let Z=0;Z<P.length;Z++){let $=Y+Z;$>=N&&$<=tt&&D($,M,P[Z],"text")}}}}else{let[w,G]=E<st?[z,y]:[y,z],[F,C]=E<st?[W.cardinality1,W.cardinality2]:[W.cardinality2,W.cardinality1],N=w.y+w.height,tt=G.y-1,m=w.x+Math.floor(w.width/2);for(let k=N;k<=tt;k++)D(m,k,V,"line");let b=G.x+Math.floor(G.width/2);if(m!==b){let k=Math.floor((N+tt)/2),P=Math.min(m,b),Y=Math.max(m,b);for(let M=P;M<=Y;M++)D(M,k,O,"line");for(let M=k+1;M<=tt;M++)D(b,M,V,"line")}let S=zt(F,r,!1);for(let k=0;k<S.length;k++)D(m-Math.floor(S.length/2)+k,N,S[k],"arrow");let v=m!==b?b:m,Q=zt(C,r,!0);for(let k=0;k<Q.length;k++)D(v-Math.floor(Q.length/2)+k,tt,Q[k],"arrow");if(W.label){let k=ct(W.label),Y=Math.floor((N+tt)/2)-Math.floor((k.length-1)/2);for(let M=0;M<k.length;M++){let Z=k[M],$=m+2,K=Y+M;if(K>=0)for(let rt=0;rt<Z.length;rt++){let et=$+rt;et>=0&&(Ft(B,et+1,K+1),Lt(R,et+1,K+1),D(et,K,Z[rt],"text"))}}}}}return ne(B,{roleCanvas:R,colorMode:n,theme:s})}function Br(t){let e={},n={},s=[],i,o=!1;for(let r of t){if(/^xychart(-beta)?\b/i.test(r)){/\bhorizontal\b/i.test(r)&&(o=!0);continue}let c=r.match(/^title\s+"([^"]+)"/);if(c){i=c[1];continue}let a=r.match(/^x-axis\s+(?:"([^"]*)"\s*)?\[([^\]]+)\]/);if(a){a[1]&&(e.title=a[1]),e.categories=a[2].split(",").map(g=>g.trim());continue}let A=r.match(/^x-axis\s+(?:"([^"]*)"\s+)?(-?\d+(?:\.\d+)?)\s*-->\s*(-?\d+(?:\.\d+)?)/);if(A){A[1]&&(e.title=A[1]),e.range={min:parseFloat(A[2]),max:parseFloat(A[3])};continue}let l=r.match(/^y-axis\s+(?:"([^"]*)"\s+)?(-?\d+(?:\.\d+)?)\s*-->\s*(-?\d+(?:\.\d+)?)/);if(l){l[1]&&(n.title=l[1]),n.range={min:parseFloat(l[2]),max:parseFloat(l[3])};continue}let u=r.match(/^y-axis\s+"([^"]+)"\s*$/);if(u){n.title=u[1];continue}let d=r.match(/^bar\s+\[([^\]]+)\]/);if(d){s.push({type:"bar",data:Ke(d[1])});continue}let h=r.match(/^line\s+\[([^\]]+)\]/);if(h){s.push({type:"line",data:Ke(h[1])});continue}}if(!n.range&&s.length>0){let r=s.flatMap(l=>l.data),c=Math.min(...r),a=Math.max(...r),A=a-c||1;c=c-A*.1,a=a+A*.1,c>0&&c<A*.5&&(c=0),n.range={min:c,max:a}}return n.range||(n.range={min:0,max:100}),{title:i,horizontal:o,xAxis:e,yAxis:n,series:s}}function Ke(t){return t.split(",").map(e=>parseFloat(e.trim()))}var tn="#3b82f6";function en(t){let e=t.replace("#",""),n=parseInt(e.substring(0,2),16)/255,s=parseInt(e.substring(2,4),16)/255,i=parseInt(e.substring(4,6),16)/255,o=Math.max(n,s,i),r=Math.min(n,s,i),c=(o+r)/2;if(o===r)return[0,0,c*100];let a=o-r,A=c>.5?a/(2-o-r):a/(o+r),l;return o===n?l=((s-i)/a+(s<i?6:0))/6:o===s?l=((i-n)/a+2)/6:l=((n-s)/a+4)/6,[l*360,A*100,c*100]}function Mr(t,e,n){let s=e/100,i=n/100,o=(1-Math.abs(2*i-1))*s,r=o*(1-Math.abs(t/60%2-1)),c=i-o/2,a,A,l;t<60?(a=o,A=r,l=0):t<120?(a=r,A=o,l=0):t<180?(a=0,A=o,l=r):t<240?(a=0,A=r,l=o):t<300?(a=r,A=0,l=o):(a=o,A=0,l=r);let u=d=>Math.round((d+c)*255).toString(16).padStart(2,"0");return`#${u(a)}${u(A)}${u(l)}`}function Je(t){return/^#[0-9a-fA-F]{6}$/.test(t)}function kr(t){return en(t)[2]<50}function $r(t,e,n){if(t===0)return e;let s=Je(e)?e:tn,i=n&&Je(n)?n:void 0,[o,r]=en(s),c=Math.max(55,Math.min(85,r)),a=Math.ceil(t/2),A=t%2===1,l=i&&kr(i)?!A:A,u=l?Math.max(25,48-a*13):Math.min(78,55+a*11),d=(l?-8:12)*a,h=((o+d)%360+360)%360;return Mr(h,c,u)}var nn=60,on=20,Dr={hLine:"\u2500",vLine:"\u2502",origin:"\u253C",yTick:"\u2524",xTick:"\u252C",bar:"\u2588",grid:"\xB7",cornerTL:"\u256D",cornerTR:"\u256E",cornerBL:"\u2570",cornerBR:"\u256F"},vr={hLine:"-",vLine:"|",origin:"+",yTick:"+",xTick:"+",bar:"#",grid:".",cornerTL:"+",cornerTR:"+",cornerBL:"+",cornerBR:"+"};function rn(t,e){let n=e.accent??tn;return t<=1?[n]:Array.from({length:t},(s,i)=>$r(i,n,e.bg))}function Sr(t,e){switch(t){case"text":return e.fg;case"border":return e.border;case"line":return e.line;case"arrow":return e.arrow;case"corner":return e.corner??e.line;case"junction":return e.junction??e.border;default:return e.fg}}function Er(t,e,n,s){let i=t.split(`
10`).map(c=>c.trim()).filter(c=>c.length>0&&!c.startsWith("%%")),o=Br(i),r=e.useAscii?vr:Dr;return o.horizontal?Yr(o,r,n,s):Wr(o,r,n,s)}function Wr(t,e,n,s){let i=hn(t);if(i===0)return"";let o=t.yAxis.range,r=gn(o.min,o.max),c=r.map(E=>te(E)),a=Math.max(...c.map(E=>E.length))+1,A=Math.max(nn,i*6),l=on,u=Math.floor(A/i),d=un(t,i),h=!!t.title,g=!!t.xAxis.title,f=t.series.length>1,p=h?0:-1,I=(h?2:0)+(f?1:0),H=a+1,B=H+u*i+2,R=I+l,D=R+1,j=g?D+1:-1,ht=D+1+(g?1:0)+0,L=cn(B,ht),nt=an(B,ht),W=An(B,ht),z=rn(t.series.length,s),y=E=>{let _=(E-o.min)/(o.max-o.min||1);return Math.round(_*(l-1))},O=E=>H+Math.floor(u*(E+.5));h&&p>=0&&$t(L,nt,p,Math.floor(B/2-t.title.length/2),t.title,"text"),f&&sn(L,nt,W,t,h?1:0,B,e,z);for(let E=0;E<l;E++){let _=I+(l-1-E);gt(L,nt,_,H-1,e.vLine,"border")}gt(L,nt,R,H-1,e.origin,"border");for(let E of r){let _=y(E);if(_<0||_>=l)continue;let st=I+(l-1-_),it=te(E);gt(L,nt,st,H-1,_===0?e.origin:e.yTick,"border");let w=a-it.length;$t(L,nt,st,Math.max(0,w),it,"text")}for(let E=H;E<H+u*i;E++)gt(L,nt,R,E,e.hLine,"border");for(let E=0;E<i;E++){let _=O(E);gt(L,nt,R,_,e.xTick,"border");let st=d[E],it=_-Math.floor(st.length/2);$t(L,nt,D,Math.max(0,it),st,"text")}if(g&&j>=0){let E=t.xAxis.title;$t(L,nt,j,Math.floor(B/2-E.length/2),E,"text")}for(let E of r){let _=y(E);if(_<0||_>=l)continue;let st=I+(l-1-_);for(let it=H;it<H+u*i;it++)ln(L,st,it)===" "&>(L,nt,st,it,e.grid,"line")}let V=[];for(let E=0;E<t.series.length;E++)t.series[E].type==="bar"&&V.push({data:t.series[E].data,globalIdx:E});if(V.length>0){let E=V.length,_=Math.max(1,u-2),st=Math.max(1,Math.min(Math.floor(_/E),8)),it=st*E+(E-1),w=y(Math.max(0,o.min));for(let G=0;G<V.length;G++){let F=V[G],C=z[F.globalIdx];for(let N=0;N<F.data.length;N++){let b=O(N)-Math.floor(it/2)+G*(st+1),S=y(F.data[N]),v=Math.min(w,S),Q=Math.max(w,S);for(let k=v;k<=Q;k++){let P=I+(l-1-k);for(let Y=b;Y<b+st;Y++)gt(L,nt,P,Y,e.bar,"arrow",W,C)}}}}let q=[];for(let E=0;E<t.series.length;E++)t.series[E].type==="line"&&q.push({data:t.series[E].data,globalIdx:E});for(let E of q){if(E.data.length===0)continue;let _=z[E.globalIdx];Fr(L,nt,E.data,O,y,I,l,H,u*i,e,W,_)}return dn(L,nt,W,n,s)}function Yr(t,e,n,s){let i=hn(t);if(i===0)return"";let o=t.yAxis.range,r=gn(o.min,o.max),c=un(t,i),a=Math.max(...c.map(y=>y.length))+1,A=Math.max(nn,40),l=Math.max(2,Math.floor(on/i)),u=l*i,d=!!t.title,h=!!t.yAxis.title,g=t.series.length>1,f=(d?2:0)+(g?1:0),p=a+1,I=p+A+2,H=f+u+2+(h?1:0),B=f+u,R=cn(I,H),D=an(I,H),j=An(I,H),ht=rn(t.series.length,s),L=y=>{let O=(y-o.min)/(o.max-o.min||1);return p+Math.round(O*(A-1))},nt=y=>f+Math.floor(l*(y+.5));d&&$t(R,D,0,Math.floor(I/2-t.title.length/2),t.title,"text"),g&&sn(R,D,j,t,d?1:0,I,e,ht);for(let y=f;y<f+u;y++)gt(R,D,y,p-1,e.vLine,"border");gt(R,D,B,p-1,e.origin,"border");for(let y=0;y<i;y++){let O=nt(y),V=c[y],q=a-V.length;$t(R,D,O,Math.max(0,q),V,"text")}for(let y=p;y<p+A;y++)gt(R,D,B,y,e.hLine,"border");for(let y of r){let O=L(y);if(O<p||O>=p+A)continue;gt(R,D,B,O,e.xTick,"border");let V=te(y);$t(R,D,B+1,O-Math.floor(V.length/2),V,"text")}if(h){let y=t.yAxis.title;$t(R,D,H-1,Math.floor(I/2-y.length/2),y,"text")}for(let y of r){let O=L(y);if(!(O<p||O>=p+A))for(let V=f;V<f+u;V++)ln(R,V,O)===" "&>(R,D,V,O,e.grid,"line")}let W=[];for(let y=0;y<t.series.length;y++)t.series[y].type==="bar"&&W.push({data:t.series[y].data,globalIdx:y});if(W.length>0){let y=W.length,O=1,V=O*y+(y-1),q=L(Math.max(0,o.min));for(let E=0;E<W.length;E++){let _=W[E],st=ht[_.globalIdx];for(let it=0;it<_.data.length;it++){let F=nt(it)-Math.floor(V/2)+E*(O+1),C=L(_.data[it]),N=Math.min(q,C),tt=Math.max(q,C);for(let m=F;m<F+O;m++)for(let b=N;b<=tt;b++)gt(R,D,m,b,e.bar,"arrow",j,st)}}}let z=[];for(let y=0;y<t.series.length;y++)t.series[y].type==="line"&&z.push({data:t.series[y].data,globalIdx:y});for(let y of z){if(y.data.length===0)continue;let O=ht[y.globalIdx];Qr(R,D,y.data,nt,L,f,u,p,A,e,j,O)}return dn(R,D,j,n,s)}function Fr(t,e,n,s,i,o,r,c,a,A,l,u){if(n.length===0)return;let d=n.map((g,f)=>({col:s(f),row:i(g)})),h=(g,f,p)=>{let I=o+(r-1-f);I>=0&&g>=c&&g<c+a&>(t,e,I,g,p,"arrow",l,u)};if(d.length===1){h(d[0].col,d[0].row,A.hLine);return}for(let g=0;g<d.length-1;g++){let f=d[g],p=d[g+1];if(f.row===p.row){for(let D=f.col;D<=p.col;D++)h(D,f.row,A.hLine);continue}let I=Math.round((f.col+p.col)/2),H=p.row>f.row;for(let D=f.col;D<I;D++)h(D,f.row,A.hLine);H?h(I,f.row,A.cornerBR):h(I,f.row,A.cornerTR);let B=Math.min(f.row,p.row),R=Math.max(f.row,p.row);for(let D=B+1;D<R;D++)h(I,D,A.vLine);H?h(I,p.row,A.cornerTL):h(I,p.row,A.cornerBL);for(let D=I+1;D<=p.col;D++)h(D,p.row,A.hLine);if(g===0){let D=Math.max(c,f.col-Math.floor((p.col-f.col)/4));for(let j=D;j<f.col;j++)h(j,f.row,A.hLine)}if(g===d.length-2){let D=Math.min(c+a-1,p.col+Math.floor((p.col-f.col)/4));for(let j=p.col+1;j<=D;j++)h(j,p.row,A.hLine)}}}function Qr(t,e,n,s,i,o,r,c,a,A,l,u){if(n.length===0)return;let d=n.map((g,f)=>({row:s(f),col:i(g)})),h=(g,f,p)=>{g>=o&&g<o+r&&f>=c&&f<c+a&>(t,e,g,f,p,"arrow",l,u)};if(d.length===1){h(d[0].row,d[0].col,A.vLine);return}for(let g=0;g<d.length-1;g++){let f=d[g],p=d[g+1];if(f.col===p.col){for(let D=f.row;D<=p.row;D++)h(D,f.col,A.vLine);continue}let I=Math.round((f.row+p.row)/2),H=p.col>f.col;for(let D=f.row;D<I;D++)h(D,f.col,A.vLine);H?h(I,f.col,A.cornerBL):h(I,f.col,A.cornerBR);let B=Math.min(f.col,p.col),R=Math.max(f.col,p.col);for(let D=B+1;D<R;D++)h(I,D,A.hLine);H?h(I,p.col,A.cornerTR):h(I,p.col,A.cornerTL);for(let D=I+1;D<=p.row;D++)h(D,p.col,A.vLine)}}function sn(t,e,n,s,i,o,r,c){let a=[],A=0,l=0;for(let g=0;g<s.series.length;g++)s.series[g].type==="bar"?(a.push({symbol:r.bar,label:`Bar ${A+1}`,globalIdx:g}),A++):(a.push({symbol:r.hLine,label:`Line ${l+1}`,globalIdx:g}),l++);let u=0;for(let g=0;g<a.length;g++)g>0&&(u+=2),u+=2+a[g].label.length;let h=Math.max(0,Math.floor(o/2-u/2));for(let g=0;g<a.length;g++){g>0&&(h+=2);let f=a[g];gt(t,e,i,h,f.symbol,"arrow",n,c[f.globalIdx]),h+=1,h+=1,$t(t,e,i,h,f.label,"text"),h+=f.label.length}}function cn(t,e){return Array.from({length:t},()=>Array.from({length:e},()=>" "))}function an(t,e){return Array.from({length:t},()=>Array.from({length:e}).fill(null))}function An(t,e){return Array.from({length:t},()=>Array.from({length:e}).fill(null))}function gt(t,e,n,s,i,o,r,c){s>=0&&s<t.length&&n>=0&&n<t[0].length&&(t[s][n]=i,e[s][n]=o,r&&c&&(r[s][n]=c))}function ln(t,e,n){return n>=0&&n<t.length&&e>=0&&e<t[0].length?t[n][e]:" "}function $t(t,e,n,s,i,o){for(let r=0;r<i.length;r++)gt(t,e,n,s+r,i[r],o)}function dn(t,e,n,s,i){if(t.length===0)return"";let o=t[0].length,r=t.length,c=[];for(let a=0;a<o;a++){let A=[],l=[],u=[];for(let h=0;h<r;h++)A.push(t[h][a]),l.push(e[h][a]),u.push(n[h][a]);let d=A.length-1;for(;d>=0&&A[d]===" ";)d--;d<0?c.push(""):c.push(Hr(A.slice(0,d+1),l.slice(0,d+1),u.slice(0,d+1),i,s))}for(;c.length>0&&c[c.length-1]==="";)c.pop();return c.join(`
11`)}function Hr(t,e,n,s,i){if(i==="none")return t.join("");let o="",r=null,c="";for(let a=0;a<t.length;a++){let A=t[a];if(A===" "){c.length>0&&(o+=r?Ae(c,r,i):c,c="",r=null),o+=" ";continue}let l=n[a]??null,u=e[a]??null,d=l??(u?Sr(u,s):null);d===r?c+=A:(c.length>0&&(o+=r?Ae(c,r,i):c),c=A,r=d)}return c.length>0&&(o+=r?Ae(c,r,i):c),o}function hn(t){if(t.xAxis.categories)return t.xAxis.categories.length;for(let e of t.series)if(e.data.length>0)return e.data.length;return 0}function un(t,e){if(t.xAxis.categories)return t.xAxis.categories;if(t.xAxis.range){let{min:n,max:s}=t.xAxis.range,i=e>1?(s-n)/(e-1):0;return Array.from({length:e},(o,r)=>te(n+i*r))}return Array.from({length:e},(n,s)=>String(s+1))}function gn(t,e){let n=e-t;if(n<=0)return[t];let s=n/6,i=Math.pow(10,Math.floor(Math.log10(s))),o=s/i,r;o<=1.5?r=i:o<=3?r=2*i:o<=7?r=5*i:r=10*i;let c=Math.ceil(t/r)*r,a=[];for(let A=c;A<=e+r*.001;A+=r)a.push(Math.round(A*1e10)/1e10);return a}function te(t){return Number.isInteger(t)?String(t):t.toFixed(Math.abs(t)<10?1:0)}function Lr(t){let e=t.trim().split(`
12`)[0]?.trim().toLowerCase()??"";return/^xychart(-beta)?\b/.test(e)?"xychart":/^sequencediagram\s*$/.test(e)?"sequence":/^classdiagram\s*$/.test(e)?"class":/^erdiagram\s*$/.test(e)?"er":"flowchart"}function Gr(t,e={}){let n={useAscii:e.useAscii??!1,paddingX:e.paddingX??5,paddingY:e.paddingY??5,boxBorderPadding:e.boxBorderPadding??1,graphDirection:"TD"},s=e.colorMode==="auto"||e.colorMode===void 0?Gn():e.colorMode,i={...Ne,...e.theme};switch(Lr(t)){case"xychart":return Er(t,n,s,i);case"sequence":return ar(t,n,s,i);case"class":return fr(t,n,s,i);case"er":return Ir(t,n,s,i);case"flowchart":default:{let r=Mn(t);r.direction==="LR"||r.direction==="RL"?n.graphDirection="LR":n.graphDirection="TD";let c=zn(r,n);return sr(c),Vo(c),r.direction==="BT"&&(Un(c.canvas),Pn(c.roleCanvas)),ne(c.canvas,{roleCanvas:c.roleCanvas,colorMode:s,theme:i})}}}var Rr="'JetBrains Mono'",Ms=`${Rr}, 'SF Mono', 'Fira Code', ui-monospace, monospace`;export{Gr as renderMermaidASCII};
13