import { jsxs as K, jsx as u } from "react/jsx-runtime";
import { useRef as v, useMemo as p, useEffect as P } from "react";
import { isArray as V, isEmpty as x } from "es-toolkit/compat";
import N from "./basics/Polyline.js";
import b from "./basics/Marker.js";
import U from "../../exceptions/MapException.js";
import { EventUtils as s } from "../../utils/eventHandling.js";
import { useMapContext as X } from "../MapContext.js";
const J = "Route requires a minimum of 1 positions", q = "If isBorderIncludedInWidth is set to true than width has to be greater than twice of borderWidth", Y = "rgba(48, 194, 255, 1)", z = "rgba(48, 194, 255, 0.5)", Q = "rgb(200, 243, 247)", Z = "rgba(22, 100, 141, 1)", $ = "rgba(255, 255, 255, 0)", Ee = "#e22837", ue = "#ff8e3c", Re = "#5cb85c", ye = "rgba(255, 255, 255, 1)", _e = "rgba(167, 83, 25, 1)", j = 10, B = 5, ee = 2, M = {
  width: B,
  color: Y,
  borderWidth: ee,
  borderColor: Z,
  isBorderIncludedInWidth: !1
}, te = {
  ...M,
  color: Q
}, re = (a) => {
  if (x(a))
    throw new U(J);
  const l = new H.geo.LineString();
  if (a.forEach((o) => {
    l.pushLatLngAlt(o.lat, o.lng, 0);
  }), a.length === 1) {
    const [o] = a;
    l.pushLatLngAlt(o.lat, o.lng, 0);
  }
  return l;
}, oe = (a) => {
  const { style: l, hoverStyle: o, isReduced: d, isRouteAlternative: h, showHitArea: f } = a, n = (m) => {
    const I = {
      ...h ? te : M,
      ...m
    }, {
      color: T,
      width: c = B,
      borderColor: _,
      borderWidth: r = 0,
      isBorderIncludedInWidth: O,
      lineDash: i
    } = I;
    if (O && r >= c)
      throw new U(q);
    return {
      borderWidth: d ? 0 : r,
      routeStyle: new H.map.SpatialStyle({
        strokeColor: d ? z : T,
        lineWidth: d ? r + c : c,
        lineCap: "round",
        lineJoin: "bevel",
        ...i && { lineDash: i }
      }),
      borderStyle: new H.map.SpatialStyle({
        strokeColor: d ? $ : _,
        lineWidth: r + c,
        lineCap: "round",
        lineJoin: "bevel",
        ...i && { lineDash: i }
      }),
      hitAreaStyle: new H.map.SpatialStyle({
        strokeColor: f ? "rgba(255, 0, 0, 0.35)" : "rgba(255, 255, 255, 0.001)",
        lineWidth: r + c + j,
        lineCap: "round",
        lineJoin: "round"
      })
    };
  }, R = n(l), y = o ? n({ ...l, ...o }) : R;
  return {
    ...R,
    hoverRouteStyle: y.routeStyle,
    hoverBorderStyle: y.borderStyle
  };
}, Oe = (a) => {
  const {
    style: l,
    hoverStyle: o,
    showHitArea: d = !1,
    isReduced: h = !1,
    isRouteAlternative: f = !1,
    positions: n,
    startIcon: R,
    middleIcon: y,
    endIcon: m,
    arrowStyle: I = {},
    hasArrows: T = !0,
    markers: c = [],
    customData: _,
    eventListenerMap: r,
    cursor: O
  } = a, { api: i } = X(), D = v(), L = v(), S = v(!1), E = p(() => {
    if (O)
      return O;
    if (r?.[s.TAP] || r?.[s.DBL_TAP] || r?.[s.CONTEXTMENU])
      return "pointer";
  }, [O, r]), C = p(() => re(n), [n]), t = p(
    () => oe({
      style: l,
      hoverStyle: o,
      isReduced: h,
      isRouteAlternative: f,
      showHitArea: d
    }),
    [l, o, h, f, d]
  ), W = (e) => {
    if (!i.map)
      return;
    const G = i.map.getElement(), k = i.map.getViewPort().element;
    G.style.cursor = e, k.style.cursor = e;
  }, A = (e) => {
    D.current?.line.setStyle(e ? t.hoverRouteStyle : t.routeStyle), L.current && L.current.line.setStyle(
      e ? t.hoverBorderStyle : t.borderStyle
    );
  }, F = () => {
    S.current = !0, A(!0), E && W(E);
  }, g = () => {
    S.current = !1, A(!1), E && W("");
  };
  P(() => {
    A(S.current);
  }, [
    t.routeStyle,
    t.borderStyle,
    t.hoverRouteStyle,
    t.hoverBorderStyle
  ]), P(
    () => () => {
      S.current = !1, g();
    },
    [i.map, E]
  );
  const w = p(() => {
    if (r || o || E)
      return {
        ...r,
        [s.POINTER_ENTER]: (...e) => {
          F(), r?.[s.POINTER_ENTER]?.(...e);
        },
        [s.POINTER_LEAVE]: (...e) => {
          g(), r?.[s.POINTER_LEAVE]?.(...e);
        },
        [s.POINTER_CANCEL]: (...e) => {
          g(), r?.[s.POINTER_CANCEL]?.(...e);
        }
      };
  }, [r, o, E, t]);
  return /* @__PURE__ */ K("div", { children: [
    t.borderWidth > 0 && /* @__PURE__ */ u(
      N,
      {
        geometry: C,
        style: t.borderStyle,
        customData: _,
        onReady: (e) => {
          L.current = e, e && e.line.setStyle(
            S.current ? t.hoverBorderStyle : t.borderStyle
          );
        }
      }
    ),
    /* @__PURE__ */ u(
      N,
      {
        hasArrows: T,
        arrowStyle: I,
        geometry: C,
        style: t.routeStyle,
        customData: _,
        onReady: (e) => {
          D.current = e, e && e.line.setStyle(
            S.current ? t.hoverRouteStyle : t.routeStyle
          );
        }
      }
    ),
    w && // Keep interaction on a single invisible polyline so hover/cursor/click stay stable
    // even though the visible route may consist of multiple rendered polylines.
    /* @__PURE__ */ u(
      N,
      {
        eventListenerMap: w,
        geometry: C,
        style: t.hitAreaStyle,
        customData: _
      }
    ),
    R && /* @__PURE__ */ u(b, { position: n[0], icon: R }, "startIcon"),
    y && /* @__PURE__ */ u(b, { position: n[Math.floor(n.length / 2)], icon: y }, "middleIcon"),
    m && /* @__PURE__ */ u(b, { position: n[n.length - 1], icon: m }, "endIcon"),
    V(c) && c.map((e) => e)
  ] });
};
export {
  _e as ARROW_COLOR_WARNING,
  Z as DEFAULT_BORDER_STROKE_COLOR,
  $ as DEFAULT_BORDER_STROKE_COLOR_REDUCED,
  Y as DEFAULT_LINE_STROKE_COLOR,
  Q as DEFAULT_LINE_STROKE_COLOR_ALTERNATIVE,
  z as DEFAULT_LINE_STROKE_COLOR_REDUCED,
  J as MISSING_POSITIONS_EXCEPTION_MSG,
  Ee as ROUTE_COLOR_DANGER,
  Re as ROUTE_COLOR_SUCCESS,
  ue as ROUTE_COLOR_WARNING,
  ye as ROUTE_COLOR_WHITE,
  q as TO_LESS_LINE_WIDTH_EXCEPTION_MSG,
  Oe as default
};
//# sourceMappingURL=Route.js.map
