import dayjs from 'dayjs' ; import { CalendarEvent , MeetingType } from './types' ; /**
 * Helper for lazily importing the Google JavaScript API.
 */ async function importGapi ( ) { await new Promise ( ( resolve ) => { const script = document . createElement ( 'script' ) ; script . src = 'https://apis.google.com/js/api.js' ; document . body . appendChild ( script ) ; script . onload = resolve ; } ) ; } /**
 * Loads the Google API client.
 */ async function loadClient ( ) { await new Promise ( ( resolve ) => gapi . load ( 'client' , resolve ) ) ; } let isApiLoaded = false ; param apiKey export async function maybeLoadCalendarAPI ( apiKey ) { if ( isApiLoaded ) { return ; } await importGapi ( ) ; await loadClient ( ) ; await gapi . client . init ( { apiKey } ) ; await gapi . client . load ( 'https://content.googleapis.com/discovery/v1/apis/calendar/v3/rest' , 'v3' ) ; isApiLoaded = true ; } param calendarId param activeStartDate returns export async function fetchEvents ( calendarId , activeStartDate ) { const { result } = await gapi . client . calendar . events . list ( { calendarId , singleEvents : true , timeMin : activeStartDate . startOf ( 'month' ) . subtract ( 1 , 'week' ) . toISOString ( ) , timeMax : activeStartDate . endOf ( 'month' ) . add ( 1 , 'week' ) . toISOString ( ) , } ) ; const events = [ ] ; const visitedDates = new Set ( ) ; result . items . filter ( ( event ) => event . status === 'confirmed' ) . forEach ( ( event ) => { const title = event . summary ; let type ; if ( title . includes ( 'community meeting' ) ) { type = 'community' ; } else if ( title . includes ( 'working group' ) ) { type = 'workingGroup' ; } else { type = 'other' ; } const start = dayjs ( event . start . dateTime ?? event . start . date ?? '' ) ; const end = dayjs ( event . end . dateTime ?? event . end . date ?? '' ) ; const dateStr = start . toString ( ) ; if ( visitedDates . has ( dateStr ) ) { return ; } visitedDates . add ( dateStr ) ; events . push ( { calendarId , end , start , title , type , description : event . description , htmlLink : event . htmlLink , location : event . location ?? '' , recurringEventId : event . recurringEventId , } ) ; } ) ; return events ; } param calendarId param eventId returns export async function fetchEvent ( calendarId , eventId ) { const { result } = await gapi . client . calendar . events . get ( { calendarId , eventId , } ) ; return result ; }