battler 0.9.0

Pokémon battle engine for 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
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
use alloc::{
    string::{
        String,
        ToString,
    },
    vec::Vec,
};

use anyhow::Error;
use hashbrown::HashMap;
use serde::{
    Deserialize,
    Serialize,
};
use serde_string_enum::{
    DeserializeLabeledStringEnum,
    SerializeLabeledStringEnum,
};

use crate::{
    WrapResultError,
    battle::SpeedOrderable,
    effect::fxlang::{
        LocalData,
        ValueType,
    },
};

/// Flags used to indicate the input and output of a [`Callback`].
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod CallbackFlag {
    pub const TakesGeneralMon: u32 = 1 << 1;
    pub const TakesTargetMon: u32 = 1 << 2;
    pub const TakesSourceMon: u32 = 1 << 3;
    pub const TakesEffect: u32 = 1 << 4;
    pub const TakesActiveMove: u32 = 1 << 5;
    pub const TakesUserMon: u32 = 1 << 6;
    pub const TakesSourceTargetMon: u32 = 1 << 7;
    pub const TakesSourceEffect: u32 = 1 << 8;
    pub const TakesSide: u32 = 1 << 9;
    pub const TakesOptionalEffect: u32 = 1 << 10;
    pub const TakesPlayer: u32 = 1 << 11;

    pub const ReturnsActiveMove: u32 = 1 << 18;
    pub const ReturnsType: u32 = 1 << 19;
    pub const ReturnsStatTable: u32 = 1 << 20;
    pub const ReturnsMoveTarget: u32 = 1 << 21;
    pub const ReturnsStrings: u32 = 1 << 22;
    pub const ReturnsSecondaryEffects: u32 = 1 << 23;
    pub const ReturnsTypes: u32 = 1 << 24;
    pub const ReturnsMon: u32 = 1 << 25;
    pub const ReturnsBoosts: u32 = 1 << 26;
    pub const ReturnsString: u32 = 1 << 27;
    pub const ReturnsEventResult: u32 = 1 << 28;
    pub const ReturnsNumber: u32 = 1 << 29;
    pub const ReturnsBoolean: u32 = 1 << 30;
    pub const ReturnsVoid: u32 = 1 << 31;
}

/// Common types of [`Callback`]s, defined for convenience.
///
/// - `ApplyingEffect` - An effect being applied to a target Mon, potentially from a source Mon. The
///   focus is on the applying effect itself.
/// - `Effect` - Same as `ApplyingEffect`, but the applying effect is considered to be the "source
///   effect."
/// - `SourceMove` - An active move being used by a Mon, potentially with a target.
/// - `Move` - An active move being used by a Mon against a target.
/// - `Mon` - A callback on the Mon itself, with no associated effect.
/// - `Side` - A callback on the side itself, with no associated effect, potentially with a source
///   Mon.
/// - `MoveSide` - An active move being used by a Mon against a side.
/// - `MoveField` - An active move being used by a Mon against the field.
#[repr(u32)]
enum CommonCallbackType {
    ApplyingEffectModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,
    ApplyingEffectBoolean = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsVoid,
    ApplyingEffectResult = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    ApplyingEffectVoid = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsVoid,
    ApplyingEffectBoostModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsBoosts
        | CallbackFlag::ReturnsVoid,

    MaybeApplyingEffectVoid = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesOptionalEffect
        | CallbackFlag::ReturnsVoid,
    MaybeApplyingEffectModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesOptionalEffect
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,
    MaybeApplyingEffectBoostModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesOptionalEffect
        | CallbackFlag::ReturnsBoosts
        | CallbackFlag::ReturnsVoid,

    EffectBoolean = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsVoid,
    EffectResult = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    EffectVoid = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsVoid,

    NoContextBoolean = CallbackFlag::ReturnsBoolean | CallbackFlag::ReturnsVoid,
    NoContextVoid = CallbackFlag::ReturnsVoid,

    SourceMoveModifier = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,
    SourceMoveResult = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    SourceMoveVoid = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsVoid,
    SourceMoveMonModifier = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsMon
        | CallbackFlag::ReturnsVoid,
    SourceMoveActiveMove = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsActiveMove
        | CallbackFlag::ReturnsVoid,

    SourceEffectType = CallbackFlag::TakesUserMon
        | CallbackFlag::TakesSourceTargetMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsType
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,

    MoveModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,
    MoveBoolean = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsVoid,
    MoveVoid = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsVoid,
    MoveHitOutcomeResult = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    MoveResult = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    MoveSecondaryEffectModifier = CallbackFlag::TakesTargetMon
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsSecondaryEffects
        | CallbackFlag::ReturnsVoid,

    MonModifier =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsNumber | CallbackFlag::ReturnsVoid,
    MonBoolean =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsBoolean | CallbackFlag::ReturnsVoid,
    MonResult = CallbackFlag::TakesGeneralMon
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    MonVoid = CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsVoid,
    MonInfo =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsString | CallbackFlag::ReturnsVoid,
    MonType = CallbackFlag::TakesGeneralMon
        | CallbackFlag::ReturnsType
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    MonTypes =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsTypes | CallbackFlag::ReturnsVoid,
    MonBoostModifier =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsBoosts | CallbackFlag::ReturnsVoid,
    MonValidator = CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsStrings,
    MonMoveTarget =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsMoveTarget | CallbackFlag::ReturnsVoid,
    MonStatTableModifier =
        CallbackFlag::TakesGeneralMon | CallbackFlag::ReturnsStatTable | CallbackFlag::ReturnsVoid,

    PlayerValidator = CallbackFlag::TakesPlayer | CallbackFlag::ReturnsStrings,

    PlayerEffectVoid = CallbackFlag::TakesPlayer
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsVoid,

    SideVoid = CallbackFlag::TakesSide
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsVoid,
    SideResult = CallbackFlag::TakesSide
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,

    SideEffectVoid = CallbackFlag::TakesSide
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsVoid,
    SideEffectModifier = CallbackFlag::TakesSide
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,

    MoveSideResult = CallbackFlag::TakesSide
        | CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,

    MoveFieldResult = CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesActiveMove
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,

    FieldVoid =
        CallbackFlag::TakesSourceMon | CallbackFlag::TakesSourceEffect | CallbackFlag::ReturnsVoid,
    FieldResult = CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesSourceEffect
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,

    FieldEffectResult = CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsEventResult
        | CallbackFlag::ReturnsBoolean
        | CallbackFlag::ReturnsString
        | CallbackFlag::ReturnsVoid,
    FieldEffectVoid =
        CallbackFlag::TakesSourceMon | CallbackFlag::TakesEffect | CallbackFlag::ReturnsVoid,
    FieldEffectModifier = CallbackFlag::TakesSourceMon
        | CallbackFlag::TakesEffect
        | CallbackFlag::ReturnsNumber
        | CallbackFlag::ReturnsVoid,
}

/// A modifier on a [`BattleEvent`].
#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    SerializeLabeledStringEnum,
    DeserializeLabeledStringEnum,
)]
pub enum BattleEventModifier {
    /// The default event.
    #[default]
    #[string = ""]
    None,
    /// Runs for an ally of the target Mon.
    #[string = "ally"]
    Ally,
    /// Runs for any Mon.
    #[string = "any"]
    Any,
    /// Runs on the field.
    #[string = "field"]
    Field,
    /// Runs for a foe of the target Mon.
    #[string = "foe"]
    Foe,
    /// Runs for the side of the target Mon.
    #[string = "side"]
    Side,
    /// Runs for the source of the effect.
    #[string = "source"]
    Source,
}

