mermaid-text 0.42.2

Render Mermaid diagrams as Unicode box-drawing text — no browser, no image protocols, pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
//! Hand-rolled parser for Mermaid `graph`/`flowchart` syntax.
//!
//! The parser works statement-by-statement. A "statement" is one logical
//! declaration separated by a newline or semicolon. Each statement is
//! classified as either:
//!
//! - A **node definition**: `A[Label]`, `A{Label}`, `A((Label))`, `A(Label)`, or bare `A`
//! - An **edge chain**: `A --> B --> C`, potentially with inline labels
//! - A **subgraph block**: `subgraph ID [Label]` … `end`
//! - A **header line**: `graph LR` / `flowchart TD` (handled before entering this module)
//! - A blank / comment line — silently ignored
//!
//! Edge style (`-->`, `---`, `-.->`, `==>`, `<-->`, `--o`, `--x`) is parsed
//! and stored on each [`Edge`] for the renderer to use.

use crate::{
    Error,
    parser::common::{
        apply_pending_classes, extract_class_modifier, parse_class_def_directive,
        parse_class_directive, parse_link_style_directive, parse_style_directive,
    },
    types::{
        ClickTarget, Direction, Edge, EdgeEndpoint, EdgeStyle, Graph, Node, NodeShape, Subgraph,
    },
};

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Parse a Mermaid `graph`/`flowchart` source string into a [`Graph`].
///
/// The function expects the *full* input including the header line
/// (`graph LR`, `flowchart TD`, etc.). Both newlines and semicolons are
/// treated as statement separators, so `graph LR; A-->B` is valid.
///
/// # Arguments
///
/// * `input` — the complete Mermaid source string
///
/// # Returns
///
/// A [`Graph`] containing all parsed nodes, edges, and subgraphs.
///
/// # Errors
///
/// Returns [`crate::Error::ParseError`] if the header statement is missing or
/// the direction keyword is unrecognised.
///
/// # Examples
///
/// ```
/// use mermaid_text::parser::parse;
/// use mermaid_text::{Direction, NodeShape};
///
/// let graph = parse("graph LR; A[Start] --> B[End]").unwrap();
/// assert_eq!(graph.direction, Direction::LeftToRight);
/// assert_eq!(graph.node("A").unwrap().label, "Start");
/// assert_eq!(graph.node("B").unwrap().shape, NodeShape::Rectangle);
/// assert_eq!(graph.edges.len(), 1);
/// ```
pub fn parse(input: &str) -> Result<Graph, Error> {
    // Normalise: replace newlines with semicolons, then split on ';'.
    // This means both `graph LR; A-->B` and multi-line input are handled
    // identically — the first non-blank, non-comment statement is the header.
    let normalised = input.replace('\n', ";").replace('\r', "");

    let statements: Vec<&str> = normalised
        .split(';')
        .map(str::trim)
        .filter(|s| !s.is_empty() && !s.starts_with("%%"))
        .collect();

    let mut iter = statements.iter().copied();

    // ---- Find and parse the header statement ----------------------------
    let direction = parse_header_stmt(&mut iter)?;
    let mut graph = Graph::new(direction);

    // ---- Parse each remaining statement ---------------------------------
    // We collect remaining statements into a Vec so we can do a stateful
    // multi-statement parse (subgraph blocks span multiple statements).
    //
    // `pending_classes` collects `(target_id, class_name)` from `class …`
    // directives and from inline `:::className` shorthands. Resolution
    // happens at end-of-parse so a `class A foo` statement can appear
    // before its `classDef foo …` definition (a real Mermaid pattern).
    let remaining: Vec<&str> = iter.collect();
    let mut pending_classes: Vec<(String, String)> = Vec::new();
    parse_statements(&remaining, &mut graph, &mut None, &mut pending_classes);
    apply_pending_classes(&mut graph, &pending_classes);

    Ok(graph)
}

// ---------------------------------------------------------------------------
// Header parsing
// ---------------------------------------------------------------------------

/// Consume the first statement from `stmts` and parse it as a
/// `graph`/`flowchart` header, returning the [`Direction`].
///
/// The direction is the first whitespace-delimited token after the keyword.
fn parse_header_stmt<'a>(stmts: &mut impl Iterator<Item = &'a str>) -> Result<Direction, Error> {
    let stmt = stmts
        .next()
        .ok_or_else(|| Error::ParseError("no 'graph'/'flowchart' header found".to_string()))?;

    // e.g. "graph LR" or "flowchart TD"
    let mut parts = stmt.splitn(3, |c: char| c.is_whitespace());
    let keyword = parts.next().unwrap_or("").to_lowercase();

    if keyword != "graph" && keyword != "flowchart" {
        return Err(Error::ParseError(format!(
            "expected 'graph' or 'flowchart', got '{keyword}'"
        )));
    }

    // The direction is the next whitespace-separated token (just the first
    // word — we ignore any trailing content on the header line since we
    // already split on semicolons above).
    let dir_str = parts
        .next()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .unwrap_or("TD"); // default to top-down if omitted

    Direction::parse(dir_str)
        .ok_or_else(|| Error::ParseError(format!("unknown direction '{dir_str}'")))
}

// ---------------------------------------------------------------------------
// Statement parsing
// ---------------------------------------------------------------------------

/// Parse a slice of statements into `graph`.
///
/// `current_subgraph_id` is `Some(id)` when we are inside a subgraph block
/// (used to register node membership). This function is called recursively
/// for nested subgraphs — the inner call consumes statements up through `end`
/// and the outer call continues from there.
///
/// Returns the index of the statement **after** the `end` that closed the
/// innermost subgraph block, or `stmts.len()` if there was no `end`.
fn parse_statements(
    stmts: &[&str],
    graph: &mut Graph,
    current_subgraph_id: &mut Option<String>,
    pending_classes: &mut Vec<(String, String)>,
) -> usize {
    let mut i = 0;
    while i < stmts.len() {
        let stmt = stmts[i];
        let first_word = stmt.split_whitespace().next().unwrap_or("");

        match first_word {
            "subgraph" => {
                // Parse the subgraph header: `subgraph ID` or `subgraph ID Label`
                let (sg_id, sg_label) = parse_subgraph_header(stmt);

                // Register this subgraph in the parent (or at the top level).
                if let Some(ref parent_id) = current_subgraph_id.clone() {
                    // Nested: link child into parent.
                    if let Some(parent) = graph.subgraphs.iter_mut().find(|s| &s.id == parent_id) {
                        parent.subgraph_ids.push(sg_id.clone());
                    }
                }
                graph.subgraphs.push(Subgraph::new(sg_id.clone(), sg_label));

                // Recurse into the subgraph body. The recursive call consumes
                // statements until it hits the matching `end` and returns the
                // index of the statement after `end`.
                let mut inner_sg = Some(sg_id);
                i += 1;
                let consumed = parse_statements(&stmts[i..], graph, &mut inner_sg, pending_classes);
                i += consumed;
            }
            "end" => {
                // Close the current subgraph block. Tell the caller we consumed
                // through (and including) this `end`.
                return i + 1;
            }
            "direction" => {
                // `direction TB` inside a subgraph: store it on the model.
                if let Some(ref sg_id) = current_subgraph_id.clone() {
                    let dir_word = stmt.split_whitespace().nth(1).unwrap_or("");
                    if let Some(dir) = Direction::parse(dir_word)
                        && let Some(sg) = graph.subgraphs.iter_mut().find(|s| s.id == *sg_id)
                    {
                        sg.direction = Some(dir);
                    }
                }
                i += 1;
            }
            "style" => {
                parse_style_directive(stmt, graph);
                i += 1;
            }
            "linkStyle" => {
                parse_link_style_directive(stmt, graph);
                i += 1;
            }
            "classDef" => {
                parse_class_def_directive(stmt, graph);
                i += 1;
            }
            "class" => {
                parse_class_directive(stmt, pending_classes);
                i += 1;
            }
            "click" => {
                parse_click_directive(stmt, graph);
                i += 1;
            }
            // Accessibility directives — no visual representation.
            "accTitle" | "accDescr" => {
                i += 1;
            }
            _ => {
                // Regular node definition or edge chain.
                parse_statement(stmt, graph, current_subgraph_id, pending_classes);
                i += 1;
            }
        }
    }
    // Consumed all statements without seeing `end` (top-level or unclosed block).
    stmts.len()
}

