ferrocat-po 3.0.0

Performance-first PO parsing, serialization, and catalog update primitives for ferrocat.
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
use std::collections::{BTreeMap, btree_map};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};

use rustc_hash::{FxHashMap, FxHasher};

use crate::{ParseError, PoVec, diagnostic_codes::DiagnosticCode};

use super::mt::MachineMetadata;
use super::plural::PluralProfile;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Source origin metadata for an extracted message.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct CatalogOrigin {
    /// Path-like source file identifier where the message came from.
    ///
    /// Ferrocat intentionally tracks no line number: line numbers shift on every
    /// edit above a message and add diff and merge churn without identifying
    /// anything the `(msgid, msgctxt)` key does not already.
    pub file: String,
    /// Optional stable scope within the file, such as the enclosing component,
    /// function, class, route handler, or similar named authoring unit.
    ///
    /// Unlike a line number it survives edits, so it adds context for
    /// translators and tools without churn. It is metadata, not message
    /// identity, and it is not a replacement for gettext context / `msgctxt`.
    /// Producers (e.g. extractors) fill it with values like `CheckoutButton`,
    /// `formatInvoiceStatus`, or `SettingsPage`; serialized with the file as
    /// `file#scope`.
    pub scope: Option<String>,
}

/// Structured singular message input used by catalog update operations.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ExtractedSingularMessage {
    /// Source message identifier.
    pub msgid: String,
    /// Optional gettext message context.
    pub msgctxt: Option<String>,
    /// Extracted comments that should become translator-facing guidance.
    pub comments: Vec<String>,
    /// Source locations collected by the extractor.
    pub origin: Vec<CatalogOrigin>,
    /// Placeholder hints keyed by placeholder name.
    pub placeholders: BTreeMap<String, Vec<String>>,
}

/// Source-side plural forms for structured catalog messages.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct PluralSource {
    /// Singular source form, when one exists separately from `other`.
    pub one: Option<String>,
    /// Required plural catch-all source form.
    pub other: String,
}

/// Structured plural message input used by catalog update operations.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ExtractedPluralMessage {
    /// Stable source identifier for the message family.
    pub msgid: String,
    /// Optional gettext message context.
    pub msgctxt: Option<String>,
    /// Structured source-side plural forms.
    pub source: PluralSource,
    /// Extracted comments that should become translator-facing guidance.
    pub comments: Vec<String>,
    /// Source locations collected by the extractor.
    pub origin: Vec<CatalogOrigin>,
    /// Placeholder hints keyed by placeholder name.
    pub placeholders: BTreeMap<String, Vec<String>>,
}

/// Structured extractor input accepted by [`super::update_catalog`] and [`super::update_catalog_file`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExtractedMessage {
    /// Message that has a single source/translation value.
    Singular(ExtractedSingularMessage),
    /// Message that carries structured plural source forms.
    Plural(ExtractedPluralMessage),
}

/// Source-first extractor input that lets `ferrocat` infer plural structure.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SourceExtractedMessage {
    /// Source message text used both as identifier and source value.
    pub msgid: String,
    /// Optional gettext message context.
    pub msgctxt: Option<String>,
    /// Extracted comments that should become translator-facing guidance.
    pub comments: Vec<String>,
    /// Source locations collected by the extractor.
    pub origin: Vec<CatalogOrigin>,
    /// Placeholder hints keyed by placeholder name.
    pub placeholders: BTreeMap<String, Vec<String>>,
}

/// Input payload accepted by catalog update operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CatalogUpdateInput {
    /// Pre-projected singular/plural messages.
    Structured(Vec<ExtractedMessage>),
    /// Source-first messages that let `ferrocat` infer plural structure.
    SourceFirst(Vec<SourceExtractedMessage>),
}

impl Default for CatalogUpdateInput {
    fn default() -> Self {
        Self::Structured(Vec::new())
    }
}

impl From<Vec<ExtractedMessage>> for CatalogUpdateInput {
    fn from(value: Vec<ExtractedMessage>) -> Self {
        Self::Structured(value)
    }
}

impl From<Vec<SourceExtractedMessage>> for CatalogUpdateInput {
    fn from(value: Vec<SourceExtractedMessage>) -> Self {
        Self::SourceFirst(value)
    }
}

/// Public translation shape returned from parsed catalogs.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
pub enum TranslationShape {
    /// Message represented by a single string value.
    Singular {
        /// The current translation value.
        value: String,
    },
    /// Message represented by structured plural categories.
    Plural {
        /// Source-side plural forms.
        source: PluralSource,
        /// Translation values keyed by plural category.
        translation: BTreeMap<String, String>,
        /// Variable name used when re-synthesizing ICU plural strings.
        variable: String,
    },
}

/// Borrowed view over a message translation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EffectiveTranslationRef<'a> {
    /// Singular translation borrowed from the parsed catalog.
    Singular(&'a str),
    /// Plural translation borrowed from the parsed catalog.
    Plural(&'a BTreeMap<String, String>),
}

/// Owned translation value materialized from a parsed catalog.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EffectiveTranslation {
    /// Singular translation value.
    Singular(String),
    /// Plural translation values keyed by category.
    Plural(BTreeMap<String, String>),
}

/// Public message representation returned by [`super::parse_catalog`].
///
/// Not `Eq`: AI confidence is a float ([`MachineMetadata`]).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct CatalogMessage {
    /// Source message identifier.
    pub msgid: String,
    /// Optional gettext message context.
    pub msgctxt: Option<String>,
    /// Public translation representation.
    pub translation: TranslationShape,
    /// Free-text notes for translators (developer- or translator-provided).
    pub comments: Vec<String>,
    /// Source origins preserved from PO references.
    pub origin: PoVec<CatalogOrigin>,
    /// Obsolete state: `None` when active, `Some` when the message is obsolete
    /// (with an optional write-once `since` date, see [`ObsoleteInfo`]).
    pub obsolete: Option<ObsoleteInfo>,
    /// Optional metadata when the current value is machine-managed (see
    /// [`MachineMetadata`]).
    pub machine: Option<MachineMetadata>,
}

/// Obsolete-entry payload. Presence on [`CatalogMessage::obsolete`] marks the
/// entry obsolete; `since` records when it became obsolete so hosts can
/// age-cleanup with [`ObsoleteStrategy::DropObsoleteBefore`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct ObsoleteInfo {
    /// Write-once ISO-8601 date the entry became obsolete, set from the host's
    /// injected clock. `None` when the obsolescence date is unknown.
    pub since: Option<String>,
}

impl CatalogMessage {
    /// Returns the lookup key for this message.
    #[must_use]
    pub fn key(&self) -> CatalogMessageKey {
        CatalogMessageKey {
            msgid: self.msgid.clone(),
            msgctxt: self.msgctxt.clone(),
        }
    }

    /// Returns the effective translation without source-locale fallback.
    #[must_use]
    pub fn effective_translation(&self) -> EffectiveTranslationRef<'_> {
        match &self.translation {
            TranslationShape::Singular { value } => EffectiveTranslationRef::Singular(value),
            TranslationShape::Plural { translation, .. } => {
                EffectiveTranslationRef::Plural(translation)
            }
        }
    }

    pub(super) fn effective_translation_owned(&self) -> EffectiveTranslation {
        match &self.translation {
            TranslationShape::Singular { value } => EffectiveTranslation::Singular(value.clone()),
            TranslationShape::Plural { translation, .. } => {
                EffectiveTranslation::Plural(translation.clone())
            }
        }
    }

    /// Applies the source-locale fallback semantics used by compilation and
    /// runtime artifact generation.
    ///
    /// Singular messages fall back to `msgid` when empty. Plural messages keep
    /// their category shape and only fill categories that are missing or empty.
    pub(super) fn source_fallback_translation(&self, locale: Option<&str>) -> EffectiveTranslation {
        match &self.translation {
            TranslationShape::Singular { value } => {
                if value.is_empty() {
                    EffectiveTranslation::Singular(self.msgid.clone())
                } else {
                    EffectiveTranslation::Singular(value.clone())
                }
            }
            TranslationShape::Plural {
                source,
                translation,
                ..
            } => {
                let profile = PluralProfile::for_locale(locale);
                let mut effective = profile.materialize_translation(translation);
                for category in profile.categories() {
                    let should_fill = effective.get(category).is_none_or(String::is_empty);
                    if should_fill {
                        effective.insert(
                            category.clone(),
                            profile.source_locale_value(category, source),
                        );
                    }
                }
                EffectiveTranslation::Plural(effective)
            }
        }
    }
}

