bible-io 1.1.0

A Rust library for working with Bible text data structures
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
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
//! Loading, navigation, reference resolution, search, and serialization.

use std::{
    collections::{HashMap, HashSet},
    fs,
    hash::{Hash, Hasher},
    path::Path,
    sync::{OnceLock, RwLock},
    time::{Duration, SystemTime},
};

use bible_io_references::{
    BookPassage, ChapterPassage, Language, ParseError, ParseErrorKind, Passage, PassageParser,
    PassageSequence, Reference, ReferenceFormatter, ReferenceParser, VersePassage, VerseRange,
    VerseRef,
};
use serde_json::{Map, Value};

use crate::{
    bible_books_enum::BibleBook,
    book::Book,
    chapter::Chapter,
    errors::{BibleDataFormatError, BibleDataFormatErrorCode, ModelError},
    json_value::{hash_json_map, validate_annotations, JsonMap},
    loading::{
        BibleDataValidationOptions, BibleLoadOptions, BibleLoadPhase, BibleLoadProgress,
        CURRENT_BIBLE_SCHEMA_VERSION,
    },
    location::{parse_book_identifier, BibleLocation, BibleVerseKey},
    search::{
        find_match_ranges, fuzzy_match_ranges, fuzzy_matches, matches_search_text, SearchHit,
        SearchIndexMode, SearchOptions, SearchResults,
    },
    search_index::SearchIndex,
    source::{BibleMetadata, BibleSource},
    text_search::{
        build_search_index_terms, extract_unicode_words, normalize_search_text,
        tokenize_search_text,
    },
    verse::Verse,
};

pub use crate::errors::BibleError;

const LEGACY_REFERENCE_ALIASES: &[(&str, BibleBook)] = &[
    ("ge", BibleBook::Genesis),
    ("le", BibleBook::Leviticus),
    ("nu", BibleBook::Numbers),
    ("sos", BibleBook::SongOfSolomon),
    ("songofsongs", BibleBook::SongOfSolomon),
    ("da", BibleBook::Daniel),
    ("joe", BibleBook::Joel),
    ("1thes", BibleBook::FirstThessalonians),
    ("2thes", BibleBook::SecondThessalonians),
    ("jam", BibleBook::James),
    ("estg", BibleBook::EstherAdditions),
    ("dan3", BibleBook::DanielSongOfThree),
];

/// An inclusive range ordered by the loaded edition, including custom canons.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EditionVerseRange {
    /// Inclusive first verse.
    pub start: VerseRef,
    /// Inclusive final verse.
    pub end: VerseRef,
}

/// A parsed verse or range whose ordering may follow a custom edition canon.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EditionReference {
    /// One verse.
    Verse(VerseRef),
    /// An inclusive range ordered by the loaded edition.
    Range(EditionVerseRange),
}

/// An immutable, edition-ordered selection of resolved verses.
///
/// This type intentionally exposes only shared slice access. It preserves the
/// source order and duplicates produced by range and passage resolution while
/// preventing callers from pushing, removing, or reordering entries.
///
/// ```compile_fail
/// # use bible_io::VerseSelection;
/// # fn cannot_reorder(mut verses: VerseSelection<'_>) {
/// verses.swap(0, 1);
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VerseSelection<'a> {
    verses: Box<[&'a Verse]>,
}

impl<'a> VerseSelection<'a> {
    fn from_vec(verses: Vec<&'a Verse>) -> Self {
        Self {
            verses: verses.into_boxed_slice(),
        }
    }

    /// Borrow the resolved verses as an immutable slice.
    #[must_use]
    pub fn as_slice(&self) -> &[&'a Verse] {
        &self.verses
    }

    /// Iterate over the resolved verses in edition order.
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &'a Verse> + ExactSizeIterator + '_ {
        self.verses.iter().copied()
    }

    /// Return the number of resolved verses.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.verses.len()
    }

    /// Return whether the selection contains no verses.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.verses.is_empty()
    }

    /// Borrow the verse at `index`.
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&'a Verse> {
        self.verses.get(index).copied()
    }

    /// Borrow the first resolved verse.
    #[must_use]
    pub fn first(&self) -> Option<&'a Verse> {
        self.verses.first().copied()
    }

    /// Borrow the last resolved verse.
    #[must_use]
    pub fn last(&self) -> Option<&'a Verse> {
        self.verses.last().copied()
    }
}

impl<'a> AsRef<[&'a Verse]> for VerseSelection<'a> {
    fn as_ref(&self) -> &[&'a Verse] {
        self.as_slice()
    }
}

impl<'a> std::ops::Deref for VerseSelection<'a> {
    type Target = [&'a Verse];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<'a, 'selection> IntoIterator for &'selection VerseSelection<'a> {
    type Item = &'a Verse;
    type IntoIter = std::iter::Copied<std::slice::Iter<'selection, &'a Verse>>;

    fn into_iter(self) -> Self::IntoIter {
        self.verses.iter().copied()
    }
}

impl<'a> IntoIterator for VerseSelection<'a> {
    type Item = &'a Verse;
    type IntoIter = std::vec::IntoIter<&'a Verse>;

    fn into_iter(self) -> Self::IntoIter {
        self.verses.into_vec().into_iter()
    }
}

/// The resolved result of a narrow Bible reference.
///
/// A single-verse reference retains the direct borrowed-verse shape of Dart's
/// `getByRef`; an inclusive range is represented by an immutable selection.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BibleReferenceResult<'a> {
    /// One resolved verse.
    Verse(&'a Verse),
    /// An inclusive, edition-ordered verse range.
    Range(VerseSelection<'a>),
}

impl<'a> BibleReferenceResult<'a> {
    /// Borrow the single verse, or return `None` for a range.
    #[must_use]
    pub const fn as_verse(&self) -> Option<&'a Verse> {
        match self {
            Self::Verse(verse) => Some(*verse),
            Self::Range(_) => None,
        }
    }

    /// Borrow the range selection, or return `None` for a single verse.
    #[must_use]
    pub const fn as_range(&self) -> Option<&VerseSelection<'a>> {
        match self {
            Self::Verse(_) => None,
            Self::Range(verses) => Some(verses),
        }
    }

    /// Consume the result and return its range selection, if present.
    #[must_use]
    pub fn into_range(self) -> Option<VerseSelection<'a>> {
        match self {
            Self::Verse(_) => None,
            Self::Range(verses) => Some(verses),
        }
    }
}

/// A parsed passage whose verse range may follow the loaded edition's canon.
///
/// Most values are represented by the shared references package. The
/// `Reference` variant covers the one shape that package deliberately cannot
/// construct: a cross-book range that ascends in a custom edition order while
/// descending in canonical enum order.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EditionPassage {
    /// A passage represented directly by `bible-io-references`.
    Standard(Passage),
    /// A narrow verse or range reference ordered by this edition.
    Reference(EditionReference),
}

/// An edition-aware parsed value with optional language-detection metadata.
///
/// Metadata is absent only for a custom-canon range, because the dependency's
/// metadata wrapper can only be produced after constructing its canonically
/// ordered range type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EditionParsed<T> {
    value: T,
    metadata: Option<bible_io_references::ParseMetadata>,
}

impl<T> EditionParsed<T> {
    const fn new(value: T, metadata: Option<bible_io_references::ParseMetadata>) -> Self {
        Self { value, metadata }
    }

    /// Borrow the parsed value.
    #[must_use]
    pub const fn value(&self) -> &T {
        &self.value
    }

    /// Borrow language-detection metadata when the shared parser produced it.
    #[must_use]
    pub const fn metadata(&self) -> Option<&bible_io_references::ParseMetadata> {
        self.metadata.as_ref()
    }

    /// Consume this wrapper and return its value and optional metadata.
    #[must_use]
    pub fn into_parts(self) -> (T, Option<bible_io_references::ParseMetadata>) {
        (self.value, self.metadata)
    }
}

/// Counts and averages for one Bible.
#[derive(Debug, Clone, PartialEq)]
pub struct BibleStats {
    /// Number of loaded books.
    pub book_count: usize,
    /// Number of loaded chapters.
    pub chapter_count: usize,
    /// Number of loaded verses.
    pub verse_count: usize,
    /// Number of Unicode word tokens.
    pub total_words: usize,
    /// Mean verse byte length, rounded to the nearest integer.
    pub average_verse_length: usize,
    /// Verse count grouped by book.
    pub verses_per_book: HashMap<BibleBook, usize>,
}

/// Diagnostics for loaded content and the retained index.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BiblePerformanceMetrics {
    /// File/model load time when measured.
    pub load_time: Duration,
    /// Distinct retained index terms.
    pub search_index_size: usize,
    /// Whether an index is currently retained.
    pub search_index_built: bool,
    /// Number of verses.
    pub verse_count: usize,
    /// Number of retained index postings.
    pub posting_count: usize,
    /// Total UTF-8 text bytes.
    pub text_bytes: usize,
    /// Conservative approximate model/index memory in KiB.
    pub memory_usage_kib: usize,
}

/// Complete immutable Bible content with an optionally retained search cache.
#[derive(Debug)]
pub struct Bible {
    books: Vec<Book>,
    index_by_book: HashMap<BibleBook, usize>,
    reference_parser: ReferenceParser,
    passage_parser: PassageParser,
    search_index: RwLock<Option<SearchIndex>>,
    search_index_mode: SearchIndexMode,
    schema_version: u32,
    metadata: BibleMetadata,
    annotations: JsonMap,
    language: Language,
    load_time: Duration,
    created_at: SystemTime,
}

