// src/lib/gsc/client.ts — Google Search Console REST helper (no googleapis dep).
import "server-only";
import { parseGscSites } from "./parse";
import type { GscProperty } from "./types";

export { parseGscSites };

const SITES_URL = "https://www.googleapis.com/webmasters/v3/sites";

/** List the verified GSC properties for the given access token. */
export async function fetchGscProperties(
  accessToken: string,
): Promise<GscProperty[]> {
  const res = await fetch(SITES_URL, {
    headers: { Authorization: `Bearer ${accessToken}` },
  });
  if (!res.ok) {
    throw new Error(`GSC sites.list failed: HTTP ${res.status}`);
  }
  return parseGscSites(await res.json());
}
