import { useRef as C, useLayoutEffect as d } from "react";
import { isEmpty as g } from "es-toolkit/compat";
import { noop as b } from "es-toolkit/function";
import { useMapContext as M } from "../MapContext.js";
const L = "H_context_menu_item", T = (
  // eslint-disable-next-line max-len
  '<svg aria-labelledby="ggtrwic-aria" role="img" height="14" width="135px" class="ContentLoader  use-mix-blend-mode"><title id="ggtrwic-aria">Loading...</title><rect role="presentation" x="0" y="0" width="100%" height="100%" clip-path="url(#ggtrwic-diff)" style="fill: url(&quot;#ggtrwic-animated-diff&quot;);"></rect><defs><clipPath id="ggtrwic-diff"><rect width="100%" height="100%" rx="3" ry="3"></rect></clipPath><linearGradient id="ggtrwic-animated-diff"><stop offset="0%" stop-color="#f5f6f7" stop-opacity="1"><animate attributeName="offset" values="-2; -2; 1" keyTimes="0; 0.25; 1" dur="2s" repeatCount="indefinite"></animate></stop><stop offset="50%" stop-color="#aaaaaa" stop-opacity="1"><animate attributeName="offset" values="-1; -1; 2" keyTimes="0; 0.25; 1" dur="2s" repeatCount="indefinite"></animate></stop><stop offset="100%" stop-color="#f5f6f7" stop-opacity="1"><animate attributeName="offset" values="0; 0; 3" keyTimes="0; 0.25; 1" dur="2s" repeatCount="indefinite"></animate></stop></linearGradient></defs></svg>'
), h = () => [...document.getElementsByClassName(L)], w = (n, s) => {
  Array.from(n).forEach((o, a) => {
    const r = s[a];
    if (!r)
      return;
    const { hasSpinner: f, className: c, labelClassName: t = "", icon: i, label: p } = r.props, e = `<span class="${t}">${p}</span>`, l = `<span class="rioglyph ${i} margin-right-5"></span>${e}`, u = i ? l : e;
    f ? o.innerHTML = T : o.innerHTML = u;
    const x = o.classList;
    c && c.split(" ").map((E) => x.add(E));
  });
}, y = (n, s, o, a) => {
  const r = n.length - 1, f = s.viewportX, c = a || o.screenToGeo(f, s.viewportY);
  n.forEach((t, i) => {
    if (!t)
      return;
    const { label: p, disabled: e, callback: l = b } = t.props;
    s.items.push(
      new H.util.ContextItem({
        label: p,
        disabled: e,
        callback: () => l(c)
      })
    ), i < r && s.items.push(H.util.ContextItem.SEPARATOR);
  });
}, v = (n) => n.type === "contextmenu", m = (n, s) => n.target === s || n.target?.data?.source_name?.startsWith("H-"), P = (n) => {
  const { onOpen: s = b, menuItems: o, contextMenuEvent: a, targetPosition: r } = n, { api: f, baseLayer: c } = M(), { map: t } = f, i = C();
  i.current = r, d(() => {
    if (o && t) {
      const e = g(a?.items);
      a && !m(a, t) && e && y(o, a, t, i.current), w(h(), o);
    }
  }, [o, a, i.current]);
  const p = (e) => {
    if (!v(e) || !t)
      return;
    m(e, t) && (i.current = void 0);
    const l = e.viewportX, u = i.current || t?.screenToGeo(l, e.viewportY);
    u && s(u), m(e, t) && o && g(e.items) && (y(o, e, t, r), w(h(), o));
  };
  return d(() => {
    if (t)
      return t.addEventListener("contextmenu", p), () => {
        t.removeEventListener("contextmenu", p);
      };
  }, [c, t]), null;
};
export {
  P as default,
  v as isContextMenuEvent,
  m as isMapTarget
};
//# sourceMappingURL=ContextMenu.js.map