impl Clone for Bible {
    fn clone(&self) -> Self {
        Self {
            books: self.books.clone(),
            index_by_book: self.index_by_book.clone(),
            reference_parser: self.reference_parser.clone(),
            passage_parser: self.passage_parser.clone(),
            search_index: RwLock::new(
                self.search_index
                    .read()
                    .expect("search lock poisoned")
                    .clone(),
            ),
            search_index_mode: self.search_index_mode,
            schema_version: self.schema_version,
            metadata: self.metadata.clone(),
            annotations: self.annotations.clone(),
            language: self.language,
            load_time: self.load_time,
            created_at: self.created_at,
        }
    }
}

impl PartialEq for Bible {
    fn eq(&self, other: &Self) -> bool {
        self.books == other.books
            && self.schema_version == other.schema_version
            && self.metadata == other.metadata
            && self.annotations == other.annotations
            && self.language == other.language
    }
}

impl Eq for Bible {}

impl Hash for Bible {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.books.hash(state);
        self.schema_version.hash(state);
        self.metadata.hash(state);
        hash_json_map(&self.annotations, state);
        self.language.hash(state);
    }
}

impl Bible {
    /// Load a Bible from a JSON file using strict validation.
    pub fn new(json_path: &str) -> Result<Self, Box<dyn std::error::Error>> {
        Self::from_path(json_path, BibleLoadOptions::default())
            .map_err(|error| Box::new(error) as Box<dyn std::error::Error>)
    }

