import { j as jsxRuntimeExports } from "../../../../../_virtual/jsx-runtime.js";
import { useRef, useEffect } from "react";
import { a as r, b as f } from "../../../../sdk/dist/shared/chunk-R6ATC4TC.js";
import "@sinclair/typebox";
import "../../../../sdk/dist/shared/chunk-3YVJ2ELD.js";
import "../../../../sdk/dist/shared/chunk-AFVJZPLS.js";
import "../../../../sdk/dist/shared/chunk-A3GF4PYW.js";
import "../../../../sdk/dist/shared/chunk-W33JMRRN.js";
import { useBrickStyle } from "../hooks/use-brick-style.js";
import { tx as D } from "../../../../style-system/dist/twind.js";
import { LatLng, Point, Map as Map$1, TileLayer, Marker } from "../../../../../node_modules/.pnpm/leaflet@2.0.0-alpha/node_modules/leaflet/dist/leaflet-src.js";
/* empty css                                                                                          */
import BrickRoot from "../components/BrickRoot.js";
function WidgetMap({ brick, editable }) {
  const styles = useBrickStyle(brick);
  const classes = Object.values(styles);
  const props = brick.props;
  const containerRef = useRef(null);
  const mapRef = useRef(null);
  const mapInstanceRef = useRef(null);
  const markerRef = useRef(null);
  const initRef = useRef(false);
  const lat = props.lat ?? r.lat;
  const lng = props.lng ?? r.lng;
  useEffect(() => {
    async function init() {
      if (!mapRef.current) return;
      try {
        const map = new Map$1(mapRef.current, {
          zoomControl: false,
          attributionControl: false,
          dragging: false,
          // Disable dragging for static maps
          // @ts-ignore @types/leaflet are not in sync with latest leadflet beta, pingchZoom does exist!
          pinchZoom: false,
          // Disable touch zoom for static maps
          scrollWheelZoom: false,
          // Disable scroll wheel zoom for static maps
          doubleClickZoom: false,
          // Disable double click zoom for static maps
          boxZoom: false,
          // Disable box zoom for static maps
          keyboard: false,
          // Disable keyboard controls for static maps
          trackResize: editable
          // Enable resize tracking
        }).setView([parseFloat(lat), parseFloat(lng)], props.zoom ?? r.zoom);
        new TileLayer("https://tiles.stadiamaps.com/tiles/osm_bright/{z}/{x}/{y}{r}.png", {
          zIndex: 40
        }).addTo(map);
        const marker = new Marker([parseFloat(lat), parseFloat(lng)]).addTo(map);
        if (props.tooltip) {
          marker.bindTooltip(props.tooltip, {
            permanent: true,
            direction: "bottom",
            sticky: true,
            // Keep the tooltip open
            offset: new Point(0, 10)
            // Adjust tooltip position
          }).openTooltip();
        }
        initRef.current = true;
        mapInstanceRef.current = map;
        markerRef.current = marker;
      } catch (error) {
      }
    }
    init();
    return () => {
      if (mapInstanceRef.current) {
        mapInstanceRef.current.remove();
        mapInstanceRef.current = null;
      }
      if (markerRef.current) {
        markerRef.current = null;
      }
    };
  }, []);
  useEffect(() => {
    if (mapInstanceRef.current && markerRef.current) {
      const newLatLng = new LatLng(parseFloat(lat), parseFloat(lng));
      mapInstanceRef.current.setView(newLatLng, props.zoom ?? r.zoom);
      markerRef.current.setLatLng(newLatLng);
      if (props.tooltip) {
        markerRef.current.bindTooltip(props.tooltip, {
          permanent: true,
          direction: "bottom",
          sticky: true,
          // Keep the tooltip open
          offset: new Point(0, 10)
          // Adjust tooltip position
        }).openTooltip();
      } else {
        markerRef.current.unbindTooltip();
      }
    }
  }, [lat, lng, props.tooltip, props.zoom]);
  const resizingDelayTimer = useRef(null);
  useEffect(() => {
    if (containerRef.current && editable) {
      const resizeObserver = new ResizeObserver(() => {
        clearTimeout(resizingDelayTimer.current ?? void 0);
        resizingDelayTimer.current = setTimeout(() => {
          if (mapInstanceRef.current) {
            mapInstanceRef.current.invalidateSize();
          }
        }, 100);
      });
      resizeObserver.observe(containerRef.current);
      return () => {
        setTimeout(() => {
          resizeObserver.disconnect();
        }, 200);
      };
    }
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
    BrickRoot,
    {
      brick,
      editable,
      manifest: f,
      className: D("rounded-[inherit] relative overflow-hidden", ...classes),
      ref: containerRef,
      children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { ref: mapRef, className: D("h-full w-full rounded-[inherit] absolute inset-0 z-40") })
    }
  );
}
export {
  WidgetMap as default
};
//# sourceMappingURL=map.js.map