/// A battle event that can trigger a [`Callback`].
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    SerializeLabeledStringEnum,
    DeserializeLabeledStringEnum,
)]
pub enum BattleEvent {
    /// Runs when the accuracy check of a move against a target fails.
    ///
    /// Runs in the context of a move target.
    #[string = "AccuracyCheckFailed"]
    AccuracyCheckFailed,
    /// Runs when the accuracy of a move against a target is being determined.
    ///
    /// Runs in the context of a move target.
    #[string = "AccuracyExempt"]
    AccuracyExempt,
    /// Runs when an effect activates.
    ///
    /// Runs when activated by a battle effect. Used for shared logic between multiple event
    /// callbacks.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Activate"]
    Activate,
    /// Runs when an effect activates.
    ///
    /// Runs when activated by a battle effect. Used for shared logic between multiple event
    /// callbacks.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "ActivateField"]
    ActivateField,
    /// Runs when an effect activates.
    ///
    /// Runs when activated by a battle effect. Used for shared logic between multiple event
    /// callbacks.
    ///
    /// Runs in the context of an applying effect on a player.
    #[string = "ActivatePlayer"]
    ActivatePlayer,
    /// Runs when an effect activates.
    ///
    /// Runs when activated by a battle effect. Used for shared logic between multiple event
    /// callbacks.
    ///
    /// Runs in the context of an applying effect on a side.
    #[string = "ActivateSide"]
    ActivateSide,
    /// Runs when a pseudo-weather is being added to the field.
    ///
    /// Runs before the pseudo-weather effect is applied. Can be used to fail the pseudo-weather.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "AddPseudoWeather"]
    AddPseudoWeather,
    /// Runs when a type is being added to a Mon.
    ///
    /// Runs before the type addition is applied. Can be used to fail the type addition.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AddType"]
    AddType,
    /// Runs after a volatile effect is added to a Mon.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AddVolatile"]
    AddVolatile,
    /// Runs after a primary battle action finishes.
    ///
    /// Runs in the context of a Mon.
    #[string = "AfterAction"]
    AfterAction,
    /// Runs after a new pseudo-weather is added to the field.
    ///
    /// Only runs if the pseudo-weather has been added successfully. This event will not undo the
    /// pseudo-weather.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "AfterAddPseudoWeather"]
    AfterAddPseudoWeather,
    /// Runs after a Mon receives a new volatile effect.
    ///
    /// Only runs if the volatile has been added successfully. This event will not undo the
    /// volatile.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterAddVolatile"]
    AfterAddVolatile,
    /// Runs after stat boosts are applied.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterBoost"]
    AfterBoost,
    /// Runs after a Mon's current status is cured.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterCureStatus"]
    AfterCureStatus,
    /// Runs after a Mon takes damage.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterDamage"]
    AfterDamage,
    /// Runs after an individual stat boost is applied.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterEachBoost"]
    AfterEachBoost,
    /// Runs after a Mon causes one or more Mons to faint.
    ///
    /// Runs in the context of a Mon.
    #[string = "AfterFainted"]
    AfterFainted,
    /// Runs after a Mon heals.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterHeal"]
    AfterHeal,
    /// Runs after a Mon hits another Mon with a move.
    ///
    /// Runs on the active move.
    #[string = "AfterHit"]
    AfterHit,
    /// Runs after a Mon Mega Evolves.
    ///
    /// Runs in the context of a Mon.
    #[string = "AfterMegaEvolution"]
    AfterMegaEvolution,
    /// Runs after a Mon finishes using a move.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "AfterMove"]
    AfterMove,
    /// Runs after a move's secondary effects have been applied, for all targets the move was
    /// successful against.
    ///
    /// Should be viewed as the last effect the move needs to apply on the target.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "AfterMoveSecondaryEffects"]
    AfterMoveSecondaryEffects,
    /// Runs after a move's secondary effects have been applied, for all targets affected by damage.
    ///
    /// Should be viewed as the last effect the move needs to apply on the target. Minimal
    /// difference with `AfterMove`; the key difference is that Sheer Force prevents this event from
    /// running.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "AfterMoveSecondaryEffectsDamage"]
    AfterMoveSecondaryEffectsDamage,
    /// Runs after a move's secondary effects have been applied.
    ///
    /// Should be viewed as the last effect the move needs to apply on the user. Minimal difference
    /// with `AfterMove`; the key difference is that Sheer Force prevents this event from running.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "AfterMoveSecondaryEffectsUser"]
    AfterMoveSecondaryEffectsUser,
    /// Runs after a Mon has its ability set.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterSetAbility"]
    AfterSetAbility,
    /// Runs after a Mon has its item set.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterSetItem"]
    AfterSetItem,
    /// Runs after a Mon's status effect is changed.
    ///
    /// Only runs if the status has been set successfully. This event will not undo a status
    /// change.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterSetStatus"]
    AfterSetStatus,
    /// Runs after damage is applied to a substitute.
    ///
    /// Hitting a substitute does not trigger ordinary effects that run when a target is hit. Thus,
    /// this event is used to cover for scenarios where hitting a substitute should still trigger
    /// some callback.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "AfterSubstituteDamage"]
    AfterSubstituteDamage,
    /// Runs after a Mon switches out.
    ///
    /// Runs in the context of a Mon.
    #[string = "AfterSwitchOut"]
    AfterSwitchOut,
    /// Runs after a Mon has its item taken.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterTakeItem"]
    AfterTakeItem,
    /// Runs after a Mon Terastallizes.
    ///
    /// Runs in the context of a Mon.
    #[string = "AfterTerastallization"]
    AfterTerastallization,
    /// Runs after a Mon uses its item.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "AfterUseItem"]
    AfterUseItem,
    /// Runs when a move's base power is being calculated for a target.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "BasePower"]
    BasePower,
    /// Runs before a turn of a battle ends.
    ///
    /// Runs in the context of the battle.
    #[string = "BattleEndTurn"]
    BattleEndTurn,
    /// Runs when a Mon is using a charge move, on the charging turn.
    ///
    /// Runs in the context of a move user.
    #[string = "BeforeChargeMove"]
    BeforeChargeMove,
    /// Runs before a Mon Dynamaxes.
    ///
    /// Runs in the context of a Mon.
    #[string = "BeforeDynamax"]
    BeforeDynamax,
    /// Runs before a Mon uses a move.
    ///
    /// Can prevent the move from being used.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "BeforeMove"]
    BeforeMove,
    /// Runs before an effect starts.
    ///
    /// Used to set up state prior to the Start event.
    ///
    /// Runs on the effect.
    #[string = "BeforeStart"]
    BeforeStart,
    /// Runs when a Mon switches in, prior to `SwitchIn`.
    ///
    /// Not really prior to switching in.
    ///
    /// Runs in the context of a Mon.
    #[string = "BeforeSwitchIn"]
    BeforeSwitchIn,
    /// Runs before a Mon switches out.
    ///
    /// Runs in the context of a Mon.
    #[string = "BeforeSwitchOut"]
    BeforeSwitchOut,
    /// Runs before a Mon Terastallizes.
    ///
    /// Runs in the context of a Mon.
    #[string = "BeforeTerastallization"]
    BeforeTerastallization,
    /// Runs before a turn of a battle.
    ///
    /// Runs on the move and in the context of a move user.
    #[string = "BeforeTurn"]
    BeforeTurn,
    /// Runs when determining the health at which the Mon should eat berries.
    ///
    /// Runs in the context of a Mon.
    #[string = "BerryEatingHealth"]
    BerryEatingHealth,
    /// Runs when determining if a Mon can Dynamax.
    ///
    /// Runs in the context of a Mon.
    #[string = "CanDynamax"]
    CanDynamax,
    /// Runs when a Mon is attempting to escape from battle.
    ///
    /// Runs in the context of a Mon.
    #[string = "CanEscape"]
    CanEscape,
    /// Runs when determining if a Mon can heal.
    ///
    /// Runs in the context of a Mon.
    #[string = "CanHeal"]
    CanHeal,
    /// Runs when a Mon is caught.
    ///
    /// Runs on the item (used to catch the Mon) and in the context of a Mon.
    #[string = "Catch"]
    Catch,
    /// Runs when a Mon fails to be caught.
    ///
    /// Runs on the item (used to catch the Mon) and in the context of a Mon.
    #[string = "CatchFailed"]
    CatchFailed,
    /// Runs when a group of stat boosts is being applied to a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "ChangeBoosts"]
    ChangeBoosts,
    /// Runs when a Mon's stat is being calculated.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "CalculateStat"]
    CalculateStat,
    /// Runs when a Mon is using a charge move, on the charging turn.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "ChargeMove"]
    ChargeMove,
    /// Runs when the field's terrain is being cleared.
    ///
    /// Runs before the terrain effect is cleared. Can be used to fail the clear.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "ClearTerrain"]
    ClearTerrain,
    /// Runs when the field's weather is being cleared.
    ///
    /// Runs before the weather effect is cleared. Can be used to fail the clear.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "ClearWeather"]
    ClearWeather,
    /// Runs when copying a volatile effect to the target Mon.
    ///
    /// Runs on the effect.
    #[string = "CopyVolatile"]
    CopyVolatile,
    /// Runs when a move critical hits a target.
    ///
    /// Runs in the context of a move target.
    #[string = "CriticalHit"]
    CriticalHit,
    /// Runs when a Mon's current status is cured.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "CureStatus"]
    CureStatus,
    /// Runs when a Mon's damage is being calculated for a target.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Damage"]
    Damage,
    /// Runs after a Mon hits another Mon with a move, causing a nonzero amount of damage.
    ///
    /// Runs once per hit (i.e., multi-hit moves execute one event per hit).
    ///
    /// Runs in the context of a move target.
    #[string = "DamagingHit"]
    DamagingHit,
    /// Runs after a move is used that should have PP deducted.
    ///
    /// Runs in the context of a move user.
    #[string = "DeductPp"]
    DeductPp,
    /// Runs when determining which moves are disabled.
    ///
    /// Runs in the context of a Mon and on the move.
    #[string = "DisableMove"]
    DisableMove,
    /// Runs before a Mon is dragged out of battle.
    ///
    /// Can cancel the force switch.
    ///
    /// Runs in the context of a Mon.
    #[string = "DragOut"]
    DragOut,
    /// Runs when determining the duration of an effect.
    ///
    /// Runs on the effect.
    #[string = "Duration"]
    Duration,
    /// Runs when an item is eaten.
    ///
    /// Runs on the item.
    #[string = "Eat"]
    Eat,
    /// Runs when a Mon eats its item.
    ///
    /// Runs in the context of a Mon.
    #[string = "EatItem"]
    EatItem,
    /// Runs when determining the type effectiveness of a move.
    ///
    /// Runs on the effect and in the context of an applying effect on a Mon.
    #[string = "Effectiveness"]
    Effectiveness,
    /// Runs when an effect ends.
    ///
    /// Runs on the effect.
    #[string = "End"]
    End,
    /// Runs when a Mon is active when the battle has ended.
    ///
    /// Runs in the context of a Mon.
    #[string = "EndBattle"]
    EndBattle,
    /// Runs before a turn of a battle ends.
    ///
    /// Runs in the context of a Mon.
    #[string = "EndTurn"]
    EndTurn,
    /// Runs when a Mon exits the battle (is no longer active).
    ///
    /// Runs in the context of a Mon.
    #[string = "Exit"]
    Exit,
    /// Runs when a Mon faints.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Faint"]
    Faint,
    /// Runs when a field condition ends.
    ///
    /// Runs on the field condition.
    #[string = "FieldEnd"]
    FieldEnd,
    /// Runs at the end of every turn to apply residual effects on the field.
    ///
    /// Runs on the field condition.
    #[string = "FieldResidual"]
    FieldResidual,
    /// Runs when a field condition restarts.
    ///
    /// Runs on the field condition.
    #[string = "FieldRestart"]
    FieldRestart,
    /// Runs when a field condition starts.
    ///
    /// Runs on the field condition.
    #[string = "FieldStart"]
    FieldStart,
    /// Runs when a Mon flinches.
    ///
    /// Runs in the context of the target Mon.
    #[string = "Flinch"]
    Flinch,
    /// Runs when determining the type effectiveness of an effect, to prevent normal type
    /// effectiveness from being used.
    ///
    /// Runs on the effect and in the context of an applying effect on a Mon.
    #[string = "ForceEffectiveness"]
    ForceEffectiveness,
    /// Runs when a Mon is attempting to escape from battle, prior to any speed check.
    ///
    /// Runs in the context of a Mon.
    #[string = "ForceEscape"]
    ForceEscape,
    /// Runs when determining if a Mon can terastallize.
    ///
    /// Runs in the context of a Mon.
    #[string = "ForceTeraType"]
    ForceTeraType,
    /// Runs when determining the types of a Mon, to force types early.
    ///
    /// Runs in the context of a Mon.
    #[string = "ForceTypes"]
    ForceTypes,
    /// Runs when a Mon is hit by a move.
    ///
    /// Can fail, but will only fail the move if everything else failed. Can be viewed as part of
    /// the applying hit effect.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "Hit"]
    Hit,
    /// Runs when the field is hit by a move.
    ///
    /// Can fail, but will only fail the move if everything else failed. Can be viewed as part of
    /// the applying hit effect.
    ///
    /// Runs on the active move.
    #[string = "HitField"]
    HitField,
    /// Runs when a side is hit by a move.
    ///
    /// Can fail, but will only fail the move if everything else failed. Can be viewed as part of
    /// the applying hit effect.
    ///
    /// Runs on the active move and in the context of an applying effect on a side.
    #[string = "HitSide"]
    HitSide,
    /// Runs when a Mon uses a move that defines a user hit effect.
    ///
    /// Can fail, but will only fail the move if everything else failed. Can be viewed as part of
    /// the applying hit effect.
    ///
    /// Runs on the active move.
    #[string = "HitUser"]
    HitUser,
    /// Runs when determining if a move should ignore type immunity.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "IgnoreImmunity"]
    IgnoreImmunity,
    /// Runs when determining if a Mon is immune to some status.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Immunity"]
    Immunity,
    /// Runs when determining if a Mon is invulnerable to targeting moves.
    ///
    /// Runs as the very first step in a move.
    ///
    /// Runs in the context of a move target.
    #[string = "Invulnerability"]
    Invulnerability,
    /// Runs when determining if a Mon is asleep.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsAsleep"]
    IsAsleep,
    /// Runs when determining if a Mon is away from the field (e.g., immobilized by Sky Drop).
    ///
    /// Runs in the context of a Mon.
    #[string = "IsAwayFromField"]
    IsAwayFromField,
    /// Runs when determining if a Mon is behind a substitute.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsBehindSubstitute"]
    IsBehindSubstitute,
    /// Runs when determining if a Mon is locked into its previous choice.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsChoiceLocked"]
    IsChoiceLocked,
    /// Runs when determining if a Mon is protected from making contact with other Mons.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsContactProof"]
    IsContactProof,
    /// Runs when determining if a Mon is grounded.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsGrounded"]
    IsGrounded,
    /// Runs when determining if a Mon is immune to entry hazards.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsImmuneToEntryHazards"]
    IsImmuneToEntryHazards,
    /// Runs when determining if a weather includes raining.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsRaining"]
    IsRaining,
    /// Runs when determining if a Mon is in a semi-invulnerable state.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsSemiInvulnerable"]
    IsSemiInvulnerable,
    /// Runs when determining if a weather includes snowing.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsSnowing"]
    IsSnowing,
    /// Runs when determining if a Mon is soundproof.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsSoundproof"]
    IsSoundproof,
    /// Runs when determining if a weather includes sunny weather.
    ///
    /// Runs in the context of a Mon.
    #[string = "IsSunny"]
    IsSunny,
    /// Runs when determining if a Mon is locked into a move.
    ///
    /// Runs in the context of a Mon.
    #[string = "LockMove"]
    LockMove,
    /// Runs when calculating the accuracy of a move.
    ///
    /// Runs in the context of a move target.
    #[string = "ModifyAccuracy"]
    ModifyAccuracy,
    /// Runs when calculating the speed of an action.
    ///
    /// Runs in the context of a Mon.
    #[string = "ModifyActionSpeed"]
    ModifyActionSpeed,
    /// Runs when calculating a Mon's Atk stat.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifyAtk"]
    ModifyAtk,
    /// Runs when modifying a Mon's stat boosts used for stat calculations.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifyBoosts"]
    ModifyBoosts,
    /// Runs when calculating the modified catch rate of a Mon.
    ///
    /// Runs in the context of the item and in the context of an applying effect on a Mon.
    #[string = "ModifyCatchRate"]
    ModifyCatchRate,
    /// Runs when calculating a move's critical hit chance.
    ///
    /// Runs in the context of a move user.
    #[string = "ModifyCritChance"]
    ModifyCritChance,
    /// Runs when calculating a move's critical hit ratio.
    ///
    /// Runs in the context of a move user.
    #[string = "ModifyCritRatio"]
    ModifyCritRatio,
    /// Runs when calculating the damage applied to a Mon.
    ///
    /// Runs as the very last step in the regular damage calculation formula.
    ///
    /// Runs in the context of a move user.
    #[string = "ModifyDamage"]
    ModifyDamage,
    /// Runs when calculating a Mon's Def stat.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifyDef"]
    ModifyDef,
    /// Runs when calculating the duration of a condition applying to a Mon.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifyDuration"]
    ModifyDuration,
    /// Runs when determining the type effectiveness of a move.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifyEffectiveness"]
    ModifyEffectiveness,
    /// Runs when calculating the EV yield gained by a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "ModifyEvYield"]
    ModifyEvYield,
    /// Runs when calculating the amount of experience gained by a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "ModifyExperience"]
    ModifyExperience,
    /// Runs when calculating the duration of a condition applying to the field.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "ModifyFieldDuration"]
    ModifyFieldDuration,
    /// Runs when calculating the amount of friendship gained by a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "ModifyFriendshipIncrease"]
    ModifyFriendshipIncrease,
    /// Runs when modifying the type of a move.
    ///
    /// Runs on the move and in the context of a move user.
    #[string = "ModifyMoveType"]
    ModifyMoveType,
    /// Runs when determining the priority of a move.
    ///
    /// Runs in the context of a move user.
    #[string = "ModifyPriority"]
    ModifyPriority,
    /// Runs before applying secondary move effects.
    ///
    /// Runs in the context of a move target.
    #[string = "ModifySecondaryEffects"]
    ModifySecondaryEffects,
    /// Runs when calculating the duration of a condition applying to a side.
    ///
    /// Runs in the context of an applying effect on a side.
    #[string = "ModifySideDuration"]
    ModifySideDuration,
    /// Runs when calculating the duration of a condition applying to a slot.
    ///
    /// Runs in the context of an applying effect on a side.
    #[string = "ModifySlotDuration"]
    ModifySlotDuration,
    /// Runs when calculating a Mon's SpA stat.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifySpA"]
    ModifySpA,
    /// Runs when calculating a Mon's SpD stat.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifySpD"]
    ModifySpD,
    /// Runs when calculating a Mon's Spe stat.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "ModifySpe"]
    ModifySpe,
    /// Runs when calculating the base species catch rate of a Mon.
    ///
    /// Runs in the context of the item and in the context of an applying effect on a Mon.
    #[string = "ModifySpeciesCatchRate"]
    ModifySpeciesCatchRate,
    /// Runs when calculating a move's STAB multiplier.
    ///
    /// Runs in the context of a move user.
    #[string = "ModifyStab"]
    ModifyStab,
    /// Runs before a move is used, to modify the target Mon.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "ModifyTarget"]
    ModifyTarget,
    /// Runs when calculating a Mon's weight.
    ///
    /// Runs in the context of a Mon.
    #[string = "ModifyWeight"]
    ModifyWeight,
    /// Runs when a move is aborted due to failing the BeforeMove event.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "MoveAborted"]
    MoveAborted,
    /// Runs when a move's base power is being calculated for a target.
    ///
    /// Runs on the active move.
    #[string = "MoveBasePower"]
    MoveBasePower,
    /// Runs when a move's damage is being calculated for a target.
    ///
    /// Runs on the active move.
    #[string = "MoveDamage"]
    MoveDamage,
    /// Runs when a move fails, only on the move itself.
    ///
    /// A move fails when it is successfully used by the user, but it does not hit or apply its
    /// primary effect to any targets.
    ///
    /// Runs on the active move.
    #[string = "MoveFailed"]
    MoveFailed,
    /// Runs when a move's target type is determined for a Mon selecting a move.
    ///
    /// Runs on the move.
    #[string = "MoveTargetOverride"]
    MoveTargetOverride,
    /// Runs when determining if a Mon's immunity against a single type should be negated.
    ///
    /// Runs in the context of a Mon.
    #[string = "NegateImmunity"]
    NegateImmunity,
    /// Runs when a Mon uses a move, to override the chosen move.
    ///
    /// Runs in the context of a Mon.
    #[string = "OverrideMove"]
    OverrideMove,
    /// Runs when determining if a move should be overwritten.
    ///
    /// Runs in the context of a Mon.
    #[string = "OverwriteMove"]
    OverwriteMove,
    /// Runs when determining the effective weather for a Mon. Overrides the weather without looking
    /// at the actual field weather or weather suppression effects.
    ///
    /// Runs in the context of a Mon.
    #[string = "OverrideWeather"]
    OverrideWeather,
    /// Runs when a player tries to choose to use an item.
    ///
    /// Runs on the item.
    #[string = "PlayerTryUseItem"]
    PlayerTryUseItem,
    /// Runs when an item is used on a Mon by a player.
    ///
    /// Runs on the item.
    #[string = "PlayerUse"]
    PlayerUse,
    /// Runs when applying any pre-move effect.
    ///
    /// Very similar to `UseMove`, except it runs after the move is announced.
    ///
    /// Runs in the context of a move user.
    #[string = "PreMoveEffect"]
    PreMoveEffect,
    /// Runs when a Mon is preparing to hit all of its targets with a move.
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "PrepareHit"]
    PrepareHit,
    /// Runs when determining if a Mon can have items used on it.
    ///
    /// Runs in the context of a Mon.
    #[string = "PreventUsedItems"]
    PreventUsedItems,
    /// Runs before at the start of the turn, when a move is charging for the turn.
    ///
    /// Runs in the context of a move user.
    #[string = "PriorityChargeMove"]
    PriorityChargeMove,
    /// Runs when a move is going to target one Mon but can be redirected towards a different
    /// target.
    ///
    /// Runs in the context of a move user.
    #[string = "RedirectTarget"]
    RedirectTarget,
    /// Runs at the end of every turn to apply residual effects.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Residual"]
    Residual,
    /// Runs when a volatile effect is applied to a Mon that already has the volatile effect.
    ///
    /// Runs on the effect.
    #[string = "Restart"]
    Restart,
    /// Runs when restoring PP to a move.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "RestorePp"]
    RestorePp,
    /// Runs when a Mon is selected for a Mon's active position.
    ///
    /// Runs in the context of a Mon.
    #[string = "Select"]
    Select,
    /// Runs when a Mon's ability is being set.
    ///
    /// Runs before the ability is changed. Can be used to fail the ability change.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "SetAbility"]
    SetAbility,
    /// Runs when an item is being given to a Mon.
    ///
    /// Can prevent the item from being set.
    ///
    /// Runs on the item and in the context of an applying effect on a Mon.
    #[string = "SetItem"]
    SetItem,
    /// Runs when the Mon's last move selected is being set.
    ///
    /// Runs in the context of a Mon.
    #[string = "SetLastMove"]
    SetLastMove,
    /// Runs when a Mon's status effect is being set.
    ///
    /// Runs before the status effect is applied. Can be used to fail the status change.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "SetStatus"]
    SetStatus,
    /// Runs when the field's terrain is being set.
    ///
    /// Runs before the terrain effect is applied. Can be used to fail the terrain.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "SetTerrain"]
    SetTerrain,
    /// Runs when a Mon's types are being changed.
    ///
    /// Runs before the types are applied. Can be used to fail the type change.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "SetTypes"]
    SetTypes,
    /// Runs when the field's weather is being set.
    ///
    /// Runs before the weather effect is applied. Can be used to fail the weather.
    ///
    /// Runs in the context of an applying effect on the field.
    #[string = "SetWeather"]
    SetWeather,
    /// Runs when a side condition starts successfully.
    ///
    /// Runs in the context of an applying effect on a side.
    #[string = "SideConditionStart"]
    SideConditionStart,
    /// Runs when a side condition ends.
    ///
    /// Runs in the context of the side condition.
    #[string = "SideEnd"]
    SideEnd,
    /// Runs at the end of every turn to apply residual effects on the side.
    ///
    /// Runs in the context of the side condition.
    #[string = "SideResidual"]
    SideResidual,
    /// Runs when a side condition restarts.
    ///
    /// Runs in the context of the side condition.
    #[string = "SideRestart"]
    SideRestart,
    /// Runs when a side condition starts.
    ///
    /// Runs in the context of the side condition.
    #[string = "SideStart"]
    SideStart,
    /// Runs when a slot condition ends.
    ///
    /// Runs in the context of the slot condition.
    #[string = "SlotEnd"]
    SlotEnd,
    /// Runs when a slot condition restarts.
    ///
    /// Runs in the context of the slot condition.
    #[string = "SlotRestart"]
    SlotRestart,
    /// Runs when a slot condition starts.
    ///
    /// Runs in the context of the slot condition.
    #[string = "SlotStart"]
    SlotStart,
    /// Runs when a Mon attempts a stalling move (e.g., Protect).
    ///
    /// Can fail the stalling move (assuming the stalling move integrates with the event properly).
    ///
    /// Runs in the context of a Mon.
    #[string = "StallMove"]
    StallMove,
    /// Runs when an effect starts.
    ///
    /// Used to set up state.
    ///
    /// Runs on the effect.
    #[string = "Start"]
    Start,
    /// Runs when the battle starts.
    ///
    /// Runs in the context of the battle.
    #[string = "StartBattle"]
    StartBattle,
    /// Runs when Mon starts using a move.
    ///
    /// Runs in the context of a Mon.
    #[string = "StartUsingMove"]
    StartUsingMove,
    /// Runs when Mon stops using a move.
    ///
    /// Runs in the context of a Mon.
    #[string = "StopUsingMove"]
    StopUsingMove,
    /// Runs when determining the sub-priority of a move.
    ///
    /// Runs in the context of a move user.
    #[string = "SubPriority"]
    SubPriority,
    /// Runs when determining if terrain on the field is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressFieldTerrain"]
    SuppressFieldTerrain,
    /// Runs when determining if weather on the field is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressFieldWeather"]
    SuppressFieldWeather,
    /// Runs when determining if a Mon's ability is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressMonAbility"]
    SuppressMonAbility,
    /// Runs when determining if the item on the Mon is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressMonItem"]
    SuppressMonItem,
    /// Runs when determining if terrain on the Mon is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressMonTerrain"]
    SuppressMonTerrain,
    /// Runs when determining if weather on the Mon is suppressed, for some other active effect.
    ///
    /// Runs on the effect.
    #[string = "SuppressMonWeather"]
    SuppressMonWeather,
    /// Runs when a Mon swaps positions when another Mon.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Swap"]
    Swap,
    /// Runs when a Mon switches in.
    ///
    /// Runs in the context of a Mon.
    #[string = "SwitchIn"]
    SwitchIn,
    /// Runs when a Mon is switching in, prior to `SwitchIn`.
    ///
    /// Runs in the context of a Mon.
    #[string = "SwitchingIn"]
    SwitchingIn,
    /// Runs when a Mon is switching out.
    ///
    /// Runs in the context of a Mon.
    #[string = "SwitchOut"]
    SwitchOut,
    /// Runs when an item is being taken from a Mon.
    ///
    /// Can prevent the item from being taken.
    ///
    /// Runs on the item and in the context of an applying effect on a Mon.
    #[string = "TakeItem"]
    TakeItem,
    /// Runs when the terrain over a Mon changes.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "TerrainChange"]
    TerrainChange,
    /// Runs when determining if a Mon is trapped (i.e., cannot switch out).
    ///
    /// Runs in the context of a Mon.
    #[string = "TrapMon"]
    TrapMon,
    /// Runs when a group of stat boosts is being applied to a Mon.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "TryBoost"]
    TryBoost,
    /// Runs when a Mon tries to eat its item.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "TryEatItem"]
    TryEatItem,
    /// Runs when trying to end an effect.
    ///
    /// Can prevent the effect from ending.
    ///
    /// Runs on the effect.
    #[string = "TryEnd"]
    TryEnd,
    /// Runs before a Mon is healed for some amount of damage.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "TryHeal"]
    TryHeal,
    /// Runs when a move is trying to hit a set of targets.
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move and in the context of a move target.
    #[string = "TryHit"]
    TryHit,
    /// Runs when a move is trying to hit the whole field.
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move and in the context of an applying effect on the field.
    #[string = "TryHitField"]
    TryHitField,
    /// Runs when a move is trying to hit an entire side
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move and in the context of an applying effect on a side.
    #[string = "TryHitSide"]
    TryHitSide,
    /// Runs when a move is checking general immunity for its target.
    ///
    /// Can fail the move (by marking the target as immune).
    ///
    /// Runs in the context of the active move.
    #[string = "TryImmunity"]
    TryImmunity,
    /// Runs when trying to use a move.
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "TryMove"]
    TryMove,
    /// Runs when a move's primary hit is being applied to a target.
    ///
    /// Used to override the core battle engine logic. Can fail the move or return an amount of
    /// damage dealt to the target. If zero damage is returned, the core battle engine assumes a
    /// substitute was hit for the purposes of hit effects (i.e., hit effects do not apply to the
    /// target).
    ///
    /// Runs in the context of a move target.
    #[string = "TryPrimaryHit"]
    TryPrimaryHit,
    /// Runs when a Mon tries to use an item.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "TryUseItem"]
    TryUseItem,
    /// Runs when a Mon is trying to use a move on a set of targets.
    ///
    /// Can fail the move.
    ///
    /// Runs on the active move.
    #[string = "TryUseMove"]
    TryUseMove,
    /// Runs when determining if a Mon has immunity against a single type.
    ///
    /// Runs in the context of a Mon.
    #[string = "TypeImmunity"]
    TypeImmunity,
    /// Runs when determining the types of a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "Types"]
    Types,
    /// Runs when miscellaneous Mon effects in the battle could activate.
    ///
    /// Runs in the context of a Mon.
    #[string = "Update"]
    Update,
    /// Runs when a Mon uses a move, to upgrade the chosen move.
    ///
    /// Runs in the context of a move user.
    #[string = "UpgradeMove"]
    UpgradeMove,
    /// Runs when an item is used.
    ///
    /// Runs on the item.
    #[string = "Use"]
    Use,
    /// Runs when a Mon uses a move.
    ///
    /// Can be used to modify a move when it is used.
    ///
    /// Runs on the active move and in the context of a move user.
    #[string = "UseMove"]
    UseMove,
    /// Runs when a custom message should be displayed when a Mon uses a move.
    ///
    /// Runs on the active move.
    #[string = "UseMoveMessage"]
    UseMoveMessage,
    /// Runs when validating a Mon.
    ///
    /// Runs in the context of a Mon.
    #[string = "ValidateMon"]
    ValidateMon,
    /// Runs when validating a team.
    ///
    /// Runs in the context of a player.
    #[string = "ValidateTeam"]
    ValidateTeam,
    /// Runs when weather is activated at the end of each turn.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "Weather"]
    Weather,
    /// Runs when the weather over a Mon changes.
    ///
    /// Runs in the context of an applying effect on a Mon.
    #[string = "WeatherChange"]
    WeatherChange,
    /// Runs when calculating the damage applied to a Mon.
    ///
    /// Runs in the context of a move user.
    #[string = "WeatherModifyDamage"]
    WeatherModifyDamage,
}

