//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
	if (from && typeof from === "object" || typeof from === "function") {
		for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
			key = keys[i];
			if (!__hasOwnProp.call(to, key) && key !== except) {
				__defProp(to, key, {
					get: ((k) => from[k]).bind(null, key),
					enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
				});
			}
		}
	}
	return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
	value: mod,
	enumerable: true
}) : target, mod));

//#endregion
const require_updateAuthFromVerifyResponse = require('./updateAuthFromVerifyResponse-CElYpi97.cjs');
let zod_mini = require("zod/mini");
zod_mini = __toESM(zod_mini);

//#region src/utils/retryOnFail/InvalidRetryOnFailCallError.ts
/**
* This error is thrown when the `retryOnFail` function is called with an invalid
* number of retries (i.e. less than 0).
*/
var InvalidRetryOnFailCallError = class extends require_updateAuthFromVerifyResponse.BaseError {
	constructor(maxRetries) {
		super({
			cause: null,
			code: "invalid_retry_on_fail_call_error",
			docsUrl: null,
			name: "InvalidRetryOnFailCallError",
			shortMessage: `Invalid retries parameter for retryOnFail call: ${maxRetries}`
		});
	}
};

//#endregion
//#region src/utils/retryOnFail/retryOnFail.ts
/** @notInstrumented */
const retryOnFail = async ({ delay = 0, fn, maxRetries }) => {
	for (let retry = 0; retry <= maxRetries; retry++) try {
		return await fn();
	} catch (error) {
		if (retry >= maxRetries) throw error;
		if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));
	}
	/**
	* Reaching this point should never happen and this
	* error is thrown to help us debug the issue.
	*/
	throw new InvalidRetryOnFailCallError(maxRetries);
};

//#endregion
//#region src/modules/auth/isSignedIn/isSignedIn.ts
/**
* Checks if the user is currently signed in to the Dynamic client.
*
* The client is considered to be in a signed in state if a user has
* authenticated or if the client has at least one wallet connected.
*
* @deprecated What counts as "signed in" is your app's business logic, not the
* SDK's. This helper bakes in one opinion (a user OR any connected wallet),
* which rarely matches every app and forces a dependency on us for every
* variation. Compose your own check from the underlying primitives instead —
* e.g. `Boolean(client.user) || isAnyWalletAccountConnected(client)` — and
* layer in whatever your app requires (device registration, completed
* onboarding, satisfied MFA, a required embedded wallet, …). See the
* "Check if signed in" guide:
* https://www.dynamic.xyz/docs/javascript/user-session-management
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if the user is signed in, false otherwise.
* @notInstrumented
*/
const isSignedIn = (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => Boolean(client.user || require_updateAuthFromVerifyResponse.getWalletAccounts(client).length > 0);

//#endregion
//#region src/services/storage/createStorageKeySchema/createStorageKeySchema.ts
/** @notInstrumented */
const createStorageKeySchema = (params) => {
	return params;
};

//#endregion
//#region src/modules/projectSettings/fetchProjectSettings/projectSettingsExpirationScheme.ts
/**
* The schema to track the expiration time of the project settings.
*/
const projectSettingsExpirationStorageKeySchema = createStorageKeySchema({
	key: "projectSettingsExpiration",
	schema: zod_mini.number()
});

//#endregion
//#region src/modules/projectSettings/fetchProjectSettings/fetchProjectSettings.ts
/**
* Expiration time of the project settings in milliseconds.
*/
const PROJECT_SETTINGS_EXPIRATION_TIME = 1e3 * 60 * 5;
/**
* Fetches and updates the project settings from the API.
*
* This function retrieves the latest project configuration settings
* from Dynamic's servers, including authentication options, enabled chains,
* and security configurations. The settings are cached for performance.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves to the updated project settings.
* @notInstrumented
* @noHook Internal settings fetch that is not part of the public hook surface.
*/
const fetchProjectSettings = async (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const currentExpiration = await core.storage.getItem(projectSettingsExpirationStorageKeySchema);
	if (Boolean(client.projectSettings) && !(currentExpiration && currentExpiration < Date.now()) && isSignedIn(client)) return client.projectSettings;
	const apiClient = require_updateAuthFromVerifyResponse.createApiClient({}, client);
	core.logger.debug("[fetchProjectSettings] Fetching project settings...");
	const doFetch = async () => apiClient.getEnvironmentSettings({
		environmentId: core.environmentId,
		sdkVersion: `${require_updateAuthFromVerifyResponse.CLIENT_SDK_NAME}/${core.version}`
	}, { credentials: "omit" });
	const projectSettings = await retryOnFail({
		fn: doFetch,
		maxRetries: 2
	});
	core.state.set({ projectSettings: projectSettings ?? null });
	await core.storage.setItem(projectSettingsExpirationStorageKeySchema, Date.now() + PROJECT_SETTINGS_EXPIRATION_TIME);
	return projectSettings;
};

//#endregion
//#region src/modules/sessionKeys/generateSessionKeys/generateSessionKeys.ts
/** @notInstrumented */
const generateSessionKeys = async (client) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const currentState = core.state.get();
	const isRotation = Boolean(currentState.sessionKeys);
	const logContext = {
		environmentId: core.environmentId,
		operation: isRotation ? "rotate" : "create",
		userId: currentState.user?.id ?? null
	};
	core.logger.info(isRotation ? "[generateSessionKeys] Rotating session key..." : "[generateSessionKeys] Creating session key...", logContext);
	await core.keychain.removeKey("session");
	const publicKey = await core.keychain.generateKey("session");
	require_updateAuthFromVerifyResponse.assertDefined(publicKey, "Public key not found");
	core.state.set({ sessionKeys: publicKey });
	core.logger.info(isRotation ? "[generateSessionKeys] Session key rotated" : "[generateSessionKeys] Session key created", logContext);
	return { publicKey };
};

