const apiBaseUrl = import.meta.env?.VITE_API_BASE_URL ?? '/api/v1'; export async function createAdminSale(input, csrfToken) { const response = await fetch(`${apiBaseUrl}/admin/sales`, { method: 'POST', credentials: 'include', headers: { 'content-type': 'application/json', 'x-csrf-token': csrfToken, }, body: JSON.stringify(input), }); const payload = await response.json().catch(() => null); if (!response.ok) throw new Error(payload?.error?.message ?? 'Sale request failed.'); return payload?.data; } export async function listAdminSales() { const response = await fetch(`${apiBaseUrl}/admin/sales`, { credentials: 'include' }); const payload = await response.json().catch(() => null); if (!response.ok) throw new Error(payload?.error?.message ?? 'Sale history is unavailable.'); return payload?.data ?? []; } export async function voidAdminSale(id, reason, csrfToken) { const response = await fetch(`${apiBaseUrl}/admin/sales/${encodeURIComponent(id)}/void`, { method: 'POST', credentials: 'include', headers: { 'content-type': 'application/json', 'x-csrf-token': csrfToken, }, body: JSON.stringify({ reason }), }); const payload = await response.json().catch(() => null); if (!response.ok) throw new Error(payload?.error?.message ?? 'Sale correction failed.'); return payload?.data; }