import { useRouter } from "../router/RouteContext.js";
import { useCoreSignUp } from "../contexts/CoreClientContext.js";
import { EmailLinkStatusCard } from "./EmailLinkStatusCard.js";
import { sleep } from "../utils/sleep.js";
import React from "react";
import { useClerk } from "@clerk/shared/react";
import { jsx } from "@emotion/react/jsx-runtime";
import { EmailLinkErrorCodeStatus, isEmailLinkError } from "@clerk/shared/error";
import { completeSignUpFlow } from "@clerk/shared/internal/clerk-js/completeSignUpFlow";

//#region src/common/EmailLinkVerify.tsx
const EmailLinkVerify = (props) => {
	const { redirectUrl, redirectUrlComplete, verifyEmailPath, verifyPhonePath, continuePath } = props;
	const { handleEmailLinkVerification } = useClerk();
	const { navigate } = useRouter();
	const signUp = useCoreSignUp();
	const [verificationStatus, setVerificationStatus] = React.useState("loading");
	const startVerification = async () => {
		try {
			await sleep(750);
			await handleEmailLinkVerification({
				redirectUrlComplete,
				redirectUrl
			}, navigate);
			setVerificationStatus("verified_switch_tab");
			await sleep(750);
			await completeSignUpFlow({
				signUp,
				verifyEmailPath,
				verifyPhonePath,
				continuePath,
				navigate
			});
		} catch (err) {
			if (isEmailLinkError(err) && (err.code === EmailLinkErrorCodeStatus.Expired || err.code === EmailLinkErrorCodeStatus.ClientMismatch)) {
				setVerificationStatus(err.code);
				return;
			}
			setVerificationStatus(EmailLinkErrorCodeStatus.Failed);
		}
	};
	React.useEffect(() => {
		startVerification();
	}, []);
	return /* @__PURE__ */ jsx(EmailLinkStatusCard, {
		title: props.texts[verificationStatus]?.title || "",
		subtitle: props.texts[verificationStatus]?.subtitle || "",
		status: verificationStatus
	});
};

//#endregion
export { EmailLinkVerify };
//# sourceMappingURL=EmailLinkVerify.js.map