#include <AK/String.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MouseEvent.h>
#include <LibWeb/Bindings/UIEvent.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/UIEvents/MouseEvent.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

void MouseEventConstructor::initialize(JS::Realm& realm, JS::NativeFunction& object)
{
    auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;

    object.set_prototype(&ensure_web_constructor<UIEventPrototype>(realm, "UIEvent"_fly_string));
    object.define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "MouseEvent"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<MouseEventPrototype>(realm, "MouseEvent"_fly_string), 0);
}

JS::ThrowCompletionOr<GC::Ref<JS::Object>> MouseEventConstructor::construct([[maybe_unused]] InterfaceConstructor& constructor, [[maybe_unused]] JS::FunctionObject& new_target)
{
    WebIDL::log_trace(constructor.vm(), "MouseEventConstructor::construct");
    auto& vm = constructor.vm();
    [[maybe_unused]] auto& realm = *vm.current_realm();

    // To internally create a new object implementing the interface MouseEvent:

    // 3.2. Let prototype be ? Get(newTarget, "prototype").
    auto prototype = TRY(new_target.get(vm.names.prototype));

    // 3.3. If Type(prototype) is not Object, then:
    if (!prototype.is_object()) {
        // 1. Let targetRealm be ? GetFunctionRealm(newTarget).
        auto* target_realm = TRY(JS::get_function_realm(vm, new_target));

        // 2. Set prototype to the interface prototype object for interface in targetRealm.
        VERIFY(target_realm);
        prototype = &Bindings::ensure_web_prototype<MouseEventPrototype>(*target_realm, "MouseEvent"_fly_string);
    }

    if (vm.argument_count() < 1)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "MouseEvent");

    auto arg0 = vm.argument(0);
    auto type = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<String> {
        return TRY(WebIDL::to_string(vm, arg0));
    }(); }));

    auto arg1 = vm.argument(1);
    MouseEventInit event_init_dict = MouseEventInit {};
    if (!arg1.is_undefined())
        event_init_dict = TRY(throw_dom_exception_if_needed(vm, [&] { return convert_to_idl_value_for_mouse_event_init(vm, arg1); }));

    auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return UIEvents::MouseEvent::construct_impl(realm, type, event_init_dict); }));

    // 7. Set instance.[[Prototype]] to prototype.
    VERIFY(prototype.is_object());
    impl->set_prototype(&prototype.as_object());

    // FIXME: Steps 8...11. of the "internally create a new object implementing the interface MouseEvent" algorithm
    // (https://webidl.spec.whatwg.org/#js-platform-objects) are currently not handled, or are handled within UIEvents::MouseEvent::construct_impl().

    return *impl;
}

