export async function fetchGoogleCalendarEvents(token, startDate, endDate) {
  console.log(token);
  const response = await fetch(
    `https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}&time_max=${endDate}`,
    {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    },
  );

  const data = await response.json();
  console.log(data);
  const googleEvents = [];
  data.items?.forEach((item) => {
    let isAllDay = false;
    const start = item.start;
    let startDateTime;
    if (start !== undefined) {
      if (start.dateTime) {
        const stringDate = start.dateTime;
        startDateTime = new Date(stringDate);
      } else {
        const stringDate = start.date;
        startDateTime = new Date(stringDate);
        isAllDay = true;
      }
    }

    const end = item.end;
    let endDateTime;
    if (end !== undefined) {
      if (end.dateTime) {
        const stringDate = end.dateTime;
        endDateTime = new Date(stringDate);
      } else {
        const stringDate = end.date;
        endDateTime = new Date(stringDate);
      }
    }

    const googleEvent = {
      id: item.id,
      title: item.summary,
      startDate: startDateTime,
      endDate: endDateTime,
      allDay: isAllDay,
    };
    googleEvents.push(googleEvent);
  });

  return googleEvents;
}
