xbrl-rs 0.3.0

XBRL parser and validation
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
use super::{Context, ContextId, Fact, InstanceDocument, ItemFact, TupleFact, Unit};
use crate::{
    ExpandedName, NamespacePrefix, NamespaceUri, PresentationArc, RoleUri, TaxonomySet,
    taxonomy::{Concept, ElementParticle, GroupParticle, Particle, PeriodType},
};
use std::{
    cmp::Ordering,
    collections::{HashMap, HashSet},
};

const ARCROLE_ALL: &str = "http://xbrl.org/int/dim/arcrole/all";
const ARCROLE_NOT_ALL: &str = "http://xbrl.org/int/dim/arcrole/notAll";
const ARCROLE_DOMAIN_MEMBER: &str = "http://xbrl.org/int/dim/arcrole/domain-member";

/// Builds a template instance document based on the taxonomy structure, with
/// nil facts for all concrete items and tuples. The generated instance includes
/// all namespaces and schema refs declared in the taxonomy, and the provided
/// contexts and units.
///
/// Tuple facts are emitted according to the schema structure defined by tuple
/// content models. For exclusive single-choice tuple models, only a nil tuple
/// placeholder is emitted without any children, since the template is intended
/// to be fully populated by the user.
///
/// Dimensional items (members of dimensional base sets) are emitted with all
/// applicable dimensional contexts for their period type. If no applicable
/// dimensional contexts are provided for a dimensional item’s period type, that
/// item is omitted.
pub(crate) fn build_instance(
    taxonomy: &TaxonomySet,
    namespaces: HashMap<NamespacePrefix, NamespaceUri>,
    instant_context: Context,
    duration_context: Context,
    dimensional_instant_contexts: Vec<Context>,
    dimensional_duration_contexts: Vec<Context>,
    units: &[Unit],
) -> InstanceDocument {
    let mut instance = InstanceDocument::default();

    for (prefix, uri) in namespaces {
        instance.add_namespace(prefix, uri);
    }

    for schema_url in taxonomy.schema_refs().keys() {
        instance.add_schema_ref(schema_url.to_string());
    }

    let instant_context_ref = instant_context.id.clone();
    let duration_context_ref = duration_context.id.clone();

    instance.add_context(instant_context);
    instance.add_context(duration_context);

    let dimensional_instant_context_refs = dimensional_instant_contexts
        .iter()
        .map(|ctx| ctx.id.clone())
        .collect::<Vec<_>>();
    let dimensional_duration_context_refs = dimensional_duration_contexts
        .iter()
        .map(|ctx| ctx.id.clone())
        .collect::<Vec<_>>();

    for context in dimensional_instant_contexts {
        instance.add_context(context);
    }

    for context in dimensional_duration_contexts {
        instance.add_context(context);
    }

    for unit in units {
        instance.add_unit(unit.clone());
    }

    // Build a deterministic schema graph using tuple content models. The
    // traversal order is schema discovery order + in-schema declaration
    // order + particle child order.
    let mut recursion_path: HashSet<ExpandedName> = HashSet::new();
    let mut emitted_items: HashSet<ExpandedName> = HashSet::new();
    let mut emitted_dimensional_items: HashSet<(ExpandedName, ContextId)> = HashSet::new();
    let mut emitted_tuples: HashSet<ExpandedName> = HashSet::new();
    let concepts = taxonomy.concepts().collect::<Vec<_>>();
    let dimensional_hypercube_items = dimensional_hypercube_items(taxonomy, &[]);
    let schema_index = build_schema_child_index(&concepts, taxonomy);
    let roots = schema_roots(&concepts, &schema_index);
    let mut seeded_nodes: HashSet<ExpandedName> = HashSet::new();
    let skip_items: HashSet<ExpandedName> = HashSet::new();

    for root in roots {
        seeded_nodes.insert(root.clone());
        let mut hoisted: Vec<Fact> = Vec::new();
        populate_from_tree(
            &schema_index,
            root,
            taxonomy,
            &instant_context_ref,
            &duration_context_ref,
            &dimensional_instant_context_refs,
            &dimensional_duration_context_refs,
            units,
            &mut instance.facts,
            &mut emitted_items,
            &mut emitted_dimensional_items,
            &mut emitted_tuples,
            &mut recursion_path,
            None,
            &dimensional_hypercube_items,
            &skip_items,
            &mut hoisted,
        );
        instance.facts.extend(hoisted);
    }

    for concept in &concepts {
        if !schema_participates(concept, &schema_index) {
            continue;
        }

        if seeded_nodes.contains(&concept.name) {
            continue;
        }

        // Skip items already emitted as tuple children during the root traversal
        // above. Without this guard the fallback loop would re-emit them at the
        // top level, producing duplicate facts.
        if emitted_items.contains(&concept.name) {
            continue;
        }

        let mut hoisted: Vec<Fact> = Vec::new();

        populate_from_tree(
            &schema_index,
            &concept.name,
            taxonomy,
            &instant_context_ref,
            &duration_context_ref,
            &dimensional_instant_context_refs,
            &dimensional_duration_context_refs,
            units,
            &mut instance.facts,
            &mut emitted_items,
            &mut emitted_dimensional_items,
            &mut emitted_tuples,
            &mut recursion_path,
            None,
            &dimensional_hypercube_items,
            &skip_items,
            &mut hoisted,
        );

        instance.facts.extend(hoisted);
    }

    instance
}

