draco-io 0.2.0

Rust IO helpers for Draco geometry compression formats
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
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
//! Document-preserving glTF Draco compression.
//!
//! [`compress_gltf_bytes`] takes a self-contained glTF or GLB document and
//! returns a copy whose triangle-mesh geometry is compressed with
//! `KHR_draco_mesh_compression`, while **everything else in the document is
//! carried through untouched**: materials, textures, images, samplers, cameras,
//! nodes, animations, skins, `extras`, and unknown extension JSON that has no
//! opaque binary references. Unknown buffer/view/offset-like extension fields
//! are rejected because they cannot be remapped safely.
//!
//! This is the key difference from the `read meshes -> write fresh glTF` path,
//! which only models geometry and therefore drops materials and other content.
//! Here we mutate the original JSON document in place and only touch the parts
//! that change: the compressed primitives, their geometry accessors, the
//! buffer, and the buffer views.
//!
//! # What gets compressed
//!
//! A primitive is compressed only when its structure can be reproduced. The
//! default quantization is intentionally lossy; set an attribute class to
//! `None` in [`QuantizationOptions`] to disable its quantization:
//!
//! - triangle list (`mode` 4 or absent), indexed or non-indexed (a fresh
//!   indices accessor is generated for the non-indexed case),
//! - not already Draco-compressed,
//! - its geometry accessors are not shared with any other primitive,
//! - decoding and re-encoding succeed and reproduce the exact attribute set.
//!
//! All standard attribute semantics are compressed, including `TANGENT`,
//! `JOINTS_n`, `WEIGHTS_n`, multiple `TEXCOORD_n`/`COLOR_n`, and custom `_*`
//! attributes: non-standard ones ride along inside the Draco stream as generic
//! attributes and are named by the extension's attribute map (the glTF semantic
//! lives in the map, not in the Draco attribute). Skinned and tangent-bearing
//! meshes are therefore compressed, not just preserved.
//!
//! Primitives that fall outside this scope — already Draco, sharing geometry
//! accessors, sparse accessors, or an attribute layout the encoder rejects —
//! are left uncompressed but fully preserved, along with the rest of the
//! document.
//!
//! Non-triangle primitives are likewise left uncompressed, and this is required
//! by the spec, not a limitation: `KHR_draco_mesh_compression` restricts the
//! primitive `mode` to `TRIANGLES` or `TRIANGLE_STRIP` ("Restrictions on
//! geometry type"), so point clouds (`POINTS`) and line modes cannot be
//! Draco-compressed in glTF at all. (Only `TRIANGLES` is compressed here;
//! `TRIANGLE_STRIP` is allowed by the spec but uncommon and left as-is.)

use std::collections::{BTreeSet, HashMap};
#[cfg(feature = "gltf-reader")]
use std::path::Path;

use draco_core::decoder_buffer::DecoderBuffer;
use draco_core::mesh::Mesh;
use draco_core::mesh_decoder::MeshDecoder;
use serde_json::{Map, Value};

pub use crate::gltf_container::OutputFormat;
#[cfg(feature = "gltf-reader")]
use crate::gltf_container::{
    parse_gltf_container, serialize_gltf_document, FileResourceResolver, ResourceLimits,
    ResourceResolver,
};
use crate::gltf_geometry::{
    component_type_for_data_type, gltf_type_for_num_components, validate_semantic_accessor,
    GltfError,
};
// The byte API parses + resolves buffers through the reader; the in-memory
// `compress_gltf_value` does not need it.
use crate::gltf_khr_draco::{parse_khr_draco_mesh_compression, validate_khr_draco_document};
#[cfg(feature = "gltf-reader")]
use crate::gltf_reader::GltfReader;
use crate::gltf_writer::encode_draco_mesh_with_info;

type Result<T> = std::result::Result<T, GltfError>;

const KHR_DRACO: &str = "KHR_draco_mesh_compression";
const MODE_TRIANGLES: u64 = 4;
const MODE_TRIANGLE_STRIP: u64 = 5;

/// Draco mesh encoding method selection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum EncodingMethod {
    /// Let the encoder choose based on the geometry and speed settings.
    #[default]
    Auto,
    /// Force the sequential mesh encoder.
    Sequential,
    /// Force the EdgeBreaker mesh encoder.
    Edgebreaker,
}

/// Per-attribute quantization. `None` disables quantization for that class.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QuantizationOptions {
    pub position: Option<u8>,
    pub normal: Option<u8>,
    pub color: Option<u8>,
    pub texcoord: Option<u8>,
    pub generic: Option<u8>,
}

impl Default for QuantizationOptions {
    fn default() -> Self {
        Self {
            position: Some(14),
            normal: Some(10),
            color: Some(8),
            texcoord: Some(12),
            generic: Some(8),
        }
    }
}

/// Options shared by the document compressor and geometry writer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GltfCompressionOptions {
    pub quantization: QuantizationOptions,
    pub encoding_speed: u8,
    pub decoding_speed: u8,
    pub encoding_method: EncodingMethod,
    pub output_format: OutputFormat,
}

impl GltfCompressionOptions {
    /// Validate all numeric ranges without clamping.
    pub fn validate(&self) -> Result<()> {
        if self.encoding_speed > 10 {
            return Err(GltfError::InvalidOptions(format!(
                "encoding_speed {} is outside 0..=10",
                self.encoding_speed
            )));
        }
        if self.decoding_speed > 10 {
            return Err(GltfError::InvalidOptions(format!(
                "decoding_speed {} is outside 0..=10",
                self.decoding_speed
            )));
        }
        validate_quantization("position", self.quantization.position, 1, 31)?;
        validate_quantization("normal", self.quantization.normal, 2, 30)?;
        validate_quantization("color", self.quantization.color, 1, 31)?;
        validate_quantization("texcoord", self.quantization.texcoord, 1, 31)?;
        validate_quantization("generic", self.quantization.generic, 1, 31)?;
        Ok(())
    }
}

impl Default for GltfCompressionOptions {
    fn default() -> Self {
        Self {
            quantization: QuantizationOptions::default(),
            encoding_speed: 5,
            decoding_speed: 5,
            encoding_method: EncodingMethod::Auto,
            output_format: OutputFormat::SameAsInput,
        }
    }
}

fn validate_quantization(name: &str, bits: Option<u8>, min: u8, max: u8) -> Result<()> {
    if bits.is_some_and(|bits| !(min..=max).contains(&bits)) {
        return Err(GltfError::InvalidOptions(format!(
            "{name} quantization must be None or {min}..={max}"
        )));
    }
    Ok(())
}

/// Stable location of a primitive in a glTF document.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PrimitiveLocation {
    pub mesh: usize,
    pub primitive: usize,
}

/// Why a valid primitive was preserved instead of compressed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PreserveReason {
    AlreadyDraco,
    UnsupportedMode { mode: u32 },
    UnsupportedLayout { detail: String },
    SparseAccessor { accessor: usize },
    MorphTargets,
    SharedAccessor { accessor: usize },
}

/// One preserved primitive and its typed reason.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PreservedPrimitive {
    pub primitive: PrimitiveLocation,
    pub reason: PreserveReason,
}

/// Primitive-by-primitive result of compression.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CompressionReport {
    pub compressed_primitives: Vec<PrimitiveLocation>,
    pub preserved_primitives: Vec<PreservedPrimitive>,
}

/// Data plus its compression report.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompressionOutput<T> {
    pub data: T,
    pub report: CompressionReport,
}

fn json_index(value: &Value, label: &str) -> Result<usize> {
    value
        .as_u64()
        .and_then(|value| usize::try_from(value).ok())
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} is not a valid index")))
}

/// Compress the geometry of a self-contained glTF/GLB document with Draco,
/// preserving all other document content.
///
/// `input` may be GLB bytes or glTF JSON whose buffers/images are embedded as
/// data URIs (use [`compress_gltf_bytes_with_base_path`] for external files).
/// The output container matches the input (GLB in -> GLB out, glTF in -> glTF
/// out with an embedded buffer).
#[cfg(feature = "gltf-reader")]
pub fn compress_gltf_bytes(input: &[u8]) -> Result<CompressionOutput<Vec<u8>>> {
    compress_gltf_bytes_with_options(input, &GltfCompressionOptions::default())
}

/// Compress with explicit options and no external file resolver.
#[cfg(feature = "gltf-reader")]
pub fn compress_gltf_bytes_with_options(
    input: &[u8],
    options: &GltfCompressionOptions,
) -> Result<CompressionOutput<Vec<u8>>> {
    compress_gltf_bytes_impl(input, None, &ResourceLimits::default(), options)
}

/// Like [`compress_gltf_bytes`], but resolves external buffers/`.bin` files
/// relative to `base_path`.
#[cfg(feature = "gltf-reader")]
pub fn compress_gltf_bytes_with_base_path(
    input: &[u8],
    base_path: Option<&Path>,
    options: &GltfCompressionOptions,
) -> Result<CompressionOutput<Vec<u8>>> {
    let resolver =
        base_path.map(|base| FileResourceResolver::new(base, crate::ExternalFilePolicy::Allow));
    compress_gltf_bytes_impl(
        input,
        resolver
            .as_ref()
            .map(|resolver| resolver as &dyn ResourceResolver),
        &ResourceLimits::default(),
        options,
    )
}

/// Compress using a caller-provided resource resolver and quotas.
#[cfg(feature = "gltf-reader")]
pub fn compress_gltf_bytes_with_resolver(
    input: &[u8],
    resolver: &dyn ResourceResolver,
    limits: &ResourceLimits,
    options: &GltfCompressionOptions,
) -> Result<CompressionOutput<Vec<u8>>> {
    compress_gltf_bytes_impl(input, Some(resolver), limits, options)
}

