// /utils/polylines/contextMenu.js
export function closePolylineSelectionAndContextMenu(map) {
  try {
    if (window.selectedPolyline) {
      window.selectedPolyline.setStyle({ weight: 3 });
      window.selectedPolyline = null;
    }

    if (map?.contextmenu?.hide) {
      map.contextmenu.hide();
    }
  } catch (error) {
    console.error("Fehler beim Schließen des Kontextmenüs:", error);
  }

  localStorage.removeItem("contextMenuCountdown");
  localStorage.removeItem("contextMenuExpired");
}

export function monitorContextMenu(map) {
  function checkAndClose() {
    if (localStorage.getItem("contextMenuExpired") === "true") {
      closePolylineSelectionAndContextMenu(map);
      localStorage.removeItem("contextMenuExpired");
    }
    setTimeout(checkAndClose, 1000);
  }
  checkAndClose();
}
