import { useState } from "react";
import { PendingMembersContext } from "./PendingMembersContext";

import getUserToValidate from "../../utils/users/getUserToValidate";
import getUserById from "../../utils/users/getUserById";

export function PendingMembersProvider({ children }) {

    const [pendingMembers, setPendingMembers] = useState([]);

    const updatePendingMembers = async () => {
        const result = await getUserToValidate();
        const userIds = result.data;

        if (!userIds || userIds.length === 0) {
            setPendingMembers([]);
            return;
        }

        const users = await Promise.all(
            userIds.map(id => getUserById(id))
        );

        setPendingMembers(users.map(u => u.data));
    };

    return (
        <PendingMembersContext.Provider
            value={{
                pendingMembers,
                updatePendingMembers
            }}
        >
            {children}
        </PendingMembersContext.Provider>
    );
}