#[cfg(feature = "gltf-reader")]
fn compress_gltf_bytes_impl(
    input: &[u8],
    resolver: Option<&dyn ResourceResolver>,
    limits: &ResourceLimits,
    options: &GltfCompressionOptions,
) -> Result<CompressionOutput<Vec<u8>>> {
    options.validate()?;
    let container = parse_gltf_container(input)?;
    let doc = serde_json::from_slice::<Value>(container.json)?;

    // Reuse the reader (lenient: do not reject skins/animations/morph targets,
    // we only preserve them) for geometry decoding and resolved buffer bytes.
    let reader = if let Some(resolver) = resolver {
        GltfReader::from_bytes_lenient_with_resolver(input, resolver, limits)?
    } else {
        GltfReader::from_bytes_lenient(input)?
    };
    let compressed = compress_gltf_value(doc, reader.buffers(), options, |mesh, prim| {
        reader.decode_primitive_with_semantics(mesh, prim)
    })?;
    let (doc, bin) = compressed.data;
    let data = serialize_gltf_document(&doc, &bin, container.format, options.output_format)?;
    Ok(CompressionOutput {
        data,
        report: compressed.report,
    })
}

/// The document-preserving compression core: transforms a parsed glTF document
/// (`doc`) in place, returning the mutated document plus the new single binary
/// blob. Geometry decoding is supplied by `decode`, so callers that already have
/// a parsed scene (e.g. a `gltf-rs` document) can compress without re-parsing
/// through the byte API.
///
/// `decode(mesh_index, primitive_index)` returns the primitive's geometry as a
/// [`draco_core::Mesh`] plus the `(glTF semantic, Draco attribute id)` mapping;
/// an `Err` marks the primitive as not compressible (it is preserved). `buffers`
/// holds the resolved bytes for each glTF buffer (used for repacking the
/// non-compressed buffer views).
///
/// The returned document's single buffer carries `byteLength` but no URI; the
/// caller embeds the returned `bin` (as a GLB BIN chunk or a data URI).
pub fn compress_gltf_value<F>(
    mut doc: Value,
    buffers: &[Vec<u8>],
    options: &GltfCompressionOptions,
    decode: F,
) -> Result<CompressionOutput<(Value, Vec<u8>)>>
where
    F: Fn(usize, usize) -> Result<(draco_core::Mesh, Vec<(String, u32)>)>,
{
    if !doc.is_object() {
        return Err(GltfError::InvalidGltf("glTF root is not an object".into()));
    }
    options.validate()?;
    validate_gltf_document_for_repacking(&doc, buffers)?;

    // Reference-count accessor usage across every primitive so we only mutate
    // accessors that belong exclusively to a single primitive we compress.
    let accessor_users = count_accessor_users(&doc)?;

    let (plans, mut report) = build_plans(&doc, buffers, &decode, &accessor_users, options)?;

    // Mutate accessors of compressed primitives: drop their buffer view and set
    // the count to the Draco-encoded value. Done before scanning for orphans so
    // the now-unreferenced geometry buffer views fall out naturally.
    apply_accessor_mutations(&mut doc, &plans)?;

    // Non-indexed primitives need a generated indices accessor (Draco glTF
    // primitives are indexed).
    add_generated_indices(&mut doc, &plans)?;

    // Repack the binary: keep only buffer views still referenced by the JSON,
    // append one Draco buffer view per compressed primitive, and reindex every
    // buffer-view reference in the document.
    let repack = repack_buffers(&mut doc, buffers, &plans)?;

    // Write the Draco extension onto each compressed primitive (after reindex,
    // so the freshly appended buffer-view indices are not remapped).
    for (i, plan) in plans.iter().enumerate() {
        let draco_bv = repack.draco_buffer_views[i];
        set_primitive_draco_extension(&mut doc, plan, draco_bv)?;
    }

    if !plans.is_empty() {
        ensure_extension_listed(&mut doc, "extensionsUsed")?;
        ensure_extension_listed(&mut doc, "extensionsRequired")?;
    }

    set_single_buffer(&mut doc, repack.bin.len())?;

    report.compressed_primitives = plans
        .iter()
        .map(|plan| PrimitiveLocation {
            mesh: plan.mesh_idx,
            primitive: plan.prim_idx,
        })
        .collect();
    Ok(CompressionOutput {
        data: (doc, repack.bin),
        report,
    })
}

/// Consolidate resolved glTF buffers using the same known-reference and opaque
/// extension policy as the compressor.
pub fn consolidate_gltf_buffers(
    mut document: Value,
    buffers: &[Vec<u8>],
) -> Result<(Value, Vec<u8>)> {
    if !document.is_object() {
        return Err(GltfError::InvalidGltf("glTF root is not an object".into()));
    }
    validate_gltf_document_for_repacking(&document, buffers)?;
    let repack = repack_buffers(&mut document, buffers, &[])?;
    set_single_buffer(&mut document, repack.bin.len())?;
    Ok((document, repack.bin))
}

#[derive(Clone, Copy)]
struct ValidatedBufferView {
    buffer: usize,
    byte_offset: usize,
    byte_length: usize,
    byte_stride: Option<usize>,
}

/// Validate binary declarations and every accessor reference before deciding
/// whether a primitive is compressible. Preserve reasons are only for valid
/// but unsupported data; malformed sparse/morph/shared geometry must never be
/// converted into a successful report entry.
/// Validate a parsed document before any binary consolidation or compression.
///
/// This combines strict KHR Draco validation, conservative opaque-reference
/// rejection, accessor/reference validation, and checked buffer-view bounds.
pub fn validate_gltf_document_for_repacking(document: &Value, buffers: &[Vec<u8>]) -> Result<()> {
    validate_khr_draco_document(document)?;
    reject_opaque_binary_references(document)?;
    validate_gltf_document_binary_layout(document, buffers)
}

/// Validate all glTF binary declarations and accessor references.
pub fn validate_gltf_document_binary_layout(document: &Value, buffers: &[Vec<u8>]) -> Result<()> {
    let declared_buffers = optional_array(document, "buffers")?;
    if declared_buffers.len() != buffers.len() {
        return Err(GltfError::InvalidGltf(format!(
            "document declares {} buffers but {} were resolved",
            declared_buffers.len(),
            buffers.len()
        )));
    }
    for (index, declaration) in declared_buffers.iter().enumerate() {
        let declaration = declaration
            .as_object()
            .ok_or_else(|| GltfError::InvalidGltf(format!("buffer {index} is not an object")))?;
        let byte_length = required_usize(declaration, "byteLength", "buffer")?;
        let actual = buffers[index].len();
        if actual < byte_length {
            return Err(GltfError::InvalidGltf(format!(
                "buffer {index} byteLength {byte_length} exceeds resolved length {actual}"
            )));
        }
    }

    let view_values = optional_array(document, "bufferViews")?;
    let mut views = Vec::new();
    views.try_reserve_exact(view_values.len()).map_err(|_| {
        GltfError::ResourceLimitExceeded("bufferView validation allocation failed".into())
    })?;
    for (index, value) in view_values.iter().enumerate() {
        let view = value.as_object().ok_or_else(|| {
            GltfError::InvalidGltf(format!("bufferView {index} is not an object"))
        })?;
        let buffer = required_usize(view, "buffer", "bufferView")?;
        let byte_offset = optional_usize(view, "byteOffset", "bufferView")?.unwrap_or(0);
        let byte_length = required_usize(view, "byteLength", "bufferView")?;
        let byte_stride = optional_usize(view, "byteStride", "bufferView")?;
        if let Some(stride) = byte_stride {
            if !(4..=252).contains(&stride) || !stride.is_multiple_of(4) {
                return Err(GltfError::InvalidGltf(format!(
                    "bufferView {index} byteStride must be a multiple of 4 in 4..=252"
                )));
            }
        }
        let buffer_data = buffers.get(buffer).ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "bufferView {index} references invalid buffer {buffer}"
            ))
        })?;
        let end = byte_offset
            .checked_add(byte_length)
            .filter(|end| *end <= buffer_data.len())
            .ok_or_else(|| GltfError::InvalidGltf(format!("bufferView {index} is out of range")))?;
        let _ = end;
        views.push(ValidatedBufferView {
            buffer,
            byte_offset,
            byte_length,
            byte_stride,
        });
    }

    let accessor_values = optional_array(document, "accessors")?;
    for (index, value) in accessor_values.iter().enumerate() {
        validate_accessor(index, value, &views, buffers)?;
    }
    validate_primitive_accessor_contracts(document, accessor_values, &views, buffers)?;
    validate_accessor_references(document, accessor_values.len())
}

fn optional_array<'a>(document: &'a Value, key: &str) -> Result<&'a [Value]> {
    match document.get(key) {
        Some(value) => value
            .as_array()
            .map(Vec::as_slice)
            .ok_or_else(|| GltfError::InvalidGltf(format!("{key} is not an array"))),
        None => Ok(&[]),
    }
}

fn required_usize(object: &Map<String, Value>, key: &str, label: &str) -> Result<usize> {
    let value = object
        .get(key)
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} is missing {key}")))?;
    json_index(value, &format!("{label}.{key}"))
}

fn optional_usize(object: &Map<String, Value>, key: &str, label: &str) -> Result<Option<usize>> {
    object
        .get(key)
        .map(|value| json_index(value, &format!("{label}.{key}")))
        .transpose()
}

fn component_size(component_type: u64, label: &str) -> Result<usize> {
    match component_type {
        5120 | 5121 => Ok(1),
        5122 | 5123 | 5131 => Ok(2),
        5124..=5126 => Ok(4),
        5130 | 5132 | 5133 => Ok(8),
        _ => Err(GltfError::InvalidGltf(format!(
            "{label} has invalid componentType {component_type}"
        ))),
    }
}

fn accessor_element_size(accessor_type: &str, component_size: usize) -> Result<usize> {
    let (columns, rows) = match accessor_type {
        "SCALAR" => (1usize, 1usize),
        "VEC2" => (1, 2),
        "VEC3" => (1, 3),
        "VEC4" => (1, 4),
        "MAT2" => (2, 2),
        "MAT3" => (3, 3),
        "MAT4" => (4, 4),
        _ => {
            return Err(GltfError::InvalidGltf(format!(
                "accessor has invalid type {accessor_type}"
            )));
        }
    };
    let column_size = rows
        .checked_mul(component_size)
        .ok_or_else(|| GltfError::InvalidGltf("accessor element size overflow".into()))?;
    let column_stride = if columns > 1 && component_size < 4 {
        column_size
            .checked_add(3)
            .map(|size| size / 4 * 4)
            .ok_or_else(|| GltfError::InvalidGltf("matrix element size overflow".into()))?
    } else {
        column_size
    };
    columns
        .checked_mul(column_stride)
        .ok_or_else(|| GltfError::InvalidGltf("accessor element size overflow".into()))
}

