import { promises } from 'fs'; import path from 'path'; import { randomUUID } from 'crypto'; const getDataPath = (filename) => { const cwd = process.cwd(); if (cwd.endsWith(".output")) { return path.join(cwd, "../public/data", filename); } return path.join(cwd, "public/data", filename); }; const TERMINE_FILE = getDataPath("termine.csv"); async function readTermine() { try { const data = await promises.readFile(TERMINE_FILE, "utf-8"); const lines = data.split("\n").filter((line) => line.trim() !== ""); if (lines.length < 2) return []; const termine = []; for (let i = 1; i < lines.length; i++) { const values = []; let current = ""; let inQuotes = false; for (let j = 0; j < lines[i].length; j++) { const char = lines[i][j]; if (char === '"') { inQuotes = !inQuotes; } else if (char === "," && !inQuotes) { values.push(current.trim()); current = ""; } else { current += char; } } values.push(current.trim()); if (values.length >= 4) { termine.push({ id: randomUUID(), // Generate ID on-the-fly for editing datum: values[0], titel: values[1], beschreibung: values[2], kategorie: values[3] }); } } return termine; } catch (error) { if (error.code === "ENOENT") { return []; } console.error("Fehler beim Lesen der Termine:", error); return []; } } async function writeTermine(termine) { try { let csv = '"datum","titel","beschreibung","kategorie"\n'; for (const termin of termine) { const datum = termin.datum || ""; const titel = termin.titel || ""; const beschreibung = termin.beschreibung || ""; const kategorie = termin.kategorie || ""; const escapedDatum = datum.replace(/"/g, '""'); const escapedTitel = titel.replace(/"/g, '""'); const escapedBeschreibung = beschreibung.replace(/"/g, '""'); const escapedKategorie = kategorie.replace(/"/g, '""'); csv += `"${escapedDatum}","${escapedTitel}","${escapedBeschreibung}","${escapedKategorie}" `; } await promises.writeFile(TERMINE_FILE, csv, "utf-8"); return true; } catch (error) { console.error("Fehler beim Schreiben der Termine:", error); return false; } } async function saveTermin(terminData) { const termine = await readTermine(); const newTermin = { datum: terminData.datum, titel: terminData.titel, beschreibung: terminData.beschreibung || "", kategorie: terminData.kategorie || "Sonstiges" }; termine.push(newTermin); termine.sort((a, b) => new Date(a.datum) - new Date(b.datum)); await writeTermine(termine); return true; } async function deleteTermin(terminData) { let termine = await readTermine(); termine = termine.filter( (t) => !(t.datum === terminData.datum && t.titel === terminData.titel && t.beschreibung === terminData.beschreibung && t.kategorie === terminData.kategorie) ); await writeTermine(termine); return true; } export { deleteTermin as d, readTermine as r, saveTermin as s }; //# sourceMappingURL=termine.mjs.map