    /// Load a Bible from a path with explicit construction options.
    pub fn from_path(
        path: impl AsRef<Path>,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let started = std::time::Instant::now();
        let bytes = fs::read(path.as_ref()).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                format!("unable to read Bible file {}", path.as_ref().display()),
            )
            .with_cause(error)
        })?;
        let mut bible = Self::from_json_slice_with_options(&bytes, options)?;
        bible.load_time = started.elapsed();
        Ok(bible)
    }

    /// Load a Bible from a path while reporting stable reading, processing,
    /// and completion phases.
    pub fn from_path_with_progress(
        path: impl AsRef<Path>,
        options: BibleLoadOptions,
        mut on_progress: impl FnMut(BibleLoadProgress),
    ) -> Result<Self, BibleDataFormatError> {
        let started = std::time::Instant::now();
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Reading, 0.0, 0.0)
                .expect("constant progress is valid"),
        );
        let bytes = fs::read(path.as_ref()).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                format!("unable to read Bible file {}", path.as_ref().display()),
            )
            .with_cause(error)
        })?;
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Reading, 0.65, 1.0)
                .expect("constant progress is valid"),
        );
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Processing, 0.65, 0.0)
                .expect("constant progress is valid"),
        );
        let mut bible = Self::from_json_slice_with_options(&bytes, options)?;
        bible.load_time = started.elapsed();
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Processing, 1.0, 1.0)
                .expect("constant progress is valid"),
        );
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Complete, 1.0, 1.0)
                .expect("constant progress is valid"),
        );
        Ok(bible)
    }

    /// Load a Bible from a path and attach explicit catalog provenance.
    pub fn from_path_with_source(
        path: impl AsRef<Path>,
        source: &BibleSource,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let started = std::time::Instant::now();
        let bytes = fs::read(path.as_ref()).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                format!("unable to read Bible file {}", path.as_ref().display()),
            )
            .with_cause(error)
        })?;
        let mut bible = Self::from_json_slice_with_source(&bytes, source, options)?;
        bible.load_time = started.elapsed();
        Ok(bible)
    }

    /// Load from a path with explicit provenance and progress reporting.
    pub fn from_path_with_source_and_progress(
        path: impl AsRef<Path>,
        source: &BibleSource,
        options: BibleLoadOptions,
        mut on_progress: impl FnMut(BibleLoadProgress),
    ) -> Result<Self, BibleDataFormatError> {
        let started = std::time::Instant::now();
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Reading, 0.0, 0.0)
                .expect("constant progress is valid"),
        );
        let bytes = fs::read(path.as_ref()).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                format!("unable to read Bible file {}", path.as_ref().display()),
            )
            .with_cause(error)
        })?;
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Reading, 0.65, 1.0)
                .expect("constant progress is valid"),
        );
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Processing, 0.65, 0.0)
                .expect("constant progress is valid"),
        );
        let mut bible = Self::from_json_slice_with_source(&bytes, source, options)?;
        bible.load_time = started.elapsed();
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Processing, 1.0, 1.0)
                .expect("constant progress is valid"),
        );
        on_progress(
            BibleLoadProgress::new(BibleLoadPhase::Complete, 1.0, 1.0)
                .expect("constant progress is valid"),
        );
        Ok(bible)
    }

    /// Decode a Bible from UTF-8 JSON bytes with strict defaults.
    pub fn from_json_slice(bytes: &[u8]) -> Result<Self, BibleDataFormatError> {
        Self::from_json_slice_with_options(bytes, BibleLoadOptions::default())
    }

    /// Decode a Bible from UTF-8 JSON bytes and explicit options.
    pub fn from_json_slice_with_options(
        bytes: &[u8],
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let text = std::str::from_utf8(bytes).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                "Bible bytes must contain valid UTF-8 JSON",
            )
            .with_cause(error)
        })?;
        Self::from_json_str_with_options(text, options)
    }

    /// Decode UTF-8 JSON bytes and attach an explicit catalog source.
    pub fn from_json_slice_with_source(
        bytes: &[u8],
        source: &BibleSource,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let text = std::str::from_utf8(bytes).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                "Bible bytes must contain valid UTF-8 JSON",
            )
            .with_cause(error)
        })?;
        Self::from_json_str_with_source(text, source, options)
    }

    /// Decode a Bible from JSON text with strict defaults.
    pub fn from_json_str(input: &str) -> Result<Self, BibleDataFormatError> {
        Self::from_json_str_with_options(input, BibleLoadOptions::default())
    }

    /// Decode a Bible from JSON text and explicit options.
    pub fn from_json_str_with_options(
        input: &str,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let value: Value = serde_json::from_str(input).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                "Bible content is not valid JSON",
            )
            .with_cause(error)
        })?;
        Self::from_json_value_with_options(value, options)
    }

    /// Decode JSON text and attach an explicit catalog source. Explicit
    /// source provenance replaces an embedded source while nested metadata
    /// fields retain precedence for edition-specific values.
    pub fn from_json_str_with_source(
        input: &str,
        source: &BibleSource,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let value: Value = serde_json::from_str(input).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidJson,
                "$",
                "Bible content is not valid JSON",
            )
            .with_cause(error)
        })?;
        Self::from_json_value_with_source(value, source, options)
    }

    /// Decode a Bible from an already parsed JSON value with strict defaults.
    pub fn from_json_value(value: Value) -> Result<Self, BibleDataFormatError> {
        Self::from_json_value_with_options(value, BibleLoadOptions::default())
    }

    /// Decode a Bible from a JSON value and explicit options.
    pub fn from_json_value_with_options(
        value: Value,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        Self::from_json_value_with_optional_source(value, None, options)
    }

    /// Decode a JSON value while attaching an explicit catalog source.
    pub fn from_json_value_with_source(
        value: Value,
        source: &BibleSource,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        Self::from_json_value_with_optional_source(value, Some(source), options)
    }

    fn from_json_value_with_optional_source(
        value: Value,
        source: Option<&BibleSource>,
        options: BibleLoadOptions,
    ) -> Result<Self, BibleDataFormatError> {
        let root = value.as_object().ok_or_else(|| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::InvalidType,
                "$",
                "Bible JSON must have an object at its root",
            )
            .with_value(value.clone())
        })?;
        let schema_version = match root.get("schemaVersion") {
            None => CURRENT_BIBLE_SCHEMA_VERSION,
            Some(Value::Number(number)) => number
                .as_u64()
                .and_then(|value| u32::try_from(value).ok())
                .ok_or_else(|| {
                    data_error(
                        BibleDataFormatErrorCode::InvalidType,
                        "$.schemaVersion",
                        "schemaVersion must be an integer",
                        root.get("schemaVersion"),
                    )
                })?,
            other => {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidType,
                    "$.schemaVersion",
                    "schemaVersion must be an integer",
                    other,
                ))
            }
        };
        if schema_version != CURRENT_BIBLE_SCHEMA_VERSION {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidValue,
                "$.schemaVersion",
                "unsupported Bible schema version",
                root.get("schemaVersion"),
            ));
        }

        let metadata = read_metadata(root, source)?;
        let language = resolve_language(root.get("language"), &metadata)?;
        let books_value = root.get("books");
        let books_object = match books_value {
            Some(Value::Object(object)) => object,
            None if !options.validation.require_books => EMPTY_MAP.get_or_init(Map::new),
            None => {
                return Err(BibleDataFormatError::new(
                    BibleDataFormatErrorCode::MissingField,
                    "$.books",
                    "Bible content must declare a books object",
                ))
            }
            other => {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidType,
                    "$.books",
                    "Bible books must be an object",
                    other,
                ))
            }
        };
        if options.validation.require_books && books_object.is_empty() {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidValue,
                "$.books",
                "Bible content must contain at least one book",
                books_value,
            ));
        }
        let mut parsed = HashMap::new();
        let mut declared_order = Vec::with_capacity(books_object.len());
        for (identifier, raw_book) in books_object {
            let path = json_path("$.books", identifier);
            let book = parse_book_identifier(identifier).ok_or_else(|| {
                data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &path,
                    "unsupported Bible book identifier",
                    Some(&Value::String(identifier.clone())),
                )
            })?;
            if parsed.contains_key(&book) {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &path,
                    "the same Bible book is declared more than once",
                    Some(&Value::String(identifier.clone())),
                ));
            }
            declared_order.push(book);
            parsed.insert(book, read_book(book, raw_book, &path, options.validation)?);
        }
        let order = read_book_order(root.get("bookOrder"), &parsed, &declared_order)?;
        let books: Vec<_> = order
            .into_iter()
            .map(|book| parsed.remove(&book).unwrap())
            .collect();
        validate_aliases(&books)?;
        let annotations = additional_fields(root, ROOT_FIELDS);
        Self::from_parts(
            books,
            language,
            metadata,
            schema_version,
            annotations,
            options.search_index_mode,
            Duration::ZERO,
        )
    }

    /// Construct a Bible directly from validated model values.
    pub fn from_books(
        books: Vec<Book>,
        language: Language,
        metadata: BibleMetadata,
        annotations: JsonMap,
        search_index_mode: SearchIndexMode,
    ) -> Result<Self, BibleDataFormatError> {
        let metadata = crate::source::merge_bible_metadata(
            Some(&metadata),
            None,
            (!language.is_auto()).then(|| language.display_name()),
            (!language.is_auto()).then(|| language.code()),
        )?;
        validate_aliases(&books)?;
        Self::from_parts(
            books,
            language,
            metadata,
            CURRENT_BIBLE_SCHEMA_VERSION,
            annotations,
            search_index_mode,
            Duration::ZERO,
        )
    }

    /// Derive a validated Bible value with selected replacements.
    pub fn copy_with(
        &self,
        books: Option<Vec<Book>>,
        language: Option<Language>,
        metadata: Option<BibleMetadata>,
        annotations: Option<JsonMap>,
        search_index_mode: Option<SearchIndexMode>,
    ) -> Result<Self, BibleDataFormatError> {
        let metadata = metadata.unwrap_or_else(|| self.metadata.clone());
        metadata.validate("$.metadata")?;
        let books = books.unwrap_or_else(|| self.books.clone());
        validate_aliases(&books)?;
        Self::from_parts(
            books,
            language.unwrap_or(self.language),
            metadata,
            self.schema_version,
            annotations.unwrap_or_else(|| self.annotations.clone()),
            search_index_mode.unwrap_or(self.search_index_mode),
            Duration::ZERO,
        )
    }

    fn from_parts(
        books: Vec<Book>,
        language: Language,
        metadata: BibleMetadata,
        schema_version: u32,
        annotations: JsonMap,
        search_index_mode: SearchIndexMode,
        load_time: Duration,
    ) -> Result<Self, BibleDataFormatError> {
        validate_annotations(&annotations, ROOT_FIELDS).map_err(|error| {
            BibleDataFormatError::new(
                BibleDataFormatErrorCode::ReservedField,
                "$",
                "Bible annotations must not shadow structural fields",
            )
            .with_cause(error)
        })?;
        let mut index_by_book = HashMap::new();
        for (index, book) in books.iter().enumerate() {
            if index_by_book.insert(book.book(), index).is_some() {
                return Err(BibleDataFormatError::new(
                    BibleDataFormatErrorCode::InvalidValue,
                    "$.books",
                    "Bible books must have unique identifiers",
                ));
            }
        }
        let reference_parser = build_reference_parser(&books, language)?;
        let passage_parser = PassageParser::from_reference_parser(reference_parser.clone());
        let bible = Self {
            books,
            index_by_book,
            reference_parser,
            passage_parser,
            search_index: RwLock::new(None),
            search_index_mode,
            schema_version,
            metadata,
            annotations,
            language,
            load_time,
            created_at: SystemTime::now(),
        };
        if search_index_mode == SearchIndexMode::Eager {
            *bible.search_index.write().expect("search lock poisoned") =
                Some(bible.build_search_index());
        }
        Ok(bible)
    }

    /// Return the versioned content-schema number.
    #[must_use]
    pub const fn schema_version(&self) -> u32 {
        self.schema_version
    }
    /// Return the full edition metadata.
    #[must_use]
    pub fn metadata(&self) -> &BibleMetadata {
        &self.metadata
    }
    /// Return losslessly retained root annotations.
    #[must_use]
    pub fn annotations(&self) -> &JsonMap {
        &self.annotations
    }
    /// Return the resolved reference language.
    #[must_use]
    pub const fn language_id(&self) -> Language {
        self.language
    }
    /// Return the configured index lifecycle policy.
    #[must_use]
    pub const fn search_index_mode(&self) -> SearchIndexMode {
        self.search_index_mode
    }
    /// Return when this in-memory Bible value was constructed.
    #[must_use]
    pub const fn created_at(&self) -> SystemTime {
        self.created_at
    }
    /// Return the edition ID, or an empty string for legacy content without one.
    #[must_use]
    pub fn id(&self) -> &str {
        self.metadata.id.as_deref().unwrap_or("")
    }
    /// Return the translation display name, or an empty string.
    #[must_use]
    pub fn name(&self) -> &str {
        self.metadata.translation_name.as_deref().unwrap_or("")
    }
    /// Return the translation description, or an empty string.
    #[must_use]
    pub fn description(&self) -> &str {
        self.metadata.description.as_deref().unwrap_or("")
    }
    /// Return the language display name, or an empty string.
    #[must_use]
    pub fn language(&self) -> &str {
        self.metadata.language_name.as_deref().unwrap_or("")
    }
    /// Return the optional ISO language code.
    #[must_use]
    pub fn language_code(&self) -> Option<&str> {
        self.metadata.language_code.as_deref()
    }
    /// Return the optional translation abbreviation.
    #[must_use]
    pub fn abbreviation(&self) -> Option<&str> {
        self.metadata.abbreviation.as_deref()
    }
    /// Return the optional publication year.
    #[must_use]
    pub const fn year(&self) -> Option<i32> {
        self.metadata.year
    }
    /// Return the optional source/provenance name.
    #[must_use]
    pub fn source_name(&self) -> Option<&str> {
        self.metadata.source_name.as_deref()
    }
    /// Return the optional content copyright statement.
    #[must_use]
    pub fn copyright(&self) -> Option<&str> {
        self.metadata.copyright.as_deref()
    }
    /// Return the optional content license.
    #[must_use]
    pub fn license(&self) -> Option<&str> {
        self.metadata.license.as_deref()
    }
    /// Return the optional canon label.
    #[must_use]
    pub fn canon(&self) -> Option<&str> {
        self.metadata.canon.as_deref()
    }
    /// Return the optional version date.
    #[must_use]
    pub fn version_date(&self) -> Option<&str> {
        self.metadata.version_date.as_deref()
    }
    /// Return the resolved text-direction hint.
    #[must_use]
    pub const fn direction(&self) -> crate::source::TextDirectionHint {
        self.metadata.direction
    }
    /// Return nested source provenance, when available.
    #[must_use]
    pub fn source(&self) -> Option<&BibleSource> {
        self.metadata.source.as_ref()
    }
    /// Return all books in edition order.
    #[must_use]
    pub fn books(&self) -> &[Book] {
        &self.books
    }
    /// Iterate every verse in edition order.
    pub fn all_verses(&self) -> impl Iterator<Item = &Verse> {
        self.books
            .iter()
            .flat_map(|book| book.chapters())
            .flat_map(Chapter::get_verses)
    }
    /// Fetch a book by canonical identifier.
    pub fn get_book(&self, book: BibleBook) -> Result<&Book, BibleError> {
        self.index_by_book
            .get(&book)
            .map(|index| &self.books[*index])
            .ok_or_else(|| BibleError::BookNotFound {
                book_abbrev: book.abbreviation().to_string(),
                book_name: book.full_name().to_string(),
                translation: self.name().to_string(),
            })
    }
    /// Fetch a book by any supported compact, full, OSIS, or USFM identifier.
    pub fn get_book_by_abbrev(&self, identifier: &str) -> Result<&Book, BibleError> {
        let book = parse_book_identifier(identifier).ok_or_else(|| BibleError::BookNotFound {
            book_abbrev: identifier.to_ascii_lowercase(),
            book_name: identifier.to_string(),
            translation: self.name().to_string(),
        })?;
        self.get_book(book)
    }
    /// Fetch a book by 1-based edition position.
    pub fn get_book_by_id(&self, position: usize) -> Result<&Book, BibleError> {
        self.books
            .get(position.wrapping_sub(1))
            .ok_or_else(|| BibleError::BookNotFound {
                book_abbrev: position.to_string(),
                book_name: position.to_string(),
                translation: self.name().to_string(),
            })
    }
    /// Fetch a chapter by declared number.
    pub fn get_chapter(&self, book: BibleBook, chapter: usize) -> Result<&Chapter, BibleError> {
        self.get_book(book)?.get_chapter(chapter)
    }
    /// Fetch every verse in a declared chapter.
    pub fn get_verses(&self, book: BibleBook, chapter: usize) -> Result<&[Verse], BibleError> {
        self.get_book(book)?.get_verses(chapter)
    }
    /// Fetch a verse by declared coordinates.
    pub fn get_verse(
        &self,
        book: BibleBook,
        chapter: usize,
        verse: usize,
    ) -> Result<&Verse, BibleError> {
        self.get_book(book)?.get_verse(chapter, verse)
    }
    /// Fetch a chapter at a stable location.
    pub fn get_chapter_at(&self, location: BibleLocation) -> Result<&Chapter, BibleError> {
        self.get_chapter(location.book(), location.chapter())
    }
    /// Fetch a verse at a stable location.
    pub fn get_verse_at(&self, location: BibleLocation) -> Result<&Verse, BibleError> {
        self.get_verse(
            location.book(),
            location.chapter(),
            location.verse().ok_or(BibleError::VerseRequired)?,
        )
    }
    /// Return whether a declared chapter or verse exists.
    #[must_use]
    pub fn contains_reference(&self, location: BibleLocation) -> bool {
        location.verse().map_or_else(
            || self.get_chapter_at(location).is_ok(),
            |_| self.get_verse_at(location).is_ok(),
        )
    }

    /// Parse a narrow verse or range reference with edition aliases and order.
    pub fn parse_reference(&self, input: &str) -> Result<EditionReference, BibleError> {
        self.parse_edition_reference(input)
    }
    /// Parse a reference and retain language-detection and ambiguity metadata.
    pub fn parse_reference_detailed(
        &self,
        input: &str,
    ) -> Result<EditionParsed<EditionReference>, BibleError> {
        self.parse_edition_reference_detailed(input)
    }
    /// Parse a reference using one explicit language and this edition's order.
    pub fn parse_reference_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionReference, BibleError> {
        self.parse_edition_reference_with_language(input, language)
    }
    /// Parse a reference in one explicit language and retain parse metadata.
    pub fn parse_reference_detailed_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionParsed<EditionReference>, BibleError> {
        self.parse_edition_reference_detailed_with_language(input, language)
    }
    /// Parse the dependency's canonically ordered reference type directly.
    pub fn parse_canonical_reference(&self, input: &str) -> Result<Reference, ParseError> {
        self.reference_parser.parse(input)
    }
    /// Parse the dependency's canonical reference and retain detection metadata.
    pub fn parse_canonical_reference_detailed(
        &self,
        input: &str,
    ) -> Result<bible_io_references::Parsed<Reference>, ParseError> {
        self.reference_parser.parse_detailed(input)
    }
    /// Parse the dependency's canonical reference in one explicit language.
    pub fn parse_canonical_reference_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<Reference, ParseError> {
        self.reference_parser.parse_with_language(input, language)
    }
    /// Parse a canonical reference in one language and retain metadata.
    pub fn parse_canonical_reference_detailed_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<bible_io_references::Parsed<Reference>, ParseError> {
        self.reference_parser
            .parse_detailed_with_language(input, language)
    }
    /// Parse a verse or range while allowing the range to use this edition's
    /// declared book order rather than the dependency's canonical order.
    pub fn parse_edition_reference(&self, input: &str) -> Result<EditionReference, BibleError> {
        self.parse_edition_reference_impl(input, None)
            .map(EditionParsed::into_parts)
            .map(|(value, _)| value)
    }
    /// Parse an edition-aware reference with one explicit input language.
    pub fn parse_edition_reference_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionReference, BibleError> {
        self.parse_edition_reference_impl(input, Some(language))
            .map(EditionParsed::into_parts)
            .map(|(value, _)| value)
    }
    /// Parse an edition-aware reference while retaining dependency parse
    /// metadata when the shared parser can represent it.
    pub fn parse_edition_reference_detailed(
        &self,
        input: &str,
    ) -> Result<EditionParsed<EditionReference>, BibleError> {
        self.parse_edition_reference_impl(input, None)
    }
    /// Parse an edition-aware reference in one language and retain metadata.
    pub fn parse_edition_reference_detailed_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionParsed<EditionReference>, BibleError> {
        self.parse_edition_reference_impl(input, Some(language))
    }
    /// Parse a rich passage expression with edition aliases and book order.
    pub fn parse_passage(&self, input: &str) -> Result<EditionPassage, BibleError> {
        self.parse_edition_passage_impl(input, None)
            .map(EditionParsed::into_parts)
            .map(|(value, _)| value)
    }
    /// Parse a rich passage and retain language-detection metadata.
    pub fn parse_passage_detailed(
        &self,
        input: &str,
    ) -> Result<EditionParsed<EditionPassage>, BibleError> {
        self.parse_edition_passage_impl(input, None)
    }
    /// Parse a rich passage using one explicit language.
    pub fn parse_passage_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionPassage, BibleError> {
        self.parse_edition_passage_impl(input, Some(language))
            .map(EditionParsed::into_parts)
            .map(|(value, _)| value)
    }
    /// Parse a rich passage in one explicit language and retain metadata.
    pub fn parse_passage_detailed_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<EditionParsed<EditionPassage>, BibleError> {
        self.parse_edition_passage_impl(input, Some(language))
    }
    /// Parse the dependency's canonically ordered passage type directly.
    pub fn parse_canonical_passage(&self, input: &str) -> Result<Passage, ParseError> {
        self.passage_parser.parse(input)
    }
    /// Parse a canonical passage and retain language-detection metadata.
    pub fn parse_canonical_passage_detailed(
        &self,
        input: &str,
    ) -> Result<bible_io_references::Parsed<Passage>, ParseError> {
        self.passage_parser.parse_detailed(input)
    }
    /// Parse a canonical passage with one explicit input language.
    pub fn parse_canonical_passage_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<Passage, ParseError> {
        self.passage_parser.parse_with_language(input, language)
    }
    /// Parse a canonical passage in one language and retain metadata.
    pub fn parse_canonical_passage_detailed_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<bible_io_references::Parsed<Passage>, ParseError> {
        self.passage_parser
            .parse_detailed_with_language(input, language)
    }
    /// Retrieve a specific verse from a human-readable reference.
    pub fn get_verse_by_reference(&self, reference: &str) -> Result<&Verse, BibleError> {
        let input = reference.trim();
        let parsed = self.reference_parser.parse_verse(input).map_err(|error| {
            if error.kind() == ParseErrorKind::UnknownBook {
                BibleError::BookNotFound {
                    book_abbrev: reference_book_token(input).to_ascii_lowercase(),
                    book_name: reference_book_token(input).to_string(),
                    translation: self.name().to_string(),
                }
            } else {
                BibleError::ReferenceParse {
                    input: input.to_string(),
                    cause: error,
                }
            }
        })?;
        self.resolve_verse_reference(parsed)
    }
    /// Parse and resolve one verse, returning `None` for any invalid or absent reference.
    #[must_use]
    pub fn verse_or_none(&self, reference: &str) -> Option<&Verse> {
        self.get_verse_by_reference(reference).ok()
    }
    /// Resolve a typed verse coordinate.
    pub fn resolve_verse_reference(&self, reference: VerseRef) -> Result<&Verse, BibleError> {
        self.get_verse(
            reference.book(),
            usize::from(reference.chapter()),
            usize::from(reference.verse()),
        )
    }
    /// Resolve a typed narrow reference into edition-ordered verses.
    pub fn resolve_reference(&self, reference: Reference) -> Result<Vec<&Verse>, BibleError> {
        match reference {
            Reference::Verse(verse) => Ok(vec![self.resolve_verse_reference(verse)?]),
            Reference::Range(range) => self.resolve_verse_range(range),
        }
    }
    /// Resolve an edition-aware typed reference into edition-ordered verses.
    pub fn resolve_edition_reference(
        &self,
        reference: EditionReference,
    ) -> Result<Vec<&Verse>, BibleError> {
        match reference {
            EditionReference::Verse(verse) => Ok(vec![self.resolve_verse_reference(verse)?]),
            EditionReference::Range(range) => self.resolve_edition_range(range),
        }
    }
    /// Resolve a typed reference while preserving its single-versus-range shape.
    pub fn resolve_reference_result(
        &self,
        reference: Reference,
    ) -> Result<BibleReferenceResult<'_>, BibleError> {
        match reference {
            Reference::Verse(verse) => Ok(BibleReferenceResult::Verse(
                self.resolve_verse_reference(verse)?,
            )),
            Reference::Range(range) => Ok(BibleReferenceResult::Range(
                self.resolve_verse_range_selection(range)?,
            )),
        }
    }
    /// Resolve an edition-aware reference while retaining its result shape.
    pub fn resolve_edition_reference_result(
        &self,
        reference: EditionReference,
    ) -> Result<BibleReferenceResult<'_>, BibleError> {
        match reference {
            EditionReference::Verse(verse) => Ok(BibleReferenceResult::Verse(
                self.resolve_verse_reference(verse)?,
            )),
            EditionReference::Range(range) => Ok(BibleReferenceResult::Range(
                self.resolve_edition_range_selection(range)?,
            )),
        }
    }
    /// Resolve a typed reference into an immutable verse selection.
    pub fn resolve_reference_selection(
        &self,
        reference: Reference,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_reference(reference)
            .map(VerseSelection::from_vec)
    }
    /// Resolve an edition-aware reference into an immutable verse selection.
    pub fn resolve_edition_reference_selection(
        &self,
        reference: EditionReference,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_edition_reference(reference)
            .map(VerseSelection::from_vec)
    }
    /// Resolve an inclusive canonical range in loaded edition order.
    pub fn resolve_verse_range(&self, range: VerseRange) -> Result<Vec<&Verse>, BibleError> {
        self.resolve_edition_range(EditionVerseRange {
            start: range.start(),
            end: range.end(),
        })
    }
    /// Resolve an inclusive canonical range into an immutable selection.
    pub fn resolve_verse_range_selection(
        &self,
        range: VerseRange,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_verse_range(range)
            .map(VerseSelection::from_vec)
    }
    /// Resolve an inclusive range whose ordering follows this edition.
    pub fn resolve_edition_range(
        &self,
        range: EditionVerseRange,
    ) -> Result<Vec<&Verse>, BibleError> {
        self.resolve_verse_reference(range.start)?;
        self.resolve_verse_reference(range.end)?;
        let start = self.location_order(BibleLocation::from_verse_ref(range.start))?;
        let end = self.location_order(BibleLocation::from_verse_ref(range.end))?;
        if start > end {
            return Err(BibleError::InvalidRange {
                message: "verse range start must come before the end in edition order".to_string(),
            });
        }
        Ok(self
            .all_verses()
            .filter(|verse| {
                let order = self
                    .location_order(verse.location())
                    .expect("loaded verse has an order");
                order >= start && order <= end
            })
            .collect())
    }
    /// Resolve an edition-ordered range into an immutable selection.
    pub fn resolve_edition_range_selection(
        &self,
        range: EditionVerseRange,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_edition_range(range)
            .map(VerseSelection::from_vec)
    }
    /// Parse and resolve either one verse or an inclusive verse range.
    pub fn get_by_reference(&self, input: &str) -> Result<BibleReferenceResult<'_>, BibleError> {
        self.resolve_edition_reference_result(self.parse_reference(input)?)
    }
    /// Parse and resolve either one verse or a range in one explicit language.
    pub fn get_by_reference_with_language(
        &self,
        input: &str,
        language: Language,
    ) -> Result<BibleReferenceResult<'_>, BibleError> {
        self.resolve_edition_reference_result(self.parse_reference_with_language(input, language)?)
    }
    /// Parse and resolve a range, including a range ordered by custom bookOrder.
    pub fn get_verse_range_by_reference(&self, input: &str) -> Result<Vec<&Verse>, BibleError> {
        match self.reference_parser.parse_range(input) {
            Ok(range) => self.resolve_verse_range(range),
            Err(cause) => match self.parse_edition_range(input, None) {
                Ok(range) => self.resolve_edition_range(range),
                Err(_) => Err(BibleError::ReferenceParse {
                    input: input.trim().to_string(),
                    cause,
                }),
            },
        }
    }
    /// Parse and resolve a range into an immutable verse selection.
    pub fn get_verse_range_selection_by_reference(
        &self,
        input: &str,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.get_verse_range_by_reference(input)
            .map(VerseSelection::from_vec)
    }
    /// Parse and resolve a verse range, returning `None` on failure.
    #[must_use]
    pub fn verses_or_none(&self, input: &str) -> Option<Vec<&Verse>> {
        self.get_verse_range_by_reference(input).ok()
    }
    /// Resolve a rich passage and preserve selection/sequence duplicates.
    pub fn resolve_passage(&self, passage: &Passage) -> Result<Vec<&Verse>, BibleError> {
        match passage {
            Passage::Book(value) => self.resolve_book_passage(*value),
            Passage::Chapter(value) => self.resolve_chapter_passage(*value),
            Passage::Verses(value) => self.resolve_verse_passage(value),
            Passage::Sequence(value) => self.resolve_passage_sequence(value),
        }
    }
    /// Resolve a rich passage into an immutable, source-ordered selection.
    pub fn resolve_passage_selection(
        &self,
        passage: &Passage,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_passage(passage).map(VerseSelection::from_vec)
    }
    /// Resolve a passage parsed with this edition's custom canon semantics.
    pub fn resolve_edition_passage(
        &self,
        passage: &EditionPassage,
    ) -> Result<Vec<&Verse>, BibleError> {
        match passage {
            EditionPassage::Standard(passage) => self.resolve_passage(passage),
            EditionPassage::Reference(reference) => self.resolve_edition_reference(*reference),
        }
    }
    /// Resolve an edition-aware passage into an immutable selection.
    pub fn resolve_edition_passage_selection(
        &self,
        passage: &EditionPassage,
    ) -> Result<VerseSelection<'_>, BibleError> {
        self.resolve_edition_passage(passage)
            .map(VerseSelection::from_vec)
    }
    /// Parse and resolve a rich passage expression.
    pub fn get_passage(&self, input: &str) -> Result<Vec<&Verse>, BibleError> {
        let passage = self.parse_passage(input)?;
        self.resolve_edition_passage(&passage)
    }
    /// Parse and resolve a rich passage into an immutable selection.
    pub fn get_passage_selection(&self, input: &str) -> Result<VerseSelection<'_>, BibleError> {
        let passage = self.parse_passage(input)?;
        self.resolve_edition_passage_selection(&passage)
    }

    fn resolve_book_passage(&self, passage: BookPassage) -> Result<Vec<&Verse>, BibleError> {
        Ok(self
            .get_book(passage.book())?
            .chapters()
            .iter()
            .flat_map(Chapter::get_verses)
            .collect())
    }
    fn resolve_chapter_passage(&self, passage: ChapterPassage) -> Result<Vec<&Verse>, BibleError> {
        let end = passage.end_chapter().unwrap_or(passage.start_chapter());
        let mut verses = Vec::new();
        for chapter in passage.start_chapter()..=end {
            verses.extend(self.get_verses(passage.book(), usize::from(chapter))?);
        }
        Ok(verses)
    }
    fn resolve_verse_passage(&self, passage: &VersePassage) -> Result<Vec<&Verse>, BibleError> {
        let mut verses = Vec::new();
        for reference in passage.selections() {
            verses.extend(self.resolve_reference(*reference)?);
        }
        Ok(verses)
    }
    fn resolve_passage_sequence(
        &self,
        sequence: &PassageSequence,
    ) -> Result<Vec<&Verse>, BibleError> {
        let mut verses = Vec::new();
        for passage in sequence.passages() {
            verses.extend(self.resolve_passage(passage)?);
        }
        Ok(verses)
    }

    /// Format a loaded location with its edition-specific book name.
    pub fn format_location(&self, location: BibleLocation) -> Result<String, BibleError> {
        if location.has_verse() {
            self.get_verse_at(location)?;
        } else {
            self.get_chapter_at(location)?;
        }
        let name = self.get_book(location.book())?.title();
        Ok(location.verse().map_or_else(
            || format!("{name} {}", location.chapter()),
            |verse| format!("{name} {}:{verse}", location.chapter()),
        ))
    }
    /// Format with a chosen reference-package formatter.
    pub fn format_location_with(
        &self,
        location: BibleLocation,
        formatter: ReferenceFormatter,
    ) -> Result<String, BibleError> {
        if location.has_verse() {
            let reference = location
                .to_verse_ref()
                .map_err(|_| BibleError::VerseRequired)?;
            self.resolve_verse_reference(reference)?;
            Ok(formatter.format(reference).to_string())
        } else {
            self.get_chapter_at(location)?;
            Ok(format!(
                "{} {}",
                formatter.book_name(location.book()),
                location.chapter()
            ))
        }
    }
    /// Create an edition-aware persisted-state key for a loaded verse.
    pub fn key_for_verse(&self, verse: &Verse) -> Result<BibleVerseKey, BibleError> {
        let id = self
            .metadata
            .id
            .as_deref()
            .filter(|id| !id.trim().is_empty())
            .ok_or(BibleError::MissingEditionId)?;
        if self.get_verse(verse.book(), verse.chapter(), verse.number())? != verse {
            return Err(BibleError::InvalidReference {
                input: "verse does not belong to this edition".to_string(),
            });
        }
        BibleVerseKey::from_verse(id, verse).map_err(|error| BibleError::InvalidReference {
            input: error.to_string(),
        })
    }
    /// Create a persisted key from a validated verse location.
    pub fn key_for_location(&self, location: BibleLocation) -> Result<BibleVerseKey, BibleError> {
        self.key_for_verse(self.get_verse_at(location)?)
    }

    /// Return the next declared chapter, crossing book boundaries.
    pub fn next_chapter(
        &self,
        current: BibleLocation,
    ) -> Result<Option<BibleLocation>, BibleError> {
        self.adjacent_chapter(current, true)
    }
    /// Return the previous declared chapter, crossing book boundaries.
    pub fn previous_chapter(
        &self,
        current: BibleLocation,
    ) -> Result<Option<BibleLocation>, BibleError> {
        self.adjacent_chapter(current, false)
    }
    /// Return whether another declared chapter follows this location.
    pub fn has_next_chapter(&self, current: BibleLocation) -> Result<bool, BibleError> {
        self.next_chapter(current).map(|value| value.is_some())
    }
    /// Return whether another declared chapter precedes this location.
    pub fn has_previous_chapter(&self, current: BibleLocation) -> Result<bool, BibleError> {
        self.previous_chapter(current).map(|value| value.is_some())
    }
    fn adjacent_chapter(
        &self,
        current: BibleLocation,
        next: bool,
    ) -> Result<Option<BibleLocation>, BibleError> {
        let book_index = *self
            .index_by_book
            .get(&current.book())
            .ok_or_else(|| self.missing_book(current.book()))?;
        let book = &self.books[book_index];
        let chapter_index = book
            .chapters()
            .binary_search_by_key(&current.chapter(), Chapter::number)
            .map_err(|_| book.get_chapter(current.chapter()).unwrap_err())?;
        if next {
            if let Some(chapter) = book.chapters().get(chapter_index + 1) {
                return Ok(Some(
                    BibleLocation::new(current.book(), chapter.number(), None).unwrap(),
                ));
            }
            for book in &self.books[book_index + 1..] {
                if let Some(chapter) = book.chapters().first() {
                    return Ok(Some(
                        BibleLocation::new(book.book(), chapter.number(), None).unwrap(),
                    ));
                }
            }
        } else {
            if chapter_index > 0 {
                let chapter = &book.chapters()[chapter_index - 1];
                return Ok(Some(
                    BibleLocation::new(current.book(), chapter.number(), None).unwrap(),
                ));
            }
            for book in self.books[..book_index].iter().rev() {
                if let Some(chapter) = book.chapters().last() {
                    return Ok(Some(
                        BibleLocation::new(book.book(), chapter.number(), None).unwrap(),
                    ));
                }
            }
        }
        Ok(None)
    }
    /// Return the next declared verse, crossing sparse chapters and books.
    pub fn next_verse(&self, current: BibleLocation) -> Result<Option<BibleLocation>, BibleError> {
        self.adjacent_verse(current, true)
    }
    /// Return the previous declared verse.
    pub fn previous_verse(
        &self,
        current: BibleLocation,
    ) -> Result<Option<BibleLocation>, BibleError> {
        self.adjacent_verse(current, false)
    }
    /// Return whether another declared verse follows this location.
    pub fn has_next_verse(&self, current: BibleLocation) -> Result<bool, BibleError> {
        self.next_verse(current).map(|value| value.is_some())
    }
    /// Return whether another declared verse precedes this location.
    pub fn has_previous_verse(&self, current: BibleLocation) -> Result<bool, BibleError> {
        self.previous_verse(current).map(|value| value.is_some())
    }
    fn adjacent_verse(
        &self,
        current: BibleLocation,
        next: bool,
    ) -> Result<Option<BibleLocation>, BibleError> {
        self.get_verse_at(current)?;
        let locations: Vec<_> = self.all_verses().map(Verse::location).collect();
        let index = locations
            .iter()
            .position(|location| *location == current)
            .expect("validated location is loaded");
        Ok(if next {
            locations.get(index + 1).copied()
        } else {
            index
                .checked_sub(1)
                .and_then(|index| locations.get(index).copied())
        })
    }

    /// Return whether a search index is currently retained.
    #[must_use]
    pub fn has_search_index(&self) -> bool {
        self.search_index
            .read()
            .expect("search lock poisoned")
            .is_some()
    }
    /// Build and retain the index unless indexing is disabled.
    pub fn prewarm_search_index(&self) {
        if self.search_index_mode != SearchIndexMode::Disabled && !self.has_search_index() {
            *self.search_index.write().expect("search lock poisoned") =
                Some(self.build_search_index());
        }
    }
    /// Release the retained index.
    pub fn clear_search_index(&self) {
        *self.search_index.write().expect("search lock poisoned") = None;
    }
    /// Build a reusable default all-term index.
    #[must_use]
    pub fn build_search_index(&self) -> SearchIndex {
        let mut index: HashMap<String, Vec<_>> = HashMap::new();
        for verse in self.all_verses() {
            let location = (verse.book(), verse.chapter(), verse.number());
            for term in build_search_index_terms(verse.text(), 3) {
                index.entry(term).or_default().push(location);
            }
        }
        SearchIndex::new(index)
    }
    /// Fast all-distinct-term search in stable edition order.
    #[must_use]
    pub fn search(&self, query: &str) -> Vec<Verse> {
        let options = SearchOptions {
            mode: crate::search::SearchMode::All,
            ..SearchOptions::default()
        };
        if tokenize_search_text(query, false, true, false).is_empty() {
            return Vec::new();
        }
        if self.search_index_mode == SearchIndexMode::Disabled {
            return self.search_internal(query, &options, None).into_verses();
        }
        self.prewarm_search_index();
        self.search_index
            .read()
            .expect("search lock poisoned")
            .as_ref()
            .expect("index was prewarmed")
            .search(query)
            .into_iter()
            .filter_map(|(book, chapter, verse)| self.get_verse(book, chapter, verse).ok())
            .filter(|verse| matches_search_text(verse.text(), query, &options))
            .cloned()
            .collect()
    }
    /// Return loaded books paired with their zero-based edition positions.
    pub fn books_with_index(&self) -> impl Iterator<Item = (usize, &Book)> {
        self.books.iter().enumerate()
    }
    /// Return books containing one complete normalized word.
    #[must_use]
    pub fn books_containing(&self, word: &str) -> Vec<&Book> {
        self.books
            .iter()
            .filter(|book| !book.chapters_containing(word).is_empty())
            .collect()
    }
    /// Advanced search with explicit modes, scope, normalization, and paging.
    pub fn search_with_options(
        &self,
        query: &str,
        options: &SearchOptions,
    ) -> Result<SearchResults, ModelError> {
        options
            .validate()
            .map_err(|message| ModelError::new("options", message))?;
        Ok(self.search_internal(query, options, None))
    }
    /// Typo-tolerant search using bounded Unicode-scalar edit distance.
    pub fn fuzzy_search(
        &self,
        query: &str,
        max_distance: usize,
        options: &SearchOptions,
    ) -> Result<SearchResults, ModelError> {
        options
            .validate()
            .map_err(|message| ModelError::new("options", message))?;
        Ok(self.search_internal(query, options, Some(max_distance)))
    }
    fn search_internal(
        &self,
        query: &str,
        options: &SearchOptions,
        fuzzy: Option<usize>,
    ) -> SearchResults {
        let has_text = !query.trim().is_empty();
        let limit = options.max_results.unwrap_or(usize::MAX);
        let mut matched_count = 0_usize;
        let mut page = Vec::new();
        let mut has_more = false;
        for verse in self.all_verses().filter(|verse| {
            self.matches_scope(verse, options)
                && if !has_text {
                    fuzzy.is_none()
                } else if let Some(distance) = fuzzy {
                    fuzzy_matches(verse.text(), query, options, distance)
                } else {
                    matches_search_text(verse.text(), query, options)
                }
        }) {
            if matched_count < options.offset {
                matched_count += 1;
                continue;
            }
            if page.len() == limit {
                has_more = true;
                break;
            }
            page.push(verse);
            matched_count += 1;
        }
        let total_count = (!has_more).then_some(matched_count);
        let hits: Vec<_> = page
            .iter()
            .map(|verse| {
                let ranges = fuzzy.map_or_else(
                    || find_match_ranges(verse.text(), query, options),
                    |distance| {
                        fuzzy_match_ranges(verse.text(), query, options, distance)
                            .unwrap_or_default()
                    },
                );
                SearchHit::with_context(
                    (*verse).clone(),
                    self.get_book(verse.book()).expect("loaded book"),
                    ranges,
                    160,
                )
                .expect("internally constructed hit is valid")
            })
            .collect();
        SearchResults::from_hits(
            query,
            hits,
            options.offset,
            options.max_results,
            total_count,
            has_more,
        )
        .expect("internally constructed search page is valid")
    }
    fn matches_scope(&self, verse: &Verse, options: &SearchOptions) -> bool {
        options.book.is_none_or(|book| book == verse.book())
            && options
                .chapter
                .is_none_or(|chapter| chapter == verse.chapter())
            && options.verse.is_none_or(|number| number == verse.number())
    }

    /// Return aggregate counts and averages.
    #[must_use]
    pub fn stats(&self) -> BibleStats {
        let verses: Vec<_> = self.all_verses().collect();
        let total_characters: usize = verses.iter().map(|verse| verse.len()).sum();
        let mut verses_per_book = self
            .books
            .iter()
            .map(|book| (book.book(), 0))
            .collect::<HashMap<_, _>>();
        for verse in &verses {
            *verses_per_book.entry(verse.book()).or_insert(0) += 1;
        }
        BibleStats {
            book_count: self.books.len(),
            chapter_count: self.books.iter().map(|book| book.chapters().len()).sum(),
            verse_count: verses.len(),
            total_words: verses
                .iter()
                .map(|verse| extract_unicode_words(verse.text()).len())
                .sum(),
            average_verse_length: if verses.is_empty() {
                0
            } else {
                (total_characters as f64 / verses.len() as f64).round() as usize
            },
            verses_per_book,
        }
    }
    /// Return load and retained-index diagnostics.
    #[must_use]
    pub fn performance_metrics(&self) -> BiblePerformanceMetrics {
        let guard = self.search_index.read().expect("search lock poisoned");
        let verses: Vec<_> = self.all_verses().collect();
        let text_bytes = verses.iter().map(|verse| verse.text().len()).sum::<usize>();
        let posting_count = guard.as_ref().map_or(0, SearchIndex::posting_count);
        let search_index_size = guard.as_ref().map_or(0, SearchIndex::len);
        BiblePerformanceMetrics {
            load_time: self.load_time,
            search_index_size,
            search_index_built: guard.is_some(),
            verse_count: verses.len(),
            posting_count,
            text_bytes,
            memory_usage_kib: (text_bytes + verses.len() * 64 + posting_count * 24).div_ceil(1024),
        }
    }
    /// Serialize the complete versioned content contract without data loss.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(&self.to_json_value())
    }
    /// Return the complete versioned content as a JSON value.
    #[must_use]
    pub fn to_json_value(&self) -> Value {
        let mut root = self.annotations.clone();
        root.insert(
            "schemaVersion".to_string(),
            Value::from(self.schema_version),
        );
        root.insert(
            "language".to_string(),
            Value::String(self.language.display_name().to_string()),
        );
        root.insert(
            "metadata".to_string(),
            serde_json::to_value(&self.metadata).expect("metadata contains JSON values"),
        );
        root.insert(
            "bookOrder".to_string(),
            Value::Array(
                self.books
                    .iter()
                    .map(|book| Value::String(book.abbrev().to_string()))
                    .collect(),
            ),
        );
        root.insert(
            "books".to_string(),
            Value::Object(
                self.books
                    .iter()
                    .map(|book| (book.abbrev().to_string(), book.to_json_value()))
                    .collect(),
            ),
        );
        Value::Object(root)
    }

    fn parse_edition_reference_impl(
        &self,
        input: &str,
        language: Option<Language>,
    ) -> Result<EditionParsed<EditionReference>, BibleError> {
        let parsed = match language {
            Some(language) => self
                .reference_parser
                .parse_detailed_with_language(input, language),
            None => self.reference_parser.parse_detailed(input),
        };
        match parsed {
            Ok(parsed) => {
                let (reference, metadata) = parsed.into_parts();
                let reference = match reference {
                    Reference::Verse(verse) => EditionReference::Verse(verse),
                    Reference::Range(range) => EditionReference::Range(EditionVerseRange {
                        start: range.start(),
                        end: range.end(),
                    }),
                };
                Ok(EditionParsed::new(reference, Some(metadata)))
            }
            Err(cause) if cause.kind() == ParseErrorKind::CrossBookRangeNotAscending => self
                .parse_edition_range(input, language)
                .map(|range| EditionParsed::new(EditionReference::Range(range), None))
                .map_err(|_| BibleError::ReferenceParse {
                    input: input.trim().to_string(),
                    cause,
                }),
            Err(cause) => Err(BibleError::ReferenceParse {
                input: input.trim().to_string(),
                cause,
            }),
        }
    }

    fn parse_edition_passage_impl(
        &self,
        input: &str,
        language: Option<Language>,
    ) -> Result<EditionParsed<EditionPassage>, BibleError> {
        let parsed = match language {
            Some(language) => self
                .passage_parser
                .parse_detailed_with_language(input, language),
            None => self.passage_parser.parse_detailed(input),
        };
        match parsed {
            Ok(parsed) => {
                let (passage, metadata) = parsed.into_parts();
                Ok(EditionParsed::new(
                    EditionPassage::Standard(passage),
                    Some(metadata),
                ))
            }
            Err(cause) if cause.kind() == ParseErrorKind::CrossBookRangeNotAscending => self
                .parse_edition_range(input, language)
                .map(|range| {
                    EditionParsed::new(
                        EditionPassage::Reference(EditionReference::Range(range)),
                        None,
                    )
                })
                .map_err(|_| BibleError::ReferenceParse {
                    input: input.trim().to_string(),
                    cause,
                }),
            Err(cause) => Err(BibleError::ReferenceParse {
                input: input.trim().to_string(),
                cause,
            }),
        }
    }

    fn parse_edition_range(
        &self,
        input: &str,
        language: Option<Language>,
    ) -> Result<EditionVerseRange, BibleError> {
        for (index, character) in input
            .char_indices()
            .filter(|(_, character)| matches!(character, '-' | '–' | '—' | '―'))
        {
            let left = input[..index].trim();
            let right = input[index + character.len_utf8()..].trim();
            let parse_verse = |value| match language {
                Some(language) => self
                    .reference_parser
                    .parse_verse_with_language(value, language),
                None => self.reference_parser.parse_verse(value),
            };
            if let (Ok(start), Ok(end)) = (parse_verse(left), parse_verse(right)) {
                let range = EditionVerseRange { start, end };
                if let (Ok(start_order), Ok(end_order)) = (
                    self.location_order(BibleLocation::from_verse_ref(start)),
                    self.location_order(BibleLocation::from_verse_ref(end)),
                ) {
                    if start.book() != end.book() && start_order < end_order {
                        return Ok(range);
                    }
                }
            }
        }
        Err(BibleError::InvalidReference {
            input: input.to_string(),
        })
    }
    fn location_order(&self, location: BibleLocation) -> Result<(usize, usize, usize), BibleError> {
        let book = *self
            .index_by_book
            .get(&location.book())
            .ok_or_else(|| self.missing_book(location.book()))?;
        Ok((book, location.chapter(), location.verse().unwrap_or(0)))
    }
    fn missing_book(&self, book: BibleBook) -> BibleError {
        BibleError::BookNotFound {
            book_abbrev: book.abbreviation().to_string(),
            book_name: book.full_name().to_string(),
            translation: self.name().to_string(),
        }
    }
}

