import w, { useRef as k, useEffect as l } from "react";
import O from "react-dom/server";
import { removeEventListenerMap as b, addEventListenerMap as g } from "../../../utils/eventHandling.js";
import { useMapContext as y } from "../../MapContext.js";
const v = (e) => e instanceof H.map.Icon, p = (e) => e ? !v(e) : !1, x = 500, u = /* @__PURE__ */ new Map();
let M = /* @__PURE__ */ new WeakMap(), C = !0;
const S = () => ({
  cacheEnabled: C,
  domIconCreations: 0,
  markupCacheHits: 0,
  markupCacheMisses: 0,
  reactElementCacheHits: 0,
  reactElementCacheMisses: 0,
  uncachedDomIconCreations: 0,
  markerSetups: 0,
  markerDestroys: 0,
  markerIconUpdates: 0,
  markerIconRecreates: 0,
  markerSetIconCalls: 0
}), r = S(), N = () => ({
  ...r,
  cacheEnabled: C
}), W = () => {
  Object.assign(r, S());
}, z = (e) => {
  C = e;
}, G = () => {
  u.clear(), M = /* @__PURE__ */ new WeakMap();
}, j = (e) => {
  const n = e ?? C;
  return r.cacheEnabled = n, n;
}, f = (e) => {
  const n = u.get(e);
  if (n)
    return r.markupCacheHits += 1, u.delete(e), u.set(e, n), n;
  r.markupCacheMisses += 1, r.domIconCreations += 1;
  const o = new H.map.DomIcon(e);
  if (u.set(e, o), u.size > x) {
    const c = u.keys().next().value;
    c !== void 0 && u.delete(c);
  }
  return o;
}, R = (e, n = {}) => {
  if (!e || v(e) || e instanceof H.map.DomIcon)
    return e || "";
  const o = j(n.cacheEnabled);
  if (w.isValidElement(e)) {
    const i = O.renderToStaticMarkup(e);
    if (!o)
      return r.reactElementCacheMisses += 1, r.domIconCreations += 1, r.uncachedDomIconCreations += 1, new H.map.DomIcon(i);
    const a = M.get(e);
    if (a)
      return r.reactElementCacheHits += 1, a;
    r.reactElementCacheMisses += 1;
    const m = f(i);
    return M.set(e, m), m;
  }
  const c = e;
  return o ? f(c) : (r.domIconCreations += 1, r.uncachedDomIconCreations += 1, new H.map.DomIcon(c));
}, D = (e, n, o) => {
  const { position: c, customData: i, icon: a, draggable: m, eventListenerMap: t } = e;
  r.markerSetups += 1;
  const d = p(a) ? H.map.DomMarker : H.map.Marker, s = new d(c, {
    icon: a && R(a, { cacheEnabled: o })
  }), I = n.map;
  return s.setData(i), s.draggable = !!m, I?.addObject(s), I && g(s, t, I), s;
}, E = (e, n) => {
  r.markerDestroys += 1, b(e), n.map?.getObjects().includes(e) && n.map.removeObject(e), e.dispose();
}, K = (e) => {
  const { position: n, customData: o, icon: c, eventListenerMap: i } = e, { api: a, enableMarkerIconCache: m } = y(), t = k(), d = k(p(c));
  return l(() => (t.current = D(e, a, m), d.current = p(c), () => {
    t.current && (E(t.current, a), t.current = void 0);
  }), []), l(() => {
    t.current && t.current.setGeometry(n);
  }, [n, t.current]), l(() => {
    t.current && t.current.setData(o);
  }, [o, t.current]), l(() => {
    if (t.current) {
      const s = b(t.current);
      g(t.current, i, s);
    }
  }, [i, t.current]), l(() => {
    if (!(t.current && c))
      return;
    r.markerIconUpdates += 1;
    const s = p(c);
    if (d.current !== s)
      r.markerIconRecreates += 1, E(t.current, a), t.current = D(e, a, m), d.current = s;
    else {
      const h = R(c, { cacheEnabled: m });
      r.markerSetIconCalls += 1, h && t.current.setIcon(h);
    }
  }, [c, t.current, m]), null;
};
export {
  G as clearMarkerIconCache,
  K as default,
  N as getMarkerDebugStats,
  R as getOrCreateIcon,
  p as isDomMakerNeeded,
  v as isHMapIcon,
  W as resetMarkerDebugStats,
  z as setMarkerIconCacheEnabled
};
//# sourceMappingURL=Marker.js.map
