globalThis.process ??= {};
globalThis.process.env ??= {};
import { f as createDbNotificationStore, n as getFullDb } from "./db_BQ9NAt8x.mjs";
//#region src/lib/notifications.ts
var SUBMISSION_CREATED_EVENT = "submission.created";
var COMMENT_CREATED_EVENT = "comment.created";
var EVENT_PAYLOADS = {
	"submission.created": {
		title: "New contact submission",
		href: "/admin/submissions",
		kind: "submission"
	},
	"comment.created": {
		title: "New comment pending moderation",
		href: "/admin/comments",
		kind: "comment"
	}
};
/** Notifications db handle — reuses the host full-db client typed to NotificationsSchema. */
function getNotificationsDb(env) {
	return getFullDb(env).db;
}
/** Recipients = active admins (users who pass the linked route's requireAdmin gate today). */
async function resolveAdminRecipientIds(engine) {
	const ids = [];
	let offset = 0;
	while (true) {
		const page = await engine.listUsers({
			limit: 500,
			offset
		});
		for (const user of page.users) if (user.status === "active" && user.roles.includes("admin")) ids.push(user.id);
		offset += page.users.length;
		if (offset >= page.total || page.users.length === 0) break;
	}
	return ids;
}
/** Best-effort fan-out — never throws; one failed put must not abort others. */
async function notifyAdmins(env, engine, event) {
	try {
		const recipients = await resolveAdminRecipientIds(engine);
		if (recipients.length === 0) return;
		const payload = EVENT_PAYLOADS[event];
		const store = createDbNotificationStore(getNotificationsDb(env));
		await Promise.allSettled(recipients.map((userId) => store.put({
			userId,
			title: payload.title,
			href: payload.href,
			kind: payload.kind
		})));
	} catch {}
}
//#endregion
export { notifyAdmins as i, SUBMISSION_CREATED_EVENT as n, getNotificationsDb as r, COMMENT_CREATED_EVENT as t };
