/*
 * Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorHelperPrototype.h>
#include <LibJS/Runtime/Realm.h>

namespace JS {

GC_DEFINE_ALLOCATOR(IteratorHelperPrototype);

// 27.1.2.1 The %IteratorHelperPrototype% Object, https://tc39.es/ecma262/#sec-%iteratorhelperprototype%-object
IteratorHelperPrototype::IteratorHelperPrototype(Realm& realm)
    : PrototypeObject(realm.intrinsics().iterator_prototype())
{
}

void IteratorHelperPrototype::initialize(Realm& realm)
{
    auto& vm = this->vm();
    Base::initialize(realm);

    u8 attr = Attribute::Writable | Attribute::Configurable;
    define_native_function(realm, vm.names.next, next, 0, attr);
    define_native_function(realm, vm.names.return_, return_, 0, attr);

    // 27.1.2.1.3 %IteratorHelperPrototype% [ %Symbol.toStringTag% ], https://tc39.es/ecma262/#sec-%iteratorhelperprototype%-%symbol.tostringtag%
    define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Iterator Helper"_string), Attribute::Configurable);
}

// 27.1.2.1.1 %IteratorHelperPrototype%.next ( ), https://tc39.es/ecma262/#sec-%iteratorhelperprototype%.next
JS_DEFINE_NATIVE_FUNCTION(IteratorHelperPrototype::next)
{
    auto iterator = TRY(typed_this_object(vm));

    // 1. Return ? GeneratorResume(this value, undefined, "Iterator Helper").
    auto iteration_result = TRY(iterator->resume(vm, js_undefined(), "Iterator Helper"sv));
    return create_iterator_result_object(vm, iteration_result.value, iteration_result.done);
}

// 27.1.2.1.2 %IteratorHelperPrototype%.return ( ), https://tc39.es/ecma262/#sec-%iteratorhelperprototype%.return
JS_DEFINE_NATIVE_FUNCTION(IteratorHelperPrototype::return_)
{
    // 1. Let O be this value.
    // 2. Perform ? RequireInternalSlot(O, [[UnderlyingIterators]]).
    auto iterator = TRY(typed_this_object(vm));

    // 3. Assert: O has a [[GeneratorState]] slot.
    // 4. If O.[[GeneratorState]] is suspended-start, then
    if (iterator->generator_state() == GeneratorObject::GeneratorState::SuspendedStart) {
        // a. Set O.[[GeneratorState]] to completed.
        iterator->set_generator_state(GeneratorObject::GeneratorState::Completed);

        // b. NOTE: Once a generator enters the completed state it never leaves it and its associated execution context is never resumed. Any execution state associated with O can be discarded at this point.

        // c. Perform ? IteratorCloseAll(O.[[UnderlyingIterators]], NormalCompletion(UNUSED)).
        TRY(iterator_close_all(vm, iterator->underlying_iterators(), normal_completion(js_undefined())));

        // d. Return CreateIterResultObject(undefined, true).
        return create_iterator_result_object(vm, js_undefined(), true);
    }

    // 5. Let C be Completion { [[Type]]: return, [[Value]]: undefined, [[Target]]: empty }.
    Completion completion { Completion::Type::Return, js_undefined() };

    // 6. Return ? GeneratorResumeAbrupt(O, C, "Iterator Helper").
    auto iteration_result = TRY(iterator->resume_abrupt(vm, move(completion), "Iterator Helper"sv));
    return create_iterator_result_object(vm, iteration_result.value, iteration_result.done);
}

}
