globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { d as mapContentError, l as jsonError, u as jsonOk } from "./crypto_CY2zBtG3.mjs";
import { T as listAudit, n as getFullDb, w as InvalidCursorError } from "./db_DCN9iNkm.mjs";
import { i as requireAdmin } from "./auth_B0DWM0G6.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/admin/audit.ts
var audit_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	prerender: () => false
});
var DATE_ONLY_RE = /^\d{4}-\d{2}-\d{2}$/;
function parseDateParam(raw, field, endOfDay = false) {
	if (raw === null) return void 0;
	const value = endOfDay && DATE_ONLY_RE.test(raw) ? `${raw}T23:59:59.999Z` : raw;
	const d = new Date(value);
	if (Number.isNaN(d.getTime())) throw new RangeError(`invalid ${field}`);
	return d;
}
var GET = async ({ request, cookies }) => {
	try {
		await requireAdmin(request, env, cookies);
		const { db } = getFullDb(env);
		const q = new URL(request.url).searchParams;
		const limitRaw = q.get("limit");
		const filter = {
			actor: q.get("actor") ?? void 0,
			entityType: q.get("entityType") ?? void 0,
			entityId: q.get("entityId") ?? void 0,
			action: q.get("action") ?? void 0,
			q: q.get("q") ?? void 0,
			from: parseDateParam(q.get("from"), "from"),
			to: parseDateParam(q.get("to"), "to", true),
			cursor: q.get("cursor") ?? void 0,
			limit: limitRaw === null ? void 0 : Number(limitRaw)
		};
		const result = await listAudit(db, filter);
		return jsonOk(result);
	} catch (error) {
		if (error instanceof RangeError) return jsonError(400, "validation_error", error.message);
		if (error instanceof InvalidCursorError || error?.name === "InvalidCursorError") return jsonError(400, "validation_error", "invalid cursor");
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/audit@_@ts
var page = () => audit_exports;
//#endregion
export { page };
