import { sql } from "drizzle-orm";
import { bigserial, boolean, check, index, integer, jsonb, numeric, pgTable, primaryKey, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
//#region src/schema.ts
var FieldDefinitionVersionError = class extends Error {
	field;
	detail;
	name = "FieldDefinitionVersionError";
	code = "invalid-definition-version";
	constructor(field, detail) {
		super(`field definition version ${field}: ${detail}`);
		this.field = field;
		this.detail = detail;
	}
};
function isFieldDefinitionVersionError(error) {
	return typeof error === "object" && error !== null && error.code === "invalid-definition-version" && typeof error.field === "string" && typeof error.detail === "string";
}
const MAX_DEPTH = 12;
const MAX_NODES = 1e3;
const MAX_INPUT_NODES = 1e4;
const MAX_STRING = 4096;
const MAX_BYTES = 262144;
const MAX_ITEMS = 1e3;
const forbidden = /* @__PURE__ */ new Set([
	"__proto__",
	"prototype",
	"constructor"
]);
function fail(field, detail) {
	throw new FieldDefinitionVersionError(field, detail);
}
function preflight(value, budget, depth = 0, seen = /* @__PURE__ */ new Set()) {
	if (depth > 64) fail("fieldDefinitionVersion", "exceeds maximum depth");
	if (value === null || typeof value === "boolean") {
		budget.bytes += 5;
		return;
	}
	if (typeof value === "string") {
		budget.bytes += new TextEncoder().encode(value).byteLength;
		if (value.length > MAX_STRING) fail("fieldDefinitionVersion", "contains an oversized string");
		return;
	}
	if (typeof value === "number") {
		if (!Number.isFinite(value)) fail("fieldDefinitionVersion", "contains a non-finite number");
		budget.bytes += 8;
		return;
	}
	if (value instanceof Date) {
		if (Object.getOwnPropertyNames(value).length !== 0 || Object.getOwnPropertySymbols(value).length !== 0 || Number.isNaN(Date.prototype.getTime.call(value))) fail("fieldDefinitionVersion", "contains an invalid Date");
		budget.bytes += 24;
		return;
	}
	if (typeof value !== "object" || value === null || seen.has(value)) fail("fieldDefinitionVersion", "must be an acyclic data graph");
	if (++budget.nodes > MAX_INPUT_NODES || budget.bytes > MAX_BYTES) fail("fieldDefinitionVersion", "exceeds maximum input size");
	seen.add(value);
	if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype || Object.getOwnPropertySymbols(value).length) fail("fieldDefinitionVersion", "must contain only plain data objects and arrays");
	const descriptors = Object.getOwnPropertyDescriptors(value);
	const keys = Object.keys(value);
	if (keys.length > MAX_ITEMS) fail("fieldDefinitionVersion", "contains an oversized collection");
	for (const key of keys) {
		const d = descriptors[key];
		if (forbidden.has(key) || !d.enumerable || !("value" in d)) fail(`fieldDefinitionVersion.${key}`, "contains a forbidden key or accessor");
		budget.bytes += new TextEncoder().encode(key).byteLength;
		preflight(d.value, budget, depth + 1, seen);
	}
	seen.delete(value);
	if (budget.bytes > MAX_BYTES) fail("fieldDefinitionVersion", "exceeds maximum input size");
}
function cloneData(value) {
	if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
	if (Array.isArray(value)) return Object.freeze(value.map(cloneData));
	const source = obj(value, "defaultValue");
	const output = {};
	for (const key of Object.keys(source)) output[key] = cloneData(source[key]);
	return Object.freeze(output);
}
function obj(value, path) {
	if (typeof value !== "object" || value === null || Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype) fail(path, "must be a plain object");
	return value;
}
function keys(v, allowed, path) {
	for (const k of Object.keys(v)) if (!allowed.includes(k)) fail(`${path}.${k}`, "is not allowed");
}
function str(v, path) {
	if (typeof v !== "string" || !v.length || v.length > MAX_STRING) fail(path, "must be a bounded non-empty string");
	return v;
}
function bool(v, p) {
	if (v === void 0) return;
	if (typeof v !== "boolean") fail(p, "must be a boolean");
	return v;
}
function num(v, p, integer = false, positive = false) {
	if (v === void 0) return;
	if (typeof v !== "number" || !Number.isFinite(v) || integer && !Number.isInteger(v) || positive && v <= 0) fail(p, `must be a ${positive ? "positive " : ""}${integer ? "integer" : "finite number"}`);
	return v;
}
function enumValue(v, values, p) {
	if (typeof v !== "string" || !values.includes(v)) fail(p, "has an invalid value");
	return v;
}
function array(v, p, max = MAX_ITEMS) {
	if (!Array.isArray(v) || v.length > max) fail(p, "must be a bounded array");
	return v;
}
function strings(v, p, required = false) {
	if (v === void 0 && !required) return;
	return Object.freeze(array(v, p).map((x, i) => str(x, `${p}.${i}`)));
}
function boundedMap(v, p) {
	const x = obj(v, p);
	const out = {};
	for (const k of Object.keys(x)) {
		if (!k || k.length > MAX_STRING) fail(p, "has invalid key");
		out[k] = str(x[k], `${p}.${k}`);
	}
	return Object.freeze(out);
}
function pair(min, max, p) {
	if (min !== void 0 && max !== void 0 && min > max) fail(p, "minimum exceeds maximum");
}
function safePattern(v, p) {
	const x = obj(v, p);
	keys(x, ["source", "flags"], p);
	const source = str(x.source, `${p}.source`);
	const flags = x.flags === void 0 ? void 0 : str(x.flags, `${p}.flags`);
	if (flags !== void 0 && !/^[dgimsuvy]*$/.test(flags)) fail(`${p}.flags`, "has invalid flags");
	return Object.freeze({
		source,
		...flags === void 0 ? {} : { flags }
	});
}
function fieldPath(v, p) {
	const a = array(v, p, 64);
	if (!a.length) fail(p, "must not be empty");
	return Object.freeze(a.map((x, i) => typeof x === "number" && Number.isSafeInteger(x) && x >= 0 ? x : str(x, `${p}.${i}`)));
}
function conditional(v, p) {
	if (v === void 0) return;
	return Object.freeze(array(v, p, 64).map((group, i) => Object.freeze(array(group, `${p}.${i}`, 64).map((raw, j) => {
		const x = obj(raw, `${p}.${i}.${j}`);
		keys(x, [
			"fieldPath",
			"operator",
			"value",
			"quantifier"
		], `${p}.${i}.${j}`);
		const operator = enumValue(x.operator, [
			"eq",
			"neq",
			"contains",
			"notContains",
			"gt",
			"gte",
			"lt",
			"lte",
			"empty",
			"notEmpty",
			"matches"
		], `${p}.${i}.${j}.operator`);
		const fp = fieldPath(x.fieldPath, `${p}.${i}.${j}.fieldPath`);
		const q = x.quantifier === void 0 ? void 0 : enumValue(x.quantifier, ["any", "all"], `${p}.${i}.${j}.quantifier`);
		let value = x.value;
		if (["empty", "notEmpty"].includes(operator)) {
			if (value !== void 0) fail(`${p}.${i}.${j}.value`, "is not allowed");
		} else if (operator === "matches") value = safePattern(value, `${p}.${i}.${j}.value`);
		else if ([
			"gt",
			"gte",
			"lt",
			"lte"
		].includes(operator)) {
			if (typeof value !== "number" || !Number.isFinite(value)) fail(`${p}.${i}.${j}.value`, "must be finite number");
		} else if (operator === "contains" || operator === "notContains") {
			if (typeof value !== "string" && typeof value !== "number") fail(`${p}.${i}.${j}.value`, "has invalid value");
		} else if (!(value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean")) fail(`${p}.${i}.${j}.value`, "has invalid value");
		return Object.freeze({
			fieldPath: fp,
			operator,
			...value === void 0 ? {} : { value },
			...q === void 0 ? {} : { quantifier: q }
		});
	}))));
}
function location(v, p) {
	return Object.freeze(array(v, p, 64).map((group, i) => Object.freeze(array(group, `${p}.${i}`, 64).map((raw, j) => {
		const x = obj(raw, `${p}.${i}.${j}`);
		keys(x, [
			"parameter",
			"operator",
			"value"
		], `${p}.${i}.${j}`);
		const op = enumValue(x.operator, [
			"eq",
			"neq",
			"in",
			"notIn",
			"contains"
		], `${p}.${i}.${j}.operator`);
		const value = x.value;
		const isList = Array.isArray(value);
		if (op === "in" || op === "notIn" ? !isList : isList || !(typeof value === "string" || typeof value === "number" || typeof value === "boolean")) fail(`${p}.${i}.${j}.value`, "has invalid operator/value combination");
		const copied = isList ? Object.freeze(array(value, `${p}.${i}.${j}.value`, MAX_ITEMS).map((item, k) => typeof item === "string" || typeof item === "number" ? item : fail(`${p}.${i}.${j}.value.${k}`, "must be string or number"))) : value;
		return Object.freeze({
			parameter: str(x.parameter, `${p}.${i}.${j}.parameter`),
			operator: op,
			value: copied
		});
	}))));
}
const builtin = /* @__PURE__ */ new Set([
	"text",
	"textarea",
	"number",
	"range",
	"email",
	"url",
	"password",
	"boolean",
	"select",
	"checkbox",
	"radio",
	"buttonGroup",
	"richText",
	"oembed",
	"link",
	"message",
	"image",
	"file",
	"gallery",
	"entity",
	"entityLink",
	"relationship",
	"taxonomy",
	"user",
	"date",
	"dateTime",
	"time",
	"color",
	"icon",
	"map",
	"accordion",
	"tab",
	"group",
	"repeater",
	"flexible",
	"clone"
]);
function settings(value, type, p, depth, b) {
	const x = obj(value, p);
	const common = (allowed) => keys(x, allowed, p);
	const copy = (allowed) => {
		common(allowed);
		const out = {};
		for (const k of allowed) if (x[k] !== void 0) out[k] = x[k];
		return out;
	};
	if (type === "text" || type === "email") {
		const o = copy([
			"placeholder",
			"prepend",
			"append",
			"maxLength",
			"pattern"
		]);
		for (const k of [
			"placeholder",
			"prepend",
			"append"
		]) if (o[k] !== void 0) o[k] = str(o[k], `${p}.${k}`);
		o.maxLength = num(o.maxLength, `${p}.maxLength`, true);
		if (o.maxLength !== void 0 && o.maxLength < 0) fail(`${p}.maxLength`, "must be non-negative");
		if (o.pattern !== void 0) o.pattern = safePattern(o.pattern, `${p}.pattern`);
		return Object.freeze(o);
	}
	if (type === "textarea") {
		const o = copy([
			"placeholder",
			"prepend",
			"append",
			"maxLength",
			"pattern",
			"rows",
			"newline"
		]);
		for (const k of [
			"placeholder",
			"prepend",
			"append"
		]) if (o[k] !== void 0) o[k] = str(o[k], `${p}.${k}`);
		o.maxLength = num(o.maxLength, `${p}.maxLength`, true);
		if (o.maxLength !== void 0 && o.maxLength < 0) fail(`${p}.maxLength`, "must be non-negative");
		o.rows = num(o.rows, `${p}.rows`, true, true);
		o.newline = enumValue(o.newline, [
			"preserve",
			"br",
			"paragraph"
		], `${p}.newline`);
		if (o.pattern !== void 0) o.pattern = safePattern(o.pattern, `${p}.pattern`);
		return Object.freeze(o);
	}
	if (type === "number" || type === "range") {
		const o = copy([
			"min",
			"max",
			"step",
			"integer",
			"prepend",
			"append"
		]);
		o.min = num(o.min, `${p}.min`);
		o.max = num(o.max, `${p}.max`);
		o.step = num(o.step, `${p}.step`, false, true);
		pair(o.min, o.max, p);
		o.integer = bool(o.integer, `${p}.integer`);
		for (const k of ["prepend", "append"]) if (o[k] !== void 0) o[k] = str(o[k], `${p}.${k}`);
		return Object.freeze(o);
	}
	if (type === "url") {
		const o = copy([
			"placeholder",
			"prepend",
			"append",
			"maxLength",
			"pattern",
			"schemes",
			"allowRelative"
		]);
		for (const k of [
			"placeholder",
			"prepend",
			"append"
		]) if (o[k] !== void 0) o[k] = str(o[k], `${p}.${k}`);
		o.maxLength = num(o.maxLength, `${p}.maxLength`, true);
		if (o.maxLength !== void 0 && o.maxLength < 0) fail(`${p}.maxLength`, "must be non-negative");
		o.schemes = strings(o.schemes, `${p}.schemes`, true);
		o.allowRelative = bool(o.allowRelative, `${p}.allowRelative`);
		if (o.pattern !== void 0) o.pattern = safePattern(o.pattern, `${p}.pattern`);
		return Object.freeze(o);
	}
	if (type === "password") {
		const o = copy([
			"hasherKey",
			"minLength",
			"maxLength"
		]);
		o.hasherKey = str(o.hasherKey, `${p}.hasherKey`);
		o.minLength = num(o.minLength, `${p}.minLength`, true);
		o.maxLength = num(o.maxLength, `${p}.maxLength`, true);
		pair(o.minLength, o.maxLength, p);
		return Object.freeze(o);
	}
	if (type === "boolean") {
		const o = copy([
			"style",
			"onLabel",
			"offLabel"
		]);
		if (o.style !== void 0) o.style = enumValue(o.style, ["switch", "checkbox"], `${p}.style`);
		for (const k of ["onLabel", "offLabel"]) if (o[k] !== void 0) o[k] = str(o[k], `${p}.${k}`);
		return Object.freeze(o);
	}
	if ([
		"select",
		"checkbox",
		"radio",
		"buttonGroup"
	].includes(type)) {
		const o = copy([
			"choices",
			"multiple",
			"allowNull",
			"allowCustom",
			"min",
			"max",
			"searchable",
			"orientation"
		]);
		o.choices = boundedMap(o.choices, `${p}.choices`);
		for (const k of [
			"multiple",
			"allowNull",
			"allowCustom",
			"searchable"
		]) o[k] = bool(o[k], `${p}.${k}`);
		o.min = num(o.min, `${p}.min`, true);
		o.max = num(o.max, `${p}.max`, true);
		pair(o.min, o.max, p);
		if (o.min !== void 0 && o.min < 0) fail(`${p}.min`, "must be non-negative");
		if (o.orientation !== void 0) o.orientation = enumValue(o.orientation, ["vertical", "horizontal"], `${p}.orientation`);
		return Object.freeze(o);
	}
	if (type === "richText") {
		const o = copy([
			"toolbar",
			"media",
			"tabs",
			"delayedInit"
		]);
		if (o.toolbar !== void 0) o.toolbar = str(o.toolbar, `${p}.toolbar`);
		o.media = bool(o.media, `${p}.media`);
		if (o.tabs !== void 0) o.tabs = Object.freeze(array(o.tabs, `${p}.tabs`).map((v, i) => enumValue(v, ["visual", "source"], `${p}.tabs.${i}`)));
		o.delayedInit = bool(o.delayedInit, `${p}.delayedInit`);
		return Object.freeze(o);
	}
	if (type === "oembed") {
		const o = copy([
			"providers",
			"width",
			"height"
		]);
		o.providers = strings(o.providers, `${p}.providers`);
		o.width = num(o.width, `${p}.width`, true, true);
		o.height = num(o.height, `${p}.height`, true, true);
		return Object.freeze(o);
	}
	if (type === "link") {
		const o = copy(["schemes", "targets"]);
		o.schemes = strings(o.schemes, `${p}.schemes`, true);
		if (o.targets !== void 0) o.targets = Object.freeze(array(o.targets, `${p}.targets`).map((v, i) => enumValue(v, ["_self", "_blank"], `${p}.targets.${i}`)));
		return Object.freeze(o);
	}
	if ([
		"message",
		"accordion",
		"tab"
	].includes(type)) {
		const o = copy([
			"text",
			"open",
			"endpoint"
		]);
		if (o.text !== void 0) o.text = str(o.text, `${p}.text`);
		o.open = bool(o.open, `${p}.open`);
		o.endpoint = bool(o.endpoint, `${p}.endpoint`);
		return Object.freeze(o);
	}
	if ([
		"image",
		"file",
		"gallery"
	].includes(type)) {
		const o = copy([
			"mimeTypes",
			"minBytes",
			"maxBytes",
			"source",
			"return",
			...type !== "file" ? [
				"minWidth",
				"maxWidth",
				"minHeight",
				"maxHeight"
			] : [],
			...type === "gallery" ? [
				"min",
				"max",
				"previewSize"
			] : []
		]);
		o.mimeTypes = strings(o.mimeTypes, `${p}.mimeTypes`);
		for (const k of [
			"minBytes",
			"maxBytes",
			"minWidth",
			"maxWidth",
			"minHeight",
			"maxHeight",
			"min",
			"max"
		]) o[k] = num(o[k], `${p}.${k}`, true);
		pair(o.minBytes, o.maxBytes, p);
		pair(o.minWidth, o.maxWidth, p);
		pair(o.minHeight, o.maxHeight, p);
		pair(o.min, o.max, p);
		if (o.min !== void 0 && o.min < 0) fail(`${p}.min`, "must be non-negative");
		if (o.source !== void 0) o.source = enumValue(o.source, ["all", "uploadedToEntity"], `${p}.source`);
		o.return = enumValue(o.return, [
			"ref",
			"id",
			"url"
		], `${p}.return`);
		if (o.previewSize !== void 0) o.previewSize = str(o.previewSize, `${p}.previewSize`);
		return Object.freeze(o);
	}
	if ([
		"entity",
		"entityLink",
		"relationship"
	].includes(type)) {
		const o = copy([
			"entityTypes",
			"statuses",
			"taxonomies",
			"return",
			...type === "relationship" ? [
				"min",
				"max",
				"bidirectional"
			] : []
		]);
		o.entityTypes = strings(o.entityTypes, `${p}.entityTypes`);
		o.statuses = strings(o.statuses, `${p}.statuses`);
		if (o.taxonomies !== void 0) {
			const tax = obj(o.taxonomies, `${p}.taxonomies`), out = {};
			for (const k of Object.keys(tax)) out[k] = strings(tax[k], `${p}.taxonomies.${k}`, true);
			o.taxonomies = Object.freeze(out);
		}
		o.return = enumValue(o.return, [
			"ref",
			"id",
			"object"
		], `${p}.return`);
		o.min = num(o.min, `${p}.min`, true);
		o.max = num(o.max, `${p}.max`, true);
		pair(o.min, o.max, p);
		if (o.min !== void 0 && o.min < 0) fail(`${p}.min`, "must be non-negative");
		if (o.bidirectional !== void 0) {
			const q = obj(o.bidirectional, `${p}.bidirectional`);
			keys(q, ["targetFieldKey"], `${p}.bidirectional`);
			o.bidirectional = Object.freeze({ targetFieldKey: str(q.targetFieldKey, `${p}.bidirectional.targetFieldKey`) });
		}
		return Object.freeze(o);
	}
	if (type === "taxonomy") {
		const o = copy([
			"taxonomies",
			"multiple",
			"allowCreate",
			"saveTerms",
			"loadTerms",
			"return"
		]);
		o.taxonomies = strings(o.taxonomies, `${p}.taxonomies`, true);
		for (const k of [
			"multiple",
			"allowCreate",
			"saveTerms",
			"loadTerms"
		]) o[k] = bool(o[k], `${p}.${k}`);
		o.return = enumValue(o.return, [
			"ref",
			"id",
			"object"
		], `${p}.return`);
		return Object.freeze(o);
	}
	if (type === "user") {
		const o = copy([
			"roles",
			"multiple",
			"return"
		]);
		o.roles = strings(o.roles, `${p}.roles`);
		o.multiple = bool(o.multiple, `${p}.multiple`);
		o.return = enumValue(o.return, [
			"ref",
			"id",
			"object"
		], `${p}.return`);
		return Object.freeze(o);
	}
	if ([
		"date",
		"dateTime",
		"time"
	].includes(type)) {
		const o = copy([
			"inputFormat",
			"displayFormat",
			"returnFormat",
			...type !== "time" ? ["firstWeekday"] : [],
			...type === "dateTime" ? ["timezone"] : []
		]);
		for (const k of [
			"inputFormat",
			"displayFormat",
			"returnFormat"
		]) o[k] = str(o[k], `${p}.${k}`);
		o.firstWeekday = num(o.firstWeekday, `${p}.firstWeekday`, true);
		if (o.firstWeekday !== void 0 && (o.firstWeekday < 0 || o.firstWeekday > 6)) fail(`${p}.firstWeekday`, "must be between 0 and 6");
		if (o.timezone !== void 0) o.timezone = enumValue(o.timezone, [
			"site",
			"user",
			"utc"
		], `${p}.timezone`);
		return Object.freeze(o);
	}
	if (type === "color") {
		const o = copy(["alpha", "palette"]);
		o.alpha = bool(o.alpha, `${p}.alpha`);
		o.palette = strings(o.palette, `${p}.palette`);
		return Object.freeze(o);
	}
	if (type === "icon") {
		const o = copy(["sets", "allowNull"]);
		o.sets = strings(o.sets, `${p}.sets`);
		o.allowNull = bool(o.allowNull, `${p}.allowNull`);
		return Object.freeze(o);
	}
	if (type === "map") {
		const o = copy([
			"providerKey",
			"center",
			"zoom",
			"height"
		]);
		o.providerKey = str(o.providerKey, `${p}.providerKey`);
		if (o.center !== void 0) {
			const c = obj(o.center, `${p}.center`);
			keys(c, ["latitude", "longitude"], `${p}.center`);
			o.center = Object.freeze({
				latitude: num(c.latitude, `${p}.center.latitude`),
				longitude: num(c.longitude, `${p}.center.longitude`)
			});
		}
		o.zoom = num(o.zoom, `${p}.zoom`);
		o.height = num(o.height, `${p}.height`, true, true);
		return Object.freeze(o);
	}
	if (type === "group" || type === "repeater") {
		const o = copy(type === "group" ? ["fields", "layout"] : [
			"fields",
			"layout",
			"min",
			"max",
			"collapsedBy",
			"pagination"
		]);
		o.fields = parseFields(o.fields, `${p}.fields`, depth + 1, b);
		if (o.layout !== void 0) o.layout = enumValue(o.layout, [
			"block",
			"table",
			"row"
		], `${p}.layout`);
		o.min = num(o.min, `${p}.min`, true);
		o.max = num(o.max, `${p}.max`, true);
		pair(o.min, o.max, p);
		if (o.collapsedBy !== void 0) o.collapsedBy = str(o.collapsedBy, `${p}.collapsedBy`);
		if (o.pagination !== void 0) {
			const q = obj(o.pagination, `${p}.pagination`);
			keys(q, ["pageSize"], `${p}.pagination`);
			o.pagination = Object.freeze({ pageSize: num(q.pageSize, `${p}.pagination.pageSize`, true, true) });
		}
		return Object.freeze(o);
	}
	if (type === "flexible") {
		const o = copy([
			"layouts",
			"min",
			"max"
		]);
		o.layouts = Object.freeze(array(o.layouts, `${p}.layouts`).map((raw, i) => {
			const q = obj(raw, `${p}.layouts.${i}`);
			keys(q, [
				"key",
				"name",
				"label",
				"fields"
			], `${p}.layouts.${i}`);
			return Object.freeze({
				key: str(q.key, `${p}.layouts.${i}.key`),
				name: str(q.name, `${p}.layouts.${i}.name`),
				label: str(q.label, `${p}.layouts.${i}.label`),
				fields: parseFields(q.fields, `${p}.layouts.${i}.fields`, depth + 1, b)
			});
		}));
		const ls = o.layouts;
		if (new Set(ls.map((l) => l.key)).size !== ls.length) fail(`${p}.layouts`, "contains duplicate layout keys");
		o.min = num(o.min, `${p}.min`, true);
		o.max = num(o.max, `${p}.max`, true);
		pair(o.min, o.max, p);
		return Object.freeze(o);
	}
	const o = copy([
		"sourceGroupKeys",
		"sourceFieldKeys",
		"display",
		"prefixNames"
	]);
	o.sourceGroupKeys = strings(o.sourceGroupKeys, `${p}.sourceGroupKeys`);
	o.sourceFieldKeys = strings(o.sourceFieldKeys, `${p}.sourceFieldKeys`);
	if (o.sourceGroupKeys === void 0 && o.sourceFieldKeys === void 0) fail(p, "must name a clone source");
	if (o.display !== void 0) o.display = enumValue(o.display, ["seamless", "group"], `${p}.display`);
	o.prefixNames = bool(o.prefixNames, `${p}.prefixNames`);
	return Object.freeze(o);
}
function parseField(raw, p, depth, b) {
	if (depth > MAX_DEPTH) fail(p, "exceeds maximum recursive definition depth");
	if (++b.nodes > MAX_NODES) fail(p, "exceeds maximum field count");
	const x = obj(raw, p);
	keys(x, [
		"key",
		"name",
		"label",
		"type",
		"instructions",
		"required",
		"defaultValue",
		"settings",
		"wrapper",
		"conditionalLogic",
		"disabled",
		"readOnly",
		"presentation"
	], p);
	const type = enumValue(x.type, [...builtin], `${p}.type`);
	const key = str(x.key, `${p}.key`);
	if (!/^[a-z][a-z0-9_]*$/.test(key)) fail(`${p}.key`, "must be a normalized field key");
	if ([
		"password",
		"message",
		"accordion",
		"tab"
	].includes(type) && x.defaultValue !== void 0) fail(`${p}.defaultValue`, "is not permitted for this field type");
	const out = {
		key,
		name: str(x.name, `${p}.name`),
		label: str(x.label, `${p}.label`),
		type,
		settings: settings(x.settings, type, `${p}.settings`, depth, b)
	};
	for (const k of ["instructions"]) if (x[k] !== void 0) out[k] = str(x[k], `${p}.${k}`);
	for (const k of [
		"required",
		"disabled",
		"readOnly"
	]) if (x[k] !== void 0) out[k] = bool(x[k], `${p}.${k}`);
	if (x.defaultValue !== void 0) out.defaultValue = cloneData(x.defaultValue);
	if (x.wrapper !== void 0) {
		const q = obj(x.wrapper, `${p}.wrapper`);
		keys(q, [
			"width",
			"className",
			"data"
		], `${p}.wrapper`);
		const w = {};
		w.width = num(q.width, `${p}.wrapper.width`);
		if (w.width !== void 0 && (w.width < 0 || w.width > 100)) fail(`${p}.wrapper.width`, "must be between 0 and 100");
		if (q.className !== void 0) w.className = str(q.className, `${p}.wrapper.className`);
		if (q.data !== void 0) w.data = boundedMap(q.data, `${p}.wrapper.data`);
		out.wrapper = Object.freeze(w);
	}
	const c = conditional(x.conditionalLogic, `${p}.conditionalLogic`);
	if (c !== void 0) out.conditionalLogic = c;
	if (x.presentation !== void 0) {
		const q = obj(x.presentation, `${p}.presentation`);
		keys(q, ["labelPlacement", "instructionPlacement"], `${p}.presentation`);
		out.presentation = Object.freeze({
			...q.labelPlacement === void 0 ? {} : { labelPlacement: enumValue(q.labelPlacement, ["top", "left"], `${p}.presentation.labelPlacement`) },
			...q.instructionPlacement === void 0 ? {} : { instructionPlacement: enumValue(q.instructionPlacement, ["label", "field"], `${p}.presentation.instructionPlacement`) }
		});
	}
	return Object.freeze(out);
}
function parseFields(v, p, depth, b) {
	const fs = Object.freeze(array(v, p).map((f, i) => parseField(f, `${p}.${i}`, depth, b)));
	if (new Set(fs.map((f) => f.key)).size !== fs.length) fail(p, "contains duplicate sibling field keys");
	return fs;
}
/** Parses one built-in field definition through the same bounded trust boundary as immutable group snapshots. */
function parseFieldDefinition(value) {
	preflight(value, {
		nodes: 0,
		bytes: 0
	});
	return parseField(value, "definition", 1, {
		nodes: 0,
		bytes: 0
	});
}
function parseFieldGroup(value) {
	const x = obj(value, "definition");
	keys(x, [
		"key",
		"title",
		"fields",
		"location",
		"order",
		"position",
		"style",
		"labelPlacement",
		"instructionPlacement",
		"hideOnScreen",
		"active",
		"description"
	], "definition");
	const key = str(x.key, "definition.key");
	if (!/^[a-z][a-z0-9_]*$/.test(key)) fail("definition.key", "must be a normalized group key");
	const out = {
		key,
		title: str(x.title, "definition.title"),
		fields: parseFields(x.fields, "definition.fields", 1, {
			nodes: 0,
			bytes: 0
		}),
		location: location(x.location, "definition.location"),
		active: bool(x.active, "definition.active")
	};
	if (out.active === void 0) fail("definition.active", "is required");
	out.order = num(x.order, "definition.order", true);
	if (x.position !== void 0) out.position = enumValue(x.position, [
		"normal",
		"side",
		"afterTitle"
	], "definition.position");
	if (x.style !== void 0) out.style = enumValue(x.style, ["default", "seamless"], "definition.style");
	if (x.labelPlacement !== void 0) out.labelPlacement = enumValue(x.labelPlacement, ["top", "left"], "definition.labelPlacement");
	if (x.instructionPlacement !== void 0) out.instructionPlacement = enumValue(x.instructionPlacement, ["label", "field"], "definition.instructionPlacement");
	out.hideOnScreen = strings(x.hideOnScreen, "definition.hideOnScreen");
	if (x.description !== void 0) out.description = str(x.description, "definition.description");
	for (const k of Object.keys(out)) if (out[k] === void 0) delete out[k];
	return Object.freeze(out);
}
/** Parses one complete field-group definition at an untrusted runtime boundary. */
function parseFieldGroupDefinition(value) {
	preflight(value, {
		nodes: 0,
		bytes: 0
	});
	return parseFieldGroup(value);
}
/** Parses a persisted/imported immutable snapshot before hashing or storage. */
async function parseFieldDefinitionVersion(value) {
	preflight(value, {
		nodes: 0,
		bytes: 0
	});
	const x = obj(value, "fieldDefinitionVersion");
	keys(x, [
		"groupKey",
		"revision",
		"canonicalHash",
		"definition",
		"createdAt",
		"origin"
	], "fieldDefinitionVersion");
	const groupKey = str(x.groupKey, "fieldDefinitionVersion.groupKey");
	if (!/^[a-z][a-z0-9_]*$/.test(groupKey)) fail("fieldDefinitionVersion.groupKey", "must be a normalized group key");
	if (typeof x.revision !== "number" || !Number.isSafeInteger(x.revision) || x.revision < 1) fail("fieldDefinitionVersion.revision", "must be a positive safe integer");
	const canonicalHash = str(x.canonicalHash, "fieldDefinitionVersion.canonicalHash");
	if (!/^[a-f0-9]{64}$/i.test(canonicalHash)) fail("fieldDefinitionVersion.canonicalHash", "must be a SHA-256 hex digest");
	const origin = enumValue(x.origin, [
		"code",
		"db",
		"import"
	], "fieldDefinitionVersion.origin");
	const created = x.createdAt instanceof Date ? new Date(Date.prototype.getTime.call(x.createdAt)) : typeof x.createdAt === "string" ? new Date(x.createdAt) : /* @__PURE__ */ new Date("");
	if (Number.isNaN(created.getTime())) fail("fieldDefinitionVersion.createdAt", "must be a valid Date or ISO timestamp");
	const definition = parseFieldGroup(x.definition);
	if (definition.key !== groupKey) fail("fieldDefinitionVersion.definition.key", "must match groupKey");
	const normalizedHash = await canonicalFieldGroupHash(definition);
	if (canonicalHash.toLowerCase() !== normalizedHash) fail("fieldDefinitionVersion.canonicalHash", "does not match the canonical definition bytes");
	return Object.freeze({
		groupKey,
		revision: x.revision,
		canonicalHash: normalizedHash,
		definition,
		createdAt: Object.freeze(created),
		origin
	});
}
function canonicalJson(value) {
	if (value === void 0) return "null";
	if (value === null) return "null";
	if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return JSON.stringify(value);
	if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
	const record = value;
	return `{${Object.keys(record).filter((key) => record[key] !== void 0).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`;
}
/** Computes the canonical immutable FieldGroup digest with the web-standard SHA-256 API. */
async function canonicalFieldGroupHash(definition) {
	const bytes = new TextEncoder().encode(canonicalJson(definition));
	const digest = await crypto.subtle.digest("SHA-256", bytes);
	return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
}
/** Current immutable group registry state; historical bytes live in fieldDefinitionVersions. */
const fieldGroupDefinitions = pgTable("field_group_definitions", {
	key: text("key").primaryKey(),
	origin: text("origin").$type().notNull(),
	version: integer("version").notNull().default(1),
	active: boolean("active").notNull().default(true),
	currentRevision: integer("current_revision").notNull().default(1),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	shadowedDbVersion: integer("shadowed_db_version"),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [
	check("field_group_definitions_version_positive", sql`${t.version} > 0`),
	check("field_group_definitions_revision_positive", sql`${t.currentRevision} > 0`),
	index("field_group_definitions_active_idx").on(t.active, t.key)
]);
const fieldDefinitionVersions = pgTable("field_definition_versions", {
	groupKey: text("group_key").notNull(),
	revision: integer("revision").notNull(),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	origin: text("origin").$type().notNull(),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [
	primaryKey({ columns: [t.groupKey, t.revision] }),
	check("field_definition_versions_revision_positive", sql`${t.revision} > 0`),
	index("field_definition_versions_hash_idx").on(t.canonicalHash)
]);
const fieldOptionsDefinitions = pgTable("field_options_definitions", {
	key: text("key").primaryKey(),
	origin: text("origin").$type().notNull(),
	version: integer("version").notNull().default(1),
	active: boolean("active").notNull().default(true),
	currentRevision: integer("current_revision").notNull().default(1),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	shadowedDbVersion: integer("shadowed_db_version"),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [index("field_options_definitions_active_idx").on(t.active, t.key)]);
const fieldOptionsDefinitionVersions = pgTable("field_options_definition_versions", {
	optionsKey: text("options_key").notNull(),
	revision: integer("revision").notNull(),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	origin: text("origin").$type().notNull(),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [primaryKey({ columns: [t.optionsKey, t.revision] }), check("field_options_definition_versions_revision_positive", sql`${t.revision} > 0`)]);
const fieldBlockDefinitions = pgTable("field_block_definitions", {
	key: text("key").primaryKey(),
	origin: text("origin").$type().notNull(),
	version: integer("version").notNull().default(1),
	active: boolean("active").notNull().default(true),
	currentRevision: integer("current_revision").notNull().default(1),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	shadowedDbVersion: integer("shadowed_db_version"),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [index("field_block_definitions_active_idx").on(t.active, t.key)]);
const fieldBlockDefinitionVersions = pgTable("field_block_definition_versions", {
	blockKey: text("block_key").notNull(),
	revision: integer("revision").notNull(),
	canonicalHash: text("canonical_hash").notNull(),
	definition: jsonb("definition").$type().notNull(),
	origin: text("origin").$type().notNull(),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [primaryKey({ columns: [t.blockKey, t.revision] }), check("field_block_definition_versions_revision_positive", sql`${t.revision} > 0`)]);
const fieldBlockDocuments = pgTable("field_block_documents", {
	entityType: text("entity_type").notNull(),
	entityId: text("entity_id").notNull(),
	version: integer("version").notNull().default(1),
	document: jsonb("document").$type().notNull(),
	definitionVersions: jsonb("definition_versions").$type().notNull(),
	parentRevisionId: text("parent_revision_id").notNull(),
	hostSessionId: text("host_session_id"),
	updatedBy: text("updated_by").notNull(),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [primaryKey({ columns: [t.entityType, t.entityId] }), check("field_block_documents_version_positive", sql`${t.version} > 0`)]);
const fieldReusableBlocks = pgTable("field_reusable_blocks", {
	id: text("id").primaryKey(),
	version: integer("version").notNull().default(1),
	document: jsonb("document").$type().notNull(),
	definitionVersions: jsonb("definition_versions").$type().notNull(),
	deletedAt: timestamp("deleted_at", {
		withTimezone: true,
		precision: 3
	}),
	hostSessionId: text("host_session_id"),
	updatedBy: text("updated_by").notNull(),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [check("field_reusable_blocks_version_positive", sql`${t.version} > 0`)]);
const fieldRevisions = pgTable("field_revisions", {
	id: uuid("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
	entityType: text("entity_type").notNull(),
	entityId: text("entity_id").notNull(),
	parentRevisionId: text("parent_revision_id").notNull(),
	parentAutosaveId: text("parent_autosave_id"),
	kind: text("kind").$type().notNull(),
	retentionClass: text("retention_class"),
	definitionVersions: jsonb("definition_versions").$type().notNull(),
	values: jsonb("values").$type().notNull(),
	blockDocument: jsonb("block_document").$type(),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	createdBy: text("created_by").notNull(),
	sourceVersion: text("source_version").notNull(),
	hostSessionId: text("host_session_id")
}, (t) => [index("field_revisions_entity_created_idx").on(t.entityType, t.entityId, t.createdAt, t.id), index("field_revisions_parent_idx").on(t.parentRevisionId, t.kind)]);
const fieldsOperationReceipts = pgTable("fields_operation_receipts", {
	operationId: text("operation_id").primaryKey(),
	kind: text("kind").notNull(),
	principalScope: text("principal_scope").notNull(),
	requestHash: text("request_hash").notNull(),
	result: jsonb("result").$type().notNull(),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [index("fields_operation_receipts_kind_idx").on(t.kind, t.createdAt)]);
/** Final recursive typed EAV source of truth. */
const fieldValueNodes = pgTable("field_value_nodes", {
	nodeId: uuid("node_id").primaryKey().$defaultFn(() => crypto.randomUUID()),
	entityType: text("entity_type").notNull(),
	entityId: text("entity_id").notNull(),
	groupKey: text("group_key").notNull(),
	definitionRevision: integer("definition_revision").notNull(),
	fieldKey: text("field_key").notNull(),
	path: text("path").notNull(),
	parentNodeId: uuid("parent_node_id").references(() => fieldValueNodes.nodeId, { onDelete: "cascade" }),
	rowId: text("row_id"),
	layoutKey: text("layout_key"),
	nodeKind: text("node_kind").$type().notNull(),
	ordinal: integer("ordinal").notNull().default(0),
	isNull: boolean("is_null").notNull().default(false),
	valueText: text("value_text"),
	valueNumber: numeric("value_number"),
	valueBoolean: boolean("value_boolean"),
	valueDateTime: text("value_date_time"),
	valueRef: text("value_ref"),
	valueJson: text("value_json"),
	legacySourceIds: jsonb("legacy_source_ids").$type(),
	legacySourceHash: text("legacy_source_hash"),
	hostSessionId: text("host_session_id"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [
	uniqueIndex("field_value_nodes_entity_group_path_uq").on(t.entityType, t.entityId, t.groupKey, t.path),
	uniqueIndex("field_value_nodes_sibling_ordinal_uq").on(t.entityType, t.entityId, t.groupKey, sql`COALESCE(${t.parentNodeId}, '00000000-0000-0000-0000-000000000000'::uuid)`, t.ordinal),
	index("field_value_nodes_entity_idx").on(t.entityType, t.entityId, t.groupKey),
	index("field_value_nodes_definition_idx").on(t.groupKey, t.definitionRevision),
	index("field_value_nodes_text_idx").on(t.entityType, t.fieldKey, t.valueText),
	index("field_value_nodes_number_idx").on(t.entityType, t.fieldKey, t.valueNumber),
	index("field_value_nodes_ref_idx").on(t.entityType, t.fieldKey, t.valueRef),
	check("field_value_nodes_ordinal_nonnegative", sql`${t.ordinal} >= 0`),
	check("field_value_nodes_revision_positive", sql`${t.definitionRevision} > 0`),
	check("field_value_nodes_row_shape", sql`
      (${t.nodeKind} IN ('repeaterRow','flexibleRow') AND ${t.rowId} IS NOT NULL)
      OR (${t.nodeKind} NOT IN ('repeaterRow','flexibleRow') AND ${t.rowId} IS NULL)
    `),
	check("field_value_nodes_layout_shape", sql`
      (${t.nodeKind} = 'flexibleRow' AND ${t.layoutKey} IS NOT NULL)
      OR (${t.nodeKind} <> 'flexibleRow' AND ${t.layoutKey} IS NULL)
    `),
	check("field_value_nodes_lane_shape", sql`
      CASE
        WHEN ${t.nodeKind} <> 'leaf' THEN
          ${t.isNull} = false AND ${t.valueText} IS NULL AND ${t.valueNumber} IS NULL AND ${t.valueBoolean} IS NULL
          AND ${t.valueDateTime} IS NULL AND ${t.valueRef} IS NULL AND ${t.valueJson} IS NULL
        WHEN ${t.isNull} = true THEN
          ${t.valueText} IS NULL AND ${t.valueNumber} IS NULL AND ${t.valueBoolean} IS NULL
          AND ${t.valueDateTime} IS NULL AND ${t.valueRef} IS NULL AND ${t.valueJson} IS NULL
        ELSE
          (CASE WHEN ${t.valueText} IS NOT NULL THEN 1 ELSE 0 END
           + CASE WHEN ${t.valueNumber} IS NOT NULL THEN 1 ELSE 0 END
           + CASE WHEN ${t.valueBoolean} IS NOT NULL THEN 1 ELSE 0 END
           + CASE WHEN ${t.valueDateTime} IS NOT NULL THEN 1 ELSE 0 END
           + CASE WHEN ${t.valueRef} IS NOT NULL THEN 1 ELSE 0 END
           + CASE WHEN ${t.valueJson} IS NOT NULL THEN 1 ELSE 0 END) = 1
      END
    `)
]);
const fieldsImportStaging = pgTable("fields_import_staging", {
	hostSessionId: text("host_session_id").notNull(),
	section: text("section").notNull(),
	ordinal: integer("ordinal").notNull(),
	canonicalHash: text("canonical_hash").notNull(),
	payload: jsonb("payload").$type().notNull(),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [primaryKey({ columns: [
	t.hostSessionId,
	t.section,
	t.ordinal
] }), check("fields_import_staging_ordinal_nonnegative", sql`${t.ordinal} >= 0`)]);
const fieldsImportJournal = pgTable("fields_import_journal", {
	importId: uuid("import_id").primaryKey().$defaultFn(() => crypto.randomUUID()),
	hostSessionId: text("host_session_id").notNull(),
	manifestHash: text("manifest_hash").notNull(),
	planHash: text("plan_hash").notNull(),
	state: text("state").notNull(),
	version: integer("version").notNull().default(0),
	nextSection: text("next_section"),
	nextOffset: integer("next_offset"),
	highWaterMark: text("high_water_mark"),
	payload: jsonb("payload").$type(),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [uniqueIndex("fields_import_journal_session_uq").on(t.hostSessionId), check("fields_import_journal_version_nonnegative", sql`${t.version} >= 0`)]);
const fieldsMigrationCheckpoints = pgTable("fields_migration_checkpoints", {
	migrationId: text("migration_id").notNull(),
	adapter: text("adapter").$type().notNull(),
	phase: text("phase").$type().notNull(),
	batchOrdinal: integer("batch_ordinal").notNull().default(0),
	version: integer("version").notNull().default(0),
	lastKey: text("last_key"),
	sourceCount: integer("source_count").notNull(),
	targetCount: integer("target_count"),
	sourceHash: text("source_hash").notNull(),
	targetHash: text("target_hash"),
	highWaterMark: text("high_water_mark"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [primaryKey({ columns: [
	t.migrationId,
	t.adapter,
	t.phase,
	t.batchOrdinal
] })]);
const fieldsMigrationWriteJournal = pgTable("fields_migration_write_journal", {
	seq: bigserial("seq", { mode: "number" }).primaryKey(),
	migrationId: text("migration_id").notNull(),
	operation: text("operation").$type().notNull(),
	valueId: text("value_id").notNull(),
	beforeValue: jsonb("before_value").$type(),
	afterValue: jsonb("after_value").$type(),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [index("fields_migration_write_journal_migration_seq_idx").on(t.migrationId, t.seq)]);
const fieldValues = pgTable("field_values", {
	id: uuid("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
	entityType: text("entity_type").notNull(),
	entityId: text("entity_id").notNull(),
	groupId: text("group_id").notNull(),
	fieldKey: text("field_key").notNull(),
	ordinal: integer("ordinal").notNull().default(0),
	valueText: text("value_text"),
	valueNum: numeric("value_num"),
	valueBool: boolean("value_bool"),
	valueDate: timestamp("value_date", {
		withTimezone: true,
		precision: 3
	}),
	refType: text("ref_type"),
	refId: text("ref_id"),
	refMeta: jsonb("ref_meta"),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [
	uniqueIndex("field_values_entity_group_field_ord_uq").on(t.entityType, t.entityId, t.groupId, t.fieldKey, t.ordinal),
	index("field_values_entity_idx").on(t.entityType, t.entityId),
	index("field_values_facet_text_idx").on(t.entityType, t.fieldKey, t.valueText),
	index("field_values_facet_num_idx").on(t.entityType, t.fieldKey, t.valueNum),
	index("field_values_facet_date_idx").on(t.entityType, t.fieldKey, t.valueDate),
	index("field_values_ref_idx").on(t.entityType, t.refType, t.refId),
	check("field_values_one_lane", sql`
      (CASE WHEN ${t.valueText} IS NOT NULL THEN 1 ELSE 0 END
     + CASE WHEN ${t.valueNum} IS NOT NULL THEN 1 ELSE 0 END
     + CASE WHEN ${t.valueBool} IS NOT NULL THEN 1 ELSE 0 END
     + CASE WHEN ${t.valueDate} IS NOT NULL THEN 1 ELSE 0 END
     + CASE WHEN ${t.refId} IS NOT NULL THEN 1 ELSE 0 END) = 1`)
]);
const fieldGroups = pgTable("field_groups", {
	id: uuid("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
	entityType: text("entity_type").notNull(),
	subType: text("sub_type"),
	key: text("key").notNull(),
	label: text("label").notNull(),
	fields: jsonb("fields").notNull(),
	position: integer("position").notNull().default(0),
	createdAt: timestamp("created_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
	updatedAt: timestamp("updated_at", {
		withTimezone: true,
		precision: 3
	}).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
}, (t) => [uniqueIndex("field_groups_entity_key_uq").on(t.entityType, t.key)]);
const fieldsSchema = {
	fieldValues,
	fieldGroups,
	fieldGroupDefinitions,
	fieldDefinitionVersions,
	fieldOptionsDefinitions,
	fieldOptionsDefinitionVersions,
	fieldBlockDefinitions,
	fieldBlockDefinitionVersions,
	fieldBlockDocuments,
	fieldReusableBlocks,
	fieldRevisions,
	fieldsOperationReceipts,
	fieldValueNodes,
	fieldsImportStaging,
	fieldsImportJournal,
	fieldsMigrationCheckpoints,
	fieldsMigrationWriteJournal
};
//#endregion
export { FieldDefinitionVersionError, canonicalFieldGroupHash, fieldBlockDefinitionVersions, fieldBlockDefinitions, fieldBlockDocuments, fieldDefinitionVersions, fieldGroupDefinitions, fieldGroups, fieldOptionsDefinitionVersions, fieldOptionsDefinitions, fieldReusableBlocks, fieldRevisions, fieldValueNodes, fieldValues, fieldsImportJournal, fieldsImportStaging, fieldsMigrationCheckpoints, fieldsMigrationWriteJournal, fieldsOperationReceipts, fieldsSchema, isFieldDefinitionVersionError, parseFieldDefinition, parseFieldDefinitionVersion, parseFieldGroupDefinition };