fn validate_range_in_view(
    view: ValidatedBufferView,
    byte_offset: usize,
    count: usize,
    element_size: usize,
    stride: usize,
    label: &str,
) -> Result<()> {
    if stride < element_size {
        return Err(GltfError::InvalidGltf(format!(
            "{label} stride {stride} is smaller than element size {element_size}"
        )));
    }
    let byte_length = count
        .checked_sub(1)
        .and_then(|prefix| prefix.checked_mul(stride))
        .and_then(|prefix| prefix.checked_add(element_size))
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} byte range overflow")))?;
    let end = byte_offset
        .checked_add(byte_length)
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} byte range overflow")))?;
    if end > view.byte_length {
        return Err(GltfError::InvalidGltf(format!(
            "{label} does not fit its bufferView"
        )));
    }
    Ok(())
}

fn validate_accessor(
    index: usize,
    value: &Value,
    views: &[ValidatedBufferView],
    buffers: &[Vec<u8>],
) -> Result<()> {
    let accessor = value
        .as_object()
        .ok_or_else(|| GltfError::InvalidGltf(format!("accessor {index} is not an object")))?;
    let component_type = accessor
        .get("componentType")
        .and_then(Value::as_u64)
        .ok_or_else(|| {
            GltfError::InvalidGltf(format!("accessor {index} has invalid componentType"))
        })?;
    let component_size = component_size(component_type, &format!("accessor {index}"))?;
    let accessor_type = accessor
        .get("type")
        .and_then(Value::as_str)
        .ok_or_else(|| GltfError::InvalidGltf(format!("accessor {index} has invalid type")))?;
    let element_size = accessor_element_size(accessor_type, component_size)?;
    let count = required_usize(accessor, "count", &format!("accessor {index}"))?;
    if count == 0 {
        return Err(GltfError::InvalidGltf(format!(
            "accessor {index} count must be greater than zero"
        )));
    }
    if accessor
        .get("normalized")
        .is_some_and(|normalized| !normalized.is_boolean())
    {
        return Err(GltfError::InvalidGltf(format!(
            "accessor {index}.normalized is not a boolean"
        )));
    }
    let byte_offset = optional_usize(accessor, "byteOffset", "accessor")?.unwrap_or(0);
    if !byte_offset.is_multiple_of(component_size) {
        return Err(GltfError::InvalidGltf(format!(
            "accessor {index} byteOffset is not component-aligned"
        )));
    }
    if let Some(view_index) = optional_usize(accessor, "bufferView", "accessor")? {
        let view = *views.get(view_index).ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "accessor {index} references invalid bufferView {view_index}"
            ))
        })?;
        let stride = view.byte_stride.unwrap_or(element_size);
        validate_range_in_view(
            view,
            byte_offset,
            count,
            element_size,
            stride,
            &format!("accessor {index}"),
        )?;
    } else if byte_offset != 0 {
        return Err(GltfError::InvalidGltf(format!(
            "accessor {index} has byteOffset without bufferView"
        )));
    }

    if let Some(sparse) = accessor.get("sparse") {
        validate_sparse_accessor(index, sparse, count, element_size, views, buffers)?;
    }
    Ok(())
}

fn validate_sparse_accessor(
    accessor_index: usize,
    sparse: &Value,
    accessor_count: usize,
    element_size: usize,
    views: &[ValidatedBufferView],
    buffers: &[Vec<u8>],
) -> Result<()> {
    let sparse = sparse.as_object().ok_or_else(|| {
        GltfError::InvalidGltf(format!("accessor {accessor_index}.sparse is not an object"))
    })?;
    let count = required_usize(sparse, "count", "sparse accessor")?;
    if count == 0 || count > accessor_count {
        return Err(GltfError::InvalidGltf(format!(
            "accessor {accessor_index} has invalid sparse count {count}"
        )));
    }

    let indices = sparse
        .get("indices")
        .and_then(Value::as_object)
        .ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "accessor {accessor_index}.sparse.indices is not an object"
            ))
        })?;
    let indices_view_index = required_usize(indices, "bufferView", "sparse indices")?;
    let indices_view = *views.get(indices_view_index).ok_or_else(|| {
        GltfError::InvalidGltf(format!(
            "sparse indices references invalid bufferView {indices_view_index}"
        ))
    })?;
    if indices_view.byte_stride.is_some() {
        return Err(GltfError::InvalidGltf(
            "sparse indices bufferView must not define byteStride".into(),
        ));
    }
    let index_component = indices
        .get("componentType")
        .and_then(Value::as_u64)
        .ok_or_else(|| GltfError::InvalidGltf("sparse indices componentType is invalid".into()))?;
    let index_size = match index_component {
        5121 => 1,
        5123 => 2,
        5125 => 4,
        _ => {
            return Err(GltfError::InvalidGltf(format!(
                "sparse indices has invalid componentType {index_component}"
            )));
        }
    };
    let indices_offset = optional_usize(indices, "byteOffset", "sparse indices")?.unwrap_or(0);
    if !indices_offset.is_multiple_of(index_size) {
        return Err(GltfError::InvalidGltf(
            "sparse indices byteOffset is not component-aligned".into(),
        ));
    }
    validate_range_in_view(
        indices_view,
        indices_offset,
        count,
        index_size,
        index_size,
        "sparse indices",
    )?;
    let indices_buffer = buffers
        .get(indices_view.buffer)
        .ok_or_else(|| GltfError::InvalidGltf("sparse indices buffer is out of range".into()))?;
    let indices_start = indices_view
        .byte_offset
        .checked_add(indices_offset)
        .ok_or_else(|| GltfError::InvalidGltf("sparse indices offset overflow".into()))?;
    let mut previous = None;
    for sparse_index in 0..count {
        let offset = sparse_index
            .checked_mul(index_size)
            .and_then(|offset| indices_start.checked_add(offset))
            .ok_or_else(|| GltfError::InvalidGltf("sparse indices offset overflow".into()))?;
        let end = offset
            .checked_add(index_size)
            .ok_or_else(|| GltfError::InvalidGltf("sparse indices offset overflow".into()))?;
        let bytes = indices_buffer
            .get(offset..end)
            .ok_or_else(|| GltfError::InvalidGltf("sparse indices are out of range".into()))?;
        let value = match index_component {
            5121 => bytes[0] as u32,
            5123 => u16::from_le_bytes([bytes[0], bytes[1]]) as u32,
            5125 => u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
            _ => {
                return Err(GltfError::InvalidGltf(
                    "sparse indices component type changed after validation".into(),
                ));
            }
        } as usize;
        if value >= accessor_count {
            return Err(GltfError::InvalidGltf(format!(
                "accessor {accessor_index} sparse index {value} is out of range"
            )));
        }
        if previous.is_some_and(|previous| value <= previous) {
            return Err(GltfError::InvalidGltf(format!(
                "accessor {accessor_index} sparse indices are not strictly increasing"
            )));
        }
        previous = Some(value);
    }

    let values = sparse
        .get("values")
        .and_then(Value::as_object)
        .ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "accessor {accessor_index}.sparse.values is not an object"
            ))
        })?;
    let values_view_index = required_usize(values, "bufferView", "sparse values")?;
    let values_view = *views.get(values_view_index).ok_or_else(|| {
        GltfError::InvalidGltf(format!(
            "sparse values references invalid bufferView {values_view_index}"
        ))
    })?;
    if values_view.byte_stride.is_some() {
        return Err(GltfError::InvalidGltf(
            "sparse values bufferView must not define byteStride".into(),
        ));
    }
    let values_offset = optional_usize(values, "byteOffset", "sparse values")?.unwrap_or(0);
    validate_range_in_view(
        values_view,
        values_offset,
        count,
        element_size,
        element_size,
        "sparse values",
    )
}

