import { __name } from './chunk-CGILA3WO.mjs';
import { createContext, useRef, useState, useCallback, useMemo, useContext, useEffect } from 'react';
import Map from 'react-map-gl/maplibre';
import { RotateCcw, ExternalLink } from 'lucide-react';
import { cn } from '@djangocfg/ui-core/lib';
import 'maplibre-gl/dist/maplibre-gl.css';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';

var MapContext = createContext(null);
var DEFAULT_VIEWPORT = {
  longitude: 115.1889,
  latitude: -8.4095,
  zoom: 10,
  bearing: 0,
  pitch: 0
};
function MapProvider({ children, initialViewport }) {
  const mapRef = useRef(null);
  const initialViewportRef = useRef({
    ...DEFAULT_VIEWPORT,
    ...initialViewport
  });
  const [viewport, setViewportState] = useState(initialViewportRef.current);
  const [markers, setMarkers] = useState([]);
  const [selectedMarker, setSelectedMarker] = useState(null);
  const [hoveredFeature, setHoveredFeature] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const setViewport = useCallback((newViewport) => {
    setViewportState((prev) => ({ ...prev, ...newViewport }));
  }, []);
  const resetToInitial = 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 = 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__ */ jsx(MapContext.Provider, { value, children });
}
__name(MapProvider, "MapProvider");
function useMapContext() {
  const context = useContext(MapContext);
  if (!context) {
    throw new Error("useMapContext must be used within a MapProvider");
  }
  return context;
}
__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 = useRef(null);
  const isInteractingRef = 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 = 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 = useCallback(() => {
    isInteractingRef.current = true;
    if (resetTimerRef.current) {
      clearTimeout(resetTimerRef.current);
      resetTimerRef.current = null;
    }
  }, []);
  const handleMoveEnd = useCallback(() => {
    isInteractingRef.current = false;
    if (autoResetDelay > 0) {
      resetTimerRef.current = setTimeout(() => {
        if (!isInteractingRef.current) {
          resetToInitial();
        }
      }, autoResetDelay);
    }
  }, [autoResetDelay, resetToInitial]);
  const handleLoad = useCallback(() => {
    setIsLoaded(true);
  }, [setIsLoaded]);
  useEffect(() => {
    return () => {
      if (resetTimerRef.current) {
        clearTimeout(resetTimerRef.current);
      }
    };
  }, []);
  const resolvedStyle = mapStyle in MAP_STYLES ? MAP_STYLES[mapStyle] : mapStyle;
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      Map,
      {
        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__ */ jsxs("div", { className: "absolute bottom-3 right-3 flex items-center gap-2", children: [
      showResetButton && hasViewportChanged && /* @__PURE__ */ jsxs(
        "button",
        {
          type: "button",
          onClick: resetToInitial,
          className: 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__ */ jsx(RotateCcw, { className: "w-4 h-4" }),
            "Reset"
          ]
        }
      ),
      openInMapsUrl && /* @__PURE__ */ jsxs(
        "a",
        {
          href: openInMapsUrl,
          target: "_blank",
          rel: "noopener noreferrer",
          className: 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__ */ jsx(ExternalLink, { className: "w-4 h-4" }),
            openInMapsLabel
          ]
        }
      )
    ] })
  ] });
}
__name(MapInner, "MapInner");
function MapContainer({
  children,
  initialViewport,
  className,
  ...props
}) {
  return /* @__PURE__ */ jsx("div", { className, style: { width: "100%", height: "100%", position: "relative" }, children: /* @__PURE__ */ jsx(MapProvider, { initialViewport, children: /* @__PURE__ */ jsx(MapInner, { ...props, children }) }) });
}
__name(MapContainer, "MapContainer");
function MapView(props) {
  return /* @__PURE__ */ jsx(MapInner, { ...props });
}
__name(MapView, "MapView");

export { MapContainer, MapView };
//# sourceMappingURL=MapContainer-GXQLP5WY.mjs.map
//# sourceMappingURL=MapContainer-GXQLP5WY.mjs.map