"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Mutation hook that updates an existing AgentInstance resource.
 *
 * Wraps `stigmer.agentInstance.update()` with loading/error state.
 * The caller is responsible for refreshing the instance after a
 * successful update (e.g., via `refetch` from `useAgentInstances` or
 * `useAgentInstance`).
 *
 * @example
 * ```tsx
 * const { update, isUpdating } = useUpdateAgentInstance();
 *
 * const handleSave = async () => {
 *   await update({
 *     ...agentInstanceToInput(instance),
 *     environmentRefs: newRefs,
 *   });
 *   refetch();
 * };
 * ```
 */
export function useUpdateAgentInstance() {
    const stigmer = useStigmer();
    const [isUpdating, setIsUpdating] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const update = useCallback(async (input) => {
        setIsUpdating(true);
        setError(null);
        try {
            return await stigmer.agentInstance.update(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsUpdating(false);
        }
    }, [stigmer]);
    return { update, isUpdating, error, clearError };
}
//# sourceMappingURL=useUpdateAgentInstance.js.map