/// Parse a single statement (already trimmed, no leading/trailing whitespace).
///
/// A statement is either a standalone node definition or an edge chain that
/// may include inline node definitions.
///
/// Any nodes referenced in edges are auto-created if they have not been
/// explicitly defined yet. If `current_subgraph_id` is `Some`, newly seen
/// node IDs are recorded as members of that subgraph.
fn parse_statement(
    stmt: &str,
    graph: &mut Graph,
    current_subgraph_id: &mut Option<String>,
    pending_classes: &mut Vec<(String, String)>,
) {
    // Try to parse as an edge chain first (contains an arrow token).
    if looks_like_edge_chain(stmt) {
        parse_edge_chain(stmt, graph, current_subgraph_id, pending_classes);
    } else {
        // Pure node definition: A[label]:::cls, A{label}, A((label)), A(label), A
        // Strip the optional `:::cls1:::cls2` shorthand BEFORE the shape
        // parser sees the token — shape parsing doesn't know about
        // class modifiers.
        let (clean_stmt, classes) = extract_class_modifier(stmt);
        if let Some(node) = parse_node_definition(&clean_stmt) {
            let node_id = node.id.clone();
            for class_name in classes {
                pending_classes.push((node_id.clone(), class_name));
            }
            graph.upsert_node(node);
            register_node_in_subgraph(graph, &node_id, current_subgraph_id);
        }
    }
}

/// Register `node_id` as a direct member of the current subgraph (if any).
///
/// Only registers if the node is not already a member (avoids duplicates from
/// multiple references to the same node within one subgraph body).
fn register_node_in_subgraph(
    graph: &mut Graph,
    node_id: &str,
    current_subgraph_id: &Option<String>,
) {
    if let Some(sg_id) = current_subgraph_id
        && let Some(sg) = graph.subgraphs.iter_mut().find(|s| s.id == *sg_id)
        && !sg.node_ids.contains(&node_id.to_string())
    {
        sg.node_ids.push(node_id.to_string());
    }
}

/// Parse the `subgraph` header statement and extract `(id, label)`.
///
/// Mermaid supports these forms:
/// - `subgraph ID`           — label defaults to ID
/// - `subgraph ID[Label]`    — label in square brackets (no space before `[`)
/// - `subgraph ID [Label]`   — same with a space
/// - `subgraph "Label"`      — quoted label used as both id and label
fn parse_subgraph_header(stmt: &str) -> (String, String) {
    // Strip the "subgraph" keyword.
    let rest = stmt.trim_start_matches("subgraph").trim();

    if rest.is_empty() {
        // Bare `subgraph` with no identifier — use a placeholder.
        return ("__sg__".to_string(), "".to_string());
    }

    // Check for bracket-style label: `ID[Label]` or `ID [Label]`.
    if let Some(bracket_pos) = rest.find('[') {
        let id = rest[..bracket_pos].trim().to_string();
        let rest_after = &rest[bracket_pos + 1..];
        let label = if let Some(close) = rest_after.find(']') {
            rest_after[..close].trim().to_string()
        } else {
            rest_after.trim().to_string()
        };
        let id = if id.is_empty() { label.clone() } else { id };
        return (id, label);
    }

    // No bracket: the entire rest is the ID, and label == ID.
    let id = rest.to_string();
    (id.clone(), id)
}

/// Return `true` if the statement appears to contain at least one edge arrow.
fn looks_like_edge_chain(s: &str) -> bool {
    // Quick scan: any known arrow token.
    //
    // Note: the inline-quoted dashed form `-. "label" .->` does NOT contain
    // the substring `-.->` as a contiguous run (the label splits the two
    // halves), so we check for `-. "` explicitly.  The thick form `== "label"
    // ==>` does contain `==>`, so no extra check is needed there.
    s.contains("-->")
        || s.contains("---")
        || s.contains("-.->")
        || s.contains("-. \"") // inline-quoted dashed: `-. "label" .->`
        || s.contains("==>")
        || s.contains("<-->")
        || s.contains("--o")
        || s.contains("--x")
        || s.contains("-- ") // "-- label -->" form
        || s.contains("--") // catch-all for remaining "--" forms
}

// ---------------------------------------------------------------------------
// Edge chain parsing
// ---------------------------------------------------------------------------

/// Parse an edge chain statement and push nodes + edges into `graph`.
///
/// The chain is tokenised by splitting on edge markers while preserving
/// edge-label content between `|...|` delimiters.
fn parse_edge_chain(
    stmt: &str,
    graph: &mut Graph,
    current_subgraph_id: &mut Option<String>,
    pending_classes: &mut Vec<(String, String)>,
) {
    // We build a list of (node_token, edge_label_or_none) pairs.
    // Strategy: walk char-by-char, extracting alternating node/edge segments.

    let tokens = tokenise_chain(stmt);
    if tokens.is_empty() {
        return;
    }

    // tokens = [node_tok, edge_tok, node_tok, edge_tok, node_tok, ...]
    // Odd indices are node tokens, even indices are edge (arrow+label) tokens.
    // Actually our tokeniser returns: node, arrow, node, arrow, node
    // i.e. length is always odd and ≥ 1.

    // Collect (node_token, Option<edge_label_before_next_node>) pairs.
    // We iterate pairs of (node_tok, Option<arrow_tok>).
    let mut i = 0;
    let mut prev_id: Option<String> = None;

    // Pending edge metadata carried forward between node tokens.
    let mut pending_edge_label: Option<String> = None;
    let mut pending_edge_style = EdgeStyle::Solid;
    let mut pending_edge_start = EdgeEndpoint::None;
    let mut pending_edge_end = EdgeEndpoint::Arrow;

    while i < tokens.len() {
        let tok = tokens[i].trim();

        if i % 2 == 0 {
            // Node token
            if tok.is_empty() {
                i += 1;
                continue;
            }
            // Strip optional `:::cls1:::cls2` shorthand BEFORE the
            // shape parser sees the token.
            let (clean_tok, classes) = extract_class_modifier(tok);
            let node = parse_node_definition(&clean_tok).unwrap_or_else(|| {
                // Treat as bare ID (after class stripping).
                Node::new(clean_tok.clone(), clean_tok.clone(), NodeShape::Rectangle)
            });
            let node_id = node.id.clone();
            for class_name in classes {
                pending_classes.push((node_id.clone(), class_name));
            }
            graph.upsert_node(node);
            register_node_in_subgraph(graph, &node_id, current_subgraph_id);

            if let Some(ref from) = prev_id {
                let edge = Edge::new_styled(
                    from.clone(),
                    node_id.clone(),
                    pending_edge_label.take(),
                    pending_edge_style,
                    pending_edge_start,
                    pending_edge_end,
                );
                graph.edges.push(edge);
                // Reset per-edge state for the next edge in the chain.
                pending_edge_style = EdgeStyle::Solid;
                pending_edge_start = EdgeEndpoint::None;
                pending_edge_end = EdgeEndpoint::Arrow;
            }
            prev_id = Some(node_id);
        } else {
            // Arrow token — extract style and optional label.
            let (style, start, end) = classify_arrow(tok);
            pending_edge_style = style;
            pending_edge_start = start;
            pending_edge_end = end;
            pending_edge_label = extract_arrow_label(tok);
        }

        i += 1;
    }
}