/// Stable lookup key for catalog messages.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct CatalogMessageKey {
    /// Source message identifier.
    pub msgid: String,
    /// Optional gettext message context.
    pub msgctxt: Option<String>,
}

impl CatalogMessageKey {
    /// Creates a message key from `msgid` and optional context.
    #[must_use]
    pub fn new(msgid: impl Into<String>, msgctxt: Option<String>) -> Self {
        Self {
            msgid: msgid.into(),
            msgctxt,
        }
    }

    /// Creates a message key from `msgid` and context.
    ///
    /// This is equivalent to `CatalogMessageKey::new(msgid, Some(msgctxt.into()))`
    /// but lets callers pass borrowed context strings without spelling the
    /// intermediate `Some(String)`.
    #[must_use]
    pub fn with_context(msgid: impl Into<String>, msgctxt: impl Into<String>) -> Self {
        Self {
            msgid: msgid.into(),
            msgctxt: Some(msgctxt.into()),
        }
    }
}

/// Severity level attached to a [`Diagnostic`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum DiagnosticSeverity {
    /// Informational message that does not indicate a problem.
    Info,
    /// Non-fatal condition that may require user attention.
    Warning,
    /// Serious condition associated with invalid input or unsupported output.
    Error,
}

/// Non-fatal issue collected while parsing or updating catalogs.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct Diagnostic {
    /// Severity level for the diagnostic.
    pub severity: DiagnosticSeverity,
    /// Stable machine-readable code for the diagnostic.
    pub code: DiagnosticCode,
    /// Human-readable explanation of the condition.
    pub message: String,
    /// Source `msgid`, when the diagnostic can be tied to one message.
    pub msgid: Option<String>,
    /// Source `msgctxt`, when the diagnostic can be tied to one message.
    pub msgctxt: Option<String>,
}

impl Diagnostic {
    pub(super) fn new(
        severity: DiagnosticSeverity,
        code: impl Into<DiagnosticCode>,
        message: impl Into<String>,
    ) -> Self {
        Self {
            severity,
            code: code.into(),
            message: message.into(),
            msgid: None,
            msgctxt: None,
        }
    }

    pub(super) fn with_identity(mut self, msgid: &str, msgctxt: Option<&str>) -> Self {
        self.msgid = Some(msgid.to_owned());
        self.msgctxt = msgctxt.map(str::to_owned);
        self
    }
}

/// Basic counters describing an update operation.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CatalogStats {
    /// Total messages in the final catalog.
    pub total: usize,
    /// Messages added during the update.
    pub added: usize,
    /// Existing messages whose rendered representation changed.
    pub changed: usize,
    /// Existing messages preserved without changes.
    pub unchanged: usize,
    /// Messages newly marked obsolete.
    pub obsolete_marked: usize,
    /// Messages removed because the obsolete strategy deleted them.
    pub obsolete_removed: usize,
}

/// Result returned by catalog update operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CatalogUpdateResult {
    /// Final PO content after applying the update.
    pub content: String,
    /// Whether the update created a new catalog from scratch.
    pub created: bool,
    /// Whether the final content differs from the original input.
    pub updated: bool,
    /// Summary counters for the operation.
    pub stats: CatalogStats,
    /// Non-fatal diagnostics collected during processing.
    pub diagnostics: Vec<Diagnostic>,
}

/// One catalog input passed to [`super::combine_catalogs`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CatalogCombineInput<'a> {
    /// Catalog content to parse and include in the combine operation.
    pub content: &'a str,
    /// Optional human-readable label used in diagnostics.
    pub label: Option<&'a str>,
}

impl<'a> CatalogCombineInput<'a> {
    /// Creates a combine input without a diagnostic label.
    #[must_use]
    pub const fn new(content: &'a str) -> Self {
        Self {
            content,
            label: None,
        }
    }

    /// Creates a combine input with a diagnostic label.
    #[must_use]
    pub const fn labeled(content: &'a str, label: &'a str) -> Self {
        Self {
            content,
            label: Some(label),
        }
    }
}

/// Strategy used when multiple catalogs define conflicting translations for one identity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CatalogConflictStrategy {
    /// Keep the first non-empty translation encountered for each `msgid`/`msgctxt`.
    #[default]
    UseFirst,
    /// Replace the current non-empty translation with the latest non-empty definition.
    UseLast,
    /// Return an error when two non-empty translations differ.
    Error,
}

/// Selection rule used after definitions from all inputs have been counted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CatalogCombineSelection {
    /// Keep every message identity.
    #[default]
    All,
    /// Keep identities with more than the provided number of definitions.
    MoreThan(usize),
    /// Keep identities with less than the provided number of definitions.
    LessThan(usize),
    /// Keep identities defined only once.
    Unique,
}

impl CatalogCombineSelection {
    pub(super) const fn includes(self, definitions: usize) -> bool {
        match self {
            Self::All => true,
            Self::MoreThan(limit) => definitions > limit,
            Self::LessThan(limit) => definitions < limit,
            Self::Unique => definitions < 2,
        }
    }
}

/// Basic counters describing a catalog combine operation.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CatalogCombineStats {
    /// Number of input catalogs parsed.
    pub inputs: usize,
    /// Total message definitions considered after obsolete filtering.
    pub definitions: usize,
    /// Message identities written to the final catalog.
    pub selected: usize,
    /// Message identities removed by the selection rule.
    pub skipped: usize,
    /// Translation conflicts resolved according to the selected strategy.
    pub conflicts_resolved: usize,
    /// Total messages in the final catalog.
    pub total: usize,
}

/// Result returned by catalog combine operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CatalogCombineResult {
    /// Final catalog content after combining the inputs.
    pub content: String,
    /// Summary counters for the operation.
    pub stats: CatalogCombineStats,
    /// Non-fatal diagnostics collected during processing.
    pub diagnostics: Vec<Diagnostic>,
}

/// File format used by disk-based catalog combine operations.
///
/// This enum is non-exhaustive because Ferrocat can add additional catalog
/// file formats over time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CatalogFileFormat {
    /// Classic gettext PO catalog files, including gettext template (`.pot`) files.
    #[default]
    Po,
    /// Ferrocat Catalog Lines (`.fcl`) files.
    Fcl,
}

impl CatalogFileFormat {
    /// Infers a catalog file format from a path extension.
    ///
    /// Supported path suffixes are `.po`, `.pot`, and `.fcl`.
    ///
    /// # Errors
    ///
    /// Returns [`ApiError::Unsupported`] when the path suffix does not map to a
    /// supported catalog file format.
    pub fn infer_from_path(path: &Path) -> Result<Self, ApiError> {
        let name = path
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or_default()
            .to_ascii_lowercase();

        if name.ends_with(".po") || name.ends_with(".pot") {
            return Ok(Self::Po);
        }
        if name.ends_with(".fcl") {
            return Ok(Self::Fcl);
        }

        Err(ApiError::Unsupported(format!(
            "could not infer catalog file format from `{}`; expected .po, .pot, or .fcl",
            path.display()
        )))
    }

    pub(super) const fn default_mode(self) -> CatalogMode {
        match self {
            Self::Po => CatalogMode::IcuPo,
            Self::Fcl => CatalogMode::IcuFcl,
        }
    }
}

