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

namespace Web::Bindings {

// https://webidl.spec.whatwg.org/#idl-enumeration
JS::ThrowCompletionOr<PopoverTargetActionAttribute> convert_to_idl_value_for_popover_target_action_attribute(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 == "toggle"sv)
        return PopoverTargetActionAttribute::Toggle;
    if (value_as_string == "show"sv)
        return PopoverTargetActionAttribute::Show;
    if (value_as_string == "hide"sv)
        return PopoverTargetActionAttribute::Hide;
    return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidEnumerationValue, value_as_string, "PopoverTargetActionAttribute");
}

// https://webidl.spec.whatwg.org/#idl-enumeration
Utf16String idl_enum_to_string(PopoverTargetActionAttribute 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 PopoverTargetActionAttribute::Toggle:
        return "toggle"_utf16;
    case PopoverTargetActionAttribute::Show:
        return "show"_utf16;
    case PopoverTargetActionAttribute::Hide:
        return "hide"_utf16;
    }
    VERIFY_NOT_REACHED();
}

} // namespace Web::Bindings
