import { ConfigService } from "../../../configService/index.mjs";
import { apiMutate } from "../../useAxiosMutate.mjs";
import { LoadingIndicator } from "../../../UI/loading/LoadingIndicator/LoadingIndicator.mjs";
import React, { useEffect } from "react";
import { useLocation } from "react-router-dom";
//#region src/utilities/redirect/pages/OutlookCallback.jsx
function OutlookCallback() {
	const location = useLocation();
	useEffect(() => {
		const fetchData = async () => {
			const urlParams = new URLSearchParams(location.search);
			const code = urlParams.get("code");
			const token = urlParams.get("outlookAccessToken");
			if (code) {
				await exchangeCodeForToken(code);
				window.opener.postMessage("loginSuccess", "*");
			} else if (token) localStorage.setItem("outlookAccessToken", token);
		};
		fetchData();
	}, [location]);
	function addSecondsToCurrentTimeUnix(seconds) {
		return Date.now() + seconds * 1e3;
	}
	const exchangeCodeForToken = async (code_0) => {
		try {
			if (!ConfigService.integrationV2ApiUrl) throw new Error("REACT_APP_INTEGRATION_V2_API_BASE is undefined");
			if (!ConfigService.config.UNITY_URL) throw new Error("REACT_APP_UNITY_URL is undefined");
			const data = await (await apiMutate(ConfigService.integrationV2ApiUrl, `OutlookIntegrationAccessToken/getAccessToken`, {
				method: "post",
				data: {
					option: "code",
					value: code_0,
					redirectUri: ConfigService.config.UNITY_URL + "/aad_redirect"
				}
			})).data;
			data.accessToken && localStorage.setItem("outlookAccessToken", data.accessToken);
			data.refreshToken && localStorage.setItem("outlookRefreshToken", data.refreshToken);
			data.expiresIn && localStorage.setItem("outlookExpiresAt", addSecondsToCurrentTimeUnix(data.expiresIn));
			window.close();
		} catch (error) {
			console.error("Error exchanging code for token", error);
		}
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(LoadingIndicator, null));
}
//#endregion
export { OutlookCallback };

//# sourceMappingURL=OutlookCallback.mjs.map