//#region extensions/crypto/src/services/confirmation-store.ts
const CONFIRMATION_TIMEOUT_MS = 300 * 1e3;
/** Pending confirmations keyed by a compound key: `${userId}` (most recent wins). */
const pendingByUser = /* @__PURE__ */ new Map();
/** All pending confirmations keyed by executionId for cleanup. */
const pendingByExecution = /* @__PURE__ */ new Map();
/**
* Create a pending confirmation for a step.
* Returns a Promise<boolean> that resolves when the user responds.
* Times out after 5 minutes (resolves false).
*/
function createPendingConfirmation(opts) {
	return new Promise((resolve) => {
		const pending = {
			...opts,
			createdAt: Date.now(),
			resolve
		};
		const existing = pendingByUser.get(opts.userId);
		if (existing) {
			existing.resolve(false);
			pendingByExecution.delete(existing.executionId);
		}
		pendingByUser.set(opts.userId, pending);
		pendingByExecution.set(opts.executionId, pending);
		setTimeout(() => {
			if (pendingByUser.get(opts.userId) === pending) {
				pending.resolve(false);
				pendingByUser.delete(opts.userId);
				pendingByExecution.delete(opts.executionId);
			}
		}, CONFIRMATION_TIMEOUT_MS);
	});
}
/**
* Respond to the most recent pending confirmation for a user.
* Returns the confirmation details if found, or null if no pending confirmation.
*/
function respondToConfirmation(userId, approved) {
	const pending = pendingByUser.get(userId);
	if (!pending) return null;
	pending.resolve(approved);
	pendingByUser.delete(userId);
	pendingByExecution.delete(pending.executionId);
	return pending;
}
/**
* Get the pending confirmation for a user (for display purposes).
*/
function getPendingConfirmation(userId) {
	const pending = pendingByUser.get(userId);
	if (!pending) return null;
	if (Date.now() - pending.createdAt > CONFIRMATION_TIMEOUT_MS) {
		pending.resolve(false);
		pendingByUser.delete(userId);
		pendingByExecution.delete(pending.executionId);
		return null;
	}
	return pending;
}
/**
* Cancel all pending confirmations for an execution (e.g., when plan is cancelled).
*/
function cancelExecutionConfirmations(executionId) {
	const pending = pendingByExecution.get(executionId);
	if (pending) {
		pending.resolve(false);
		pendingByUser.delete(pending.userId);
		pendingByExecution.delete(executionId);
	}
}
/**
* How many confirmations are pending.
*/
function pendingCount() {
	return pendingByUser.size;
}
//#endregion
export { cancelExecutionConfirmations, createPendingConfirmation, getPendingConfirmation, pendingCount, respondToConfirmation };

//# sourceMappingURL=confirmation-store.mjs.map