/// Builds a template instance document restricted to the given presentation
/// roles. Facts are emitted in presentation-arc order; concepts not covered by
/// any of the specified roles are omitted.
///
/// Tuple subtrees are always populated from the schema content model (same as
/// [`build_instance`]) so the tuple structure stays consistent regardless of
/// what the presentation linkbase says about tuple children.
#[allow(clippy::too_many_arguments)]
pub(crate) fn build_instance_from_sections(
    taxonomy: &TaxonomySet,
    roles: &[RoleUri],
    namespaces: HashMap<NamespacePrefix, NamespaceUri>,
    instant_context: Context,
    duration_context: Context,
    dimensional_instant_contexts: Vec<Context>,
    dimensional_duration_contexts: Vec<Context>,
    units: &[Unit],
    dimensional_hypercubes: &[ExpandedName],
) -> InstanceDocument {
    let mut instance = InstanceDocument::default();

    for (prefix, uri) in namespaces {
        instance.add_namespace(prefix, uri);
    }

    for schema_url in taxonomy.schema_refs().keys() {
        instance.add_schema_ref(schema_url.to_string());
    }

    let instant_context_ref = instant_context.id.clone();
    let duration_context_ref = duration_context.id.clone();

    instance.add_context(instant_context);
    instance.add_context(duration_context);

    let dimensional_instant_context_refs = dimensional_instant_contexts
        .iter()
        .map(|ctx| ctx.id.clone())
        .collect::<Vec<_>>();
    let dimensional_duration_context_refs = dimensional_duration_contexts
        .iter()
        .map(|ctx| ctx.id.clone())
        .collect::<Vec<_>>();

    for context in dimensional_instant_contexts {
        instance.add_context(context);
    }

    for context in dimensional_duration_contexts {
        instance.add_context(context);
    }

    for unit in units {
        instance.add_unit(unit.clone());
    }

    let target_dimensional_items = dimensional_hypercube_items(taxonomy, dimensional_hypercubes);
    // When a hypercube filter is active, facts from other hypercubes must not be
    // emitted at all — neither with dimensional nor with plain contexts. Compute
    // the "skip" set: all hypercube members minus the target ones.
    let skip_items: HashSet<ExpandedName> = if dimensional_hypercubes.is_empty() {
        HashSet::new()
    } else {
        let all_dimensional_items = dimensional_hypercube_items(taxonomy, &[]);
        all_dimensional_items
            .difference(&target_dimensional_items)
            .cloned()
            .collect()
    };
    let concepts = taxonomy.concepts().collect::<Vec<_>>();
    let schema_index = build_schema_child_index(&concepts, taxonomy);

    let mut emitted_items: HashSet<ExpandedName> = HashSet::new();
    let mut emitted_dimensional_items: HashSet<(ExpandedName, ContextId)> = HashSet::new();
    let mut emitted_tuples: HashSet<ExpandedName> = HashSet::new();
    let mut recursion_path: HashSet<ExpandedName> = HashSet::new();

    for role in roles {
        let Some(arcs) = taxonomy.presentation_arcs(role.as_str()) else {
            continue;
        };

        let mut arc_index: HashMap<&ExpandedName, Vec<&PresentationArc>> = HashMap::new();

        for arc in arcs {
            arc_index.entry(&arc.from).or_default().push(arc);
        }

        for children in arc_index.values_mut() {
            children.sort_by(|a, b| match (a.order, b.order) {
                (Some(x), Some(y)) => x.cmp(&y),
                (Some(_), None) => Ordering::Less,
                (None, Some(_)) => Ordering::Greater,
                (None, None) => Ordering::Equal,
            });
        }

        let roots = super::view::find_roots(arcs, &arc_index);

        for root in roots {
            recursion_path.clear();
            populate_from_presentation(
                &arc_index,
                &schema_index,
                root,
                taxonomy,
                &instant_context_ref,
                &duration_context_ref,
                &dimensional_instant_context_refs,
                &dimensional_duration_context_refs,
                units,
                &mut instance.facts,
                &mut emitted_items,
                &mut emitted_dimensional_items,
                &mut emitted_tuples,
                &mut recursion_path,
                &target_dimensional_items,
                &skip_items,
            );
        }
    }

    instance
}

