"use client";
import { useCallback, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { GetOrgOAuthAppInputSchema, SetOrgOAuthAppInputSchema, DeleteOrgOAuthAppInputSchema, } from "@stigmer/protos/ai/stigmer/agentic/mcpserver/v1/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
import { useFetch } from "../internal/useFetch";
const IDLE_DATA = {
    hasOverride: false,
    oauthAppId: null,
    clientId: null,
};
const IDLE = {
    ...IDLE_DATA,
    isLoading: false,
    isRefetching: false,
    error: null,
    refetch: () => { },
    setOrgOAuthApp: () => Promise.resolve(""),
    isSetting: false,
    setError: null,
    deleteOrgOAuthApp: () => Promise.resolve(false),
    isDeleting: false,
    deleteError: null,
    clearErrors: () => { },
};
/**
 * Hybrid data + behavior hook for managing org-level BYOA OAuth app overrides.
 *
 * **Data side**: Auto-fetches `getOrgOAuthApp` when both parameters are
 * non-null. Returns override existence, OAuthApp ID, and client ID for
 * display. Follows the same fetch-on-mount pattern as
 * {@link useOAuthGrantStatus}.
 *
 * **Behavior side**: Exposes `setOrgOAuthApp` and `deleteOrgOAuthApp`
 * mutations bound to the hook's resource + org context, eliminating
 * parameter repetition at call sites.
 *
 * Pass `null` for either parameter to skip fetching (stable no-op).
 *
 * @example
 * ```tsx
 * const orgApp = useOrgOAuthApp(mcpServer?.metadata?.id ?? null, org);
 *
 * if (orgApp.hasOverride) {
 *   return <span>Using your OAuth app (client: {orgApp.clientId})</span>;
 * }
 *
 * const handleSubmit = async (clientId: string, clientSecret: string) => {
 *   await orgApp.setOrgOAuthApp(clientId, clientSecret);
 *   orgApp.refetch();
 *   credentials.refetch();
 * };
 * ```
 */
export function useOrgOAuthApp(resourceId, org) {
    const stigmer = useStigmer();
    const { data, isLoading, isRefetching, error, refetch } = useFetch(resourceId && org
        ? async () => {
            const result = await stigmer.mcpServer.getOrgOAuthApp(create(GetOrgOAuthAppInputSchema, { resourceId, org }));
            return {
                hasOverride: result.hasOverride,
                oauthAppId: result.oauthAppId || null,
                clientId: result.clientId || null,
            };
        }
        : null, [resourceId, org, stigmer], IDLE_DATA);
    const [isSetting, setIsSetting] = useState(false);
    const [setError_, setSetError] = useState(null);
    const [isDeleting, setIsDeleting] = useState(false);
    const [deleteError, setDeleteError] = useState(null);
    const clearErrors = useCallback(() => {
        setSetError(null);
        setDeleteError(null);
    }, []);
    const setOrgOAuthApp = useCallback(async (newClientId, clientSecret) => {
        if (!resourceId || !org) {
            throw new Error("useOrgOAuthApp: resourceId and org are required for setOrgOAuthApp");
        }
        setIsSetting(true);
        setSetError(null);
        try {
            const result = await stigmer.mcpServer.setOrgOAuthApp(create(SetOrgOAuthAppInputSchema, {
                resourceId,
                org,
                clientId: newClientId,
                clientSecret,
            }));
            return result.oauthAppId;
        }
        catch (err) {
            const wrapped = toError(err);
            setSetError(wrapped);
            throw wrapped;
        }
        finally {
            setIsSetting(false);
        }
    }, [stigmer, resourceId, org]);
    const deleteOrgOAuthApp = useCallback(async () => {
        if (!resourceId || !org) {
            throw new Error("useOrgOAuthApp: resourceId and org are required for deleteOrgOAuthApp");
        }
        setIsDeleting(true);
        setDeleteError(null);
        try {
            const result = await stigmer.mcpServer.deleteOrgOAuthApp(create(DeleteOrgOAuthAppInputSchema, { resourceId, org }));
            return result.deleted;
        }
        catch (err) {
            const wrapped = toError(err);
            setDeleteError(wrapped);
            throw wrapped;
        }
        finally {
            setIsDeleting(false);
        }
    }, [stigmer, resourceId, org]);
    if (!resourceId || !org)
        return { ...IDLE, refetch, clearErrors };
    return {
        ...data,
        isLoading,
        isRefetching,
        error,
        refetch,
        setOrgOAuthApp,
        isSetting,
        setError: setError_,
        deleteOrgOAuthApp,
        isDeleting,
        deleteError,
        clearErrors,
    };
}
//# sourceMappingURL=useOrgOAuthApp.js.map