/// Options for combining catalog files on disk.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CombineCatalogFilesOptions<'a> {
    /// Input catalog paths in precedence order.
    pub input_paths: &'a [PathBuf],
    /// Output catalog path to atomically replace after a successful combine.
    pub output_path: &'a Path,
    /// Optional explicit file format. When `None`, Ferrocat infers it from the
    /// input and output paths and requires all inferred formats to match.
    pub format: Option<CatalogFileFormat>,
    /// Optional high-level catalog mode. When `None`, Ferrocat chooses
    /// `CatalogMode::IcuPo` for PO files and `CatalogMode::IcuFcl` for FCL files.
    pub mode: Option<CatalogMode>,
    /// Locale of the combined catalog. When `None`, Ferrocat uses the first input locale if present.
    pub locale: Option<&'a str>,
    /// Source locale used for source-side semantics and validation.
    pub source_locale: &'a str,
    /// Strategy for resolving conflicting non-empty translations.
    /// Empty template translations never clear non-empty values.
    pub conflict_strategy: CatalogConflictStrategy,
    /// Message identity selection rule applied after all inputs are read.
    pub selection: CatalogCombineSelection,
    /// Sort order for the final rendered catalog.
    pub order_by: OrderBy,
    /// Whether source origins should be rendered as references.
    pub include_origins: bool,
    /// Whether obsolete definitions should participate in the combine operation.
    pub include_obsolete: bool,
}

impl<'a> CombineCatalogFilesOptions<'a> {
    /// Creates file combine options with required fields set.
    ///
    /// Optional fields use the same defaults as [`CombineCatalogOptions`].
    #[must_use]
    pub fn new(input_paths: &'a [PathBuf], output_path: &'a Path, source_locale: &'a str) -> Self {
        Self {
            input_paths,
            output_path,
            format: None,
            mode: None,
            locale: None,
            source_locale,
            conflict_strategy: CatalogConflictStrategy::UseFirst,
            selection: CatalogCombineSelection::All,
            order_by: OrderBy::Msgid,
            include_origins: true,
            include_obsolete: false,
        }
    }

    /// Returns options that use the given input paths.
    #[must_use]
    pub fn with_input_paths(mut self, input_paths: &'a [PathBuf]) -> Self {
        self.input_paths = input_paths;
        self
    }

    /// Returns options that atomically replace the given output path.
    #[must_use]
    pub fn with_output_path(mut self, output_path: &'a Path) -> Self {
        self.output_path = output_path;
        self
    }

    /// Returns options that use the given explicit file format.
    #[must_use]
    pub fn with_format(mut self, format: CatalogFileFormat) -> Self {
        self.format = Some(format);
        self
    }

    /// Returns options that read and render with the given catalog mode.
    #[must_use]
    pub fn with_mode(mut self, mode: CatalogMode) -> Self {
        self.mode = Some(mode);
        self
    }

    /// Returns options that use the given combined catalog locale.
    #[must_use]
    pub fn with_locale(mut self, locale: &'a str) -> Self {
        self.locale = Some(locale);
        self
    }

    /// Returns options that resolve conflicts with the given strategy.
    #[must_use]
    pub fn with_conflict_strategy(mut self, conflict_strategy: CatalogConflictStrategy) -> Self {
        self.conflict_strategy = conflict_strategy;
        self
    }

    /// Returns options that apply the given message selection.
    #[must_use]
    pub fn with_selection(mut self, selection: CatalogCombineSelection) -> Self {
        self.selection = selection;
        self
    }

    /// Returns options that render the combined catalog with the given sort order.
    #[must_use]
    pub fn with_order_by(mut self, order_by: OrderBy) -> Self {
        self.order_by = order_by;
        self
    }

    /// Returns options that enable or disable rendered source origins.
    #[must_use]
    pub fn with_include_origins(mut self, include_origins: bool) -> Self {
        self.include_origins = include_origins;
        self
    }

    /// Returns options that include or skip obsolete definitions.
    #[must_use]
    pub fn with_include_obsolete(mut self, include_obsolete: bool) -> Self {
        self.include_obsolete = include_obsolete;
        self
    }
}

/// Result returned by catalog file combine operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CatalogFileCombineResult {
    /// Output path replaced by the operation.
    pub output_path: PathBuf,
    /// File format used for reading inputs and writing the output.
    pub format: CatalogFileFormat,
    /// Summary counters for the operation.
    pub stats: CatalogCombineStats,
    /// Non-fatal diagnostics collected during processing.
    pub diagnostics: Vec<Diagnostic>,
}

/// Parsed catalog plus diagnostics and normalized headers.
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedCatalog {
    /// Declared or overridden catalog locale.
    pub locale: Option<String>,
    /// High-level semantics used to parse the catalog.
    pub semantics: CatalogSemantics,
    /// Normalized header map keyed by header name.
    pub headers: BTreeMap<String, String>,
    /// Parsed catalog messages in source order.
    pub messages: Vec<CatalogMessage>,
    /// Non-fatal diagnostics collected while parsing.
    pub diagnostics: Vec<Diagnostic>,
}

impl ParsedCatalog {
    /// Builds a lookup-oriented view that rejects duplicate message keys.
    ///
    /// # Errors
    ///
    /// Returns [`ApiError::Conflict`] when the parsed catalog contains
    /// duplicate `msgid`/`msgctxt` pairs.
    pub fn into_normalized_view(self) -> Result<NormalizedParsedCatalog, ApiError> {
        NormalizedParsedCatalog::new(self)
    }
}

/// Parsed catalog with fast key-based lookup helpers.
#[derive(Debug, Clone, PartialEq)]
pub struct NormalizedParsedCatalog {
    pub(super) catalog: ParsedCatalog,
    pub(super) key_index: BTreeMap<CatalogMessageKey, usize>,
    msgid_hash_index: FxHashMap<u64, Vec<usize>>,
}

impl NormalizedParsedCatalog {
    /// Builds the lookup index once and rejects duplicate gettext identities up front.
    pub(super) fn new(catalog: ParsedCatalog) -> Result<Self, ApiError> {
        let mut key_index = BTreeMap::new();
        let mut msgid_hash_index = FxHashMap::<u64, Vec<usize>>::with_capacity_and_hasher(
            catalog.messages.len(),
            Default::default(),
        );
        for (index, message) in catalog.messages.iter().enumerate() {
            let key = message.key();
            match key_index.entry(key) {
                btree_map::Entry::Vacant(entry) => {
                    entry.insert(index);
                }
                btree_map::Entry::Occupied(entry) => {
                    let key = entry.key();
                    return Err(ApiError::Conflict(format!(
                        "duplicate parsed catalog message for msgid {:?} and context {:?}",
                        key.msgid, key.msgctxt
                    )));
                }
            }
            msgid_hash_index
                .entry(message_id_hash(&message.msgid))
                .or_default()
                .push(index);
        }
        Ok(Self {
            catalog,
            key_index,
            msgid_hash_index,
        })
    }

    /// Returns the underlying parsed catalog.
    #[must_use]
    pub const fn parsed_catalog(&self) -> &ParsedCatalog {
        &self.catalog
    }

    /// Consumes the normalized view and returns the underlying parsed catalog.
    #[must_use]
    pub fn into_parsed_catalog(self) -> ParsedCatalog {
        self.catalog
    }

    /// Returns a message by key.
    #[must_use]
    pub fn get(&self, key: &CatalogMessageKey) -> Option<&CatalogMessage> {
        self.key_index
            .get(key)
            .map(|index| &self.catalog.messages[*index])
    }

    /// Returns a message by borrowed `msgid` and optional context parts.
    ///
    /// This avoids constructing an owned [`CatalogMessageKey`] when callers
    /// already have borrowed source identity fields.
    #[must_use]
    pub fn get_by_parts(&self, msgid: &str, msgctxt: Option<&str>) -> Option<&CatalogMessage> {
        self.msgid_hash_index
            .get(&message_id_hash(msgid))?
            .iter()
            .find_map(|index| {
                let message = &self.catalog.messages[*index];
                (message.msgid.as_str() == msgid && message.msgctxt.as_deref() == msgctxt)
                    .then_some(message)
            })
    }

    /// Returns `true` if a message for `key` exists.
    #[must_use]
    pub fn contains_key(&self, key: &CatalogMessageKey) -> bool {
        self.key_index.contains_key(key)
    }

    /// Returns `true` if a message exists for borrowed `msgid` and context parts.
    #[must_use]
    pub fn contains_parts(&self, msgid: &str, msgctxt: Option<&str>) -> bool {
        self.get_by_parts(msgid, msgctxt).is_some()
    }

    /// Returns the number of indexed messages.
    #[must_use]
    pub fn message_count(&self) -> usize {
        self.catalog.messages.len()
    }