/// Walk one node of the presentation tree and emit facts.
///
/// - Concrete tuple  → delegates to [`populate_from_tree`] (schema content model owns children).
/// - Concrete item   → emits a nil [`ItemFact`]; recurses into presentation children.
/// - Abstract / grouping → recurses into presentation children.
///
/// `skip_items` lists concepts that belong to hypercubes for which no dimensional
/// contexts were provided. These are marked as emitted but not pushed as facts.
#[allow(clippy::too_many_arguments)]
fn populate_from_presentation(
    arc_index: &HashMap<&ExpandedName, Vec<&PresentationArc>>,
    schema_index: &HashMap<ExpandedName, Vec<ExpandedName>>,
    concept_name: &ExpandedName,
    taxonomy: &TaxonomySet,
    instant_ctx: &ContextId,
    duration_ctx: &ContextId,
    dimensional_instant_ctxs: &[ContextId],
    dimensional_duration_ctxs: &[ContextId],
    units: &[Unit],
    facts: &mut Vec<Fact>,
    emitted_items: &mut HashSet<ExpandedName>,
    emitted_dimensional_items: &mut HashSet<(ExpandedName, ContextId)>,
    emitted_tuples: &mut HashSet<ExpandedName>,
    recursion_path: &mut HashSet<ExpandedName>,
    dimensional_hypercube_items: &HashSet<ExpandedName>,
    skip_items: &HashSet<ExpandedName>,
) {
    if !recursion_path.insert(concept_name.clone()) {
        return;
    }

    if let Some(concept) = taxonomy.find_concept(concept_name) {
        if concept.is_tuple() && !concept.is_abstract {
            // Delegate entirely to schema-based traversal so tuple children
            // follow the xs:complexType content model. Use a fresh
            // recursion_path so the schema walk's visited set doesn't
            // contaminate the outer presentation walk.
            let mut tuple_recursion_path: HashSet<ExpandedName> = HashSet::new();
            let mut hoisted: Vec<Fact> = Vec::new();
            populate_from_tree(
                schema_index,
                concept_name,
                taxonomy,
                instant_ctx,
                duration_ctx,
                dimensional_instant_ctxs,
                dimensional_duration_ctxs,
                units,
                facts,
                emitted_items,
                emitted_dimensional_items,
                emitted_tuples,
                &mut tuple_recursion_path,
                None,
                dimensional_hypercube_items,
                skip_items,
                &mut hoisted,
            );
            facts.extend(hoisted);
            recursion_path.remove(concept_name);
            return;
        }

        if !concept.is_abstract
            && let Some(ref period_type) = concept.period_type
        {
            if dimensional_hypercube_items.contains(concept_name) {
                let context_refs = dimensional_context_refs_for_period(
                    period_type,
                    dimensional_instant_ctxs,
                    dimensional_duration_ctxs,
                );
                if context_refs.is_empty() {
                    recursion_path.remove(concept_name);
                    return;
                }

                for context_ref in context_refs {
                    if !emitted_dimensional_items
                        .insert((concept_name.clone(), context_ref.clone()))
                    {
                        continue;
                    }

                    let mut fact = ItemFact::new(
                        None,
                        concept.name.clone(),
                        context_ref.to_string(),
                        unit_ref_for_concept(concept, units),
                        String::new(),
                        true,
                        None,
                        None,
                    );
                    fact.set_nil(true);
                    facts.push(Fact::Item(fact));
                }
            } else if skip_items.contains(concept_name) {
                // Member of a hypercube we have no contexts for: omit but mark
                // as emitted so the fallback traversal doesn't re-emit it.
                emitted_items.insert(concept_name.clone());
            } else if emitted_items.insert(concept_name.clone()) {
                let context_ref =
                    default_context_ref_for_period(period_type, instant_ctx, duration_ctx);
                let mut fact = ItemFact::new(
                    None,
                    concept.name.clone(),
                    context_ref.to_string(),
                    unit_ref_for_concept(concept, units),
                    String::new(),
                    true,
                    None,
                    None,
                );
                fact.set_nil(true);
                facts.push(Fact::Item(fact));
            }
        }
    }

    // Recurse into presentation children for abstract concepts, concrete items,
    // and concepts not found in the schema.
    let children = arc_index
        .get(concept_name)
        .map(Vec::as_slice)
        .unwrap_or(&[]);
    for arc in children {
        populate_from_presentation(
            arc_index,
            schema_index,
            &arc.to,
            taxonomy,
            instant_ctx,
            duration_ctx,
            dimensional_instant_ctxs,
            dimensional_duration_ctxs,
            units,
            facts,
            emitted_items,
            emitted_dimensional_items,
            emitted_tuples,
            recursion_path,
            dimensional_hypercube_items,
            skip_items,
        );
    }

    recursion_path.remove(concept_name);
}

