'use strict';

var chunkOLISEQHS_cjs = require('./chunk-OLISEQHS.cjs');
var react = require('react');
var Map = require('react-map-gl/maplibre');
var lucideReact = require('lucide-react');
var lib = require('@djangocfg/ui-core/lib');
require('maplibre-gl/dist/maplibre-gl.css');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var Map__default = /*#__PURE__*/_interopDefault(Map);

var MapContext = react.createContext(null);
var DEFAULT_VIEWPORT = {
  longitude: 115.1889,
  latitude: -8.4095,
  zoom: 10,
  bearing: 0,
  pitch: 0
};
function MapProvider({ children, initialViewport }) {
  const mapRef = react.useRef(null);
  const initialViewportRef = react.useRef({
    ...DEFAULT_VIEWPORT,
    ...initialViewport
  });
  const [viewport, setViewportState] = react.useState(initialViewportRef.current);
  const [markers, setMarkers] = react.useState([]);
  const [selectedMarker, setSelectedMarker] = react.useState(null);
  const [hoveredFeature, setHoveredFeature] = react.useState(null);
  const [isLoaded, setIsLoaded] = react.useState(false);
  const setViewport = react.useCallback((newViewport) => {
    setViewportState((prev) => ({ ...prev, ...newViewport }));
  }, []);
  const resetToInitial = react.useCallback(() => {
    const map = mapRef.current;
    if (map) {
      map.flyTo({
        center: [initialViewportRef.current.longitude, initialViewportRef.current.latitude],
        zoom: initialViewportRef.current.zoom,
        bearing: initialViewportRef.current.bearing ?? 0,
        pitch: initialViewportRef.current.pitch ?? 0,
        duration: 1e3
      });
    }
  }, []);
  const value = react.useMemo(
    () => ({
      mapRef,
      viewport,
      setViewport,
      initialViewport: initialViewportRef.current,
      resetToInitial,
      markers,
      setMarkers,
      selectedMarker,
      setSelectedMarker,
      hoveredFeature,
      setHoveredFeature,
      isLoaded,
      setIsLoaded
    }),
    [viewport, setViewport, resetToInitial, markers, selectedMarker, hoveredFeature, isLoaded]
  );
  return /* @__PURE__ */ jsxRuntime.jsx(MapContext.Provider, { value, children });
}
chunkOLISEQHS_cjs.__name(MapProvider, "MapProvider");
function useMapContext() {
  const context = react.useContext(MapContext);
  if (!context) {
    throw new Error("useMapContext must be used within a MapProvider");
  }
  return context;
}
chunkOLISEQHS_cjs.__name(useMapContext, "useMapContext");

// src/tools/Map/styles/index.ts
var MAP_STYLES = {
  light: "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
  dark: "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json",
  streets: "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json",
  satellite: "https://api.maptiler.com/maps/satellite/style.json"
};
function MapInner({
  children,
  mapStyle = "light",
  interactiveLayerIds,
  style,
  cursor,
  attributionControl = true,
  reuseMaps = true,
  openInMapsUrl,
  openInMapsLabel = "Open in Maps",
  autoResetDelay = 0,
  showResetButton = false
}) {
  const { mapRef, viewport, setViewport, setIsLoaded, resetToInitial, initialViewport } = useMapContext();
  const resetTimerRef = react.useRef(null);
  const isInteractingRef = react.useRef(false);
  const hasViewportChanged = Math.abs(viewport.longitude - initialViewport.longitude) > 1e-4 || Math.abs(viewport.latitude - initialViewport.latitude) > 1e-4 || Math.abs(viewport.zoom - initialViewport.zoom) > 0.1;
  const handleMove = react.useCallback(
    (evt) => {
      setViewport({
        longitude: evt.viewState.longitude,
        latitude: evt.viewState.latitude,
        zoom: evt.viewState.zoom,
        bearing: evt.viewState.bearing,
        pitch: evt.viewState.pitch
      });
    },
    [setViewport]
  );
  const handleMoveStart = react.useCallback(() => {
    isInteractingRef.current = true;
    if (resetTimerRef.current) {
      clearTimeout(resetTimerRef.current);
      resetTimerRef.current = null;
    }
  }, []);
  const handleMoveEnd = react.useCallback(() => {
    isInteractingRef.current = false;
    if (autoResetDelay > 0) {
      resetTimerRef.current = setTimeout(() => {
        if (!isInteractingRef.current) {
          resetToInitial();
        }
      }, autoResetDelay);
    }
  }, [autoResetDelay, resetToInitial]);
  const handleLoad = react.useCallback(() => {
    setIsLoaded(true);
  }, [setIsLoaded]);
  react.useEffect(() => {
    return () => {
      if (resetTimerRef.current) {
        clearTimeout(resetTimerRef.current);
      }
    };
  }, []);
  const resolvedStyle = mapStyle in MAP_STYLES ? MAP_STYLES[mapStyle] : mapStyle;
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      Map__default.default,
      {
        ref: mapRef,
        ...viewport,
        onMove: handleMove,
        onMoveStart: handleMoveStart,
        onMoveEnd: handleMoveEnd,
        onLoad: handleLoad,
        mapStyle: resolvedStyle,
        interactiveLayerIds,
        attributionControl: attributionControl ? {} : false,
        reuseMaps,
        style: {
          width: "100%",
          height: "100%",
          ...style
        },
        cursor,
        children
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "absolute bottom-3 right-3 flex items-center gap-2", children: [
      showResetButton && hasViewportChanged && /* @__PURE__ */ jsxRuntime.jsxs(
        "button",
        {
          type: "button",
          onClick: resetToInitial,
          className: lib.cn(
            "inline-flex items-center gap-2 px-3 py-2 rounded-lg",
            "bg-background/95 backdrop-blur-sm text-foreground text-sm font-medium",
            "hover:bg-background transition-colors shadow-sm border border-border"
          ),
          children: [
            /* @__PURE__ */ jsxRuntime.jsx(lucideReact.RotateCcw, { className: "w-4 h-4" }),
            "Reset"
          ]
        }
      ),
      openInMapsUrl && /* @__PURE__ */ jsxRuntime.jsxs(
        "a",
        {
          href: openInMapsUrl,
          target: "_blank",
          rel: "noopener noreferrer",
          className: lib.cn(
            "inline-flex items-center gap-2 px-4 py-2 rounded-lg",
            "bg-background/95 backdrop-blur-sm text-foreground text-sm font-medium",
            "hover:bg-background transition-colors shadow-sm border border-border"
          ),
          children: [
            /* @__PURE__ */ jsxRuntime.jsx(lucideReact.ExternalLink, { className: "w-4 h-4" }),
            openInMapsLabel
          ]
        }
      )
    ] })
  ] });
}
chunkOLISEQHS_cjs.__name(MapInner, "MapInner");
function MapContainer({
  children,
  initialViewport,
  className,
  ...props
}) {
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className, style: { width: "100%", height: "100%", position: "relative" }, children: /* @__PURE__ */ jsxRuntime.jsx(MapProvider, { initialViewport, children: /* @__PURE__ */ jsxRuntime.jsx(MapInner, { ...props, children }) }) });
}
chunkOLISEQHS_cjs.__name(MapContainer, "MapContainer");
function MapView(props) {
  return /* @__PURE__ */ jsxRuntime.jsx(MapInner, { ...props });
}
chunkOLISEQHS_cjs.__name(MapView, "MapView");

exports.MapContainer = MapContainer;
exports.MapView = MapView;
//# sourceMappingURL=MapContainer-76YL2JXL.cjs.map
//# sourceMappingURL=MapContainer-76YL2JXL.cjs.map