<script setup>
import { useNuxtApp, showError } from "#app";
import { callOnce } from "#imports";
const { $auth0 } = useNuxtApp();
const emit = defineEmits(["success", "error"]);
const params = new URLSearchParams(window.location.search);
const code = params.get("code");
const error = params.get("error");
const invitation = params.get("invitation");
const organization = params.get("organization");
const redirect = params.get("redirect");
function handleInvite() {
  return $auth0.loginWithRedirect({ authorizationParams: { organization, invitation } });
}
function handleError() {
  const errorDescription = params.get("error_description") ?? "Auth error";
  emit("error", errorDescription);
  return showError({
    statusCode: 400,
    statusMessage: errorDescription
  });
}
function handleCode() {
  return void $auth0.handleRedirectCallback().then(() => emit("success"));
}
callOnce(async () => {
  if (error) return handleError();
  if (code) return handleCode();
  if (invitation && organization) return handleInvite();
  return $auth0.loginWithRedirect({ appState: { target: redirect || "/" } });
});
</script>

<template>
  <div class="d-flex gap-3 justify-content-center align-items-center h-100">
    <BSpinner variant="primary" />
    <span v-text="'Loading...'" />
  </div>
</template>