/// Recursively walk one node of the presentation tree and emit facts.
///
/// - Concrete tuple -> push a [`TupleFact`] and recurse into its children.
/// - Concrete item  -> push an [`ItemFact`] (nil placeholder).  If the item
///   is not a valid schema child of the enclosing tuple (per its
///   `xs:complexType` content model) it is pushed to `hoisted` instead,
///   which `from_taxonomy` appends to the top-level facts after all sections
///   have been traversed.
/// - Abstract / grouping -> recurse into children at the same level.
#[allow(clippy::too_many_arguments)]
fn populate_from_tree(
    schema_index: &HashMap<ExpandedName, Vec<ExpandedName>>,
    concept_name: &ExpandedName,
    taxonomy: &TaxonomySet,
    instant_ctx: &ContextId,
    duration_ctx: &ContextId,
    dimensional_instant_ctxs: &[ContextId],
    dimensional_duration_ctxs: &[ContextId],
    units: &[Unit],
    facts: &mut Vec<Fact>,
    emitted_items: &mut HashSet<ExpandedName>,
    emitted_dimensional_items: &mut HashSet<(ExpandedName, ContextId)>,
    emitted_tuples: &mut HashSet<ExpandedName>,
    recursion_path: &mut HashSet<ExpandedName>,
    parent_tuple_element: Option<&Concept>,
    dimensional_hypercube_items: &HashSet<ExpandedName>,
    skip_items: &HashSet<ExpandedName>,
    hoisted: &mut Vec<Fact>,
) {
    if !recursion_path.insert(concept_name.clone()) {
        return; // cycle guard within current recursion branch
    }

    let children = schema_index
        .get(concept_name)
        .map(Vec::as_slice)
        .unwrap_or(&[]);

    if let Some(concept) = taxonomy.find_concept(concept_name) {
        if concept.is_tuple() && !concept.is_abstract {
            if emitted_tuples.insert(concept_name.clone()) {
                let mut tuple = TupleFact::new(concept.name.clone());

                // For exclusive single-choice tuple models, generate a nil
                // tuple placeholder.
                if tuple_uses_nil_template(concept) {
                    tuple.set_nil(true);
                    facts.push(Fact::Tuple(tuple));

                    // Skip materialization of this tuple's presentation
                    // descendants and mark them as emitted so fallback
                    // traversal does not emit them at top level.
                    let mut skipped_visited: HashSet<ExpandedName> = HashSet::new();
                    for child_name in children {
                        mark_schema_subtree_as_emitted(
                            schema_index,
                            child_name,
                            emitted_items,
                            emitted_tuples,
                            &mut skipped_visited,
                        );
                    }

                    recursion_path.remove(concept_name);
                    return;
                }

                facts.push(Fact::Tuple(tuple));

                let tuple_children = match facts.last_mut() {
                    Some(Fact::Tuple(tuple)) => tuple.children_mut(),
                    _ => unreachable!(),
                };

                for child_name in children {
                    populate_from_tree(
                        schema_index,
                        child_name,
                        taxonomy,
                        instant_ctx,
                        duration_ctx,
                        dimensional_instant_ctxs,
                        dimensional_duration_ctxs,
                        units,
                        tuple_children,
                        emitted_items,
                        emitted_dimensional_items,
                        emitted_tuples,
                        recursion_path,
                        Some(concept),
                        dimensional_hypercube_items,
                        skip_items,
                        hoisted,
                    );
                }
            }
            recursion_path.remove(concept_name);
            return;
        }

        if !concept.is_abstract
            && let Some(ref period_type) = concept.period_type
        {
            if dimensional_hypercube_items.contains(concept_name) {
                let context_refs = dimensional_context_refs_for_period(
                    period_type,
                    dimensional_instant_ctxs,
                    dimensional_duration_ctxs,
                );

                if context_refs.is_empty() {
                    recursion_path.remove(concept_name);
                    return;
                }

                for context_ref in context_refs {
                    if !emitted_dimensional_items
                        .insert((concept_name.clone(), context_ref.clone()))
                    {
                        continue;
                    }

                    let mut fact = ItemFact::new(
                        None,
                        concept.name.clone(),
                        context_ref.to_string(),
                        unit_ref_for_concept(concept, units),
                        String::new(),
                        true,
                        None,
                        None,
                    );
                    fact.set_nil(true);

                    // Items not allowed by the tuple's content model are hoisted to
                    // the top level so they still appear in the generated template.
                    if let Some(parent_el) = parent_tuple_element
                        && !item_allowed_in_tuple(parent_el, concept, taxonomy)
                    {
                        hoisted.push(Fact::Item(fact));
                    } else {
                        facts.push(Fact::Item(fact));
                    }
                }
            } else if skip_items.contains(concept_name) {
                // Member of a hypercube we have no contexts for: omit but mark
                // as emitted so the fallback traversal doesn't re-emit it.
                emitted_items.insert(concept_name.clone());
            } else {
                // Mark as emitted so `populate_from_presentation` does not re-emit
                // this concept as a top-level fact later.
                emitted_items.insert(concept_name.clone());

                let context_ref =
                    default_context_ref_for_period(period_type, instant_ctx, duration_ctx);
                let mut fact = ItemFact::new(
                    None,
                    concept.name.clone(),
                    context_ref.to_string(),
                    unit_ref_for_concept(concept, units),
                    String::new(),
                    true,
                    None,
                    None,
                );
                fact.set_nil(true);

                // Items not allowed by the tuple's content model are hoisted to
                // the top level so they still appear in the generated template.
                if let Some(parent_el) = parent_tuple_element
                    && !item_allowed_in_tuple(parent_el, concept, taxonomy)
                {
                    hoisted.push(Fact::Item(fact));
                } else {
                    facts.push(Fact::Item(fact));
                }
            }
        }
    }

    // Recurse children at the same level for abstract/grouping parents.
    for child_name in children {
        populate_from_tree(
            schema_index,
            child_name,
            taxonomy,
            instant_ctx,
            duration_ctx,
            dimensional_instant_ctxs,
            dimensional_duration_ctxs,
            units,
            facts,
            emitted_items,
            emitted_dimensional_items,
            emitted_tuples,
            recursion_path,
            parent_tuple_element,
            dimensional_hypercube_items,
            skip_items,
            hoisted,
        );
    }

    recursion_path.remove(concept_name);
}

