#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/MutationRecord.h>
#include <LibWeb/DOM/MutationRecord.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/NodeList.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Tracing.h>

namespace Web::Bindings {

void MutationRecordConstructor::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, "MutationRecord"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<MutationRecordPrototype>(realm, "MutationRecord"_fly_string), 0);
}

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

void MutationRecordPrototype::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 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 target_id = "target"_utf16_fly_string;
    auto native_target_getter = JS::NativeFunction::create(realm, target_getter, 0, target_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_target_setter;

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

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

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(target_id, native_target_getter, native_target_setter, target_attributes);

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

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto added_nodes_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(added_nodes_id, native_added_nodes_getter, native_added_nodes_setter, added_nodes_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 removed_nodes_id = "removedNodes"_utf16_fly_string;
    auto native_removed_nodes_getter = JS::NativeFunction::create(realm, removed_nodes_getter, 0, removed_nodes_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_removed_nodes_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto removed_nodes_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(removed_nodes_id, native_removed_nodes_getter, native_removed_nodes_setter, removed_nodes_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 previous_sibling_id = "previousSibling"_utf16_fly_string;
    auto native_previous_sibling_getter = JS::NativeFunction::create(realm, previous_sibling_getter, 0, previous_sibling_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_previous_sibling_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto previous_sibling_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(previous_sibling_id, native_previous_sibling_getter, native_previous_sibling_setter, previous_sibling_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 next_sibling_id = "nextSibling"_utf16_fly_string;
    auto native_next_sibling_getter = JS::NativeFunction::create(realm, next_sibling_getter, 0, next_sibling_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_next_sibling_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto next_sibling_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(next_sibling_id, native_next_sibling_getter, native_next_sibling_setter, next_sibling_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 attribute_name_id = "attributeName"_utf16_fly_string;
    auto native_attribute_name_getter = JS::NativeFunction::create(realm, attribute_name_getter, 0, attribute_name_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_attribute_name_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto attribute_name_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(attribute_name_id, native_attribute_name_getter, native_attribute_name_setter, attribute_name_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 attribute_namespace_id = "attributeNamespace"_utf16_fly_string;
    auto native_attribute_namespace_getter = JS::NativeFunction::create(realm, attribute_namespace_getter, 0, attribute_namespace_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_attribute_namespace_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto attribute_namespace_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(attribute_namespace_id, native_attribute_namespace_getter, native_attribute_namespace_setter, attribute_namespace_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 old_value_id = "oldValue"_utf16_fly_string;
    auto native_old_value_getter = JS::NativeFunction::create(realm, old_value_getter, 0, old_value_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_old_value_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto old_value_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(old_value_id, native_old_value_getter, native_old_value_setter, old_value_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, "MutationRecord"_utf16), JS::Attribute::Configurable);
}

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<DOM::MutationRecord*> 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(MutationRecordPrototype::type_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::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 WebIDL::primitive_string_from_string(vm, R);
}

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

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


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

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MutationRecordPrototype::added_nodes_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::added_nodes_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->added_nodes(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MutationRecordPrototype::removed_nodes_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::removed_nodes_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->removed_nodes(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(MutationRecordPrototype::previous_sibling_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::previous_sibling_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->previous_sibling(); }));

    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(MutationRecordPrototype::next_sibling_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::next_sibling_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->next_sibling(); }));

    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(MutationRecordPrototype::attribute_name_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::attribute_name_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->attribute_name(); }));

    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R.has_value())
            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(WebIDL::primitive_string_from_string(vm, R.value()));
    }();
}

JS_DEFINE_NATIVE_FUNCTION(MutationRecordPrototype::attribute_namespace_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::attribute_namespace_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->attribute_namespace(); }));

    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R.has_value())
            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(WebIDL::primitive_string_from_string(vm, R.value()));
    }();
}

JS_DEFINE_NATIVE_FUNCTION(MutationRecordPrototype::old_value_getter)
{
    WebIDL::log_trace(vm, "MutationRecordPrototype::old_value_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->old_value(); }));

    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R.has_value())
            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(WebIDL::primitive_string_from_string(vm, R.value()));
    }();
}

} // namespace Web::Bindings