    /// Iterates over all indexed messages in key order.
    pub fn iter(&self) -> impl Iterator<Item = (&CatalogMessageKey, &CatalogMessage)> + '_ {
        self.key_index
            .iter()
            .map(|(key, index)| (key, &self.catalog.messages[*index]))
    }

    /// Returns the effective translation for `key`, if present.
    pub fn effective_translation(
        &self,
        key: &CatalogMessageKey,
    ) -> Option<EffectiveTranslationRef<'_>> {
        self.get(key).map(CatalogMessage::effective_translation)
    }

    /// Returns the effective translation for borrowed `msgid` and context parts.
    pub fn effective_translation_by_parts(
        &self,
        msgid: &str,
        msgctxt: Option<&str>,
    ) -> Option<EffectiveTranslationRef<'_>> {
        self.get_by_parts(msgid, msgctxt)
            .map(CatalogMessage::effective_translation)
    }

    /// Returns the effective translation and fills empty source-locale values
    /// from the source text when appropriate.
    #[must_use]
    pub fn effective_translation_with_source_fallback(
        &self,
        key: &CatalogMessageKey,
        source_locale: &str,
    ) -> Option<EffectiveTranslation> {
        let message = self.get(key)?;
        Some(self.effective_translation_for_message(message, source_locale))
    }

    /// Returns the effective translation for borrowed identity parts and fills
    /// empty source-locale values from the source text when appropriate.
    #[must_use]
    pub fn effective_translation_with_source_fallback_by_parts(
        &self,
        msgid: &str,
        msgctxt: Option<&str>,
        source_locale: &str,
    ) -> Option<EffectiveTranslation> {
        let message = self.get_by_parts(msgid, msgctxt)?;
        Some(self.effective_translation_for_message(message, source_locale))
    }

    pub(super) fn effective_translation_for_message(
        &self,
        message: &CatalogMessage,
        source_locale: &str,
    ) -> EffectiveTranslation {
        if self
            .catalog
            .locale
            .as_deref()
            .is_none_or(|locale| locale == source_locale)
        {
            message.source_fallback_translation(self.catalog.locale.as_deref())
        } else {
            message.effective_translation_owned()
        }
    }
}

fn message_id_hash(msgid: &str) -> u64 {
    let mut hasher = FxHasher::default();
    msgid.hash(&mut hasher);
    hasher.finish()
}

/// Encoding used for plural messages in PO files.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PluralEncoding {
    /// Keep plural messages in Ferrocat's structured ICU-oriented representation.
    #[default]
    Icu,
    /// Materialize plural messages as classic gettext `msgid_plural` plus `msgstr[n]`.
    Gettext,
}

/// Storage format used by the high-level catalog API.
///
/// This enum is non-exhaustive because Ferrocat can add additional catalog
/// storage formats over time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CatalogStorageFormat {
    /// Read and write classic gettext PO catalogs.
    #[default]
    Po,
    /// Read and write Ferrocat Catalog Lines (`.fcl`): a line-oriented,
    /// git-merge-optimized, machine-owned catalog format.
    Fcl,
}

/// High-level semantics used by the catalog API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CatalogSemantics {
    /// ICU-native semantics with raw ICU/text messages as the primary representation.
    #[default]
    IcuNative,
    /// Classic gettext plural semantics used for PO compatibility workflows.
    GettextCompat,
}

/// ICU parser behavior used by catalog audit and runtime artifact validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum IcuSyntaxPolicy {
    /// Parse ICU MessageFormat v1 with Ferrocat's strict apostrophe rules.
    #[default]
    Strict,
    /// Treat ordinary literal apostrophes as runtime-valid text before parsing.
    ///
    /// Use this when a downstream runtime accepts messages such as `you're`
    /// and `We've got {count, plural, one {...} other {...}}` without requiring
    /// translators to double every literal apostrophe.
    /// Callers that rely on ICU apostrophe quoting should keep [`Self::Strict`].
    RuntimeLiteralApostrophes,
}

/// Valid high-level catalog mode combinations.
///
/// This type groups the storage format, semantic model, and plural encoding
/// choices that must be kept in sync for catalog parse, update, and combine
/// operations. It is non-exhaustive so future supported catalog modes can be
/// added without breaking downstream matches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CatalogMode {
    /// Gettext PO storage with ICU-native message semantics.
    #[default]
    IcuPo,
    /// Gettext PO storage with classic gettext plural semantics.
    GettextPo,
    /// FCL (Ferrocat Catalog Lines) storage with ICU-native message semantics.
    IcuFcl,
}

impl CatalogMode {
    /// Returns the storage format implied by this mode.
    #[must_use]
    pub const fn storage_format(self) -> CatalogStorageFormat {
        match self {
            Self::IcuPo | Self::GettextPo => CatalogStorageFormat::Po,
            Self::IcuFcl => CatalogStorageFormat::Fcl,
        }
    }

    /// Returns the catalog semantics implied by this mode.
    #[must_use]
    pub const fn semantics(self) -> CatalogSemantics {
        match self {
            Self::IcuPo | Self::IcuFcl => CatalogSemantics::IcuNative,
            Self::GettextPo => CatalogSemantics::GettextCompat,
        }
    }

    /// Returns the plural encoding implied by this mode.
    #[must_use]
    pub const fn plural_encoding(self) -> PluralEncoding {
        match self {
            Self::IcuPo | Self::IcuFcl => PluralEncoding::Icu,
            Self::GettextPo => PluralEncoding::Gettext,
        }
    }

    /// Returns the matching catalog mode for explicit parts.
    #[must_use]
    pub const fn from_parts(
        storage_format: CatalogStorageFormat,
        semantics: CatalogSemantics,
        plural_encoding: PluralEncoding,
    ) -> Option<Self> {
        match (storage_format, semantics, plural_encoding) {
            (CatalogStorageFormat::Po, CatalogSemantics::IcuNative, PluralEncoding::Icu) => {
                Some(Self::IcuPo)
            }
            (CatalogStorageFormat::Fcl, CatalogSemantics::IcuNative, PluralEncoding::Icu) => {
                Some(Self::IcuFcl)
            }
            (
                CatalogStorageFormat::Po,
                CatalogSemantics::GettextCompat,
                PluralEncoding::Gettext,
            ) => Some(Self::GettextPo),
            _ => None,
        }
    }
}

/// Strategy used for messages that disappear from the extracted input.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ObsoleteStrategy {
    /// Mark missing messages obsolete and keep them in the file.
    #[default]
    Mark,
    /// Remove missing messages entirely.
    Delete,
    /// Keep missing messages as active entries.
    Keep,
    /// Mark missing messages obsolete (like [`ObsoleteStrategy::Mark`]) and
    /// additionally drop any obsolete entry whose `since` date predates the given
    /// ISO-8601 cutoff. Undated obsolete entries are kept. The host computes the
    /// cutoff (e.g. today minus 90 days); ISO dates compare lexicographically, so
    /// no date arithmetic happens inside Ferrocat. See
    /// [ADR 0025](https://ferrocat.dev/architecture/adr/0025-obsolete-age-and-cleanup).
    DropObsoleteBefore(String),
}

/// Sort order used when writing output catalogs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OrderBy {
    /// Sort by `msgid` then context.
    #[default]
    Msgid,
    /// Sort by the first source origin, then by message identity.
    Origin,
}

/// Controls whether placeholder hints are emitted as extracted comments.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlaceholderCommentMode {
    /// Do not emit placeholder comments.
    Disabled,
    /// Emit up to `limit` placeholder comments per placeholder name.
    Enabled {
        /// Maximum number of values rendered per placeholder name.
        limit: usize,
    },
}

impl Default for PlaceholderCommentMode {
    fn default() -> Self {
        Self::Enabled { limit: 3 }
    }
}

