import { aL as T, aS as _, aO as S } from "./index-THhC4TET.js";
function i(a, t) {
  if (t) {
    window.open(a, "_blank");
    return;
  }
  window.location.href = a;
}
function A(a, t) {
  const u = T.usePathname(), l = `${S}${u}`;
  return _({
    ...t,
    mutationFn: async (n) => {
      var g, f, h;
      let r;
      const s = n && n.teamId ? n.teamId : void 0, c = n && n.defaultInstall ? n.defaultInstall : void 0, o = t != null && t.returnTo ? t.returnTo : n && n.returnTo ? n.returnTo : void 0;
      if (n === "" ? r = a : r = n.type, r != null && r.endsWith("_other_calendar") && (r = r.split("_other_calendar")[0]), t != null && t.installGoogleVideo && r !== "google_calendar")
        throw new Error("Could not install Google Meet");
      const m = {
        onErrorReturnTo: l,
        fromApp: !0,
        ...s && { teamId: s },
        ...r === "google_calendar" && { installGoogleVideo: t == null ? void 0 : t.installGoogleVideo },
        ...o && { returnTo: o },
        ...c && { defaultInstall: c }
      }, w = JSON.stringify(m), P = I({
        stateStr: w,
        teamId: s,
        returnTo: o
      }), d = await fetch(`/api/integrations/${r}/add${P}`);
      if (!d.ok) {
        const p = await d.json();
        throw new Error(p.message || "Something went wrong");
      }
      const e = await d.json();
      return /https?:\/\//.test(e == null ? void 0 : e.url) && !((g = e == null ? void 0 : e.url) != null && g.startsWith(window.location.origin)) ? (i(e.url, e.newTab), { setupPending: !e.newTab }) : e.url ? (i(e.url, e.newTab), {
        setupPending: ((f = e == null ? void 0 : e.url) == null ? void 0 : f.endsWith("/setup")) || ((h = e == null ? void 0 : e.url) == null ? void 0 : h.includes("/apps/installation/event-types"))
      }) : o ? (i(o, !1), { setupPending: !0 }) : { setupPending: !1 };
    }
  });
}
const I = ({
  stateStr: a,
  teamId: t,
  returnTo: u
}) => {
  const l = new URL("https://example.com");
  return l.searchParams.append("state", a), t !== void 0 && l.searchParams.append("teamId", t.toString()), u && l.searchParams.append("returnTo", u), l.search;
};
export {
  A as u
};