static EMPTY_MAP: OnceLock<Map<String, Value>> = OnceLock::new();

fn build_reference_parser(
    books: &[Book],
    language: Language,
) -> Result<ReferenceParser, BibleDataFormatError> {
    let mut builder = ReferenceParser::builder();
    if !language.is_auto() && language.is_parsing_supported() {
        builder = builder.preferred_languages([language]);
    }
    for &(alias, book) in LEGACY_REFERENCE_ALIASES {
        builder = builder.language_alias(Language::English, alias, book);
    }
    for book in books {
        if !book.title().eq_ignore_ascii_case(book.book().full_name()) {
            builder = if language.is_auto() {
                builder.alias(book.title(), book.book())
            } else {
                builder.language_alias(language, book.title(), book.book())
            };
        }
    }
    builder.build().map_err(|error| {
        BibleDataFormatError::new(
            BibleDataFormatErrorCode::InvalidValue,
            "$.books",
            "loaded book names create an ambiguous reference alias",
        )
        .with_cause(error)
    })
}

fn read_metadata(
    root: &Map<String, Value>,
    source: Option<&BibleSource>,
) -> Result<BibleMetadata, BibleDataFormatError> {
    let document = Value::Object(root.clone());
    let mut metadata = BibleMetadata::from_document_value(&document, source)?;
    // Root-level unknown fields belong to Bible annotations, not the nested
    // metadata extension map. Keep only values that originated in metadata.
    if let Some(Value::Object(nested)) = root.get("metadata") {
        metadata
            .additional
            .retain(|key, _| nested.contains_key(key));
    } else {
        metadata.additional.clear();
    }
    Ok(metadata)
}