/// Shared rendering options for catalog serialization.
///
/// These fields control how a catalog is sorted and which optional reference and
/// placeholder details are written. Some storage formats still impose their own
/// invariants: FCL always renders in canonical `(id, ctxt)` order, while origin
/// and placeholder detail flags apply across supported catalog storage formats.
///
/// References render the source file only; Ferrocat does not track or emit line
/// numbers (see [`CatalogOrigin`]).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RenderOptions<'a> {
    /// Sort order for the final rendered catalog.
    pub order_by: OrderBy,
    /// Whether source origins should be rendered as references.
    pub include_origins: bool,
    /// Controls emission of placeholder comments.
    pub print_placeholders_in_comments: PlaceholderCommentMode,
    /// Optional additional header attributes to inject or override.
    pub custom_header_attributes: Option<&'a BTreeMap<String, String>>,
}

impl Default for RenderOptions<'_> {
    fn default() -> Self {
        Self {
            order_by: OrderBy::Msgid,
            include_origins: true,
            print_placeholders_in_comments: PlaceholderCommentMode::Enabled { limit: 3 },
            custom_header_attributes: None,
        }
    }
}

impl<'a> RenderOptions<'a> {
    /// Returns options that render messages with the given sort order.
    #[must_use]
    pub fn with_order_by(mut self, order_by: OrderBy) -> Self {
        self.order_by = order_by;
        self
    }

    /// Returns options that enable or disable rendered source origins.
    #[must_use]
    pub fn with_include_origins(mut self, include_origins: bool) -> Self {
        self.include_origins = include_origins;
        self
    }

    /// Returns options that use the given placeholder comment mode.
    #[must_use]
    pub fn with_placeholder_comments(
        mut self,
        print_placeholders_in_comments: PlaceholderCommentMode,
    ) -> Self {
        self.print_placeholders_in_comments = print_placeholders_in_comments;
        self
    }

    /// Returns options that inject or override the given header attributes.
    #[must_use]
    pub fn with_custom_header_attributes(
        mut self,
        custom_header_attributes: &'a BTreeMap<String, String>,
    ) -> Self {
        self.custom_header_attributes = Some(custom_header_attributes);
        self
    }
}

/// Options for in-memory catalog updates.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UpdateCatalogOptions<'a> {
    /// Locale of the catalog being updated. When `None`, Ferrocat infers it from the existing file.
    pub locale: Option<&'a str>,
    /// Source locale used for source-side semantics and fallback handling.
    pub source_locale: &'a str,
    /// Extracted messages to merge into the catalog.
    pub input: CatalogUpdateInput,
    /// Existing catalog content, when updating an in-memory catalog.
    pub existing: Option<&'a str>,
    /// High-level catalog mode used when parsing, merging, and rendering the catalog.
    pub mode: CatalogMode,
    /// Strategy for messages absent from the extracted input.
    pub obsolete_strategy: ObsoleteStrategy,
    /// Whether source-locale translations should be refreshed from the extracted source strings.
    pub overwrite_source_translations: bool,
    /// Optional host-provided clock as an ISO-8601 date. When set, entries newly
    /// transitioning to obsolete are stamped with this `since` date (write-once).
    /// Ferrocat never reads a clock itself, so updates stay deterministic given
    /// their inputs. See [ADR 0025](https://ferrocat.dev/architecture/adr/0025-obsolete-age-and-cleanup).
    pub now: Option<&'a str>,
    /// Shared serialization options for the rendered catalog.
    pub render: RenderOptions<'a>,
}

impl<'a> UpdateCatalogOptions<'a> {
    /// Creates in-memory update options with required fields set.
    ///
    /// Optional fields default to an inferred locale, ICU-native PO mode,
    /// marking missing messages obsolete, preserving source-locale translations,
    /// no host clock, and [`RenderOptions::default`].
    #[must_use]
    pub fn new(source_locale: &'a str, input: impl Into<CatalogUpdateInput>) -> Self {
        Self {
            locale: None,
            source_locale,
            input: input.into(),
            existing: None,
            mode: CatalogMode::default(),
            obsolete_strategy: ObsoleteStrategy::Mark,
            overwrite_source_translations: false,
            now: None,
            render: RenderOptions::default(),
        }
    }

    /// Returns options that use the given catalog locale.
    #[must_use]
    pub fn with_locale(mut self, locale: &'a str) -> Self {
        self.locale = Some(locale);
        self
    }

    /// Returns options that update the given existing catalog content.
    #[must_use]
    pub fn with_existing(mut self, existing: &'a str) -> Self {
        self.existing = Some(existing);
        self
    }

    /// Returns options that parse, merge, and render with the given catalog mode.
    #[must_use]
    pub fn with_mode(mut self, mode: CatalogMode) -> Self {
        self.mode = mode;
        self
    }

    /// Returns options that render the updated catalog with the given options.
    #[must_use]
    pub fn with_render(mut self, render: RenderOptions<'a>) -> Self {
        self.render = render;
        self
    }

    /// Returns options that handle missing extracted messages with the given strategy.
    #[must_use]
    pub fn with_obsolete_strategy(mut self, obsolete_strategy: ObsoleteStrategy) -> Self {
        self.obsolete_strategy = obsolete_strategy;
        self
    }

    /// Returns options that enable or disable source-locale translation refreshes.
    #[must_use]
    pub fn with_overwrite_source_translations(
        mut self,
        overwrite_source_translations: bool,
    ) -> Self {
        self.overwrite_source_translations = overwrite_source_translations;
        self
    }

    /// Returns options that stamp newly obsolete entries with the given ISO date.
    #[must_use]
    pub fn with_now(mut self, now: &'a str) -> Self {
        self.now = Some(now);
        self
    }
}

/// Options for updating a catalog file on disk.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UpdateCatalogFileOptions<'a> {
    /// Path to the catalog file that should be read and conditionally written.
    pub target_path: &'a Path,
    /// In-memory update options applied to the file content.
    pub options: UpdateCatalogOptions<'a>,
}

impl<'a> UpdateCatalogFileOptions<'a> {
    /// Creates file update options with required fields set.
    ///
    /// Optional fields on the nested update options use
    /// [`UpdateCatalogOptions::new`] defaults.
    #[must_use]
    pub fn new(
        target_path: &'a Path,
        source_locale: &'a str,
        input: impl Into<CatalogUpdateInput>,
    ) -> Self {
        Self {
            target_path,
            options: UpdateCatalogOptions::new(source_locale, input),
        }
    }

    /// Returns file update options that use the given in-memory update options.
    #[must_use]
    pub fn with_options(mut self, options: UpdateCatalogOptions<'a>) -> Self {
        self.options = options;
        self
    }
}

/// Options for combining multiple catalogs into one deterministic catalog.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CombineCatalogOptions<'a> {
    /// Input catalogs in precedence order.
    pub inputs: &'a [CatalogCombineInput<'a>],
    /// Locale of the combined catalog. When `None`, Ferrocat uses the first input locale if present.
    pub locale: Option<&'a str>,
    /// Source locale used for source-side semantics and validation.
    pub source_locale: &'a str,
    /// High-level catalog mode used when reading inputs and rendering the result.
    pub mode: CatalogMode,
    /// Strategy for resolving conflicting non-empty translations.
    /// Empty template translations never clear non-empty values.
    pub conflict_strategy: CatalogConflictStrategy,
    /// Message identity selection rule applied after all inputs are read.
    pub selection: CatalogCombineSelection,
    /// Sort order for the final rendered catalog.
    pub order_by: OrderBy,
    /// Whether source origins should be rendered as references.
    pub include_origins: bool,
    /// Whether obsolete definitions should participate in the combine operation.
    pub include_obsolete: bool,
}

