import { apiDelete, apiGet, apiPost, apiPut } from "@/app/lib/clientApi"; import type { CmsPage, pageBody } from "@/app/lib/cmsPages"; export interface PageListParams { q?: string; page: number; pageSize: number; companyId?: number; } export async function listPages(params: PageListParams): Promise<{ items: CmsPage[]; total: number }> { const qs = new URLSearchParams(); if (params.q) qs.set("q", params.q); if (params.companyId) qs.set("companyId", String(params.companyId)); qs.set("page", String(Math.max(0, params.page - 1))); qs.set("size", String(params.pageSize)); const raw = await apiGet<{ content?: CmsPage[]; totalElements?: number }>(`/api/pages?${qs.toString()}`); const items = Array.isArray(raw?.content) ? raw.content : []; return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length }; } export const getPage = (id: number) => apiGet(`/api/pages/${id}`); export const savePage = (id: number | undefined, body: ReturnType) => id == null ? apiPost("/api/pages", body) : apiPut(`/api/pages/${id}`, body); export const deletePage = (id: number) => apiDelete(`/api/pages/${id}`); /** Base64 without the data-URL prefix; the counter checks type (PNG, JPEG, WebP) and size (2 MB). */ export const uploadPageImage = (companyId: number | undefined, data: string) => apiPost<{ id: number }>("/api/pages/images", { companyId, data });