#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/DOMRectReadOnly.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Geometry/DOMRectReadOnly.h>
#include <LibWeb/WebIDL/Tracing.h>

namespace Web::Bindings {

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

    
    object.define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "DOMRectReadOnly"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<DOMRectReadOnlyPrototype>(realm, "DOMRectReadOnly"_fly_string), 0);
    object.define_native_function(realm, "fromRect"_utf16_fly_string, from_rect, 0, JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable);

}

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

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

    // 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<DOMRectReadOnlyPrototype>(*target_realm, "DOMRectReadOnly"_fly_string);
    }

    auto arg0 = vm.argument(0);
    double x = 0;
    if (!arg0.is_undefined())
        x = TRY(throw_dom_exception_if_needed(vm, [&] { return arg0.to_double(vm); }));

    auto arg1 = vm.argument(1);
    double y = 0;
    if (!arg1.is_undefined())
        y = TRY(throw_dom_exception_if_needed(vm, [&] { return arg1.to_double(vm); }));

    auto arg2 = vm.argument(2);
    double width = 0;
    if (!arg2.is_undefined())
        width = TRY(throw_dom_exception_if_needed(vm, [&] { return arg2.to_double(vm); }));

    auto arg3 = vm.argument(3);
    double height = 0;
    if (!arg3.is_undefined())
        height = TRY(throw_dom_exception_if_needed(vm, [&] { return arg3.to_double(vm); }));

    auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return Geometry::DOMRectReadOnly::construct_impl(realm, x, y, width, height); }));

    // 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 DOMRectReadOnly" algorithm
    // (https://webidl.spec.whatwg.org/#js-platform-objects) are currently not handled, or are handled within Geometry::DOMRectReadOnly::construct_impl().

    return *impl;
}

void DOMRectReadOnlyPrototype::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(realm.intrinsics().object_prototype());

    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 width_id = "width"_utf16_fly_string;
    auto native_width_getter = JS::NativeFunction::create(realm, width_getter, 0, width_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_width_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto width_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(width_id, native_width_getter, native_width_setter, width_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 height_id = "height"_utf16_fly_string;
    auto native_height_getter = JS::NativeFunction::create(realm, height_getter, 0, height_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_height_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto height_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(height_id, native_height_getter, native_height_setter, height_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 top_id = "top"_utf16_fly_string;
    auto native_top_getter = JS::NativeFunction::create(realm, top_getter, 0, top_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_top_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto top_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(top_id, native_top_getter, native_top_setter, top_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 right_id = "right"_utf16_fly_string;
    auto native_right_getter = JS::NativeFunction::create(realm, right_getter, 0, right_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_right_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto right_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(right_id, native_right_getter, native_right_setter, right_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 bottom_id = "bottom"_utf16_fly_string;
    auto native_bottom_getter = JS::NativeFunction::create(realm, bottom_getter, 0, bottom_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_bottom_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto bottom_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(bottom_id, native_bottom_getter, native_bottom_setter, bottom_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 left_id = "left"_utf16_fly_string;
    auto native_left_getter = JS::NativeFunction::create(realm, left_getter, 0, left_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_left_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto left_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(left_id, native_left_getter, native_left_setter, left_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, "toJSON"_utf16_fly_string, to_json, 0, default_attributes);

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

void DOMRectReadOnlyPrototype::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<Geometry::DOMRectReadOnly*> impl_from(JS::VM& vm, JS::Value js_value)
{

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

[[maybe_unused]] static JS::ThrowCompletionOr<Geometry::DOMRectReadOnly*> 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(DOMRectReadOnlyConstructor::from_rect)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyConstructor::from_rect");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    auto arg0 = vm.argument(0);
    DOMRectInit other = DOMRectInit {};
    if (!arg0.is_undefined())
        other = TRY(throw_dom_exception_if_needed(vm, [&] { return convert_to_idl_value_for_dom_rect_init(vm, arg0); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return Geometry::DOMRectReadOnly::from_rect(vm, other); }));
    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::x_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::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(DOMRectReadOnlyPrototype::y_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::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(DOMRectReadOnlyPrototype::width_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::width_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->width(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::height_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::height_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->height(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::top_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::top_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->top(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::right_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::right_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->right(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::bottom_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::bottom_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->bottom(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::left_getter)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::left_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->left(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(DOMRectReadOnlyPrototype::to_json)
{
    WebIDL::log_trace(vm, "DOMRectReadOnlyPrototype::to_json");
    auto& realm = *vm.current_realm();

    [[maybe_unused]] auto* idl_object = TRY(impl_from(vm));

    // 4. Let result be OrdinaryObjectCreate(%Object.prototype%).
    auto result = JS::Object::create(realm, realm.intrinsics().object_prototype());

    // 5. For each key → value of map:
    auto x_0_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->x(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto x_0_key = "x"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto x_0_value_js = JS::Value(x_0_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(x_0_key, x_0_value_js));
    auto y_1_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->y(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto y_1_key = "y"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto y_1_value_js = JS::Value(y_1_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(y_1_key, y_1_value_js));
    auto width_2_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->width(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto width_2_key = "width"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto width_2_value_js = JS::Value(width_2_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(width_2_key, width_2_value_js));
    auto height_3_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->height(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto height_3_key = "height"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto height_3_value_js = JS::Value(height_3_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(height_3_key, height_3_value_js));
    auto top_4_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->top(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto top_4_key = "top"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto top_4_value_js = JS::Value(top_4_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(top_4_key, top_4_value_js));
    auto right_5_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->right(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto right_5_key = "right"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto right_5_value_js = JS::Value(right_5_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(right_5_key, right_5_value_js));
    auto bottom_6_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->bottom(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto bottom_6_key = "bottom"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto bottom_6_value_js = JS::Value(bottom_6_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(bottom_6_key, bottom_6_value_js));
    auto left_7_value = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->left(); }));

    // 1. Let k be key converted to a JavaScript value.
    auto left_7_key = "left"_utf16_fly_string;

    // 2. Let v be value converted to a JavaScript value.
    auto left_7_value_js = JS::Value(left_7_value);

    // 3. Perform ! CreateDataPropertyOrThrow(result, k, v).
    MUST(result->create_data_property(left_7_key, left_7_value_js));

    // 6. Return result.
    return result;
}

// https://webidl.spec.whatwg.org/#es-dictionary
JS::ThrowCompletionOr<DOMRectInit> convert_to_idl_value_for_dom_rect_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, "DOMRectInit");

    // 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 DOMRectInit {
        .height = 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("height"_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_member_value.to_double(vm); }));

                // 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;
        }()),
        .width = 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("width"_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_member_value.to_double(vm); }));

                // 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;
        }()),
        .x = 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("x"_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_member_value.to_double(vm); }));

                // 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;
        }()),
        .y = 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("y"_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_member_value.to_double(vm); }));

                // 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
