import { FluentButton } from "../../UI/inputs/buttons/FluentButton.mjs";
import { FreeFormField } from "./FreeFormField.mjs";
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { fromLonLat } from "ol/proj";
//#region src/map-library/geoLocation/GeoLocation.jsx
const useStyles = makeStyles((theme) => ({
	root: {
		display: "flex",
		flexDirection: "row"
	},
	button: { marginTop: theme.spacing(1) }
}));
const GeoLocation = (props) => {
	const [city, setCity] = useState("");
	const [state, setState] = useState("");
	const [zip, setZip] = useState("");
	const [country, setCountry] = useState("");
	const [house, setHouse] = useState("");
	const [address1, setAddress1] = useState("");
	const [address2, setAddress2] = useState("");
	const [response, setResponse] = useState("{}");
	const [coordinates, setCoordinates] = useState([-117.29982, 33.054275]);
	const { sendSelected, center } = props;
	const debug = true;
	async function geocode(address) {
		let formattedAddress = address.split(" ").join("+");
		formattedAddress = formattedAddress.split("\n").join("+");
		console.log("FORMATTED ADDRESS", formattedAddress);
		const url = `https://geocoder.say.rocks/?address=${formattedAddress}&debug=${debug}`;
		try {
			const response_0 = await fetch(url, {});
			if (!response_0.ok) throw new Error(response_0.statusText);
			const json = await response_0.json();
			const { house_number, country: country_0, house: house_0, road, city: city_0, state: state_0, unit, postcode } = json;
			setAddress1(`${house_number} ${road}`);
			setAddress2(`${unit}`);
			setHouse(house_0);
			setCity(city_0);
			setState(state_0);
			setZip(postcode);
			setCountry(country_0);
			setResponse(JSON.stringify(json, null, 2));
		} catch (err) {
			console.log(`Node: ${err}`);
		}
	}
	async function getPoints(address_0) {
		const url_0 = `http://dev.virtualearth.net/REST/v1/Locations/US/${house}%20${address1},%20${city},%20${state}?o=json&key=AuLp5uwpaqURqvvQt3hd8V5YDYx2yCWSUZuuHHhgwR5PYW8vlOT7Zh__eC-Su9rA`;
		try {
			const response_1 = await fetch(url_0, {});
			if (!response_1.ok) throw new Error(response_1.statusText);
			const json_0 = await response_1.json();
			console.log("BING RESPONSE: ", json_0.resourceSets[0].resources[0].geocodePoints[0].coordinates);
			setCoordinates(json_0.resourceSets[0].resources[0].geocodePoints[0].coordinates);
			sendSelected(fromLonLat(json_0.resourceSets[0].resources[0].geocodePoints[0].coordinates.reverse()));
		} catch (err_0) {
			console.log(`Node: ${err_0}`);
		}
	}
	useEffect(() => {
		const listener = (event) => {
			if (event.code === "Enter" || event.code === "NumpadEnter") {
				console.log("Enter key was pressed.", event);
				getPoints();
			}
		};
		document.addEventListener("keydown", listener);
		return () => {
			document.removeEventListener("keydown", listener);
		};
	}, [center]);
	const classes = useStyles();
	return /* @__PURE__ */ React.createElement("div", { className: classes.root }, /* @__PURE__ */ React.createElement(FreeFormField, { didUpdateText: (text) => {
		if (text.length) geocode(text);
		else {
			setAddress1("");
			setAddress2("");
			setCity("");
			setState("");
			setZip("");
			setCountry("");
			setResponse("{}");
		}
	} }), /* @__PURE__ */ React.createElement(FluentButton, {
		className: classes.button,
		size: "small",
		onClick: getPoints
	}, "Go"));
};
//#endregion
export { GeoLocation as default };

//# sourceMappingURL=GeoLocation.mjs.map