/// Split a chain statement into alternating node/arrow tokens.
///
/// Returns a `Vec<String>` where even indices are node tokens and odd indices
/// are arrow tokens (including any `|label|` portion).
fn tokenise_chain(stmt: &str) -> Vec<String> {
    let mut tokens: Vec<String> = Vec::new();
    let chars: Vec<char> = stmt.chars().collect();
    let len = chars.len();
    let mut i = 0;
    let mut current = String::new();

    while i < len {
        // Detect start of an arrow sequence.
        // Arrows: -->, ---, -.->, ==>, <-->, --o, --x, -- label -->, -->|label|
        // We look for `-`, `=`, or `<` (for bidirectional) not inside a node bracket.
        let ch = chars[i];

        let is_potential_arrow_start =
            (ch == '-' || ch == '=' || ch == '<') && !current.trim().is_empty();

        if is_potential_arrow_start && is_arrow_start(&chars, i) {
            // Push the current node token
            tokens.push(current.trim().to_string());
            current = String::new();

            // Consume the full arrow (including optional |label|)
            let (arrow_tok, consumed) = consume_arrow(&chars, i);
            tokens.push(arrow_tok);
            i += consumed;
            continue;
        }

        current.push(ch);
        i += 1;
    }

    // Push the last node token
    let last = current.trim().to_string();
    if !last.is_empty() {
        tokens.push(last);
    }

    tokens
}

/// Return `true` if position `i` in `chars` starts an arrow sequence.
fn is_arrow_start(chars: &[char], i: usize) -> bool {
    let remaining: String = chars[i..].iter().collect();
    remaining.starts_with("-->")
        || remaining.starts_with("---")
        || remaining.starts_with("-.->")
        || remaining.starts_with("==>")
        || remaining.starts_with("<-->")
        || remaining.starts_with("--o")
        || remaining.starts_with("--x")
        || remaining.starts_with("-- ") // "-- label -->" and "-- \"label\" -->"
        || remaining.starts_with("--")
        // Inline-quoted label forms: `-. "label" .->` and `== "label" ==>`
        // These don't start with the bare arrow token, so they need explicit
        // detection here so the tokeniser fires an arrow-consumption pass
        // instead of swallowing the `-`/`=` into the node token.
        || inline_quoted_arrow_matches(&remaining)
}

/// Return `true` if `s` starts with an inline-quoted-label arrow prefix.
///
/// Mermaid allows `A -. "label" .-> B` and `A == "label" ==> B` in addition
/// to the pipe-label form (`A -.->|"label"| B`). The inline-quoted form puts
/// the label *between* the two halves of the arrow, which means neither half
/// alone is a complete arrow token — we must detect the opening half here.
///
/// We check for `-. ` (dashed inline) and `== ` (thick inline) followed by
/// an opening double-quote. Single-quoted variants are not part of the
/// Mermaid spec and are intentionally not supported.
fn inline_quoted_arrow_matches(s: &str) -> bool {
    // Dashed: -. "label" .->
    (s.starts_with("-. \"") && s.contains(".->"))
        // Thick: == "label" ==>
        || (s.starts_with("== \"") && s.contains("==>"))
}

/// Classify an arrow token into `(style, start_endpoint, end_endpoint)`.
///
/// The classification mirrors the Mermaid specification:
/// - `<-->` → bidirectional solid
/// - `==>` → thick with arrow
/// - `-.->` / `-..->` → dotted with arrow
/// - `-->` → solid with arrow (default)
/// - `---` → solid, no arrow
/// - `--o` → solid, circle endpoint
/// - `--x` → solid, cross endpoint
fn classify_arrow(arrow: &str) -> (EdgeStyle, EdgeEndpoint, EdgeEndpoint) {
    // Strip any |label| portion before classifying.
    let base = if let Some(pipe) = arrow.find('|') {
        &arrow[..pipe]
    } else {
        arrow
    }
    .trim();

    // Bidirectional: <-->
    if base.starts_with('<') && base.ends_with('>') {
        return (EdgeStyle::Solid, EdgeEndpoint::Arrow, EdgeEndpoint::Arrow);
    }
    // Circle endpoint: --o
    if base.ends_with('o') && base.starts_with('-') {
        return (EdgeStyle::Solid, EdgeEndpoint::None, EdgeEndpoint::Circle);
    }
    // Cross endpoint: --x
    if base.ends_with('x') && base.starts_with('-') {
        return (EdgeStyle::Solid, EdgeEndpoint::None, EdgeEndpoint::Cross);
    }
    // Thick with arrow: ==>
    if base.starts_with('=') {
        let has_arrow = base.ends_with('>');
        let end = if has_arrow {
            EdgeEndpoint::Arrow
        } else {
            EdgeEndpoint::None
        };
        return (EdgeStyle::Thick, EdgeEndpoint::None, end);
    }
    // Dotted: -.- or -.->
    if base.contains(".-") || base.contains("-.") {
        let has_arrow = base.ends_with('>');
        let end = if has_arrow {
            EdgeEndpoint::Arrow
        } else {
            EdgeEndpoint::None
        };
        return (EdgeStyle::Dotted, EdgeEndpoint::None, end);
    }
    // Solid no-arrow: ---  or "-- label --" (no trailing >)
    if base.starts_with('-') && !base.ends_with('>') && !base.ends_with('o') && !base.ends_with('x')
    {
        return (EdgeStyle::Solid, EdgeEndpoint::None, EdgeEndpoint::None);
    }
    // Default: solid arrow -->
    (EdgeStyle::Solid, EdgeEndpoint::None, EdgeEndpoint::Arrow)
}

/// Consume an arrow starting at position `i`, returning `(arrow_token, chars_consumed)`.
///
/// Handles these forms:
/// - `-->` / `-->|label|`
/// - `---`
/// - `-.->` / `-.->|label|`
/// - `-. "label" .->` (inline-quoted dashed; normalised to `-.->|label|` internally)
/// - `==>`
/// - `== "label" ==>` (inline-quoted thick; normalised to `==>|label|` internally)
/// - `<-->`
/// - `--o` / `--x`
/// - `-- label -->` / `-- "label" -->`
fn consume_arrow(chars: &[char], start: usize) -> (String, usize) {
    let remaining: String = chars[start..].iter().collect();

    // "<-->" bidirectional (must check before "--" forms that start with '<')
    if let Some(rest) = remaining.strip_prefix("<-->") {
        let (label_part, extra) = try_consume_pipe_label(rest);
        let tok = format!("<-->{label_part}");
        return (tok, 4 + extra);
    }

    // "-- label -->" and "-- \"label\" -->" forms (must check before plain "--")
    if let Some(arrow) = try_consume_labeled_dash_arrow(&remaining) {
        let len = arrow.chars().count();
        return (arrow, len);
    }

    // Inline-quoted dashed:  `-. "label" .->` → normalise to `-.->|label|`
    // Inline-quoted thick:   `== "label" ==>` → normalise to `==>|label|`
    //
    // We normalise to the pipe-label form so that `classify_arrow` and
    // `extract_arrow_label` require no changes: both code paths share the
    // same downstream label handling.
    if let Some((tok, consumed)) = try_consume_inline_quoted_arrow(&remaining) {
        return (tok, consumed);
    }

    // "-.->"|label|? (also handles "-..->")
    if remaining.starts_with("-.-") {
        let base = if remaining.starts_with("-.->") { 4 } else { 3 };
        let (label_part, extra) = try_consume_pipe_label(&remaining[base..]);
        let tok = format!("{}{label_part}", &remaining[..base]);
        return (tok, base + extra);
    }

    // "==>" (also "===", "===>", etc.)
    if remaining.starts_with("==") {
        // Consume all '=' chars then optional '>'
        let mut len = 0;
        for ch in remaining.chars() {
            if ch == '=' {
                len += 1;
            } else {
                break;
            }
        }
        let has_arrow = remaining[len..].starts_with('>');
        if has_arrow {
            len += 1;
        }
        let (label_part, extra) = try_consume_pipe_label(&remaining[len..]);
        let tok = format!("{}{label_part}", &remaining[..len]);
        return (tok, len + extra);
    }

    // "--o" and "--x" endpoint markers
    if remaining.starts_with("--o") {
        return ("--o".to_string(), 3);
    }
    if remaining.starts_with("--x") {
        return ("--x".to_string(), 3);
    }

    // "-->" / "---"
    if let Some(rest) = remaining.strip_prefix("-->") {
        let (label_part, extra) = try_consume_pipe_label(rest);
        let tok = format!("-->{label_part}");
        return (tok, 3 + extra);
    }
    if let Some(rest) = remaining.strip_prefix("---") {
        let (label_part, extra) = try_consume_pipe_label(rest);
        let tok = format!("---{label_part}");
        return (tok, 3 + extra);
    }
    // Fallback: consume "--"
    (remaining[..2].to_string(), 2)
}

