/*import {useState} from 'react';
import {EventContext} from "./EventContext";
import getAllEvents from "../../utils/events/getAllEvents.js";
import createEvent from "../../utils/events/createEvent.js";
import deleteEvent from "../../utils/events/deleteEvent.js";


export function EventProvider({ children }) {

    const [events, setEvents] = useState([]);

    const updateEvent = async () => {
        const result = await getAllEvents();
        setEvents(result.data.data);
    }

    const addEvent = async (name, description, start, end) => {
        await createEvent(name, description, start, end);
        await updateEvent();
    }

    const removeEvent = async (eventId) => {
        const result = await deleteEvent(eventId);
        await updateEvent();
        return result;
    }

    return (
        <EventContext.Provider value={{ events, updateEvent, addEvent, removeEvent }}>
            {children}
        </EventContext.Provider>
    );
}*/

import { useState, ReactNode } from 'react';
import { EventContext, EventGroup, EventContextType } from "./EventContext";
import getAllEvents from "../../utils/events/getAllEvents";
import createEvent from "../../utils/events/createEvent";
import deleteEvent from "../../utils/events/deleteEvent";

interface EventProviderProps {
    children: ReactNode;
}

export function EventProvider({ children }: EventProviderProps) {
    const [events, setEvents] = useState<EventGroup[]>([]);

    const updateEvent = async () => {
        const result = await getAllEvents();
        setEvents(result.data.data);
    };

    const addEvent = async (name: string, description: string, start: string, end: string) => {
        await createEvent(name, description, start, end);
        await updateEvent();
    };

    const removeEvent = async (eventId: number) => {
        await deleteEvent(eventId);
        await updateEvent();
    };

    const contextValue: EventContextType = {
        events,
        updateEvent,
        addEvent,
        removeEvent,
    };

    return (
        <EventContext.Provider value={contextValue}>
            {children}
        </EventContext.Provider>
    );
}