import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.js";
import { T as assertSafeSlug } from "./session-store-DWxJ5Pof.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
var interactions_writer_exports = /* @__PURE__ */ __exportAll({
	appendInteraction: () => appendInteraction,
	formatInteractionEntry: () => formatInteractionEntry,
	readInteractions: () => readInteractions
});
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");
}
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, formatInteractionEntry as n, interactions_writer_exports as r, appendInteraction as t };

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