/// Try to parse inline-quoted-label arrow forms, returning `(normalised_token, chars_consumed)`.
///
/// Recognised patterns and their normalised output (pipe-label form):
///
/// | Source syntax        | Normalised token |
/// |----------------------|------------------|
/// | `-. "label" .->`     | `-.->|"label"|`  |
/// | `== "label" ==>`     | `==>|"label"|`   |
///
/// Normalising to pipe-label keeps `classify_arrow` and `extract_arrow_label`
/// unchanged — all three inline-quoted styles (solid handled by
/// `try_consume_labeled_dash_arrow`, dashed and thick handled here) share the
/// same downstream normalization path via `extract_arrow_label`'s pipe-label branch.
///
/// Returns `None` when `s` does not start with a recognised inline-quoted prefix.
fn try_consume_inline_quoted_arrow(s: &str) -> Option<(String, usize)> {
    // All characters in the arrow tokens and ASCII quotes are single-byte UTF-8,
    // so byte length == char count for consumption purposes. The label text
    // itself may contain multi-byte chars, but we don't need to count it
    // separately — we measure the whole consumed slice as `s.len() - tail.len()`.

    // Dashed: `-. "label" .->`  →  normalised as `-.->|"label"|`
    // `strip_prefix` enforces the opening `-. "` in one step (avoids the
    // clippy::manual_strip lint that fires on `starts_with` + index-slice).
    if let Some(after_open) = s.strip_prefix("-. \"")
        && let Some(close_q) = after_open.find('"')
    {
        // after_open[..close_q] is the label text; after_open[close_q+1..] is
        // everything after the closing quote.
        let label = &after_open[..close_q];
        let tail_start = after_open[close_q + 1..].trim_start_matches(' ');
        if let Some(tail) = tail_start.strip_prefix(".->") {
            let pipe_tok = format!("-.->|\"{label}\"|");
            let consumed = s.len() - tail.len();
            return Some((pipe_tok, consumed));
        }
    }

    // Thick: `== "label" ==>`  →  normalised as `==>|"label"|`
    if let Some(after_open) = s.strip_prefix("== \"")
        && let Some(close_q) = after_open.find('"')
    {
        let label = &after_open[..close_q];
        let tail_start = after_open[close_q + 1..].trim_start_matches(' ');
        if let Some(tail) = tail_start.strip_prefix("==>") {
            let pipe_tok = format!("==>|\"{label}\"|");
            let consumed = s.len() - tail.len();
            return Some((pipe_tok, consumed));
        }
    }

    None
}

/// Try to parse `-- label -->` form. Returns the full token string if matched.
fn try_consume_labeled_dash_arrow(s: &str) -> Option<String> {
    // Must start with "-- " (dash dash space)
    if !s.starts_with("-- ") {
        return None;
    }
    // Find closing "-->"
    let rest = &s[3..];
    rest.find("-->").map(|end| {
        let full_len = 3 + end + 3; // "-- " + label + "-->"
        s[..full_len].to_string()
    })
}

/// Try to consume a `|label|` suffix. Returns `(consumed_string, char_count)`.
fn try_consume_pipe_label(s: &str) -> (String, usize) {
    if let Some(inner) = s.strip_prefix('|')
        && let Some(end) = inner.find('|')
    {
        let portion = &s[..end + 2]; // includes both pipes
        return (portion.to_string(), end + 2);
    }
    (String::new(), 0)
}

/// Extract a label string from an arrow token, if present.
///
/// Handles `-->|label|`, `-- label -->`, etc.
fn extract_arrow_label(arrow: &str) -> Option<String> {
    // Pipe-style: -->|label| or -.->|label|
    let raw = if let Some(start) = arrow.find('|')
        && let Some(end) = arrow[start + 1..].find('|')
    {
        Some(arrow[start + 1..start + 1 + end].trim())
    } else if arrow.starts_with("-- ")
        && let Some(end) = arrow.rfind("-->")
    {
        // Dash-style: -- label -->
        Some(arrow[3..end].trim())
    } else {
        None
    };
    raw.and_then(|s| {
        // Strip the optional surrounding quotes Mermaid allows so labels
        // with commas / spaces survive, then run through `normalize_label`
        // so HTML `<br>` tags become real newlines (matching node labels)
        // and overly-long lines get soft-wrapped.
        let unquoted = s.trim_matches('"');
        let normalised = normalize_label(unquoted);
        if normalised.is_empty() {
            None
        } else {
            Some(normalised)
        }
    })
}

// ---------------------------------------------------------------------------
// Node definition parsing
// ---------------------------------------------------------------------------

/// Parse a single node-definition token such as `A[Label]`, `B{text}`,
/// `C((name))`, `D(rounded)`, `E([Stadium])`, `F[[Sub]]`, etc., or bare `E`.
///
/// Shape patterns are matched **most-specific-first** to handle multi-char
/// delimiters like `(((`, `((`, `{{`, `[[`, `([`, `[(` before single chars.
///
/// Returns `None` if the token is empty or unparseable.
pub(crate) fn parse_node_definition(token: &str) -> Option<Node> {
    let token = token.trim();
    if token.is_empty() {
        return None;
    }

    // Asymmetric `>label]` — starts with `>`, no bracket for ID split.
    if token.starts_with('>') && token.ends_with(']') {
        // Mermaid: `A>label]` where `A` is the id extracted from the caller
        // context. Since we see the whole token here (id prepended), we must
        // find where the `>` starts.
        if let Some(pos) = token.find('>') {
            let id = token[..pos].trim().to_string();
            if !id.is_empty() {
                let inner = token[pos + 1..token.len() - 1].trim().to_string();
                let label = normalize_label(&inner);
                return Some(Node::new(id, label, NodeShape::Asymmetric));
            }
        }
    }

    // Find the first bracket/brace/paren/angle character to split id from shape.
    let shape_start = token.find(['[', '{', '(', '>']);

    let (id, label, shape) = if let Some(pos) = shape_start {
        let id = token[..pos].trim().to_string();
        let rest = &token[pos..];

        // --- Most specific first ---

        // Triple paren: A(((text))) → DoubleCircle
        if rest.starts_with("(((") && rest.ends_with(")))") {
            let inner = rest[3..rest.len() - 3].trim().to_string();
            (id, inner, NodeShape::DoubleCircle)
        }
        // Stadium: A([text])
        else if rest.starts_with("([") && rest.ends_with("])") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Stadium)
        }
        // Cylinder: A[(text)]
        else if rest.starts_with("[(") && rest.ends_with(")]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Cylinder)
        }
        // Subroutine: A[[text]]
        else if rest.starts_with("[[") && rest.ends_with("]]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Subroutine)
        }
        // Parallelogram (lean-right): A[/text/]
        else if rest.starts_with("[/") && rest.ends_with("/]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Parallelogram)
        }
        // Trapezoid (wider top): A[/text\]
        else if rest.starts_with("[/") && rest.ends_with("\\]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Trapezoid)
        }
        // Parallelogram (lean-left / backslash): A[\text\]
        else if rest.starts_with("[\\") && rest.ends_with("\\]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::ParallelogramBackslash)
        }
        // Inverted trapezoid (wider bottom): A[\text/]
        else if rest.starts_with("[\\") && rest.ends_with("/]") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::TrapezoidInverted)
        }
        // Hexagon: A{{text}}
        else if rest.starts_with("{{") && rest.ends_with("}}") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Hexagon)
        }
        // Double paren: A((text)) → Circle
        else if rest.starts_with("((") && rest.ends_with("))") {
            let inner = rest[2..rest.len() - 2].trim().to_string();
            (id, inner, NodeShape::Circle)
        }
        // Diamond: A{text}
        else if rest.starts_with('{') && rest.ends_with('}') {
            let inner = rest[1..rest.len() - 1].trim().to_string();
            (id, inner, NodeShape::Diamond)
        }
        // Rectangle: A[text]
        else if rest.starts_with('[') && rest.ends_with(']') {
            let inner = rest[1..rest.len() - 1].trim().to_string();
            (id, inner, NodeShape::Rectangle)
        }
        // Rounded: A(text)
        else if rest.starts_with('(') && rest.ends_with(')') {
            let inner = rest[1..rest.len() - 1].trim().to_string();
            (id, inner, NodeShape::Rounded)
        }
        // Asymmetric: A>text]
        else if rest.starts_with('>') && rest.ends_with(']') {
            let inner = rest[1..rest.len() - 1].trim().to_string();
            (id, inner, NodeShape::Asymmetric)
        } else {
            // Unrecognised bracket pattern — treat entire token as bare ID.
            let id = token.to_string();
            (id.clone(), id, NodeShape::Rectangle)
        }
    } else {
        // Bare ID
        (token.to_string(), token.to_string(), NodeShape::Rectangle)
    };

    if id.is_empty() {
        return None;
    }

    let label = normalize_label(&label);
    Some(Node::new(id, label, shape))
}

