import { r as readCms, s as slugify, w as writeCms } from './cms_CRTVymfB.mjs';

const publishPost = async (input) => {
  const cms = await readCms();
  const title = input.title.trim();
  const slug = slugify(input.slug || title);
  if (!title || !slug) {
    throw new Error("title is required");
  }
  const body = Array.isArray(input.body) ? input.body : String(input.body ?? "").split(/\n\s*\n/).map((item) => item.trim()).filter(Boolean);
  const existingIndex = cms.posts.findIndex((item) => item.slug === slug);
  const existing = existingIndex >= 0 ? cms.posts[existingIndex] : void 0;
  const post = {
    slug,
    title,
    category: input.category || cms.categories[0] || "عمومی",
    time: input.time || "هم‌اکنون",
    readTime: input.readTime || "۳ دقیقه",
    excerpt: input.excerpt || body[0] || "",
    body,
    status: input.status || "pending",
    image: input.image || void 0,
    thumbnail: input.thumbnail || void 0,
    urgent: input.urgent ?? false,
    sidebar: input.sidebar ?? false,
    sourceUrl: input.sourceUrl || void 0,
    publishedAt: input.publishedAt || existing?.publishedAt || (/* @__PURE__ */ new Date()).toISOString()
  };
  if (existingIndex >= 0) {
    cms.posts[existingIndex] = post;
  } else {
    cms.posts.unshift(post);
  }
  if (!cms.categories.includes(post.category)) {
    cms.categories.push(post.category);
  }
  await writeCms(cms);
  return post;
};

export { publishPost as p };
