typespec_rs 0.4.1

A Rust implementation of the TypeSpec type system — parser, checker, and emitter
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
//! Checker Type System
//! Ported from TypeSpec compiler/src/core/types.ts
//!
//! Defines the Type enum, TypeId, and TypeStore that form the core
//! of the checker's type representation.

use crate::ast::node::NodeId;
use std::collections::HashMap;

/// Type mapper for template instantiation.
/// Ported from TS compiler TypeMapper interface.
///
/// Maps template parameters to their concrete types when a template is instantiated.
/// Template declarations have `template_mapper = None`; template instances have `Some`.
#[derive(Debug, Clone)]
pub struct TypeMapper {
    /// Whether this is a partial mapping
    pub partial: bool,
    /// The mapping from template parameter NodeIds to TypeIds
    pub map: HashMap<NodeId, TypeId>,
    /// Arguments used for instantiation (TypeIds)
    pub args: Vec<TypeId>,
    /// Source node used to create this mapper
    pub source_node: Option<NodeId>,
    /// Parent mapper if any
    pub parent_mapper: Option<Box<TypeMapper>>,
}

impl TypeMapper {
    pub fn new() -> Self {
        Self {
            partial: false,
            map: HashMap::new(),
            args: Vec::new(),
            source_node: None,
            parent_mapper: None,
        }
    }
}

impl Default for TypeMapper {
    fn default() -> Self {
        Self::new()
    }
}

/// Type ID - unique identifier for a type in the type store
pub type TypeId = u32;

/// Reserved TypeId for invalid/uninitialized type
pub const INVALID_TYPE_ID: TypeId = 0;

/// Result of inferring a scalar type for a primitive value.
/// Ported from TS checker.ts inferScalarForPrimitiveValue.
/// When multiple scalars match the same primitive value (e.g., int32 | int64),
/// `ambiguous` contains the diagnostic info and `scalar` is None.
#[derive(Debug, Clone)]
pub struct InferredScalar {
    /// The inferred scalar TypeId, if unambiguous
    pub scalar: Option<TypeId>,
    /// If ambiguous: (value_name, ambiguous_type_names, example_name)
    pub ambiguous: Option<(String, String, String)>,
}

impl InferredScalar {
    /// Create an unambiguous result with a single scalar
    pub fn single(scalar: TypeId) -> Self {
        Self {
            scalar: Some(scalar),
            ambiguous: None,
        }
    }

    /// Create a result with no match
    pub fn none() -> Self {
        Self {
            scalar: None,
            ambiguous: None,
        }
    }

    /// Create an ambiguous result
    pub fn ambiguous(value_name: String, type_names: String, example_name: String) -> Self {
        Self {
            scalar: None,
            ambiguous: Some((value_name, type_names, example_name)),
        }
    }
}

// ============================================================================
// Intrinsic Types
// ============================================================================

/// Intrinsic type names (void, never, unknown, null, ErrorType)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IntrinsicTypeName {
    ErrorType,
    Void,
    Never,
    Unknown,
    Null,
}

/// Intrinsic type representation
#[derive(Debug, Clone)]
pub struct IntrinsicType {
    pub id: TypeId,
    pub name: IntrinsicTypeName,
    pub node: Option<NodeId>,
    pub is_finished: bool,
}

// ============================================================================
// Literal Types
// ============================================================================

/// String literal type - e.g., "hello"
#[derive(Debug, Clone)]
pub struct StringType {
    pub id: TypeId,
    pub value: String,
    pub node: Option<NodeId>,
    pub is_finished: bool,
}

/// Numeric literal type - e.g., 42
#[derive(Debug, Clone)]
pub struct NumericType {
    pub id: TypeId,
    pub value: f64,
    pub value_as_string: String,
    pub node: Option<NodeId>,
    pub is_finished: bool,
}

/// Boolean literal type - true or false
#[derive(Debug, Clone)]
pub struct BooleanType {
    pub id: TypeId,
    pub value: bool,
    pub node: Option<NodeId>,
    pub is_finished: bool,
}

// ============================================================================
// Composite Types
// ============================================================================

/// How a source model was used in building a model type.
/// Ported from TS SourceModel.usage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SourceModelUsage {
    /// `model A is B`
    Is,
    /// `model A {...B}` (spread)
    Spread,
    /// `alias A = B & C` (intersection)
    Intersection,
}

impl SourceModelUsage {
    /// Get the string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            SourceModelUsage::Is => "is",
            SourceModelUsage::Spread => "spread",
            SourceModelUsage::Intersection => "intersection",
        }
    }
}

/// Information about a source model used to build a model type.
/// Ported from TS SourceModel interface.
#[derive(Debug, Clone)]
pub struct SourceModel {
    /// How was this model used (is, spread, or intersection)
    pub usage: SourceModelUsage,
    /// The source model TypeId
    pub model: TypeId,
    /// Node where this source model was referenced
    pub node: Option<NodeId>,
}

/// Model type - represents a TypeSpec model
#[derive(Debug, Clone)]
pub struct ModelType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub properties: HashMap<String, TypeId>,
    pub property_names: Vec<String>, // Ordered property names
    pub base_model: Option<TypeId>,
    pub derived_models: Vec<TypeId>,
    pub source_model: Option<TypeId>,
    /// Source models used to build this model, with usage information.
    /// Includes models referenced via `model is`, `...` spread, or intersection `&`.
    /// TS: ModelType.sourceModels (SourceModel[])
    pub source_models: Vec<SourceModel>,
    pub indexer: Option<(TypeId, TypeId)>, // (key_type, value_type)
    pub template_node: Option<NodeId>,
    /// Template mapper for template instances. None for template declarations and non-templated types.
    /// TS: TemplatedType.templateMapper
    pub template_mapper: Option<Box<TypeMapper>>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl ModelType {
    /// Create a new ModelType with sensible defaults for all optional fields.
    pub fn new(id: TypeId, name: String, node: Option<NodeId>, namespace: Option<TypeId>) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            properties: HashMap::new(),
            property_names: Vec::new(),
            base_model: None,
            derived_models: Vec::new(),
            source_model: None,
            source_models: vec![],
            indexer: None,
            template_node: None,
            template_mapper: None,
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Model property type
#[derive(Debug, Clone)]
pub struct ModelPropertyType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub r#type: TypeId,
    pub optional: bool,
    pub default_value: Option<TypeId>,
    pub model: Option<TypeId>,
    /// For properties copied via `is`, points to the source property
    /// TS: ModelProperty.sourceProperty
    pub source_property: Option<TypeId>,
    pub decorators: Vec<DecoratorApplication>,
    pub is_finished: bool,
}