fn validate_primitive_accessor_contracts(
    document: &Value,
    accessors: &[Value],
    views: &[ValidatedBufferView],
    buffers: &[Vec<u8>],
) -> Result<()> {
    for (mesh_index, mesh) in optional_array(document, "meshes")?.iter().enumerate() {
        let primitives = mesh
            .get("primitives")
            .and_then(Value::as_array)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("mesh {mesh_index}.primitives is not an array"))
            })?;
        for (primitive_index, primitive) in primitives.iter().enumerate() {
            let primitive = primitive.as_object().ok_or_else(|| {
                GltfError::InvalidGltf(format!(
                    "primitive {mesh_index}:{primitive_index} is not an object"
                ))
            })?;
            let attributes = primitive
                .get("attributes")
                .and_then(Value::as_object)
                .ok_or_else(|| {
                    GltfError::InvalidGltf(format!(
                        "primitive {mesh_index}:{primitive_index}.attributes is not an object"
                    ))
                })?;
            let mut vertex_count = None;
            for (semantic, accessor_index) in attributes {
                let accessor_index = json_index(accessor_index, "primitive attribute accessor")?;
                let accessor = accessors
                    .get(accessor_index)
                    .and_then(Value::as_object)
                    .ok_or_else(|| {
                        GltfError::InvalidGltf(format!(
                            "primitive {mesh_index}:{primitive_index} accessor {accessor_index} is out of range"
                        ))
                    })?;
                let count = required_usize(accessor, "count", "primitive attribute accessor")?;
                if let Some(expected) = vertex_count {
                    if expected != count {
                        return Err(GltfError::InvalidGltf(format!(
                            "primitive {mesh_index}:{primitive_index} attribute counts do not match"
                        )));
                    }
                } else {
                    vertex_count = Some(count);
                }
                let component_type = accessor
                    .get("componentType")
                    .and_then(Value::as_u64)
                    .and_then(|value| u32::try_from(value).ok())
                    .ok_or_else(|| {
                        GltfError::InvalidGltf(format!(
                            "accessor {accessor_index} has invalid componentType"
                        ))
                    })?;
                // glTF 2.1 component types are outside the current codec scope
                // and are preserved later. Core glTF 2.0 layouts must satisfy
                // the semantic contract even when another preserve reason wins.
                if matches!(component_type, 5120 | 5121 | 5122 | 5123 | 5126) {
                    let accessor_type =
                        accessor
                            .get("type")
                            .and_then(Value::as_str)
                            .ok_or_else(|| {
                                GltfError::InvalidGltf(format!(
                                    "accessor {accessor_index} has invalid type"
                                ))
                            })?;
                    let normalized = match accessor.get("normalized") {
                        None => false,
                        Some(Value::Bool(normalized)) => *normalized,
                        Some(_) => {
                            return Err(GltfError::InvalidGltf(format!(
                                "accessor {accessor_index}.normalized is not a boolean"
                            )));
                        }
                    };
                    validate_semantic_accessor(
                        semantic,
                        accessor_type,
                        component_type,
                        normalized,
                    )?;
                }
            }
            let vertex_count = vertex_count.unwrap_or(0);
            let mode = primitive
                .get("mode")
                .and_then(Value::as_u64)
                .unwrap_or(MODE_TRIANGLES);
            let element_count = if let Some(indices) = primitive.get("indices") {
                let accessor_index = json_index(indices, "primitive indices accessor")?;
                let values =
                    read_unsigned_scalar_accessor(accessor_index, accessors, views, buffers)?;
                if values.iter().any(|&index| index as usize >= vertex_count) {
                    return Err(GltfError::InvalidGltf(format!(
                        "primitive {mesh_index}:{primitive_index} index is out of range for {vertex_count} vertices"
                    )));
                }
                values.len()
            } else {
                vertex_count
            };
            validate_primitive_element_count(mode, element_count, mesh_index, primitive_index)?;

            if let Some(targets) = primitive.get("targets") {
                let targets = targets.as_array().ok_or_else(|| {
                    GltfError::InvalidGltf("primitive.targets is not an array".into())
                })?;
                for (target_index, target) in targets.iter().enumerate() {
                    let target = target.as_object().ok_or_else(|| {
                        GltfError::InvalidGltf("morph target is not an object".into())
                    })?;
                    if target.is_empty() {
                        return Err(GltfError::InvalidGltf("morph target is empty".into()));
                    }
                    for (semantic, accessor_index) in target {
                        let accessor_index = json_index(accessor_index, "morph target accessor")?;
                        let accessor = accessors
                            .get(accessor_index)
                            .and_then(Value::as_object)
                            .ok_or_else(|| {
                                GltfError::InvalidGltf(format!(
                                    "morph target accessor {accessor_index} is out of range"
                                ))
                            })?;
                        let count = required_usize(accessor, "count", "morph target accessor")?;
                        if count != vertex_count
                            || !matches!(semantic.as_str(), "POSITION" | "NORMAL" | "TANGENT")
                            || accessor.get("type").and_then(Value::as_str) != Some("VEC3")
                            || accessor.get("componentType").and_then(Value::as_u64) != Some(5126)
                            || accessor
                                .get("normalized")
                                .is_some_and(|normalized| normalized != &Value::Bool(false))
                        {
                            return Err(GltfError::InvalidGltf(format!(
                                "primitive {mesh_index}:{primitive_index} morph target {target_index} {semantic} accessor has an invalid contract"
                            )));
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

fn validate_primitive_element_count(
    mode: u64,
    count: usize,
    mesh: usize,
    primitive: usize,
) -> Result<()> {
    let valid = match mode {
        0 => count >= 1,
        1 => count >= 2 && count.is_multiple_of(2),
        2 | 3 => count >= 2,
        4 => count >= 3 && count.is_multiple_of(3),
        5 | 6 => count >= 3,
        _ => false,
    };
    if !valid {
        return Err(GltfError::InvalidGltf(format!(
            "primitive {mesh}:{primitive} has invalid element count {count} for mode {mode}"
        )));
    }
    Ok(())
}

fn read_unsigned_scalar_accessor(
    accessor_index: usize,
    accessors: &[Value],
    views: &[ValidatedBufferView],
    buffers: &[Vec<u8>],
) -> Result<Vec<u32>> {
    let accessor = accessors
        .get(accessor_index)
        .and_then(Value::as_object)
        .ok_or_else(|| {
            GltfError::InvalidGltf(format!("accessor {accessor_index} is out of range"))
        })?;
    if accessor.get("type").and_then(Value::as_str) != Some("SCALAR")
        || accessor
            .get("normalized")
            .is_some_and(|normalized| normalized != &Value::Bool(false))
    {
        return Err(GltfError::InvalidGltf(format!(
            "indices accessor {accessor_index} has an invalid contract"
        )));
    }
    let component_type = accessor
        .get("componentType")
        .and_then(Value::as_u64)
        .ok_or_else(|| GltfError::InvalidGltf("indices componentType is invalid".into()))?;
    let value_size = match component_type {
        5121 => 1,
        5123 => 2,
        5125 => 4,
        _ => {
            return Err(GltfError::InvalidGltf(format!(
                "indices accessor {accessor_index} has invalid componentType {component_type}"
            )));
        }
    };
    let count = required_usize(accessor, "count", "indices accessor")?;
    let mut values = Vec::new();
    values.try_reserve_exact(count).map_err(|_| {
        GltfError::ResourceLimitExceeded("indices validation allocation failed".into())
    })?;
    values.resize(count, 0);
    if let Some(view_index) = optional_usize(accessor, "bufferView", "indices accessor")? {
        let view = *views.get(view_index).ok_or_else(|| {
            GltfError::InvalidGltf(format!("indices bufferView {view_index} is out of range"))
        })?;
        if view.byte_stride.is_some() {
            return Err(GltfError::InvalidGltf(
                "indices bufferView must not define byteStride".into(),
            ));
        }
        let byte_offset = optional_usize(accessor, "byteOffset", "indices accessor")?.unwrap_or(0);
        for (index, value) in values.iter_mut().enumerate() {
            *value = read_unsigned_from_view(
                view,
                byte_offset,
                index,
                value_size,
                component_type,
                buffers,
                "indices accessor",
            )?;
        }
    }
    if let Some(sparse) = accessor.get("sparse").and_then(Value::as_object) {
        let sparse_count = required_usize(sparse, "count", "sparse accessor")?;
        let sparse_indices = sparse
            .get("indices")
            .and_then(Value::as_object)
            .ok_or_else(|| GltfError::InvalidGltf("sparse indices are invalid".into()))?;
        let sparse_values = sparse
            .get("values")
            .and_then(Value::as_object)
            .ok_or_else(|| GltfError::InvalidGltf("sparse values are invalid".into()))?;
        let sparse_index_component = sparse_indices
            .get("componentType")
            .and_then(Value::as_u64)
            .ok_or_else(|| GltfError::InvalidGltf("sparse index type is invalid".into()))?;
        let sparse_index_size = component_size(sparse_index_component, "sparse indices")?;
        let index_view = *views
            .get(required_usize(
                sparse_indices,
                "bufferView",
                "sparse indices",
            )?)
            .ok_or_else(|| GltfError::InvalidGltf("sparse indices view is invalid".into()))?;
        let value_view = *views
            .get(required_usize(
                sparse_values,
                "bufferView",
                "sparse values",
            )?)
            .ok_or_else(|| GltfError::InvalidGltf("sparse values view is invalid".into()))?;
        let index_offset =
            optional_usize(sparse_indices, "byteOffset", "sparse indices")?.unwrap_or(0);
        let value_offset =
            optional_usize(sparse_values, "byteOffset", "sparse values")?.unwrap_or(0);
        for sparse_index in 0..sparse_count {
            let destination = read_unsigned_from_view(
                index_view,
                index_offset,
                sparse_index,
                sparse_index_size,
                sparse_index_component,
                buffers,
                "sparse indices",
            )? as usize;
            let value = read_unsigned_from_view(
                value_view,
                value_offset,
                sparse_index,
                value_size,
                component_type,
                buffers,
                "sparse values",
            )?;
            *values
                .get_mut(destination)
                .ok_or_else(|| GltfError::InvalidGltf("sparse index is out of range".into()))? =
                value;
        }
    }
    Ok(values)
}

fn read_unsigned_from_view(
    view: ValidatedBufferView,
    byte_offset: usize,
    index: usize,
    component_size: usize,
    component_type: u64,
    buffers: &[Vec<u8>],
    label: &str,
) -> Result<u32> {
    let start = index
        .checked_mul(component_size)
        .and_then(|offset| byte_offset.checked_add(offset))
        .and_then(|offset| view.byte_offset.checked_add(offset))
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} offset overflow")))?;
    let end = start
        .checked_add(component_size)
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} offset overflow")))?;
    let bytes = buffers
        .get(view.buffer)
        .and_then(|buffer| buffer.get(start..end))
        .ok_or_else(|| GltfError::InvalidGltf(format!("{label} is out of range")))?;
    match component_type {
        5121 => Ok(bytes[0] as u32),
        5123 => Ok(u16::from_le_bytes([bytes[0], bytes[1]]) as u32),
        5125 => Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
        _ => Err(GltfError::InvalidGltf(format!(
            "{label} has invalid componentType {component_type}"
        ))),
    }
}

fn validate_accessor_reference(value: &Value, count: usize, label: &str) -> Result<()> {
    let index = json_index(value, label)?;
    if index >= count {
        return Err(GltfError::InvalidGltf(format!(
            "{label} {index} is out of range"
        )));
    }
    Ok(())
}

fn validate_accessor_references(document: &Value, accessor_count: usize) -> Result<()> {
    for (mesh_index, mesh) in optional_array(document, "meshes")?.iter().enumerate() {
        let mesh = mesh
            .as_object()
            .ok_or_else(|| GltfError::InvalidGltf(format!("mesh {mesh_index} is not an object")))?;
        let primitives = mesh
            .get("primitives")
            .and_then(Value::as_array)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("mesh {mesh_index}.primitives is not an array"))
            })?;
        if primitives.is_empty() {
            return Err(GltfError::InvalidGltf(format!(
                "mesh {mesh_index} has no primitives"
            )));
        }
        for (primitive_index, primitive) in primitives.iter().enumerate() {
            let primitive = primitive.as_object().ok_or_else(|| {
                GltfError::InvalidGltf(format!(
                    "primitive {mesh_index}:{primitive_index} is not an object"
                ))
            })?;
            let attributes = primitive
                .get("attributes")
                .and_then(Value::as_object)
                .ok_or_else(|| {
                    GltfError::InvalidGltf(format!(
                        "primitive {mesh_index}:{primitive_index}.attributes is not an object"
                    ))
                })?;
            if attributes.is_empty() {
                return Err(GltfError::InvalidGltf(format!(
                    "primitive {mesh_index}:{primitive_index}.attributes is empty"
                )));
            }
            for accessor in attributes.values() {
                validate_accessor_reference(accessor, accessor_count, "primitive attribute")?;
            }
            if let Some(indices) = primitive.get("indices") {
                validate_accessor_reference(indices, accessor_count, "primitive indices")?;
            }
            if let Some(mode) = primitive.get("mode") {
                let mode = mode.as_u64().ok_or_else(|| {
                    GltfError::InvalidGltf("primitive.mode is not an integer".into())
                })?;
                if mode > 6 {
                    return Err(GltfError::InvalidGltf(format!(
                        "primitive mode {mode} is outside the glTF enum"
                    )));
                }
            }
            if let Some(targets) = primitive.get("targets") {
                let targets = targets.as_array().ok_or_else(|| {
                    GltfError::InvalidGltf("primitive.targets is not an array".into())
                })?;
                for target in targets {
                    let target = target.as_object().ok_or_else(|| {
                        GltfError::InvalidGltf("morph target is not an object".into())
                    })?;
                    if target.is_empty() {
                        return Err(GltfError::InvalidGltf("morph target is empty".into()));
                    }
                    for accessor in target.values() {
                        validate_accessor_reference(
                            accessor,
                            accessor_count,
                            "morph target accessor",
                        )?;
                    }
                }
            }
        }
    }

    for animation in optional_array(document, "animations")? {
        if let Some(samplers) = animation.get("samplers") {
            let samplers = samplers.as_array().ok_or_else(|| {
                GltfError::InvalidGltf("animation.samplers is not an array".into())
            })?;
            for sampler in samplers {
                let sampler = sampler.as_object().ok_or_else(|| {
                    GltfError::InvalidGltf("animation sampler is not an object".into())
                })?;
                for key in ["input", "output"] {
                    let accessor = sampler.get(key).ok_or_else(|| {
                        GltfError::InvalidGltf(format!("animation sampler is missing {key}"))
                    })?;
                    validate_accessor_reference(accessor, accessor_count, "animation accessor")?;
                }
            }
        }
    }
    for skin in optional_array(document, "skins")? {
        if let Some(accessor) = skin.get("inverseBindMatrices") {
            validate_accessor_reference(accessor, accessor_count, "inverseBindMatrices")?;
        }
    }
    for node in optional_array(document, "nodes")? {
        if let Some(attributes) = node
            .get("extensions")
            .and_then(|extensions| extensions.get("EXT_mesh_gpu_instancing"))
            .and_then(|extension| extension.get("attributes"))
        {
            let attributes = attributes.as_object().ok_or_else(|| {
                GltfError::InvalidGltf("EXT_mesh_gpu_instancing.attributes is not an object".into())
            })?;
            for accessor in attributes.values() {
                validate_accessor_reference(accessor, accessor_count, "instancing accessor")?;
            }
        }
    }
    Ok(())
}

