///|
#cfg(target="js")
#external
type Event

///|
#cfg(target="js")
fn[A, B] js_identity(a : A) -> B {
  @js.Value::cast_from(a).cast()
}

///|
#cfg(target="js")
pub trait IsEvent: @js.Cast {
  target(Self) -> EventTarget = _
  current_target(Self) -> @js.Nullable[EventTarget] = _
  prevent_default(Self) -> Unit = _
  stop_propagation(Self) -> Unit = _
  as_event(Self) -> Event = _
  to_ui_event(Self) -> UIEvent? = _
  to_clipboard_event(Self) -> ClipboardEvent? = _
  to_mouse_event(Self) -> MouseEvent? = _
  to_input_event(Self) -> InputEvent? = _
  to_focus_event(Self) -> FocusEvent? = _
  to_keyboard_event(Self) -> KeyboardEvent? = _
  to_animation_event(Self) -> AnimationEvent? = _
  to_before_unload_event(Self) -> BeforeUnloadEvent? = _
  to_blob_event(Self) -> BlobEvent? = _
  to_close_event(Self) -> CloseEvent? = _
  to_composition_event(Self) -> CompositionEvent? = _
  to_custom_event(Self) -> CustomEvent? = _
  to_drag_event(Self) -> DragEvent? = _
  to_message_event(Self) -> MessageEvent? = _
  to_wheel_event(Self) -> WheelEvent? = _
}

///|
#cfg(target="js")
pub impl IsEvent for Event

///|
#cfg(target="js")
pub impl @js.Cast for Event with into(value) {
  value |> ffi_to_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
pub impl @js.Cast for Event with from(value) {
  value |> js_identity
}

///|
#cfg(target="js")
impl IsEvent with target(s) {
  s |> js_identity |> ffi_event_target
}

///|
#cfg(target="js")
impl IsEvent with current_target(s) {
  s |> js_identity |> ffi_event_current_target
}

///|
#cfg(target="js")
impl IsEvent with prevent_default(s) {
  s.as_event() |> ffi_event_prevent_default
}

///|
#cfg(target="js")
impl IsEvent with stop_propagation(s) {
  s.as_event() |> ffi_event_stop_propagation
}

///|
#cfg(target="js")
impl IsEvent with as_event(s) {
  js_identity(s)
}

///|
#cfg(target="js")
impl IsEvent with to_ui_event(s) {
  s |> js_identity |> ffi_to_ui_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_clipboard_event(s) {
  s |> js_identity |> ffi_to_clipboard_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_mouse_event(s) {
  s |> js_identity |> ffi_to_mouse_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_input_event(s) {
  s |> js_identity |> ffi_to_input_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_focus_event(s) {
  s |> js_identity |> ffi_to_focus_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_keyboard_event(s) {
  s |> js_identity |> ffi_to_keyboard_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_animation_event(s) {
  s |> js_identity |> ffi_to_animation_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_before_unload_event(s) {
  s |> js_identity |> ffi_to_before_unload_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_blob_event(s) {
  s |> js_identity |> ffi_to_blob_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_close_event(s) {
  s |> js_identity |> ffi_to_close_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_composition_event(s) {
  s |> js_identity |> ffi_to_composition_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_custom_event(s) {
  s |> js_identity |> ffi_to_custom_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_drag_event(s) {
  s |> js_identity |> ffi_to_drag_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_message_event(s) {
  s |> js_identity |> ffi_to_message_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
impl IsEvent with to_wheel_event(s) {
  s |> js_identity |> ffi_to_wheel_event |> y => { y.to_option() }
}

///|
#cfg(target="js")
extern "js" fn ffi_event_target(x : @js.Value) -> EventTarget = "(self) => self.target"

///|
#cfg(target="js")
extern "js" fn ffi_event_current_target(
  x : @js.Value,
) -> @js.Nullable[EventTarget] = "(self) => self.currentTarget"

///|
#cfg(target="js")
extern "js" fn ffi_to_ui_event(x : @js.Value) -> @js.Nullable[UIEvent] =
  #| (x) => x instanceof UIEvent ? x : null

///|
#cfg(target="js")
extern "js" fn ffi_to_clipboard_event(
  x : @js.Value,
) -> @js.Nullable[ClipboardEvent] =
  #| (x) => x instanceof ClipboardEvent ? x : null

///|
#cfg(target="js")
extern "js" fn ffi_event_prevent_default(x : Event) = "(self) => self.preventDefault()"

///|
#cfg(target="js")
extern "js" fn ffi_event_stop_propagation(x : Event) = "(self) => self.stopPropagation()"

///|
#cfg(target="js")
extern "js" fn ffi_to_mouse_event(x : @js.Value) -> @js.Nullable[MouseEvent] =
  #| (e) => e instanceof MouseEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_input_event(x : @js.Value) -> @js.Nullable[InputEvent] =
  #| (e) => e instanceof InputEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_focus_event(x : @js.Value) -> @js.Nullable[FocusEvent] =
  #| (e) => e instanceof FocusEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_keyboard_event(
  x : @js.Value,
) -> @js.Nullable[KeyboardEvent] =
  #| (e) => e instanceof KeyboardEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_animation_event(
  x : @js.Value,
) -> @js.Nullable[AnimationEvent] =
  #| (e) => e instanceof AnimationEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_before_unload_event(
  x : @js.Value,
) -> @js.Nullable[BeforeUnloadEvent] =
  #| (e) => e instanceof BeforeUnloadEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_blob_event(x : @js.Value) -> @js.Nullable[BlobEvent] =
  #| (e) => e instanceof BlobEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_close_event(x : @js.Value) -> @js.Nullable[CloseEvent] =
  #| (e) => e instanceof CloseEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_composition_event(
  x : @js.Value,
) -> @js.Nullable[CompositionEvent] =
  #| (e) => e instanceof CompositionEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_custom_event(x : @js.Value) -> @js.Nullable[CustomEvent] =
  #| (e) => e instanceof CustomEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_drag_event(x : @js.Value) -> @js.Nullable[DragEvent] =
  #| (e) => e instanceof DragEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_message_event(
  x : @js.Value,
) -> @js.Nullable[MessageEvent] =
  #| (e) => e instanceof MessageEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_wheel_event(x : @js.Value) -> @js.Nullable[WheelEvent] =
  #| (e) => e instanceof WheelEvent ? e : null

///|
#cfg(target="js")
extern "js" fn ffi_to_event(x : @js.Value) -> @js.Nullable[Event] =
  #| (e) => e instanceof Event ? e : null