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 path from "path";
import fs from "fs";
import { z } from "zod";
//#region src/schemas/pipeline.ts
const PipelineDealSchema = z.object({
	name: z.string().min(1),
	stage: z.enum([
		"lead",
		"qualified",
		"proposal",
		"negotiation",
		"won",
		"lost"
	]),
	value: z.number().optional(),
	currency: z.string().default("EUR"),
	probability: z.number().min(0).max(100).optional(),
	close_date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD required").optional(),
	notes: z.string().optional(),
	updated: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD required")
});
//#endregion
//#region src/fs/pipeline-writer.ts
var pipeline_writer_exports = /* @__PURE__ */ __exportAll({
	readPipeline: () => readPipeline,
	readPipelineSync: () => readPipelineSync,
	upsertDeal: () => upsertDeal
});
const PIPELINE_HEADER = "# Pipeline\n\n";
const TABLE_HEADER = `| Name | Stage | Value | Currency | Probability | Close Date | Notes | Updated |
|------|-------|-------|----------|-------------|------------|-------|---------|`;
function escapeMd(val) {
	if (val === void 0 || val === null) return "";
	return String(val).replace(/\|/g, "\\|");
}
function serializeDeal(deal) {
	return `| ${escapeMd(deal.name)} | ${escapeMd(deal.stage)} | ${deal.value !== void 0 ? String(deal.value) : ""} | ${escapeMd(deal.currency)} | ${deal.probability !== void 0 ? String(deal.probability) : ""} | ${escapeMd(deal.close_date)} | ${escapeMd(deal.notes)} | ${escapeMd(deal.updated)} |`;
}
function parseDealsFromMarkdown(content) {
	const lines = content.split("\n");
	const deals = [];
	let inTable = false;
	let headerParsed = false;
	for (const line of lines) {
		const trimmed = line.trim();
		if (!inTable && trimmed.startsWith("| Name |")) {
			inTable = true;
			headerParsed = false;
			continue;
		}
		if (inTable && !headerParsed && trimmed.startsWith("|---")) {
			headerParsed = true;
			continue;
		}
		if (inTable && headerParsed && trimmed.startsWith("|")) {
			const [name, stage, valueStr, currency, probabilityStr, close_date, notes, updated] = trimmed.split("|").slice(1, -1).map((c) => c.trim().replace(/\\\|/g, "|"));
			if (!name || !stage || !updated) continue;
			const raw = {
				name,
				stage,
				currency: currency || "EUR",
				updated
			};
			if (valueStr) raw["value"] = parseFloat(valueStr);
			if (probabilityStr) raw["probability"] = parseFloat(probabilityStr);
			if (close_date) raw["close_date"] = close_date;
			if (notes) raw["notes"] = notes;
			const result = PipelineDealSchema.safeParse(raw);
			if (result.success) deals.push(result.data);
		} else if (inTable && !trimmed.startsWith("|")) inTable = false;
	}
	return deals;
}
function serializeDeals(deals) {
	if (deals.length === 0) return `${PIPELINE_HEADER}<!-- Deals listed here -->\n`;
	return `${PIPELINE_HEADER}${TABLE_HEADER}\n${deals.map(serializeDeal).join("\n")}\n`;
}
function readPipelineSync(dataDir, slug) {
	const filePath = path.join(dataDir, "customers", slug, "pipeline.md");
	if (!fs.existsSync(filePath)) return [];
	return parseDealsFromMarkdown(fs.readFileSync(filePath, "utf-8"));
}
async function readPipeline(dataDir, slug) {
	return readPipelineSync(dataDir, slug);
}
async function upsertDeal(dataDir, slug, deal) {
	assertSafeSlug(slug);
	const filePath = path.join(dataDir, "customers", slug, "pipeline.md");
	const existing = await readPipeline(dataDir, slug);
	const idx = existing.findIndex((d) => d.name === deal.name);
	let updated;
	if (idx >= 0) {
		updated = [...existing];
		updated[idx] = deal;
	} else updated = [...existing, deal];
	writeFileAtomic(filePath, serializeDeals(updated));
}
//#endregion
export { upsertDeal as i, readPipeline as n, readPipelineSync as r, pipeline_writer_exports as t };

//# sourceMappingURL=pipeline-writer-rDj-ni6q.js.map