///|
/// A way from a value to another one, through properties and indexers, such as `Child.(Grid.Row)` or `Items[0].Name`.
///
/// `parse` makes one. Two paths are equal when they have the same registry and equal segments, also when they were written
/// differently.
pub struct PropertyPath {
  priv registry : @dependency.PropertyRegistry
  priv types : TypeTable
  priv segments : ReadOnlyArray[PathSegment]
  /// Where each segment is in the text that was parsed.
  priv ranges : ReadOnlyArray[(Int32, Int32)]
}

///|
/// A step of a path. `written` is a type as the path names it, which does not matter for the equality.
pub enum PathSegment {
  /// The property with this name of whatever value the path has reached.
  Property(String)
  /// A member of an owner type, such as `(Grid.Row)`.
  Qualified(PathMember, written~ : String)
  /// The arguments for the indexer of the value that the path has reached.
  Indexer(ReadOnlyArray[PathArgument])
}

///|
/// What a segment such as `(Grid.Row)` names.
#derive(Eq)
pub enum PathMember {
  Dependency(@dependency.PropertyInfo)
  /// A property that `register_object_type` gave to the type.
  Registered(@dependency.OwnerType, String)
}

///|
/// The type on which the member was registered, not one that `add_owner` added or that derives from it.
pub fn PathMember::owner_type(self : PathMember) -> @dependency.OwnerType {
  match self {
    Dependency(property) => property.owner_type()
    Registered(owner, _) => owner
  }
}

///|
pub fn PathMember::name(self : PathMember) -> String {
  match self {
    Dependency(property) => property.name()
    Registered(_, name) => name
  }
}

///|
/// An argument of an indexer.
pub enum PathArgument {
  /// Text that the indexer converts.
  Text(String)
  /// Text with a type, such as `(sys:Int32)42`, as a value of that type. `type_name` is the name of the `ArgumentType`.
  Typed(
    written~ : String,
    type_name~ : String,
    text~ : String,
    value~ : @dependency.AnyValue,
  )
}

///|
pub impl Eq for PathSegment with fn equal(self, other) {
  match (self, other) {
    (Property(a), Property(b)) => a == b
    (Qualified(a, ..), Qualified(b, ..)) => a == b
    (Indexer(a), Indexer(b)) => a == b
    _ => false
  }
}

///|
/// Typed arguments are equal when the names of their types and their texts are, because an `AnyValue` has no equality.
pub impl Eq for PathArgument with fn equal(self, other) {
  match (self, other) {
    (Text(a), Text(b)) => a == b
    (Typed(type_name~, text~, ..), Typed(type_name=other_name, text=other, ..)) =>
      type_name == other_name && text == other
    _ => false
  }
}

///|
pub impl Eq for PropertyPath with fn equal(self, other) {
  self.registry_id() == other.registry_id() && self.segments == other.segments
}

///|
/// The text of `print`.
pub impl Show for PropertyPath with fn output(self, logger) {
  logger.write_string(self.print())
}

///|
pub extend PropertyPath with Show::{to_string}

///|
pub impl Debug for PropertyPath with fn to_repr(self) {
  @debug.Repr::literal(self.print())
}

///|
/// The segments. A path without any, which is written as `.`, is the source itself.
pub fn PropertyPath::segments(
  self : PropertyPath,
) -> ReadOnlyArray[PathSegment] {
  self.segments
}

///|
/// The identity of the registry that the path was parsed with. The path works only with the objects of this registry.
pub fn PropertyPath::registry_id(self : PropertyPath) -> @dependency.RegistryId {
  self.registry.id()
}