/// Interface type
#[derive(Debug, Clone)]
pub struct InterfaceType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub operations: HashMap<String, TypeId>,
    pub operation_names: Vec<String>,
    pub extends: Vec<TypeId>,
    /// TS: sourceInterfaces - interfaces that this interface extends
    pub source_interfaces: Vec<TypeId>,
    pub template_node: Option<NodeId>,
    /// Template mapper for template instances. None for template declarations and non-templated types.
    /// TS: TemplatedType.templateMapper
    pub template_mapper: Option<Box<TypeMapper>>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl InterfaceType {
    pub fn new(id: TypeId, name: String, node: Option<NodeId>, namespace: Option<TypeId>) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            operations: HashMap::new(),
            operation_names: Vec::new(),
            extends: Vec::new(),
            source_interfaces: Vec::new(),
            template_node: None,
            template_mapper: None,
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Operation type
#[derive(Debug, Clone)]
pub struct OperationType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub parameters: Option<TypeId>, // ModelType for params
    pub return_type: Option<TypeId>,
    pub source_operation: Option<TypeId>, // TS: sourceOperation - when `op foo is bar`
    /// The interface this operation belongs to, if any.
    /// TS: Operation.interface
    pub interface_: Option<TypeId>,
    pub template_node: Option<NodeId>,
    /// Template mapper for template instances. None for template declarations and non-templated types.
    /// TS: TemplatedType.templateMapper
    pub template_mapper: Option<Box<TypeMapper>>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl OperationType {
    pub fn new(id: TypeId, name: String, node: Option<NodeId>, namespace: Option<TypeId>) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            parameters: None,
            return_type: None,
            source_operation: None,
            interface_: None,
            template_node: None,
            template_mapper: None,
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Enum type
#[derive(Debug, Clone)]
pub struct EnumType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub members: HashMap<String, TypeId>,
    pub member_names: Vec<String>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl EnumType {
    pub fn new(id: TypeId, name: String, node: Option<NodeId>, namespace: Option<TypeId>) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            members: HashMap::new(),
            member_names: Vec::new(),
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Enum member type
#[derive(Debug, Clone)]
pub struct EnumMemberType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub r#enum: Option<TypeId>,
    pub value: Option<TypeId>, // String or Numeric type
    /// TS: sourceMember - when spread operators make new enum members,
    /// this tracks the enum member we copied from
    pub source_member: Option<TypeId>,
    pub decorators: Vec<DecoratorApplication>,
    pub is_finished: bool,
}

/// Union type
#[derive(Debug, Clone)]
pub struct UnionType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub variants: HashMap<String, TypeId>,
    pub variant_names: Vec<String>,
    /// Whether this is an anonymous union expression (e.g., `string | int32`)
    /// vs a named union declaration.
    /// TS: Union.expression
    pub expression: bool,
    pub template_node: Option<NodeId>,
    /// Template mapper for template instances. None for template declarations and non-templated types.
    /// TS: TemplatedType.templateMapper
    pub template_mapper: Option<Box<TypeMapper>>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl UnionType {
    pub fn new(
        id: TypeId,
        name: String,
        node: Option<NodeId>,
        namespace: Option<TypeId>,
        expression: bool,
    ) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            expression,
            variants: HashMap::new(),
            variant_names: Vec::new(),
            template_node: None,
            template_mapper: None,
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Union variant type
#[derive(Debug, Clone)]
pub struct UnionVariantType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub r#type: TypeId,
    pub union: Option<TypeId>,
    pub decorators: Vec<DecoratorApplication>,
    pub is_finished: bool,
}

/// Scalar type
#[derive(Debug, Clone)]
pub struct ScalarType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub base_scalar: Option<TypeId>,
    pub constructors: Vec<TypeId>,
    /// TS: derivedScalars - list of scalars that extend this one
    pub derived_scalars: Vec<TypeId>,
    pub template_node: Option<NodeId>,
    /// Template mapper for template instances. None for template declarations and non-templated types.
    /// TS: TemplatedType.templateMapper
    pub template_mapper: Option<Box<TypeMapper>>,
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl ScalarType {
    pub fn new(
        id: TypeId,
        name: String,
        node: Option<NodeId>,
        namespace: Option<TypeId>,
        base_scalar: Option<TypeId>,
    ) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            base_scalar,
            constructors: Vec::new(),
            derived_scalars: Vec::new(),
            template_node: None,
            template_mapper: None,
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished: false,
        }
    }
}

/// Scalar constructor type
#[derive(Debug, Clone)]
pub struct ScalarConstructorType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub scalar: Option<TypeId>,
    pub parameters: Vec<TypeId>,
    pub is_finished: bool,
}

/// Template parameter type
#[derive(Debug, Clone)]
pub struct TemplateParameterType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub constraint: Option<TypeId>,
    pub default: Option<TypeId>,
    pub is_finished: bool,
}

/// Tuple type
#[derive(Debug, Clone)]
pub struct TupleType {
    pub id: TypeId,
    pub node: Option<NodeId>,
    pub values: Vec<TypeId>,
    pub is_finished: bool,
}

/// Namespace type
#[derive(Debug, Clone)]
pub struct NamespaceType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub models: HashMap<String, TypeId>,
    pub model_names: Vec<String>, // Ordered model names
    pub scalars: HashMap<String, TypeId>,
    pub scalar_names: Vec<String>, // Ordered scalar names
    pub operations: HashMap<String, TypeId>,
    pub operation_names: Vec<String>, // Ordered operation names
    pub interfaces: HashMap<String, TypeId>,
    pub interface_names: Vec<String>, // Ordered interface names
    pub enums: HashMap<String, TypeId>,
    pub enum_names: Vec<String>, // Ordered enum names
    pub unions: HashMap<String, TypeId>,
    pub union_names: Vec<String>, // Ordered union names
    pub namespaces: HashMap<String, TypeId>,
    pub namespace_names: Vec<String>, // Ordered namespace names
    /// TS: decoratorDeclarations - decorator declarations in this namespace
    pub decorator_declarations: HashMap<String, TypeId>,
    pub decorator_declaration_names: Vec<String>, // Ordered decorator declaration names
    /// TS: functionDeclarations - function declarations in this namespace
    pub function_declarations: HashMap<String, TypeId>,
    pub function_declaration_names: Vec<String>, // Ordered function declaration names
    pub decorators: Vec<DecoratorApplication>,
    /// Documentation comment
    pub doc: Option<String>,
    /// Summary comment
    pub summary: Option<String>,
    pub is_finished: bool,
}