/// A primitive that will be compressed, with everything needed to rewrite it.
struct CompressPlan {
    mesh_idx: usize,
    prim_idx: usize,
    draco_bytes: Vec<u8>,
    /// `(glTF semantic, Draco attribute id)` for the extension's attribute map.
    semantic_to_id: Vec<(String, u32)>,
    /// Accessor index for each attribute.
    attribute_accessors: Vec<usize>,
    /// The source indices accessor, or `None` for a non-indexed primitive (a
    /// fresh indices accessor is generated, since Draco glTF primitives are
    /// always indexed).
    indices_accessor: Option<usize>,
    num_points: usize,
    num_indices: usize,
}

fn build_plans<F>(
    doc: &Value,
    buffers: &[Vec<u8>],
    decode: &F,
    accessor_users: &HashMap<usize, usize>,
    options: &GltfCompressionOptions,
) -> Result<(Vec<CompressPlan>, CompressionReport)>
where
    F: Fn(usize, usize) -> Result<(draco_core::Mesh, Vec<(String, u32)>)>,
{
    let mut plans = Vec::new();
    let mut report = CompressionReport::default();
    let Some(meshes) = doc.get("meshes").and_then(Value::as_array) else {
        return Ok((plans, report));
    };

    for (mesh_idx, mesh) in meshes.iter().enumerate() {
        let Some(primitives) = mesh.get("primitives").and_then(Value::as_array) else {
            continue;
        };
        for (prim_idx, prim) in primitives.iter().enumerate() {
            let location = PrimitiveLocation {
                mesh: mesh_idx,
                primitive: prim_idx,
            };
            match plan_for_primitive(
                doc,
                prim,
                buffers,
                location,
                decode,
                accessor_users,
                options,
            )? {
                PlanDecision::Compress(plan) => plans.push(plan),
                PlanDecision::Preserve(reason) => {
                    report.preserved_primitives.push(PreservedPrimitive {
                        primitive: location,
                        reason,
                    })
                }
            }
        }
    }
    Ok((plans, report))
}

enum PlanDecision {
    Compress(CompressPlan),
    Preserve(PreserveReason),
}

fn validate_existing_draco_primitive(
    document: &Value,
    primitive: &Value,
    buffers: &[Vec<u8>],
    mode: u32,
) -> Result<()> {
    let extension = parse_khr_draco_mesh_compression(document, primitive)?.ok_or_else(|| {
        GltfError::InvalidGltf("primitive has no KHR_draco_mesh_compression payload".into())
    })?;
    let views = document
        .get("bufferViews")
        .and_then(Value::as_array)
        .ok_or_else(|| GltfError::InvalidGltf("missing bufferViews array".into()))?;
    let view = views
        .get(extension.buffer_view)
        .and_then(Value::as_object)
        .ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "Draco bufferView {} is out of range",
                extension.buffer_view
            ))
        })?;
    let data = buffer_view_bytes(view, buffers)?;
    let mut decoder_buffer = DecoderBuffer::new(data);
    let mut mesh = Mesh::new();
    MeshDecoder::new()
        .decode(&mut decoder_buffer, &mut mesh)
        .map_err(GltfError::DracoDecode)?;

    let primitive_attributes = primitive
        .get("attributes")
        .and_then(Value::as_object)
        .ok_or_else(|| GltfError::InvalidGltf("primitive.attributes is not an object".into()))?;
    let accessors = document
        .get("accessors")
        .and_then(Value::as_array)
        .ok_or_else(|| GltfError::InvalidGltf("missing accessors array".into()))?;

    for (semantic, &unique_id) in &extension.attributes {
        let accessor_index = primitive_attributes
            .get(semantic)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!(
                    "Draco semantic {semantic} is absent from primitive.attributes"
                ))
            })
            .and_then(|value| json_index(value, "Draco attribute accessor"))?;
        let accessor = accessors
            .get(accessor_index)
            .and_then(Value::as_object)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("accessor {accessor_index} is out of range"))
            })?;
        let accessor_type = accessor
            .get("type")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("accessor {accessor_index} has invalid type"))
            })?;
        let component_type = accessor
            .get("componentType")
            .and_then(Value::as_u64)
            .and_then(|value| u32::try_from(value).ok())
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!(
                    "accessor {accessor_index} has invalid componentType"
                ))
            })?;
        let normalized = match accessor.get("normalized") {
            None => false,
            Some(Value::Bool(normalized)) => *normalized,
            Some(_) => {
                return Err(GltfError::InvalidGltf("normalized is not a boolean".into()));
            }
        };
        let semantic_spec =
            validate_semantic_accessor(semantic, accessor_type, component_type, normalized)?;
        let attribute = mesh.attribute_by_unique_id(unique_id).ok_or_else(|| {
            GltfError::InvalidGltf(format!(
                "Draco unique attribute id {unique_id} for {semantic} is absent"
            ))
        })?;
        if attribute.attribute_type() != semantic_spec.attribute_type
            || gltf_type_for_num_components(attribute.num_components())? != accessor_type
            || component_type_for_data_type(attribute.data_type())? != component_type
            || attribute.normalized() != normalized
        {
            return Err(GltfError::InvalidGltf(format!(
                "decoded Draco attribute {semantic} does not match accessor {accessor_index}"
            )));
        }
        let count = required_usize(accessor, "count", "Draco attribute accessor")?;
        if attribute.size() != count || count != mesh.num_points() {
            return Err(GltfError::InvalidGltf(format!(
                "decoded Draco attribute {semantic} count {} does not match accessor count {count}",
                attribute.size()
            )));
        }
    }

    if let Some(indices) = primitive.get("indices") {
        let accessor_index = json_index(indices, "Draco indices accessor")?;
        let accessor = accessors
            .get(accessor_index)
            .and_then(Value::as_object)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("accessor {accessor_index} is out of range"))
            })?;
        if accessor.get("type").and_then(Value::as_str) != Some("SCALAR")
            || accessor
                .get("componentType")
                .and_then(Value::as_u64)
                .is_none_or(|component| !matches!(component, 5121 | 5123 | 5125))
            || accessor
                .get("normalized")
                .is_some_and(|normalized| normalized != &Value::Bool(false))
        {
            return Err(GltfError::InvalidGltf(
                "Draco indices accessor has an invalid contract".into(),
            ));
        }
        let expected_count = if mode == MODE_TRIANGLES as u32 {
            mesh.num_faces().checked_mul(3)
        } else {
            mesh.num_faces().checked_add(2)
        }
        .ok_or_else(|| GltfError::InvalidGltf("decoded Draco index count overflow".into()))?;
        let count = required_usize(accessor, "count", "Draco indices accessor")?;
        if count != expected_count {
            return Err(GltfError::InvalidGltf(format!(
                "Draco indices accessor count {count} does not match decoded count {expected_count}"
            )));
        }
    }
    Ok(())
}

