export async function getTotalMetadata(): Promise<number | null> {
  const url = "https://satupeta.jatimprov.go.id/api/geonetwork-record";

  try {
    const res = await fetch(url, { method: "GET", cache: "no-store" });
    if (!res.ok) {
      console.error(`Error fetching total metadata: ${res.status} ${res.statusText}`);
      return null;
    }

    const data = await res.json();

    if (typeof data === "number") return data;

    const candidates = [data?.total, data?.count, data?.value, data?.metadataTotal, data?.hits?.total?.value].filter((v) => typeof v === "number");
    if (candidates.length > 0) return candidates[0] as number;

    if (Array.isArray(data)) return data.length;

    return null;
  } catch (error) {
    console.error("Failed to fetch total metadata:", error);
    return null;
  }
}
