helios-fhirpath 0.2.0

This is an implementation of HL7's FHIRPath Specification.
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
//! # FHIRPath Polymorphic Element Access
//!
//! Handles accessing polymorphic FHIR elements (e.g., value[x]) in FHIRPath expressions.

use helios_fhirpath_support::{EvaluationError, EvaluationResult};
use std::collections::HashMap;

/// # Polymorphic Access
///
/// This module implements polymorphic access for FHIR choice elements in FHIRPath.
///
/// In FHIR, choice elements are fields that can contain different types of data,
/// indicated by a suffix in the field name. For example, Observation.value\[x\]
/// might be represented as:
/// - valueQuantity (with type Quantity)
/// - valueString (with type String)
/// - valueCodeableConcept (with type CodeableConcept)
/// - etc.
///
/// FHIRPath allows accessing choice elements using the base name, without the type suffix.
/// For example, `Observation.value` should resolve to the appropriate element (valueQuantity,
/// valueString, etc.) based on which one is present in the resource.
///
/// This module provides the implementation for this polymorphic access pattern, including:
/// - Identifying choice elements in FHIR resources
/// - Accessing choice elements by their base name
/// - Filtering choice elements by type (using is/as operators)
///
/// Handles polymorphic access to FHIR resource choice elements.
///
/// This function resolves a field name in a FHIR resource object, handling choice elements
/// according to FHIRPath rules. For choice elements like value\[x\], it will find the
/// appropriate concrete field (e.g., valueQuantity) based on what's available in the object.
///
/// # Arguments
///
/// * `obj` - A reference to a HashMap representing a FHIR resource or part of a resource
/// * `field_name` - The name of the field to access, which may be a choice element base name
///
/// # Returns
///
/// * `Some(EvaluationResult)` if the field was found (either directly or via polymorphic access)
/// * `None` if the field wasn't found
///
/// # Examples
///
/// // For a FHIR Observation with valueQuantity:
/// // access_polymorphic_element(observation, "value") -> Some(valueQuantity)
/// // access_polymorphic_element(observation, "value.unit") -> Some(unit)
pub fn access_polymorphic_element(
    obj: &HashMap<String, EvaluationResult>,
    field_name: &str,
) -> Option<EvaluationResult> {
    // First, try direct access - field might already be the right name
    if let Some(value) = obj.get(field_name) {
        return Some(value.clone());
    }

    // Special case for common polymorphic path patterns (like 'value.unit', 'value.code', etc.)
    if field_name.contains('.') {
        let parts: Vec<&str> = field_name.split('.').collect();
        let first_part = parts[0];
        let rest = &parts[1..].join(".");

        // Handle path with potential choice element as the first part
        if is_choice_element(first_part) {
            // Try to resolve the choice element
            let matches = get_polymorphic_fields(obj, first_part);

            // Process each matching field
            for (_, value) in &matches {
                if let EvaluationResult::Object {
                    map: inner_obj,
                    type_info: _,
                } = value
                {
                    // Recursively resolve the rest of the path
                    if let Some(result) = access_polymorphic_element(inner_obj, rest) {
                        return Some(result);
                    }
                }
            }

            // Handle special cases for all potential typed fields
            // This covers patterns like value.unit -> valueQuantity.unit
            for (key, value) in obj.iter() {
                // Check if key starts with the first part and has a type suffix
                if key.starts_with(first_part) && key.len() > first_part.len() {
                    // Extract the type suffix (need uppercase letter after base name)
                    if let Some(c) = key.chars().nth(first_part.len()) {
                        if c.is_uppercase() {
                            // This is a potential choice element with type suffix
                            if let EvaluationResult::Object {
                                map: inner_obj,
                                type_info: _,
                            } = value
                            {
                                // Try to resolve the rest of the path
                                if let Some(result) = access_polymorphic_element(inner_obj, rest) {
                                    return Some(result);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            // Regular path (not a choice element)
            if let Some(value) = obj.get(first_part) {
                if let EvaluationResult::Object {
                    map: inner_obj,
                    type_info: _,
                } = value
                {
                    return access_polymorphic_element(inner_obj, rest);
                }
            }
        }

        // No match found for the path
        return None;
    }

    // Check if this could be a choice element
    // Even without metadata, we can try to find polymorphic fields
    // based on the pattern of fields in the object
    let matching_fields = get_polymorphic_fields(obj, field_name);

    // If we found any matches, it's likely a choice element
    if !matching_fields.is_empty() {
        // If we found exactly one match, return it
        if matching_fields.len() == 1 {
            return Some(matching_fields[0].1.clone());
        }

        // If we found multiple matches, return the first one
        return Some(matching_fields[0].1.clone());
    }

    // No matching field found
    None
}

/// Gets all possible polymorphic fields for a choice element.
///
/// This function searches an object for fields that match the polymorphic pattern
/// for a given base name. For example, with base_name "value", it will look for
/// fields like "valueQuantity", "valueString", etc.
///
/// # Arguments
///
/// * `obj` - A reference to a HashMap representing a FHIR resource or part of a resource
/// * `base_name` - The base name of the choice element to search for
///
/// # Returns
///
/// A vector of tuples containing the field name and value for all matching fields
fn get_polymorphic_fields(
    obj: &HashMap<String, EvaluationResult>,
    base_name: &str,
) -> Vec<(String, EvaluationResult)> {
    let mut matches = Vec::new();

    if let Some(value) = obj.get(base_name) {
        matches.push((base_name.to_string(), value.clone()));
    }

    // Preferred path: when `obj` identifies a FHIR resource, consult the
    // generated `FIELD_TYPES` table for that parent and pull out only the
    // typed variants that are both declared in the spec and present in the
    // data. More accurate than the JSON-key prefix scan below, which can
    // match unrelated fields whose names happen to start with `base_name`.
    let mut consulted_field_types = false;
    if let Some(EvaluationResult::String(resource_type, _, _)) = obj.get("resourceType")
        && let Some(table) = helios_fhir::field_types(helios_fhir::FhirVersion::default_enabled())
    {
        consulted_field_types = true;
        for (parent, field, _ty, _is_collection) in table {
            if parent != resource_type {
                continue;
            }
            let Some(suffix) = field.strip_prefix(base_name) else {
                continue;
            };
            if !suffix
                .chars()
                .next()
                .is_some_and(|c| c.is_ascii_uppercase())
            {
                continue;
            }
            if matches.iter().any(|(n, _)| n == field) {
                continue;
            }
            if let Some(value) = obj.get(*field) {
                let converted = convert_fhir_field_to_fhirpath_type(value, suffix);
                matches.push(((*field).to_string(), converted));
            }
        }
    }

    // Fallback for nested objects (no `resourceType`) and for the
    // version-feature-disabled case — preserves prior behavior so callers
    // working below the resource root still resolve typed variants.
    if !consulted_field_types {
        for (field_name, value) in obj {
            if matches.iter().any(|(name, _)| name == field_name) {
                continue;
            }
            if field_name.starts_with(base_name) && field_name.len() > base_name.len() {
                if let Some(c) = field_name.chars().nth(base_name.len()) {
                    if c.is_uppercase() {
                        let type_suffix = &field_name[base_name.len()..];
                        let converted_value =
                            convert_fhir_field_to_fhirpath_type(value, type_suffix);
                        matches.push((field_name.clone(), converted_value));
                    }
                }
            }
        }
    }

    // Observation/`value` policy: prefer `valueQuantity` when present. This
    // is a FHIRPath-evaluator product choice (the FHIR spec doesn't declare
    // it canonical), kept stable through the structural refactor above.
    if base_name == "value"
        && matches.len() > 1
        && obj.get("resourceType") == Some(&EvaluationResult::string("Observation".to_string()))
        && let Some(idx) = matches.iter().position(|(name, _)| name == "valueQuantity")
    {
        let item = matches.remove(idx);
        matches.insert(0, item);
    }

    matches
}

/// Converts a FHIR field value to the appropriate FHIRPath type based on the field suffix.
///
/// This function handles the conversion of FHIR string values to their appropriate
/// FHIRPath types when accessed through polymorphic paths. For example, a `valueDateTime`
/// field that contains a string like "2010-10-10" should be treated as a `DateTime`
/// type in FHIRPath expressions.
///
/// # Arguments
///
/// * `value` - The original FHIR field value
/// * `suffix` - The FHIR type suffix (e.g., "DateTime", "Date", "Time")
///
/// # Returns
///
/// An `EvaluationResult` with the appropriate FHIRPath type
fn convert_fhir_field_to_fhirpath_type(value: &EvaluationResult, suffix: &str) -> EvaluationResult {
    match value {
        EvaluationResult::String(s, _, _) => {
            match suffix {
                "DateTime" => {
                    // Convert string to DateTime if it's a valid date/datetime format
                    EvaluationResult::datetime(s.clone())
                }
                "Date" => {
                    // Convert string to Date if it's a valid date format
                    EvaluationResult::date(s.clone())
                }
                "Time" => {
                    // Convert string to Time if it's a valid time format
                    EvaluationResult::time(s.clone())
                }
                "Instant" => {
                    // Convert string to Instant type (which is a datetime with required timezone)
                    // Use DateTime with instant type info
                    EvaluationResult::DateTime(
                        s.clone(),
                        Some(helios_fhirpath_support::TypeInfoResult::new(
                            "FHIR", "instant",
                        )),
                        None,
                    )
                }
                "Code" => {
                    // Convert string to code type
                    EvaluationResult::fhir_string(s.clone(), "code")
                }
                "Id" => {
                    // Convert string to id type
                    EvaluationResult::fhir_string(s.clone(), "id")
                }
                "Uri" => {
                    // Convert string to uri type
                    EvaluationResult::fhir_string(s.clone(), "uri")
                }
                "Url" => {
                    // Convert string to url type
                    EvaluationResult::fhir_string(s.clone(), "url")
                }
                "Uuid" => {
                    // Convert string to uuid type
                    EvaluationResult::fhir_string(s.clone(), "uuid")
                }
                "Canonical" => {
                    // Convert string to canonical type
                    EvaluationResult::fhir_string(s.clone(), "canonical")
                }
                "Oid" => {
                    // Convert string to oid type
                    EvaluationResult::fhir_string(s.clone(), "oid")
                }
                "Markdown" => {
                    // Convert string to markdown type
                    EvaluationResult::fhir_string(s.clone(), "markdown")
                }
                "Base64Binary" => {
                    // Convert string to base64Binary type
                    EvaluationResult::fhir_string(s.clone(), "base64Binary")
                }
                _ => {
                    // For other types or when the conversion doesn't apply, return as-is
                    value.clone()
                }
            }
        }
        _ => {
            // For non-string values, return as-is
            value.clone()
        }
    }
}

/// Determines if a field name represents a FHIR choice element.
///
/// In FHIR, choice elements are indicated by a \[x\] suffix in the field definition,
/// such as value\[x\]. In actual JSON data, these appear with a type suffix (valueQuantity).
/// This function checks if a given field name (without the type suffix) is likely to be
/// a choice element.
///
/// # Arguments
///
/// * `field_name` - The field name to check
///
/// # Returns
///
/// `true` if the field is likely to be a choice element, `false` otherwise
///
/// # Examples
///
/// ```ignore
/// // This function is used internally by the FHIRPath evaluator
/// assert!(is_choice_element("value"));
/// assert!(is_choice_element("effective"));
/// assert!(!is_choice_element("name"));
/// ```
/// Checks if a field name represents a FHIR choice element.
///
/// This function uses context-aware detection to determine if a field
/// is a choice element. When metadata is available (through FhirResourceMetadata),
/// it uses that for accurate detection. Otherwise, it falls back to
/// conservative heuristics.
///
/// # Arguments
/// * `field_name` - The field name to check
/// * `context_metadata` - Optional slice of known choice element names for the context
///
/// # Returns
/// `true` if the field is a choice element, `false` otherwise
pub fn is_choice_element_with_context(field_name: &str, context_metadata: Option<&[&str]>) -> bool {
    // Pattern 1: Field name contains [x] - definitely a choice element
    if field_name.contains("[x]") {
        return true;
    }

    // If we have metadata, use it for accurate detection
    if let Some(choice_elements) = context_metadata {
        // Check if this field name is in the known choice elements
        if choice_elements.contains(&field_name) {
            return true;
        }

        // Also check if this looks like a typed variant of a known choice element
        // e.g., if "value" is a choice element, then "valueQuantity" is too
        for base_name in choice_elements {
            if field_name.starts_with(base_name) && field_name.len() > base_name.len() {
                // Check if the character after the base name is uppercase
                if let Some(c) = field_name.chars().nth(base_name.len()) {
                    if c.is_uppercase() {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    // Without metadata, consult the generated `FIELD_TYPES` table for the
    // default FHIR version: `field_name` is a choice base if at least one
    // field in any parent type has the form `<field_name><UppercaseLetter>...`.
    is_polymorphic_base_in_default_version(field_name)
}

/// Convenience function that calls is_choice_element_with_context without metadata.
/// This is less accurate but maintains backward compatibility.
pub fn is_choice_element(field_name: &str) -> bool {
    is_choice_element_with_context(field_name, None)
}

/// Returns true when `name` is the base of a polymorphic FHIR field in the
/// default FHIR version's generated `FIELD_TYPES` table — i.e. some declared
/// field is `<name><UppercaseLetter>...`. Lets the no-context choice-element
/// check return a useful answer for common polymorphic bases (`value`,
/// `effective`, `onset`, …) instead of the always-false fallback that
/// preceded this.
fn is_polymorphic_base_in_default_version(name: &str) -> bool {
    let table: &[(&str, &str, &str, bool)] = match helios_fhir::FhirVersion::default_enabled() {
        #[cfg(feature = "R4")]
        helios_fhir::FhirVersion::R4 => helios_fhir::r4::FIELD_TYPES,
        #[cfg(feature = "R4B")]
        helios_fhir::FhirVersion::R4B => helios_fhir::r4b::FIELD_TYPES,
        #[cfg(feature = "R5")]
        helios_fhir::FhirVersion::R5 => helios_fhir::r5::FIELD_TYPES,
        #[cfg(feature = "R6")]
        helios_fhir::FhirVersion::R6 => helios_fhir::r6::FIELD_TYPES,
        #[allow(unreachable_patterns)]
        _ => return false,
    };
    table.iter().any(|(_, f, _, _)| {
        f.strip_prefix(name)
            .and_then(|rest| rest.chars().next())
            .is_some_and(|c| c.is_ascii_uppercase())
    })
}

/// Applies a type-based operation to a value, handling polymorphic choice elements.
///
/// This function implements the 'is' and 'as' operators for FHIRPath, with special
/// handling for FHIR choice elements. It allows expressions like:
/// - Observation.value.is(Quantity) - Returns true if value is a Quantity
/// - Observation.value.as(Quantity) - Returns the value as a Quantity if possible
///
/// # Arguments
///
/// * `value` - The value to apply the type operation to
/// * `op` - The operation to perform: "is" or "as"
/// * `type_name` - The name of the type to check/convert to
/// * `namespace` - Optional namespace for the type (e.g., "System", "FHIR")
///
/// # Returns
///
/// * For "is" operations, returns a Boolean result indicating if the value matches the type
/// * For "as" operations, returns the value converted to the requested type, or Empty if not possible
///
/// # Examples
///
/// ```ignore
/// // This function is used internally by the FHIRPath evaluator
/// // to handle polymorphic type operations on FHIR choice elements
/// let result = apply_polymorphic_type_operation(value, op_type, target_type);
/// let result1 = apply_polymorphic_type_operation(&value, "is", "Quantity", None);
/// let result2 = apply_polymorphic_type_operation(&value, "as", "Quantity", None);
/// ```
pub fn apply_polymorphic_type_operation(
    value: &EvaluationResult,
    op: &str,
    type_name: &str,
    _namespace: Option<&str>,
) -> Result<EvaluationResult, EvaluationError> {
    // Handle empty values first
    if let EvaluationResult::Empty = value {
        // For Empty values, we can't perform type operations but we can do some operation-specific handling
        if op == "is" && type_name == "Empty" {
            // Empty.is(Empty) is true
            return Ok(EvaluationResult::boolean(true));
        } else if op == "is" {
            // Empty is not any other type
            return Ok(EvaluationResult::boolean(false));
        } else if op == "as" {
            // Casting Empty to any type remains Empty
            return Ok(EvaluationResult::Empty);
        }
        return Ok(EvaluationResult::Empty);
    }

    if let EvaluationResult::Collection { items, .. } = value {
        if items.len() != 1 {
            return Ok(EvaluationResult::Empty);
        }
        return apply_polymorphic_type_operation(&items[0], op, type_name, _namespace);
    }

    // Since we need to determine if the original path is a choice element
    if op == "is" || op == "as" {
        // The value being checked could be:
        // 1. Direct access already succeeded (like Observation.valueQuantity)
        // 2. Polymorphic access that needs to be checked (like Observation.value which should match valueQuantity)

        // First handle direct FHIR resource type checks
        if let EvaluationResult::Object {
            map: obj,
            type_info: _,
        } = value
        {
            // For polymorphic value checks (like value.is(Quantity))
            // We need to handle both:
            // - Direct check on a quantity-like object
            // - Check on a polymorphic property that could be a choice element

            // Special case for Quantity type when called on a value object
            if type_name == "Quantity" || type_name == "quantity" {
                // Check if this is already a Quantity by structure
                if obj.contains_key("value")
                    && (obj.contains_key("unit") || obj.contains_key("code"))
                {
                    return if op == "is" {
                        // This looks like a Quantity, so return true
                        Ok(EvaluationResult::boolean(true))
                    } else {
                        // op == "as"
                        // Return the object itself since it already has the expected Quantity structure
                        Ok(value.clone())
                    };
                }

                // Check if this object has a valueQuantity field (for parent objects)
                if obj.contains_key("valueQuantity") {
                    return if op == "is" {
                        Ok(EvaluationResult::boolean(true))
                    } else {
                        // op == "as"
                        // Return the valueQuantity field
                        if let Some(quantity) = obj.get("valueQuantity") {
                            Ok(quantity.clone())
                        } else {
                            Ok(EvaluationResult::Empty)
                        }
                    };
                }

                // Check if this resource is an Observation with a valueQuantity field
                if let Some(EvaluationResult::String(resource_type, _, _)) = obj.get("resourceType")
                {
                    if resource_type == "Observation" && obj.contains_key("valueQuantity") {
                        return if op == "is" {
                            Ok(EvaluationResult::boolean(true))
                        } else {
                            // op == "as"
                            // Return the valueQuantity field
                            if let Some(quantity) = obj.get("valueQuantity") {
                                Ok(quantity.clone())
                            } else {
                                Ok(EvaluationResult::Empty)
                            }
                        };
                    }
                }
            }

            // Check resource type - handle FHIR resource type checking generically
            if let Some(EvaluationResult::String(resource_type, _, _)) = obj.get("resourceType") {
                // For direct resource type checks (like Patient.is(Patient)), use case-insensitive comparison
                if resource_type.to_lowercase() == type_name.to_lowercase() {
                    return if op == "is" {
                        Ok(EvaluationResult::boolean(true))
                    } else {
                        // op == "as"
                        Ok(value.clone())
                    };
                }

                // Handle parent types like DomainResource and Resource
                if type_name.to_lowercase() == "domainresource"
                    && crate::resource_type::is_fhir_domain_resource(resource_type)
                {
                    return if op == "is" {
                        Ok(EvaluationResult::boolean(true))
                    } else {
                        // op == "as"
                        Ok(value.clone())
                    };
                }

                // All FHIR resources are Resource types
                if type_name.to_lowercase() == "resource" {
                    return if op == "is" {
                        Ok(EvaluationResult::boolean(true))
                    } else {
                        // op == "as"
                        Ok(value.clone())
                    };
                }
            }
        }

        // For proper type checking, delegate to resource_type module which has type hierarchy support
        match op {
            "is" => {
                // First try using the resource_type module for proper type checking with hierarchy support
                if let Some(ns) = _namespace {
                    let type_spec = crate::parser::TypeSpecifier::QualifiedIdentifier(
                        ns.to_string(),
                        Some(type_name.to_string()),
                    );
                    // Create a minimal context for type checking
                    let context = crate::EvaluationContext::new_empty_with_default_version();
                    if let Ok(result) =
                        crate::resource_type::is_of_type_with_context(value, &type_spec, &context)
                    {
                        return Ok(EvaluationResult::boolean(result));
                    }
                } else {
                    let type_spec = crate::parser::TypeSpecifier::QualifiedIdentifier(
                        type_name.to_string(),
                        None,
                    );
                    let context = crate::EvaluationContext::new_empty_with_default_version();
                    if let Ok(result) =
                        crate::resource_type::is_of_type_with_context(value, &type_spec, &context)
                    {
                        return Ok(EvaluationResult::boolean(result));
                    }
                }

                // Fall back to original implementation if resource_type didn't handle it
                match value {
                    EvaluationResult::Object {
                        map: obj,
                        type_info: _,
                    } => {
                        // First check for FHIR resource type matching (for objects with type_info)
                        if let Some(EvaluationResult::String(resource_type, _, _)) =
                            obj.get("resourceType")
                        {
                            // For direct resource type checks (like Patient.is(Patient) or Patient.is(FHIR.Patient))
                            if resource_type.to_lowercase() == type_name.to_lowercase() {
                                return Ok(EvaluationResult::boolean(true));
                            }

                            // Handle parent types like DomainResource and Resource
                            if type_name.to_lowercase() == "domainresource"
                                && crate::resource_type::is_fhir_domain_resource(resource_type)
                            {
                                return Ok(EvaluationResult::boolean(true));
                            }

                            // All FHIR resources are Resource types
                            if type_name.to_lowercase() == "resource" {
                                return Ok(EvaluationResult::boolean(true));
                            }
                        }

                        // Continue with other type checking logic...
                        // Check for boolean-like properties in FHIR resources without hardcoding specific fields
                        if type_name.to_lowercase() == "boolean" {
                            // Check for properties with names often used for boolean flags in FHIR
                            for key in obj.keys() {
                                // Skip resourceType
                                if key == "resourceType" {
                                    continue;
                                }

                                // Properties that typically contain booleans have names relating to state/flags
                                if key.to_lowercase().contains("active")
                                    || key.to_lowercase().contains("flag")
                                    || key.to_lowercase().contains("enabled")
                                    || key.to_lowercase().contains("status")
                                    || key.to_lowercase().contains("is")
                                {
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }

                            // If this object contains a boolean field (other than resourceType), it's likely a boolean property
                            for (key, value) in obj.iter() {
                                if key != "resourceType"
                                    && matches!(value, EvaluationResult::Boolean(_, _, _))
                                {
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }

                            // If this is a small object that represents a single property
                            // (like a FHIR boolean property), check if it has the right structure
                            if obj.len() < 5 && !obj.contains_key("resourceType") {
                                // Look for clues that this is a boolean property
                                // Often FHIR properties are wrapped in objects with few fields
                                if obj.contains_key("id") || obj.contains_key("extension") {
                                    return Ok(EvaluationResult::boolean(true));
                                }

                                // Special case for the 'active' property itself
                                if obj.keys().len() <= 2 {
                                    // If it's a very small object, it's likely a primitive boolean property
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }
                        }

                        // Check for date-like properties in any FHIR resource without hardcoding specific fields
                        if type_name.to_lowercase() == "date" || type_name == "Date" {
                            // Look for any property that could be a date
                            for (key, val) in obj.iter() {
                                // Skip resourceType
                                if key == "resourceType" {
                                    continue;
                                }

                                // Check value type - date values could be stored as strings or as Date type
                                match val {
                                    EvaluationResult::Date(_, None, None) => {
                                        return Ok(EvaluationResult::boolean(true));
                                    }
                                    EvaluationResult::String(s, _, _) => {
                                        // Check if string looks like a date (YYYY-MM-DD)
                                        if s.len() >= 10
                                            && s.chars().nth(4) == Some('-')
                                            && s.chars().nth(7) == Some('-')
                                        {
                                            return Ok(EvaluationResult::boolean(true));
                                        }
                                    }
                                    _ => {}
                                }

                                // Date-related property names often contain "date" or "time"
                                if key.to_lowercase().contains("date")
                                    || key.to_lowercase().contains("time")
                                    || key.to_lowercase().contains("birth")
                                {
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }
                        }

                        // First try direct polymorphic field matching
                        for key in obj.keys() {
                            if key.ends_with(type_name) && key.len() > type_name.len() {
                                let base_name = &key[0..(key.len() - type_name.len())];
                                if is_choice_element(base_name) {
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }
                        }

                        // Check for specific cases like "value" -> valueQuantity for Observation.value.is(Quantity)
                        if obj.contains_key("value") && type_name == "Quantity" {
                            // Check if the value field looks like a Quantity
                            if let Some(EvaluationResult::Object { map: value_obj, .. }) =
                                obj.get("value")
                            {
                                if value_obj.contains_key("value") && value_obj.contains_key("unit")
                                {
                                    return Ok(EvaluationResult::boolean(true));
                                }
                            }

                            // Also check for valueQuantity
                            if obj.contains_key("valueQuantity") {
                                return Ok(EvaluationResult::boolean(true));
                            }
                        }

                        // Try matching the value's type directly
                        // For native types mapped to FHIR primitive types
                        if let Some(EvaluationResult::String(value_type, _, _)) = obj.get("type") {
                            if value_type == type_name {
                                return Ok(EvaluationResult::boolean(true));
                            }
                        }

                        // No match found
                        Ok(EvaluationResult::boolean(false))
                    }
                    // Match native types to FHIRPath types
                    EvaluationResult::Boolean(_, _, _) => {
                        // Check for qualifiers like "System.Boolean" and "FHIR.boolean"
                        let is_boolean_type = type_name == "Boolean"
                            || type_name == "boolean"
                            || type_name.ends_with(".Boolean")
                            || type_name.ends_with(".boolean");
                        Ok(EvaluationResult::boolean(is_boolean_type))
                    }
                    EvaluationResult::Integer(_, _, _) => {
                        // Check for qualifiers like "System.Integer" and "FHIR.integer"
                        let is_integer_type = type_name == "Integer"
                            || type_name == "integer"
                            || type_name.ends_with(".Integer")
                            || type_name.ends_with(".integer");
                        Ok(EvaluationResult::boolean(is_integer_type))
                    }
                    EvaluationResult::Decimal(_, _, _) => {
                        // Check for qualifiers like "System.Decimal" and "FHIR.decimal"
                        let is_decimal_type = type_name == "Decimal"
                            || type_name == "decimal"
                            || type_name.ends_with(".Decimal")
                            || type_name.ends_with(".decimal");
                        Ok(EvaluationResult::boolean(is_decimal_type))
                    }
                    EvaluationResult::String(_, _, _) => {
                        // Check for qualifiers like "System.String" and "FHIR.string"
                        let is_string_type = type_name == "String"
                            || type_name == "string"
                            || type_name.ends_with(".String")
                            || type_name.ends_with(".string");
                        Ok(EvaluationResult::boolean(is_string_type))
                    }
                    EvaluationResult::Date(_, _, _) => {
                        // Check for qualifiers like "System.Date" and "FHIR.date"
                        let is_date_type = type_name == "Date"
                            || type_name == "date"
                            || type_name.ends_with(".Date")
                            || type_name.ends_with(".date");
                        Ok(EvaluationResult::boolean(is_date_type))
                    }
                    EvaluationResult::DateTime(_, _, _) => {
                        // Check for qualifiers like "System.DateTime" and "FHIR.dateTime"
                        let is_datetime_type = type_name == "DateTime"
                            || type_name == "dateTime"
                            || type_name.ends_with(".DateTime")
                            || type_name.ends_with(".dateTime");
                        Ok(EvaluationResult::boolean(is_datetime_type))
                    }
                    EvaluationResult::Time(_, _, _) => {
                        // Check for qualifiers like "System.Time" and "FHIR.time"
                        let is_time_type = type_name == "Time"
                            || type_name == "time"
                            || type_name.ends_with(".Time")
                            || type_name.ends_with(".time");
                        Ok(EvaluationResult::boolean(is_time_type))
                    }
                    EvaluationResult::Quantity(_, _, _, _) => {
                        // Check for qualifiers like "System.Quantity" and "FHIR.Quantity"
                        let is_quantity_type =
                            type_name == "Quantity" || type_name.ends_with(".Quantity");
                        Ok(EvaluationResult::boolean(is_quantity_type))
                    }
                    // These cases should never happen due to earlier checks
                    EvaluationResult::Empty => Ok(EvaluationResult::boolean(false)),
                    EvaluationResult::Collection { .. } => Ok(EvaluationResult::boolean(false)),
                    #[cfg(not(any(feature = "R4", feature = "R4B")))]
                    EvaluationResult::Integer64(_, _, _) => {
                        // Check for qualifiers like "System.Integer64" and "FHIR.integer64"
                        let is_integer64_type = type_name == "Integer64"
                            || type_name == "integer64"
                            || type_name.ends_with(".Integer64")
                            || type_name.ends_with(".integer64");
                        Ok(EvaluationResult::boolean(is_integer64_type))
                    }
                    #[cfg(any(feature = "R4", feature = "R4B"))]
                    EvaluationResult::Integer64(_, _, _) => {
                        // In R4 and R4B, Integer64 should be treated as Integer
                        let is_integer_type = type_name == "Integer"
                            || type_name == "integer"
                            || type_name.ends_with(".Integer")
                            || type_name.ends_with(".integer");
                        Ok(EvaluationResult::boolean(is_integer_type))
                    }
                }
            }
            "as" => {
                // The 'as' operator returns the input value if it 'is' of the specified type,
                // otherwise it returns Empty.
                let is_type_result =
                    apply_polymorphic_type_operation(value, "is", type_name, _namespace)?;
                match is_type_result {
                    EvaluationResult::Boolean(true, _, _) => Ok(value.clone()),
                    EvaluationResult::Boolean(false, _, _) => Ok(EvaluationResult::Empty),
                    EvaluationResult::Empty => Ok(EvaluationResult::Empty), // 'is' on Empty can be Empty
                    _ => Err(EvaluationError::TypeError(format!(
                        "'is' operation returned non-Boolean: {:?}",
                        is_type_result
                    ))),
                }
            }
            _ => Err(EvaluationError::TypeError(format!(
                "Unsupported polymorphic type operation: {}",
                op
            ))),
        }
    } else {
        // Unsupported operation
        Err(EvaluationError::TypeError(format!(
            "Unsupported polymorphic type operation: {}",
            op
        )))
    }
}

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

    // Helper function to create a FHIR Observation with a valueQuantity
    fn create_observation_with_quantity() -> HashMap<String, EvaluationResult> {
        let mut obs = HashMap::new();

        // Add resourceType
        obs.insert(
            "resourceType".to_string(),
            EvaluationResult::string("Observation".to_string()),
        );

        // Add id
        obs.insert(
            "id".to_string(),
            EvaluationResult::string("123".to_string()),
        );

        // Add valueQuantity
        let mut quantity = HashMap::new();
        quantity.insert(
            "value".to_string(),
            EvaluationResult::decimal(rust_decimal::Decimal::from(185)),
        );
        quantity.insert(
            "unit".to_string(),
            EvaluationResult::string("lbs".to_string()),
        );
        quantity.insert(
            "system".to_string(),
            EvaluationResult::string("http://unitsofmeasure.org".to_string()),
        );
        quantity.insert(
            "code".to_string(),
            EvaluationResult::string("lb_av".to_string()),
        );

        obs.insert(
            "valueQuantity".to_string(),
            EvaluationResult::Object {
                map: quantity,
                type_info: None,
            },
        );

        obs
    }

    #[test]
    fn test_access_polymorphic_element() {
        let obs = create_observation_with_quantity();

        // Test accessing a polymorphic element
        let value = access_polymorphic_element(&obs, "value").unwrap();

        // Verify that it correctly finds valueQuantity
        if let EvaluationResult::Object {
            map: quantity,
            type_info: _,
        } = &value
        {
            assert_eq!(
                quantity.get("unit").unwrap(),
                &EvaluationResult::string("lbs".to_string())
            );
        } else {
            panic!("Expected Object result, got {:?}", value);
        }
    }

    #[test]
    fn test_is_type_operation() {
        let obs = create_observation_with_quantity();
        let value_quantity = obs.get("valueQuantity").unwrap().clone();

        // Test is(Quantity) on valueQuantity object directly
        // Since we enhanced our polymorphic_access.rs for choice elements,
        // we'll now recognize a valueQuantity object as a Quantity type
        let result =
            apply_polymorphic_type_operation(&value_quantity, "is", "Quantity", None).unwrap();
        assert_eq!(result, EvaluationResult::boolean(true)); // Now tests for true

        // Test is(String) on valueQuantity object directly
        let result =
            apply_polymorphic_type_operation(&value_quantity, "is", "String", None).unwrap();
        assert_eq!(result, EvaluationResult::boolean(false));

        // Test is() on the Observation object itself
        let obj = EvaluationResult::Object {
            map: obs,
            type_info: None,
        };
        let result = apply_polymorphic_type_operation(&obj, "is", "Observation", None).unwrap();
        assert_eq!(result, EvaluationResult::boolean(true));
    }

    #[test]
    fn test_as_type_operation() {
        let obs = create_observation_with_quantity();

        // First, let's test as(Quantity) on the valueQuantity object directly
        let value_quantity = obs.get("valueQuantity").unwrap().clone();
        let result =
            apply_polymorphic_type_operation(&value_quantity, "is", "Quantity", None).unwrap();
        // The valueQuantity looks like a Quantity type now, so is(Quantity) should be true
        assert_eq!(result, EvaluationResult::boolean(true)); // Updated to true

        // Now since is(Quantity) is true, as(Quantity) should return the original value
        let result =
            apply_polymorphic_type_operation(&value_quantity, "as", "Quantity", None).unwrap();
        assert_eq!(result, value_quantity);

        // Test with an Observation object
        let obj = EvaluationResult::Object {
            map: obs.clone(),
            type_info: None,
        };

        // Testing valueQuantity field indirectly via Quantity
        // In our updated implementation, Observation.is(Quantity) should return true if it contains a valueQuantity
        let result = apply_polymorphic_type_operation(&obj, "is", "Quantity", None).unwrap();
        assert_eq!(result, EvaluationResult::boolean(true)); // Should return true because it contains valueQuantity

        // Test for a wrong type
        let result = apply_polymorphic_type_operation(&obj, "is", "NonExistentType", None).unwrap();
        assert_eq!(result, EvaluationResult::boolean(false));
    }
}