import { t as assertSafeSlug } from "./customer-dir-CkMMXhb0.js";
import { t as writeFileAtomic } from "./atomic-write-8yjqqLtS.js";
import { t as withFileQueue } from "./write-queue-IbsAjUnh.js";
import path from "path";
import fs from "fs";
//#region src/fs/interactions-writer.ts
const INTERACTION_SEPARATOR = "---";
function formatInteractionEntry(entry) {
	const header = `## ${entry.date} · ${entry.type}${entry.direction ? ` · ${entry.direction}` : ""}`;
	const withLabel = entry.type === "Email" ? "Subject" : "With";
	const nextStepsBlock = entry.nextSteps.length > 0 ? entry.nextSteps.map((s) => `- [ ] ${s}`).join("\n") : "- [ ] —";
	const attachmentsLine = entry.attachments && entry.attachments.length > 0 ? `\n**Attachments:** ${entry.attachments.map((a) => `[${a.split("/").pop()}](attachments/${a})`).join(", ")}` : "";
	return `${header}
**${withLabel}:** ${entry.with}
**Summary:** ${entry.summary}
**Next Steps:**
${nextStepsBlock}${attachmentsLine}
**Source:** ${entry.sourceRef}
**Synced:** ${entry.synced}
${INTERACTION_SEPARATOR}
`;
}
async function readInteractions(dataDir, slug) {
	const filePath = path.join(dataDir, "customers", slug, "interactions.md");
	if (!fs.existsSync(filePath)) return "";
	return fs.readFileSync(filePath, "utf-8");
}
/**
* Per-run source-ref deduplication index for bulk imports. Loads each slug's
* interactions file once (not once per row) and tracks freshly-appended refs
* in memory, so a 5k-row import stays linear instead of re-reading a growing
* file on every row (the previous O(rows²) behavior).
*/
var InteractionDedup = class {
	dataDir;
	cache = /* @__PURE__ */ new Map();
	constructor(dataDir) {
		this.dataDir = dataDir;
	}
	/** True if `sourceRef` already exists for `slug` (on disk or appended this run). */
	async seen(slug, sourceRef) {
		let content = this.cache.get(slug);
		if (content === void 0) {
			content = await readInteractions(this.dataDir, slug).catch(() => "");
			this.cache.set(slug, content);
		}
		return content.includes(sourceRef);
	}
	/** Record that `sourceRef` was just appended, so later rows dedupe against it. */
	markAppended(slug, sourceRef) {
		this.cache.set(slug, (this.cache.get(slug) ?? "") + sourceRef);
	}
};
async function appendInteraction(dataDir, slug, entry) {
	assertSafeSlug(slug);
	const filePath = path.join(dataDir, "customers", slug, "interactions.md");
	return withFileQueue(filePath, async () => {
		const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf-8") : "";
		const formatted = formatInteractionEntry(entry);
		let newContent;
		if (existing === "") newContent = formatted;
		else {
			const headerEnd = existing.indexOf("\n\n");
			if (headerEnd > -1) {
				const header = existing.slice(0, headerEnd + 2);
				const body = existing.slice(headerEnd + 2);
				newContent = header + formatted + "\n" + body;
			} else newContent = existing + "\n" + formatted;
		}
		writeFileAtomic(filePath, newContent);
	});
}
//#endregion
export { readInteractions as i, appendInteraction as n, formatInteractionEntry as r, InteractionDedup as t };

//# sourceMappingURL=interactions-writer-B8XAzdqR.js.map