Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

use pel::expression::Symbol;
use pel::runtime::{Binding as PelBinding, Context, ValueHandler};
use pel::Reference;

pub struct MergedContext<'a> {
    root: &'a dyn Context,
    current: &'a dyn Context,
}

impl<'a> MergedContext<'a> {
    pub fn new(root: &'a dyn Context, current: &'a dyn Context) -> Self {
        Self { root, current }
    }
}

impl Context for MergedContext<'_> {
    fn resolve(&self, symbol: &Symbol) -> PelBinding {
        match self.current.resolve(symbol) {
            PelBinding::Unknown => self.root.resolve(symbol),
            b => b,
        }
    }

    fn value_handler(&self, reference: Reference) -> Option<&dyn ValueHandler> {
        self.current
            .value_handler(reference)
            .or_else(|| self.root.value_handler(reference))
    }
}