fn resolve_language(
    raw: Option<&Value>,
    metadata: &BibleMetadata,
) -> Result<Language, BibleDataFormatError> {
    if let Some(value) = raw {
        if !value.is_string() {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                "$.language",
                "Bible language must be a string",
                Some(value),
            ));
        }
    }
    for candidate in [
        raw.and_then(Value::as_str),
        metadata.language_code.as_deref(),
        metadata.language_name.as_deref(),
    ]
    .into_iter()
    .flatten()
    {
        if let Ok(language) = candidate.parse() {
            return Ok(language);
        }
    }
    Ok(Language::Auto)
}

fn read_book(
    book: BibleBook,
    value: &Value,
    path: &str,
    validation: BibleDataValidationOptions,
) -> Result<Book, BibleDataFormatError> {
    let object = value.as_object().ok_or_else(|| {
        data_error(
            BibleDataFormatErrorCode::InvalidType,
            path,
            "a Bible book must be an object",
            Some(value),
        )
    })?;
    let title = match object.get("name") {
        None => book.full_name().to_string(),
        Some(Value::String(name)) if !name.trim().is_empty() => name.clone(),
        Some(Value::String(name)) => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidValue,
                &format!("{path}.name"),
                "book name must be a non-blank string",
                Some(&Value::String(name.clone())),
            ));
        }
        other => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                &format!("{path}.name"),
                "book name must be a non-blank string",
                other,
            ));
        }
    };

    let chapters_value = object.get("chapters");
    let mut chapters = Vec::new();
    match chapters_value {
        Some(Value::Object(chapters_object)) => {
            if validation.require_chapters && chapters_object.is_empty() {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &format!("{path}.chapters"),
                    "a Bible book must contain at least one chapter",
                    chapters_value,
                ));
            }
            let mut numbers = HashSet::new();
            for (key, chapter) in chapters_object {
                let chapter_path = json_path(&format!("{path}.chapters"), key);
                let number = positive_key(key, &chapter_path)?;
                if !numbers.insert(number) {
                    return Err(data_error(
                        BibleDataFormatErrorCode::InvalidValue,
                        &chapter_path,
                        "duplicate numeric chapter number",
                        Some(&Value::String(key.clone())),
                    ));
                }
                chapters.push(read_chapter(
                    book,
                    number,
                    chapter,
                    &chapter_path,
                    validation,
                )?);
            }
        }
        // Preserve the package's pre-1.1 array input while always serializing
        // the unambiguous versioned map representation.
        Some(Value::Array(chapter_values)) => {
            if validation.require_chapters && chapter_values.is_empty() {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &format!("{path}.chapters"),
                    "a Bible book must contain at least one chapter",
                    chapters_value,
                ));
            }
            for (index, chapter) in chapter_values.iter().enumerate() {
                let number = index + 1;
                chapters.push(read_chapter(
                    book,
                    number,
                    chapter,
                    &format!("{path}.chapters[{index}]"),
                    validation,
                )?);
            }
        }
        None if !validation.require_chapters => {}
        None => {
            return Err(BibleDataFormatError::new(
                BibleDataFormatErrorCode::MissingField,
                format!("{path}.chapters"),
                "a Bible book must declare chapters",
            ));
        }
        other => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                &format!("{path}.chapters"),
                "book chapters must be an object or legacy array",
                other,
            ));
        }
    }
    Book::checked(
        book,
        title,
        chapters,
        additional_fields(object, &["name", "chapters"]),
    )
    .map_err(|error| {
        BibleDataFormatError::new(
            BibleDataFormatErrorCode::InvalidValue,
            path,
            "Bible book violates model invariants",
        )
        .with_cause(error)
    })
}

