import { a as readMainFacts, i as listCustomerSlugs } from "./customer-dir-CkMMXhb0.js";
import { r as writeJsonArray, t as readJsonArray } from "./json-store-WWsFzXub.js";
import path from "path";
//#region src/core/segments.ts
function segmentsPath(dataDir) {
	return path.join(dataDir, ".agentic", "segments.json");
}
function loadSegments(dataDir) {
	return readJsonArray(segmentsPath(dataDir), "segments");
}
function defineSegment(dataDir, name, criteria) {
	const segs = loadSegments(dataDir);
	const idx = segs.findIndex((s) => s.name === name);
	const def = {
		name,
		criteria
	};
	if (idx >= 0) segs[idx] = def;
	else segs.push(def);
	writeJsonArray(segmentsPath(dataDir), "segments", segs);
	return segs;
}
function daysBetween(fromIso, toIso) {
	const a = new Date(fromIso).getTime();
	const b = new Date(toIso).getTime();
	if (Number.isNaN(a) || Number.isNaN(b)) return 0;
	return Math.floor((b - a) / 864e5);
}
/** Return the customer slugs matching the criteria (now defaults to today). */
async function evaluateSegment(dataDir, criteria, now = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)) {
	const matches = [];
	for (const slug of listCustomerSlugs(dataDir)) {
		const facts = await readMainFacts(dataDir, slug).catch(() => null);
		if (!facts) continue;
		if (criteria.stage && facts.relationship_stage !== criteria.stage) continue;
		if (criteria.minDealValue !== void 0 && !(typeof facts.deal_value === "number" && facts.deal_value >= criteria.minDealValue)) continue;
		if (criteria.tags && criteria.tags.length > 0) {
			const tags = facts.tags ?? [];
			if (!criteria.tags.every((t) => tags.includes(t))) continue;
		}
		if (criteria.staleDays !== void 0) {
			const lt = facts.updated;
			if (!lt || daysBetween(lt, now) < criteria.staleDays) continue;
		}
		matches.push(slug);
	}
	return matches;
}
//#endregion
export { defineSegment, evaluateSegment, loadSegments };

//# sourceMappingURL=segments-DI3LOQNe.js.map