use super::{Binding, Frame, Run, openehr_path};
use crate::engine::outcome::Location;
use crate::model::ast::keyword::Direction;
use crate::resolve::program::mapping::Mapping;
use crate::resolve::program::target::FhirTarget;
use crate::resolve::program::target::OpenehrTarget;
use crate::tree::Occurrence;
use crate::tree::element::Table;
use crate::tree::locate::locate;
use fhir_types::codec::Value;
use std::collections::BTreeMap;
impl<T: Table + ?Sized> Run<'_, T> {
pub(super) fn locate(&self, mapping: &Mapping, occurrence: &Occurrence) -> Location {
self.locate_targets(mapping.fhir(), occurrence, mapping.openehr())
}
pub(super) fn locate_targets(
&self,
fhir: Option<&FhirTarget>,
occurrence: &Occurrence,
openehr: Option<&OpenehrTarget>,
) -> Location {
let mut location = Location::default();
if let Some(target) = fhir {
let document = self.document_for(target);
location = location.with_fhir(locate(document, target.resolved(), occurrence));
if let Some(reference) = reference_of(document) {
location = location.with_resource(reference);
}
}
if let Some(target) = openehr {
location = location.with_openehr(openehr_path(target));
}
location
}
pub(super) fn document_for(&self, target: &FhirTarget) -> &Value {
self.frames
.get(target.level())
.map_or(&self.fhir, |frame| &frame.document)
}
pub(super) fn condition_document(
&self,
gate: &crate::resolve::program::condition::Condition,
) -> &Value {
match *gate.target() {
crate::resolve::program::target::Target::Fhir(ref target) => self.document_for(target),
crate::resolve::program::target::Target::Openehr(_) => &self.fhir,
}
}
pub(super) fn document_for_mut(&mut self, target: &FhirTarget) -> &mut Value {
match self.frames.get_mut(target.level()) {
Some(frame) => &mut frame.document,
None => &mut self.fhir,
}
}
pub(super) fn pin_for(&self, target: &FhirTarget) -> Option<&(Vec<String>, Occurrence)> {
self.frames
.get(target.level())
.map_or(self.fhir_pin.as_ref(), |frame| frame.pin.as_ref())
}
pub(super) fn pin_at(&mut self, level: usize) -> &mut Option<(Vec<String>, Occurrence)> {
match self.frames.get_mut(level) {
Some(frame) => &mut frame.pin,
None => &mut self.fhir_pin,
}
}
pub(super) fn counters_at(&mut self, level: usize) -> &mut BTreeMap<(String, String), usize> {
match self.frames.get_mut(level) {
Some(frame) => &mut frame.counters,
None => &mut self.counters,
}
}
pub(super) fn scoped(&self, target: Option<&FhirTarget>, parent: &Binding) -> Binding {
let Some(target) = target.filter(|target| target.level() != parent.level) else {
return parent.clone();
};
self.frames.get(target.level()).map_or_else(
|| parent.clone(),
|frame| parent.keeping_openehr(frame.occurrence.clone(), target.level()),
)
}
pub(super) fn enter_frame(&mut self, document: Value, occurrence: Occurrence) -> usize {
let document = core::mem::replace(&mut self.fhir, document);
let (pin, counters) = match self.direction {
Direction::FhirToOpenehr => (self.fhir_pin.take(), BTreeMap::new()),
Direction::OpenehrToFhir => (None, core::mem::take(&mut self.counters)),
};
self.frames.push(Frame {
document,
occurrence,
pin,
counters,
});
self.frames.len()
}
#[expect(
clippy::expect_used,
reason = "every caller pairs this with the enter_frame it made in the same function, \
so a frame is always there to leave"
)]
pub(super) fn leave_frame(&mut self) -> Value {
let frame = self
.frames
.pop()
.expect("a reference should leave the frame it entered");
match self.direction {
Direction::FhirToOpenehr => self.fhir_pin = frame.pin,
Direction::OpenehrToFhir => self.counters = frame.counters,
}
core::mem::replace(&mut self.fhir, frame.document)
}
}
fn reference_of(document: &Value) -> Option<String> {
let kind = document.get("resourceType").and_then(Value::as_str)?;
let id = document.get("id").and_then(Value::as_str)?;
Some(format!("{kind}/{id}"))
}