impl BattleEvent {
    /// Maps the event to the [`CallbackFlag`] flags.
    pub fn callback_type_flags(&self) -> u32 {
        // Maintain alphabetical order.
        match self {
            Self::AccuracyCheckFailed => CommonCallbackType::MoveVoid as u32,
            Self::AccuracyExempt => CommonCallbackType::MoveBoolean as u32,
            Self::Activate => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::ActivateField => CommonCallbackType::FieldEffectVoid as u32,
            Self::ActivatePlayer => CommonCallbackType::PlayerEffectVoid as u32,
            Self::ActivateSide => CommonCallbackType::SideEffectVoid as u32,
            Self::AddPseudoWeather => CommonCallbackType::FieldEffectResult as u32,
            Self::AddType => CommonCallbackType::ApplyingEffectResult as u32,
            Self::AddVolatile => CommonCallbackType::ApplyingEffectResult as u32,
            Self::AfterAction => CommonCallbackType::MonVoid as u32,
            Self::AfterAddPseudoWeather => CommonCallbackType::FieldEffectVoid as u32,
            Self::AfterAddVolatile => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterBoost => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterCureStatus => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterDamage => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterEachBoost => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterFainted => CommonCallbackType::MonVoid as u32,
            Self::AfterHeal => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterHit => CommonCallbackType::MoveVoid as u32,
            Self::AfterMegaEvolution => CommonCallbackType::MonVoid as u32,
            Self::AfterMove => CommonCallbackType::SourceMoveVoid as u32,
            Self::AfterMoveSecondaryEffects => CommonCallbackType::MoveVoid as u32,
            Self::AfterMoveSecondaryEffectsDamage => CommonCallbackType::MoveVoid as u32,
            Self::AfterMoveSecondaryEffectsUser => CommonCallbackType::SourceMoveVoid as u32,
            Self::AfterSetAbility => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterSetItem => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterSetStatus => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterSubstituteDamage => CommonCallbackType::MoveVoid as u32,
            Self::AfterSwitchOut => CommonCallbackType::MonVoid as u32,
            Self::AfterTakeItem => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::AfterTerastallization => CommonCallbackType::MonVoid as u32,
            Self::AfterUseItem => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::BasePower => CommonCallbackType::MoveModifier as u32,
            Self::BattleEndTurn => CommonCallbackType::NoContextVoid as u32,
            Self::BeforeChargeMove => CommonCallbackType::SourceMoveVoid as u32,
            Self::BeforeDynamax => CommonCallbackType::MonResult as u32,
            Self::BeforeMove => CommonCallbackType::SourceMoveResult as u32,
            Self::BeforeStart => CommonCallbackType::EffectResult as u32,
            Self::BeforeSwitchIn => CommonCallbackType::MonVoid as u32,
            Self::BeforeSwitchOut => CommonCallbackType::MonVoid as u32,
            Self::BeforeTerastallization => CommonCallbackType::MonResult as u32,
            Self::BeforeTurn => CommonCallbackType::SourceMoveVoid as u32,
            Self::BerryEatingHealth => CommonCallbackType::MonModifier as u32,
            Self::CalculateStat => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::CanDynamax => CommonCallbackType::MonBoolean as u32,
            Self::CanEscape => CommonCallbackType::MonBoolean as u32,
            Self::CanHeal => CommonCallbackType::MonBoolean as u32,
            Self::Catch => CommonCallbackType::MonVoid as u32,
            Self::CatchFailed => CommonCallbackType::MonVoid as u32,
            Self::ChangeBoosts => CommonCallbackType::MonBoostModifier as u32,
            Self::ChargeMove => CommonCallbackType::SourceMoveResult as u32,
            Self::ClearTerrain => CommonCallbackType::FieldEffectResult as u32,
            Self::ClearWeather => CommonCallbackType::FieldEffectResult as u32,
            Self::CopyVolatile => CommonCallbackType::EffectResult as u32,
            Self::CriticalHit => CommonCallbackType::MoveBoolean as u32,
            Self::CureStatus => CommonCallbackType::ApplyingEffectResult as u32,
            Self::Damage => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::DamagingHit => CommonCallbackType::MoveVoid as u32,
            Self::DisableMove => CommonCallbackType::MonVoid as u32,
            Self::DeductPp => CommonCallbackType::SourceMoveModifier as u32,
            Self::DragOut => CommonCallbackType::MonResult as u32,
            Self::Duration => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::Effectiveness => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::Eat => CommonCallbackType::MonVoid as u32,
            Self::EatItem => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::End => CommonCallbackType::EffectVoid as u32,
            Self::EndBattle => CommonCallbackType::MonVoid as u32,
            Self::EndTurn => CommonCallbackType::MonVoid as u32,
            Self::Exit => CommonCallbackType::MonVoid as u32,
            Self::Faint => CommonCallbackType::MaybeApplyingEffectVoid as u32,
            Self::FieldEnd => CommonCallbackType::FieldVoid as u32,
            Self::FieldResidual => CommonCallbackType::FieldVoid as u32,
            Self::FieldRestart => CommonCallbackType::FieldResult as u32,
            Self::FieldStart => CommonCallbackType::FieldResult as u32,
            Self::Flinch => CommonCallbackType::MonVoid as u32,
            Self::ForceEffectiveness => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::ForceEscape => CommonCallbackType::MonBoolean as u32,
            Self::ForceTeraType => CommonCallbackType::MonType as u32,
            Self::ForceTypes => CommonCallbackType::MonTypes as u32,
            Self::Hit => CommonCallbackType::MoveResult as u32,
            Self::HitField => CommonCallbackType::MoveFieldResult as u32,
            Self::HitSide => CommonCallbackType::MoveSideResult as u32,
            Self::HitUser => CommonCallbackType::MoveResult as u32,
            Self::IgnoreImmunity => CommonCallbackType::MoveBoolean as u32,
            Self::Immunity => CommonCallbackType::ApplyingEffectBoolean as u32,
            Self::Invulnerability => CommonCallbackType::MoveBoolean as u32,
            Self::IsAsleep => CommonCallbackType::MonBoolean as u32,
            Self::IsAwayFromField => CommonCallbackType::MonBoolean as u32,
            Self::IsBehindSubstitute => CommonCallbackType::MonBoolean as u32,
            Self::IsChoiceLocked => CommonCallbackType::MonBoolean as u32,
            Self::IsContactProof => CommonCallbackType::MonBoolean as u32,
            Self::IsGrounded => CommonCallbackType::MonBoolean as u32,
            Self::IsImmuneToEntryHazards => CommonCallbackType::MonBoolean as u32,
            Self::IsRaining => CommonCallbackType::NoContextBoolean as u32,
            Self::IsSemiInvulnerable => CommonCallbackType::MonBoolean as u32,
            Self::IsSnowing => CommonCallbackType::NoContextBoolean as u32,
            Self::IsSoundproof => CommonCallbackType::MonBoolean as u32,
            Self::IsSunny => CommonCallbackType::NoContextBoolean as u32,
            Self::LockMove => CommonCallbackType::MonInfo as u32,
            Self::ModifyAccuracy => CommonCallbackType::MoveModifier as u32,
            Self::ModifyActionSpeed => CommonCallbackType::MonModifier as u32,
            Self::ModifyAtk => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::ModifyBoosts => CommonCallbackType::MaybeApplyingEffectBoostModifier as u32,
            Self::ModifyCatchRate => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::ModifyCritChance => CommonCallbackType::SourceMoveModifier as u32,
            Self::ModifyCritRatio => CommonCallbackType::SourceMoveModifier as u32,
            Self::ModifyDamage => CommonCallbackType::SourceMoveModifier as u32,
            Self::ModifyDef => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::ModifyDuration => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::ModifyEffectiveness => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::ModifyEvYield => CommonCallbackType::MonStatTableModifier as u32,
            Self::ModifyExperience => CommonCallbackType::MonModifier as u32,
            Self::ModifyFieldDuration => CommonCallbackType::FieldEffectModifier as u32,
            Self::ModifyFriendshipIncrease => CommonCallbackType::MonModifier as u32,
            Self::ModifyMoveType => CommonCallbackType::SourceEffectType as u32,
            Self::ModifyPriority => CommonCallbackType::SourceMoveModifier as u32,
            Self::ModifySecondaryEffects => CommonCallbackType::MoveSecondaryEffectModifier as u32,
            Self::ModifySideDuration => CommonCallbackType::SideEffectModifier as u32,
            Self::ModifySlotDuration => CommonCallbackType::SideEffectModifier as u32,
            Self::ModifySpA => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::ModifySpD => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::ModifySpe => CommonCallbackType::MaybeApplyingEffectModifier as u32,
            Self::ModifySpeciesCatchRate => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::ModifyStab => CommonCallbackType::SourceMoveModifier as u32,
            Self::ModifyTarget => CommonCallbackType::SourceMoveMonModifier as u32,
            Self::ModifyWeight => CommonCallbackType::MonModifier as u32,
            Self::MoveAborted => CommonCallbackType::SourceMoveVoid as u32,
            Self::MoveBasePower => CommonCallbackType::MoveModifier as u32,
            Self::MoveDamage => CommonCallbackType::MoveModifier as u32,
            Self::MoveFailed => CommonCallbackType::SourceMoveVoid as u32,
            Self::MoveTargetOverride => CommonCallbackType::MonMoveTarget as u32,
            Self::NegateImmunity => CommonCallbackType::MonBoolean as u32,
            Self::OverrideWeather => CommonCallbackType::MonInfo as u32,
            Self::OverwriteMove => CommonCallbackType::MonVoid as u32,
            Self::OverrideMove => CommonCallbackType::MonInfo as u32,
            Self::PlayerTryUseItem => CommonCallbackType::EffectBoolean as u32,
            Self::PlayerUse => CommonCallbackType::MonVoid as u32,
            Self::PreMoveEffect => CommonCallbackType::SourceMoveVoid as u32,
            Self::PrepareHit => CommonCallbackType::SourceMoveResult as u32,
            Self::PreventUsedItems => CommonCallbackType::MonBoolean as u32,
            Self::PriorityChargeMove => CommonCallbackType::SourceMoveVoid as u32,
            Self::RedirectTarget => CommonCallbackType::SourceMoveMonModifier as u32,
            Self::Residual => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::Restart => CommonCallbackType::EffectResult as u32,
            Self::RestorePp => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::Select => CommonCallbackType::MonVoid as u32,
            Self::SetAbility => CommonCallbackType::ApplyingEffectResult as u32,
            Self::SetItem => CommonCallbackType::ApplyingEffectResult as u32,
            Self::SetLastMove => CommonCallbackType::MonBoolean as u32,
            Self::SetStatus => CommonCallbackType::ApplyingEffectResult as u32,
            Self::SetTerrain => CommonCallbackType::FieldEffectResult as u32,
            Self::SetTypes => CommonCallbackType::ApplyingEffectResult as u32,
            Self::SetWeather => CommonCallbackType::FieldEffectResult as u32,
            Self::SideConditionStart => CommonCallbackType::SideVoid as u32,
            Self::SideEnd => CommonCallbackType::SideVoid as u32,
            Self::SideResidual => CommonCallbackType::SideVoid as u32,
            Self::SideRestart => CommonCallbackType::SideResult as u32,
            Self::SideStart => CommonCallbackType::SideResult as u32,
            Self::SlotEnd => CommonCallbackType::SideResult as u32,
            Self::SlotRestart => CommonCallbackType::SideResult as u32,
            Self::SlotStart => CommonCallbackType::SideResult as u32,
            Self::StallMove => CommonCallbackType::MonBoolean as u32,
            Self::Start => CommonCallbackType::EffectResult as u32,
            Self::StartBattle => CommonCallbackType::NoContextVoid as u32,
            Self::StartUsingMove => CommonCallbackType::MonVoid as u32,
            Self::StopUsingMove => CommonCallbackType::MonVoid as u32,
            Self::SubPriority => CommonCallbackType::SourceMoveModifier as u32,
            Self::SuppressFieldTerrain => CommonCallbackType::NoContextBoolean as u32,
            Self::SuppressFieldWeather => CommonCallbackType::NoContextBoolean as u32,
            Self::SuppressMonAbility => CommonCallbackType::MonBoolean as u32,
            Self::SuppressMonItem => CommonCallbackType::MonBoolean as u32,
            Self::SuppressMonTerrain => CommonCallbackType::MonBoolean as u32,
            Self::SuppressMonWeather => CommonCallbackType::MonBoolean as u32,
            Self::Swap => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::SwitchIn => CommonCallbackType::MonVoid as u32,
            Self::SwitchingIn => CommonCallbackType::MonVoid as u32,
            Self::SwitchOut => CommonCallbackType::MonVoid as u32,
            Self::TakeItem => CommonCallbackType::ApplyingEffectResult as u32,
            Self::TerrainChange => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::TrapMon => CommonCallbackType::MonBoolean as u32,
            Self::TryBoost => CommonCallbackType::ApplyingEffectBoostModifier as u32,
            Self::TryEatItem => CommonCallbackType::ApplyingEffectResult as u32,
            Self::TryEnd => CommonCallbackType::EffectResult as u32,
            Self::TryHeal => CommonCallbackType::ApplyingEffectModifier as u32,
            Self::TryHit => CommonCallbackType::MoveResult as u32,
            Self::TryHitField => CommonCallbackType::MoveFieldResult as u32,
            Self::TryHitSide => CommonCallbackType::MoveSideResult as u32,
            Self::TryImmunity => CommonCallbackType::MoveBoolean as u32,
            Self::TryMove => CommonCallbackType::SourceMoveResult as u32,
            Self::TryPrimaryHit => CommonCallbackType::MoveHitOutcomeResult as u32,
            Self::TryUseItem => CommonCallbackType::ApplyingEffectResult as u32,
            Self::TryUseMove => CommonCallbackType::SourceMoveResult as u32,
            Self::TypeImmunity => CommonCallbackType::MonBoolean as u32,
            Self::Types => CommonCallbackType::MonTypes as u32,
            Self::Update => CommonCallbackType::MonVoid as u32,
            Self::UpgradeMove => CommonCallbackType::SourceMoveActiveMove as u32,
            Self::Use => CommonCallbackType::MonVoid as u32,
            Self::UseMove => CommonCallbackType::SourceMoveVoid as u32,
            Self::UseMoveMessage => CommonCallbackType::SourceMoveVoid as u32,
            Self::ValidateMon => CommonCallbackType::MonValidator as u32,
            Self::ValidateTeam => CommonCallbackType::PlayerValidator as u32,
            Self::Weather => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::WeatherChange => CommonCallbackType::ApplyingEffectVoid as u32,
            Self::WeatherModifyDamage => CommonCallbackType::SourceMoveModifier as u32,
        }
    }