impl NamespaceType {
    /// Create a new NamespaceType with empty collections.
    /// Only `id`, `name`, `node`, `namespace`, and `is_finished` need to be specified.
    pub fn new(
        id: TypeId,
        name: String,
        node: Option<NodeId>,
        namespace: Option<TypeId>,
        is_finished: bool,
    ) -> Self {
        Self {
            id,
            name,
            node,
            namespace,
            models: HashMap::new(),
            model_names: Vec::new(),
            scalars: HashMap::new(),
            scalar_names: Vec::new(),
            operations: HashMap::new(),
            operation_names: Vec::new(),
            interfaces: HashMap::new(),
            interface_names: Vec::new(),
            enums: HashMap::new(),
            enum_names: Vec::new(),
            unions: HashMap::new(),
            union_names: Vec::new(),
            namespaces: HashMap::new(),
            namespace_names: Vec::new(),
            decorator_declarations: HashMap::new(),
            decorator_declaration_names: Vec::new(),
            function_declarations: HashMap::new(),
            function_declaration_names: Vec::new(),
            decorators: Vec::new(),
            doc: None,
            summary: None,
            is_finished,
        }
    }

    /// Look up a member by name across all member maps (namespaces, models, scalars, etc.).
    /// Returns the first match in order: namespaces, models, scalars, interfaces, enums, unions, operations, decorator_declarations.
    pub fn lookup_member(&self, name: &str) -> Option<TypeId> {
        self.namespaces
            .get(name)
            .copied()
            .or_else(|| self.models.get(name).copied())
            .or_else(|| self.scalars.get(name).copied())
            .or_else(|| self.interfaces.get(name).copied())
            .or_else(|| self.enums.get(name).copied())
            .or_else(|| self.unions.get(name).copied())
            .or_else(|| self.operations.get(name).copied())
            .or_else(|| self.decorator_declarations.get(name).copied())
            .or_else(|| self.function_declarations.get(name).copied())
    }
}

/// Decorator type
#[derive(Debug, Clone)]
pub struct DecoratorType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub target: Option<TypeId>,
    /// The target type constraint (e.g., "Model", "unknown") - describes what the decorator can be applied to
    pub target_type: String,
    pub parameters: Vec<FunctionParameterType>,
    pub is_finished: bool,
}

/// Function type — represents an `extern fn` declaration.
/// Unlike decorators, functions can accept both type and value arguments (mixed parameters).
#[derive(Debug, Clone)]
pub struct FunctionTypeType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub namespace: Option<TypeId>,
    pub parameters: Vec<FunctionParameterType>,
    pub return_type: Option<TypeId>,
    pub is_finished: bool,
}

/// Function parameter type
#[derive(Debug, Clone)]
pub struct FunctionParameterType {
    pub id: TypeId,
    pub name: String,
    pub node: Option<NodeId>,
    pub r#type: Option<TypeId>,
    pub optional: bool,
    pub rest: bool,
    pub is_finished: bool,
}

/// String template type
#[derive(Debug, Clone)]
pub struct StringTemplateType {
    pub id: TypeId,
    pub node: Option<NodeId>,
    pub spans: Vec<TypeId>,
    /// The computed string value if all interpolated elements are string-serializable.
    /// None if any element cannot be converted to a string.
    pub string_value: Option<String>,
    pub is_finished: bool,
}

/// String template span type
#[derive(Debug, Clone)]
pub struct StringTemplateSpanType {
    pub id: TypeId,
    pub node: Option<NodeId>,
    pub expression: Option<TypeId>,
    pub r#type: Option<TypeId>,
    pub is_finished: bool,
}

// ============================================================================
// Type Enum
// ============================================================================

/// The Type enum - represents all possible TypeSpec types.
/// Ported from TS `type Type = BooleanLiteral | Decorator | ...`
#[derive(Debug, Clone)]
pub enum Type {
    Intrinsic(IntrinsicType),
    Model(ModelType),
    ModelProperty(ModelPropertyType),
    Interface(InterfaceType),
    Operation(OperationType),
    Enum(EnumType),
    EnumMember(EnumMemberType),
    Union(UnionType),
    UnionVariant(UnionVariantType),
    Scalar(ScalarType),
    ScalarConstructor(ScalarConstructorType),
    TemplateParameter(TemplateParameterType),
    Tuple(TupleType),
    String(StringType),
    Number(NumericType),
    Boolean(BooleanType),
    Namespace(Box<NamespaceType>),
    Decorator(DecoratorType),
    FunctionType(FunctionTypeType),
    FunctionParameter(FunctionParameterType),
    StringTemplate(StringTemplateType),
    StringTemplateSpan(StringTemplateSpanType),
}

/// Trait for types that support template parameters (Model, Interface, Operation, Union, Scalar).
/// Provides unified access to template-related fields, eliminating the need for
/// 5-arm match expressions in is_template_instance/is_template_declaration.
pub trait TemplateInfo {
    /// Whether a template mapper is set (true for template instances)
    fn has_template_mapper(&self) -> bool;
    fn template_node(&self) -> Option<NodeId>;
    fn node(&self) -> Option<NodeId>;
}

/// Macro to implement TemplateInfo for types that support templates.
macro_rules! impl_template_info {
    ($ty:ty) => {
        impl TemplateInfo for $ty {
            fn has_template_mapper(&self) -> bool {
                self.template_mapper.is_some()
            }
            fn template_node(&self) -> Option<NodeId> {
                self.template_node
            }
            fn node(&self) -> Option<NodeId> {
                self.node
            }
        }
    };
}

impl_template_info!(ModelType);
impl_template_info!(InterfaceType);
impl_template_info!(OperationType);
impl_template_info!(UnionType);
impl_template_info!(ScalarType);

/// Get template info for a Type, if it supports templates.
pub fn get_template_info(t: &Type) -> Option<&dyn TemplateInfo> {
    match t {
        Type::Model(m) => Some(m),
        Type::Interface(i) => Some(i),
        Type::Operation(o) => Some(o),
        Type::Union(u) => Some(u),
        Type::Scalar(s) => Some(s),
        _ => None,
    }
}