fn default_context_ref_for_period<'a>(
    period_type: &PeriodType,
    instant_ctx: &'a ContextId,
    duration_ctx: &'a ContextId,
) -> &'a ContextId {
    match period_type {
        PeriodType::Duration => duration_ctx,
        PeriodType::Instant => instant_ctx,
    }
}

fn dimensional_context_refs_for_period<'a>(
    period_type: &PeriodType,
    dimensional_instant_ctxs: &'a [ContextId],
    dimensional_duration_ctxs: &'a [ContextId],
) -> &'a [ContextId] {
    match period_type {
        PeriodType::Duration => dimensional_duration_ctxs,
        PeriodType::Instant => dimensional_instant_ctxs,
    }
}

/// Collect concepts that belong to dimensional base sets and therefore require
/// dimensional contexts.
///
/// When `hypercube_filter` is non-empty, only concepts that are domain members
/// of the listed hypercubes are returned. Pass an empty slice to collect from
/// all hypercubes.
fn dimensional_hypercube_items(
    taxonomy: &TaxonomySet,
    hypercube_filter: &[ExpandedName],
) -> HashSet<ExpandedName> {
    let filter_set: HashSet<&ExpandedName> = hypercube_filter.iter().collect();
    let use_filter = !filter_set.is_empty();
    let mut result: HashSet<ExpandedName> = HashSet::new();

    for arcs in taxonomy.definitions().values() {
        if use_filter {
            let has_target = arcs.iter().any(|arc| {
                (arc.arcrole.as_str() == ARCROLE_ALL || arc.arcrole.as_str() == ARCROLE_NOT_ALL)
                    && filter_set.contains(&arc.to)
            });

            if !has_target {
                continue;
            }
        }

        let mut domain_children: HashMap<ExpandedName, Vec<ExpandedName>> = HashMap::new();
        let mut roots: Vec<ExpandedName> = Vec::new();

        for arc in arcs {
            match arc.arcrole.as_str() {
                ARCROLE_ALL | ARCROLE_NOT_ALL => {
                    roots.push(arc.from.clone());
                }
                ARCROLE_DOMAIN_MEMBER => {
                    domain_children
                        .entry(arc.from.clone())
                        .or_default()
                        .push(arc.to.clone());
                }
                _ => {}
            }
        }

        let mut stack = roots;
        let mut visited: HashSet<ExpandedName> = HashSet::new();

        while let Some(current) = stack.pop() {
            if !visited.insert(current.clone()) {
                continue;
            }

            if let Some(children) = domain_children.get(&current) {
                stack.extend(children.iter().cloned());
            }
        }

        result.extend(visited);
    }

    result
}

/// Determine the correct `unitRef` string for an element based on its XSD type.
///
/// - Monetary items  -> first currency unit (`is_currency()`)
/// - Shares items    -> first shares unit (`is_shares()`)
/// - Other numeric   -> first pure unit (`is_pure()`)
/// - Non-numeric     -> `None` (unitRef forbidden by the XBRL spec)
fn unit_ref_for_concept(concept: &Concept, units: &[Unit]) -> Option<String> {
    let type_name = &concept.data_type;

    if type_name.is_monetary() {
        return units
            .iter()
            .find(|u| u.is_currency())
            .map(|u| u.id.to_string());
    }

    if type_name.is_shares() {
        return units
            .iter()
            .find(|u| u.is_shares())
            .map(|u| u.id.to_string());
    }

    if type_name.is_numeric() {
        return units.iter().find(|u| u.is_pure()).map(|u| u.id.to_string());
    }

    None
}

