//#region src/auth/password-reset-link-store.ts
const STORE_KEY = Symbol.for("inkeep:password-reset-link-store");
function getPendingResolvers() {
	const g = globalThis;
	if (!g[STORE_KEY]) g[STORE_KEY] = /* @__PURE__ */ new Map();
	return g[STORE_KEY];
}
/**
* Sets up a listener that resolves when `setPasswordResetLink` fires for this email.
* Call BEFORE `auth.api.requestPasswordReset()`.
*
* This creates a per-request promise bridge: the `sendResetPassword` callback
* (configured in auth.ts) calls `setPasswordResetLink`, which resolves this promise
* within the same HTTP request on the same server instance.
*/
function waitForPasswordResetLink(email, timeoutMs = 1e4) {
	const pendingResolvers = getPendingResolvers();
	const key = email.toLowerCase();
	return new Promise((resolve, reject) => {
		const timeout = setTimeout(() => {
			pendingResolvers.delete(key);
			reject(/* @__PURE__ */ new Error("Timed out waiting for password reset link"));
		}, timeoutMs);
		pendingResolvers.set(key, (entry) => {
			clearTimeout(timeout);
			pendingResolvers.delete(key);
			resolve(entry);
		});
	});
}
/**
* Called from the `sendResetPassword` callback in auth config.
* Resolves the pending promise for this email (if any).
*/
function setPasswordResetLink(entry) {
	const pendingResolvers = getPendingResolvers();
	const key = entry.email.toLowerCase();
	const resolver = pendingResolvers.get(key);
	if (resolver) resolver({
		email: entry.email,
		url: entry.url,
		token: entry.token
	});
}

//#endregion
export { setPasswordResetLink, waitForPasswordResetLink };