    /// Checks if the event has the given [`CallbackFlag`] flag set.
    pub fn has_flag(&self, flag: u32) -> bool {
        self.callback_type_flags() & flag != 0
    }

    /// The target of the event callback is the "origin" of the event.
    ///
    /// Most events target a Mon. Some event callbacks receive a Mon as the "source" of the effect.
    /// However, some events trigger against against the source Mon. This is most common for move
    /// events that run "in the context of a move user." In this sense, the target of the event
    /// callback is actually the source.
    ///
    /// This method allows event callbacks to understand who the true "origin" is, no matter who the
    /// target or source is. This is important for effects that need to understand why some event is
    /// running. For example, the ability "Mega Sol" needs to override weather event callbacks
    /// whenever the ability holder is the origin, which can sometimes be the source (e.g.,
    /// ModifySpd) or the target (e.g., WeatherModifyDamage).
    pub fn target_is_event_origin(&self) -> bool {
        self.has_flag(CallbackFlag::TakesGeneralMon) || self.has_flag(CallbackFlag::TakesUserMon)
    }

    /// Does the event allow custom input variables?
    pub fn allows_custom_input_vars(&self) -> bool {
        // Maintain alphabetical order.
        match self {
            Self::Activate => true,
            _ => false,
        }
    }

    /// The name of the input variable by index.
    pub fn input_vars(&self) -> &[(&str, ValueType, bool)] {
        // Maintain alphabetical order.
        match self {
            Self::AddPseudoWeather | Self::AfterAddPseudoWeather => {
                &[("pseudo_weather", ValueType::Effect, true)]
            }
            Self::AddType => &[("type", ValueType::Type, true)],
            Self::AddVolatile | Self::AfterAddVolatile => &[("volatile", ValueType::Effect, true)],
            Self::AfterBoost => &[("boosts", ValueType::BoostTable, true)],
            Self::AfterDamage => &[("damage", ValueType::UFraction, true)],
            Self::AfterEachBoost => &[
                ("boost", ValueType::Boost, true),
                ("value", ValueType::Fraction, true),
            ],
            Self::AfterFainted => &[
                ("count", ValueType::UFraction, true),
                ("effect", ValueType::Effect, false),
            ],
            Self::AfterHeal => &[("damage", ValueType::UFraction, true)],
            Self::AfterMove => &[("success", ValueType::Boolean, true)],
            Self::AfterMoveSecondaryEffectsDamage => &[
                ("damage", ValueType::UFraction, true),
                ("original_hp", ValueType::UFraction, true),
            ],
            Self::AfterMoveSecondaryEffectsUser => &[("targets", ValueType::List, true)],
            Self::AfterSetItem | Self::AfterTakeItem | Self::AfterUseItem => {
                &[("item", ValueType::Effect, true)]
            }
            Self::BasePower => &[("base_power", ValueType::UFraction, true)],
            Self::BerryEatingHealth => &[("hp", ValueType::UFraction, true)],
            Self::CalculateStat => &[
                ("stat", ValueType::UFraction, true),
                ("name", ValueType::Stat, true),
            ],
            Self::CatchFailed => &[("item", ValueType::Effect, true)],
            Self::ChangeBoosts => &[("boosts", ValueType::BoostTable, true)],
            Self::Damage => &[("damage", ValueType::UFraction, true)],
            Self::DamagingHit => &[("damage", ValueType::UFraction, true)],
            Self::DeductPp => &[("pp", ValueType::UFraction, true)],
            Self::EatItem => &[("item", ValueType::Effect, true)],
            Self::Effectiveness => &[
                ("modifier", ValueType::Fraction, true),
                ("type", ValueType::Type, true),
                ("index", ValueType::UFraction, true),
            ],
            Self::ForceEffectiveness => &[("modifier", ValueType::Fraction, true)],
            Self::ForceTeraType => &[("type", ValueType::Type, true)],
            Self::ModifyAccuracy => &[("acc", ValueType::UFraction, true)],
            Self::ModifyActionSpeed => &[("spe", ValueType::UFraction, true)],
            Self::ModifyAtk => &[("atk", ValueType::UFraction, true)],
            Self::ModifyBoosts => &[("boosts", ValueType::BoostTable, true)],
            Self::ModifyCatchRate | Self::ModifySpeciesCatchRate => {
                &[("catch_rate", ValueType::UFraction, true)]
            }
            Self::ModifyCritChance => &[("chance", ValueType::UFraction, true)],
            Self::ModifyCritRatio => &[("crit_ratio", ValueType::UFraction, true)],
            Self::ModifyDamage | Self::WeatherModifyDamage => {
                &[("damage", ValueType::UFraction, true)]
            }
            Self::ModifyDef => &[("def", ValueType::UFraction, true)],
            Self::ModifyDuration | Self::ModifySideDuration | Self::ModifyFieldDuration => &[
                ("duration", ValueType::UFraction, true),
                ("condition", ValueType::Effect, true),
            ],
            Self::ModifyEffectiveness => &[("modifier", ValueType::Fraction, true)],
            Self::ModifyEvYield => &[("evs", ValueType::StatTable, true)],
            Self::ModifyExperience => &[("exp", ValueType::UFraction, true)],
            Self::ModifyFriendshipIncrease => &[("friendship", ValueType::UFraction, true)],
            Self::ModifyMoveType => &[("type", ValueType::Type, true)],
            Self::ModifyPriority => &[("priority", ValueType::Fraction, true)],
            Self::ModifySecondaryEffects => &[("secondary_effects", ValueType::List, true)],
            Self::ModifySlotDuration => &[
                ("duration", ValueType::UFraction, true),
                ("slot", ValueType::UFraction, true),
                ("condition", ValueType::Effect, true),
            ],
            Self::ModifySpA => &[("spa", ValueType::UFraction, true)],
            Self::ModifySpD => &[("spd", ValueType::UFraction, true)],
            Self::ModifySpe => &[("spe", ValueType::UFraction, true)],
            Self::ModifyStab => &[("stab", ValueType::UFraction, true)],
            Self::ModifyTarget => &[("target", ValueType::Mon, false)],
            Self::ModifyWeight => &[("weight", ValueType::UFraction, true)],
            Self::NegateImmunity => &[("type", ValueType::Type, true)],
            Self::OverrideMove => &[("move", ValueType::String, true)],
            Self::PlayerTryUseItem => &[("input", ValueType::Object, true)],
            Self::PlayerUse => &[("input", ValueType::Object, true)],
            Self::RedirectTarget => &[("target", ValueType::Mon, true)],
            Self::RestorePp => &[("pp", ValueType::UFraction, true)],
            Self::Select => &[("selected", ValueType::Mon, true)],
            Self::SetAbility | Self::AfterSetAbility => &[("ability", ValueType::Effect, true)],
            Self::SetItem => &[("item", ValueType::Effect, true)],
            Self::SetStatus | Self::AfterSetStatus => &[("status", ValueType::Effect, true)],
            Self::SetTerrain => &[("terrain", ValueType::Effect, true)],
            Self::SetTypes => &[("types", ValueType::List, true)],
            Self::SetWeather => &[("weather", ValueType::Effect, true)],
            Self::SideConditionStart => &[("condition", ValueType::Effect, true)],
            Self::SlotEnd => &[("slot", ValueType::UFraction, true)],
            Self::SlotRestart => &[("slot", ValueType::UFraction, true)],
            Self::SlotStart => &[("slot", ValueType::UFraction, true)],
            Self::SubPriority => &[("sub_priority", ValueType::Fraction, true)],
            Self::TakeItem => &[("item", ValueType::Effect, true)],
            Self::TryBoost => &[("boosts", ValueType::BoostTable, true)],
            Self::TryEatItem => &[("item", ValueType::Effect, true)],
            Self::TryHit => &[("report", ValueType::Boolean, false)],
            Self::TryUseItem => &[("item", ValueType::Effect, true)],
            Self::TryHeal => &[("damage", ValueType::UFraction, true)],
            Self::TypeImmunity => &[("type", ValueType::Type, true)],
            Self::Types | Self::ForceTypes => &[("types", ValueType::List, true)],
            Self::ValidateMon | Self::ValidateTeam => &[("problems", ValueType::List, true)],
            _ => &[],
        }
    }

