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, "../server/data", filename);
  }
  return path.join(cwd, "server/data", filename);
};
const NEWS_FILE = getDataPath("news.json");
async function readNews() {
  try {
    const data = await promises.readFile(NEWS_FILE, "utf-8");
    return JSON.parse(data);
  } catch (error) {
    if (error.code === "ENOENT") {
      return [];
    }
    console.error("Fehler beim Lesen der News:", error);
    return [];
  }
}
async function writeNews(news) {
  try {
    await promises.writeFile(NEWS_FILE, JSON.stringify(news, null, 2), "utf-8");
    return true;
  } catch (error) {
    console.error("Fehler beim Schreiben der News:", error);
    return false;
  }
}
async function saveNews(newsData) {
  const newsList = await readNews();
  if (newsData.id) {
    const index = newsList.findIndex((n) => n.id === newsData.id);
    if (index !== -1) {
      newsList[index] = {
        ...newsList[index],
        ...newsData,
        updated: (/* @__PURE__ */ new Date()).toISOString()
      };
    } else {
      throw new Error("News nicht gefunden");
    }
  } else {
    const newItem = {
      ...newsData,
      id: randomUUID(),
      // Cryptographically secure unique ID
      isPublic: newsData.isPublic || false,
      // Default to internal
      created: (/* @__PURE__ */ new Date()).toISOString(),
      updated: (/* @__PURE__ */ new Date()).toISOString()
    };
    newsList.unshift(newItem);
  }
  await writeNews(newsList);
  return true;
}
async function deleteNews(id) {
  const newsList = await readNews();
  const filtered = newsList.filter((n) => n.id !== id);
  await writeNews(filtered);
  return true;
}

export { deleteNews as d, readNews as r, saveNews as s };
//# sourceMappingURL=news.mjs.map
