import { t as assertSafePathSegment } from "./safe-path-mpp0dKtO.js";
import { r as writeJsonArray, t as readJsonArray } from "./json-store-WWsFzXub.js";
import { r as validateCustomFields } from "./custom-fields-GzpOHW_2.js";
import path from "path";
import { randomBytes } from "crypto";
//#region src/core/custom-objects.ts
function objectsSchemaPath(dataDir) {
	return path.join(dataDir, ".agentic", "schema", "custom-objects.json");
}
function recordsPath(dataDir, name) {
	assertSafePathSegment(name, "custom object name");
	return path.join(dataDir, ".agentic", "objects", `${name}.json`);
}
function loadCustomObjects(dataDir) {
	return readJsonArray(objectsSchemaPath(dataDir), "objects");
}
function getObjectDefinition(dataDir, name) {
	return loadCustomObjects(dataDir).find((o) => o.name === name);
}
/** Add or update (by name) a custom object definition. */
function defineCustomObject(dataDir, def) {
	assertSafePathSegment(def.name, "custom object name");
	const objs = loadCustomObjects(dataDir);
	const idx = objs.findIndex((o) => o.name === def.name);
	if (idx >= 0) objs[idx] = def;
	else objs.push(def);
	writeJsonArray(objectsSchemaPath(dataDir), "objects", objs);
	return objs;
}
function listRecords(dataDir, name) {
	return readJsonArray(recordsPath(dataDir, name), "records");
}
function writeRecords(dataDir, name, records) {
	writeJsonArray(recordsPath(dataDir, name), "records", records);
}
function createRecord(dataDir, name, values) {
	const def = getObjectDefinition(dataDir, name);
	if (!def) return {
		ok: false,
		errors: [`Unknown object: ${name}`]
	};
	const validation = validateCustomFields(values, def.fields);
	if (!validation.valid) return {
		ok: false,
		errors: validation.errors
	};
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const record = {
		id: `${name}_${randomBytes(6).toString("hex")}`,
		createdAt: now,
		updatedAt: now,
		values: validation.values
	};
	writeRecords(dataDir, name, [...listRecords(dataDir, name), record]);
	return {
		ok: true,
		record
	};
}
//#endregion
export { loadCustomObjects as a, listRecords as i, defineCustomObject as n, getObjectDefinition as r, createRecord as t };

//# sourceMappingURL=custom-objects-CxW1gHwJ.js.map