/// Soft-wrap threshold for a single label line. Lines longer than this get
/// wrapped at the nearest comma or space before the threshold, producing
/// additional line breaks. Lines without any break point remain intact so
/// a long identifier (`a_very_long_ident_without_separators`) isn't mangled.
const LABEL_WRAP_THRESHOLD: usize = 40;

/// Normalise a label for multi-row rendering.
///
/// Two transformations are applied, in order:
///
/// 1. HTML line-break tags (`<br/>`, `<br>`, `<br />`, case-insensitive on the
///    tag name) are replaced with `\n`. Mermaid uses these as explicit line
///    breaks inside node labels and we honor them.
/// 2. Any resulting line wider than [`LABEL_WRAP_THRESHOLD`] terminal cells is
///    soft-wrapped at the last comma or space at or before the threshold.
///    Words without any wrap-friendly break stay on a single line.
///
/// The renderer interprets `\n` as a line-break and draws each segment on its
/// own row inside the node box, widening the box vertically instead of
/// horizontally.
fn normalize_label(s: &str) -> String {
    // Step 1: replace HTML <br> variants with `\n`. Lower-case first; the
    // upper-case variants are the only other common spellings on the wild.
    let with_breaks = s
        .replace("<br/>", "\n")
        .replace("<br>", "\n")
        .replace("<br />", "\n")
        .replace("<BR/>", "\n")
        .replace("<BR>", "\n")
        .replace("<BR />", "\n");

    // Step 2: soft-wrap each resulting line.
    let mut out = String::with_capacity(with_breaks.len());
    let mut first = true;
    for line in with_breaks.lines() {
        if !first {
            out.push('\n');
        }
        first = false;
        soft_wrap_into(line, &mut out);
    }
    out
}

/// Append `line` to `out`, inserting `\n` breaks at word boundaries so that
/// no resulting row exceeds [`LABEL_WRAP_THRESHOLD`] columns.
///
/// The break character (comma or space) stays on the head side of the split —
/// a trailing space gets trimmed, a trailing comma is preserved so the user's
/// list formatting is kept.
fn soft_wrap_into(line: &str, out: &mut String) {
    use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

    if UnicodeWidthStr::width(line) <= LABEL_WRAP_THRESHOLD {
        out.push_str(line);
        return;
    }

    // Walk chars, tracking cumulative width and the byte index of the last
    // break-friendly char (comma or space) seen within the budget.
    let mut cum_w = 0usize;
    let mut last_break: Option<usize> = None;
    for (i, ch) in line.char_indices() {
        cum_w += UnicodeWidthChar::width(ch).unwrap_or(0);
        if cum_w > LABEL_WRAP_THRESHOLD {
            break;
        }
        if ch == ',' || ch == ' ' {
            last_break = Some(i);
        }
    }

    let Some(break_at) = last_break else {
        // No break point within the budget — emit the line as-is rather than
        // mangling a single long word.
        out.push_str(line);
        return;
    };

    // `split_at(break_at + 1)`: `break_at` is the byte index of the break
    // character; `+ 1` includes the break char (all break chars are ASCII,
    // so their UTF-8 length is 1) in the head.
    let (head, tail) = line.split_at(break_at + 1);
    let head = head.trim_end();
    let tail = tail.trim_start();
    out.push_str(head);
    out.push('\n');
    soft_wrap_into(tail, out);
}

// ---------------------------------------------------------------------------
// Style directive parsing
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Click directive parsing
// ---------------------------------------------------------------------------

/// Parse a `click` directive and record it on `graph.click_targets`.
///
/// Recognised syntaxes:
///
/// | Source                                     | Result                             |
/// |--------------------------------------------|------------------------------------|
/// | `click NodeId "url"`                       | URL, no tooltip                    |
/// | `click NodeId "url" "tooltip"`             | URL + tooltip                      |
/// | `click NodeId href "url"`                  | URL (alternate `href` keyword)     |
/// | `click NodeId href "url" "tooltip"`        | URL + tooltip                      |
/// | `click NodeId callbackFn`                  | silently ignored (JS callback)     |
/// | `click NodeId callbackFn "tooltip"`        | silently ignored                   |
///
/// The JS-callback forms have no renderable equivalent in a text terminal, so
/// they are silently dropped — matching Mermaid's own silent-fail behaviour
/// when a callback is undefined in the browser.
///
/// # Arguments
///
/// * `stmt`  — the raw statement string (already trimmed, `"click …"`)
/// * `graph` — the [`Graph`] to insert the result into
pub(crate) fn parse_click_directive(stmt: &str, graph: &mut Graph) {
    // Strip the "click " keyword prefix. The statement is guaranteed to start
    // with "click" because the parser dispatched on that first word.
    let rest = stmt.strip_prefix("click").unwrap_or("").trim();
    if rest.is_empty() {
        return;
    }

    // Extract the node ID — it's the first whitespace-delimited token.
    let mut parts = rest.splitn(2, |c: char| c.is_whitespace());
    let node_id = parts.next().unwrap_or("").trim();
    if node_id.is_empty() {
        return;
    }
    let after_id = parts.next().unwrap_or("").trim();

    // Check for the optional `href` keyword (alternate Mermaid syntax).
    // `click NodeId href "url" ["tooltip"]`
    let url_part = if let Some(stripped) = after_id.strip_prefix("href") {
        stripped.trim()
    } else {
        after_id
    };

    // The URL must start with a double-quote. If it doesn't, this is a
    // JS-callback form — silently ignore.
    let Some(url_rest) = url_part.strip_prefix('"') else {
        return;
    };

    // Find the closing double-quote for the URL.
    let Some(url_end) = url_rest.find('"') else {
        return;
    };
    let url = url_rest[..url_end].to_string();

    // Everything after the closing URL quote may contain an optional tooltip.
    let after_url = url_rest[url_end + 1..].trim();
    let tooltip = parse_quoted_string(after_url);

    graph
        .click_targets
        .insert(node_id.to_string(), ClickTarget { url, tooltip });
}

