import { r as writeJsonArray, t as readJsonArray } from "./json-store-WWsFzXub.js";
import path from "path";
//#region src/core/custom-fields.ts
function schemaPath(dataDir) {
	return path.join(dataDir, ".agentic", "schema", "custom-fields.json");
}
function loadFieldDefinitions(dataDir) {
	return readJsonArray(schemaPath(dataDir), "fields");
}
/** Add or update (by name) a custom field definition. */
function defineCustomField(dataDir, def) {
	const defs = loadFieldDefinitions(dataDir);
	const idx = defs.findIndex((d) => d.name === def.name);
	if (idx >= 0) defs[idx] = def;
	else defs.push(def);
	writeJsonArray(schemaPath(dataDir), "fields", defs);
	return defs;
}
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
/** Validate + coerce a record of raw values against custom field definitions. */
function validateCustomFields(input, defs) {
	const byName = new Map(defs.map((d) => [d.name, d]));
	const values = {};
	const errors = [];
	for (const [key, raw] of Object.entries(input)) {
		const def = byName.get(key);
		if (!def) {
			errors.push(`Unknown custom field: ${key}`);
			continue;
		}
		const str = String(raw).trim();
		switch (def.type) {
			case "number": {
				const n = Number(str);
				if (!Number.isFinite(n)) errors.push(`${key}: not a number`);
				else values[key] = n;
				break;
			}
			case "boolean":
				if (/^(true|yes|1)$/i.test(str)) values[key] = true;
				else if (/^(false|no|0)$/i.test(str)) values[key] = false;
				else errors.push(`${key}: not a boolean`);
				break;
			case "date":
				if (DATE_RE.test(str)) values[key] = str;
				else errors.push(`${key}: expected YYYY-MM-DD`);
				break;
			case "select":
				if (def.options && def.options.includes(str)) values[key] = str;
				else errors.push(`${key}: must be one of ${(def.options ?? []).join(", ")}`);
				break;
			default: values[key] = str;
		}
	}
	return {
		valid: errors.length === 0,
		values,
		errors
	};
}
//#endregion
export { loadFieldDefinitions as n, validateCustomFields as r, defineCustomField as t };

//# sourceMappingURL=custom-fields-GzpOHW_2.js.map