"use client";
import { useCallback, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { DisconnectOAuthInputSchema } from "@stigmer/protos/ai/stigmer/agentic/mcpserver/v1/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that wraps `mcpServer.disconnectOAuth()` with loading
 * and error state.
 *
 * Removes the user's OAuth grant and associated managed environment
 * for a given MCP server resource. After a successful disconnect the
 * UI should revert to the "Not connected" state — call `refetch()` on
 * the credentials / grant status hooks to reflect the change.
 *
 * @example
 * ```tsx
 * const { disconnect, isDisconnecting, error } = useDisconnectOAuth();
 *
 * await disconnect(mcpServerId, org);
 * credentials.refetch(); // refresh grant status + env
 * ```
 */
export function useDisconnectOAuth() {
    const stigmer = useStigmer();
    const [isDisconnecting, setIsDisconnecting] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const disconnect = useCallback(async (resourceId, org) => {
        setIsDisconnecting(true);
        setError(null);
        try {
            const result = await stigmer.mcpServer.disconnectOAuth(create(DisconnectOAuthInputSchema, { resourceId, org }));
            return result.disconnected;
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsDisconnecting(false);
        }
    }, [stigmer]);
    return { disconnect, isDisconnecting, error, clearError };
}
//# sourceMappingURL=useDisconnectOAuth.js.map