"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../../hooks";
import { toError } from "../../internal/toError";
/**
 * Mutation hook that updates an existing WorkflowInstance resource.
 *
 * Wraps `stigmer.workflowInstance.update()` with loading/error state.
 * The caller is responsible for refreshing the instance after a successful
 * update (e.g., via `refetch` from `useWorkflowInstances` or `useWorkflowInstance`).
 *
 * @example
 * ```tsx
 * const { update, isUpdating } = useUpdateWorkflowInstance();
 *
 * const handleSave = async () => {
 *   await update({
 *     ...instanceToInput(instance),
 *     environmentRefs: newRefs,
 *   });
 *   refetch();
 * };
 * ```
 */
export function useUpdateWorkflowInstance() {
    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.workflowInstance.update(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsUpdating(false);
        }
    }, [stigmer]);
    return { update, isUpdating, error, clearError };
}
//# sourceMappingURL=useUpdateWorkflowInstance.js.map