    /// Checks if the given output type is allowed.
    pub fn output_type_allowed(&self, value_type: Option<ValueType>) -> bool {
        match value_type {
            Some(value_type) if value_type.is_number() => {
                self.has_flag(CallbackFlag::ReturnsNumber)
            }
            Some(ValueType::Boolean) => {
                self.has_flag(CallbackFlag::ReturnsBoolean | CallbackFlag::ReturnsEventResult)
            }
            Some(ValueType::String) => self.has_flag(
                CallbackFlag::ReturnsString
                    | CallbackFlag::ReturnsEventResult
                    | CallbackFlag::ReturnsMoveTarget,
            ),
            Some(ValueType::EventResult) => self.has_flag(CallbackFlag::ReturnsEventResult),
            Some(ValueType::Mon) => self.has_flag(CallbackFlag::ReturnsMon),
            Some(ValueType::ActiveMove) => self.has_flag(CallbackFlag::ReturnsActiveMove),
            Some(ValueType::BoostTable) => self.has_flag(CallbackFlag::ReturnsBoosts),
            Some(ValueType::MoveTarget) => self.has_flag(CallbackFlag::ReturnsMoveTarget),
            Some(ValueType::StatTable) => self.has_flag(CallbackFlag::ReturnsStatTable),
            Some(ValueType::Type) => self.has_flag(CallbackFlag::ReturnsType),
            Some(ValueType::List) => self.has_flag(
                CallbackFlag::ReturnsTypes
                    | CallbackFlag::ReturnsSecondaryEffects
                    | CallbackFlag::ReturnsStrings,
            ),
            Some(ValueType::Undefined) => self.has_flag(CallbackFlag::ReturnsVoid),
            None => self.has_flag(CallbackFlag::ReturnsVoid),
            _ => false,
        }
    }