fn plan_for_primitive<F>(
    doc: &Value,
    prim: &Value,
    buffers: &[Vec<u8>],
    location: PrimitiveLocation,
    decode: &F,
    accessor_users: &HashMap<usize, usize>,
    options: &GltfCompressionOptions,
) -> Result<PlanDecision>
where
    F: Fn(usize, usize) -> Result<(draco_core::Mesh, Vec<(String, u32)>)>,
{
    let mesh_idx = location.mesh;
    let prim_idx = location.primitive;
    // KHR_draco_mesh_compression restricts compressed primitives to TRIANGLES
    // or TRIANGLE_STRIP ("Restrictions on geometry type"), so point clouds
    // (POINTS) and line modes can never be Draco-compressed in glTF. We compress
    // the triangle list (mode 4 / default); everything else is preserved as-is.
    let mode = prim
        .get("mode")
        .map(|value| {
            value
                .as_u64()
                .ok_or_else(|| GltfError::InvalidGltf("primitive.mode is not an integer".into()))
        })
        .transpose()?
        .unwrap_or(MODE_TRIANGLES);
    let mode_u32 = u32::try_from(mode)
        .map_err(|_| GltfError::InvalidGltf("primitive.mode exceeds u32".into()))?;
    // A repeated compression request must prove that an existing Draco payload
    // and all of its accessor contracts are valid before reporting it as
    // preserved. Schema-only validation would turn corrupt input into a
    // successful AlreadyDraco result.
    if prim
        .get("extensions")
        .and_then(|extensions| extensions.get(KHR_DRACO))
        .is_some()
    {
        validate_existing_draco_primitive(doc, prim, buffers, mode_u32)?;
        return Ok(PlanDecision::Preserve(PreserveReason::AlreadyDraco));
    }
    if mode != MODE_TRIANGLES && mode != MODE_TRIANGLE_STRIP {
        return Ok(PlanDecision::Preserve(PreserveReason::UnsupportedMode {
            mode: mode_u32,
        }));
    }
    if mode == MODE_TRIANGLE_STRIP {
        return Ok(PlanDecision::Preserve(PreserveReason::UnsupportedLayout {
            detail: "TRIANGLE_STRIP compression is not implemented".into(),
        }));
    }
    if let Some(targets) = prim.get("targets") {
        let targets = targets
            .as_array()
            .ok_or_else(|| GltfError::InvalidGltf("primitive.targets is not an array".into()))?;
        if !targets.is_empty() {
            return Ok(PlanDecision::Preserve(PreserveReason::MorphTargets));
        }
    }
    // Indexed and non-indexed triangle lists are both supported. Non-indexed
    // primitives get a freshly generated indices accessor below, since Draco
    // glTF primitives are always indexed.
    let indices_accessor = prim
        .get("indices")
        .map(|value| json_index(value, "indices accessor"))
        .transpose()?;

    // Collect attribute semantics + accessors; require a round-trippable set.
    let Some(attributes) = prim.get("attributes").and_then(Value::as_object) else {
        return Err(GltfError::InvalidGltf(
            "primitive.attributes must be a non-empty object".into(),
        ));
    };
    if attributes.is_empty() || !attributes.contains_key("POSITION") {
        return Ok(PlanDecision::Preserve(PreserveReason::UnsupportedLayout {
            detail: "primitive has no POSITION attribute".into(),
        }));
    }
    let mut attribute_accessors = Vec::new();
    for accessor in attributes.values() {
        attribute_accessors.push(json_index(accessor, "attribute accessor")?);
    }

    let accessors = doc
        .get("accessors")
        .and_then(Value::as_array)
        .ok_or_else(|| GltfError::InvalidGltf("missing accessors array".into()))?;
    for &accessor in attribute_accessors.iter().chain(indices_accessor.iter()) {
        let value = accessors
            .get(accessor)
            .and_then(Value::as_object)
            .ok_or_else(|| {
                GltfError::InvalidGltf(format!("accessor {accessor} is out of range"))
            })?;
        if value.contains_key("sparse") {
            return Ok(PlanDecision::Preserve(PreserveReason::SparseAccessor {
                accessor,
            }));
        }
    }

    // All geometry accessors must be used by exactly this primitive, so that
    // dropping their buffer view / changing their count cannot corrupt another
    // primitive that shares them.
    let exclusive = |acc: usize| accessor_users.get(&acc).copied().unwrap_or(0) == 1;
    if let Some(accessor) = indices_accessor.filter(|accessor| !exclusive(*accessor)) {
        return Ok(PlanDecision::Preserve(PreserveReason::SharedAccessor {
            accessor,
        }));
    }
    if let Some(accessor) = attribute_accessors
        .iter()
        .copied()
        .find(|accessor| !exclusive(*accessor))
    {
        return Ok(PlanDecision::Preserve(PreserveReason::SharedAccessor {
            accessor,
        }));
    }

    // Decode geometry with the original glTF semantic names. An unsupported
    // attribute/layout means "leave this primitive uncompressed".
    let (mesh, semantic_to_uid) = match decode(mesh_idx, prim_idx) {
        Ok(out) => out,
        Err(GltfError::Unsupported(detail)) => {
            return Ok(PlanDecision::Preserve(PreserveReason::UnsupportedLayout {
                detail,
            }))
        }
        Err(error) => return Err(error),
    };
    let (draco_bytes, info) = match encode_draco_mesh_with_info(&mesh, options) {
        Ok(out) => out,
        Err(crate::gltf_writer::GltfWriteError::Unsupported(detail)) => {
            return Ok(PlanDecision::Preserve(PreserveReason::UnsupportedLayout {
                detail,
            }))
        }
        Err(crate::gltf_writer::GltfWriteError::InvalidMesh(detail)) => {
            return Err(GltfError::InvalidGltf(detail))
        }
        Err(crate::gltf_writer::GltfWriteError::InvalidOptions(detail)) => {
            return Err(GltfError::InvalidOptions(detail))
        }
        Err(crate::gltf_writer::GltfWriteError::ResourceLimit(detail)) => {
            return Err(GltfError::ResourceLimitExceeded(detail))
        }
        Err(crate::gltf_writer::GltfWriteError::DracoEncode(source)) => {
            return Err(GltfError::DracoEncode(source))
        }
        Err(error) => {
            return Err(GltfError::InvalidGltf(format!(
                "Draco writer failed: {error}"
            )))
        }
    };

    // The decoded attribute set must match the source primitive exactly, so the
    // extension's attribute map is faithful (no dropped or renamed attribute).
    let produced: BTreeSet<&str> = semantic_to_uid.iter().map(|(s, _)| s.as_str()).collect();
    let original: BTreeSet<&str> = attributes.keys().map(String::as_str).collect();
    if produced != original {
        return Err(GltfError::InvalidGltf(
            "decoded attribute set does not match primitive.attributes".into(),
        ));
    }
    let semantic_to_id = semantic_to_uid;

    let num_indices = info
        .num_encoded_faces
        .checked_mul(3)
        .ok_or_else(|| GltfError::ResourceLimitExceeded("index count overflow".into()))?;

    Ok(PlanDecision::Compress(CompressPlan {
        mesh_idx,
        prim_idx,
        draco_bytes,
        semantic_to_id,
        attribute_accessors,
        indices_accessor,
        num_points: info.num_encoded_points,
        num_indices,
    }))
}

/// Counts, for each accessor index, how many primitives reference it (via
/// attributes or indices) across the whole document.
fn count_accessor_users(doc: &Value) -> Result<HashMap<usize, usize>> {
    let mut users: HashMap<usize, usize> = HashMap::new();
    let mut add = |value: &Value, label: &str| -> Result<()> {
        let accessor = json_index(value, label)?;
        let count = users.entry(accessor).or_default();
        *count = count
            .checked_add(1)
            .ok_or_else(|| GltfError::InvalidGltf("accessor use count overflow".into()))?;
        Ok(())
    };

    if let Some(meshes) = doc.get("meshes") {
        let meshes = meshes
            .as_array()
            .ok_or_else(|| GltfError::InvalidGltf("meshes is not an array".into()))?;
        for mesh in meshes {
            let primitives = mesh
                .get("primitives")
                .and_then(Value::as_array)
                .ok_or_else(|| GltfError::InvalidGltf("mesh.primitives is not an array".into()))?;
            for prim in primitives {
                let attrs = prim
                    .get("attributes")
                    .and_then(Value::as_object)
                    .ok_or_else(|| {
                        GltfError::InvalidGltf("primitive.attributes is not an object".into())
                    })?;
                for accessor in attrs.values() {
                    add(accessor, "primitive attribute accessor")?;
                }
                if let Some(accessor) = prim.get("indices") {
                    add(accessor, "primitive indices accessor")?;
                }
                if let Some(targets) = prim.get("targets") {
                    let targets = targets.as_array().ok_or_else(|| {
                        GltfError::InvalidGltf("primitive.targets is not an array".into())
                    })?;
                    for target in targets {
                        let target = target.as_object().ok_or_else(|| {
                            GltfError::InvalidGltf("morph target is not an object".into())
                        })?;
                        for accessor in target.values() {
                            add(accessor, "morph target accessor")?;
                        }
                    }
                }
            }
        }
    }

    if let Some(animations) = doc.get("animations") {
        for animation in animations
            .as_array()
            .ok_or_else(|| GltfError::InvalidGltf("animations is not an array".into()))?
        {
            if let Some(samplers) = animation.get("samplers") {
                for sampler in samplers.as_array().ok_or_else(|| {
                    GltfError::InvalidGltf("animation.samplers is not an array".into())
                })? {
                    let sampler = sampler.as_object().ok_or_else(|| {
                        GltfError::InvalidGltf("animation sampler is not an object".into())
                    })?;
                    for key in ["input", "output"] {
                        let accessor = sampler.get(key).ok_or_else(|| {
                            GltfError::InvalidGltf(format!("animation sampler is missing {key}"))
                        })?;
                        add(accessor, "animation sampler accessor")?;
                    }
                }
            }
        }
    }

    if let Some(skins) = doc.get("skins") {
        for skin in skins
            .as_array()
            .ok_or_else(|| GltfError::InvalidGltf("skins is not an array".into()))?
        {
            if let Some(accessor) = skin.get("inverseBindMatrices") {
                add(accessor, "skin inverseBindMatrices accessor")?;
            }
        }
    }

    if let Some(nodes) = doc.get("nodes") {
        for node in nodes
            .as_array()
            .ok_or_else(|| GltfError::InvalidGltf("nodes is not an array".into()))?
        {
            if let Some(attributes) = node
                .get("extensions")
                .and_then(|extensions| extensions.get("EXT_mesh_gpu_instancing"))
                .and_then(|extension| extension.get("attributes"))
            {
                let attributes = attributes.as_object().ok_or_else(|| {
                    GltfError::InvalidGltf(
                        "EXT_mesh_gpu_instancing.attributes is not an object".into(),
                    )
                })?;
                for accessor in attributes.values() {
                    add(accessor, "EXT_mesh_gpu_instancing accessor")?;
                }
            }
        }
    }

    Ok(users)
}