/// Macro to dispatch a method call across all Type variants.
/// Reduces boilerplate for methods that access a common field (id, node, is_finished).
macro_rules! type_dispatch {
    ($self:expr, $inner:ident, $body:expr) => {
        match $self {
            Type::Intrinsic($inner) => $body,
            Type::Model($inner) => $body,
            Type::ModelProperty($inner) => $body,
            Type::Interface($inner) => $body,
            Type::Operation($inner) => $body,
            Type::Enum($inner) => $body,
            Type::EnumMember($inner) => $body,
            Type::Union($inner) => $body,
            Type::UnionVariant($inner) => $body,
            Type::Scalar($inner) => $body,
            Type::ScalarConstructor($inner) => $body,
            Type::TemplateParameter($inner) => $body,
            Type::Tuple($inner) => $body,
            Type::String($inner) => $body,
            Type::Number($inner) => $body,
            Type::Boolean($inner) => $body,
            Type::Namespace($inner) => $body,
            Type::Decorator($inner) => $body,
            Type::FunctionType($inner) => $body,
            Type::FunctionParameter($inner) => $body,
            Type::StringTemplate($inner) => $body,
            Type::StringTemplateSpan($inner) => $body,
        }
    };
}

/// Macro for partial dispatch — only matches listed variants, returns None for others.
/// Use for methods that only apply to some Type variants (e.g., namespace, decorators).
macro_rules! type_dispatch_partial {
    // Form 1: body is a direct value (not Option), wraps in Some
    ($self:expr, [$($variant:ident),*], $inner:ident, $body:expr) => {
        match $self {
            $(Type::$variant($inner) => Some($body),)*
            _ => None,
        }
    };
    // Form 2: body already returns Option, no wrapping
    (opt $self:expr, [$($variant:ident),*], $inner:ident, $body:expr) => {
        match $self {
            $(Type::$variant($inner) => $body,)*
            _ => None,
        }
    };
}

/// Macro for Value enum dispatch — matches all 10 variants.
macro_rules! value_dispatch {
    ($self:expr, $inner:ident, $body:expr) => {
        match $self {
            Value::StringValue($inner) => $body,
            Value::NumericValue($inner) => $body,
            Value::BooleanValue($inner) => $body,
            Value::ObjectValue($inner) => $body,
            Value::ArrayValue($inner) => $body,
            Value::EnumValue($inner) => $body,
            Value::NullValue($inner) => $body,
            Value::ScalarValue($inner) => $body,
            Value::FunctionValue($inner) => $body,
            Value::TemplateValue($inner) => $body,
        }
    };
}

impl Type {
    /// Get the TypeId of this type
    pub fn id(&self) -> TypeId {
        type_dispatch!(self, t, t.id)
    }

