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

namespace Web::Bindings {

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

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

void TrustedScriptPrototype::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());
    object.define_native_function(realm, "toJSON"_utf16_fly_string, to_json, 0, default_attributes);

    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, "TrustedScript"_utf16), JS::Attribute::Configurable);
}

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<TrustedTypes::TrustedScript*> 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(TrustedScriptPrototype::to_json)
{
    WebIDL::log_trace(vm, "TrustedScriptPrototype::to_json");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] TrustedTypes::TrustedScript* idl_object = TRY(impl_from(vm));

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

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

} // namespace Web::Bindings
