import { t as escapeRegExp } from "./regex-Jt5DatPi.js";
import path from "path";
import fs from "fs";
import matter from "gray-matter";
//#region src/core/context-builder.ts
const MAX_INTERACTIONS = 10;
function estimateTokens(text) {
	return Math.ceil(text.length / 4);
}
function parseRecentInteractions(filePath, limit) {
	if (!fs.existsSync(filePath)) return "";
	return fs.readFileSync(filePath, "utf-8").split(/(?=^## \d{4}-\d{2}-\d{2})/m).filter((e) => e.trim()).slice(0, limit).join("\n").trim();
}
function parsePipelineContent(filePath) {
	if (!fs.existsSync(filePath)) return "";
	return fs.readFileSync(filePath, "utf-8").trim();
}
function extractSection(content, sectionName) {
	const match = new RegExp(`## ${escapeRegExp(sectionName)}([\\s\\S]*?)(?=^## |$)`, "m").exec(content);
	return match ? (match[1] ?? "").trim() : "";
}
async function buildContext(dataDir, slug) {
	const customerDir = path.join(dataDir, "customers", slug);
	if (!fs.existsSync(customerDir)) throw new Error(`Customer '${slug}' not found`);
	const mainFactsPath = path.join(customerDir, "main_facts.md");
	const interactionsPath = path.join(customerDir, "interactions.md");
	const pipelinePath = path.join(customerDir, "pipeline.md");
	let mainContent = "";
	let frontmatterStr = "";
	if (fs.existsSync(mainFactsPath)) {
		const raw = matter(fs.readFileSync(mainFactsPath, "utf-8"));
		mainContent = raw.content ?? "";
		frontmatterStr = Object.entries(raw.data).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join("\n");
	}
	const quickRef = extractSection(mainContent, "Quick Reference");
	const contacts = extractSection(mainContent, "Contacts");
	const criticalContext = extractSection(mainContent, "Critical Context");
	const openQuestions = extractSection(mainContent, "Open Questions");
	const recentActivity = parseRecentInteractions(interactionsPath, MAX_INTERACTIONS);
	const pipelineContent = parsePipelineContent(pipelinePath);
	const raw = [
		`# Customer Context: ${slug}`,
		"",
		"## Metadata",
		frontmatterStr || "(no metadata)",
		"",
		"## Quick Reference",
		quickRef || "(not set)",
		"",
		"## Contacts",
		contacts || "(not set)",
		"",
		"## Critical Context",
		criticalContext || "(not set)",
		"",
		"## Recent Activity (last 10 interactions)",
		recentActivity || "(no interactions yet)",
		"",
		"## Pipeline",
		pipelineContent || "(no deals)",
		"",
		"## Open Questions",
		openQuestions || "(none)"
	].join("\n");
	if (estimateTokens(raw) > 3e3) {
		const trimmedActivity = parseRecentInteractions(interactionsPath, 5);
		return [
			`# Customer Context: ${slug}`,
			"",
			"## Metadata",
			frontmatterStr || "(no metadata)",
			"",
			"## Quick Reference",
			quickRef || "(not set)",
			"",
			"## Contacts",
			contacts || "(not set)",
			"",
			"## Critical Context",
			criticalContext || "(not set)",
			"",
			"## Recent Activity (last 5 interactions — trimmed for token budget)",
			trimmedActivity || "(no interactions yet)",
			"",
			"## Pipeline",
			pipelineContent || "(no deals)",
			"",
			"## Open Questions",
			openQuestions || "(none)"
		].join("\n");
	}
	return raw;
}
//#endregion
export { buildContext as t };

//# sourceMappingURL=context-builder-7Uab5-G4.js.map