/// Returns `true` if `child_element` is a valid schema child of `parent_element`.
///
/// A child is allowed when `parent_element.content_model` is `None` (no explicit
/// content model) or when the child's element name or substitution-group ancestry
/// matches an element particle in the content model.
fn item_allowed_in_tuple(
    parent_element: &Concept,
    child_element: &Concept,
    taxonomy: &TaxonomySet,
) -> bool {
    let Some(model) = &parent_element.content_model else {
        return true;
    };

    if matches_particle_model(model, child_element, taxonomy) {
        return true;
    }

    // Group references are not currently resolved into concrete particles in
    // the semantic model. When they are present, avoid false-negative child
    // rejection and keep generated tuple children in place.
    particle_contains_group_ref(model)
}

/// Returns `true` when the tuple should be emitted as tuple-level nil in
/// `from_taxonomy`, because its content model is an exclusive single-choice.
fn tuple_uses_nil_template(concept: &Concept) -> bool {
    if !concept.is_tuple() {
        return false;
    }

    concept
        .content_model
        .as_ref()
        .is_some_and(is_exclusive_single_choice_particle)
}

/// Marks all concepts reachable from `start` in the presentation arc graph as
/// emitted, so fallback traversal does not materialize them later.
fn mark_schema_subtree_as_emitted(
    schema_index: &HashMap<ExpandedName, Vec<ExpandedName>>,
    start: &ExpandedName,
    emitted_items: &mut HashSet<ExpandedName>,
    emitted_tuples: &mut HashSet<ExpandedName>,
    visited: &mut HashSet<ExpandedName>,
) {
    if !visited.insert(start.clone()) {
        return;
    }

    emitted_items.insert(start.clone());
    emitted_tuples.insert(start.clone());

    if let Some(children) = schema_index.get(start) {
        for child_name in children {
            mark_schema_subtree_as_emitted(
                schema_index,
                child_name,
                emitted_items,
                emitted_tuples,
                visited,
            );
        }
    }
}

/// Builds an index of parent concept name -> child concept names based on tuple
/// content models in the taxonomy.
fn build_schema_child_index(
    concepts: &[&Concept],
    taxonomy: &TaxonomySet,
) -> HashMap<ExpandedName, Vec<ExpandedName>> {
    let mut index: HashMap<ExpandedName, Vec<ExpandedName>> = HashMap::new();

    for parent in concepts {
        if !parent.is_tuple() {
            continue;
        }
        let Some(model) = &parent.content_model else {
            continue;
        };

        let mut children: Vec<ExpandedName> = Vec::new();
        collect_model_children(model, parent, concepts, &mut children);

        children.retain(|child| child != &parent.name);
        children.dedup();

        if children.is_empty() {
            children = fallback_presentation_children(parent, taxonomy);
        }

        if !children.is_empty() {
            index.insert(parent.name.clone(), children);
        }
    }

    index
}

/// Recursively collects allowed child concept names from a particle model.
fn collect_model_children(
    particle: &Particle,
    parent: &Concept,
    concepts: &[&Concept],
    out: &mut Vec<ExpandedName>,
) {
    match particle {
        Particle::Element { element, .. } => {
            let allowed_local = match element {
                ElementParticle::Ref(qname) => qname.local_name.as_str(),
                ElementParticle::Decl(declaration) => declaration.name.as_str(),
            };

            let mut same_namespace_matches = Vec::new();
            let mut any_namespace_matches = Vec::new();

            for concept in concepts {
                let direct_local_match = concept.name.local_name == allowed_local;
                let direct_substitution_match =
                    concept.substitution_group.original.local_name == allowed_local;

                if !(direct_local_match || direct_substitution_match) {
                    continue;
                }

                if concept.name.namespace_uri == parent.name.namespace_uri {
                    same_namespace_matches.push(concept.name.clone());
                } else {
                    any_namespace_matches.push(concept.name.clone());
                }
            }

            if same_namespace_matches.is_empty() {
                out.extend(any_namespace_matches);
            } else {
                out.extend(same_namespace_matches);
            }
        }
        Particle::Sequence { children, .. } | Particle::Choice { children, .. } => {
            for child in children {
                collect_model_children(child, parent, concepts, out);
            }
        }
        Particle::Group { group, .. } => {
            if let GroupParticle::Def(group_def) = group {
                collect_model_children(&group_def.particle, parent, concepts, out);
            }
        }
    }
}

