import { useOptions } from "../OptionsContext.js";
import { useRouter } from "../../router/RouteContext.js";
import { getInitialValuesFromQueryParams } from "../utils.js";
import { useEnvironment } from "../EnvironmentContext.js";
import { MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE, buildRedirectUrl } from "../../common/redirects.js";
import { createContext, useContext, useMemo } from "react";
import { SIGN_IN_INITIAL_VALUE_KEYS } from "@clerk/shared/internal/clerk-js/constants";
import { useClerk } from "@clerk/shared/react";
import { buildURL } from "@clerk/shared/internal/clerk-js/url";
import { RedirectUrls } from "@clerk/shared/internal/clerk-js/redirectUrls";
import { getTaskEndpoint } from "@clerk/shared/internal/clerk-js/sessionTasks";
import { isAbsoluteUrl } from "@clerk/shared/url";

//#region src/contexts/components/SignIn.ts
const SignInContext = createContext(null);
const useSignInContext = () => {
	const context = useContext(SignInContext);
	const { navigate, basePath, startPath } = useRouter();
	const { displayConfig, userSettings } = useEnvironment();
	const { queryParams, queryString } = useRouter();
	const signUpMode = userSettings.signUp.mode;
	const options = useOptions();
	const clerk = useClerk();
	if (context === null || context.componentName !== "SignIn") throw new Error(`Clerk: useSignInContext called outside of the mounted SignIn component.`);
	const isCombinedFlow = signUpMode !== "restricted" && Boolean(!options.signUpUrl && options.signInUrl && !isAbsoluteUrl(options.signInUrl)) && context.withSignUp !== false || context.withSignUp || false;
	const { componentName, mode,...ctx } = context;
	const initialValuesFromQueryParams = useMemo(() => getInitialValuesFromQueryParams(queryString, SIGN_IN_INITIAL_VALUE_KEYS), []);
	const redirectUrls = new RedirectUrls(options, {
		...ctx,
		signInFallbackRedirectUrl: ctx.signInFallbackRedirectUrl || ctx.fallbackRedirectUrl,
		signInForceRedirectUrl: ctx.signInForceRedirectUrl || ctx.forceRedirectUrl
	}, queryParams, mode);
	delete ctx.fallbackRedirectUrl;
	delete ctx.forceRedirectUrl;
	const afterSignInUrl = clerk.buildUrlWithAuth(redirectUrls.getAfterSignInUrl());
	const afterSignUpUrl = clerk.buildUrlWithAuth(redirectUrls.getAfterSignUpUrl());
	const navigateAfterSignIn = () => navigate(afterSignInUrl);
	let signInUrl = ctx.routing === "path" && ctx.path || options.signInUrl || displayConfig.signInUrl;
	let signUpUrl = isCombinedFlow ? ctx.routing === "path" && ctx.path || options.signUpUrl || displayConfig.signUpUrl : ctx.signUpUrl || options.signUpUrl || displayConfig.signUpUrl;
	let waitlistUrl = ctx.waitlistUrl || options.waitlistUrl || displayConfig.waitlistUrl;
	const preservedParams = redirectUrls.getPreservedSearchParams();
	signInUrl = buildURL({
		base: signInUrl,
		hashSearchParams: [queryParams, preservedParams]
	}, { stringify: true });
	signUpUrl = buildURL({
		base: signUpUrl,
		hashSearchParams: [queryParams, preservedParams]
	}, { stringify: true });
	waitlistUrl = buildURL({
		base: waitlistUrl,
		hashSearchParams: [queryParams, preservedParams]
	}, { stringify: true });
	const authQueryString = redirectUrls.toSearchParams().toString();
	const emailLinkRedirectUrl = buildRedirectUrl({
		routing: ctx.routing,
		baseUrl: signUpUrl,
		authQueryString,
		path: ctx.path,
		endpoint: isCombinedFlow ? "/create" + MAGIC_LINK_VERIFY_PATH_ROUTE : MAGIC_LINK_VERIFY_PATH_ROUTE
	});
	const ssoCallbackUrl = buildRedirectUrl({
		routing: ctx.routing,
		baseUrl: signUpUrl,
		authQueryString,
		path: ctx.path,
		endpoint: isCombinedFlow ? "/create" + SSO_CALLBACK_PATH_ROUTE : SSO_CALLBACK_PATH_ROUTE
	});
	if (isCombinedFlow) signUpUrl = buildURL({
		base: signInUrl,
		hashPath: "/create",
		hashSearchParams: [queryParams, preservedParams]
	}, { stringify: true });
	const signUpContinueUrl = buildURL({
		base: signUpUrl,
		hashPath: "/continue"
	}, { stringify: true });
	const navigateOnSetActive = async ({ session, redirectUrl }) => {
		const currentTask = session.currentTask;
		if (!currentTask) return navigate(redirectUrl);
		const taskEndpoint = getTaskEndpoint(currentTask);
		const taskNavigationPath = isCombinedFlow ? "/create" + taskEndpoint : taskEndpoint;
		return navigate(`/${basePath + startPath + taskNavigationPath}`);
	};
	const taskUrl = clerk.session?.currentTask ? clerk.__internal_getOption("taskUrls")?.[clerk.session?.currentTask.key] ?? buildRedirectUrl({
		routing: ctx.routing,
		baseUrl: signInUrl,
		authQueryString,
		path: ctx.path,
		endpoint: isCombinedFlow ? "/create" + getTaskEndpoint(clerk.session?.currentTask) : getTaskEndpoint(clerk.session?.currentTask)
	}) : null;
	return {
		...ctx,
		transferable: ctx.transferable ?? true,
		oauthFlow: ctx.oauthFlow || "auto",
		componentName,
		signUpUrl,
		signInUrl,
		waitlistUrl,
		afterSignInUrl,
		afterSignUpUrl,
		emailLinkRedirectUrl,
		ssoCallbackUrl,
		navigateAfterSignIn,
		signUpContinueUrl,
		queryParams,
		initialValues: {
			...ctx.initialValues,
			...initialValuesFromQueryParams
		},
		authQueryString,
		isCombinedFlow,
		navigateOnSetActive,
		taskUrl
	};
};

//#endregion
export { SignInContext, useSignInContext };
//# sourceMappingURL=SignIn.js.map