// Aliased members, content property designations, type flags and name-scope access (design D3,
// D15, F18, 2F3).

///|
/// The XML namespace, `http://www.w3.org/XML/1998/namespace`, of `xml:lang`, `xml:space` and
/// `xml:base`. The prefix `xml` is bound to it in every document.
pub let xml_namespace : String = "http://www.w3.org/XML/1998/namespace"

///|
/// A directive that a type can alias to one of its members, as System.Xaml
/// `RuntimeNamePropertyAttribute` (`x:Name`), `DictionaryKeyPropertyAttribute` (`x:Key`),
/// `UidPropertyAttribute` (`x:Uid`) and `XmlLangPropertyAttribute` (`xml:lang`) do.
#derive(Eq, Hash, Debug)
pub(all) enum AliasedDirective {
  Name
  Key
  Uid
  Lang
}

///|
/// The member name of the directive, as start member nodes carry it.
pub fn AliasedDirective::member_name(
  self : AliasedDirective,
) -> @nodes.XamlMemberName {
  match self {
    Name =>
      Directive(
        namespace_uri=@nodes.xaml_language_namespace,
        name=directive_name,
      )
    Key =>
      Directive(
        namespace_uri=@nodes.xaml_language_namespace,
        name=directive_key,
      )
    Uid =>
      Directive(
        namespace_uri=@nodes.xaml_language_namespace,
        name=directive_uid,
      )
    Lang => Directive(namespace_uri=xml_namespace, name="lang")
  }
}

///|
/// The aliased directive that the directive `name` of `namespace_uri` is, under either XAML
/// language URI, or `None` for any other directive.
pub fn AliasedDirective::from_directive(
  namespace_uri : String,
  name : String,
) -> AliasedDirective? {
  match (language_alias(namespace_uri), name) {
    (ns, "Name") if ns == @nodes.xaml_language_namespace => Some(Name)
    (ns, "Key") if ns == @nodes.xaml_language_namespace => Some(Key)
    (ns, "Uid") if ns == @nodes.xaml_language_namespace => Some(Uid)
    (ns, "lang") if ns == xml_namespace => Some(Lang)
    _ => None
  }
}

///|
/// A designation that a type inherits from its base type, sets or clears (F18).
priv enum Designation[T] {
  Inherit
  Set(T)
  Clear
}

///|
/// The value of a designation along a base chain, most derived first: the nearest that sets or
/// clears it, with the index of the link that sets it.
fn[T] designated(
  chain : Array[XamlTypeDefinition],
  pick : (XamlTypeDefinition) -> Designation[T],
) -> (T, Int32)? {
  for i, definition in chain {
    match pick(definition) {
      Inherit => continue
      Set(value) => return Some((value, i))
      Clear => return None
    }
  }
  None
}

///|
/// Looks up a name in the instance name scope of an object (.NET `INameScope.FindName`).
pub type NameScopeLookup = (@nodes.XamlObject, String) -> @nodes.XamlValue? raise

///|
/// Registers a value under a name in the instance name scope of an object (.NET
/// `INameScope.RegisterName`). It raises when the scope does not accept the name.
pub type NameScopeRegister = (@nodes.XamlObject, String, @nodes.XamlValue) -> Unit raise

///|
/// How a type gives access to its instance name scope (design D15).
pub(all) enum NameScopeAccess {
  /// Lookup and register operations on the instance, as a type that implements .NET `INameScope`.
  Operations(lookup~ : NameScopeLookup, register~ : NameScopeRegister)
  /// The instance or attached member that holds the scope of a root object, as .NET
  /// `NameScopePropertyAttribute`.
  Property(XamlMember)
}

///|
/// The name-scope access of a built type.
priv enum NameScopeSource {
  Operations(NameScopeLookup, NameScopeRegister)
  /// An instance member of the type, looked up when asked, so closed generic types substitute it.
  Member(String)
  Attached(XamlMember)
}

///|
/// Register the member `member_name` as the alias of `directive`, as the .NET attributes that `AliasedDirective`
/// lists do. Derived types inherit it unless they set or clear their own.
pub fn XamlTypeDefinition::aliased_member(
  self : XamlTypeDefinition,
  directive : AliasedDirective,
  member_name : String,
) -> XamlTypeDefinition {
  self.aliases.push((directive, Set(member_name)))
  self
}

///|
/// Clear an inherited alias of `directive`. The member stays available by name.
pub fn XamlTypeDefinition::clear_alias(
  self : XamlTypeDefinition,
  directive : AliasedDirective,
) -> XamlTypeDefinition {
  self.aliases.push((directive, Clear))
  self
}

