"use client";
import { useCallback, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { UpdateVisibilityInputSchema } from "@stigmer/protos/ai/stigmer/commons/apiresource/io_pb";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that updates the visibility of a Skill, Agent, or
 * MCP Server.
 *
 * Wraps the generated `stigmer.{kind}.updateVisibility()` SDK method
 * with loading and error state management. The hook is stateless with
 * respect to the resource — the caller is responsible for refreshing
 * the resource after a successful update (e.g., via `refetch` from
 * the corresponding data hook).
 *
 * Pass `null` for `resourceId` to produce a stable no-op (useful when
 * the resource hasn't loaded yet).
 *
 * @example
 * ```tsx
 * const { updateVisibility, isPending } = useUpdateVisibility("skill", skill.metadata.id);
 *
 * <VisibilityToggle
 *   visibility={skill.metadata.visibility}
 *   onVisibilityChange={updateVisibility}
 *   isPending={isPending}
 * />
 * ```
 */
export function useUpdateVisibility(kind, resourceId) {
    const stigmer = useStigmer();
    const [isPending, setIsPending] = useState(false);
    const [error, setError] = useState(null);
    const updateVisibility = useCallback(async (visibility) => {
        if (!resourceId)
            return;
        setIsPending(true);
        setError(null);
        try {
            const input = create(UpdateVisibilityInputSchema, {
                resourceId,
                visibility,
            });
            switch (kind) {
                case "skill":
                    await stigmer.skill.updateVisibility(input);
                    break;
                case "agent":
                    await stigmer.agent.updateVisibility(input);
                    break;
                case "mcpServer":
                    await stigmer.mcpServer.updateVisibility(input);
                    break;
            }
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsPending(false);
        }
    }, [kind, resourceId, stigmer]);
    return { updateVisibility, isPending, error };
}
//# sourceMappingURL=useUpdateVisibility.js.map