/// Fallback child collection based on presentation relationships when no
/// explicit content model is defined for a tuple concept.
fn fallback_presentation_children(parent: &Concept, taxonomy: &TaxonomySet) -> Vec<ExpandedName> {
    let mut children: Vec<(ExpandedName, Option<rust_decimal::Decimal>)> = taxonomy
        .presentations()
        .values()
        .flat_map(|arcs| arcs.iter())
        .filter(|arc| arc.from == parent.name)
        .map(|arc| (arc.to.clone(), arc.order))
        .collect();

    children.sort_by(|a, b| match (a.1, b.1) {
        (Some(x), Some(y)) => x.cmp(&y),
        (Some(_), None) => Ordering::Less,
        (None, Some(_)) => Ordering::Greater,
        (None, None) => Ordering::Equal,
    });

    let mut ordered = Vec::new();
    for (name, _) in children {
        if !ordered.contains(&name) {
            ordered.push(name);
        }
    }

    ordered
}

/// Returns the root concept names that participate in the schema (tuple or
/// concrete item concepts that are reachable from the presentation
/// relationships).
fn schema_roots<'a>(
    concepts: &'a [&'a Concept],
    schema_index: &HashMap<ExpandedName, Vec<ExpandedName>>,
) -> Vec<&'a ExpandedName> {
    let mut child_names: HashSet<&ExpandedName> = HashSet::new();

    for children in schema_index.values() {
        for child in children {
            child_names.insert(child);
        }
    }

    concepts
        .iter()
        .filter(|concept| schema_participates(concept, schema_index))
        .map(|concept| &concept.name)
        .filter(|name| !child_names.contains(name))
        .collect()
}

/// Returns `true` when the concept participates in the schema as a tuple or
/// concrete item, or as an abstract parent of other participating concepts.
fn schema_participates(
    concept: &Concept,
    schema_index: &HashMap<ExpandedName, Vec<ExpandedName>>,
) -> bool {
    concept.is_tuple()
        || concept.is_concrete_item()
        || (concept.is_abstract() && schema_index.contains_key(&concept.name))
}

/// Returns `true` when `model` effectively represents an exclusive choice
/// where at most one alternative can be selected.
///
/// Supported nested form:
/// - a direct `choice` with `maxOccurs=1`
/// - wrappers (`sequence`/`group`) with `maxOccurs=1` and exactly one child,
///   recursively ending in the same exclusive-choice shape
fn is_exclusive_single_choice_particle(model: &Particle) -> bool {
    match model {
        Particle::Choice { occurs, .. } => occurs.max == Some(1),
        Particle::Sequence { children, occurs } => {
            occurs.max == Some(1)
                && children.len() == 1
                && is_exclusive_single_choice_particle(&children[0])
        }
        Particle::Group { group, occurs } => {
            occurs.max == Some(1)
                && match group {
                    GroupParticle::Def(group_def) => {
                        is_exclusive_single_choice_particle(&group_def.particle)
                    }
                    GroupParticle::Ref(_) => false,
                }
        }
        Particle::Element { .. } => false,
    }
}

fn particle_contains_group_ref(model: &Particle) -> bool {
    match model {
        Particle::Element { .. } => false,
        Particle::Sequence { children, .. } | Particle::Choice { children, .. } => {
            children.iter().any(particle_contains_group_ref)
        }
        Particle::Group { group, .. } => match group {
            GroupParticle::Ref(_) => true,
            GroupParticle::Def(group_def) => particle_contains_group_ref(&group_def.particle),
        },
    }
}

/// Returns `true` if `child_element` satisfies any element particle in `model`,
/// either by a direct name match or via substitution-group ancestry.
fn matches_particle_model(
    model: &Particle,
    child_element: &Concept,
    taxonomy: &TaxonomySet,
) -> bool {
    model
        .elements()
        .iter()
        .any(|element_particle| matches_element_particle(element_particle, child_element, taxonomy))
}

