const require_chunk = require("./chunk-DakpK96I.cjs");
const require_session_store = require("./session-store-yfwnj0OC.cjs");
const require_atomic_write = require("./atomic-write-BYmF-ThH.cjs");
const require_write_queue = require("./write-queue-BDolUxfs.cjs");
let path = require("path");
path = require_chunk.__toESM(path, 1);
let fs = require("fs");
fs = require_chunk.__toESM(fs, 1);
//#region src/fs/interactions-writer.ts
var interactions_writer_exports = /* @__PURE__ */ require_chunk.__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.default.join(dataDir, "customers", slug, "interactions.md");
	if (!fs.default.existsSync(filePath)) return "";
	return fs.default.readFileSync(filePath, "utf-8");
}
async function appendInteraction(dataDir, slug, entry) {
	require_session_store.assertSafeSlug(slug);
	const filePath = path.default.join(dataDir, "customers", slug, "interactions.md");
	return require_write_queue.withFileQueue(filePath, async () => {
		const existing = fs.default.existsSync(filePath) ? fs.default.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;
		}
		require_atomic_write.writeFileAtomic(filePath, newContent);
	});
}
//#endregion
Object.defineProperty(exports, "appendInteraction", {
	enumerable: true,
	get: function() {
		return appendInteraction;
	}
});
Object.defineProperty(exports, "formatInteractionEntry", {
	enumerable: true,
	get: function() {
		return formatInteractionEntry;
	}
});
Object.defineProperty(exports, "interactions_writer_exports", {
	enumerable: true,
	get: function() {
		return interactions_writer_exports;
	}
});
Object.defineProperty(exports, "readInteractions", {
	enumerable: true,
	get: function() {
		return readInteractions;
	}
});

//# sourceMappingURL=interactions-writer-BRJNrefF.cjs.map