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

namespace Web::Bindings {

// https://webidl.spec.whatwg.org/#idl-enumeration
JS::ThrowCompletionOr<CanvasFillRule> convert_to_idl_value_for_canvas_fill_rule(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 == "nonzero"sv)
        return CanvasFillRule::Nonzero;
    if (value_as_string == "evenodd"sv)
        return CanvasFillRule::Evenodd;
    return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidEnumerationValue, value_as_string, "CanvasFillRule");
}

// https://webidl.spec.whatwg.org/#idl-enumeration
Utf16String idl_enum_to_string(CanvasFillRule 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 CanvasFillRule::Nonzero:
        return "nonzero"_utf16;
    case CanvasFillRule::Evenodd:
        return "evenodd"_utf16;
    }
    VERIFY_NOT_REACHED();
}

} // namespace Web::Bindings