/// Returns `true` if `child_element` satisfies the element particle, either
/// by a direct name match or via its substitution-group ancestry chain.
fn matches_element_particle(
    element_particle: &ElementParticle,
    child_element: &Concept,
    taxonomy: &TaxonomySet,
) -> bool {
    let allowed_local = match element_particle {
        ElementParticle::Ref(qname) => qname.local_name.as_str(),
        ElementParticle::Decl(declaration) => declaration.name.as_str(),
    };

    if child_element.name.local_name == allowed_local {
        return true;
    }

    // Walk the substitution group ancestry: if the child's substitution group
    // (or any ancestor in the chain) matches the declared element particle, the
    // element is a valid substitute.
    let mut current = child_element;

    loop {
        let parent_substitution_group = &current.substitution_group.original;

        if parent_substitution_group.local_name == allowed_local {
            return true;
        }

        match taxonomy.find_concept(parent_substitution_group) {
            Some(parent) => current = parent,
            None => break,
        }
    }

    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        BaseSubstitutionGroup, Concept, ElementDecl, ElementParticle, ExpandedName, GroupDef,
        GroupParticle, NamespaceUri, Occurrence, Particle, PeriodType, SubstitutionGroup, XbrlType,
    };

    fn expanded_name(local_name: &str) -> ExpandedName {
        ExpandedName::new(
            NamespaceUri::from("http://example.com/ns"),
            local_name.to_owned(),
        )
    }

    #[test]
    fn unit_ref_for_pure_concept_prefers_pure_unit() {
        let concept = Concept {
            id: Some("pureConcept".to_owned()),
            name: expanded_name("pureConcept"),
            data_type: XbrlType::Pure,
            substitution_group: SubstitutionGroup {
                base: BaseSubstitutionGroup::Item,
                original: ExpandedName::new(
                    NamespaceUri::from("http://www.xbrl.org/2003/instance"),
                    "item".to_owned(),
                ),
            },
            period_type: Some(PeriodType::Instant),
            balance: None,
            nillable: true,
            is_abstract: false,
            content_model: None,
        };

        let units = vec![
            Unit::new(
                "EUR".into(),
                vec![ExpandedName::new(
                    NamespaceUri::from("http://www.xbrl.org/2003/iso4217"),
                    "EUR".to_owned(),
                )],
                vec![],
            ),
            Unit::new(
                "pure".into(),
                vec![ExpandedName::new(
                    NamespaceUri::from("http://www.xbrl.org/2003/instance"),
                    "pure".to_owned(),
                )],
                vec![],
            ),
        ];

        assert_eq!(
            unit_ref_for_concept(&concept, &units),
            Some("pure".to_owned())
        );
    }

    #[test]
    fn exclusive_choice_particle_detected() {
        let model = Particle::Choice {
            children: vec![
                Particle::Element {
                    element: ElementParticle::Decl(ElementDecl {
                        name: "A".to_owned(),
                        type_name: None,
                        inline_type: None,
                    }),
                    occurs: Occurrence {
                        min: 0,
                        max: Some(1),
                    },
                },
                Particle::Element {
                    element: ElementParticle::Decl(ElementDecl {
                        name: "B".to_owned(),
                        type_name: None,
                        inline_type: None,
                    }),
                    occurs: Occurrence {
                        min: 0,
                        max: Some(1),
                    },
                },
            ],
            occurs: Occurrence {
                min: 1,
                max: Some(1),
            },
        };

        assert!(is_exclusive_single_choice_particle(&model));
    }

    #[test]
    fn repeating_choice_particle_not_detected() {
        let model = Particle::Choice {
            children: vec![Particle::Element {
                element: ElementParticle::Decl(ElementDecl {
                    name: "A".to_owned(),
                    type_name: None,
                    inline_type: None,
                }),
                occurs: Occurrence {
                    min: 0,
                    max: Some(1),
                },
            }],
            occurs: Occurrence { min: 0, max: None },
        };

        assert!(!is_exclusive_single_choice_particle(&model));
    }

    #[test]
    fn nested_exclusive_choice_particle_detected() {
        let model = Particle::Sequence {
            children: vec![Particle::Group {
                group: GroupParticle::Def(GroupDef {
                    name: None,
                    particle: Box::new(Particle::Choice {
                        children: vec![Particle::Element {
                            element: ElementParticle::Decl(ElementDecl {
                                name: "A".to_owned(),
                                type_name: None,
                                inline_type: None,
                            }),
                            occurs: Occurrence {
                                min: 0,
                                max: Some(1),
                            },
                        }],
                        occurs: Occurrence {
                            min: 0,
                            max: Some(1),
                        },
                    }),
                }),
                occurs: Occurrence {
                    min: 1,
                    max: Some(1),
                },
            }],
            occurs: Occurrence {
                min: 1,
                max: Some(1),
            },
        };

        assert!(is_exclusive_single_choice_particle(&model));
    }

    #[test]
    fn sequence_with_multiple_children_not_detected_as_exclusive_choice() {
        let model = Particle::Sequence {
            children: vec![
                Particle::Element {
                    element: ElementParticle::Decl(ElementDecl {
                        name: "A".to_owned(),
                        type_name: None,
                        inline_type: None,
                    }),
                    occurs: Occurrence {
                        min: 0,
                        max: Some(1),
                    },
                },
                Particle::Element {
                    element: ElementParticle::Decl(ElementDecl {
                        name: "B".to_owned(),
                        type_name: None,
                        inline_type: None,
                    }),
                    occurs: Occurrence {
                        min: 0,
                        max: Some(1),
                    },
                },
            ],
            occurs: Occurrence {
                min: 1,
                max: Some(1),
            },
        };

        assert!(!is_exclusive_single_choice_particle(&model));
    }
}