fn read_chapter(
    book: BibleBook,
    number: usize,
    value: &Value,
    path: &str,
    validation: BibleDataValidationOptions,
) -> Result<Chapter, BibleDataFormatError> {
    let (verses_value, verses_path, annotations) = match value {
        Value::Object(object) if object.contains_key("verses") => (
            object.get("verses"),
            format!("{path}.verses"),
            additional_fields(object, &["verses"]),
        ),
        Value::Object(_) | Value::Array(_) => (Some(value), path.to_string(), Map::new()),
        _ => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                path,
                "a Bible chapter must be an object or legacy array",
                Some(value),
            ));
        }
    };
    let mut verses = Vec::new();
    match verses_value {
        Some(Value::Object(verses_object)) => {
            if validation.require_verses && verses_object.is_empty() {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &verses_path,
                    "a Bible chapter must contain at least one verse",
                    verses_value,
                ));
            }
            let mut numbers = HashSet::new();
            for (key, verse) in verses_object {
                let verse_path = json_path(&verses_path, key);
                let verse_number = positive_key(key, &verse_path)?;
                if !numbers.insert(verse_number) {
                    return Err(data_error(
                        BibleDataFormatErrorCode::InvalidValue,
                        &verse_path,
                        "duplicate numeric verse number",
                        Some(&Value::String(key.clone())),
                    ));
                }
                verses.push(read_verse(
                    book,
                    number,
                    verse_number,
                    verse,
                    &verse_path,
                    validation,
                )?);
            }
        }
        Some(Value::Array(verse_values)) => {
            if validation.require_verses && verse_values.is_empty() {
                return Err(data_error(
                    BibleDataFormatErrorCode::InvalidValue,
                    &verses_path,
                    "a Bible chapter must contain at least one verse",
                    verses_value,
                ));
            }
            for (index, verse) in verse_values.iter().enumerate() {
                verses.push(read_verse(
                    book,
                    number,
                    index + 1,
                    verse,
                    &format!("{verses_path}[{index}]"),
                    validation,
                )?);
            }
        }
        other => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                &verses_path,
                "chapter verses must be an object or legacy array",
                other,
            ));
        }
    }
    Chapter::checked(book, number, verses, annotations).map_err(|error| {
        BibleDataFormatError::new(
            BibleDataFormatErrorCode::InvalidValue,
            path,
            "Bible chapter violates model invariants",
        )
        .with_cause(error)
    })
}

