import { c } from "react/compiler-runtime";
import { useCallback, useEffect, useRef, useState } from "react";
var OAUTH_MESSAGE_TYPE = "loopstack:oauth:callback", POPUP_POLL_INTERVAL_MS = 500, DEFAULT_TIMEOUT_MS = 3e5;
function isOAuthCallbackMessage(e) {
	return typeof e == "object" && !!e && e.type === OAUTH_MESSAGE_TYPE;
}
function openCenteredPopup(e, o, s) {
	let l = `width=${o},height=${s},left=${Math.round(window.screenX + (window.outerWidth - o) / 2)},top=${Math.round(window.screenY + (window.outerHeight - s) / 2)},popup=yes,noopener=no`;
	return window.open(e, "loopstack-oauth", l);
}
function useOAuthPopup() {
	let o = c(11), d;
	o[0] === Symbol.for("react.memo_cache_sentinel") ? (d = { status: "idle" }, o[0] = d) : d = o[0];
	let [p, m] = useState(d), h = useRef(null), g = useRef(null), _ = useRef(null), v = useRef(null), y;
	o[1] === Symbol.for("react.memo_cache_sentinel") ? (y = () => {
		g.current &&= (clearTimeout(g.current), null), _.current &&= (clearInterval(_.current), null), v.current = null;
	}, o[1] = y) : y = o[1];
	let b = y, x;
	o[2] === Symbol.for("react.memo_cache_sentinel") ? (x = () => {
		h.current && !h.current.closed && h.current.close(), h.current = null;
	}, o[2] = x) : x = o[2];
	let S = x, C, w;
	o[3] === Symbol.for("react.memo_cache_sentinel") ? (C = () => {
		let e = (e) => {
			if (e.origin !== window.location.origin || !isOAuthCallbackMessage(e.data) || !v.current) return;
			let o = e.data;
			o.error ? m({
				status: "error",
				error: o.errorDescription || o.error
			}) : o.code && o.state ? o.state === v.current ? m({
				status: "success",
				code: o.code,
				state: o.state
			}) : m({
				status: "error",
				error: "State mismatch. Possible CSRF attack."
			}) : m({
				status: "error",
				error: "Invalid callback response."
			}), b(), S();
		};
		return window.addEventListener("message", e), () => window.removeEventListener("message", e);
	}, w = [b, S], o[3] = C, o[4] = w) : (C = o[3], w = o[4]), useEffect(C, w);
	let T, E;
	o[5] === Symbol.for("react.memo_cache_sentinel") ? (T = () => () => {
		b();
	}, E = [b], o[5] = T, o[6] = E) : (T = o[5], E = o[6]), useEffect(T, E);
	let D;
	o[7] === Symbol.for("react.memo_cache_sentinel") ? (D = (e) => {
		let { authUrl: o, state: s, popupWidth: l, popupHeight: u, timeoutMs: d } = e, f = l === void 0 ? 500 : l, p = u === void 0 ? 700 : u, y = d === void 0 ? DEFAULT_TIMEOUT_MS : d;
		b(), S(), v.current = s;
		let x = openCenteredPopup(o, f, p);
		if (!x) {
			m({ status: "blocked" });
			return;
		}
		h.current = x, m({ status: "pending" }), _.current = setInterval(() => {
			x.closed && (_.current && clearInterval(_.current), _.current = null, m(_temp), b());
		}, POPUP_POLL_INTERVAL_MS), g.current = setTimeout(() => {
			m(_temp2), b(), S();
		}, y);
	}, o[7] = D) : D = o[7];
	let O = D, k;
	o[8] === Symbol.for("react.memo_cache_sentinel") ? (k = () => {
		b(), S(), m({ status: "idle" });
	}, o[8] = k) : k = o[8];
	let A = k, j;
	return o[9] === p ? j = o[10] : (j = {
		result: p,
		open: O,
		reset: A
	}, o[9] = p, o[10] = j), j;
}
function _temp2(e) {
	return e.status === "pending" ? { status: "timeout" } : e;
}
function _temp(e) {
	return e.status === "pending" ? {
		status: "error",
		error: "Authentication window was closed."
	} : e;
}
export { useOAuthPopup };