void MouseEventPrototype::initialize(JS::Realm& realm, JS::Object& object)
{
    [[maybe_unused]] auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable;

    object.set_prototype(GC::Ref { ensure_web_prototype<UIEventPrototype>(realm, "UIEvent"_fly_string) });

    auto screen_x_id = "screenX"_utf16_fly_string;
    auto native_screen_x_getter = JS::NativeFunction::create(realm, screen_x_getter, 0, screen_x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_screen_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto screen_x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(screen_x_id, native_screen_x_getter, native_screen_x_setter, screen_x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto screen_y_id = "screenY"_utf16_fly_string;
    auto native_screen_y_getter = JS::NativeFunction::create(realm, screen_y_getter, 0, screen_y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_screen_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto screen_y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(screen_y_id, native_screen_y_getter, native_screen_y_setter, screen_y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto page_x_id = "pageX"_utf16_fly_string;
    auto native_page_x_getter = JS::NativeFunction::create(realm, page_x_getter, 0, page_x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_page_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto page_x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(page_x_id, native_page_x_getter, native_page_x_setter, page_x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto page_y_id = "pageY"_utf16_fly_string;
    auto native_page_y_getter = JS::NativeFunction::create(realm, page_y_getter, 0, page_y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_page_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto page_y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(page_y_id, native_page_y_getter, native_page_y_setter, page_y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto client_x_id = "clientX"_utf16_fly_string;
    auto native_client_x_getter = JS::NativeFunction::create(realm, client_x_getter, 0, client_x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_client_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto client_x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(client_x_id, native_client_x_getter, native_client_x_setter, client_x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto client_y_id = "clientY"_utf16_fly_string;
    auto native_client_y_getter = JS::NativeFunction::create(realm, client_y_getter, 0, client_y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_client_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto client_y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(client_y_id, native_client_y_getter, native_client_y_setter, client_y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto x_id = "x"_utf16_fly_string;
    auto native_x_getter = JS::NativeFunction::create(realm, x_getter, 0, x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(x_id, native_x_getter, native_x_setter, x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto y_id = "y"_utf16_fly_string;
    auto native_y_getter = JS::NativeFunction::create(realm, y_getter, 0, y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(y_id, native_y_getter, native_y_setter, y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto offset_x_id = "offsetX"_utf16_fly_string;
    auto native_offset_x_getter = JS::NativeFunction::create(realm, offset_x_getter, 0, offset_x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_offset_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto offset_x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(offset_x_id, native_offset_x_getter, native_offset_x_setter, offset_x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto offset_y_id = "offsetY"_utf16_fly_string;
    auto native_offset_y_getter = JS::NativeFunction::create(realm, offset_y_getter, 0, offset_y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_offset_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto offset_y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(offset_y_id, native_offset_y_getter, native_offset_y_setter, offset_y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto ctrl_key_id = "ctrlKey"_utf16_fly_string;
    auto native_ctrl_key_getter = JS::NativeFunction::create(realm, ctrl_key_getter, 0, ctrl_key_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_ctrl_key_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto ctrl_key_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(ctrl_key_id, native_ctrl_key_getter, native_ctrl_key_setter, ctrl_key_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto shift_key_id = "shiftKey"_utf16_fly_string;
    auto native_shift_key_getter = JS::NativeFunction::create(realm, shift_key_getter, 0, shift_key_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_shift_key_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto shift_key_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(shift_key_id, native_shift_key_getter, native_shift_key_setter, shift_key_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto alt_key_id = "altKey"_utf16_fly_string;
    auto native_alt_key_getter = JS::NativeFunction::create(realm, alt_key_getter, 0, alt_key_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_alt_key_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto alt_key_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(alt_key_id, native_alt_key_getter, native_alt_key_setter, alt_key_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto meta_key_id = "metaKey"_utf16_fly_string;
    auto native_meta_key_getter = JS::NativeFunction::create(realm, meta_key_getter, 0, meta_key_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_meta_key_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto meta_key_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(meta_key_id, native_meta_key_getter, native_meta_key_setter, meta_key_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto movement_x_id = "movementX"_utf16_fly_string;
    auto native_movement_x_getter = JS::NativeFunction::create(realm, movement_x_getter, 0, movement_x_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_movement_x_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto movement_x_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(movement_x_id, native_movement_x_getter, native_movement_x_setter, movement_x_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto movement_y_id = "movementY"_utf16_fly_string;
    auto native_movement_y_getter = JS::NativeFunction::create(realm, movement_y_getter, 0, movement_y_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_movement_y_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto movement_y_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(movement_y_id, native_movement_y_getter, native_movement_y_setter, movement_y_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto button_id = "button"_utf16_fly_string;
    auto native_button_getter = JS::NativeFunction::create(realm, button_getter, 0, button_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_button_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto button_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(button_id, native_button_getter, native_button_setter, button_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto buttons_id = "buttons"_utf16_fly_string;
    auto native_buttons_getter = JS::NativeFunction::create(realm, buttons_getter, 0, buttons_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_buttons_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto buttons_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(buttons_id, native_buttons_getter, native_buttons_setter, buttons_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto related_target_id = "relatedTarget"_utf16_fly_string;
    auto native_related_target_getter = JS::NativeFunction::create(realm, related_target_getter, 0, related_target_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_related_target_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto related_target_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(related_target_id, native_related_target_getter, native_related_target_setter, related_target_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    object.define_native_function(realm, "getModifierState"_utf16_fly_string, get_modifier_state, 1, default_attributes);

    object.define_native_function(realm, "initMouseEvent"_utf16_fly_string, init_mouse_event, 1, default_attributes);

    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "MouseEvent"_utf16), JS::Attribute::Configurable);
}

void MouseEventPrototype::define_unforgeable_attributes(JS::Realm& realm, [[maybe_unused]] JS::Object& object)
{
    [[maybe_unused]] auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;
}

[[maybe_unused]] static JS::ThrowCompletionOr<UIEvents::MouseEvent*> impl_from(JS::VM& vm, JS::Value js_value)
{

    if (auto impl = js_value.as_if<UIEvents::MouseEvent>())
        return impl.ptr();
    return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "MouseEvent");
}

[[maybe_unused]] static JS::ThrowCompletionOr<UIEvents::MouseEvent*> impl_from(JS::VM& vm)
{
    auto this_value = vm.this_value();
    if (this_value.is_nullish())
        this_value = &vm.current_realm()->global_object();
    return impl_from(vm, this_value);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::screen_x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::screen_x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->screen_x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::screen_y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::screen_y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->screen_y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::page_x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::page_x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->page_x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::page_y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::page_y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->page_y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::client_x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::client_x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->client_x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::client_y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::client_y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->client_y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::offset_x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::offset_x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->offset_x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::offset_y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::offset_y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->offset_y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::ctrl_key_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::ctrl_key_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->ctrl_key(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::shift_key_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::shift_key_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->shift_key(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::alt_key_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::alt_key_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->alt_key(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::meta_key_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::meta_key_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->meta_key(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::movement_x_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::movement_x_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->movement_x(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::movement_y_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::movement_y_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->movement_y(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::button_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::button_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->button(); }));

    return JS::Value(static_cast<WebIDL::Short>(R));
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::buttons_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::buttons_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->buttons(); }));

    return JS::Value(static_cast<WebIDL::UnsignedShort>(R));
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::related_target_getter)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::related_target_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->related_target(); }));

    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R)
            return JS::js_null();

        // 2. Otherwise, the JavaScript value is the result of converting the IDL nullable type value to the inner IDL type T.
        return JS::Value(JS::Value(R));
    }();
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::get_modifier_state)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::get_modifier_state");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] UIEvents::MouseEvent* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 1)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getModifierState");

    auto arg0 = vm.argument(0);
    auto key_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<String> {
        return TRY(WebIDL::to_string(vm, arg0));
    }(); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->get_modifier_state(key_arg); }));
    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MouseEventPrototype::init_mouse_event)
{
    WebIDL::log_trace(vm, "MouseEventPrototype::init_mouse_event");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] UIEvents::MouseEvent* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 1)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "initMouseEvent");

    auto arg0 = vm.argument(0);
    auto type_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<String> {
        return TRY(WebIDL::to_string(vm, arg0));
    }(); }));

    auto arg1 = vm.argument(1);
    bool bubbles_arg = false;
    if (!arg1.is_undefined())
        bubbles_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg1.to_boolean(); }));

    auto arg2 = vm.argument(2);
    bool cancelable_arg = false;
    if (!arg2.is_undefined())
        cancelable_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg2.to_boolean(); }));

    auto arg3 = vm.argument(3);
    GC::Ptr<HTML::WindowProxy> view_arg = nullptr;
    if (!arg3.is_undefined())
        view_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<HTML::WindowProxy>> {
        GC::Ptr<HTML::WindowProxy> value;

        if (!arg3.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<HTML::WindowProxy>> {
        if (auto impl = arg3.as_if<HTML::WindowProxy>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WindowProxy");
    }());
        }
        return value;
    }(); }));

    auto arg4 = vm.argument(4);
    WebIDL::Long detail_arg = 0;
    if (!arg4.is_undefined())
        detail_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Long>(vm, arg4, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg5 = vm.argument(5);
    WebIDL::Long screen_x_arg = 0;
    if (!arg5.is_undefined())
        screen_x_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Long>(vm, arg5, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg6 = vm.argument(6);
    WebIDL::Long screen_y_arg = 0;
    if (!arg6.is_undefined())
        screen_y_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Long>(vm, arg6, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg7 = vm.argument(7);
    WebIDL::Long client_x_arg = 0;
    if (!arg7.is_undefined())
        client_x_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Long>(vm, arg7, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg8 = vm.argument(8);
    WebIDL::Long client_y_arg = 0;
    if (!arg8.is_undefined())
        client_y_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Long>(vm, arg8, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg9 = vm.argument(9);
    bool ctrl_key_arg = false;
    if (!arg9.is_undefined())
        ctrl_key_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg9.to_boolean(); }));

    auto arg10 = vm.argument(10);
    bool alt_key_arg = false;
    if (!arg10.is_undefined())
        alt_key_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg10.to_boolean(); }));

    auto arg11 = vm.argument(11);
    bool shift_key_arg = false;
    if (!arg11.is_undefined())
        shift_key_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg11.to_boolean(); }));

    auto arg12 = vm.argument(12);
    bool meta_key_arg = false;
    if (!arg12.is_undefined())
        meta_key_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return arg12.to_boolean(); }));

    auto arg13 = vm.argument(13);
    WebIDL::Short button_arg = 0;
    if (!arg13.is_undefined())
        button_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Short>(vm, arg13, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    auto arg14 = vm.argument(14);
    GC::Ptr<DOM::EventTarget> related_target_arg = nullptr;
    if (!arg14.is_undefined())
        related_target_arg = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<DOM::EventTarget>> {
        GC::Ptr<DOM::EventTarget> value;

        if (!arg14.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<DOM::EventTarget>> {
        if (auto impl = arg14.as_if<DOM::EventTarget>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "EventTarget");
    }());
        }
        return value;
    }(); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->init_mouse_event(type_arg, bubbles_arg, cancelable_arg, view_arg, detail_arg, screen_x_arg, screen_y_arg, client_x_arg, client_y_arg, ctrl_key_arg, alt_key_arg, shift_key_arg, meta_key_arg, button_arg, related_target_arg); }));
    return JS::js_undefined();
}