    /// Get the kind name of this type
    pub fn kind_name(&self) -> &'static str {
        match self {
            Type::Intrinsic(_) => "Intrinsic",
            Type::Model(_) => "Model",
            Type::ModelProperty(_) => "ModelProperty",
            Type::Interface(_) => "Interface",
            Type::Operation(_) => "Operation",
            Type::Enum(_) => "Enum",
            Type::EnumMember(_) => "EnumMember",
            Type::Union(_) => "Union",
            Type::UnionVariant(_) => "UnionVariant",
            Type::Scalar(_) => "Scalar",
            Type::ScalarConstructor(_) => "ScalarConstructor",
            Type::TemplateParameter(_) => "TemplateParameter",
            Type::Tuple(_) => "Tuple",
            Type::String(_) => "String",
            Type::Number(_) => "Number",
            Type::Boolean(_) => "Boolean",
            Type::Namespace(_) => "Namespace",
            Type::Decorator(_) => "Decorator",
            Type::FunctionType(_) => "FunctionType",
            Type::FunctionParameter(_) => "FunctionParameter",
            Type::StringTemplate(_) => "StringTemplate",
            Type::StringTemplateSpan(_) => "StringTemplateSpan",
        }
    }

    /// Get the name of this type (if it has one)
    pub fn name(&self) -> Option<&str> {
        match self {
            Type::Intrinsic(t) => Some(match t.name {
                IntrinsicTypeName::ErrorType => "ErrorType",
                IntrinsicTypeName::Void => "void",
                IntrinsicTypeName::Never => "never",
                IntrinsicTypeName::Unknown => "unknown",
                IntrinsicTypeName::Null => "null",
            }),
            Type::Model(t) => Some(&t.name),
            Type::ModelProperty(t) => Some(&t.name),
            Type::Interface(t) => Some(&t.name),
            Type::Operation(t) => Some(&t.name),
            Type::Enum(t) => Some(&t.name),
            Type::EnumMember(t) => Some(&t.name),
            Type::Union(t) => Some(&t.name),
            Type::UnionVariant(t) => Some(&t.name),
            Type::Scalar(t) => Some(&t.name),
            Type::TemplateParameter(t) => Some(&t.name),
            Type::Namespace(t) => Some(&t.name),
            Type::Decorator(t) => Some(&t.name),
            Type::FunctionType(t) => Some(&t.name),
            Type::FunctionParameter(t) => Some(&t.name),
            Type::String(t) => Some(&t.value),
            Type::Number(t) => Some(&t.value_as_string),
            Type::Boolean(t) => Some(if t.value { "true" } else { "false" }),
            _ => None,
        }
    }

    /// Set the name on this type (only works for named variants)
    pub fn set_name(&mut self, new_name: String) {
        match self {
            Type::Model(t) => t.name = new_name,
            Type::Scalar(t) => t.name = new_name,
            Type::Interface(t) => t.name = new_name,
            Type::Enum(t) => t.name = new_name,
            Type::Union(t) => t.name = new_name,
            Type::Operation(t) => t.name = new_name,
            Type::Namespace(t) => t.name = new_name,
            Type::Decorator(t) => t.name = new_name,
            Type::FunctionType(t) => t.name = new_name,
            _ => {}
        }
    }

    /// Check if this is an error intrinsic type.
    ///
    /// Note: This only checks the type structure, not whether the TypeId matches
    /// `checker.error_type`. For the full check, use `typekit::type_kind::is_error()`.
    pub fn is_error(&self) -> bool {
        matches!(self, Type::Intrinsic(t) if t.name == IntrinsicTypeName::ErrorType)
    }

    /// Get the node ID from this type, if it has one
    pub fn node_id_from_type(&self) -> Option<NodeId> {
        type_dispatch!(self, t, t.node)
    }

    /// Get the template_node for types that support it
    pub fn template_node(&self) -> Option<NodeId> {
        type_dispatch_partial!(opt self, [Model, Interface, Union, Scalar, Operation], t, t.template_node)
    }

    /// Get the template_mapper for types that support it.
    /// Returns Some if this type is a template instance (was instantiated from a template).
    /// Returns None for template declarations and non-templated types.
    pub fn template_mapper(&self) -> Option<&TypeMapper> {
        type_dispatch_partial!(
            self,
            [Model, Interface, Union, Scalar, Operation],
            t,
            t.template_mapper.as_deref()?
        )
    }

    /// Set the template mapper if not already set. Returns true if set.
    pub fn set_template_mapper_if_none(&mut self, mapper: Box<TypeMapper>) -> bool {
        match self {
            Type::Model(t) if t.template_mapper.is_none() => {
                t.template_mapper = Some(mapper);
                true
            }
            Type::Interface(t) if t.template_mapper.is_none() => {
                t.template_mapper = Some(mapper);
                true
            }
            Type::Union(t) if t.template_mapper.is_none() => {
                t.template_mapper = Some(mapper);
                true
            }
            Type::Scalar(t) if t.template_mapper.is_none() => {
                t.template_mapper = Some(mapper);
                true
            }
            Type::Operation(t) if t.template_mapper.is_none() => {
                t.template_mapper = Some(mapper);
                true
            }
            _ => false,
        }
    }

    /// Get the namespace TypeId for types that belong to a namespace
    pub fn namespace(&self) -> Option<TypeId> {
        type_dispatch_partial!(opt self, [Model, Interface, Operation, Enum, Union, Scalar, Namespace, Decorator, FunctionType], t, t.namespace)
    }

    /// Get the doc comment for this type, if any
    pub fn doc(&self) -> Option<&str> {
        type_dispatch_partial!(opt self, [Model, Interface, Operation, Enum, Scalar, Union, Namespace], t, t.doc.as_deref())
    }

    /// Get the summary comment for this type, if any
    pub fn summary(&self) -> Option<&str> {
        type_dispatch_partial!(opt self, [Model, Interface, Operation, Enum, Scalar, Union], t, t.summary.as_deref())
    }

    /// Check if this type is finished
    pub fn is_finished(&self) -> bool {
        type_dispatch!(self, t, t.is_finished)
    }

    /// Mark this type as finished
    pub fn set_finished(&mut self, value: bool) {
        type_dispatch!(self, t, t.is_finished = value)
    }

    /// Get an immutable reference to this type's decorators, if it has any.
    /// Returns None for types that don't support decorators (Intrinsic, literals, etc.)
    pub fn decorators(&self) -> Option<&Vec<DecoratorApplication>> {
        type_dispatch_partial!(
            self,
            [
                Model,
                ModelProperty,
                Interface,
                Operation,
                Enum,
                EnumMember,
                Union,
                UnionVariant,
                Scalar,
                Namespace
            ],
            t,
            &t.decorators
        )
    }

    /// Get a mutable reference to this type's decorators, if it has any.
    /// Returns None for types that don't support decorators (Intrinsic, literals, etc.)
    pub fn decorators_mut(&mut self) -> Option<&mut Vec<DecoratorApplication>> {
        match self {
            Type::Model(t) => Some(&mut t.decorators),
            Type::ModelProperty(t) => Some(&mut t.decorators),
            Type::Interface(t) => Some(&mut t.decorators),
            Type::Operation(t) => Some(&mut t.decorators),
            Type::Enum(t) => Some(&mut t.decorators),
            Type::EnumMember(t) => Some(&mut t.decorators),
            Type::Union(t) => Some(&mut t.decorators),
            Type::UnionVariant(t) => Some(&mut t.decorators),
            Type::Scalar(t) => Some(&mut t.decorators),
            Type::Namespace(t) => Some(&mut t.decorators),
            _ => None,
        }
    }

    /// Set the type id - used by TypeStore::add to auto-correct id
    pub fn set_id(&mut self, id: TypeId) {
        type_dispatch!(self, t, t.id = id)
    }
}

// ============================================================================
// Type Store
// ============================================================================

/// TypeStore - stores all created types
#[derive(Debug, Clone)]
pub struct TypeStore {
    types: Vec<Type>,
}

impl TypeStore {
    pub fn new() -> Self {
        TypeStore { types: Vec::new() }
    }

    /// Add a type to the store and return its TypeId.
    /// Automatically corrects the type's id to match its position in the store.
    pub fn add(&mut self, mut t: Type) -> TypeId {
        let id = self.types.len() as TypeId;
        t.set_id(id);
        self.types.push(t);
        id
    }

    /// Get a type by its TypeId
    pub fn get(&self, id: TypeId) -> Option<&Type> {
        self.types.get(id as usize)
    }

    /// Get a mutable reference to a type by its TypeId
    pub fn get_mut(&mut self, id: TypeId) -> Option<&mut Type> {
        self.types.get_mut(id as usize)
    }

    /// Get the number of types in the store
    pub fn len(&self) -> usize {
        self.types.len()
    }

    /// Check if the store is empty
    pub fn is_empty(&self) -> bool {
        self.types.is_empty()
    }

    /// Allocate the next TypeId
    pub fn next_type_id(&self) -> TypeId {
        self.types.len() as TypeId
    }
}

impl Default for TypeStore {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Value Types
// ============================================================================

/// Value ID - unique identifier for a value in the value store
pub type ValueId = u32;

/// Value - represents a runtime value in TypeSpec
/// Ported from TS compiler/src/core/types.ts Value types
#[derive(Debug, Clone)]
pub enum Value {
    StringValue(StringValue),
    NumericValue(NumericValue),
    BooleanValue(BooleanValue),
    ObjectValue(ObjectValue),
    ArrayValue(ArrayValue),
    EnumValue(EnumValue),
    NullValue(NullValue),
    ScalarValue(ScalarValue),
    /// Function value - represents a function reference as a value.
    /// Ported from TS FunctionValue.
    FunctionValue(FunctionValueType),
    /// Template value - internal type representing a value in a template declaration.
    /// This should never be exposed on the type graph (unlike TemplateParameter).
    /// Ported from TS TemplateValue.
    TemplateValue(TemplateValue),
}

/// Base trait for all value types
impl Value {
    /// Get the storage type of this value
    pub fn value_type(&self) -> TypeId {
        value_dispatch!(self, v, v.type_id)
    }

