import { useState as d, useRef as D, useCallback as T, useEffect as _ } from "react";
const V = 5, A = 100, p = 0.1, H = 180, k = 6371e3, y = {
  entries: [],
  canGoBack: !1,
  goBack: () => {
  }
}, R = (e) => e * Math.PI / 180, C = (e, n) => {
  const o = R(n.lat - e.lat), u = R(n.lng - e.lng), l = R(e.lat), s = R(n.lat), a = Math.sin(o / 2) ** 2 + Math.cos(l) * Math.cos(s) * Math.sin(u / 2) ** 2;
  return 2 * k * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}, f = (e) => {
  const n = e.getCenter(), { zoom: o = 0 } = e.getViewModel().getLookAtData();
  return {
    center: {
      lat: n.lat,
      lng: n.lng
    },
    zoom: o
  };
}, z = (e, n, o, u) => {
  if (!(e && n))
    return !1;
  const l = C(e.center, n.center), s = Math.abs(e.zoom - n.zoom);
  return l >= o || s >= u;
}, I = (e, n) => e?.center.lat === n?.center.lat && e?.center.lng === n?.center.lng && e?.zoom === n?.zoom, B = (e, {
  maxEntries: n = V,
  centerDistanceThresholdMeters: o = A,
  zoomThreshold: u = p,
  zoomAnimation: l = !1,
  controlledViewport: s
} = {}) => {
  const [a, M] = d({
    entries: [],
    currentViewport: s,
    isRestoring: !1
  }), m = D(), g = D(), w = T(
    (t, c) => z(t, c, o, u),
    [o, u]
  ), h = T(
    (t, c) => {
      const r = t[0];
      return r && !w(c, r) ? t : [c, ...t].slice(0, n);
    },
    [w, n]
  ), i = T(
    (t, { replace: c = !1 } = {}) => {
      M((r) => {
        if (c)
          return {
            ...r,
            currentViewport: t,
            isRestoring: !1
          };
        if (r.isRestoring)
          return {
            ...r,
            currentViewport: t,
            isRestoring: !1
          };
        const { currentViewport: E } = r;
        return E ? I(E, t) ? r : w(E, t) ? {
          ...r,
          entries: h(r.entries, E),
          currentViewport: t
        } : {
          ...r,
          currentViewport: t
        } : {
          ...r,
          currentViewport: t
        };
      });
    },
    [h, w]
  ), L = T(() => {
    const t = e?.map, c = a.entries[0];
    t && c && (window.clearTimeout(m.current), window.clearTimeout(g.current), M((r) => ({
      ...r,
      entries: r.entries.slice(1),
      currentViewport: c,
      isRestoring: !0
    })), t.getViewModel().setLookAtData(
      {
        position: c.center,
        zoom: c.zoom
      },
      l
    ), m.current = window.setTimeout(() => {
      i(f(t), { replace: !0 }), m.current = void 0;
    }, 750));
  }, [e?.map, i, a.entries, l]);
  return _(() => {
    s && i(s);
  }, [i, s]), _(() => {
    const t = e?.map;
    if (!t)
      return;
    i(f(t), { replace: !0 });
    const c = () => {
      window.clearTimeout(g.current), g.current = window.setTimeout(() => {
        g.current = void 0, i(f(t));
      }, H);
    };
    return t.addEventListener("mapviewchangeend", c), () => {
      t.removeEventListener("mapviewchangeend", c), window.clearTimeout(m.current), window.clearTimeout(g.current), m.current = void 0, g.current = void 0;
    };
  }, [e?.map, i]), {
    entries: a.entries,
    canGoBack: a.entries.length > 0,
    goBack: L
  };
};
export {
  y as defaultMapViewportHistory,
  B as useMapViewportHistory
};
//# sourceMappingURL=useMapViewportHistory.js.map