//#endregion
//#region src/modules/wallets/utils/getAvailableWalletProvidersFromWalletAccounts/getAvailableWalletProvidersFromWalletAccounts.ts
/** @notInstrumented */
const getAvailableWalletProvidersFromWalletAccounts = (client) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const walletProvidersMap = /* @__PURE__ */ new Map();
	require_updateAuthFromVerifyResponse.getWalletAccounts(client).forEach((walletAccount) => {
		if (walletProvidersMap.has(walletAccount.walletProviderKey)) return;
		try {
			const walletProvider = require_updateAuthFromVerifyResponse.getWalletProviderFromWalletAccount({ walletAccount }, client);
			walletProvidersMap.set(walletAccount.walletProviderKey, walletProvider);
		} catch (error) {
			core.logger.debug("Wallet provider not found for wallet account", {
				error,
				walletAccount: walletAccount.address
			});
		}
	});
	return Array.from(walletProvidersMap.values());
};

//#endregion
//#region src/modules/wallets/destroyWalletProviders/destroyWalletProviders.ts
/**
* Tear down every wallet provider on the client.
*
* Each provider's `destroy` is responsible for its own local + remote
* cleanup. We swallow errors so a failing destroy on one provider does not
* block teardown of the others (e.g. during logout).
*
* @notInstrumented
*/
const destroyWalletProviders = async ({ reason }, client) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const walletProviders = getAvailableWalletProvidersFromWalletAccounts(client);
	await Promise.all(walletProviders.map(async (walletProvider) => {
		if (!walletProvider.destroy) return;
		try {
			await walletProvider.destroy({ reason });
		} catch (err) {
			core.logger.error(`Error destroying wallet provider ${walletProvider.key}`, err);
		}
	}));
};

//#endregion
//#region src/modules/auth/logoutWithReason/logoutWithReason.ts
/**
* Logs the user out with an explicit reason.
*
* Revokes the session, clears auth state, terminates wallet providers, and
* emits the `logout` client event with the given reason. The reason is also
* captured as an argument on the function-instrumentation event, which is
* how Datadog distinguishes between different logout causes.
*
* Use this instead of the public `logout` when triggering a logout from
* internal or extension code — the caller owns the reason string, so the
* telemetry correctly attributes *why* the logout happened.
*
* @param params - The logout params.
* @param params.reason - Why the logout is being triggered. Extension authors
*   needing a reason outside the SDK catalog can cast with `as LogoutReason`.
* @param client - The Dynamic client instance.
* @instrumented
*/
const logoutWithReason = async ({ reason }, client) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	core.logger.debug("[logoutWithReason] Logging out...", { reason });
	await destroyWalletProviders({ reason }, client);
	if (client.user !== null) {
		const apiClient = require_updateAuthFromVerifyResponse.createApiClient({}, client);
		try {
			await apiClient.revokeSession({ environmentId: core.environmentId });
		} catch (error) {
			core.logger.error("Failed to revoke session", error);
		}
		/**
		* This deletes the auth cookie if it exists.
		* If the cookie doesn't exist, this sets a new cookie that expires immediately.
		*/
		if (require_updateAuthFromVerifyResponse.isCookieEnabled(client)) require_updateAuthFromVerifyResponse.setCookie(`${require_updateAuthFromVerifyResponse.DYNAMIC_AUTH_COOKIE_NAME}=; Max-Age=-99999999; path=/; SameSite=Lax`);
	}
	await core.keychain.removeKey("session");
	core.state.set({
		captchaToken: null,
		elevatedAccessTokens: [],
		legacyToken: null,
		mfaToken: null,
		sessionExpiresAt: null,
		sessionKeys: null,
		token: null,
		unverifiedWalletAccounts: [],
		user: null
	});
	require_updateAuthFromVerifyResponse.emitEvent({
		args: { reason },
		event: "logout"
	}, client);
	fetchProjectSettings(client);
	generateSessionKeys(client);
};
const __logoutWithReason_impl = logoutWithReason;
const __logoutWithReason_wrapped = require_updateAuthFromVerifyResponse.instrumentFunction({
	fn: __logoutWithReason_impl,
	functionName: "logoutWithReason",
	getCore: () => {
		try {
			return require_updateAuthFromVerifyResponse.getCore(require_updateAuthFromVerifyResponse.getDefaultClient());
		} catch {
			return;
		}
	}
});

