// utils/polylines/monitorContextMenu.js
import { closePolylineSelectionAndContextMenu } from "./contextMenu";

export function monitorContextMenu(map) {
  function checkAndClose() {
    const isContextMenuExpired = localStorage.getItem("contextMenuExpired") === "true";

    if (isContextMenuExpired) {
      if (map && map.contextmenu && typeof map.contextmenu.hide === "function") {
        closePolylineSelectionAndContextMenu(map);
        localStorage.removeItem("contextMenuExpired");
      } else {
        console.warn("Kontextmenü war nicht verfügbar und konnte nicht geschlossen werden.");
      }
    }

    setTimeout(checkAndClose, 1000); // **Recursive Timeout statt Intervall**
  }

  checkAndClose();
}