fn apply_accessor_mutations(doc: &mut Value, plans: &[CompressPlan]) -> Result<()> {
    let accessors = doc
        .get_mut("accessors")
        .and_then(Value::as_array_mut)
        .ok_or_else(|| GltfError::InvalidGltf("missing accessors array".into()))?;

    for plan in plans {
        for &acc in &plan.attribute_accessors {
            strip_geometry_accessor(accessors, acc, plan.num_points)?;
        }
        if let Some(indices) = plan.indices_accessor {
            strip_geometry_accessor(accessors, indices, plan.num_indices)?;
        }
    }
    Ok(())
}

/// For each non-indexed plan, append a fresh `SCALAR`/`UNSIGNED_INT` indices
/// accessor (no buffer view — the indices live in the Draco stream) and point
/// the primitive at it. Appending keeps existing accessor indices stable, and
/// the new accessor has no buffer view so it does not affect buffer repacking.
fn add_generated_indices(doc: &mut Value, plans: &[CompressPlan]) -> Result<()> {
    for plan in plans {
        if plan.indices_accessor.is_some() {
            continue;
        }
        let accessors = doc
            .get_mut("accessors")
            .and_then(Value::as_array_mut)
            .ok_or_else(|| GltfError::InvalidGltf("missing accessors array".into()))?;
        let new_idx = accessors.len();
        accessors.push(serde_json::json!({
            "componentType": 5125u64, // UNSIGNED_INT
            "count": plan.num_indices as u64,
            "type": "SCALAR",
        }));
        let prim = primitive_mut(doc, plan)?;
        prim.insert("indices".into(), Value::from(new_idx as u64));
    }
    Ok(())
}

/// Mutable access to a plan's primitive JSON object.
fn primitive_mut<'a>(
    doc: &'a mut Value,
    plan: &CompressPlan,
) -> Result<&'a mut Map<String, Value>> {
    doc.get_mut("meshes")
        .and_then(Value::as_array_mut)
        .and_then(|m| m.get_mut(plan.mesh_idx))
        .and_then(|m| m.get_mut("primitives"))
        .and_then(Value::as_array_mut)
        .and_then(|p| p.get_mut(plan.prim_idx))
        .and_then(Value::as_object_mut)
        .ok_or_else(|| GltfError::InvalidGltf("primitive vanished during rewrite".into()))
}

/// Removes an accessor's buffer view (its data now lives in Draco) and sets the
/// count to the Draco-encoded element count. Other fields (type, componentType,
/// min/max, normalized) are preserved.
fn strip_geometry_accessor(accessors: &mut [Value], idx: usize, count: usize) -> Result<()> {
    let accessor = accessors
        .get_mut(idx)
        .and_then(Value::as_object_mut)
        .ok_or_else(|| GltfError::InvalidGltf(format!("accessor {} out of range", idx)))?;
    accessor.remove("bufferView");
    accessor.remove("byteOffset");
    accessor.insert("count".into(), Value::from(count));
    Ok(())
}

struct Repack {
    bin: Vec<u8>,
    /// New buffer-view index for each plan's Draco stream, in plan order.
    draco_buffer_views: Vec<usize>,
}

fn repack_buffers(
    doc: &mut Value,
    source_buffers: &[Vec<u8>],
    plans: &[CompressPlan],
) -> Result<Repack> {
    // Which buffer views are still referenced anywhere in the JSON (accessors,
    // images, surviving Draco extensions, and any unknown extension)? Scanning
    // by key name covers known and unknown referrers uniformly.
    let mut referenced = BTreeSet::new();
    collect_buffer_view_refs(doc, &mut referenced)?;

    // Move the old table out of the document. Kept entries are then moved into
    // the new table rather than deep-cloning arbitrary extension JSON.
    let old_views = doc
        .as_object_mut()
        .ok_or_else(|| GltfError::InvalidGltf("glTF root is not an object".into()))?
        .remove("bufferViews");
    let mut old_views = match old_views {
        Some(Value::Array(views)) => views,
        Some(_) => {
            return Err(GltfError::InvalidGltf("bufferViews is not an array".into()));
        }
        None => Vec::new(),
    };

    // Build the new binary: kept views (remapped), then one view per Draco blob.
    let mut bin: Vec<u8> = Vec::new();
    let mut new_views: Vec<Value> = Vec::new();
    let new_view_count = referenced
        .len()
        .checked_add(plans.len())
        .ok_or_else(|| GltfError::ResourceLimitExceeded("bufferView table size overflow".into()))?;
    new_views.try_reserve_exact(new_view_count).map_err(|_| {
        GltfError::ResourceLimitExceeded("bufferView table allocation failed".into())
    })?;
    let mut remap: HashMap<usize, usize> = HashMap::new();
    remap.try_reserve(referenced.len()).map_err(|_| {
        GltfError::ResourceLimitExceeded("bufferView remap allocation failed".into())
    })?;

    for &old_idx in &referenced {
        let view = old_views
            .get_mut(old_idx)
            .ok_or_else(|| GltfError::InvalidGltf(format!("buffer view {old_idx} invalid")))?;
        let mut new_view = match std::mem::take(view) {
            Value::Object(view) => view,
            _ => {
                return Err(GltfError::InvalidGltf(format!(
                    "buffer view {old_idx} is not an object"
                )));
            }
        };
        let bytes = buffer_view_bytes(&new_view, source_buffers)?;
        let byte_length = bytes.len();
        align_to_4(&mut bin)?;
        let offset = bin.len();
        append_bytes(&mut bin, bytes, "buffer view")?;

        new_view.insert("buffer".into(), Value::from(0u64));
        new_view.insert("byteOffset".into(), Value::from(offset as u64));
        new_view.insert("byteLength".into(), Value::from(byte_length as u64));
        let new_idx = new_views.len();
        new_views.push(Value::Object(new_view));
        remap.insert(old_idx, new_idx);
    }

    // Reindex every buffer-view reference in the document to the kept set.
    remap_buffer_view_refs(doc, &remap)?;

    // Append the Draco buffer views (not present in the JSON yet, so they are
    // intentionally added after the remap pass).
    let mut draco_buffer_views = Vec::new();
    draco_buffer_views
        .try_reserve_exact(plans.len())
        .map_err(|_| {
            GltfError::ResourceLimitExceeded("Draco bufferView table allocation failed".into())
        })?;
    for plan in plans {
        align_to_4(&mut bin)?;
        let offset = bin.len();
        append_bytes(&mut bin, &plan.draco_bytes, "Draco bitstream")?;
        let new_idx = new_views.len();
        new_views.push(serde_json::json!({
            "buffer": 0,
            "byteOffset": offset as u64,
            "byteLength": plan.draco_bytes.len() as u64,
        }));
        draco_buffer_views.push(new_idx);
    }

    let root = doc
        .as_object_mut()
        .ok_or_else(|| GltfError::InvalidGltf("glTF root is not an object".into()))?;
    if new_views.is_empty() {
        root.remove("bufferViews");
    } else {
        root.insert("bufferViews".into(), Value::Array(new_views));
    }

    Ok(Repack {
        bin,
        draco_buffer_views,
    })
}

fn buffer_view_bytes<'a>(view: &Map<String, Value>, buffers: &'a [Vec<u8>]) -> Result<&'a [u8]> {
    let buffer_idx: usize = view
        .get("buffer")
        .and_then(Value::as_u64)
        .ok_or_else(|| GltfError::InvalidGltf("buffer view missing buffer index".into()))?
        .try_into()
        .map_err(|_| GltfError::InvalidGltf("buffer index cannot fit usize".into()))?;
    let offset_u64 = view.get("byteOffset").and_then(Value::as_u64).unwrap_or(0);
    let offset = usize::try_from(offset_u64)
        .map_err(|_| GltfError::InvalidGltf("buffer view offset cannot fit usize".into()))?;
    let length: usize = view
        .get("byteLength")
        .and_then(Value::as_u64)
        .ok_or_else(|| GltfError::InvalidGltf("buffer view missing byteLength".into()))?
        .try_into()
        .map_err(|_| GltfError::InvalidGltf("buffer view length cannot fit usize".into()))?;
    let buffer = buffers
        .get(buffer_idx)
        .ok_or_else(|| GltfError::InvalidGltf(format!("buffer {} not resolved", buffer_idx)))?;
    let end = offset
        .checked_add(length)
        .filter(|&e| e <= buffer.len())
        .ok_or_else(|| GltfError::InvalidGltf("buffer view out of range".into()))?;
    Ok(&buffer[offset..end])
}

/// Recursively collect every integer found under a `"bufferView"` key.
fn collect_buffer_view_refs(value: &Value, out: &mut BTreeSet<usize>) -> Result<()> {
    match value {
        Value::Object(map) => {
            for (key, child) in map {
                if key == "extras" {
                    continue;
                }
                if key == "extensions" {
                    if let Some(extensions) = child.as_object() {
                        if let Some(draco) = extensions.get(KHR_DRACO) {
                            collect_buffer_view_refs(draco, out)?;
                        }
                        if let Some(metadata) = extensions.get("EXT_structural_metadata") {
                            collect_structural_metadata_refs(metadata, out)?;
                        }
                    }
                    continue;
                }
                if key == "bufferView" {
                    out.insert(json_index(child, "bufferView")?);
                }
                collect_buffer_view_refs(child, out)?;
            }
        }
        Value::Array(items) => {
            for item in items {
                collect_buffer_view_refs(item, out)?;
            }
        }
        _ => {}
    }
    Ok(())
}

