/*
 * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Vector.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Promise.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Function.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>

namespace Web::HTML {

using SessionHistoryTraversalSteps = GC::Function<void(NonnullRefPtr<Core::Promise<Empty>>)>;

struct SessionHistoryTraversalQueueEntry : public JS::Cell {
    GC_CELL(SessionHistoryTraversalQueueEntry, JS::Cell);
    GC_DECLARE_ALLOCATOR(SessionHistoryTraversalQueueEntry);

public:
    static GC::Ref<SessionHistoryTraversalQueueEntry> create(JS::VM& vm, GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<HTML::Navigable> target_navigable);

    GC::Ptr<HTML::Navigable> target_navigable() const { return m_target_navigable; }
    void execute_steps(NonnullRefPtr<Core::Promise<Empty>> promise) const { m_steps->function()(move(promise)); }

private:
    SessionHistoryTraversalQueueEntry(GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<HTML::Navigable> target_navigable)
        : m_steps(steps)
        , m_target_navigable(target_navigable)
    {
    }

    virtual void visit_edges(Cell::Visitor&) override;

    GC::Ref<SessionHistoryTraversalSteps> m_steps;
    GC::Ptr<HTML::Navigable> m_target_navigable;
};

// https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
class WEB_API SessionHistoryTraversalQueue : public JS::Cell {
    GC_CELL(SessionHistoryTraversalQueue, JS::Cell);
    GC_DECLARE_ALLOCATOR(SessionHistoryTraversalQueue);

public:
    SessionHistoryTraversalQueue();

    void append(GC::Ref<SessionHistoryTraversalSteps> steps);
    void append_sync(GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<Navigable> target_navigable);

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigations-jump-queue
    GC::Ptr<SessionHistoryTraversalQueueEntry> first_synchronous_navigation_steps_with_target_navigable_not_contained_in(HashTable<GC::Ref<Navigable>> const&);

private:
    virtual void visit_edges(Cell::Visitor&) override;

    void process_queue();
    void schedule_processing();

    Vector<GC::Ref<SessionHistoryTraversalQueueEntry>> m_queue;
    bool m_processing_scheduled { false };
    RefPtr<Core::Promise<Empty>> m_current_promise;
};

}
