globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { d as mapContentError } from "./crypto_CY2zBtG3.mjs";
import { r as enforcePublicFormRateLimit } from "./rate-limit_CABs9jHB.mjs";
import { m as recordSubmission } from "./db_BQ9NAt8x.mjs";
import { r as buildAuthEngine } from "./admin-engine_Cqzo5vQB.mjs";
import { i as notifyAdmins, n as SUBMISSION_CREATED_EVENT } from "./notifications_aUOPzd3M.mjs";
import { i as renderSecret, n as getSubmissionStore, r as mapFormError, t as CONTACT_FORM } from "./forms_Bktwg2o7.mjs";
import { i as wantsJsonResponse, n as contactRedirectWithFlash } from "./contact-flash_Cpum3OBD.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/contact.ts
var contact_exports = /* @__PURE__ */ __exportAll({
	POST: () => POST,
	prerender: () => false
});
var FIELD_NAMES = [
	"name",
	"email",
	"message"
];
var MAX_BODY_BYTES = 65536;
var POST = async ({ request }) => {
	try {
		const declaredLength = Number(request.headers.get("Content-Length") ?? "0");
		if (Number.isFinite(declaredLength) && declaredLength > MAX_BODY_BYTES) return new Response("Payload too large", { status: 413 });
		const formData = await request.formData();
		const honeypot = CONTACT_FORM.antispam?.honeypot;
		if (honeypot && formData.getAll(honeypot).some((v) => v.toString().trim())) return new Response(null, { status: 200 });
		const limited = await enforcePublicFormRateLimit(env, request);
		if (limited) return limited;
		const result = await recordSubmission({
			form: CONTACT_FORM,
			raw: formData,
			store: getSubmissionStore(env),
			antispam: {
				renderToken: formData.get("render_token")?.toString() ?? void 0,
				secret: renderSecret(env),
				now: Date.now(),
				ip: request.headers.get("CF-Connecting-IP") ?? void 0,
				userAgent: request.headers.get("User-Agent") ?? void 0,
				ref: request.headers.get("Referer") ?? void 0
			}
		});
		if (!result.ok) {
			if (wantsJsonResponse(request)) return mapFormError(result.error);
			return contactRedirectWithFlash(result.error, formData, FIELD_NAMES);
		}
		notifyAdmins(env, buildAuthEngine(env), SUBMISSION_CREATED_EVENT);
		return new Response(null, {
			status: 303,
			headers: { Location: "/contact?sent=1" }
		});
	} catch (error) {
		const mapped = mapFormError(error);
		if (mapped.status !== 500) return mapped;
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/contact@_@ts
var page = () => contact_exports;
//#endregion
export { page };