/// Recursively remap every integer under a `"bufferView"` key using `remap`.
fn remap_buffer_view_refs(value: &mut Value, remap: &HashMap<usize, usize>) -> Result<()> {
    match value {
        Value::Object(map) => {
            for (key, child) in map.iter_mut() {
                if key == "extras" {
                    continue;
                }
                if key == "extensions" {
                    if let Some(extensions) = child.as_object_mut() {
                        if let Some(draco) = extensions.get_mut(KHR_DRACO) {
                            remap_buffer_view_refs(draco, remap)?;
                        }
                        if let Some(metadata) = extensions.get_mut("EXT_structural_metadata") {
                            remap_structural_metadata_refs(metadata, remap)?;
                        }
                    }
                    continue;
                }
                if key == "bufferView" {
                    let old = json_index(child, "bufferView")?;
                    let new = remap.get(&old).ok_or_else(|| {
                        GltfError::InvalidGltf(format!("bufferView {old} has no repack mapping"))
                    })?;
                    *child = Value::from(*new as u64);
                }
                remap_buffer_view_refs(child, remap)?;
            }
        }
        Value::Array(items) => {
            for item in items {
                remap_buffer_view_refs(item, remap)?;
            }
        }
        _ => {}
    }
    Ok(())
}

const STRUCTURAL_METADATA_BUFFER_VIEW_KEYS: &[&str] = &["values", "arrayOffsets", "stringOffsets"];

fn structural_metadata_properties(value: &Value) -> Result<Vec<&Map<String, Value>>> {
    let Some(tables) = value.get("propertyTables") else {
        return Ok(Vec::new());
    };
    let tables = tables.as_array().ok_or_else(|| {
        GltfError::InvalidGltf("EXT_structural_metadata.propertyTables is not an array".into())
    })?;
    let mut properties = Vec::new();
    for table in tables {
        let Some(table_properties) = table.get("properties") else {
            continue;
        };
        let table_properties = table_properties.as_object().ok_or_else(|| {
            GltfError::InvalidGltf(
                "EXT_structural_metadata property table properties is not an object".into(),
            )
        })?;
        for property in table_properties.values() {
            properties.push(property.as_object().ok_or_else(|| {
                GltfError::InvalidGltf("EXT_structural_metadata property is not an object".into())
            })?);
        }
    }
    Ok(properties)
}

fn collect_structural_metadata_refs(metadata: &Value, out: &mut BTreeSet<usize>) -> Result<()> {
    for property in structural_metadata_properties(metadata)? {
        for key in STRUCTURAL_METADATA_BUFFER_VIEW_KEYS {
            if let Some(value) = property.get(*key) {
                out.insert(json_index(value, "EXT_structural_metadata bufferView")?);
            }
        }
    }
    Ok(())
}

fn remap_structural_metadata_refs(
    metadata: &mut Value,
    remap: &HashMap<usize, usize>,
) -> Result<()> {
    let Some(tables) = metadata.get_mut("propertyTables") else {
        return Ok(());
    };
    let tables = tables.as_array_mut().ok_or_else(|| {
        GltfError::InvalidGltf("EXT_structural_metadata.propertyTables is not an array".into())
    })?;
    for table in tables {
        let Some(properties) = table.get_mut("properties") else {
            continue;
        };
        let properties = properties.as_object_mut().ok_or_else(|| {
            GltfError::InvalidGltf(
                "EXT_structural_metadata property table properties is not an object".into(),
            )
        })?;
        for property in properties.values_mut() {
            let property = property.as_object_mut().ok_or_else(|| {
                GltfError::InvalidGltf("EXT_structural_metadata property is not an object".into())
            })?;
            for key in STRUCTURAL_METADATA_BUFFER_VIEW_KEYS {
                if let Some(value) = property.get_mut(*key) {
                    let old = json_index(value, "EXT_structural_metadata bufferView")?;
                    let new = remap.get(&old).ok_or_else(|| {
                        GltfError::InvalidGltf(format!(
                            "EXT_structural_metadata bufferView {old} has no repack mapping"
                        ))
                    })?;
                    *value = Value::from(*new as u64);
                }
            }
        }
    }
    Ok(())
}

fn known_non_binary_extension(name: &str) -> bool {
    name == "KHR_texture_transform"
        || name == "EXT_mesh_features"
        || name == "EXT_mesh_gpu_instancing"
        || name == "KHR_lights_punctual"
        || name == "KHR_animation_pointer"
        || matches!(
            name,
            "KHR_materials_anisotropy"
                | "KHR_materials_clearcoat"
                | "KHR_materials_diffuse_transmission"
                | "KHR_materials_dispersion"
                | "KHR_materials_emissive_strength"
                | "KHR_materials_ior"
                | "KHR_materials_iridescence"
                | "KHR_materials_pbrSpecularGlossiness"
                | "KHR_materials_sheen"
                | "KHR_materials_specular"
                | "KHR_materials_transmission"
                | "KHR_materials_unlit"
                | "KHR_materials_variants"
                | "KHR_materials_volume"
                | "KHR_texture_basisu"
                | "EXT_texture_webp"
                | "EXT_texture_avif"
                | "MSFT_lod"
        )
}

fn reject_opaque_binary_references(document: &Value) -> Result<()> {
    fn scan(value: &Value, path: &str) -> Result<()> {
        match value {
            Value::Object(object) => {
                for (key, child) in object {
                    let normalized: String = key
                        .chars()
                        .filter(|character| character.is_ascii_alphanumeric())
                        .flat_map(char::to_lowercase)
                        .collect();
                    let looks_binary = normalized.contains("buffer")
                        || normalized.contains("offset")
                        || normalized.contains("stride")
                        || (normalized.contains("byte") && normalized.contains("length"));
                    if looks_binary {
                        return Err(GltfError::OpaqueBinaryReference(format!("{path}.{key}")));
                    }
                    scan(child, &format!("{path}.{key}"))?;
                }
            }
            Value::Array(values) => {
                for (index, child) in values.iter().enumerate() {
                    scan(child, &format!("{path}[{index}]"))?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    fn walk(value: &Value, path: &str) -> Result<()> {
        match value {
            Value::Object(object) => {
                for (key, child) in object {
                    if key == "extras" {
                        continue;
                    }
                    if key == "extensions" {
                        let extensions = child.as_object().ok_or_else(|| {
                            GltfError::InvalidGltf(format!("{path}.extensions is not an object"))
                        })?;
                        for (name, extension) in extensions {
                            let extension_path = format!("{path}.extensions.{name}");
                            if name == KHR_DRACO
                                || name == "EXT_structural_metadata"
                                || known_non_binary_extension(name)
                            {
                                // The extension's own binary layout is known (or
                                // known not to contain binary references), but
                                // nested extension objects are independent and
                                // must still be classified recursively.
                                walk(extension, &extension_path)?;
                            } else {
                                scan(extension, &extension_path)?;
                            }
                        }
                        continue;
                    }
                    walk(child, &format!("{path}.{key}"))?;
                }
            }
            Value::Array(values) => {
                for (index, child) in values.iter().enumerate() {
                    walk(child, &format!("{path}[{index}]"))?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    walk(document, "$")
}

fn set_primitive_draco_extension(
    doc: &mut Value,
    plan: &CompressPlan,
    draco_buffer_view: usize,
) -> Result<()> {
    let prim = primitive_mut(doc, plan)?;

    let mut attributes = Map::new();
    for (semantic, id) in &plan.semantic_to_id {
        attributes.insert(semantic.clone(), Value::from(*id as u64));
    }
    let draco = serde_json::json!({
        "bufferView": draco_buffer_view as u64,
        "attributes": Value::Object(attributes),
    });

    let extensions = prim
        .entry("extensions")
        .or_insert_with(|| Value::Object(Map::new()));
    if !extensions.is_object() {
        return Err(GltfError::InvalidGltf(
            "primitive.extensions is not an object".into(),
        ));
    }
    let extensions = extensions
        .as_object_mut()
        .ok_or_else(|| GltfError::InvalidGltf("primitive.extensions is not an object".into()))?;
    extensions.insert(KHR_DRACO.into(), draco);
    Ok(())
}

/// Adds `KHR_draco_mesh_compression` to a root string array (creating it if
/// absent) without duplicating it.
fn ensure_extension_listed(doc: &mut Value, key: &str) -> Result<()> {
    let root = doc
        .as_object_mut()
        .ok_or_else(|| GltfError::InvalidGltf("glTF root is not an object".into()))?;
    let list = root.entry(key).or_insert_with(|| Value::Array(Vec::new()));
    let arr = list
        .as_array_mut()
        .ok_or_else(|| GltfError::InvalidGltf(format!("{key} is not an array")))?;
    if !arr.iter().any(|v| v.as_str() == Some(KHR_DRACO)) {
        arr.push(Value::from(KHR_DRACO));
    }
    Ok(())
}

/// Collapses the document to a single buffer of `bin_len` bytes (carrying
/// `byteLength` but no URI). The caller embeds the bytes: `serialize` fills a
/// data URI for glTF output, or writes a GLB BIN chunk.
fn set_single_buffer(doc: &mut Value, bin_len: usize) -> Result<()> {
    let root = doc
        .as_object_mut()
        .ok_or_else(|| GltfError::InvalidGltf("glTF root is not an object".into()))?;
    if bin_len == 0 {
        root.remove("buffers");
        return Ok(());
    }
    let mut buffer = Map::new();
    let bin_len = u64::try_from(bin_len)
        .map_err(|_| GltfError::ResourceLimitExceeded("buffer exceeds u64".into()))?;
    buffer.insert("byteLength".into(), Value::from(bin_len));
    root.insert("buffers".into(), Value::Array(vec![Value::Object(buffer)]));
    Ok(())
}

fn align_to_4(buf: &mut Vec<u8>) -> Result<()> {
    let padding = (4 - buf.len() % 4) % 4;
    let new_len = buf
        .len()
        .checked_add(padding)
        .ok_or_else(|| GltfError::ResourceLimitExceeded("alignment size overflow".into()))?;
    buf.try_reserve(padding)
        .map_err(|_| GltfError::ResourceLimitExceeded("alignment allocation failed".into()))?;
    buf.resize(new_len, 0);
    Ok(())
}

fn append_bytes(buf: &mut Vec<u8>, bytes: &[u8], label: &str) -> Result<()> {
    buf.len()
        .checked_add(bytes.len())
        .ok_or_else(|| GltfError::ResourceLimitExceeded(format!("{label} size overflow")))?;
    buf.try_reserve(bytes.len())
        .map_err(|_| GltfError::ResourceLimitExceeded(format!("{label} allocation failed")))?;
    buf.extend_from_slice(bytes);
    Ok(())
}