import { jsx as p, jsxs as He } from "react/jsx-runtime";
import { useRef as S, useState as i, useMemo as Ue, useEffect as l } from "react";
import { isEmpty as ze } from "es-toolkit/compat";
import { isEqual as Ve } from "es-toolkit/predicate";
import { DEFAULT_MAX_ZOOM as ce, MAP_TYPE_DEFAULT as w, DEFAULT_PIXEL_RATIO as je, DEFAULT_MIN_ZOOM as We, MAP_LAYER_ROAD_RESTRICTIONS as pe, MAP_TYPE_NIGHT as V, MAP_TYPE_FLEET_STYLE as qe, MAP_TYPE_SATELLITE as Ge, MAP_TYPE_TERRAIN as Ke, MAP_LAYER_INCIDENTS as Xe, MAP_LAYER_TRAFFIC as $e } from "./constants.js";
import { removeEventListenerMap as Je, addEventListenerMap as Qe } from "../utils/eventHandling.js";
import { MapContext as et } from "./MapContext.js";
import { getMapBounds as tt, createUtils as ot } from "../utils/mapUtils.js";
import { getHereUi as nt } from "../utils/mapUi.js";
import rt from "../../../hooks/useDarkMode.js";
import at from "./features/MapZoom.js";
import st from "./features/layers/MapOverlayLayers.js";
import it from "./MapSize.js";
import lt from "./MapPosition.js";
import mt from "./MapElements.js";
import { getPlatform as ct, getBaseLayer as pt, getEngineType as ut } from "../utils/rendering.js";
import { useMapViewportHistory as Et } from "../hooks/useMapViewportHistory.js";
const dt = "map-disable-marker-pointer-events", vt = 140, s = (ue) => {
  const {
    children: Ee,
    credentials: j,
    width: W,
    height: q,
    language: de = "en",
    center: m,
    zoom: u,
    zoomAnimation: b = !1,
    boundingBox: O,
    hideClusterSettings: ve = !1,
    hideMapLayerSettings: fe = !1,
    disableMapEvents: G,
    disableBehavior: K = !1,
    disableZoomMomentum: X = !0,
    enableFractionalZoom: x = !1,
    eventListenerMap: he,
    mapType: g = w,
    mapLayer: E = [],
    showCluster: D = !0,
    showScaleBar: Me = !1,
    onMapLayerChange: Le = () => {
    },
    onMapTypeChange: Te = () => {
    },
    onShowClusterChange: ge = () => {
    },
    onZoomIn: _e = () => {
    },
    onZoomOut: Ae = () => {
    },
    onIncidentsChange: Re = () => {
    },
    mapSettingsTooltip: ye,
    mapSettings: $,
    maxViewportHistoryEntries: Pe,
    enableWebGL: d = !0,
    enableDevicePixelRatio: J = !0,
    enableMarkerIconCache: Ce = !0,
    disableMarkerPointerEventsWhileZooming: v = !1,
    pixelRatio: Q = je,
    minZoom: _ = We,
    maxZoom: N = ce
  } = ue;
  let Y = N;
  _ > N && (Y = ce, console.error("Invalid condition for zoom limits: min <= max)"));
  const A = S(null), f = S(), ee = S(!1), Z = S(v), [Ie, Se] = i(!1), [we, R] = i(!1), [t, be] = i(), [r, Oe] = i(g), [a, te] = i(E), [oe, ne] = i(D), xe = Ue(
    () => m && u !== void 0 ? { center: m, zoom: u } : void 0,
    [m?.lat, m?.lng, u]
  ), De = Et(t, {
    maxEntries: Pe,
    zoomAnimation: b,
    controlledViewport: xe
  }), re = J && window.devicePixelRatio || Q, c = /* @__PURE__ */ ((e) => e === "nb" ? "no" : e)(de.split("-")[0].toLowerCase()), h = (e) => {
    if (e && !Z.current)
      return;
    const n = A.current;
    n && (n.classList.toggle(dt, e), ee.current = e);
  }, M = () => {
    f.current !== void 0 && (window.clearTimeout(f.current), f.current = void 0);
  }, ae = () => {
    M(), f.current = window.setTimeout(() => {
      h(!1), R(!1), f.current = void 0;
    }, vt);
  }, se = () => {
    R(!0), M(), Z.current && !ee.current && h(!0);
  };
  l(() => {
    Z.current = v;
  }, [v]), l(() => {
    const e = A.current;
    if (!e)
      return;
    const n = () => {
      se(), ae();
    };
    return e.addEventListener("wheel", n, { passive: !0, capture: !0 }), () => {
      e.removeEventListener("wheel", n, { capture: !0 });
    };
  }, []), l(() => {
    let e = [], n = () => {
    };
    t?.map ? (e = t.map.getObjects(), console.debug("re-initialize here map")) : console.debug("initialize here map"), t?.map && (console.debug("disposing previous map"), Je(t.map), t.mapEvents && t.mapEvents.dispose(), t.map.dispose());
    const C = O && tt(O), L = ct(j), T = ut(r, d), k = L.createDefaultLayers({ engineType: T, lg: c, pois: !0 });
    return pt({
      baseLayerName: r,
      defaultLayers: k,
      enableWebGL: d,
      platform: L,
      engineType: T,
      vehicleRestrictions: a.includes(pe),
      language: c,
      minZoom: _,
      maxZoom: Y
    }).then((U) => {
      const le = () => {
        se();
      }, me = () => {
        ae();
      }, o = new H.Map(A.current, U.baseLayer, {
        zoom: u,
        center: m,
        bounds: C,
        engineType: T,
        pixelRatio: re,
        fixedCenter: !0
      });
      U.overlayLayer && o.addLayer(U.overlayLayer), Se(!0);
      let z = !1, I;
      G || (z = new H.mapevents.MapEvents(o)), G || K || (I = new H.mapevents.Behavior(z), x || I.disable(H.mapevents.Behavior.Feature.FRACTIONAL_ZOOM), X && I.disable(H.mapevents.Behavior.Feature.ZOOM_MOMENTUM)), Qe(o, he, o), o.addEventListener("mapviewchangestart", le), o.addEventListener("mapviewchangeend", me);
      const ke = nt(o, k, Me, c);
      be({
        credentials: j,
        defaultLayers: k,
        map: o,
        mapEvents: z,
        behavior: I,
        platform: L,
        ui: ke,
        utils: ot(o),
        // Put settings back into the map API object so the invoking service
        // can check on these settings for instance inside a test
        settings: {
          enableWebGL: d,
          baseLayer: r,
          activeLayers: a,
          showCluster: oe,
          minZoom: _,
          maxZoom: Y
        }
      }), e && o.addObjects(e), n = () => {
        o.removeEventListener("mapviewchangestart", le), o.removeEventListener("mapviewchangeend", me);
      };
    }), () => {
      n(), M(), h(!1), R(!1);
    };
  }, [
    d,
    J,
    c,
    r,
    _,
    N,
    X,
    x
  ]), l(() => (v || (M(), h(!1)), () => {
    M(), h(!1), R(!1);
  }), [v]);
  const F = rt();
  l(() => {
    F && r === w ? y(V) : !F && r === V && y(w);
  }, [F, c]), l(() => y(g), [g]);
  const [Ne, Ye] = i(E);
  Ve(E, Ne) || (te(E), Ye(E)), l(() => ne(D), [D]);
  const y = (e) => {
    Oe(e), Te(e, g);
  }, Ze = (e) => {
    const n = (L) => a.filter((T) => T !== L), C = a.includes(e) ? n(e) : [...a, e];
    te(C), Le(C, a);
  }, Fe = (e) => {
    ne(e), ge(e);
  }, ie = () => {
    document.querySelector(".AssetTree")?.classList.add("pointer-events-none");
  }, B = () => {
    document.querySelector(".AssetTree")?.classList.remove("pointer-events-none");
  }, P = {};
  q && (P.height = q), W && (P.width = W);
  const Be = ze(P) ? "height-100pct" : "";
  return /* @__PURE__ */ p(
    "div",
    {
      className: `Map position-relative map-type-${r.toLowerCase()} ${Be}`,
      style: P,
      ref: A,
      onTouchStart: ie,
      onTouchEnd: B,
      onMouseEnter: ie,
      onMouseOut: B,
      onBlur: B,
      children: Ie && t && /* @__PURE__ */ He(
        et.Provider,
        {
          value: {
            api: t,
            language: c,
            enableWebGL: d,
            enableMarkerIconCache: Ce,
            enableFractionalZoom: x,
            isZoomInteractionActive: we,
            showCluster: oe,
            activeLayers: a,
            baseLayer: r,
            hideMapLayerSettings: fe,
            hideClusterSettings: ve,
            viewportHistory: De,
            onMapTypeChange: y,
            onMapLayerChange: Ze,
            onShowClusterChange: Fe,
            mapSettingsTooltip: ye,
            onIncidentsChange: Re,
            devicePixelRatio: re
          },
          children: [
            /* @__PURE__ */ p(
              lt,
              {
                api: t,
                boundingBox: O,
                center: m,
                zoom: u,
                zoomAnimation: b
              }
            ),
            /* @__PURE__ */ p(it, { api: t }),
            /* @__PURE__ */ p(
              at,
              {
                hideZoomButtons: K,
                onZoomIn: _e,
                onZoomOut: Ae,
                zoomAnimation: b
              }
            ),
            $ && $,
            /* @__PURE__ */ p(st, { layers: a }),
            /* @__PURE__ */ p(mt, { api: t, children: Ee })
          ]
        }
      )
    }
  );
};
s.TYPE_DEFAULT = w;
s.TYPE_FLEET_STYLE = qe;
s.TYPE_SATELLITE = Ge;
s.TYPE_TERRAIN = Ke;
s.TYPE_NIGHT = V;
s.LAYER_INCIDENTS = Xe;
s.LAYER_TRAFFIC = $e;
s.LAYER_ROAD_RESTRICTIONS = pe;
export {
  s as default
};
//# sourceMappingURL=Map.js.map
