"use client"

import { Icons } from "@/components/icons"
import { buttonVariants } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { SidebarNavItem } from "@/types"
import { User } from "next-auth"
import Link from "next/link"
import { usePathname } from "next/navigation"
import { ModeToggle } from "./mode-toggle"
import { NavPromo } from "./nav-promo"

export const Sidebar = ({ items, user }: { items: SidebarNavItem[], user: User | undefined }) => {
    const path = usePathname()

    if (!items?.length) {
        return null
    }

    const visibleItems = user ? items : items.slice(0, 3)

    return (
        <nav className="grid items-start gap-2">
            {visibleItems.map((item, index) => {
                const Icon = Icons[item.icon || "arrowRight"]
                if (item.title === "My Profile") {
                    item.href = `/${user?.username}`
                }
                return (
                    item.href && (
                        <Link key={index} href={item.disabled ? "/" : item.href} className={item.title === "Settings" ? "mt-10" : ""}>
                            <span
                                className={cn(
                                    "group flex items-center rounded-md px-3 py-2 font-medium hover:bg-accent hover:text-accent-foreground",
                                    path.startsWith(item.href) ? "bg-accent" : "transparent",
                                    item.disabled && "cursor-not-allowed opacity-80"
                                )}
                            >
                                <Icon className="mr-3 h-5 w-5" />
                                <span>{item.title}</span>
                            </span>
                        </Link>
                    )
                )
            })}
            <ModeToggle />
            {!user &&
                <div className="mt-24">
                    <NavPromo />
                </div>
            }
        </nav>
    )
}