"use client";
import { create } from "@bufbuild/protobuf";
import { OAuthConnectionHealth, GetOAuthGrantStatusInputSchema, } from "@stigmer/protos/ai/stigmer/agentic/mcpserver/v1/io_pb";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
const BIGINT_ZERO = BigInt(0);
const IDLE_DATA = {
    connected: false,
    accessTokenExpiresAt: BIGINT_ZERO,
    targetEnvVar: "",
    authMethod: "",
    connectionHealth: OAuthConnectionHealth.OAUTH_CONNECTION_HEALTH_UNSPECIFIED,
};
/**
 * Data hook that fetches the OAuth grant status for a single MCP server.
 *
 * Wraps `stigmer.mcpServer.getOAuthGrantStatus()` with loading, error,
 * and idle state management. When `resourceId` or `org` changes, the
 * previous in-flight request is discarded and a fresh fetch begins.
 *
 * Pass `null` for either parameter to skip fetching (stable no-op).
 * This is useful when the MCP server resource has not loaded yet.
 *
 * @example
 * ```tsx
 * const grantStatus = useOAuthGrantStatus(mcpServer?.metadata?.id ?? null, org);
 *
 * if (grantStatus.isLoading) return <Spinner />;
 * if (grantStatus.connected) return <span>Connected via OAuth</span>;
 * return <button onClick={startOAuth}>Sign in</button>;
 * ```
 */
export function useOAuthGrantStatus(resourceId, org) {
    const stigmer = useStigmer();
    const { data, isLoading, isRefetching, error, refetch } = useFetch(resourceId && org
        ? async () => {
            const result = await stigmer.mcpServer.getOAuthGrantStatus(create(GetOAuthGrantStatusInputSchema, { resourceId, org }));
            return {
                connected: result.connected,
                accessTokenExpiresAt: result.accessTokenExpiresAt,
                targetEnvVar: result.targetEnvVar,
                authMethod: result.authMethod,
                connectionHealth: result.connectionHealth,
            };
        }
        : null, [resourceId, org, stigmer], IDLE_DATA);
    return { ...data, isLoading, isRefetching, error, refetch };
}
//# sourceMappingURL=useOAuthGrantStatus.js.map