export async function postJson<T>(url: string, payload: unknown): Promise<T> {
  const token = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content ?? '';
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRF-TOKEN': token,
      Accept: 'application/json',
    },
    body: JSON.stringify(payload),
  });

  if (!response.ok) {
    throw new Error('درخواست با خطا روبه‌رو شد.');
  }

  return response.json() as Promise<T>;
}
