import { cache } from 'react'; export const DOMAIN = 'sgemed.com'; export const API_BASE = 'https://api.meshkee.com/api/v1'; export const CUSTOMER_URL = 'https://customer.sgemed.com'; export const CUSTOMER_PROFILE_URL = `${CUSTOMER_URL}/profile`; export const WEBSITE_MANAGEMENT_URL = 'https://business.sgemed.com/website'; export class ApiError extends Error { constructor(public status: number) { super(`Meshkee request failed (${status})`); } } export async function api(path: string, init?: RequestInit): Promise { const response = await fetch(`${API_BASE}${path}`, { ...init, cache: 'no-store', signal: AbortSignal.timeout(12000) }); if (!response.ok) throw new ApiError(response.status); return response.json() as Promise; } export const getTenant = cache(() => api<{id: number | string; nameFa: string}>(`/tenants/${DOMAIN}`)); export async function tenantApi(path: string, init?: RequestInit): Promise { await getTenant(); return api(`/tenants/${DOMAIN}${path}`, init); } export function safeUrl(value: unknown): string | undefined { if (typeof value !== 'string' || !value) return; try { const url = new URL(value); if (url.protocol === 'https:') return url.href; } catch {} } export async function getFavicon() { try { const data = await tenantApi<{faviconUrl: string | null}>('/website/favicon'); return safeUrl(data.faviconUrl) || '/favicon.svg'; } catch { return '/favicon.svg'; } }