/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-floating-promises */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function displayImagesAndEventsMap(id, display, dataNodeId) {
  await loadScript('/map/map.js');
  return await displayCommonMap('map_' + id, display, async (position) => {
    let popup;
    if (position.popupHref) {
      popup = await loadPopup(position.popupHref);
    } else {
      popup = L.popup().setLatLng(position.position).setContent(`
  <a onclick="imagesAndEventsMapShowImage(document, '${id}');imagesAndEventsMapShowImg(document, '${id}', ${position.index})">
   <img src="${position.src}">
   </a>
 `);
      document.querySelector(`#image_${id}_${position.index}`)._popup = popup;
    }
    return popup;
  });
}

/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable no-undef */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function imagesAndEventsMapShowImg(document, dataNodeId, imageIndex) {
  const images = document.querySelectorAll(
    '#span-imag_' + dataNodeId + ' .span-img-hide',
  );
  if (0 > imageIndex) {
    imageIndex = images.length - 1;
  }
  if (imageIndex >= images.length) {
    imageIndex = 0;
  }
  document
    .querySelector('#cardImgGal' + dataNodeId)
    .setAttribute('data-imagesAndEventsMap-modalIndex', '' + imageIndex);
  document.querySelector(
    '#cardImgGal' + dataNodeId + ' .card-title-text',
  ).innerHTML = images[imageIndex].getAttribute(
    'data-imagesAndEventsMap-modalTitle',
  );
  document.querySelector('#cardImgGal' + dataNodeId + ' .imgFullScreen').style[
    'background-image'
  ] = images[imageIndex].getAttribute('data-imagesAndEventsMap-imgBackground');

  document
    .querySelector('#cardImgGal' + dataNodeId + ' .switch-map-image')
    .classList.remove('d-none');

  if (
    document
      .querySelector('#cardImgGal' + dataNodeId + ' .mapFullScreen')
      .classList.contains('card-map-hidden')
  ) {
    imagesAndEventsMapShowImage(document, dataNodeId);
  } else {
    imagesAndEventsMapShowMap(document, dataNodeId);
  }
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function imagesAndEventsMapShowMap(document, dataNodeId) {
  document
    .querySelector('#cardImgGal' + dataNodeId + ' .mapFullScreen')
    .classList.remove('card-map-hidden');
  document
    .querySelectorAll('#cardImgGal' + dataNodeId + ' .imageDisplay')
    .forEach((elt) => elt.classList.add('d-none'));
  document.querySelector('#cardImgGal' + dataNodeId)._map.invalidateSize();

  const popup = document.querySelector(
    `#image_${dataNodeId}_${document
      .querySelector('#cardImgGal' + dataNodeId)
      .getAttribute('data-imagesAndEventsMap-modalIndex')}`,
  )._popup;
  const map = document.querySelector('#cardImgGal' + dataNodeId)._map;
  if (popup) {
    map.setView(popup.getLatLng());
    map.openPopup(popup);
  }
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function imagesAndEventsMapShowImage(document, dataNodeId) {
  document
    .querySelector('#cardImgGal' + dataNodeId + ' .mapFullScreen')
    .classList.add('card-map-hidden');
  document
    .querySelectorAll('#cardImgGal' + dataNodeId + ' .imageDisplay')
    .forEach((elt) => elt.classList.remove('d-none'));
}