    /// Set the storage type of this value.
    /// Used when a const has an explicit type annotation — the value's type
    /// becomes the declared type, not the inferred literal type.
    /// TS: copyValue(value, { type })
    pub fn set_value_type(&mut self, new_type: TypeId) {
        value_dispatch!(self, v, v.type_id = new_type)
    }

    /// Get the kind name of this value
    pub fn value_kind_name(&self) -> &'static str {
        match self {
            Value::StringValue(_) => "StringValue",
            Value::NumericValue(_) => "NumericValue",
            Value::BooleanValue(_) => "BooleanValue",
            Value::ObjectValue(_) => "ObjectValue",
            Value::ArrayValue(_) => "ArrayValue",
            Value::EnumValue(_) => "EnumValue",
            Value::NullValue(_) => "NullValue",
            Value::ScalarValue(_) => "ScalarValue",
            Value::FunctionValue(_) => "FunctionValue",
            Value::TemplateValue(_) => "TemplateValue",
        }
    }
}

/// String value - e.g., const x = "hello"
#[derive(Debug, Clone)]
pub struct StringValue {
    pub type_id: TypeId,
    pub value: String,
    pub scalar: Option<TypeId>,
    pub node: Option<NodeId>,
}

/// Numeric value - e.g., const x = 42
#[derive(Debug, Clone)]
pub struct NumericValue {
    pub type_id: TypeId,
    pub value: f64,
    pub scalar: Option<TypeId>,
    pub node: Option<NodeId>,
}

/// Boolean value - e.g., const x = true
#[derive(Debug, Clone)]
pub struct BooleanValue {
    pub type_id: TypeId,
    pub value: bool,
    pub scalar: Option<TypeId>,
    pub node: Option<NodeId>,
}

/// Object value - e.g., const x = #{ name: "foo" }
#[derive(Debug, Clone)]
pub struct ObjectValue {
    pub type_id: TypeId,
    pub properties: Vec<ObjectValueProperty>,
    pub node: Option<NodeId>,
}

/// Object value property descriptor
#[derive(Debug, Clone)]
pub struct ObjectValueProperty {
    pub name: String,
    pub value: ValueId,
}

/// Array value - e.g., const x = #[1, 2, 3]
#[derive(Debug, Clone)]
pub struct ArrayValue {
    pub type_id: TypeId,
    pub values: Vec<ValueId>,
    pub node: Option<NodeId>,
}

/// Enum value - e.g., Status.active
#[derive(Debug, Clone)]
pub struct EnumValue {
    pub type_id: TypeId,
    pub value: TypeId, // EnumMember TypeId
    pub node: Option<NodeId>,
}

/// Null value - e.g., const x = null
#[derive(Debug, Clone)]
pub struct NullValue {
    pub type_id: TypeId,
    pub node: Option<NodeId>,
}

/// Scalar value - constructed via scalar constructor
#[derive(Debug, Clone)]
pub struct ScalarValue {
    pub type_id: TypeId,
    pub scalar: TypeId,
    pub args: Vec<ValueId>,
    pub node: Option<NodeId>,
}

/// Function value - represents a function reference as a value.
/// Ported from TS FunctionValue interface.
#[derive(Debug, Clone)]
pub struct FunctionValueType {
    pub type_id: TypeId,
    /// Function name (None for anonymous functions)
    pub name: Option<String>,
    pub node: Option<NodeId>,
}

/// Template value - internal type representing a value while in a template declaration.
/// This should never be exposed on the type graph (unlike TemplateParameter).
/// Ported from TS TemplateValue.
#[derive(Debug, Clone)]
pub struct TemplateValue {
    pub type_id: TypeId,
}

// ============================================================================
// Value Store
// ============================================================================

/// ValueStore - stores all created values
#[derive(Debug, Clone, Default)]
pub struct ValueStore {
    values: Vec<Value>,
}

impl ValueStore {
    pub fn new() -> Self {
        ValueStore { values: Vec::new() }
    }

    /// Add a value to the store and return its ValueId
    pub fn add(&mut self, v: Value) -> ValueId {
        let id = self.values.len() as ValueId;
        self.values.push(v);
        id
    }

    /// Get a value by its ValueId
    pub fn get(&self, id: ValueId) -> Option<&Value> {
        self.values.get(id as usize)
    }

    /// Get a mutable reference to a value by its ValueId
    pub fn get_mut(&mut self, id: ValueId) -> Option<&mut Value> {
        self.values.get_mut(id as usize)
    }

    /// Get the number of values in the store
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Check if the store is empty
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Allocate the next ValueId
    pub fn next_value_id(&self) -> ValueId {
        self.values.len() as ValueId
    }
}

// ============================================================================
// Entity (Type | Value | MixedConstraint | Indeterminate)
// ============================================================================

/// Entity - the result of checking a node, which may be a Type, Value,
/// MixedParameterConstraint, or Indeterminate.
/// In TS, Entity = Type | Value | MixedParameterConstraint | IndeterminateEntity.
///
/// "Indeterminate" means the result could be either a type or a value
/// depending on context (e.g., a string literal like "hello" is both
/// the type `"hello"` and the value `"hello"`).
///
/// "MixedConstraint" represents a union expression constraint that includes
/// both type and value parts (e.g., `T extends string | valueof int32`).
#[derive(Debug, Clone)]
pub enum Entity {
    /// A type entity
    Type(TypeId),
    /// A value entity
    Value(ValueId),
    /// A mixed parameter constraint entity
    /// Used when a template parameter constraint includes both type and value parts
    MixedConstraint(MixedParameterConstraint),
    /// An indeterminate entity - could be type or value
    /// The inner TypeId represents the type, which can be converted
    /// to a value if the context requires it
    Indeterminate(TypeId),
}

impl Entity {
    /// Check if this entity is a type
    pub fn is_type(&self) -> bool {
        matches!(self, Entity::Type(_))
    }

    /// Check if this entity is a value
    pub fn is_value(&self) -> bool {
        matches!(self, Entity::Value(_))
    }

    /// Check if this entity is indeterminate
    pub fn is_indeterminate(&self) -> bool {
        matches!(self, Entity::Indeterminate(_))
    }

