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

namespace Web::Bindings {

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

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

void ValidityStatePrototype::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 value_missing_id = "valueMissing"_utf16_fly_string;
    auto native_value_missing_getter = JS::NativeFunction::create(realm, value_missing_getter, 0, value_missing_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_value_missing_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto value_missing_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(value_missing_id, native_value_missing_getter, native_value_missing_setter, value_missing_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 type_mismatch_id = "typeMismatch"_utf16_fly_string;
    auto native_type_mismatch_getter = JS::NativeFunction::create(realm, type_mismatch_getter, 0, type_mismatch_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_type_mismatch_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto type_mismatch_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_mismatch_id, native_type_mismatch_getter, native_type_mismatch_setter, type_mismatch_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 pattern_mismatch_id = "patternMismatch"_utf16_fly_string;
    auto native_pattern_mismatch_getter = JS::NativeFunction::create(realm, pattern_mismatch_getter, 0, pattern_mismatch_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_pattern_mismatch_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto pattern_mismatch_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(pattern_mismatch_id, native_pattern_mismatch_getter, native_pattern_mismatch_setter, pattern_mismatch_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 too_long_id = "tooLong"_utf16_fly_string;
    auto native_too_long_getter = JS::NativeFunction::create(realm, too_long_getter, 0, too_long_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_too_long_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto too_long_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(too_long_id, native_too_long_getter, native_too_long_setter, too_long_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 too_short_id = "tooShort"_utf16_fly_string;
    auto native_too_short_getter = JS::NativeFunction::create(realm, too_short_getter, 0, too_short_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_too_short_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto too_short_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(too_short_id, native_too_short_getter, native_too_short_setter, too_short_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 range_underflow_id = "rangeUnderflow"_utf16_fly_string;
    auto native_range_underflow_getter = JS::NativeFunction::create(realm, range_underflow_getter, 0, range_underflow_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_range_underflow_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto range_underflow_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(range_underflow_id, native_range_underflow_getter, native_range_underflow_setter, range_underflow_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 range_overflow_id = "rangeOverflow"_utf16_fly_string;
    auto native_range_overflow_getter = JS::NativeFunction::create(realm, range_overflow_getter, 0, range_overflow_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_range_overflow_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto range_overflow_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(range_overflow_id, native_range_overflow_getter, native_range_overflow_setter, range_overflow_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 step_mismatch_id = "stepMismatch"_utf16_fly_string;
    auto native_step_mismatch_getter = JS::NativeFunction::create(realm, step_mismatch_getter, 0, step_mismatch_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_step_mismatch_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto step_mismatch_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(step_mismatch_id, native_step_mismatch_getter, native_step_mismatch_setter, step_mismatch_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 bad_input_id = "badInput"_utf16_fly_string;
    auto native_bad_input_getter = JS::NativeFunction::create(realm, bad_input_getter, 0, bad_input_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_bad_input_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto bad_input_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(bad_input_id, native_bad_input_getter, native_bad_input_setter, bad_input_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 custom_error_id = "customError"_utf16_fly_string;
    auto native_custom_error_getter = JS::NativeFunction::create(realm, custom_error_getter, 0, custom_error_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_custom_error_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto custom_error_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(custom_error_id, native_custom_error_getter, native_custom_error_setter, custom_error_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 valid_id = "valid"_utf16_fly_string;
    auto native_valid_getter = JS::NativeFunction::create(realm, valid_getter, 0, valid_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_valid_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto valid_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(valid_id, native_valid_getter, native_valid_setter, valid_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, "ValidityState"_utf16), JS::Attribute::Configurable);
}

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<HTML::ValidityState*> 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(ValidityStatePrototype::value_missing_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::value_missing_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->value_missing(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::type_mismatch_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::type_mismatch_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_mismatch(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::pattern_mismatch_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::pattern_mismatch_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->pattern_mismatch(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::too_long_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::too_long_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->too_long(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::too_short_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::too_short_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->too_short(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::range_underflow_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::range_underflow_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->range_underflow(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::range_overflow_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::range_overflow_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->range_overflow(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::step_mismatch_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::step_mismatch_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->step_mismatch(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::bad_input_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::bad_input_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->bad_input(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::custom_error_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::custom_error_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->custom_error(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(ValidityStatePrototype::valid_getter)
{
    WebIDL::log_trace(vm, "ValidityStatePrototype::valid_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->valid(); }));

    return JS::Value(R);
}

} // namespace Web::Bindings