// https://webidl.spec.whatwg.org/#es-dictionary
JS::ThrowCompletionOr<MouseEventInit> convert_to_idl_value_for_mouse_event_init(JS::VM& vm, JS::Value js_dict)
{
    // 1. If jsDict is not an Object and jsDict is neither undefined nor null, then throw a TypeError.
    if (!js_dict.is_object() && !js_dict.is_nullish())
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "MouseEventInit");

    // 2. Let idlDict be an empty ordered map, representing a dictionary of type D.
    // 3. Let dictionaries be a list consisting of D and all of D’s inherited dictionaries, in order from least to most derived.
    // 4. For each dictionary dictionary in dictionaries, in order:
    // NB: We defer construction until the return initializer because some members may not be default-constructible. Inherited dictionaries are represented by the generated C++ struct inheritance.

    // 5. Return idlDict.
    return MouseEventInit {
        TRY(convert_to_idl_value_for_event_modifier_init(vm, js_dict)),
        TRY([&]() -> JS::ThrowCompletionOr<WebIDL::Short> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("button"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::Short>(vm, js_member_value, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<WebIDL::UnsignedShort> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("buttons"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::UnsignedShort>(vm, js_member_value, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("clientX"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "clientX");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("clientY"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "clientY");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("movementX"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "movementX");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("movementY"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "movementY");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<GC::Ptr<DOM::EventTarget>> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("relatedTarget"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<DOM::EventTarget>> {
        GC::Ptr<DOM::EventTarget> value;

        if (!js_member_value.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<DOM::EventTarget>> {
        if (auto impl = js_member_value.as_if<DOM::EventTarget>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "EventTarget");
    }());
        }
        return value;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = nullptr;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("screenX"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "screenX");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
        TRY([&]() -> JS::ThrowCompletionOr<double> {
            // 1. Let key be the identifier of member.
            // 2. If jsDict is either undefined or null, then:
            //     1. Let jsMemberValue be undefined.
            // 3. Otherwise,
            //     1. Let jsMemberValue be ? Get(jsDict, key).
            auto js_member_value = JS::js_undefined();
            if (js_dict.is_object())
                js_member_value = TRY(js_dict.as_object().get("screenY"_utf16_fly_string));

            // 4. If jsMemberValue is not undefined, then:
            if (!js_member_value.is_undefined()) {
                // 1. Let idlMemberValue be the result of converting jsMemberValue to an IDL value whose type is the type member is declared to be of.
                auto idl_member_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(js_member_value.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "screenY");
        return x;
    }(); }));

                // 2. Set idlDict[key] to idlMemberValue.
                return idl_member_value;
            }
            // 5. Otherwise, if jsMemberValue is undefined but member has a default value, then:
            // 1. Let idlMemberValue be the result of converting member's default value to an IDL value whose type is the type member is declared to be of.
            auto idl_member_value = 0;

            // 2. Set idlDict[key] to idlMemberValue.
            return idl_member_value;
        }()),
    };
}

} // namespace Web::Bindings