fn read_verse(
    book: BibleBook,
    chapter: usize,
    number: usize,
    value: &Value,
    path: &str,
    validation: BibleDataValidationOptions,
) -> Result<Verse, BibleDataFormatError> {
    let (text, annotations, text_path) = match value {
        Value::String(text) => (text.clone(), Map::new(), path.to_string()),
        Value::Object(object) => {
            let text = match object.get("text") {
                Some(Value::String(text)) => text.clone(),
                None => {
                    return Err(BibleDataFormatError::new(
                        BibleDataFormatErrorCode::MissingField,
                        format!("{path}.text"),
                        "an annotated verse must declare text",
                    ))
                }
                other => {
                    return Err(data_error(
                        BibleDataFormatErrorCode::InvalidType,
                        &format!("{path}.text"),
                        "verse text must be a string",
                        other,
                    ))
                }
            };
            (
                text,
                additional_fields(object, &["text"]),
                format!("{path}.text"),
            )
        }
        _ => {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidType,
                path,
                "a verse must be a string or object containing text",
                Some(value),
            ))
        }
    };
    if validation.require_verse_text && text.trim().is_empty() {
        return Err(data_error(
            BibleDataFormatErrorCode::InvalidValue,
            &text_path,
            "verse text must not be blank",
            Some(&Value::String(text.clone())),
        ));
    }
    Verse::checked(book, chapter, number, text, annotations).map_err(|error| {
        BibleDataFormatError::new(
            BibleDataFormatErrorCode::InvalidValue,
            path,
            "Bible verse violates model invariants",
        )
        .with_cause(error)
    })
}

