import { u as useToast, r as reactExports, j as jsxRuntimeExports, S as SESSION_KEYS, I as InspectorOAuthClientProvider, a as auth } from "./index-CMyU_PZH.js";
const parseOAuthCallbackParams = (location) => {
  const params = new URLSearchParams(location);
  const code = params.get("code");
  if (code) {
    return { successful: true, code };
  }
  const error = params.get("error");
  const error_description = params.get("error_description");
  const error_uri = params.get("error_uri");
  if (error) {
    return { successful: false, error, error_description, error_uri };
  }
  return {
    successful: false,
    error: "invalid_request",
    error_description: "Missing code or error in response",
    error_uri: null
  };
};
const generateOAuthErrorDescription = (params) => {
  const error = params.error;
  const errorDescription = params.error_description;
  const errorUri = params.error_uri;
  return [
    `Error: ${error}.`,
    errorDescription ? `Details: ${errorDescription}.` : "",
    errorUri ? `More info: ${errorUri}.` : ""
  ].filter(Boolean).join("\n");
};
const OAuthCallback = ({ onConnect }) => {
  const { toast } = useToast();
  const hasProcessedRef = reactExports.useRef(false);
  reactExports.useEffect(() => {
    const handleCallback = async () => {
      if (hasProcessedRef.current) {
        return;
      }
      hasProcessedRef.current = true;
      const notifyError = (description) => void toast({
        title: "OAuth Authorization Error",
        description,
        variant: "destructive"
      });
      const params = parseOAuthCallbackParams(window.location.search);
      if (!params.successful) {
        return notifyError(generateOAuthErrorDescription(params));
      }
      const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
      if (!serverUrl) {
        return notifyError("Missing Server URL");
      }
      let result;
      try {
        const serverAuthProvider = new InspectorOAuthClientProvider(serverUrl);
        result = await auth(serverAuthProvider, {
          serverUrl,
          authorizationCode: params.code
        });
      } catch (error) {
        console.error("OAuth callback error:", error);
        return notifyError(`Unexpected error occurred: ${error}`);
      }
      if (result !== "AUTHORIZED") {
        return notifyError(
          `Expected to be authorized after providing auth code, got: ${result}`
        );
      }
      toast({
        title: "Success",
        description: "Successfully authenticated with OAuth",
        variant: "default"
      });
      onConnect(serverUrl);
    };
    handleCallback().finally(() => {
      window.history.replaceState({}, document.title, "/");
    });
  }, [toast, onConnect]);
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "flex items-center justify-center h-screen", children: /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-lg text-gray-500", children: "Processing OAuth callback..." }) });
};
export {
  OAuthCallback as default
};
