#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/NavigationType.h>

namespace Web::Bindings {

// https://webidl.spec.whatwg.org/#idl-enumeration
JS::ThrowCompletionOr<NavigationType> convert_to_idl_value_for_navigation_type(JS::VM& vm, JS::Value value)
{
    // 1. Let S be the result of calling ? ToString(V).
    auto value_as_string = TRY(value.to_utf16_string(vm));

    // 2. If S is not one of E’s enumeration values, then throw a TypeError.
    // 3. Return the enumeration value of type E that is equal to S.
    if (value_as_string == "push"sv)
        return NavigationType::Push;
    if (value_as_string == "replace"sv)
        return NavigationType::Replace;
    if (value_as_string == "reload"sv)
        return NavigationType::Reload;
    if (value_as_string == "traverse"sv)
        return NavigationType::Traverse;
    return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidEnumerationValue, value_as_string, "NavigationType");
}

// https://webidl.spec.whatwg.org/#idl-enumeration
Utf16String idl_enum_to_string(NavigationType value)
{
    // The result of converting an IDL enumeration type value to a JavaScript value is the String value that represents the same sequence of code units as the enumeration value.
    switch (value) {
    case NavigationType::Push:
        return "push"_utf16;
    case NavigationType::Replace:
        return "replace"_utf16;
    case NavigationType::Reload:
        return "reload"_utf16;
    case NavigationType::Traverse:
        return "traverse"_utf16;
    }
    VERIFY_NOT_REACHED();
}

} // namespace Web::Bindings
