import { useState as M, useRef as h, useEffect as d, useMemo as S } from "react";
import { useMapContext as I } from "../../MapContext.js";
import b from "../../../exceptions/MapException.js";
import { EventUtils as i, removeEventListenerMap as p, addEventListenerMap as w } from "../../../utils/eventHandling.js";
import { isVectorBased as v } from "../../../utils/rendering.js";
const j = "Error in Polyline: Polyline without geometry makes no sense", A = "Error in Polyline: property geometry must be of type H.geo.LineString", V = (T) => {
  const {
    geometry: r,
    hasArrows: y,
    arrowStyle: E,
    style: O = H.map.SpatialStyle.DEFAULT_STYLE,
    customData: u,
    eventListenerMap: n,
    onReady: m
  } = T, { api: o, enableWebGL: L, baseLayer: _ } = I(), [l, N] = M(), s = h([]), f = h(m);
  d(() => {
    f.current = m;
  }, [m]);
  const g = S(() => {
    if (!n)
      return;
    const e = Object.entries(n).filter(
      ([t]) => [i.POINTER_ENTER, i.POINTER_LEAVE, i.POINTER_CANCEL].includes(t)
    );
    if (e.length !== 0)
      return Object.fromEntries(e);
  }, [n]), P = S(() => {
    if (!n)
      return;
    const e = Object.entries(n).filter(
      ([t]) => ![i.POINTER_ENTER, i.POINTER_LEAVE, i.POINTER_CANCEL].includes(t)
    );
    if (e.length !== 0)
      return Object.fromEntries(e);
  }, [n]);
  if (!r)
    throw new b(j);
  if (!(r instanceof H.geo.LineString))
    throw new b(A);
  return d(() => {
    const e = new H.map.Group({ data: u }), t = new H.map.Polyline(r, { style: O });
    let a;
    t.setData(u);
    const R = [t];
    if (y) {
      if (v(L, _))
        a = new H.map.Polyline(r, {
          style: {
            strokeColor: "rgba(42, 55, 64, 1)",
            lineWidth: 5,
            lineDash: [1.8, 4],
            // @ts-expect-error-next-line - the types are outdated here
            lineDashImage: H.map.SpatialStyle.DashImage.ARROW,
            ...E
          }
        });
      else {
        const c = {
          lineWidth: 2,
          fillColor: "rgba(42, 55, 64, 1)",
          strokeColor: "rgba(0, 0, 0, 0.8)",
          lineDash: [0, 4],
          lineTailCap: "arrow-tail",
          lineHeadCap: "arrow-head",
          ...E
        }, C = new H.map.SpatialStyle({
          lineWidth: 10,
          strokeColor: "rgba(255, 255, 255, 0)"
        });
        a = new H.map.Polyline(r, { style: C, arrows: c });
      }
      a.setData(u), R.push(a), e.addObjects([t, a]);
    } else
      e.addObjects([t]);
    return o.map?.addObject(e), s.current = R, N(e), f.current?.({ group: e, line: t, routeArrows: a }), () => {
      f.current?.(), p(e), s.current.forEach(p), s.current = [], e.forEach((c) => {
        e.removeObject(c), c.dispose();
      }), o.map?.getObjects().includes(e) && o.map.removeObject(e), e.dispose();
    };
  }, [o.map, r, O, y, E]), d(() => {
    const e = o.map;
    if (l && e)
      return w(l, P, e), s.current.forEach((t) => {
        w(t, g, e);
      }), () => {
        p(l), s.current.forEach(p);
      };
  }, [o.map, l, P, g]), null;
};
export {
  j as MISSING_GEOMETRY_PROPS_EXCEPTION_MSG,
  A as WRONG_GEOMETRY_PROPS_EXCEPTION_MSG,
  V as default
};
//# sourceMappingURL=Polyline.js.map
