"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks.js";
import { toError } from "../internal/toError.js";
/**
 * Behavior hook that wraps `agent.apply()` with loading and error state.
 *
 * Creates an agent blueprint from an {@link AgentInput}. Uses `apply()`
 * (upsert) rather than `create()` so re-submissions are idempotent —
 * matching the CLI's `stigmer apply` semantics.
 *
 * Follows the established SDK mutation hook pattern: `isCreating` flag,
 * `error` state, `clearError` reset, result returned from the promise
 * (not stored in hook state).
 *
 * @example
 * ```tsx
 * const { create, isCreating, error, clearError } = useCreateAgent();
 *
 * const agent = await create({
 *   name: "pr-review-bot",
 *   org: "acme",
 *   instructions: "Review pull requests for code quality...",
 *   mcpServerUsages: [{ mcpServerRef: { org: "acme", slug: "github" } }],
 * });
 * // agent.metadata?.slug → "pr-review-bot"
 * ```
 */
export function useCreateAgent() {
    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.agent.apply(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateAgent.js.map