    /// Check if this entity is a mixed parameter constraint
    pub fn is_mixed_constraint(&self) -> bool {
        matches!(self, Entity::MixedConstraint(_))
    }

    /// Get the TypeId if this is a Type entity
    pub fn as_type_id(&self) -> Option<TypeId> {
        match self {
            Entity::Type(id) => Some(*id),
            Entity::Indeterminate(id) => Some(*id),
            _ => None,
        }
    }

    /// Get the ValueId if this is a Value entity
    pub fn as_value_id(&self) -> Option<ValueId> {
        match self {
            Entity::Value(id) => Some(*id),
            _ => None,
        }
    }
}

// ============================================================================
// Mixed Parameter Constraint
// ============================================================================

/// Mixed parameter constraint - for template parameter constraints
/// that can include both type and value constraints (e.g., `T extends string | valueof int32`)
#[derive(Debug, Clone)]
pub struct MixedParameterConstraint {
    /// The node that defines this constraint
    pub node: Option<NodeId>,
    /// Type constraint (for `T extends SomeType`)
    pub type_constraint: Option<TypeId>,
    /// Value constraint (for `T extends valueof SomeType`)
    pub value_constraint: Option<TypeId>,
}

// ============================================================================
// Decorator Application Types
// ============================================================================

/// DecoratorArgument - a single argument passed to a decorator
#[derive(Debug, Clone)]
pub struct DecoratorArgument {
    /// The value (as a type or literal)
    pub value: NodeId,
    /// The JS-marshalled value for use in JavaScript interop
    pub js_value: Option<DecoratorMarshalledValue>,
    /// Node where this argument appears
    pub node: Option<NodeId>,
}

/// DecoratorMarshalledValue - JS-marshalled values
#[derive(Debug, Clone)]
pub enum DecoratorMarshalledValue {
    Type(NodeId),
    Value(NodeId),
    Record(HashMap<String, NodeId>),
    Array(Vec<NodeId>),
    String(String),
    Number(f64),
    Boolean(bool),
    Null,
}

/// DecoratorApplication - a decorator applied to a declaration
#[derive(Debug, Clone)]
pub struct DecoratorApplication {
    /// The decorator definition (TypeId pointing to a DecoratorType)
    pub definition: Option<TypeId>,
    /// The decorator node ID
    pub decorator: NodeId,
    /// Arguments to the decorator
    pub args: Vec<DecoratorArgument>,
    /// The node where this decorator was applied
    pub node: Option<NodeId>,
}

impl DecoratorApplication {
    pub fn new(decorator: NodeId) -> Self {
        Self {
            definition: None,
            decorator,
            args: Vec::new(),
            node: None,
        }
    }

    pub fn with_args(mut self, args: Vec<DecoratorArgument>) -> Self {
        self.args = args;
        self
    }

    pub fn with_definition(mut self, definition: TypeId) -> Self {
        self.definition = Some(definition);
        self
    }

    pub fn with_node(mut self, node: NodeId) -> Self {
        self.node = Some(node);
        self
    }
}

// ============================================================================
// Type Mapper (template instantiation)
// ============================================================================

