use super::{coded_lens, plain};
use crate::engine::fhir::FhirKind;
use crate::engine::lens::Lens;
use crate::engine::lens::ordinal::OrdinalLens;
use crate::engine::lens::term_mapping::Withheld;
use crate::engine::lens::text::TextLens;
use crate::engine::rm::RmValue;
use fhir_types::r4::codeable_concept::CodeableConcept;
use fhir_types::r4::coding::Coding;
use openehr_rm::v1_2::data_types::text::code_phrase::CodePhrase;
use openehr_rm::v1_2::data_types::text::dv_coded_text::DvCodedText;
use openehr_rm::v1_2::data_types::text::dv_text::DvTextData;
#[must_use]
pub fn withheld(source: &RmValue, kind: FhirKind, binding: Option<&str>) -> Vec<Withheld> {
match (source, kind) {
(RmValue::Text(text), FhirKind::String) => {
Lens::<DvTextData, fhir_types::r4::primitives::String>::withheld(&TextLens, text)
}
(RmValue::CodedText(text), FhirKind::String) => {
Lens::<DvTextData, fhir_types::r4::primitives::String>::withheld(
&TextLens,
&plain(text.as_ref()),
)
}
(RmValue::CodedText(text), FhirKind::Coding) => {
Lens::<DvCodedText, Coding>::withheld(&coded_lens(binding), text.as_ref())
}
(RmValue::Text(text), FhirKind::Coding) => {
Lens::<DvTextData, Coding>::withheld(&TextLens, text)
}
(RmValue::Text(text), FhirKind::CodeableConcept) => {
Lens::<DvTextData, CodeableConcept>::withheld(&TextLens, text)
}
(RmValue::CodedText(text), FhirKind::CodeableConcept) => {
Lens::<DvCodedText, CodeableConcept>::withheld(&coded_lens(binding), text.as_ref())
}
(RmValue::Ordinal(ordinal), FhirKind::CodeableConcept) => {
OrdinalLens::new(coded_lens(binding), &[]).withheld(ordinal.as_ref())
}
(_source, _kind) => Vec::new(),
}
}
#[must_use]
pub fn uncarried_code(source: &RmValue, kind: FhirKind) -> Option<&CodePhrase> {
match (source, kind) {
(RmValue::CodedText(text), FhirKind::String) => Some(&text.defining_code),
(_source, _kind) => None,
}
}