import { ProductResponse, CategoryResponse, BrandResponse } from './catalogService'; const API_URL = process.env.API_URL || process.env.NEXT_PUBLIC_API_URL || 'https://ifixkartbe.trionixsolution.com'; async function serverFetch(endpoint: string, options: RequestInit = {}): Promise { const res = await fetch(`${API_URL}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', ...options.headers, }, }); if (!res.ok) { throw new Error(`Catalog Server API call failed: ${res.status} ${res.statusText} at ${endpoint}`); } return res.json(); } export const catalogServerService = { async getProducts(): Promise { const data = await serverFetch('/api/v1/catalog/products?page=1&limit=48', { next: { revalidate: 30, tags: ['products'] } }); return Array.isArray(data) ? data : data?.products || []; }, async getProductDetail(slug: string): Promise<{ product: ProductResponse; reviews: any[]; related_products: ProductResponse[]; compatible_devices: any[]; specifications: any[]; }> { return serverFetch<{ product: ProductResponse; reviews: any[]; related_products: ProductResponse[]; compatible_devices: any[]; specifications: any[]; }>(`/api/v1/catalog/products/detail/${slug}`, { next: { revalidate: 60, tags: [`product-${slug}`] } }); }, async getCategories(): Promise { return serverFetch('/api/v1/catalog/categories/all', { next: { revalidate: 300, tags: ['categories'] } }); }, async getBrands(): Promise { return serverFetch('/api/v1/catalog/brands/all', { next: { revalidate: 300, tags: ['brands'] } }); }, async fetchLayoutConfigOnServer(): Promise { return serverFetch('/api/v1/storefront/layout/home', { cache: 'no-store' }); } };