//#endregion
//#region src/modules/mfa/consumeMfaToken/consumeMfaToken.ts
/**
* Consumes and clears the current MFA token from the client state.
*
* This function retrieves the MFA token obtained from a successful MFA authentication,
* and removes it from the client, to prevent it from being used again.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns The MFA token that was consumed.
* @throws Error if no MFA token is found.
* @instrumented
* @redact-params
*/
const consumeMfaToken = (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const mfaToken = core.state.get().mfaToken;
	require_updateAuthFromVerifyResponse.assertDefined(mfaToken, "No MFA token found");
	core.state.set({ mfaToken: null });
	return mfaToken;
};
const __consumeMfaToken_impl = consumeMfaToken;
const __consumeMfaToken_wrapped = require_updateAuthFromVerifyResponse.instrumentFunction({
	fn: __consumeMfaToken_impl,
	functionName: "consumeMfaToken",
	getCore: () => {
		try {
			return require_updateAuthFromVerifyResponse.getCore(require_updateAuthFromVerifyResponse.getDefaultClient());
		} catch {
			return;
		}
	},
	redactAll: true
});

//#endregion
//#region src/modules/mfa/getMfaMethods/getMfaMethods.ts
/**
* Retrieves the available MFA methods for the current user.
*
* This function fetches information about which multi-factor authentication
* methods are available and configured for the user's account.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves to the user's MFA methods configuration.
* @notInstrumented
*/
const getMfaMethods = async (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	return await require_updateAuthFromVerifyResponse.createApiClient({}, client).getUserMfaMethods({
		environmentId: core.environmentId,
		verifiedOnly: true
	});
};

//#endregion
//#region src/modules/mfa/isMfaRequiredForAction/isMfaRequiredForAction.ts
/**
* Checks if MFA is required for a specific action.
*
* This function determines whether multi-factor authentication is required
* for the specified action based on project settings and user configuration.
*
* @param params.mfaAction - The action to check MFA requirements for.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves to true if MFA is required for the action, false otherwise.
* @notInstrumented
*/
const isMfaRequiredForAction = async ({ mfaAction }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const projectSettings = client.projectSettings;
	if (!projectSettings?.security?.mfa?.actions?.some((action) => action.action === mfaAction && action.required)) return false;
	if (projectSettings?.security?.mfa?.required) return true;
	return (await getMfaMethods(client)).userHasVerifiedMfaMethods;
};

//#endregion
Object.defineProperty(exports, '__consumeMfaToken_wrapped', {
  enumerable: true,
  get: function () {
    return __consumeMfaToken_wrapped;
  }
});
Object.defineProperty(exports, '__logoutWithReason_wrapped', {
  enumerable: true,
  get: function () {
    return __logoutWithReason_wrapped;
  }
});
Object.defineProperty(exports, '__toESM', {
  enumerable: true,
  get: function () {
    return __toESM;
  }
});
Object.defineProperty(exports, 'createStorageKeySchema', {
  enumerable: true,
  get: function () {
    return createStorageKeySchema;
  }
});
Object.defineProperty(exports, 'fetchProjectSettings', {
  enumerable: true,
  get: function () {
    return fetchProjectSettings;
  }
});
Object.defineProperty(exports, 'generateSessionKeys', {
  enumerable: true,
  get: function () {
    return generateSessionKeys;
  }
});
Object.defineProperty(exports, 'getMfaMethods', {
  enumerable: true,
  get: function () {
    return getMfaMethods;
  }
});
Object.defineProperty(exports, 'isMfaRequiredForAction', {
  enumerable: true,
  get: function () {
    return isMfaRequiredForAction;
  }
});
Object.defineProperty(exports, 'isSignedIn', {
  enumerable: true,
  get: function () {
    return isSignedIn;
  }
});
Object.defineProperty(exports, 'retryOnFail', {
  enumerable: true,
  get: function () {
    return retryOnFail;
  }
});
//# sourceMappingURL=isMfaRequiredForAction-Btejc5g3.cjs.map