///|
/// Where an event comes from.
pub(all) enum EventSource {
  /// A dependency object, which does not have to be an element.
  Object(@dependency.DependencyObject)
  /// Any other value.
  Value(@dependency.AnyValue)
}

///|
/// The state of the arguments that does not depend on the type of their data, for handlers of every event. All handlers of a
/// raise share it, so what one sets is there for the next one.
pub struct RoutingArgs {
  /// The event, and the data that its key has wrapped.
  priv mut event : (RoutedEventInfo, @dependency.AnyValue)?
  priv mut handled : Bool
  priv mut source : EventSource?
  priv mut original_source : EventSource?
}

///|
/// Fail when the arguments have no event.
fn RoutingArgs::need_event(
  self : RoutingArgs,
) -> RoutedEventInfo raise @dependency.DependencyError {
  guard self.event is Some((event, _)) else {
    raise InvalidEvent("the arguments have no routed event")
  }
  event
}

///|
/// The event that is raised, or `None` when the arguments have none yet.
pub fn RoutingArgs::event(self : RoutingArgs) -> RoutedEventInfo? {
  self.event.map(event => event.0)
}

///|
/// Whether a handler has dealt with the event, so that only handlers with `handled_events_too` still run.
pub fn RoutingArgs::handled(self : RoutingArgs) -> Bool {
  self.handled
}

///|
/// Say whether the event is dealt with. The arguments need an event for this.
pub fn RoutingArgs::set_handled(
  self : RoutingArgs,
  handled : Bool,
) -> Unit raise @dependency.DependencyError {
  ignore(self.need_event())
  self.handled = handled
}

///|
/// The element on which the event is raised, or what a handler has set. After a raise it is the `original_source`.
pub fn RoutingArgs::source(self : RoutingArgs) -> EventSource? {
  self.source
}

///|
/// Set the source for the handlers that follow. The first source is also the `original_source`. The arguments need an event
/// for this.
pub fn RoutingArgs::set_source(
  self : RoutingArgs,
  source : EventSource?,
) -> Unit raise @dependency.DependencyError {
  ignore(self.need_event())
  if self.original_source is None {
    self.original_source = source
  }
  self.source = source
}

///|
/// The first source that the arguments had, which nothing changes.
pub fn RoutingArgs::original_source(self : RoutingArgs) -> EventSource? {
  self.original_source
}

///|
/// What the handlers of an event with data of type `A` get. Handlers share one state, and what one sets is there for the next
/// one.
pub struct RoutedEventArgs[A] {
  data : A
  priv routing : RoutingArgs
}

///|
/// Arguments with `data` for a raise of `event`. Without `event` they need `set_event` before they can be raised. `source` is
/// the `original_source`, which else is the element on which the arguments are raised first.
pub fn[A] RoutedEventArgs::new(
  data : A,
  event? : RoutedEvent[A],
  source? : EventSource,
) -> RoutedEventArgs[A] {
  let event = event.map(event => (event.info, event.key.wrap(data)))
  {
    data,
    routing: { event, handled: false, source, original_source: source, },
  }
}

///|
/// The state without the data, for code that has events of many types.
pub fn[A] RoutedEventArgs::routing(self : RoutedEventArgs[A]) -> RoutingArgs {
  self.routing
}

///|
/// The event that is raised, or `None` when the arguments have none yet.
pub fn[A] RoutedEventArgs::event(self : RoutedEventArgs[A]) -> RoutedEventInfo? {
  self.routing.event()
}

///|
/// Use the arguments for another event, for example to raise the bubble event after its preview event. It is an error when
/// the two events do not have the same key for their data. A raise that is running goes on with the handlers of its event.
pub fn[A] RoutedEventArgs::set_event(
  self : RoutedEventArgs[A],
  event : RoutedEvent[A],
) -> Unit raise @dependency.DependencyError {
  let data = match self.routing.event {
    Some((old, data)) => {
      ignore(old.typed(event.key))
      data
    }
    None => event.key.wrap(self.data)
  }
  self.routing.event = Some((event.info, data))
}

///|
/// As `RoutingArgs::handled`.
pub fn[A] RoutedEventArgs::handled(self : RoutedEventArgs[A]) -> Bool {
  self.routing.handled()
}

///|
/// As `RoutingArgs::set_handled`.
pub fn[A] RoutedEventArgs::set_handled(
  self : RoutedEventArgs[A],
  handled : Bool,
) -> Unit raise @dependency.DependencyError {
  self.routing.set_handled(handled)
}

///|
/// As `RoutingArgs::source`.
pub fn[A] RoutedEventArgs::source(self : RoutedEventArgs[A]) -> EventSource? {
  self.routing.source()
}

///|
/// As `RoutingArgs::set_source`.
pub fn[A] RoutedEventArgs::set_source(
  self : RoutedEventArgs[A],
  source : EventSource?,
) -> Unit raise @dependency.DependencyError {
  self.routing.set_source(source)
}

///|
/// As `RoutingArgs::original_source`.
pub fn[A] RoutedEventArgs::original_source(
  self : RoutedEventArgs[A],
) -> EventSource? {
  self.routing.original_source()
}