#include <AK/Optional.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/CSSRotate.h>
#include <LibWeb/Bindings/CSSTransformComponent.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/CSS/CSSNumericValue.h>
#include <LibWeb/CSS/CSSRotate.h>
#include <LibWeb/WebIDL/OverloadResolution.h>
#include <LibWeb/WebIDL/Tracing.h>

namespace Web::Bindings {

void CSSRotateConstructor::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<CSSTransformComponentPrototype>(realm, "CSSTransformComponent"_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, "CSSRotate"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<CSSRotatePrototype>(realm, "CSSRotate"_fly_string), 0);
}

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

    Optional<int> chosen_overload_callable_id;
    Optional<WebIDL::EffectiveOverloadSet> effective_overload_set;

    switch (min(4, vm.argument_count())) {
    case 1:
        chosen_overload_callable_id = 0;
        break;
    case 4:
        chosen_overload_callable_id = 1;
        break;
    }

    Vector<StringView> dictionary_types {
    };

    if (!chosen_overload_callable_id.has_value()) {
        if (!effective_overload_set.has_value())
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::OverloadResolutionFailed);
        chosen_overload_callable_id = TRY(WebIDL::resolve_overload(vm, effective_overload_set.value(), dictionary_types)).callable_id;
    }


    switch (chosen_overload_callable_id.value()) {
    case 0:
        return construct0(constructor, new_target);
    case 1:
        return construct1(constructor, new_target);
    default:
        VERIFY_NOT_REACHED();
    }
}

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

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

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

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

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

    auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return CSS::CSSRotate::construct_impl(realm, angle); }));

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

    return *impl;
}

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

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

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

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

    auto arg0 = vm.argument(0);
    auto x = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (arg0.is_object()) {
            [[maybe_unused]] auto& object = arg0.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (arg0.is_number()) {
            auto x_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg0.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "x");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { x_number };
        }

        auto x_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg0.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "x");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { x_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

    auto arg1 = vm.argument(1);
    auto y = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (arg1.is_object()) {
            [[maybe_unused]] auto& object = arg1.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (arg1.is_number()) {
            auto y_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg1.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "y");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { y_number };
        }

        auto y_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg1.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "y");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { y_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

    auto arg2 = vm.argument(2);
    auto z = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (arg2.is_object()) {
            [[maybe_unused]] auto& object = arg2.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (arg2.is_number()) {
            auto z_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg2.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "z");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { z_number };
        }

        auto z_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(arg2.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "z");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { z_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

    auto arg3 = vm.argument(3);
    auto angle = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ref<CSS::CSSNumericValue>> {
        if (auto impl = arg3.as_if<CSS::CSSNumericValue>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CSSNumericValue");
    }(); }));

    auto impl = TRY(throw_dom_exception_if_needed(vm, [&] { return CSS::CSSRotate::construct_impl(realm, x, y, z, angle); }));

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

    return *impl;
}

void CSSRotatePrototype::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<CSSTransformComponentPrototype>(realm, "CSSTransformComponent"_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);
    auto native_x_setter = JS::NativeFunction::create(realm, x_setter, 1, x_id, &realm, "set"sv);

    // 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);
    auto native_y_setter = JS::NativeFunction::create(realm, y_setter, 1, y_id, &realm, "set"sv);

    // 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 z_id = "z"_utf16_fly_string;
    auto native_z_getter = JS::NativeFunction::create(realm, z_getter, 0, z_id, &realm, "get"sv);
    auto native_z_setter = JS::NativeFunction::create(realm, z_setter, 1, z_id, &realm, "set"sv);

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto z_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(z_id, native_z_getter, native_z_setter, z_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 angle_id = "angle"_utf16_fly_string;
    auto native_angle_getter = JS::NativeFunction::create(realm, angle_getter, 0, angle_id, &realm, "get"sv);
    auto native_angle_setter = JS::NativeFunction::create(realm, angle_setter, 1, angle_id, &realm, "set"sv);

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto angle_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(angle_id, native_angle_getter, native_angle_setter, angle_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_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "CSSRotate"_utf16), JS::Attribute::Configurable);
}