    /// The layer that the event is used for callback lookup.
    ///
    /// Some events can be used during callback lookup, which can cause unnecessary and even
    /// infinite recursion. To combat this, we give events used for callback lookup a layer number.
    /// When looking up the callbacks for event A, do not run callback lookup event B if `A.layer <=
    /// B.layer` (if A is below or at the same layer as B).
    ///
    /// For example, `Types` is in layer 0 and `SuppressFieldWeather` is in layer 1. Since
    /// `SuppressFieldWeather` is used for determining a Mon's effective weather, the Mon's
    /// effective weather should not be used as a callback for the `Types` event. In other words,
    /// the weather on the field cannot impact a Mon's types directly.
    ///
    /// This creates some limitations that must be carefully considered. These are very niche edge
    /// cases (such as the one described above), and there is almost always a workaround (in the
    /// above case, weather can apply a volatile effect to Mons for the duration of the weather
    /// that changes each Mon's type).
    ///
    /// An example of infinite recursion:
    /// - The battle engine runs the `Immunity` event for some Mon.
    /// - The Mon's types are included in the set of effects that could have a callback for this
    ///   event.
    /// - To determine the Mon's types, the battle engine runs the `Types` event.
    /// - The Mon's types are included in the set of effects that could have a callback for this
    ///   event.
    /// - The `Types` event leads to infinite recursion.
    ///
    /// An example of unnecessary recursion:
    /// - The battle engine runs the `Immunity` event for some Mon.
    /// - The Mon's types are included in the set of effects that could have a callback for this
    ///   event.
    /// - To determine the Mon's types, the callback lookup code runs the `Types` event.
    /// - The Mon's effective weather is included in the set of effects that could have a callback
    ///   for this event.
    /// - To determine the Mon's effective weather, the battle engine runs the `SuppressMonWeather`
    ///   event.
    /// - If the weather is not suppressed, the effective weather is based on the field's effective
    ///   weather.
    /// - To determine the field's effective weather, the battle engine runs the
    ///   `SuppressFieldWeather` event.
    /// - After those two events run, the effective weather for the `Types` event has been
    ///   determined.
    /// - All callbacks run to determine the Mon's types.
    /// - Then, the `SuppressMonWeather` and `SuppressFieldWeather` events are run *again* for the
    ///   `Immunity` event.
    /// - The weather events are run twice. If weather does not ever impact the Mon's types, we do
    ///   not need to run the weather events in the `Types` event.
    pub fn callback_lookup_layer(&self) -> usize {
        match self {
            Self::SuppressMonAbility => 0,
            Self::SuppressMonItem => 1,
            Self::ForceTypes => 2,
            Self::Types => 2,
            Self::IsGrounded => 3,
            Self::IsSemiInvulnerable => 3,
            Self::SuppressFieldTerrain => 4,
            Self::SuppressFieldWeather => 4,
            Self::SuppressMonTerrain => 5,
            Self::SuppressMonWeather => 5,
            Self::OverrideWeather => 5,
            _ => usize::MAX,
        }
    }

