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

namespace Web::Bindings {

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

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

void WorkerLocationPrototype::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 href_id = "href"_utf16_fly_string;
    auto native_href_getter = JS::NativeFunction::create(realm, href_getter, 0, href_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_href_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto href_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(href_id, native_href_getter, native_href_setter, href_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 origin_id = "origin"_utf16_fly_string;
    auto native_origin_getter = JS::NativeFunction::create(realm, origin_getter, 0, origin_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_origin_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto origin_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(origin_id, native_origin_getter, native_origin_setter, origin_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 protocol_id = "protocol"_utf16_fly_string;
    auto native_protocol_getter = JS::NativeFunction::create(realm, protocol_getter, 0, protocol_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_protocol_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto protocol_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(protocol_id, native_protocol_getter, native_protocol_setter, protocol_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 host_id = "host"_utf16_fly_string;
    auto native_host_getter = JS::NativeFunction::create(realm, host_getter, 0, host_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_host_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto host_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(host_id, native_host_getter, native_host_setter, host_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 hostname_id = "hostname"_utf16_fly_string;
    auto native_hostname_getter = JS::NativeFunction::create(realm, hostname_getter, 0, hostname_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_hostname_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto hostname_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(hostname_id, native_hostname_getter, native_hostname_setter, hostname_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 port_id = "port"_utf16_fly_string;
    auto native_port_getter = JS::NativeFunction::create(realm, port_getter, 0, port_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_port_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto port_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(port_id, native_port_getter, native_port_setter, port_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 pathname_id = "pathname"_utf16_fly_string;
    auto native_pathname_getter = JS::NativeFunction::create(realm, pathname_getter, 0, pathname_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_pathname_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto pathname_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(pathname_id, native_pathname_getter, native_pathname_setter, pathname_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 search_id = "search"_utf16_fly_string;
    auto native_search_getter = JS::NativeFunction::create(realm, search_getter, 0, search_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_search_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto search_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(search_id, native_search_getter, native_search_setter, search_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 hash_id = "hash"_utf16_fly_string;
    auto native_hash_getter = JS::NativeFunction::create(realm, hash_getter, 0, hash_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_hash_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto hash_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(hash_id, native_hash_getter, native_hash_setter, hash_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, "toString"_utf16_fly_string, to_string, 0, default_attributes);


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

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<HTML::WorkerLocation*> 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(WorkerLocationPrototype::href_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::href_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->href(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::origin_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::origin_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->origin(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::protocol_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::protocol_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->protocol(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::host_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::host_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->host(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::hostname_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::hostname_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->hostname(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::port_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::port_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->port(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::pathname_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::pathname_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->pathname(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::search_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::search_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->search(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::hash_getter)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::hash_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->hash(); }));

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

JS_DEFINE_NATIVE_FUNCTION(WorkerLocationPrototype::to_string)
{
    WebIDL::log_trace(vm, "WorkerLocationPrototype::to_string");
    auto* idl_object = TRY(impl_from(vm));
    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->href(); }));
    return WebIDL::primitive_string_from_string(vm, R);
}

} // namespace Web::Bindings
