globalThis.process ??= {};
globalThis.process.env ??= {};
import { l as jsonError } from "./crypto_CY2zBtG3.mjs";
import { n as getFullDb, p as createDbSubmissionStore } from "./db_DZJ1iBSP.mjs";
//#region ../../packages/forms/dist/index.js
/**
* Structural type-guard for a {@link FieldError} — cross-package safe (no `instanceof`;
* two deduped copies of the package would break identity-by-class).
*/
function isFieldError(e) {
	if (typeof e !== "object" || e === null) return false;
	const o = e;
	return typeof o.field === "string" && typeof o.code === "string" && typeof o.message === "string";
}
var OPTION_TYPES = /* @__PURE__ */ new Set([
	"select",
	"multiselect",
	"radio"
]);
/**
* Structural validation of a form DEFINITION (not a submission). Pure.
* Catches dup field names, option-types without options, bad pattern source, empty id/fields.
*/
function defineForm(def) {
	const errors = [];
	if (!def.id || def.id.trim() === "") errors.push({
		code: "empty-id",
		message: "form id must be a non-empty string"
	});
	if (!Array.isArray(def.fields) || def.fields.length === 0) errors.push({
		code: "no-fields",
		message: "form must declare at least one field"
	});
	const seen = /* @__PURE__ */ new Set();
	for (const f of def.fields ?? []) {
		if (!f.name || f.name.trim() === "") errors.push({
			code: "missing-name",
			message: "field name must be a non-empty string"
		});
		else if (seen.has(f.name)) errors.push({
			code: "duplicate-field",
			field: f.name,
			message: `duplicate field name: ${f.name}`
		});
		else seen.add(f.name);
		if (!f.label || f.label.trim() === "") errors.push({
			code: "missing-label",
			field: f.name,
			message: `field ${f.name} must have a label`
		});
		if (OPTION_TYPES.has(f.type) && (!f.options || f.options.length === 0)) errors.push({
			code: "no-options",
			field: f.name,
			message: `field ${f.name} (${f.type}) requires options`
		});
		if (f.pattern !== void 0) try {
			new RegExp(f.pattern);
		} catch {
			errors.push({
				code: "bad-pattern",
				field: f.name,
				message: `field ${f.name} has an invalid pattern source`
			});
		}
	}
	if (errors.length > 0) return {
		ok: false,
		error: errors
	};
	return {
		ok: true,
		value: def
	};
}
var validated = defineForm({
	id: "contact",
	fields: [
		{
			name: "name",
			type: "text",
			label: "Name",
			required: true,
			min: 1,
			max: 120
		},
		{
			name: "email",
			type: "text",
			label: "Email",
			required: true,
			pattern: "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$",
			max: 254
		},
		{
			name: "message",
			type: "textarea",
			label: "Message",
			required: true,
			min: 1,
			max: 5e3
		}
	],
	antispam: {
		honeypot: "website",
		minFillMs: 2e3
	}
});
if (!validated.ok) throw new Error(`CONTACT_FORM invalid: ${JSON.stringify(validated.error)}`);
/** Host-owned contact form — build-time validated via defineForm. */
var CONTACT_FORM = validated.value;
/** Forms db handle — reuses the host full-db client typed to FormsSchema. */
function getFormsDb(env) {
	return getFullDb(env).db;
}
function getSubmissionStore(env) {
	return createDbSubmissionStore(getFormsDb(env));
}
/** Render-token HMAC secret — reuses AUTH_SESSION_SECRET (no new env var). */
function renderSecret(env) {
	return env.AUTH_SESSION_SECRET;
}
function isFieldErrorArray(e) {
	return Array.isArray(e) && e.length > 0 && e.every(isFieldError);
}
function mapFormError(error) {
	if (isFieldErrorArray(error)) return new Response(JSON.stringify({ errors: error }), {
		status: 422,
		headers: { "Content-Type": "application/json" }
	});
	if (isFieldError(error)) return new Response(JSON.stringify({ errors: [error] }), {
		status: 422,
		headers: { "Content-Type": "application/json" }
	});
	const reason = error?.reason;
	if (reason === "too-fast" || reason === "bad-token") return jsonError(422, "validation_error", "That submission could not be accepted.");
	if (reason === "challenge-failed") return jsonError(422, "validation_error", "Verification failed.");
	if (reason === "honeypot") return new Response(null, { status: 200 });
	return jsonError(500, "internal_error", "internal error");
}
//#endregion
export { renderSecret as i, getSubmissionStore as n, mapFormError as r, CONTACT_FORM as t };
