import type { MeshkeeBlog, MeshkeeMediaItem, MeshkeePortfolio, MeshkeeProduct, } from "@/services/meshkee-content"; export function toPlainText(value: string) { if (!value.includes("<")) return value; return value .replace(//gi, "\n") .replace(/<\/p>/gi, "\n") .replace(/<[^>]+>/g, "") .replace(/ /g, " ") .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/\n{3,}/g, "\n\n") .trim(); } export function excerpt(value: string, max = 160) { const plain = toPlainText(value).replace(/\s+/g, " ").trim(); if (plain.length <= max) return plain; return `${plain.slice(0, max - 1).trimEnd()}…`; } export function formatDate(value?: string) { if (!value) return ""; const date = new Date(value); if (Number.isNaN(date.getTime())) return ""; return date.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", }); } export function productImage(product: MeshkeeProduct) { return product.thumbnailUrl || product.thumbnail || product.image || ""; } export function productSubtitle(product: MeshkeeProduct) { const raw = product.categoryName?.trim() ?? ""; if (!raw) return ""; if (raw.toLowerCase() === product.title.trim().toLowerCase()) return ""; return raw; } export function blogImage(post: MeshkeeBlog) { return post.titleImageUrl || ""; } export function portfolioImage(item: MeshkeePortfolio) { return item.titleImageUrl || item.coverUrl || item.image || ""; } export function galleryImage(item: MeshkeeMediaItem) { return item.publicUrl || ""; } export function portfolioTitleEn(item: MeshkeePortfolio) { return ( item.titleEn?.trim() || item.nameEn?.trim() || item.title?.trim() || item.name?.trim() || "" ); } export function portfolioTitleFa(item: MeshkeePortfolio) { return item.titleFa?.trim() || item.nameFa?.trim() || ""; } export function portfolioTitle(item: MeshkeePortfolio) { const candidates = [ item.titleEn, item.titleFa, item.title, item.nameEn, item.nameFa, item.name, ]; for (const value of candidates) { const text = value?.trim(); if (text) return text; } const slug = item.slug?.trim(); if (slug) { return slug .replace(/[-_]+/g, " ") .replace(/\s+/g, " ") .trim() .replace(/\b\w/g, (char) => char.toUpperCase()); } return "Untitled portfolio"; } export function buildPageItems( page: number, pageCount: number, ): (number | "…")[] { if (pageCount <= 7) { return Array.from({ length: pageCount }, (_, i) => i + 1); } const items: (number | "…")[] = [1]; const start = Math.max(2, page - 1); const end = Math.min(pageCount - 1, page + 1); if (start > 2) items.push("…"); for (let n = start; n <= end; n += 1) items.push(n); if (end < pageCount - 1) items.push("…"); items.push(pageCount); return items; } export const PRODUCT_PAGE_SIZE = 12; export const BLOG_PAGE_SIZE = 9; export const GALLERY_PAGE_SIZE = 16; export const PORTFOLIO_PAGE_SIZE = 12; export const CONTENT_PAGE_SIZE = 12;