impl<'a> CombineCatalogOptions<'a> {
    /// Creates combine options with required fields set.
    ///
    /// Optional fields default to an inferred locale, ICU-native PO mode,
    /// first-definition conflict resolution, all message identities, `msgid`
    /// ordering, rendered origins, and skipped obsolete entries.
    #[must_use]
    pub fn new(inputs: &'a [CatalogCombineInput<'a>], source_locale: &'a str) -> Self {
        Self {
            inputs,
            locale: None,
            source_locale,
            mode: CatalogMode::default(),
            conflict_strategy: CatalogConflictStrategy::UseFirst,
            selection: CatalogCombineSelection::All,
            order_by: OrderBy::Msgid,
            include_origins: true,
            include_obsolete: false,
        }
    }

    /// Returns options that use the given input catalogs.
    #[must_use]
    pub fn with_inputs(mut self, inputs: &'a [CatalogCombineInput<'a>]) -> Self {
        self.inputs = inputs;
        self
    }

    /// Returns options that use the given combined catalog locale.
    #[must_use]
    pub fn with_locale(mut self, locale: &'a str) -> Self {
        self.locale = Some(locale);
        self
    }

    /// Returns options that read and render with the given catalog mode.
    #[must_use]
    pub fn with_mode(mut self, mode: CatalogMode) -> Self {
        self.mode = mode;
        self
    }

    /// Returns options that resolve conflicts with the given strategy.
    #[must_use]
    pub fn with_conflict_strategy(mut self, conflict_strategy: CatalogConflictStrategy) -> Self {
        self.conflict_strategy = conflict_strategy;
        self
    }

    /// Returns options that apply the given message selection.
    #[must_use]
    pub fn with_selection(mut self, selection: CatalogCombineSelection) -> Self {
        self.selection = selection;
        self
    }

    /// Returns options that render the combined catalog with the given sort order.
    #[must_use]
    pub fn with_order_by(mut self, order_by: OrderBy) -> Self {
        self.order_by = order_by;
        self
    }

    /// Returns options that enable or disable rendered source origins.
    #[must_use]
    pub fn with_include_origins(mut self, include_origins: bool) -> Self {
        self.include_origins = include_origins;
        self
    }

    /// Returns options that include or skip obsolete definitions.
    #[must_use]
    pub fn with_include_obsolete(mut self, include_obsolete: bool) -> Self {
        self.include_obsolete = include_obsolete;
        self
    }
}

/// Options for parsing a catalog into the higher-level message model.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseCatalogOptions<'a> {
    /// Catalog content to parse.
    pub content: &'a str,
    /// Optional explicit locale override.
    pub locale: Option<&'a str>,
    /// Source locale used for source-side semantics and validation.
    pub source_locale: &'a str,
    /// High-level catalog mode used when interpreting catalog content.
    pub mode: CatalogMode,
    /// Whether unsupported ICU plural projection cases should become hard errors.
    pub strict: bool,
}

impl<'a> ParseCatalogOptions<'a> {
    /// Creates parse options with required fields set.
    ///
    /// Optional fields default to an inferred locale, ICU-native PO mode, and
    /// non-strict plural projection.
    #[must_use]
    pub fn new(content: &'a str, source_locale: &'a str) -> Self {
        Self {
            content,
            locale: None,
            source_locale,
            mode: CatalogMode::default(),
            strict: false,
        }
    }

    /// Returns options that use the given explicit catalog locale.
    #[must_use]
    pub fn with_locale(mut self, locale: &'a str) -> Self {
        self.locale = Some(locale);
        self
    }

    /// Returns options that interpret catalog content with the given mode.
    #[must_use]
    pub fn with_mode(mut self, mode: CatalogMode) -> Self {
        self.mode = mode;
        self
    }

    /// Returns options that enable or disable strict plural projection.
    #[must_use]
    pub fn with_strict(mut self, strict: bool) -> Self {
        self.strict = strict;
        self
    }
}

/// Error returned by catalog parsing and update APIs.
///
/// This enum is non-exhaustive so new API-level failure categories can be added
/// without turning downstream error matches into a breaking change.
#[derive(Debug)]
#[non_exhaustive]
pub enum ApiError {
    /// Underlying PO parse or string-unescape failure.
    Parse(ParseError),
    /// Filesystem failure raised by disk-based helpers.
    Io(std::io::Error),
    /// Caller-supplied arguments were missing, inconsistent, or invalid.
    InvalidArguments(String),
    /// The requested operation encountered conflicting catalog state.
    Conflict(String),
    /// The requested behavior cannot be represented safely.
    Unsupported(String),
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(error) => error.fmt(f),
            Self::Io(error) => error.fmt(f),
            Self::InvalidArguments(message)
            | Self::Conflict(message)
            | Self::Unsupported(message) => f.write_str(message),
        }
    }
}

impl std::error::Error for ApiError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Parse(error) => Some(error),
            Self::Io(error) => Some(error),
            Self::InvalidArguments(_) | Self::Conflict(_) | Self::Unsupported(_) => None,
        }
    }
}

impl ApiError {
    /// Creates a filesystem error with path context.
    #[must_use]
    pub fn io_with_path(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
        let kind = source.kind();
        Self::Io(std::io::Error::new(
            kind,
            PathIoError {
                path: path.into(),
                source,
            },
        ))
    }

    /// Returns the filesystem path associated with this error, when available.
    #[must_use]
    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::Io(error) => error
                .get_ref()
                .and_then(|source| source.downcast_ref::<PathIoError>())
                .map(|error| error.path.as_path()),
            Self::Parse(_)
            | Self::InvalidArguments(_)
            | Self::Conflict(_)
            | Self::Unsupported(_) => None,
        }
    }
}

#[derive(Debug)]
struct PathIoError {
    path: PathBuf,
    source: std::io::Error,
}

impl fmt::Display for PathIoError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "I/O error for `{}`: {}",
            self.path.display(),
            self.source
        )
    }
}

impl std::error::Error for PathIoError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.source)
    }
}

impl From<ParseError> for ApiError {
    fn from(value: ParseError) -> Self {
        Self::Parse(value)
    }
}

