import { usePrevious } from "../../utilities/usePrevious.mjs";
import MapContext from "./MapContext.mjs";
import GeoLocation from "../geoLocation/GeoLocation.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useRef, useState } from "react";
import { last } from "lodash-es";
import "./Map.css";
import * as ol from "ol";
import { DragRotateAndZoom, Select, defaults } from "ol/interaction";
import { Icon, Style } from "ol/style";
import { click } from "ol/events/condition";
import { fromLonLat } from "ol/proj";
//#region src/map-library/Map/Map.jsx
const Map = (t0) => {
	const $ = c(66);
	const { children, zoom, center, projection, sendSelected, sendType, SelectedPin, changeLat, changeLong, setPosition, width, height, addressBar, popoverOverlayProps, additionalOnclickFeature, popoverRef, setSelectInteraction, setPopoverOverlay, setMapObject } = t0;
	const mapRef = useRef();
	const [map, setMap] = useState(null);
	const [selected, setSelected] = useState();
	const [markingType, setMarkingType] = useState();
	const [count, setCount] = useState(0);
	const [mapClicked, setMapclicked] = useState(false);
	const prevCount = usePrevious(count);
	let t1;
	if ($[0] !== SelectedPin || $[1] !== additionalOnclickFeature || $[2] !== center || $[3] !== popoverOverlayProps || $[4] !== popoverRef || $[5] !== projection || $[6] !== setMapObject || $[7] !== setPopoverOverlay || $[8] !== setSelectInteraction || $[9] !== zoom) {
		t1 = () => {
			const options = {
				view: new ol.View({
					center: fromLonLat(center),
					zoom,
					projection
				}),
				interactions: defaults().extend([new DragRotateAndZoom()]),
				layers: [],
				controls: [],
				overlays: []
			};
			const mapObject = new ol.Map(options);
			mapObject.setTarget(mapRef.current);
			let popoverOverlay;
			if (popoverRef) {
				popoverOverlay = new ol.Overlay({
					element: popoverRef?.current,
					autoPan: true,
					...popoverOverlayProps
				});
				mapObject.addOverlay(popoverOverlay);
				if (setPopoverOverlay) setPopoverOverlay(popoverOverlay);
			}
			setMap(mapObject);
			if (setMapObject) setMapObject(mapObject);
			const pointSelect = new Select({
				condition: _temp,
				filter: _temp2,
				multi: true,
				hitTolerance: 10,
				style: new Style({ image: new Icon({
					anchor: [.5, 48],
					anchorXUnits: "fraction",
					anchorYUnits: "pixels",
					src: SelectedPin
				}) })
			});
			pointSelect.on("select", (e_0) => {
				const currentFeature = last(e_0.selected);
				const currentFeaturesProperties = currentFeature?.getProperties();
				if (popoverOverlay && popoverRef?.current && currentFeaturesProperties) popoverOverlay.setPosition(currentFeature?.getGeometry().getCoordinates());
				if (additionalOnclickFeature && (!currentFeaturesProperties || currentFeaturesProperties && Object.keys(currentFeaturesProperties)?.length > 1)) additionalOnclickFeature(currentFeaturesProperties);
			});
			mapObject.addInteraction(pointSelect);
			if (setSelectInteraction) setSelectInteraction(pointSelect);
			return () => mapObject.setTarget(void 0);
		};
		$[0] = SelectedPin;
		$[1] = additionalOnclickFeature;
		$[2] = center;
		$[3] = popoverOverlayProps;
		$[4] = popoverRef;
		$[5] = projection;
		$[6] = setMapObject;
		$[7] = setPopoverOverlay;
		$[8] = setSelectInteraction;
		$[9] = zoom;
		$[10] = t1;
	} else t1 = $[10];
	let t2;
	if ($[11] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = [];
		$[11] = t2;
	} else t2 = $[11];
	useEffect(t1, t2);
	let t3;
	let t4;
	if ($[12] !== selected || $[13] !== sendSelected) {
		t3 = () => {
			sendSelected(selected);
		};
		t4 = [sendSelected, selected];
		$[12] = selected;
		$[13] = sendSelected;
		$[14] = t3;
		$[15] = t4;
	} else {
		t3 = $[14];
		t4 = $[15];
	}
	useEffect(t3, t4);
	let t5;
	let t6;
	if ($[16] !== markingType || $[17] !== sendType) {
		t5 = () => {
			sendType(markingType);
		};
		t6 = [sendType, markingType];
		$[16] = markingType;
		$[17] = sendType;
		$[18] = t5;
		$[19] = t6;
	} else {
		t5 = $[18];
		t6 = $[19];
	}
	useEffect(t5, t6);
	let t7;
	if ($[20] === Symbol.for("react.memo_cache_sentinel")) {
		t7 = (id, type) => {
			setSelected(id);
			setMarkingType(type);
		};
		$[20] = t7;
	} else t7 = $[20];
	const postSelected = t7;
	let t8;
	if ($[21] !== map || $[22] !== zoom) {
		t8 = () => {
			if (!map) return;
			map.getView().setZoom(zoom);
		};
		$[21] = map;
		$[22] = zoom;
		$[23] = t8;
	} else t8 = $[23];
	let t9;
	if ($[24] !== zoom) {
		t9 = [zoom];
		$[24] = zoom;
		$[25] = t9;
	} else t9 = $[25];
	useEffect(t8, t9);
	let t10;
	let t11;
	if ($[26] !== map) {
		t10 = () => {
			if (!map) return;
			map.on("click", (e_1) => {
				setMapclicked(true);
				map.forEachFeatureAtPixel(e_1.pixel, (a) => postSelected(a.values_.geometry.flatCoordinates[2], a.values_.geometry.flatCoordinates[3]));
			});
		};
		t11 = [map];
		$[26] = map;
		$[27] = t10;
		$[28] = t11;
	} else {
		t10 = $[27];
		t11 = $[28];
	}
	useEffect(t10, t11);
	let t12;
	if ($[29] !== center || $[30] !== map || $[31] !== mapClicked || $[32] !== prevCount || $[33] !== zoom) {
		t12 = () => {
			if (!map) return;
			setCount(zoom);
			if (zoom === prevCount && !mapClicked) map.getView().setCenter(center);
		};
		$[29] = center;
		$[30] = map;
		$[31] = mapClicked;
		$[32] = prevCount;
		$[33] = zoom;
		$[34] = t12;
	} else t12 = $[34];
	let t13;
	if ($[35] !== center) {
		t13 = [center];
		$[35] = center;
		$[36] = t13;
	} else t13 = $[36];
	useEffect(t12, t13);
	let t14;
	if ($[37] !== center || $[38] !== map || $[39] !== mapClicked) {
		t14 = () => {
			if (!map) return;
			if (!mapClicked) map.getView().setCenter(center);
		};
		$[37] = center;
		$[38] = map;
		$[39] = mapClicked;
		$[40] = t14;
	} else t14 = $[40];
	let t15;
	if ($[41] !== map) {
		t15 = [map];
		$[41] = map;
		$[42] = t15;
	} else t15 = $[42];
	useEffect(t14, t15);
	let t16;
	if ($[43] !== map || $[44] !== projection) {
		t16 = () => {
			if (!map) return;
			map.getview().getprojection(projection);
		};
		$[43] = map;
		$[44] = projection;
		$[45] = t16;
	} else t16 = $[45];
	let t17;
	if ($[46] !== projection) {
		t17 = [projection];
		$[46] = projection;
		$[47] = t17;
	} else t17 = $[47];
	useEffect(t16, t17);
	let t18;
	if ($[48] !== map) {
		t18 = { map };
		$[48] = map;
		$[49] = t18;
	} else t18 = $[49];
	let t19;
	if ($[50] !== addressBar || $[51] !== center || $[52] !== changeLat || $[53] !== changeLong || $[54] !== setPosition) {
		t19 = addressBar && /* @__PURE__ */ React.createElement("div", { style: {
			marginLeft: 35,
			marginTop: -6
		} }, /* @__PURE__ */ React.createElement(GeoLocation, {
			center,
			changeLat,
			changeLong,
			setPosition
		}));
		$[50] = addressBar;
		$[51] = center;
		$[52] = changeLat;
		$[53] = changeLong;
		$[54] = setPosition;
		$[55] = t19;
	} else t19 = $[55];
	const t20 = height || "85vh";
	let t21;
	if ($[56] !== t20 || $[57] !== width) {
		t21 = {
			height: t20,
			width
		};
		$[56] = t20;
		$[57] = width;
		$[58] = t21;
	} else t21 = $[58];
	let t22;
	if ($[59] !== children || $[60] !== t21) {
		t22 = /* @__PURE__ */ React.createElement("div", {
			ref: mapRef,
			style: t21
		}, children);
		$[59] = children;
		$[60] = t21;
		$[61] = t22;
	} else t22 = $[61];
	let t23;
	if ($[62] !== t18 || $[63] !== t19 || $[64] !== t22) {
		t23 = /* @__PURE__ */ React.createElement(MapContext.Provider, { value: t18 }, t19, t22);
		$[62] = t18;
		$[63] = t19;
		$[64] = t22;
		$[65] = t23;
	} else t23 = $[65];
	return t23;
};
function _temp(e) {
	return click(e);
}
function _temp2(f) {
	if (f.getGeometry().getType() === "Point") return true;
}
//#endregion
export { Map };

//# sourceMappingURL=Map.mjs.map