    /// Whether or not to run the event callback on the source effect when running all callbacks for
    /// an event.
    pub fn run_callback_on_source_effect(&self) -> bool {
        match self {
            Self::BasePower => true,
            Self::Damage => true,
            Self::ModifyCatchRate => true,
            Self::ModifySpeciesCatchRate => true,
            Self::ModifyTarget => true,
            Self::WeatherModifyDamage => true,
            _ => false,
        }
    }

    /// Whether or not to force effects to have a default callback for the event.
    ///
    /// This is used for residual events that are suppressed. We keep the callback so that durations
    /// are updated without running the actual callback.
    pub fn force_default_callback(&self) -> bool {
        match self {
            Self::FieldStart | Self::SideStart | Self::SlotStart | Self::Start => true,
            Self::FieldResidual | Self::SideResidual | Self::Residual => true,
            _ => false,
        }
    }

    /// Whether or not to exclude effects that are not started.
    ///
    /// See [`EffectState::started`][`crate::effect::fxlang::EffectState::started`]. Ordinarily,
    /// event callbacks are still run against un-started effects. However, this may be undesirable
    /// for specific events that run very frequently. This option may be used for overriding this
    /// behavior.
    ///
    /// For example, the [`Update`][`Self::Update`] event runs after every action. However,
    /// switch-ins and switch-in events are split across two separate actions. The `Update`
    /// event after the switch-in may trigger a Mon to use its held item (e.g., eat a berry when
    /// it switches in at low HP) immediately.
    ///
    /// However, the item should only be consumed once it "starts" as part of the switch-in events
    /// action. This option forces the `Update` event callback to wait until the item is officially
    /// started, which satisfies our ordering requirements. Events such as entry hazards occur
    /// *before* the item starts and is consumed on the subsequent `Update` event.
    pub fn exclude_unstarted_effects(&self) -> bool {
        match self {
            Self::Update => true,
            _ => false,
        }
    }