impl From<std::io::Error> for ApiError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::error::Error;
    use std::io;
    use std::path::{Path, PathBuf};

    use super::{
        ApiError, CatalogCombineInput, CatalogCombineSelection, CatalogConflictStrategy,
        CatalogFileFormat, CatalogMessage, CatalogMessageKey, CatalogMode, CatalogSemantics,
        CatalogStorageFormat, CatalogUpdateInput, CombineCatalogFilesOptions,
        CombineCatalogOptions, Diagnostic, DiagnosticSeverity, EffectiveTranslation,
        EffectiveTranslationRef, NormalizedParsedCatalog, ObsoleteStrategy, OrderBy,
        ParseCatalogOptions, ParsedCatalog, PlaceholderCommentMode, PluralEncoding, PluralSource,
        RenderOptions, TranslationShape, UpdateCatalogFileOptions, UpdateCatalogOptions,
    };
    use crate::ParseError;

    #[test]
    fn catalog_update_input_defaults_and_conversions_use_expected_variants() {
        assert!(matches!(
            CatalogUpdateInput::default(),
            CatalogUpdateInput::Structured(messages) if messages.is_empty()
        ));
        assert!(matches!(
            CatalogUpdateInput::from(Vec::<super::ExtractedMessage>::new()),
            CatalogUpdateInput::Structured(messages) if messages.is_empty()
        ));
        assert!(matches!(
            CatalogUpdateInput::from(Vec::<super::SourceExtractedMessage>::new()),
            CatalogUpdateInput::SourceFirst(messages) if messages.is_empty()
        ));
    }

    #[test]
    fn catalog_message_helpers_cover_key_and_fallback_behavior() {
        let singular = CatalogMessage {
            msgid: "Hello".to_owned(),
            msgctxt: Some("button".to_owned()),
            translation: TranslationShape::Singular {
                value: String::new(),
            },
            comments: vec!["Shown in toolbar".to_owned()],
            origin: crate::PoVec::new(),
            obsolete: None,
            machine: None,
        };

        assert_eq!(
            singular.key(),
            CatalogMessageKey::with_context("Hello", "button")
        );
        assert!(matches!(
            singular.effective_translation(),
            EffectiveTranslationRef::Singular("")
        ));
        assert_eq!(
            singular.source_fallback_translation(Some("en")),
            EffectiveTranslation::Singular("Hello".to_owned())
        );

        let plural = CatalogMessage {
            msgid: "{count, plural, one {# file} other {# files}}".to_owned(),
            msgctxt: None,
            translation: TranslationShape::Plural {
                source: PluralSource {
                    one: Some("{count} file".to_owned()),
                    other: "{count} files".to_owned(),
                },
                translation: BTreeMap::from([
                    ("one".to_owned(), String::new()),
                    ("other".to_owned(), "{count} Dateien".to_owned()),
                ]),
                variable: "count".to_owned(),
            },
            comments: Vec::new(),
            origin: crate::PoVec::new(),
            obsolete: None,
            machine: None,
        };

        assert!(matches!(
            plural.effective_translation(),
            EffectiveTranslationRef::Plural(values)
                if values.get("other") == Some(&"{count} Dateien".to_owned())
        ));
        assert_eq!(
            plural.source_fallback_translation(Some("de")),
            EffectiveTranslation::Plural(BTreeMap::from([
                ("one".to_owned(), "{count} file".to_owned()),
                ("other".to_owned(), "{count} Dateien".to_owned()),
            ]))
        );
    }

    #[test]
    fn normalized_catalog_helpers_expose_lookup_and_source_fallback_views() {
        let parsed = ParsedCatalog {
            locale: Some("en".to_owned()),
            semantics: CatalogSemantics::IcuNative,
            headers: BTreeMap::new(),
            messages: vec![
                CatalogMessage {
                    msgid: "Hello".to_owned(),
                    msgctxt: None,
                    translation: TranslationShape::Singular {
                        value: String::new(),
                    },
                    comments: Vec::new(),
                    origin: crate::PoVec::new(),
                    obsolete: None,
                    machine: None,
                },
                CatalogMessage {
                    msgid: "Hello".to_owned(),
                    msgctxt: Some("button".to_owned()),
                    translation: TranslationShape::Singular {
                        value: "Howdy".to_owned(),
                    },
                    comments: Vec::new(),
                    origin: crate::PoVec::new(),
                    obsolete: None,
                    machine: None,
                },
            ],
            diagnostics: Vec::new(),
        };

        let normalized = NormalizedParsedCatalog::new(parsed.clone()).expect("normalized");
        let key = CatalogMessageKey::new("Hello", None);

        assert_eq!(normalized.message_count(), 2);
        assert!(normalized.contains_key(&key));
        assert!(normalized.contains_parts("Hello", Some("button")));
        assert_eq!(
            normalized.parsed_catalog().semantics,
            CatalogSemantics::IcuNative
        );
        assert!(normalized.get(&key).is_some());
        assert_eq!(
            normalized
                .get_by_parts("Hello", Some("button"))
                .and_then(|message| match message.effective_translation() {
                    EffectiveTranslationRef::Singular(value) => Some(value),
                    EffectiveTranslationRef::Plural(_) => None,
                }),
            Some("Howdy")
        );
        assert!(matches!(
            normalized.effective_translation_by_parts("Hello", None),
            Some(EffectiveTranslationRef::Singular(""))
        ));
        assert_eq!(
            normalized.effective_translation_with_source_fallback(&key, "en"),
            Some(EffectiveTranslation::Singular("Hello".to_owned()))
        );
        assert_eq!(
            normalized.effective_translation_with_source_fallback_by_parts(
                "Hello",
                Some("button"),
                "en"
            ),
            Some(EffectiveTranslation::Singular("Howdy".to_owned()))
        );
        assert_eq!(normalized.into_parsed_catalog(), parsed);
    }

    #[test]
    fn option_defaults_reflect_native_po_defaults() {
        let update = UpdateCatalogOptions::new("en", Vec::<super::ExtractedMessage>::new());
        assert_eq!(update.mode, CatalogMode::IcuPo);
        assert_eq!(update.obsolete_strategy, ObsoleteStrategy::Mark);
        assert_eq!(update.render.order_by, OrderBy::Msgid);
        assert!(update.render.include_origins);
        assert_eq!(
            update.render.print_placeholders_in_comments,
            PlaceholderCommentMode::Enabled { limit: 3 }
        );
        assert_eq!(update.source_locale, "en");
        assert!(matches!(
            update.input,
            CatalogUpdateInput::Structured(messages) if messages.is_empty()
        ));

        let update_file = UpdateCatalogFileOptions::new(
            Path::new("locale/de.po"),
            "en",
            Vec::<super::SourceExtractedMessage>::new(),
        );
        assert_eq!(update_file.target_path, Path::new("locale/de.po"));
        assert_eq!(update_file.options.mode, CatalogMode::IcuPo);
        assert_eq!(update_file.options.source_locale, "en");
        assert!(matches!(
            update_file.options.input,
            CatalogUpdateInput::SourceFirst(messages) if messages.is_empty()
        ));

        let parse = ParseCatalogOptions::new("msgid \"Hello\"\nmsgstr \"Hallo\"\n", "en");
        assert_eq!(parse.content, "msgid \"Hello\"\nmsgstr \"Hallo\"\n");
        assert_eq!(parse.source_locale, "en");
        assert_eq!(parse.mode, CatalogMode::IcuPo);
        assert!(!parse.strict);

        let inputs = [CatalogCombineInput::labeled(
            "msgid \"Hello\"\nmsgstr \"Hallo\"\n",
            "de.po",
        )];
        let combine = CombineCatalogOptions::new(&inputs, "en");
        assert_eq!(combine.inputs, &inputs);
        assert_eq!(combine.source_locale, "en");
        assert_eq!(combine.mode, CatalogMode::IcuPo);
        assert_eq!(combine.conflict_strategy, CatalogConflictStrategy::UseFirst);
        assert_eq!(combine.selection, CatalogCombineSelection::All);
        assert!(combine.include_origins);
        assert!(!combine.include_obsolete);

        let input_paths = [PathBuf::from("locale/de.po")];
        let combine_files =
            CombineCatalogFilesOptions::new(&input_paths, Path::new("locale/merged.po"), "en");
        assert_eq!(combine_files.input_paths, &input_paths);
        assert_eq!(combine_files.output_path, Path::new("locale/merged.po"));
        assert_eq!(combine_files.format, None);
        assert_eq!(combine_files.mode, None);
        assert_eq!(combine_files.source_locale, "en");
        assert_eq!(
            combine_files.conflict_strategy,
            CatalogConflictStrategy::UseFirst
        );
        assert_eq!(combine_files.selection, CatalogCombineSelection::All);
        assert!(combine_files.include_origins);
        assert!(!combine_files.include_obsolete);
    }

    #[test]
    fn catalog_option_builders_set_fields() {
        let headers = BTreeMap::from([("X-Generator".to_owned(), "ferrocat".to_owned())]);
        let render = RenderOptions::default()
            .with_order_by(OrderBy::Origin)
            .with_include_origins(false)
            .with_placeholder_comments(PlaceholderCommentMode::Disabled)
            .with_custom_header_attributes(&headers);

        assert_eq!(render.order_by, OrderBy::Origin);
        assert!(!render.include_origins);
        assert_eq!(
            render.print_placeholders_in_comments,
            PlaceholderCommentMode::Disabled
        );
        assert_eq!(render.custom_header_attributes, Some(&headers));

        let update = UpdateCatalogOptions::new("en", CatalogUpdateInput::default())
            .with_locale("de")
            .with_existing("msgid \"Hello\"\nmsgstr \"Hallo\"\n")
            .with_mode(CatalogMode::GettextPo)
            .with_render(render.clone())
            .with_obsolete_strategy(ObsoleteStrategy::Delete)
            .with_overwrite_source_translations(true)
            .with_now("2026-07-02");

        assert_eq!(update.locale, Some("de"));
        assert_eq!(update.existing, Some("msgid \"Hello\"\nmsgstr \"Hallo\"\n"));
        assert_eq!(update.mode, CatalogMode::GettextPo);
        assert_eq!(update.render, render);
        assert_eq!(update.obsolete_strategy, ObsoleteStrategy::Delete);
        assert!(update.overwrite_source_translations);
        assert_eq!(update.now, Some("2026-07-02"));

        let parse = ParseCatalogOptions::new("content", "en")
            .with_locale("fr")
            .with_mode(CatalogMode::IcuFcl)
            .with_strict(true);

        assert_eq!(parse.locale, Some("fr"));
        assert_eq!(parse.mode, CatalogMode::IcuFcl);
        assert!(parse.strict);

        let input_paths = [PathBuf::from("base.po"), PathBuf::from("overlay.po")];
        let output_path = Path::new("messages.fcl");
        let combine_files = CombineCatalogFilesOptions::new(&[], Path::new("unused.po"), "en")
            .with_input_paths(&input_paths)
            .with_output_path(output_path)
            .with_format(CatalogFileFormat::Fcl)
            .with_mode(CatalogMode::IcuFcl)
            .with_locale("de")
            .with_conflict_strategy(CatalogConflictStrategy::UseLast)
            .with_selection(CatalogCombineSelection::Unique)
            .with_order_by(OrderBy::Origin)
            .with_include_origins(false)
            .with_include_obsolete(true);

        assert_eq!(combine_files.input_paths, &input_paths);
        assert_eq!(combine_files.output_path, output_path);
        assert_eq!(combine_files.format, Some(CatalogFileFormat::Fcl));
        assert_eq!(combine_files.mode, Some(CatalogMode::IcuFcl));
        assert_eq!(combine_files.locale, Some("de"));
        assert_eq!(
            combine_files.conflict_strategy,
            CatalogConflictStrategy::UseLast
        );
        assert_eq!(combine_files.selection, CatalogCombineSelection::Unique);
        assert_eq!(combine_files.order_by, OrderBy::Origin);
        assert!(!combine_files.include_origins);
        assert!(combine_files.include_obsolete);

        let inputs = [CatalogCombineInput::labeled(
            "msgid \"Hello\"\nmsgstr \"Hallo\"\n",
            "base",
        )];
        let combine = CombineCatalogOptions::new(&[], "en")
            .with_inputs(&inputs)
            .with_locale("de")
            .with_mode(CatalogMode::GettextPo)
            .with_conflict_strategy(CatalogConflictStrategy::UseLast)
            .with_selection(CatalogCombineSelection::Unique)
            .with_order_by(OrderBy::Origin)
            .with_include_origins(false)
            .with_include_obsolete(true);

        assert_eq!(combine.inputs, &inputs);
        assert_eq!(combine.locale, Some("de"));
        assert_eq!(combine.mode, CatalogMode::GettextPo);
        assert_eq!(combine.conflict_strategy, CatalogConflictStrategy::UseLast);
        assert_eq!(combine.selection, CatalogCombineSelection::Unique);
        assert_eq!(combine.order_by, OrderBy::Origin);
        assert!(!combine.include_origins);
        assert!(combine.include_obsolete);

        let file_update = UpdateCatalogFileOptions::new(
            Path::new("messages.po"),
            "en",
            CatalogUpdateInput::default(),
        )
        .with_options(update.clone());

        assert_eq!(file_update.target_path, Path::new("messages.po"));
        assert_eq!(file_update.options, update);
    }

    #[test]
    fn catalog_file_format_infers_supported_suffixes() {
        assert_eq!(
            CatalogFileFormat::infer_from_path(Path::new("locale/de.po")).expect("po"),
            CatalogFileFormat::Po
        );
        assert_eq!(
            CatalogFileFormat::infer_from_path(Path::new("locale/messages.POT")).expect("pot"),
            CatalogFileFormat::Po
        );
        assert_eq!(
            CatalogFileFormat::infer_from_path(Path::new("locale/de.fcl")).expect("fcl"),
            CatalogFileFormat::Fcl
        );
        assert!(matches!(
            CatalogFileFormat::infer_from_path(Path::new("locale/de.txt")),
            Err(ApiError::Unsupported(message)) if message.contains("could not infer")
        ));
    }

    #[test]
    fn catalog_mode_maps_only_supported_catalog_combinations() {
        assert_eq!(CatalogMode::default(), CatalogMode::IcuPo);
        assert_eq!(
            CatalogMode::IcuPo.storage_format(),
            CatalogStorageFormat::Po
        );
        assert_eq!(CatalogMode::IcuPo.semantics(), CatalogSemantics::IcuNative);
        assert_eq!(CatalogMode::IcuPo.plural_encoding(), PluralEncoding::Icu);

        assert_eq!(
            CatalogMode::from_parts(
                CatalogStorageFormat::Fcl,
                CatalogSemantics::IcuNative,
                PluralEncoding::Icu
            ),
            Some(CatalogMode::IcuFcl)
        );
        assert_eq!(
            CatalogMode::from_parts(
                CatalogStorageFormat::Po,
                CatalogSemantics::GettextCompat,
                PluralEncoding::Gettext
            ),
            Some(CatalogMode::GettextPo)
        );
        assert_eq!(
            CatalogMode::from_parts(
                CatalogStorageFormat::Fcl,
                CatalogSemantics::GettextCompat,
                PluralEncoding::Gettext
            ),
            None
        );
        assert_eq!(
            CatalogMode::from_parts(
                CatalogStorageFormat::Po,
                CatalogSemantics::IcuNative,
                PluralEncoding::Icu
            ),
            Some(CatalogMode::IcuPo)
        );
        let update = UpdateCatalogOptions::new("en", Vec::<super::SourceExtractedMessage>::new())
            .with_mode(CatalogMode::GettextPo);
        assert_eq!(update.mode, CatalogMode::GettextPo);
    }

    #[test]
    fn diagnostics_and_api_errors_preserve_human_readable_messages() {
        let diagnostic = Diagnostic::new(DiagnosticSeverity::Warning, "code", "message")
            .with_identity("Hello", Some("button"));
        assert_eq!(diagnostic.severity, DiagnosticSeverity::Warning);
        assert_eq!(diagnostic.code, "code");
        assert_eq!(diagnostic.message, "message");
        assert_eq!(diagnostic.msgid.as_deref(), Some("Hello"));
        assert_eq!(diagnostic.msgctxt.as_deref(), Some("button"));

        let io_error = ApiError::from(io::Error::other("disk"));
        assert_eq!(io_error.to_string(), "disk");
        assert_eq!(
            io_error.source().map(ToString::to_string).as_deref(),
            Some("disk")
        );
        assert_eq!(io_error.path(), None);

        let io_path_error = ApiError::io_with_path(
            Path::new("locale/de.po"),
            io::Error::other("permission denied"),
        );
        assert_eq!(io_path_error.path(), Some(Path::new("locale/de.po")));
        let source = io_path_error.source().expect("io source");
        assert!(source.to_string().contains("locale/de.po"));
        assert_eq!(
            source.source().map(ToString::to_string).as_deref(),
            Some("permission denied")
        );
        assert!(io_path_error.to_string().contains("locale/de.po"));

        let parse_error = ApiError::from(ParseError::new("bad syntax"));
        assert_eq!(
            parse_error.source().map(ToString::to_string).as_deref(),
            Some("bad syntax")
        );
        assert_eq!(
            ApiError::InvalidArguments("bad input".to_owned()).to_string(),
            "bad input"
        );
        assert!(
            ApiError::InvalidArguments("bad input".to_owned())
                .source()
                .is_none()
        );
        assert_eq!(
            ApiError::Conflict("duplicate".to_owned()).to_string(),
            "duplicate"
        );
        assert_eq!(
            ApiError::Unsupported("unsupported".to_owned()).to_string(),
            "unsupported"
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn catalog_message_and_diagnostic_serde_use_stable_public_shapes() {
        let message = CatalogMessage {
            msgid: "Hello".to_owned(),
            msgctxt: Some("button".to_owned()),
            translation: TranslationShape::Singular {
                value: "Hallo".to_owned(),
            },
            comments: vec!["Shown in toolbar".to_owned()],
            origin: crate::PoVec::new(),
            obsolete: None,
            machine: None,
        };
        let message_json =
            serde_json::to_value(&message).expect("catalog message serialization must succeed");
        assert_eq!(message_json["translation"]["kind"], "singular");

        let roundtrip_message: CatalogMessage = serde_json::from_value(message_json)
            .expect("catalog message deserialization must succeed");
        assert_eq!(roundtrip_message, message);

        let diagnostic = Diagnostic::new(
            DiagnosticSeverity::Error,
            "catalog.missing",
            "missing translation",
        )
        .with_identity("Hello", Some("button"));
        let diagnostic_json =
            serde_json::to_value(&diagnostic).expect("diagnostic serialization must succeed");
        assert_eq!(diagnostic_json["severity"], "error");

        let roundtrip_diagnostic: Diagnostic = serde_json::from_value(diagnostic_json)
            .expect("diagnostic deserialization must succeed");
        assert_eq!(roundtrip_diagnostic, diagnostic);
    }
}