void CSSRotatePrototype::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<CSS::CSSRotate*> impl_from(JS::VM& vm, JS::Value js_value)
{

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

[[maybe_unused]] static JS::ThrowCompletionOr<CSS::CSSRotate*> 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(CSSRotatePrototype::x_getter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::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 R.visit(
        [&](double const& visited_union_value0) -> JS::Value
        {
            return JS::Value(visited_union_value0);
        },
        [&](GC::Ref<CSS::CSSNumericValue> const& visited_union_value1) -> JS::Value
        {
            return JS::Value(visited_union_value1);
        }
    );
}

JS_DEFINE_NATIVE_FUNCTION(CSSRotatePrototype::y_getter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::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 R.visit(
        [&](double const& visited_union_value0) -> JS::Value
        {
            return JS::Value(visited_union_value0);
        },
        [&](GC::Ref<CSS::CSSNumericValue> const& visited_union_value1) -> JS::Value
        {
            return JS::Value(visited_union_value1);
        }
    );
}

JS_DEFINE_NATIVE_FUNCTION(CSSRotatePrototype::z_getter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::z_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->z(); }));

    return R.visit(
        [&](double const& visited_union_value0) -> JS::Value
        {
            return JS::Value(visited_union_value0);
        },
        [&](GC::Ref<CSS::CSSNumericValue> const& visited_union_value1) -> JS::Value
        {
            return JS::Value(visited_union_value1);
        }
    );
}

JS_DEFINE_NATIVE_FUNCTION(CSSRotatePrototype::angle_getter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::angle_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->angle(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(CSSRotatePrototype::x_setter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::x_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]] CSS::CSSRotate* 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<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (V.is_object()) {
            [[maybe_unused]] auto& object = V.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (V.is_number()) {
            auto x_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "x");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { x_number };
        }

        auto x_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "x");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { x_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

        // 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_x(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(CSSRotatePrototype::y_setter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::y_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]] CSS::CSSRotate* 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<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (V.is_object()) {
            [[maybe_unused]] auto& object = V.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (V.is_number()) {
            auto y_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "y");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { y_number };
        }

        auto y_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "y");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { y_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

        // 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_y(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(CSSRotatePrototype::z_setter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::z_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]] CSS::CSSRotate* 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<Variant<double, GC::Ref<CSS::CSSNumericValue>>> {

        if (V.is_object()) {
            [[maybe_unused]] auto& object = V.as_object();

            if (is<PlatformObject>(object)) {

                if (auto* result = as_if<CSS::CSSNumericValue>(object))
                    return Variant<double, GC::Ref<CSS::CSSNumericValue>> { GC::Ref { *result } };
            }
        }

        if (V.is_number()) {
            auto z_number = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "z");
        return x;
    }());
            return Variant<double, GC::Ref<CSS::CSSNumericValue>> { z_number };
        }

        auto z_number_fallback = TRY([&]() -> JS::ThrowCompletionOr<double> {
        auto x = TRY(V.to_double(vm));
        if (isinf(x) || isnan(x))
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidRestrictedFloatingPointParameter, "z");
        return x;
    }());
        return Variant<double, GC::Ref<CSS::CSSNumericValue>> { z_number_fallback };

        return vm.throw_completion<JS::TypeError>("No union types matched"_utf16);
    }(); }));

        // 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_z(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(CSSRotatePrototype::angle_setter)
{
    WebIDL::log_trace(vm, "CSSRotatePrototype::angle_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]] CSS::CSSRotate* 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<GC::Ref<CSS::CSSNumericValue>> {
        if (auto impl = V.as_if<CSS::CSSNumericValue>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CSSNumericValue");
    }(); }));

        // 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_angle(idl_value); }));
    return {};
        }();

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

        return JS::js_undefined();
    };

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

} // namespace Web::Bindings
