"use client";
import { useCallback, useState } from "react";
import { PENDING_SUBJECT, } from "@stigmer/sdk";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
import { toProtoHarness } from "../models/harness";
/**
 * Behavior hook that wraps `session.create()` with loading/error state.
 *
 * Creates a Session — the conversation context that holds workspace
 * entries, thread state, and sandbox references.
 *
 * Exactly one agent resolution strategy must be provided:
 *
 * 1. **`agentInstanceId`** — Use a pre-provisioned instance directly.
 *    Typical for platform builders who manage instances programmatically.
 * 2. **`agentRef`** — Resolve the agent's default instance via
 *    `agent.getByReference()`. Useful when you know the agent slug
 *    but not the instance ID.
 *
 * This hook maps 1:1 to the Session aggregate. To start the first
 * execution within the session, compose with {@link useCreateAgentExecution}.
 *
 * @example
 * ```tsx
 * // Platform builder: pre-provisioned instance
 * const { create } = useCreateSession();
 * await create({ org: "acme", agentInstanceId: "inst-abc123" });
 * ```
 *
 * @example
 * ```tsx
 * // Agent reference: resolves to the agent's default instance
 * const { create } = useCreateSession();
 * await create({
 *   org: "acme",
 *   agentRef: { org: "acme", slug: "code-reviewer" },
 * });
 * ```
 */
export function useCreateSession() {
    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 {
            let resolvedInstanceId;
            if (input.agentInstanceId) {
                resolvedInstanceId = input.agentInstanceId;
            }
            else if (input.agentRef) {
                const agent = await stigmer.agent.getByReference(input.agentRef);
                const defaultId = agent.status?.defaultInstanceId;
                if (!defaultId) {
                    throw new Error(`Agent "${input.agentRef.org}/${input.agentRef.slug}" does not have a default instance. ` +
                        `Pass an explicit agentInstanceId instead.`);
                }
                resolvedInstanceId = defaultId;
            }
            else {
                throw new Error("useCreateSession requires either agentInstanceId or agentRef. " +
                    "Provide one to specify which agent this session should use.");
            }
            const session = await stigmer.session.create({
                name: `session-${Date.now()}`,
                org: input.org,
                subject: input.subject ?? PENDING_SUBJECT,
                workspaceEntries: input.workspaceEntries,
                mcpServerUsages: input.mcpServerUsages,
                skillRefs: input.skillRefs,
                runnerId: input.runnerId,
                agentInstanceId: resolvedInstanceId,
                harness: input.harness ? toProtoHarness(input.harness) : undefined,
            });
            return { sessionId: session.metadata.id };
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateSession.js.map