fn read_book_order(
    raw: Option<&Value>,
    books: &HashMap<BibleBook, Book>,
    declared_order: &[BibleBook],
) -> Result<Vec<BibleBook>, BibleDataFormatError> {
    let Some(raw) = raw else {
        let mut order: Vec<_> = books.keys().copied().collect();
        order.sort();
        return Ok(order);
    };
    let values = raw.as_array().ok_or_else(|| {
        data_error(
            BibleDataFormatErrorCode::InvalidType,
            "$.bookOrder",
            "bookOrder must be an array",
            Some(raw),
        )
    })?;
    let mut order = Vec::new();
    let mut seen = HashSet::new();
    for (index, value) in values.iter().enumerate() {
        let path = format!("$.bookOrder[{index}]");
        let identifier = value.as_str().ok_or_else(|| {
            data_error(
                BibleDataFormatErrorCode::InvalidType,
                &path,
                "bookOrder items must be strings",
                Some(value),
            )
        })?;
        if identifier.trim().is_empty() {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidValue,
                &path,
                "bookOrder items must be non-blank strings",
                Some(value),
            ));
        }
        let book = parse_book_identifier(identifier).ok_or_else(|| {
            data_error(
                BibleDataFormatErrorCode::InvalidValue,
                &path,
                "unsupported Bible book identifier",
                Some(value),
            )
        })?;
        if !books.contains_key(&book) || !seen.insert(book) {
            return Err(data_error(
                BibleDataFormatErrorCode::InvalidValue,
                &path,
                "bookOrder references an absent or duplicate book",
                Some(value),
            ));
        }
        order.push(book);
    }
    if order.len() != books.len() {
        let missing = declared_order
            .iter()
            .filter(|book| !seen.contains(book))
            .map(|book| Value::String(book.abbreviation().to_string()))
            .collect();
        return Err(BibleDataFormatError::new(
            BibleDataFormatErrorCode::InvalidValue,
            "$.bookOrder",
            "bookOrder must list every loaded book exactly once",
        )
        .with_value(Value::Array(missing)));
    }
    Ok(order)
}

fn validate_aliases(books: &[Book]) -> Result<(), BibleDataFormatError> {
    let mut owners = HashMap::new();
    for book in books {
        for term in [
            book.book().full_name(),
            book.book().abbreviation(),
            book.title(),
        ] {
            let normalized = normalize_search_text(
                &term.split_whitespace().collect::<Vec<_>>().join(" "),
                false,
                true,
                false,
            );
            if let Some(owner) = owners.insert(normalized, book.book()) {
                if owner != book.book() {
                    return Err(BibleDataFormatError::new(
                        BibleDataFormatErrorCode::InvalidValue,
                        "$.books",
                        "loaded book names create an ambiguous reference alias",
                    ));
                }
            }
        }
    }
    Ok(())
}
fn positive_key(value: &str, path: &str) -> Result<usize, BibleDataFormatError> {
    value
        .trim()
        .parse::<i64>()
        .ok()
        .filter(|value| *value > 0)
        .and_then(|value| usize::try_from(value).ok())
        .ok_or_else(|| {
            data_error(
                BibleDataFormatErrorCode::InvalidValue,
                path,
                "chapter and verse keys must be positive integers",
                Some(&Value::String(value.to_string())),
            )
        })
}
fn additional_fields(object: &Map<String, Value>, structural: &[&str]) -> Map<String, Value> {
    object
        .iter()
        .filter(|(key, _)| !structural.contains(&key.as_str()))
        .map(|(key, value)| (key.clone(), value.clone()))
        .collect()
}
fn data_error(
    code: BibleDataFormatErrorCode,
    path: &str,
    message: &str,
    value: Option<&Value>,
) -> BibleDataFormatError {
    let error = BibleDataFormatError::new(code, path, message);
    value.map_or(error.clone(), |value| error.with_value(value.clone()))
}
fn json_path(base: &str, key: &str) -> String {
    if key.chars().enumerate().all(|(index, character)| {
        character == '_'
            || character.is_ascii_alphanumeric() && (index > 0 || !character.is_ascii_digit())
    }) {
        format!("{base}.{key}")
    } else {
        format!("{base}[{}]", serde_json::to_string(key).unwrap())
    }
}
fn reference_book_token(reference: &str) -> &str {
    let before = reference
        .rfind([':', '.'])
        .map_or(reference, |index| &reference[..index]);
    let book = before
        .trim_end_matches(|character: char| character.is_whitespace() || character.is_numeric());
    if book.is_empty() {
        reference.trim()
    } else {
        book.trim()
    }
}

const ROOT_FIELDS: &[&str] = &[
    "schemaVersion",
    "bookOrder",
    "books",
    "metadata",
    "source",
    "id",
    "editionId",
    "edition_id",
    "description",
    "summary",
    "language",
    "languageName",
    "language_name",
    "languageCode",
    "language_code",
    "lang",
    "translationName",
    "translation_name",
    "name",
    "title",
    "version",
    "abbreviation",
    "abbr",
    "shortName",
    "short_name",
    "year",
    "direction",
    "textDirection",
    "text_direction",
    "sourceName",
    "source_name",
    "copyright",
    "license",
    "canon",
    "versionDate",
    "version_date",
    "date",
];