#include <AK/Utf16String.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/SVGGraphicsElement.h>
#include <LibWeb/Bindings/SVGSVGElement.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/NodeList.h>
#include <LibWeb/Geometry/DOMMatrix.h>
#include <LibWeb/Geometry/DOMPoint.h>
#include <LibWeb/Geometry/DOMPointReadOnly.h>
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/Geometry/DOMRectReadOnly.h>
#include <LibWeb/SVG/SVGAnimatedLength.h>
#include <LibWeb/SVG/SVGAnimatedRect.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGLength.h>
#include <LibWeb/SVG/SVGSVGElement.h>
#include <LibWeb/SVG/SVGTransform.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

void SVGSVGElementConstructor::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<SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"_fly_string));
    object.define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "SVGSVGElement"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<SVGSVGElementPrototype>(realm, "SVGSVGElement"_fly_string), 0);
}

JS::ThrowCompletionOr<GC::Ref<JS::Object>> SVGSVGElementConstructor::construct([[maybe_unused]] InterfaceConstructor& constructor, [[maybe_unused]] JS::FunctionObject& new_target)
{
    WebIDL::log_trace(constructor.vm(), "SVGSVGElementConstructor::construct");
    return constructor.vm().throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, "SVGSVGElement");
}

void SVGSVGElementPrototype::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<SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"_fly_string) });

    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 current_scale_id = "currentScale"_utf16_fly_string;
    auto native_current_scale_getter = JS::NativeFunction::create(realm, current_scale_getter, 0, current_scale_id, &realm, "get"sv);
    auto native_current_scale_setter = JS::NativeFunction::create(realm, current_scale_setter, 1, current_scale_id, &realm, "set"sv);

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto current_scale_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(current_scale_id, native_current_scale_getter, native_current_scale_setter, current_scale_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 current_translate_id = "currentTranslate"_utf16_fly_string;
    auto native_current_translate_getter = JS::NativeFunction::create(realm, current_translate_getter, 0, current_translate_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_current_translate_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto current_translate_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(current_translate_id, native_current_translate_getter, native_current_translate_setter, current_translate_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 view_box_id = "viewBox"_utf16_fly_string;
    auto native_view_box_getter = JS::NativeFunction::create(realm, view_box_getter, 0, view_box_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_view_box_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto view_box_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(view_box_id, native_view_box_getter, native_view_box_setter, view_box_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, "getIntersectionList"_utf16_fly_string, get_intersection_list, 2, default_attributes);

    object.define_native_function(realm, "getEnclosureList"_utf16_fly_string, get_enclosure_list, 2, default_attributes);

    object.define_native_function(realm, "checkIntersection"_utf16_fly_string, check_intersection, 2, default_attributes);

    object.define_native_function(realm, "checkEnclosure"_utf16_fly_string, check_enclosure, 2, default_attributes);

    object.define_native_function(realm, "deselectAll"_utf16_fly_string, deselect_all, 0, default_attributes);

    object.define_native_function(realm, "createSVGLength"_utf16_fly_string, create_svg_length, 0, default_attributes);

    object.define_native_function(realm, "createSVGPoint"_utf16_fly_string, create_svg_point, 0, default_attributes);

    object.define_native_function(realm, "createSVGMatrix"_utf16_fly_string, create_svg_matrix, 0, default_attributes);

    object.define_native_function(realm, "createSVGRect"_utf16_fly_string, create_svg_rect, 0, default_attributes);

    object.define_native_function(realm, "createSVGTransform"_utf16_fly_string, create_svg_transform, 0, default_attributes);

    object.define_native_function(realm, "getElementById"_utf16_fly_string, get_element_by_id, 1, default_attributes);

    object.define_native_function(realm, "suspendRedraw"_utf16_fly_string, suspend_redraw, 1, default_attributes);

    object.define_native_function(realm, "unsuspendRedraw"_utf16_fly_string, unsuspend_redraw, 1, default_attributes);

    object.define_native_function(realm, "unsuspendRedrawAll"_utf16_fly_string, unsuspend_redraw_all, 0, default_attributes);

    object.define_native_function(realm, "forceRedraw"_utf16_fly_string, force_redraw, 0, default_attributes);

    object.define_direct_property("createSVGAngle"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("createSVGTransformFromMatrix"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

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

void SVGSVGElementPrototype::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<SVG::SVGSVGElement*> impl_from(JS::VM& vm, JS::Value js_value)
{

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

[[maybe_unused]] static JS::ThrowCompletionOr<SVG::SVGSVGElement*> 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(SVGSVGElementPrototype::x_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::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(SVGSVGElementPrototype::y_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::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(SVGSVGElementPrototype::width_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::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(SVGSVGElementPrototype::height_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::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(SVGSVGElementPrototype::current_scale_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::current_scale_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->current_scale(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::current_translate_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::current_translate_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->current_translate(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::view_box_getter)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::view_box_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->view_box_for_bindings(); }));

    return JS::Value(R);
}

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

    // 1. Let V be undefined.
    auto V = JS::js_undefined();

    // 2. If any arguments were passed, then set V to the value of the first argument passed.
    if (vm.argument_count() > 0)
        V = vm.argument(0);

    // 3. Let id be attribute’s identifier.

    // 4. Let idlObject be null.
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = nullptr;

    // 5. If attribute is a regular attribute:

    // 1. Let jsValue be the this value, if it is not null or undefined, or realm’s global object otherwise. (This will subsequently cause a TypeError in a few steps, if the global object does not implement target and [LegacyLenientThis] is not specified.)
    auto js_value = vm.this_value();
    if (js_value.is_nullish())
        js_value = &realm.global_object();

    // 2. FIXME: If jsValue is a platform object, then perform a security check, passing jsValue, attribute’s identifier, and "setter".

    // 3. Let validThis be true if jsValue implements target, or false otherwise.
    auto maybe_idl_object = impl_from(vm, js_value);

    // 4. If validThis is false and attribute was not specified with the [LegacyLenientThis] extended attribute, then throw a TypeError.
    idl_object = TRY(maybe_idl_object);

    auto original_steps = [&]() -> JS::ThrowCompletionOr<JS::Value> {
        // 6. Let idlValue be determined as follows:
        // -> Otherwise, idlValue is the result of converting V to an IDL value of attribute’s type.
        auto idl_value = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<float> {
        float x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "currentScale");
        return x;
    }(); }));

        // 7. Run the setter steps of attribute with idlObject as this and idlValue as the value.
        auto setter_result = [&]() -> JS::ThrowCompletionOr<void> {
            TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->set_current_scale(idl_value); }));
    return {};
        }();

        if (setter_result.is_error())
            return setter_result.release_error();

        return JS::js_undefined();
    };

    // 8. Return undefined.
    return TRY(original_steps());
}

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::get_intersection_list)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::get_intersection_list");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 2)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountMany, "getIntersectionList", "2");

    auto arg0 = vm.argument(0);
    auto rect = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<Geometry::DOMRectReadOnly>> {
        if (auto impl = arg0.as_if<Geometry::DOMRectReadOnly>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOMRectReadOnly");
    }(); }));

    auto arg1 = vm.argument(1);
    auto reference_element = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<SVG::SVGElement>> {
        GC::Ptr<SVG::SVGElement> value;

        if (!arg1.is_nullish()) {

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

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::get_enclosure_list)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::get_enclosure_list");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 2)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountMany, "getEnclosureList", "2");

    auto arg0 = vm.argument(0);
    auto rect = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<Geometry::DOMRectReadOnly>> {
        if (auto impl = arg0.as_if<Geometry::DOMRectReadOnly>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOMRectReadOnly");
    }(); }));

    auto arg1 = vm.argument(1);
    auto reference_element = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<SVG::SVGElement>> {
        GC::Ptr<SVG::SVGElement> value;

        if (!arg1.is_nullish()) {

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

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::check_intersection)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::check_intersection");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 2)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountMany, "checkIntersection", "2");

    auto arg0 = vm.argument(0);
    auto element = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<SVG::SVGElement>> {
        if (auto impl = arg0.as_if<SVG::SVGElement>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SVGElement");
    }(); }));

    auto arg1 = vm.argument(1);
    auto rect = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<Geometry::DOMRectReadOnly>> {
        if (auto impl = arg1.as_if<Geometry::DOMRectReadOnly>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOMRectReadOnly");
    }(); }));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::check_enclosure)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::check_enclosure");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 2)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountMany, "checkEnclosure", "2");

    auto arg0 = vm.argument(0);
    auto element = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<SVG::SVGElement>> {
        if (auto impl = arg0.as_if<SVG::SVGElement>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SVGElement");
    }(); }));

    auto arg1 = vm.argument(1);
    auto rect = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<Geometry::DOMRectReadOnly>> {
        if (auto impl = arg1.as_if<Geometry::DOMRectReadOnly>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOMRectReadOnly");
    }(); }));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::deselect_all)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::deselect_all");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::create_svg_length)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::create_svg_length");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::create_svg_point)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::create_svg_point");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::create_svg_matrix)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::create_svg_matrix");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::create_svg_rect)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::create_svg_rect");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::create_svg_transform)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::create_svg_transform");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::get_element_by_id)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::get_element_by_id");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

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

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->get_element_by_id(element_id); }));
    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(SVGSVGElementPrototype::suspend_redraw)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::suspend_redraw");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

    auto arg0 = vm.argument(0);
    auto max_wait_milliseconds = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::UnsignedLong>(vm, arg0, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->suspend_redraw(max_wait_milliseconds); }));
    return JS::Value(static_cast<WebIDL::UnsignedLong>(R));
}

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::unsuspend_redraw)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::unsuspend_redraw");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

    auto arg0 = vm.argument(0);
    auto suspend_handle_id = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::UnsignedLong>(vm, arg0, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->unsuspend_redraw(suspend_handle_id); }));
    return JS::js_undefined();
}

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::unsuspend_redraw_all)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::unsuspend_redraw_all");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

JS_DEFINE_NATIVE_FUNCTION(SVGSVGElementPrototype::force_redraw)
{
    WebIDL::log_trace(vm, "SVGSVGElementPrototype::force_redraw");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGSVGElement* idl_object = TRY(impl_from(vm));

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

} // namespace Web::Bindings
