/**
 * Cliente HTTP simples para a API pública do AssinaPro.
 * Só depende de fetch — funciona no service worker (background) e no popup.
 */
import { API_SIGNATURES } from "./config.js";

export async function fetchSignatures(accessToken) {
  const res = await fetch(API_SIGNATURES, {
    method: "GET",
    headers: {
      "Authorization": `Bearer ${accessToken}`,
      "Accept": "application/json",
    },
  });
  if (res.status === 401) {
    const err = new Error("unauthorized");
    err.code = "unauthorized";
    throw err;
  }
  if (!res.ok) {
    throw new Error(`Erro ${res.status} ao carregar assinaturas.`);
  }
  return (await res.json()).signatures ?? [];
}
