import { r as requireAdmin } from '../../../chunks/adminAuth_CY4OwU1a.mjs';
import { r as readCms, w as writeCms } from '../../../chunks/cms_CRTVymfB.mjs';
import { p as publishPost } from '../../../chunks/publisher_Dj_UvF8J.mjs';
import { g as getTassFeedLinks } from '../../../chunks/tassAgent_jxZgSz7r.mjs';
export { renderers } from '../../../renderers.mjs';

const toList = (value) => String(value ?? "").split(/\r?\n|,/).map((item) => item.trim()).filter(Boolean);
const toParagraphs = (value) => String(value ?? "").split(/\n\s*\n/).map((item) => item.trim()).filter(Boolean);
const POST = async (context) => {
  const unauthorized = requireAdmin(context);
  if (unauthorized) {
    return unauthorized;
  }
  const formData = await context.request.formData();
  const action = String(formData.get("action") ?? "");
  const cms = await readCms();
  const tab = String(formData.get("tab") ?? "pending");
  if (action === "post-status") {
    const slug = String(formData.get("slug") ?? "");
    const status = String(formData.get("status") ?? "pending");
    cms.posts = cms.posts.map((post) => post.slug === slug ? { ...post, status } : post);
  }
  if (action === "toggle-post-flag") {
    const slug = String(formData.get("slug") ?? "");
    const flag = String(formData.get("flag") ?? "");
    cms.posts = cms.posts.map((post) => {
      if (post.slug !== slug) {
        return post;
      }
      if (flag === "urgent") {
        return { ...post, urgent: !post.urgent };
      }
      if (flag === "sidebar") {
        return { ...post, sidebar: !post.sidebar };
      }
      return post;
    });
  }
  if (action === "set-breaking-post") {
    const slug = String(formData.get("slug") ?? "");
    const post = cms.posts.find((item) => item.slug === slug);
    if (post) {
      cms.settings.breakingNews = {
        ...cms.settings.breakingNews,
        href: `/posts/${post.slug}/`,
        items: [post.title],
        postSlugs: [post.slug]
      };
    }
  }
  if (action === "set-home-featured") {
    const slug = String(formData.get("slug") ?? "");
    cms.settings.homeLayout.featuredSlug = slug;
  }
  if (action === "clear-tass-memory") {
    cms.settings.agents = {
      ...cms.settings.agents,
      tass: { processedLinks: [] }
    };
  }
  if (action === "fill-tass-memory") {
    const links = cms.posts.map((post) => post.sourceUrl).filter(Boolean);
    const feedLinks = await getTassFeedLinks(50);
    cms.settings.agents = {
      ...cms.settings.agents,
      tass: { processedLinks: Array.from(/* @__PURE__ */ new Set([...links, ...feedLinks])) }
    };
  }
  if (action === "delete-post") {
    const slug = String(formData.get("slug") ?? "");
    const confirm = String(formData.get("confirm") ?? "");
    if (confirm !== "confirm") {
      return context.redirect(`/admin/?deleteError=1&tab=${encodeURIComponent(tab)}`, 302);
    }
    cms.posts = cms.posts.filter((post) => post.slug !== slug);
  }
  if (action === "delete-all-posts") {
    const confirm = String(formData.get("confirm") ?? "");
    if (confirm !== "confirm") {
      return context.redirect(`/admin/?deleteError=1&tab=${encodeURIComponent(tab)}`, 302);
    }
    cms.posts = [];
  }
  if (action === "save-post") {
    const originalSlug = String(formData.get("originalSlug") ?? "");
    const title = String(formData.get("title") ?? "").trim();
    const post = await publishPost({
      slug: String(formData.get("slug") ?? "") || title,
      title,
      category: String(formData.get("category") ?? cms.categories[0] ?? "عمومی"),
      time: String(formData.get("time") ?? "").trim() || "هم‌اکنون",
      readTime: String(formData.get("readTime") ?? "").trim() || "۳ دقیقه",
      excerpt: String(formData.get("excerpt") ?? "").trim(),
      status: String(formData.get("status") ?? "pending"),
      body: toParagraphs(formData.get("body")),
      image: String(formData.get("image") ?? "").trim() || void 0,
      thumbnail: String(formData.get("thumbnail") ?? "").trim() || void 0,
      urgent: formData.get("urgent") === "on",
      sidebar: formData.get("sidebar") === "on"
    });
    if (originalSlug && originalSlug !== post.slug) {
      const updatedCms = await readCms();
      updatedCms.posts = updatedCms.posts.filter((post2) => post2.slug !== originalSlug);
      await writeCms(updatedCms);
    }
    return context.redirect(`/admin/?saved=1&tab=${post.status === "approved" ? "approved" : "pending"}`, 302);
  }
  if (action === "categories") {
    cms.categories = toList(formData.get("categories"));
  }
  if (action === "settings") {
    cms.settings.breakingNews = {
      label: String(formData.get("breakingLabel") ?? "فوری").trim(),
      href: String(formData.get("breakingHref") ?? "/").trim(),
      items: toList(formData.get("breakingItems")),
      postSlugs: toList(formData.get("breakingPostSlugs"))
    };
    cms.settings.hotTopics = toList(formData.get("hotTopics"));
    cms.settings.homeLayout = {
      featuredSlug: String(formData.get("featuredSlug") ?? "").trim(),
      sideFeaturedSlugs: toList(formData.get("sideFeaturedSlugs")).slice(0, 2),
      latestCount: Math.max(1, Number(formData.get("latestCount") ?? 4)),
      showAd: formData.get("showAd") === "on"
    };
    cms.settings.ad = {
      enabled: formData.get("adEnabled") === "on",
      label: String(formData.get("adLabel") ?? "آگهی").trim(),
      href: String(formData.get("adHref") ?? "/advertise/").trim(),
      text: String(formData.get("adText") ?? "").trim()
    };
  }
  await writeCms(cms);
  return context.redirect(`/admin/?saved=1&tab=${encodeURIComponent(tab)}`, 302);
};

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  POST
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
