"use client";
import { useCallback, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { RunnerStopInputSchema } from "@stigmer/protos/ai/stigmer/agentic/runner/v1/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Mutation hook that wraps `runner.stop()` with loading and error state.
 *
 * Stops a runner gracefully. Connected runners receive a stop command
 * via the bidi stream; offline runners transition directly to STOPPED.
 * The operation is idempotent — stopping an already-stopped or failed
 * runner succeeds without error.
 *
 * Returns the updated {@link Runner} resource on success so callers
 * can reflect the new phase in the UI without a separate refetch.
 *
 * @example
 * ```tsx
 * const { stop, isStopping, error } = useStopRunner();
 *
 * await stop({ runnerId: "rnr_abc123", reason: "user requested" });
 * refetch(); // refresh the runner list
 * ```
 */
export function useStopRunner() {
    const stigmer = useStigmer();
    const [isStopping, setIsStopping] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const stop = useCallback(async (input) => {
        setIsStopping(true);
        setError(null);
        try {
            return await stigmer.runner.stop(create(RunnerStopInputSchema, {
                runnerId: input.runnerId,
                reason: input.reason ?? "",
            }));
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsStopping(false);
        }
    }, [stigmer]);
    return { stop, isStopping, error, clearError };
}
//# sourceMappingURL=useStopRunner.js.map