#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/SVGComponentTransferFunctionElement.h>
#include <LibWeb/Bindings/SVGElement.h>
#include <LibWeb/SVG/SVGAnimatedEnumeration.h>
#include <LibWeb/SVG/SVGAnimatedNumber.h>
#include <LibWeb/SVG/SVGAnimatedNumberList.h>
#include <LibWeb/SVG/SVGComponentTransferFunctionElement.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

void SVGComponentTransferFunctionElementConstructor::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<SVGElementPrototype>(realm, "SVGElement"_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, "SVGComponentTransferFunctionElement"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<SVGComponentTransferFunctionElementPrototype>(realm, "SVGComponentTransferFunctionElement"_fly_string), 0);

    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(0)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(1)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_TABLE"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(2)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_DISCRETE"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(3)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_LINEAR"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(4)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_GAMMA"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(5)), JS::Attribute::Enumerable);
}

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

void SVGComponentTransferFunctionElementPrototype::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<SVGElementPrototype>(realm, "SVGElement"_fly_string) });

    auto type_id = "type"_utf16_fly_string;
    auto native_type_getter = JS::NativeFunction::create(realm, type_getter, 0, type_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_type_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto type_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(type_id, native_type_getter, native_type_setter, type_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 table_values_id = "tableValues"_utf16_fly_string;
    auto native_table_values_getter = JS::NativeFunction::create(realm, table_values_getter, 0, table_values_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_table_values_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto table_values_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(table_values_id, native_table_values_getter, native_table_values_setter, table_values_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 slope_id = "slope"_utf16_fly_string;
    auto native_slope_getter = JS::NativeFunction::create(realm, slope_getter, 0, slope_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_slope_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto slope_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(slope_id, native_slope_getter, native_slope_setter, slope_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 intercept_id = "intercept"_utf16_fly_string;
    auto native_intercept_getter = JS::NativeFunction::create(realm, intercept_getter, 0, intercept_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_intercept_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto intercept_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(intercept_id, native_intercept_getter, native_intercept_setter, intercept_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 amplitude_id = "amplitude"_utf16_fly_string;
    auto native_amplitude_getter = JS::NativeFunction::create(realm, amplitude_getter, 0, amplitude_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_amplitude_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto amplitude_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(amplitude_id, native_amplitude_getter, native_amplitude_setter, amplitude_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 exponent_id = "exponent"_utf16_fly_string;
    auto native_exponent_getter = JS::NativeFunction::create(realm, exponent_getter, 0, exponent_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_exponent_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto exponent_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(exponent_id, native_exponent_getter, native_exponent_setter, exponent_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_id = "offset"_utf16_fly_string;
    auto native_offset_getter = JS::NativeFunction::create(realm, offset_getter, 0, offset_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_offset_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto offset_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_id, native_offset_getter, native_offset_setter, offset_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.

    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(0)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(1)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_TABLE"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(2)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_DISCRETE"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(3)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_LINEAR"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(4)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("SVG_FECOMPONENTTRANSFER_TYPE_GAMMA"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(5)), JS::Attribute::Enumerable);
    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "SVGComponentTransferFunctionElement"_utf16), JS::Attribute::Configurable);
}

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<SVG::SVGComponentTransferFunctionElement*> 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(SVGComponentTransferFunctionElementPrototype::type_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::type_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->type(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::table_values_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::table_values_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->table_values(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::slope_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::slope_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->slope(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::intercept_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::intercept_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->intercept(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::amplitude_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::amplitude_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->amplitude(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::exponent_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::exponent_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->exponent(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGComponentTransferFunctionElementPrototype::offset_getter)
{
    WebIDL::log_trace(vm, "SVGComponentTransferFunctionElementPrototype::offset_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(); }));

    return JS::Value(R);
}

} // namespace Web::Bindings