///|
/// Clear an inherited content property. The member stays available by name.
pub fn XamlTypeDefinition::clear_content_property(
  self : XamlTypeDefinition,
) -> XamlTypeDefinition {
  self.content_property = Clear
  self
}

///|
/// Make the type a white-space significant collection, as .NET
/// `WhitespaceSignificantCollectionAttribute`. Derived types inherit the flag.
pub fn XamlTypeDefinition::whitespace_significant_collection(
  self : XamlTypeDefinition,
) -> XamlTypeDefinition {
  self.whitespace_significant = true
  self
}

///|
/// Trim white space around objects of the type in collections, as .NET
/// `TrimSurroundingWhitespaceAttribute`. Derived types inherit the flag.
pub fn XamlTypeDefinition::trim_surrounding_whitespace(
  self : XamlTypeDefinition,
) -> XamlTypeDefinition {
  self.trim_whitespace = true
  self
}

///|
/// Set whether an object of the type is assigned to its parent before its members are set, as
/// .NET `UsableDuringInitializationAttribute`. Omitted, the type inherits its base type's setting;
/// at the root of the chain it is false. True on a markup extension fails `build()`.
pub fn XamlTypeDefinition::usable_during_initialization(
  self : XamlTypeDefinition,
  usable : Bool,
) -> XamlTypeDefinition {
  self.usable_during_initialization = Some(usable)
  self
}

///|
/// Register the operations of the instance name scope of the type (design D15). They are inherited
/// and, registered with a base type's key, run on a derived type's objects through its base views.
pub fn XamlTypeDefinition::name_scope(
  self : XamlTypeDefinition,
  lookup~ : NameScopeLookup,
  register~ : NameScopeRegister,
) -> XamlTypeDefinition {
  self.name_scope = Some((lookup, register))
  self
}

///|
/// Name the member that holds the name scope of an object of the type at the root of a document, as .NET
/// `NameScopePropertyAttribute`: a member of the type, or with `attached_owner` the attached member
/// `name` of that type (design D15). Scope operations, also inherited ones, take precedence over it.
pub fn XamlTypeDefinition::name_scope_property(
  self : XamlTypeDefinition,
  name : String,
  attached_owner? : @nodes.XamlTypeName,
) -> XamlTypeDefinition {
  self.name_scope_property = Some((name, attached_owner))
  self
}

///|
/// The member aliased to `directive`, declared or inherited, unless the type or a nearer base type
/// clears it.
pub fn XamlType::aliased_member(
  self : XamlType,
  directive : AliasedDirective,
) -> XamlMember? {
  for entry in self.aliases {
    if entry.0 == directive {
      return self.members.get(entry.1)
    }
  }
  None
}

///|
/// Whether the type or a base type is a white-space significant collection.
pub fn XamlType::is_whitespace_significant_collection(self : XamlType) -> Bool {
  self.whitespace_significant
}

///|
/// Whether the type or a base type trims the white space around its objects in collections.
pub fn XamlType::trims_surrounding_whitespace(self : XamlType) -> Bool {
  self.trim_whitespace
}

///|
/// Whether the nearest type of the chain that sets it makes the type usable during initialization.
pub fn XamlType::is_usable_during_initialization(self : XamlType) -> Bool {
  self.usable_during_initialization
}

///|
/// Whether objects of the type are name scopes: the type or a base type registers name-scope operations, as
/// System.Xaml `XamlType.IsNameScope`. A name-scope property alone does not make them one.
pub fn XamlType::is_name_scope(self : XamlType) -> Bool {
  self.name_scope is Some(Operations(_))
}

///|
/// How objects of the type give access to their name scope, adapted to the type's key: the nearest operations
/// of the base chain, else the nearest name-scope property.
pub fn XamlType::name_scope(self : XamlType) -> NameScopeAccess? {
  match self.name_scope {
    Some(Operations(lookup, register)) => Some(Operations(lookup~, register~))
    Some(Member(name)) => self.members.get(name).map(m => Property(m))
    Some(Attached(m)) => Some(Property(m))
    None => None
  }
}

///|
/// Make the type abstract, as `x:Member` is: documents cannot create objects of it.
pub fn XamlTypeDefinition::abstract_type(
  self : XamlTypeDefinition,
) -> XamlTypeDefinition {
  self.is_abstract = true
  self
}

///|
/// Whether the type is abstract.
pub fn XamlType::is_abstract(self : XamlType) -> Bool {
  self.is_abstract
}