import { lazyCompleteSignUpFlow } from "./lazy-sign-up.js";
import { SIGN_UP_MODES } from "@clerk/shared/internal/clerk-js/constants";

//#region src/components/SignIn/handleCombinedFlowTransfer.ts
/**
* This function is used to handle transfering from a sign in to a sign up when SignIn is rendered as the combined flow.
* There is special logic to handle transfer email-based sign ups directly to verification, bypassing the initial sign up form.
*/
function handleCombinedFlowTransfer({ identifierAttribute, identifierValue, signUpMode, navigate, organizationTicket, afterSignUpUrl, clerk, handleError, redirectUrl, redirectUrlComplete, passwordEnabled, navigateOnSetActive, alternativePhoneCodeChannel, unsafeMetadata }) {
	if (signUpMode === SIGN_UP_MODES.WAITLIST) return navigate(clerk.buildWaitlistUrl(identifierAttribute === "emailAddress" ? { initialValues: { [identifierAttribute]: identifierValue } } : {}));
	clerk.client.signUp[identifierAttribute] = identifierValue;
	const paramsToForward = new URLSearchParams();
	if (organizationTicket) paramsToForward.set("__clerk_ticket", organizationTicket);
	const alternativePhoneCodeChannelParams = alternativePhoneCodeChannel ? {
		strategy: "phone_code",
		channel: alternativePhoneCodeChannel
	} : {};
	if (!passwordEnabled && !hasOptionalFields(clerk.client.signUp, identifierAttribute) && (identifierAttribute === "emailAddress" || identifierAttribute === "phoneNumber")) return clerk.client.signUp.create({
		[identifierAttribute]: identifierValue,
		...alternativePhoneCodeChannelParams,
		unsafeMetadata
	}).then(async (res) => {
		return (await lazyCompleteSignUpFlow())({
			signUp: res,
			verifyEmailPath: "create/verify-email-address",
			verifyPhonePath: "create/verify-phone-number",
			handleComplete: () => clerk.setActive({
				session: res.createdSessionId,
				navigate: async ({ session }) => {
					await navigateOnSetActive({
						session,
						redirectUrl: afterSignUpUrl
					});
				}
			}),
			navigate,
			redirectUrl,
			redirectUrlComplete
		});
	}).catch((err) => handleError(err));
	return navigate(`create`, { searchParams: paramsToForward });
}
function hasOptionalFields(signUp, identifierAttribute) {
	return signUp.optionalFields.filter((field) => {
		if (field.startsWith("oauth_") || field.startsWith("web3_") || field === "enterprise_sso") return false;
		if (field === "password") return false;
		if (identifierAttribute === "phoneNumber" && field === "phone_number") return false;
		return true;
	}).length > 0;
}

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