"use client";
import { useCallback, useMemo, useState } from "react";
import { useStigmer } from "../hooks.js";
import { toError } from "../internal/toError.js";
import { toast } from "../feedback/toast.js";
/**
 * Mutation hook for deleting a resource from its detail page.
 *
 * Wraps the type-specific delete RPC with loading state, toast
 * feedback, and error handling. The `resourceId` may be `null`
 * when the resource hasn't loaded yet — `deleteResource` becomes
 * a no-op in that case.
 *
 * @example
 * ```tsx
 * const { deleteResource, isDeleting } = useDeleteResource(
 *   "agent",
 *   resourceId,
 *   "PR Review Agent",
 * );
 *
 * const handleDelete = async () => {
 *   const confirmed = await confirm({ ... });
 *   if (confirmed) {
 *     await deleteResource();
 *     router.push("/library/agents");
 *   }
 * };
 * ```
 */
export function useDeleteResource(kind, resourceId, resourceName) {
    const stigmer = useStigmer();
    const [isDeleting, setIsDeleting] = useState(false);
    const [error, setError] = useState(null);
    const deleteResource = useCallback(async () => {
        if (!resourceId)
            return;
        setIsDeleting(true);
        setError(null);
        try {
            switch (kind) {
                case "agent":
                    await stigmer.agent.delete(resourceId);
                    break;
                case "skill":
                    await stigmer.skill.delete(resourceId);
                    break;
                case "mcpServer":
                    await stigmer.mcpServer.delete({ resourceId });
                    break;
                case "workflow":
                    await stigmer.workflow.delete(resourceId);
                    break;
            }
            toast.success(resourceName
                ? `${resourceName} deleted`
                : `${kindLabel(kind)} deleted`);
        }
        catch (err) {
            const wrapped = toError(err);
            setError(wrapped);
            toast.error(`Failed to delete ${kindLabel(kind)}`);
            throw wrapped;
        }
        finally {
            setIsDeleting(false);
        }
    }, [stigmer, kind, resourceId, resourceName]);
    return useMemo(() => ({ deleteResource, isDeleting, error }), [deleteResource, isDeleting, error]);
}
function kindLabel(kind) {
    switch (kind) {
        case "agent":
            return "agent";
        case "skill":
            return "skill";
        case "mcpServer":
            return "MCP server";
        case "workflow":
            return "workflow";
    }
}
//# sourceMappingURL=useDeleteResource.js.map