"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../../hooks";
import { toError } from "../../internal/toError";
/**
 * Mutation hook that creates a new WorkflowInstance resource.
 *
 * Wraps `stigmer.workflowInstance.create()` with loading/error state.
 * The caller is responsible for refreshing the instance list after
 * a successful create (e.g., via `refetch` from `useWorkflowInstances`).
 *
 * @example
 * ```tsx
 * const { create, isCreating, error } = useCreateWorkflowInstance();
 *
 * const handleSubmit = async () => {
 *   const instance = await create({
 *     name: "prod-deploy",
 *     org: "my-org",
 *     workflowId: workflow.metadata.id,
 *     environmentRefs: [{ org: "my-org", slug: "aws-prod" }],
 *   });
 *   // instance created — refetch list
 * };
 * ```
 */
export function useCreateWorkflowInstance() {
    const stigmer = useStigmer();
    const [isCreating, setIsCreating] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const create = useCallback(async (input) => {
        setIsCreating(true);
        setError(null);
        try {
            return await stigmer.workflowInstance.create(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateWorkflowInstance.js.map