#pragma once

#include <AK/Optional.h>
#include <AK/Utf16String.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/InterfaceObject.h>
#include <LibWeb/Forward.h>

namespace Web::Bindings {

struct ReadableStreamConstructor {
public:
    static void initialize(JS::Realm&, JS::NativeFunction&);
    static JS::ThrowCompletionOr<GC::Ref<JS::Object>> construct(InterfaceConstructor&, JS::FunctionObject&);

private:
    JS_DECLARE_NATIVE_FUNCTION(from);
};

class ReadableStreamPrototype : public JS::Object {
    JS_OBJECT(ReadableStreamPrototype, JS::Object);
    GC_DECLARE_ALLOCATOR(ReadableStreamPrototype);

public:
    static void define_unforgeable_attributes(JS::Realm&, JS::Object&);

    explicit ReadableStreamPrototype(JS::Realm&);
    virtual void initialize(JS::Realm&) override;
    virtual ~ReadableStreamPrototype() override;

private:
    JS_DECLARE_NATIVE_FUNCTION(locked_getter);
    JS_DECLARE_NATIVE_FUNCTION(cancel);
    JS_DECLARE_NATIVE_FUNCTION(get_reader);
    JS_DECLARE_NATIVE_FUNCTION(pipe_through);
    JS_DECLARE_NATIVE_FUNCTION(pipe_to);
    JS_DECLARE_NATIVE_FUNCTION(tee);
    JS_DECLARE_NATIVE_FUNCTION(values);
};

class ReadableStreamAsyncIteratorPrototype : public JS::Object {
    JS_OBJECT(ReadableStreamAsyncIteratorPrototype, JS::Object);
    GC_DECLARE_ALLOCATOR(ReadableStreamAsyncIteratorPrototype);

public:
    explicit ReadableStreamAsyncIteratorPrototype(JS::Realm&);
    virtual void initialize(JS::Realm&) override;
    virtual ~ReadableStreamAsyncIteratorPrototype() override;

private:
    JS_DECLARE_NATIVE_FUNCTION(next);
    JS_DECLARE_NATIVE_FUNCTION(return_);
};

enum class ReadableStreamReaderMode : u8 {
    Byob,
};

JS::ThrowCompletionOr<ReadableStreamReaderMode> convert_to_idl_value_for_readable_stream_reader_mode(JS::VM&, JS::Value);

Utf16String idl_enum_to_string(ReadableStreamReaderMode);

struct ReadableWritablePair {
    GC::Ref<Streams::ReadableStream> readable;
    GC::Ref<Streams::WritableStream> writable;
};

JS::ThrowCompletionOr<ReadableWritablePair> convert_to_idl_value_for_readable_writable_pair(JS::VM&, JS::Value);

struct StreamPipeOptions {
    bool prevent_abort { false };
    bool prevent_cancel { false };
    bool prevent_close { false };
    GC::Ptr<DOM::AbortSignal> signal {};
};

JS::ThrowCompletionOr<StreamPipeOptions> convert_to_idl_value_for_stream_pipe_options(JS::VM&, JS::Value);

struct ReadableStreamGetReaderOptions {
    Optional<ReadableStreamReaderMode> mode {};
};

JS::ThrowCompletionOr<ReadableStreamGetReaderOptions> convert_to_idl_value_for_readable_stream_get_reader_options(JS::VM&, JS::Value);

struct ReadableStreamIteratorOptions {
    bool prevent_cancel { false };
};

JS::ThrowCompletionOr<ReadableStreamIteratorOptions> convert_to_idl_value_for_readable_stream_iterator_options(JS::VM&, JS::Value);

} // namespace Web::Bindings