/// Extract a double-quoted string from the start of `s`, if present.
///
/// Returns `Some(inner)` when `s` starts with `"…"`, `None` otherwise.
fn parse_quoted_string(s: &str) -> Option<String> {
    let inner = s.strip_prefix('"')?;
    let end = inner.find('"')?;
    Some(inner[..end].to_string())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{EdgeEndpoint, EdgeStyle, NodeShape, Rgb};

    #[test]
    fn parse_simple_lr() {
        let g = parse("graph LR\nA-->B-->C").unwrap();
        assert_eq!(g.direction, Direction::LeftToRight);
        assert!(g.has_node("A"));
        assert!(g.has_node("B"));
        assert!(g.has_node("C"));
        assert_eq!(g.edges.len(), 2);
    }

    #[test]
    fn parse_semicolons() {
        let g = parse("graph LR; A-->B; B-->C").unwrap();
        assert_eq!(g.edges.len(), 2);
    }

    #[test]
    fn parse_labeled_nodes() {
        let g = parse("graph LR\nA[Start] --> B[End]").unwrap();
        assert_eq!(g.node("A").unwrap().label, "Start");
        assert_eq!(g.node("B").unwrap().label, "End");
    }

    #[test]
    fn parse_diamond_node() {
        let g = parse("graph LR\nA{Decision}").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Diamond);
        assert_eq!(g.node("A").unwrap().label, "Decision");
    }

    #[test]
    fn parse_circle_node() {
        let g = parse("graph LR\nA((Circle))").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Circle);
    }

    #[test]
    fn circle_syntax_does_not_leak_parens_into_label() {
        // Regression test for Bug 1: the `((` / `))` delimiters in Mermaid's
        // circle syntax are structural — they must NOT appear in the rendered
        // label.  Previously the parser extracted "( Circle )" (with the inner
        // paren pair included), causing the box to display as "│( Circle )│".
        let g = parse("graph LR\nB((Circle))").unwrap();
        let node = g.node("B").unwrap();
        assert_eq!(node.shape, NodeShape::Circle);
        // The label must be "Circle", not "( Circle )" or "(Circle)".
        assert_eq!(node.label, "Circle");
        assert!(
            !node.label.contains('('),
            "label must not contain '(': {:?}",
            node.label
        );
        assert!(
            !node.label.contains(')'),
            "label must not contain ')': {:?}",
            node.label
        );
    }

    #[test]
    fn parse_rounded_node() {
        let g = parse("graph LR\nA(Rounded)").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Rounded);
    }

    #[test]
    fn parse_edge_label_pipe() {
        let g = parse("graph LR\nA -->|yes| B").unwrap();
        assert_eq!(g.edges[0].label.as_deref(), Some("yes"));
    }

    #[test]
    fn parse_edge_label_dash() {
        let g = parse("graph LR\nA -- hello --> B").unwrap();
        assert_eq!(g.edges[0].label.as_deref(), Some("hello"));
    }

    #[test]
    fn parse_flowchart_keyword() {
        let g = parse("flowchart TD\nA-->B").unwrap();
        assert_eq!(g.direction, Direction::TopToBottom);
    }

    #[test]
    fn bad_direction_returns_error() {
        assert!(parse("graph XY\nA-->B").is_err());
    }

    #[test]
    fn no_header_returns_error() {
        assert!(parse("A-->B").is_err());
    }

    #[test]
    fn parse_subgraph_basic() {
        let src = "graph LR\nsubgraph Supervisor\nF[Factory] --> W[Worker]\nend";
        let g = parse(src).unwrap();
        assert!(g.has_node("F"), "missing F");
        assert!(g.has_node("W"), "missing W");
        assert_eq!(g.subgraphs.len(), 1);
        assert_eq!(g.subgraphs[0].id, "Supervisor");
        assert_eq!(g.subgraphs[0].label, "Supervisor");
        // Both nodes should be members of the Supervisor subgraph.
        assert!(g.subgraphs[0].node_ids.contains(&"F".to_string()));
        assert!(g.subgraphs[0].node_ids.contains(&"W".to_string()));
    }

    #[test]
    fn parse_subgraph_with_direction() {
        let src = "graph LR\nsubgraph S\ndirection TB\nA-->B\nend";
        let g = parse(src).unwrap();
        assert_eq!(g.subgraphs[0].direction, Some(Direction::TopToBottom));
    }

    #[test]
    fn parse_nested_subgraphs() {
        let src = "graph TD\nsubgraph Outer\nsubgraph Inner\nA[A]\nend\nB[B]\nend";
        let g = parse(src).unwrap();
        // Both subgraphs should be registered.
        assert!(g.find_subgraph("Outer").is_some());
        assert!(g.find_subgraph("Inner").is_some());
        // Inner should be a child of Outer.
        let outer = g.find_subgraph("Outer").unwrap();
        assert!(outer.subgraph_ids.contains(&"Inner".to_string()));
        // A is in Inner, B is in Outer.
        let inner = g.find_subgraph("Inner").unwrap();
        assert!(inner.node_ids.contains(&"A".to_string()));
        assert!(outer.node_ids.contains(&"B".to_string()));
    }

    #[test]
    fn parse_subgraph_edge_crossing_boundary() {
        let src = "graph LR\nsubgraph S\nF[Factory] --> W[Worker]\nend\nW --> HB[Heartbeat]";
        let g = parse(src).unwrap();
        assert!(g.has_node("F"));
        assert!(g.has_node("W"));
        assert!(g.has_node("HB"));
        // W → HB edge should exist (crosses boundary).
        assert!(g.edges.iter().any(|e| e.from == "W" && e.to == "HB"));
        // HB should NOT be in subgraph S.
        let s = g.find_subgraph("S").unwrap();
        assert!(!s.node_ids.contains(&"HB".to_string()));
    }

    #[test]
    fn node_to_subgraph_map() {
        let src = "graph LR\nsubgraph S\nA-->B\nend\nC-->D";
        let g = parse(src).unwrap();
        let map = g.node_to_subgraph();
        assert_eq!(map.get("A").map(String::as_str), Some("S"));
        assert_eq!(map.get("B").map(String::as_str), Some("S"));
        assert!(!map.contains_key("C"));
        assert!(!map.contains_key("D"));
    }

    // ---- New node shape parser tests ------------------------------------

    #[test]
    fn parse_stadium_node() {
        let g = parse("graph LR\nA([Stadium])").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Stadium);
        assert_eq!(g.node("A").unwrap().label, "Stadium");
    }

    #[test]
    fn parse_subroutine_node() {
        let g = parse("graph LR\nA[[Sub]]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Subroutine);
        assert_eq!(g.node("A").unwrap().label, "Sub");
    }

    #[test]
    fn parse_cylinder_node() {
        let g = parse("graph LR\nA[(DB)]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Cylinder);
        assert_eq!(g.node("A").unwrap().label, "DB");
    }

    #[test]
    fn parse_hexagon_node() {
        let g = parse("graph LR\nA{{Hex}}").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Hexagon);
        assert_eq!(g.node("A").unwrap().label, "Hex");
    }

    #[test]
    fn parse_asymmetric_node() {
        let g = parse("graph LR\nA>Flag]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Asymmetric);
        assert_eq!(g.node("A").unwrap().label, "Flag");
    }

    #[test]
    fn parse_parallelogram_node() {
        let g = parse("graph LR\nA[/Lean/]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Parallelogram);
        assert_eq!(g.node("A").unwrap().label, "Lean");
    }

    #[test]
    fn parse_trapezoid_node() {
        let g = parse("graph LR\nA[/Trap\\]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Trapezoid);
        assert_eq!(g.node("A").unwrap().label, "Trap");
    }

    #[test]
    fn parse_parallelogram_backslash_node() {
        let g = parse("graph LR\nA[\\LeanLeft\\]").unwrap();
        assert_eq!(
            g.node("A").unwrap().shape,
            NodeShape::ParallelogramBackslash
        );
        assert_eq!(g.node("A").unwrap().label, "LeanLeft");
    }

    #[test]
    fn parse_trapezoid_inverted_node() {
        let g = parse("graph LR\nA[\\InvTrap/]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::TrapezoidInverted);
        assert_eq!(g.node("A").unwrap().label, "InvTrap");
    }

    #[test]
    fn parse_double_circle_node() {
        let g = parse("graph LR\nA(((Dbl)))").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::DoubleCircle);
        assert_eq!(g.node("A").unwrap().label, "Dbl");
    }

    // Disambiguation: (( before ((( — triple paren wins.
    #[test]
    fn triple_paren_beats_double_paren() {
        let g = parse("graph LR\nA(((X)))").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::DoubleCircle);
    }

    // Disambiguation: [[ before [ — double bracket wins.
    #[test]
    fn double_bracket_beats_single_bracket() {
        let g = parse("graph LR\nA[[Y]]").unwrap();
        assert_eq!(g.node("A").unwrap().shape, NodeShape::Subroutine);
    }

    // ---- Edge style parser tests ----------------------------------------

    #[test]
    fn parse_dotted_edge_style() {
        let g = parse("graph LR\nA-.->B").unwrap();
        assert_eq!(g.edges[0].style, EdgeStyle::Dotted);
        assert_eq!(g.edges[0].end, EdgeEndpoint::Arrow);
    }

    #[test]
    fn parse_thick_edge_style() {
        let g = parse("graph LR\nA==>B").unwrap();
        assert_eq!(g.edges[0].style, EdgeStyle::Thick);
        assert_eq!(g.edges[0].end, EdgeEndpoint::Arrow);
    }

    #[test]
    fn parse_plain_line_no_arrow() {
        let g = parse("graph LR\nA---B").unwrap();
        assert_eq!(g.edges[0].style, EdgeStyle::Solid);
        assert_eq!(g.edges[0].end, EdgeEndpoint::None);
        assert_eq!(g.edges[0].start, EdgeEndpoint::None);
    }

    #[test]
    fn parse_bidirectional_edge() {
        let g = parse("graph LR\nA<-->B").unwrap();
        assert_eq!(g.edges[0].style, EdgeStyle::Solid);
        assert_eq!(g.edges[0].start, EdgeEndpoint::Arrow);
        assert_eq!(g.edges[0].end, EdgeEndpoint::Arrow);
    }

    #[test]
    fn parse_circle_endpoint() {
        let g = parse("graph LR\nA--oB").unwrap();
        assert_eq!(g.edges[0].end, EdgeEndpoint::Circle);
    }

    #[test]
    fn parse_cross_endpoint() {
        let g = parse("graph LR\nA--xB").unwrap();
        assert_eq!(g.edges[0].end, EdgeEndpoint::Cross);
    }

    #[test]
    fn parse_style_directive_records_colors() {
        let src = "graph LR\nA-->B\nstyle A fill:#336,stroke:#fff,color:#fff";
        let g = parse(src).unwrap();
        let style = g.node_styles.get("A").copied().unwrap();
        assert_eq!(style.fill, Some(Rgb(0x33, 0x33, 0x66)));
        assert_eq!(style.stroke, Some(Rgb(0xff, 0xff, 0xff)));
        assert_eq!(style.color, Some(Rgb(0xff, 0xff, 0xff)));
    }

    #[test]
    fn parse_style_directive_ignores_unknown_keys_and_bad_hex() {
        let src = "graph LR\nA\nstyle A fill:#zzz,foo:bar,stroke:#000";
        let g = parse(src).unwrap();
        let style = g.node_styles.get("A").copied().unwrap();
        assert_eq!(style.fill, None);
        assert_eq!(style.stroke, Some(Rgb(0, 0, 0)));
    }

    #[test]
    fn parse_link_style_directive_per_index() {
        let src =
            "graph LR\nA-->B\nA-->C\nlinkStyle 0 stroke:#f00\nlinkStyle 1 stroke:#0f0,color:#fff";
        let g = parse(src).unwrap();
        let e0 = g.edge_styles.get(&0).copied().unwrap();
        assert_eq!(e0.stroke, Some(Rgb(0xff, 0, 0)));
        assert!(e0.color.is_none());
        let e1 = g.edge_styles.get(&1).copied().unwrap();
        assert_eq!(e1.stroke, Some(0).map(|_| Rgb(0, 0xff, 0)));
        assert_eq!(e1.color, Some(Rgb(0xff, 0xff, 0xff)));
    }

    #[test]
    fn parse_link_style_default_applies_to_all() {
        let src = "graph LR\nA-->B\nA-->C\nlinkStyle default stroke:#abc";
        let g = parse(src).unwrap();
        assert_eq!(
            g.edge_styles.get(&0).and_then(|e| e.stroke),
            Some(Rgb(0xaa, 0xbb, 0xcc))
        );
        assert_eq!(
            g.edge_styles.get(&1).and_then(|e| e.stroke),
            Some(Rgb(0xaa, 0xbb, 0xcc))
        );
    }

    // ---- classDef / class / ::: ---------------------------------------

    #[test]
    fn class_def_directive_records_palette() {
        let src = "graph LR\nA-->B\nclassDef cache fill:#234,stroke:#9cf,color:#fff";
        let g = parse(src).unwrap();
        let style = g.class_defs.get("cache").copied().unwrap();
        assert_eq!(style.fill, Some(Rgb(0x22, 0x33, 0x44)));
        assert_eq!(style.stroke, Some(Rgb(0x99, 0xcc, 0xff)));
        assert_eq!(style.color, Some(Rgb(0xff, 0xff, 0xff)));
    }

    #[test]
    fn class_directive_applies_palette_to_each_id() {
        let src = "graph LR\nA-->B-->C\nclassDef cache fill:#234\nclass A,B cache";
        let g = parse(src).unwrap();
        assert_eq!(
            g.node_styles.get("A").and_then(|s| s.fill),
            Some(Rgb(0x22, 0x33, 0x44))
        );
        assert_eq!(
            g.node_styles.get("B").and_then(|s| s.fill),
            Some(Rgb(0x22, 0x33, 0x44))
        );
        assert!(!g.node_styles.contains_key("C"), "C wasn't in `class` list");
    }

    #[test]
    fn triple_colon_shorthand_inline_on_node() {
        let src = "graph LR\nA[Start]:::cache --> B[End]\nclassDef cache fill:#234";
        let g = parse(src).unwrap();
        assert_eq!(
            g.node_styles.get("A").and_then(|s| s.fill),
            Some(Rgb(0x22, 0x33, 0x44))
        );
        // Label and shape parsing unaffected by the modifier suffix.
        assert_eq!(g.node("A").unwrap().label, "Start");
    }

    #[test]
    fn triple_colon_chained_classes_stack() {
        let src = "graph LR\nA:::base:::overlay --> B
classDef base fill:#111,stroke:#222
classDef overlay stroke:#999,color:#fff";
        let g = parse(src).unwrap();
        let s = g.node_styles.get("A").copied().unwrap();
        assert_eq!(s.fill, Some(Rgb(0x11, 0x11, 0x11))); // from base
        // overlay applied later (right of base in source) wins on stroke
        assert_eq!(s.stroke, Some(Rgb(0x99, 0x99, 0x99)));
        assert_eq!(s.color, Some(Rgb(0xff, 0xff, 0xff))); // from overlay
    }

    #[test]
    fn forward_reference_to_class_def_resolves_at_end_of_parse() {
        // `class A foo` appears BEFORE `classDef foo …` — must still
        // resolve.
        let src = "graph LR\nA-->B\nclass A foo\nclassDef foo fill:#abc";
        let g = parse(src).unwrap();
        assert_eq!(
            g.node_styles.get("A").and_then(|s| s.fill),
            Some(Rgb(0xaa, 0xbb, 0xcc))
        );
    }

    #[test]
    fn unknown_class_name_silently_dropped() {
        // Reference to an undefined class — match Mermaid's
        // best-effort semantics: no error, no application.
        let src = "graph LR\nA-->B\nclass A undefined";
        let g = parse(src).unwrap();
        assert!(!g.node_styles.contains_key("A"));
    }

    // ---- Inline-quoted edge label tests (B1/B2 regression) ----------------

    /// B1: dashed edge with inline-quoted label.
    ///
    /// `A -. "label" .-> B` must produce a single dotted edge labelled "label",
    /// not a ghost node containing the literal text `-. "label"`.
    #[test]
    fn inline_quoted_label_dashed_edge() {
        let g = parse("graph LR\nA -. \"my label\" .-> B").unwrap();
        // Must be exactly one edge — not zero (label consumed as node) and not two.
        assert_eq!(g.edges.len(), 1, "expected 1 edge, got {}", g.edges.len());
        let e = &g.edges[0];
        assert_eq!(e.from, "A");
        assert_eq!(e.to, "B");
        assert_eq!(e.style, EdgeStyle::Dotted);
        assert_eq!(e.end, EdgeEndpoint::Arrow);
        assert_eq!(
            e.label.as_deref(),
            Some("my label"),
            "label should be extracted without surrounding quotes"
        );
    }

    /// B2: thick edge with inline-quoted label.
    ///
    /// `A == "label" ==> B` must produce a single thick edge labelled "label".
    #[test]
    fn inline_quoted_label_thick_edge() {
        let g = parse("graph LR\nA == \"my label\" ==> B").unwrap();
        assert_eq!(g.edges.len(), 1, "expected 1 edge, got {}", g.edges.len());
        let e = &g.edges[0];
        assert_eq!(e.from, "A");
        assert_eq!(e.to, "B");
        assert_eq!(e.style, EdgeStyle::Thick);
        assert_eq!(e.end, EdgeEndpoint::Arrow);
        assert_eq!(e.label.as_deref(), Some("my label"));
    }

    /// Solid inline-quoted form (`-- "label" -->`) — this worked before the
    /// fix via `try_consume_labeled_dash_arrow`. Verify it still works and
    /// that quotes are stripped from the label.
    #[test]
    fn inline_quoted_label_solid_edge() {
        let g = parse("graph LR\nA -- \"my label\" --> B").unwrap();
        assert_eq!(g.edges.len(), 1, "expected 1 edge, got {}", g.edges.len());
        let e = &g.edges[0];
        assert_eq!(e.from, "A");
        assert_eq!(e.to, "B");
        assert_eq!(e.style, EdgeStyle::Solid);
        assert_eq!(e.end, EdgeEndpoint::Arrow);
        assert_eq!(e.label.as_deref(), Some("my label"));
    }

    /// Pipe-label forms must continue to work identically after the fix.
    /// Regression guard: B1/B2 fix must not break the pre-existing pipe path.
    #[test]
    fn pipe_label_dashed_and_thick_unaffected() {
        let g = parse("graph LR\nA -.->|pipe dashed| B\nA ==>|pipe thick| C").unwrap();
        assert_eq!(g.edges.len(), 2);
        let dashed = g.edges.iter().find(|e| e.to == "B").unwrap();
        assert_eq!(dashed.style, EdgeStyle::Dotted);
        assert_eq!(dashed.label.as_deref(), Some("pipe dashed"));
        let thick = g.edges.iter().find(|e| e.to == "C").unwrap();
        assert_eq!(thick.style, EdgeStyle::Thick);
        assert_eq!(thick.label.as_deref(), Some("pipe thick"));
    }

    /// Inline-quoted labels with spaces in the label text.
    #[test]
    fn inline_quoted_label_with_spaces() {
        let g = parse("graph LR\nA -. \"sends event to\" .-> B").unwrap();
        assert_eq!(g.edges.len(), 1);
        assert_eq!(g.edges[0].label.as_deref(), Some("sends event to"));
    }

    /// Inline-quoted label in a chain: `A -. "x" .-> B ==> C`.
    /// Both edges must parse correctly within the same chain statement.
    #[test]
    fn inline_quoted_label_in_chain() {
        let g = parse("graph LR\nA -. \"step one\" .-> B ==> C").unwrap();
        assert_eq!(g.edges.len(), 2, "expected 2 edges in chain");
        let ab = g
            .edges
            .iter()
            .find(|e| e.from == "A" && e.to == "B")
            .unwrap();
        assert_eq!(ab.style, EdgeStyle::Dotted);
        assert_eq!(ab.label.as_deref(), Some("step one"));
        let bc = g
            .edges
            .iter()
            .find(|e| e.from == "B" && e.to == "C")
            .unwrap();
        assert_eq!(bc.style, EdgeStyle::Thick);
        assert!(bc.label.is_none());
    }

    #[test]
    fn style_directive_overrides_class_for_same_id() {
        // Per-id `style` lands AFTER class application (style is
        // processed inline; class is resolved at end-of-parse — but
        // the resolver merges class as overlay over base, so the
        // earlier `style` survives unless the class also sets that
        // attribute).
        let src = "graph LR
A-->B
style A fill:#aaa
classDef cls fill:#bbb,stroke:#ccc
class A cls";
        let g = parse(src).unwrap();
        let s = g.node_styles.get("A").copied().unwrap();
        // class fill overlays the style fill (overlay wins per merge_node_style).
        assert_eq!(s.fill, Some(Rgb(0xbb, 0xbb, 0xbb)));
        // class also supplies stroke that wasn't in the style — stacks.
        assert_eq!(s.stroke, Some(Rgb(0xcc, 0xcc, 0xcc)));
    }

    // ---- click directive parser tests ----------------------------------------

    #[test]
    fn click_basic_url() {
        let src = "graph LR\nA-->B\nclick A \"https://example.com\"";
        let g = parse(src).unwrap();
        let ct = g.click_targets.get("A").expect("click target for A");
        assert_eq!(ct.url, "https://example.com");
        assert!(ct.tooltip.is_none());
        // B has no click directive.
        assert!(!g.click_targets.contains_key("B"));
    }

    #[test]
    fn click_with_tooltip() {
        let src = "graph LR\nA-->B\nclick A \"https://example.com\" \"Go to example\"";
        let g = parse(src).unwrap();
        let ct = g.click_targets.get("A").expect("click target for A");
        assert_eq!(ct.url, "https://example.com");
        assert_eq!(ct.tooltip.as_deref(), Some("Go to example"));
    }

    #[test]
    fn click_href_keyword_form() {
        // `click NodeId href "url"` is an alternate Mermaid syntax.
        let src = "graph LR\nA-->B\nclick A href \"https://example.com\"";
        let g = parse(src).unwrap();
        let ct = g.click_targets.get("A").expect("click target for A");
        assert_eq!(ct.url, "https://example.com");
        assert!(ct.tooltip.is_none());
    }

    #[test]
    fn click_href_keyword_with_tooltip() {
        let src = "graph LR\nA-->B\nclick A href \"https://example.com\" \"tooltip text\"";
        let g = parse(src).unwrap();
        let ct = g.click_targets.get("A").expect("click target");
        assert_eq!(ct.url, "https://example.com");
        assert_eq!(ct.tooltip.as_deref(), Some("tooltip text"));
    }

    #[test]
    fn click_js_callback_silently_ignored() {
        // JS callback forms have no URL quote — should produce zero click targets.
        let src = "graph LR\nA-->B\nclick A myCallback\nclick B myCallback \"tooltip\"";
        let g = parse(src).unwrap();
        assert!(
            g.click_targets.is_empty(),
            "JS callbacks must be silently ignored"
        );
    }

    #[test]
    fn click_does_not_affect_nodes_without_directive() {
        let src = "graph LR\nA-->B-->C\nclick B \"https://b.example\"";
        let g = parse(src).unwrap();
        assert!(!g.click_targets.contains_key("A"));
        assert!(g.click_targets.contains_key("B"));
        assert!(!g.click_targets.contains_key("C"));
    }

    #[test]
    fn click_multiple_nodes() {
        let src = "graph LR\nA-->B\nclick A \"https://a.example\"\nclick B \"https://b.example\"";
        let g = parse(src).unwrap();
        assert_eq!(
            g.click_targets.get("A").map(|c| c.url.as_str()),
            Some("https://a.example")
        );
        assert_eq!(
            g.click_targets.get("B").map(|c| c.url.as_str()),
            Some("https://b.example")
        );
    }
}