import { slugify } from "./collections"; // Types and pure helpers behind the page editor (add-cms-pages). export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FEATURED_PRODUCTS" | "COLLECTION_LIST" | "FAQ" | "FORM"; export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURED_PRODUCTS", "COLLECTION_LIST", "FAQ", "FORM"]; export const BLOCK_LABELS: Record = { HERO: "CmsBlockHero", RICH_TEXT: "CmsBlockRichText", IMAGE_TEXT: "CmsBlockImageText", FEATURED_PRODUCTS: "CmsBlockFeaturedProducts", COLLECTION_LIST: "CmsBlockCollectionList", FAQ: "CmsBlockFaq", FORM: "CmsBlockForm", }; export interface CmsFaqItem { question: string; answer: string; } export interface CmsBlock { type: BlockType; imageUrl?: string; heading?: string; text?: string; buttonLabel?: string; buttonUrl?: string; html?: string; imagePosition?: "LEFT" | "RIGHT"; collectionId?: number | null; limit?: number; collectionIds?: number[]; items?: CmsFaqItem[]; formId?: number | null; } export interface CmsPageDraft { id?: number; slug: string; slugTouched: boolean; title: string; metaDescription: string; status: "Y" | "N"; blocks: CmsBlock[]; } export interface CmsPageListItem { id: number; slug: string; title: string; status: "Y" | "N"; updatedAt?: string | null; } export interface CmsPageView { id: number; slug: string; title: string; metaDescription: string | null; status: "Y" | "N"; blocks: CmsBlock[]; updatedAt?: string | null; } const HELP_SLUGS = new Set(["help", "contact", "delivery-returns", "warranty"]); export function emptyPage(): CmsPageDraft { return { id: undefined, slug: "", slugTouched: false, title: "", metaDescription: "", status: "Y", blocks: [], }; } export function emptyBlock(type: BlockType): CmsBlock { switch (type) { case "FAQ": return { type, heading: "", items: [{ question: "", answer: "" }] }; case "IMAGE_TEXT": return { type, imageUrl: "", heading: "", buttonLabel: "", buttonUrl: "", html: "", imagePosition: "LEFT" }; case "FEATURED_PRODUCTS": return { type, collectionId: null, limit: 8 }; case "COLLECTION_LIST": return { type, collectionIds: [] }; case "FORM": return { type, heading: "", formId: null }; default: return { type }; } } export function withTitle(draft: CmsPageDraft, title: string): CmsPageDraft { return { ...draft, title, slug: draft.slugTouched ? draft.slug : slugify(title) }; } export function moveBlock(blocks: CmsBlock[], index: number, delta: number): CmsBlock[] { const target = index + delta; if (target < 0 || target >= blocks.length) return blocks; const next = [...blocks]; [next[index], next[target]] = [next[target], next[index]]; return next; } export function pageAddress(slug: string): string { if (HELP_SLUGS.has(slug)) return "/" + slug; return "/pages/" + slug; } function isBlank(v: unknown): boolean { return v === "" || v === null || v === undefined; } export type CmsPageBody = Omit & { id?: number }; export function pageBody(draft: CmsPageDraft): CmsPageBody { const trimmedMeta = draft.metaDescription.trim(); return { id: draft.id, title: draft.title.trim(), slug: draft.slug, metaDescription: trimmedMeta || null, status: draft.status, blocks: draft.blocks.map((b) => { const obj: Record = { type: b.type }; for (const key of Object.keys(b) as (keyof CmsBlock)[]) { if (key === "type") continue; if (key === "html") { if (b.html != null && b.html.trim()) obj.html = b.html.trim(); continue; } if (key === "items") { if (b.items) { obj.items = b.items.map((it) => ({ question: it.question?.trim() ?? "", answer: it.answer?.trim() ?? "", })); } continue; } const val = b[key]; if (isBlank(val)) continue; if (typeof val === "string" && val.trim() !== val) { obj[key] = val.trim(); } else { obj[key] = val; } } return obj as unknown as CmsBlock; }), }; }