// The services that converters and markup extensions get (design D14).

///|
/// The services for a value in the open member of `object`, the innermost one.
fn XamlObjectWriter::value_services(
  self : XamlObjectWriter,
  object : ObjectFrame,
) -> @schema.ServiceContext {
  self.services(object, Some(object))
}

///|
/// The services for the value of `object`, the innermost one, which is a markup extension. The texts of its
/// positional parameters have them too, as in System.Xaml.
fn XamlObjectWriter::own_services(
  self : XamlObjectWriter,
  object : ObjectFrame,
) -> @schema.ServiceContext {
  self.services(object, self.objects.get(self.objects.length() - 2))
}

///|
/// The services for a value with the namespaces and names of `object`. It goes to the open member of `receiver`,
/// or is the result. They do not change when the load goes on, and hold no objects but those in their fields.
fn XamlObjectWriter::services(
  self : XamlObjectWriter,
  object : ObjectFrame,
  receiver : ObjectFrame?,
) -> @schema.ServiceContext {
  let names = self.names(object)
  let namespaces = object.namespaces
  let target_member = match receiver {
    // System.Xaml gives no member for a directive, also when it stands for one (`ContextServices.GetTargetProperty`).
    Some({ open_member: Some({ role: Property(definition), site, .. }), .. }) if !(site.node
      is StartMember(Directive(_))) => Some(definition)
    _ => None
  }
  @schema.ServiceContext::for_load(
    self.types.context,
    target_object?=receiver.bind(ObjectFrame::handle),
    target_member?,
    root_object?=self.table.root.bind(ObjectFrame::handle),
    parents=match receiver {
      Some(receiver) => receiver.parents_inside()
      None => @list.List([])
    },
    base_uri?=self.base_uri,
    name_resolver=self.table.resolver(object.scope),
    resolve_prefix=names.namespace_of,
    prefixes=() => {
      ReadOnlyArray::from_array(
        namespaces
        .to_array()
        .map(entry => { prefix: entry.0, namespace_uri: entry.1, }),
      )
    },
    resolve_type=text => names.registered_type(text),
  )
}

///|
/// The names of the document where `object` is.
fn XamlObjectWriter::names(
  self : XamlObjectWriter,
  object : ObjectFrame,
) -> @syntax.Names {
  let namespaces = object.namespaces
  {
    types: self.types,
    local_scope: self.settings.local_scope,
    namespace_of: prefix => namespaces.get(prefix),
  }
}

///|
/// The object, once it is created. A value that is no object is absent.
fn ObjectFrame::handle(self : ObjectFrame) -> @nodes.XamlObject? {
  match self.instance {
    Some(Object(instance)) => Some(instance)
    _ => None
  }
}

///|
/// The parents of what is in this object. They are shared with its own parents, so that each object adds one. An object
/// that a getter gives has no element, and is no parent.
fn ObjectFrame::parents_inside(
  self : ObjectFrame,
) -> @list.List[@nodes.XamlObject] {
  match self.handle() {
    Some(instance) if self.retrieved_from is None =>
      self.parents.prepend(instance)
    _ => self.parents
  }
}

///|
/// Take the value of `xml:base`, an absolute URI on the root (System.Xaml `XamlObjectWriter.Logic_CheckBaseUri`).
fn XamlObjectWriter::set_base_uri(
  self : XamlObjectWriter,
  object : ObjectFrame,
  target : MemberFrame,
  value : @nodes.XamlValue,
) -> Unit raise @nodes.XamlWriteError {
  let { site, .. } = target
  if self.objects.length() > 1 {
    raise site.error(
      "xml:base of \{object.type_.name} is not on the root, the only place for it",
    )
  }
  if value is String(text) {
    let uri = Some(@schema.XamlUri::parse(text)) catch { _ => None }
    if uri is Some(uri) && uri.is_absolute() {
      self.base_uri = Some(uri)
      return
    }
  }
  raise site.error("xml:base takes an absolute URI, not \{value_text(value)}")
}