    /// Whether or not to use the effect's order (on its
    /// [`EffectState`][`crate::effect::fxlang::EffectState`]) when ordering callbacks.
    pub fn order_using_effect_order(&self) -> bool {
        match self {
            Self::Residual | Self::SwitchIn => true,
            _ => false,
        }
    }

    /// Whether or not the event represents state rather than an active event.
    pub fn state_event(&self) -> bool {
        self.to_string().starts_with("Is")
    }

    /// Whether or not the event is intended to start the associated effect.
    pub fn starts_effect(&self) -> bool {
        match self {
            Self::FieldStart | Self::Start | Self::SideStart | Self::SlotStart => true,
            _ => false,
        }
    }

    /// Whether or not the event is intended to end the associated effect.
    pub fn ends_effect(&self) -> bool {
        match self {
            Self::FieldEnd | Self::End | Self::SideEnd | Self::SlotEnd => true,
            _ => false,
        }
    }

    /// The associated event on the field.
    ///
    /// Only used for events that are distinct when using modifiers.
    pub fn field_event(&self) -> Option<BattleEvent> {
        match self {
            Self::Residual => Some(Self::FieldResidual),
            _ => None,
        }
    }

    /// The associated event on the field.
    ///
    /// Only used for events that are distinct when using modifiers.
    pub fn side_event(&self) -> Option<BattleEvent> {
        match self {
            Self::Residual => Some(Self::SideResidual),
            _ => None,
        }
    }
}

/// An fxlang program, which describes an individual callback for an effect to be interpreted and
/// applied in battle.
///
/// Internally represented as a tree-like structure for interpretation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Program {
    /// A single statement.
    Leaf(String),
    /// A group of statements that should be executed together.
    ///
    /// A branch can be conditionally or repeatedly executed by the preceding statement.
    Branch(Vec<Program>),
}

/// Metadata for an fxlang program.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProgramMetadata {
    /// Custom parameters, assuming the event supports it.
    pub parameters: Vec<String>,
}

/// An fxlang program with priority information for ordering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgramWithPriority {
    pub program: Option<Program>,
    pub order: Option<u32>,
    pub priority: Option<i32>,
    pub sub_order: Option<u32>,
    pub metadata: Option<ProgramMetadata>,
}

/// The input to the [`Callback`] type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CallbackInput {
    Regular(Program),
    WithPriority(ProgramWithPriority),
}

/// A single callback, to be called when applying an effect on some triggered event.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Callback(Option<CallbackInput>);

impl Callback {
    /// Checks if the callback has an associated [`Program`].
    pub fn has_program(&self) -> bool {
        match &self.0 {
            Some(CallbackInput::Regular(_)) => true,
            Some(CallbackInput::WithPriority(program)) => program.program.is_some(),
            None => false,
        }
    }

    /// Returns a reference to the callback's [`Program`].
    pub fn program(&self) -> Option<&Program> {
        match self.0.as_ref()? {
            CallbackInput::Regular(program) => Some(&program),
            CallbackInput::WithPriority(program) => program.program.as_ref(),
        }
    }

    pub fn metadata(&self) -> Option<&ProgramMetadata> {
        match self.0.as_ref()? {
            CallbackInput::Regular(_) => None,
            CallbackInput::WithPriority(program) => program.metadata.as_ref(),
        }
    }
}

impl SpeedOrderable for Callback {
    fn order(&self) -> u32 {
        match &self.0 {
            Some(CallbackInput::WithPriority(program)) => program.order.unwrap_or(u32::MAX),
            _ => u32::MAX,
        }
    }

    fn priority(&self) -> i32 {
        match &self.0 {
            Some(CallbackInput::WithPriority(program)) => program.priority.unwrap_or(0),
            _ => 0,
        }
    }

    fn sub_priority(&self) -> i32 {
        0
    }

    fn speed(&self) -> u32 {
        0
    }

    fn sub_order(&self) -> u32 {
        match &self.0 {
            Some(CallbackInput::WithPriority(program)) => program.sub_order.unwrap_or(0),
            _ => 0,
        }
    }
}

/// A collection of callbacks for an effect.
pub type Callbacks = HashMap<String, Callback>;

/// Attributes for an [`Effect`] that are meaningful when attaching to some part of a battle.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ConditionAttributes {
    /// The static duration of the effect.
    ///
    /// Can be overwritten by the [`Duration`][`BattleEvent::Duration`] callback.
    pub duration: Option<u8>,

    /// Whether or not the effect can be copied to another Mon.
    ///
    /// If true, moves like "Baton Pass" will not copy this effect. `false` by default.
    #[serde(default)]
    pub no_copy: bool,

    /// Whether or not the effect should be copied when a Mon copies boosts.
    #[serde(default)]
    pub copy_with_boosts: bool,
}

impl ConditionAttributes {
    /// Extends the condition attributes with some other attribute object, overriding data if
    /// applicable.
    pub fn extend(&mut self, other: Self) {
        if let Some(duration) = other.duration {
            self.duration = Some(duration);
        }
        self.no_copy = other.no_copy || self.no_copy;
        self.copy_with_boosts = other.copy_with_boosts || self.copy_with_boosts;
    }
}

/// Attributes for an [`Effect`].
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct EffectAttributes {
    /// Effects to delegate to.
    ///
    /// Format is an effect's fxlang ID: `${type}:${id}`.
    ///
    /// Callbacks from delegate effects are imported. Any callback on this effect overwrites
    /// imported callbacks.
    #[serde(default)]
    pub delegates: Vec<String>,

    /// Attributes for an effect that attaches to some part of a battle.
    #[serde(flatten)]
    pub condition: ConditionAttributes,
}

/// An effect, whose callbacks are triggered in the context of an ongoing battle.
///
/// When an effect is active, its event callbacks are triggered throughout the course of a battle.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Effect {
    /// Event callbacks for the effect.
    #[serde(default)]
    pub callbacks: Callbacks,

    /// Local data for the effects.
    #[serde(default)]
    pub local_data: LocalData,

    /// Effect attributes.
    #[serde(flatten)]
    pub attributes: EffectAttributes,
}

impl TryFrom<serde_json::Value> for Effect {
    type Error = Error;
    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
        serde_json::from_value(value).wrap_error_with_message("invalid fxlang effect")
    }
}