#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/NavigationDestination.h>
#include <LibWeb/HTML/NavigationDestination.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

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

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

void NavigationDestinationPrototype::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 url_id = "url"_utf16_fly_string;
    auto native_url_getter = JS::NativeFunction::create(realm, url_getter, 0, url_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_url_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto url_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(url_id, native_url_getter, native_url_setter, url_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 key_id = "key"_utf16_fly_string;
    auto native_key_getter = JS::NativeFunction::create(realm, key_getter, 0, key_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_key_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto key_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(key_id, native_key_getter, native_key_setter, key_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 id_id = "id"_utf16_fly_string;
    auto native_id_getter = JS::NativeFunction::create(realm, id_getter, 0, id_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_id_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto id_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(id_id, native_id_getter, native_id_setter, id_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 index_id = "index"_utf16_fly_string;
    auto native_index_getter = JS::NativeFunction::create(realm, index_getter, 0, index_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_index_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto index_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(index_id, native_index_getter, native_index_setter, index_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 same_document_id = "sameDocument"_utf16_fly_string;
    auto native_same_document_getter = JS::NativeFunction::create(realm, same_document_getter, 0, same_document_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_same_document_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto same_document_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(same_document_id, native_same_document_getter, native_same_document_setter, same_document_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, "getState"_utf16_fly_string, get_state, 0, default_attributes);

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

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<HTML::NavigationDestination*> 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(NavigationDestinationPrototype::url_getter)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::url_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->url(); }));

    return WebIDL::primitive_string_from_string(vm, R);
}

JS_DEFINE_NATIVE_FUNCTION(NavigationDestinationPrototype::key_getter)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::key_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->key(); }));

    return WebIDL::primitive_string_from_string(vm, R);
}

JS_DEFINE_NATIVE_FUNCTION(NavigationDestinationPrototype::id_getter)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::id_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->id(); }));

    return WebIDL::primitive_string_from_string(vm, R);
}

JS_DEFINE_NATIVE_FUNCTION(NavigationDestinationPrototype::index_getter)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::index_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->index(); }));

    return JS::Value(static_cast<double>(R));
}

JS_DEFINE_NATIVE_FUNCTION(NavigationDestinationPrototype::same_document_getter)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::same_document_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->same_document(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(NavigationDestinationPrototype::get_state)
{
    WebIDL::log_trace(vm, "NavigationDestinationPrototype::get_state");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] HTML::NavigationDestination* idl_object = TRY(impl_from(vm));

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

} // namespace Web::Bindings
