import { useState as v, useRef as y, useMemo as C, useEffect as E } from "react";
import { isNil as S } from "es-toolkit/predicate";
import { useMapContext as R } from "../../MapContext.js";
import { useUpdateEventListenerMap as h } from "../../../utils/hooks.js";
import { EventUtils as a } from "../../../utils/eventHandling.js";
const T = (e) => {
  const p = new H.geo.LineString();
  return e.forEach((r) => {
    S(r.lat) || S(r.lng) || Number.isNaN(r.lat) || Number.isNaN(r.lng) || p.pushLatLngAlt(r.lat, r.lng, 0);
  }), new H.geo.Polygon(p);
}, L = (e) => {
  if (e)
    return e instanceof H.map.SpatialStyle ? {
      strokeColor: e.strokeColor,
      fillColor: e.fillColor,
      lineWidth: e.lineWidth,
      lineCap: e.lineCap,
      lineHeadCap: e.lineHeadCap,
      lineTailCap: e.lineTailCap,
      lineJoin: e.lineJoin,
      miterLimit: e.miterLimit,
      lineDash: e.lineDash,
      lineDashOffset: e.lineDashOffset
    } : e;
}, M = (e) => {
  const { points: p, style: r = H.map.SpatialStyle.DEFAULT_STYLE, hoverStyle: f, eventListenerMap: o, cursor: g } = e, { api: i } = R(), [n, P] = v(), m = y(!1), l = C(() => {
    if (g)
      return g;
    if (o?.[a.TAP] || o?.[a.DBL_TAP] || o?.[a.CONTEXTMENU])
      return "pointer";
  }, [g, o]), s = C(() => {
    if (f)
      return new H.map.SpatialStyle({
        ...L(r),
        ...L(f)
      });
  }, [f, r]), u = (t, N) => {
    t.setStyle(N && s ? s : r);
  }, c = (t) => {
    if (!i.map)
      return;
    const N = i.map.getElement(), O = i.map.getViewPort().element;
    N.style.cursor = t, O.style.cursor = t;
  };
  E(() => {
    const t = new H.map.Polygon(T(p), { style: r });
    return i.map?.addObject(t), P(t), () => {
      i.map?.getObjects().includes(t) && i.map?.removeObject(t), t.dispose();
    };
  }, [i.map]), E(() => {
    n && n.setGeometry(T(p));
  }, [p]), E(() => {
    n && u(n, m.current);
  }, [n, r, s]), E(
    () => () => {
      l && c("");
    },
    [i.map, l]
  );
  const d = C(() => o || f || l ? {
    ...o,
    [a.POINTER_ENTER]: (...t) => {
      n && s && (m.current = !0, u(n, !0)), l && c(l), o?.[a.POINTER_ENTER]?.(...t);
    },
    [a.POINTER_LEAVE]: (...t) => {
      n && s && (m.current = !1, u(n, !1)), l && c(""), o?.[a.POINTER_LEAVE]?.(...t);
    },
    [a.POINTER_CANCEL]: (...t) => {
      n && s && (m.current = !1, u(n, !1)), l && c(""), o?.[a.POINTER_CANCEL]?.(...t);
    }
  } : o, [i.map, o, n, s, l]);
  return h(n, d), null;
};
export {
  M as default
};
//# sourceMappingURL=Polygon.js.map
