async function getCalendar(icalUrl) {
  const response = await fetch(icalUrl);
  const data = await response.text();
  return data;
}

function getDateFromjCal(jCal, event, name) {
  const date = event.getFirstPropertyValue(name);
  const vtimezone = jCal.getFirstSubcomponent('vtimezone');
  if (vtimezone && moment(date.toJSDate()).utcOffset()) {
    date.zone = new ICAL.Timezone(vtimezone);
  }
  var locale = window.navigator.userLanguage || window.navigator.language;
  moment.locale(locale);
  return moment.parseZone(date.toJSDate()).utcOffset(date.utcOffset() / 60)
};

function insertCalendarEvents(dlElement, icalUrl, maxAgeDays = 1) {

  if (!icalUrl) {
    return;
  }

  getCalendar(icalUrl).then(data => {
    // console.log(data);
    var jcalData = ICAL.parse(data);
    var vcalendar = new ICAL.Component(jcalData);
    ICAL.helpers.updateTimezones(vcalendar, ICAL.TimezoneService);
    const element = dlElement;

    var events = vcalendar.getAllSubcomponents('vevent');
    var eventArray = [];
    var eventKeys = [];
    var now = moment();
    events.forEach(event => {
      var start = getDateFromjCal(vcalendar, event, 'dtstart');
      if (now.diff(start, 'days') <= maxAgeDays) {
        var key = event.getFirstPropertyValue("dtstart") + "-" + event.getFirstPropertyValue("dtend");
        eventArray[key] = event;
        eventKeys.push(key);
      }
    });
    dlElement.replaceChildren();
    if (eventKeys.length == 0) {
      const dd = document.createElement('dd');
      dd.appendChild(document.createTextNode("–"));
      element.appendChild(dd);
      return;
    }
    eventKeys.sort().reverse().forEach(key => {
      var event = eventArray[key];
      var summary = event.getFirstPropertyValue('summary');
      var start = getDateFromjCal(vcalendar, event, 'dtstart');
      var end = getDateFromjCal(vcalendar, event, 'dtend');
      const dt = document.createElement('dt');
      const time = document.createElement('time');
      dt.setAttribute('class', 'maintenance');
      const node = document.createTextNode(start.format('dddd, LL'));
      time.appendChild(node);
      time.setAttribute('datetime', start.format('YYYY-MM-DD'));
      dt.appendChild(time);
      element.appendChild(dt);
      const dd = document.createElement('dd');
      const description = document.createTextNode(start.format("HH:mm") + "-" + end.format("HH:mm") + ` ${summary}`);
      dd.appendChild(description);
      element.appendChild(dd);
    });
  }
  ).catch(error => {
    console.log(error);
  });
}