/// TemplatedType - a type that was created from a template instantiation.
/// In TS this is a type that has `templateMapper` and `templateNode` fields.
/// In Rust, we track this via the `template_node` field on individual type structs
/// and the TypeMapper stored separately in the Checker.
/// Ported from TS TemplatedType = Model | Interface | Operation | Union | Scalar.
pub type TemplatedTypeId = TypeId;

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // TypeStore tests
    // ========================================================================

    #[test]
    fn test_type_store_new() {
        let store = TypeStore::new();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_type_store_add_intrinsic() {
        let mut store = TypeStore::new();
        let id = store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        }));
        assert_eq!(id, 0);
        assert_eq!(store.len(), 1);
        assert!(store.get(0).is_some());
    }

    #[test]
    fn test_type_store_add_multiple() {
        let mut store = TypeStore::new();
        let id1 = store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        }));
        let id2 = store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Never,
            node: None,
            is_finished: true,
        }));
        let id3 = store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Unknown,
            node: None,
            is_finished: true,
        }));
        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(id3, 2);
        assert_eq!(store.len(), 3);
    }

    #[test]
    fn test_type_store_get() {
        let mut store = TypeStore::new();
        store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        }));
        let t = store.get(0).unwrap();
        assert!(matches!(t, Type::Intrinsic(i) if i.name == IntrinsicTypeName::Void));
    }

    #[test]
    fn test_type_store_get_invalid() {
        let store = TypeStore::new();
        assert!(store.get(0).is_none());
        assert!(store.get(999).is_none());
    }

    #[test]
    fn test_type_store_next_type_id() {
        let mut store = TypeStore::new();
        assert_eq!(store.next_type_id(), 0);
        store.add(Type::Intrinsic(IntrinsicType {
            id: INVALID_TYPE_ID,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        }));
        assert_eq!(store.next_type_id(), 1);
    }

    #[test]
    fn test_type_store_get_mut() {
        let mut store = TypeStore::new();
        store.add(Type::Model(ModelType {
            id: INVALID_TYPE_ID,
            name: "Foo".to_string(),
            node: None,
            properties: HashMap::new(),
            property_names: vec![],
            indexer: None,
            base_model: None,
            derived_models: vec![],
            source_model: None,
            source_models: vec![],
            namespace: None,
            decorators: vec![],
            template_node: None,
            template_mapper: None,
            doc: None,
            summary: None,
            is_finished: false,
        }));
        if let Some(Type::Model(m)) = store.get_mut(0) {
            m.is_finished = true;
        }
        let t = store.get(0).unwrap();
        if let Type::Model(m) = t {
            assert!(m.is_finished);
        } else {
            panic!("Expected Model type");
        }
    }

    // ========================================================================
    // Type method tests
    // ========================================================================

    #[test]
    fn test_type_id() {
        let t = Type::Intrinsic(IntrinsicType {
            id: 5,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        });
        assert_eq!(t.id(), 5);
    }

    #[test]
    fn test_type_kind_name() {
        let t = Type::Intrinsic(IntrinsicType {
            id: 0,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        });
        assert_eq!(t.kind_name(), "Intrinsic");

        let m = Type::Model(ModelType {
            id: 0,
            name: "Foo".to_string(),
            node: None,
            properties: HashMap::new(),
            property_names: vec![],
            indexer: None,
            base_model: None,
            derived_models: vec![],
            source_model: None,
            source_models: vec![],
            namespace: None,
            decorators: vec![],
            template_node: None,
            template_mapper: None,
            doc: None,
            summary: None,
            is_finished: true,
        });
        assert_eq!(m.kind_name(), "Model");
    }

    #[test]
    fn test_type_name() {
        let t = Type::Intrinsic(IntrinsicType {
            id: 0,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        });
        assert_eq!(t.name(), Some("void"));

        let m = Type::Model(ModelType {
            id: 0,
            name: "Foo".to_string(),
            node: None,
            properties: HashMap::new(),
            property_names: vec![],
            indexer: None,
            base_model: None,
            derived_models: vec![],
            source_model: None,
            source_models: vec![],
            namespace: None,
            decorators: vec![],
            template_node: None,
            template_mapper: None,
            doc: None,
            summary: None,
            is_finished: true,
        });
        assert_eq!(m.name(), Some("Foo"));
    }

    #[test]
    fn test_type_is_error() {
        let err = Type::Intrinsic(IntrinsicType {
            id: 0,
            name: IntrinsicTypeName::ErrorType,
            node: None,
            is_finished: true,
        });
        assert!(err.is_error());

        let void = Type::Intrinsic(IntrinsicType {
            id: 0,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        });
        assert!(!void.is_error());
    }

    #[test]
    fn test_type_is_finished() {
        let t = Type::Intrinsic(IntrinsicType {
            id: 0,
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        });
        assert!(t.is_finished());
    }

    // ========================================================================
    // Entity tests
    // ========================================================================

    #[test]
    fn test_entity_type() {
        let e = Entity::Type(5);
        assert!(e.is_type());
        assert!(!e.is_value());
        assert!(!e.is_indeterminate());
        assert_eq!(e.as_type_id(), Some(5));
        assert_eq!(e.as_value_id(), None);
    }

    #[test]
    fn test_entity_value() {
        let e = Entity::Value(10);
        assert!(!e.is_type());
        assert!(e.is_value());
        assert!(!e.is_indeterminate());
        assert_eq!(e.as_type_id(), None);
        assert_eq!(e.as_value_id(), Some(10));
    }

    #[test]
    fn test_entity_indeterminate() {
        let e = Entity::Indeterminate(3);
        assert!(!e.is_type());
        assert!(!e.is_value());
        assert!(e.is_indeterminate());
        assert_eq!(e.as_type_id(), Some(3));
        assert_eq!(e.as_value_id(), None);
    }

    // ========================================================================
    // ValueStore tests
    // ========================================================================

    #[test]
    fn test_value_store_new() {
        let store = ValueStore::new();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_value_store_add_string() {
        let mut store = ValueStore::new();
        let id = store.add(Value::StringValue(StringValue {
            type_id: 0,
            value: "hello".to_string(),
            scalar: None,
            node: None,
        }));
        assert_eq!(id, 0);
        assert_eq!(store.len(), 1);
    }

    #[test]
    fn test_value_store_add_numeric() {
        let mut store = ValueStore::new();
        let id = store.add(Value::NumericValue(NumericValue {
            type_id: 0,
            value: 42.0,
            scalar: None,
            node: None,
        }));
        assert_eq!(id, 0);
    }

    #[test]
    fn test_value_store_add_boolean() {
        let mut store = ValueStore::new();
        let id = store.add(Value::BooleanValue(BooleanValue {
            type_id: 0,
            value: true,
            scalar: None,
            node: None,
        }));
        assert_eq!(id, 0);
    }

    #[test]
    fn test_value_store_add_null() {
        let mut store = ValueStore::new();
        let id = store.add(Value::NullValue(NullValue {
            type_id: 0,
            node: None,
        }));
        assert_eq!(id, 0);
    }

    #[test]
    fn test_value_type() {
        let v = Value::StringValue(StringValue {
            type_id: 5,
            value: "test".to_string(),
            scalar: None,
            node: None,
        });
        assert_eq!(v.value_type(), 5);
    }

    #[test]
    fn test_value_kind_name() {
        assert_eq!(
            Value::NullValue(NullValue {
                type_id: 0,
                node: None
            })
            .value_kind_name(),
            "NullValue"
        );
        assert_eq!(
            Value::BooleanValue(BooleanValue {
                type_id: 0,
                value: true,
                scalar: None,
                node: None
            })
            .value_kind_name(),
            "BooleanValue"
        );
    }

    // ========================================================================
    // DecoratorApplication tests
    // ========================================================================

    #[test]
    fn test_decorator_application_new() {
        let da = DecoratorApplication::new(1);
        assert_eq!(da.decorator, 1);
        assert!(da.definition.is_none());
        assert!(da.args.is_empty());
        assert!(da.node.is_none());
    }

    #[test]
    fn test_decorator_application_builder() {
        let da = DecoratorApplication::new(1).with_definition(2).with_node(3);
        assert_eq!(da.decorator, 1);
        assert_eq!(da.definition, Some(2));
        assert_eq!(da.node, Some(3));
    }

    // ========================================================================
    // IntrinsicTypeName tests
    // ========================================================================

    #[test]
    fn test_intrinsic_type_names() {
        assert_eq!(IntrinsicTypeName::ErrorType as u8, 0);
        assert_eq!(IntrinsicTypeName::Void as u8, 1);
        assert_eq!(IntrinsicTypeName::Never as u8, 2);
        assert_eq!(IntrinsicTypeName::Unknown as u8, 3);
        assert_eq!(IntrinsicTypeName::Null as u8, 4);
    }

    #[test]
    fn test_string_type() {
        let t = Type::String(StringType {
            id: 1,
            value: "hello".to_string(),
            node: None,
            is_finished: true,
        });
        assert_eq!(t.kind_name(), "String");
        assert_eq!(t.name(), Some("hello"));
    }

    #[test]
    fn test_numeric_type() {
        let t = Type::Number(NumericType {
            id: 2,
            value: 3.15,
            value_as_string: "3.15".to_string(),
            node: None,
            is_finished: true,
        });
        assert_eq!(t.kind_name(), "Number");
        assert_eq!(t.name(), Some("3.15"));
    }

    #[test]
    fn test_boolean_type() {
        let t = Type::Boolean(BooleanType {
            id: 3,
            value: true,
            node: None,
            is_finished: true,
        });
        assert_eq!(t.kind_name(), "Boolean");
        assert_eq!(t.name(), Some("true"));
    }
}