import { useState as p, useEffect as b } from "react";
const o = (t) => ({
  id: "not-found",
  title: "",
  subtitle: null,
  type: t,
  content_type: "JSON",
  body: "",
  language: "",
  context: null,
  branding_id: null,
  branding: null,
  images: null,
  labels: null,
  created_at: "",
  updated_at: ""
}), w = (t, e, i) => {
  const [u, a] = p(null), f = async () => {
    const s = i.helpers.getSDKConfiguration(), c = s.apiKey, d = s.clientId, l = await fetch(
      `https://client.fansunitedapi.com/v1/clients/${d}/info-pages/${t}?key=${c}&disable_cache=${Date.now()}`
    ), n = await l.json();
    if (l.ok) {
      if (n.data.type !== e) {
        console.warn(
          `Info page with id ${t} is not a ${e} type. Got ${n.data.type} instead.`
        ), a(o(e));
        return;
      }
      if (n.data.content_type !== "JSON") {
        console.warn(
          `Info page with id ${t} is not a JSON type. Got ${n.data.content_type} instead.`
        ), a(o(e));
        return;
      }
      if (n.data.branding_id) {
        const r = await fetch(
          `https://client.fansunitedapi.com/v1/clients/${d}/templates/brandings/${n.data.branding_id}?key=${c}&disable_cache=${Date.now()}`
        ), g = await r.json();
        r.ok && (n.data.branding = g.data);
      }
      a(n.data);
      return;
    }
    console.warn(n), a(o(e));
  };
  return b(() => (f(), () => a(null)), [t, i]), { infoPage: u, setInfoPage: a };
};
export {
  w as u
};
