import { Barcode, Cylinder, Package, Truck } from "lucide-react";
import {
    SidebarGroup,
    SidebarGroupContent,
    SidebarGroupLabel,
    SidebarMenu,
    SidebarMenuButton,
    SidebarMenuItem,
} from "../../ui/sidebar";
import { hasPageAccess } from "@/utils/userAccess";
import { User } from "@/types/users";
import { useSubModuleStore } from "@/lib/store/useSubModuleStore";

const iconMap: any = {
    Package: Package,
    Truck: Truck,
    Cylinder: Cylinder,
    Barcode: Barcode,
};

export function LogisticsSideBar({
    user,
    moduleID,
}: {
    user: User | null;
    moduleID: string;
}) {
    const { subModules } = useSubModuleStore();

    const items = subModules.filter((m) => m.moduleName === "logistics");

    return (
        <SidebarGroup>
            <SidebarGroupLabel>Logistics</SidebarGroupLabel>
            <SidebarGroupContent>
                <SidebarMenu>
                    {items.map((item) => {
                        const Icon = iconMap[item.icon];
                        return (
                            <SidebarMenuItem key={item.submodule_id}>
                                <>
                                    {hasPageAccess(
                                        user,
                                        item.roles,
                                        moduleID
                                    ) &&
                                        item.active && (
                                            <SidebarMenuButton asChild>
                                                <a href={item.link}>
                                                    <Icon />
                                                    <span>{item.name}</span>
                                                </a>
                                            </SidebarMenuButton>
                                        )}
                                </>
                            </SidebarMenuItem>
                        );
                    })}
                </SidebarMenu>
            </SidebarGroupContent>
        </SidebarGroup>
    );
}
