import { ExpandLess, ExpandMore } from "@mui/icons-material";
import { Box, Collapse, List, ListItem, ListItemButton, ListItemIcon, ListItemText, Stack } from "@mui/material";
import Link from "next/link";
import SidebarBadge from "./SidebarBadge";

const SidebarSubitems = ({ subitem, dispatch }) => {
    return (
        <>
            <ListItem disablePadding divider>
                <ListItemButton
                    disableGutters
                    sx={{ p: 0.5, pl: 1 }}
                    selected={subitem.selected}
                    component={subitem.type === "page" ? Link : null}
                    href={subitem.type === "page" ? subitem.route : null}
                    onClick={() => {
                        dispatch({ type: "COLLAPSE_SUB_ITEMS", id: subitem.id });
                    }}
                >
                    <ListItemIcon
                        sx={{
                            minWidth: 0,
                            justifyContent: "center",
                            width: 40,
                            height: 24,
                        }}
                    >
                        {subitem.icon}
                    </ListItemIcon>
                    <ListItemText primary={subitem.label} />
                    {subitem.badges && (
                        <Stack direction={"row"} sx={{ px: 0.5 }} spacing={0.5}>
                            {subitem.badges.map((badge, i) => (
                                <SidebarBadge key={i} badge={badge} />
                            ))}
                        </Stack>
                    )}
                    {subitem.hasSubitems ? (
                        subitem.showSubitems ? (
                            <ExpandLess />
                        ) : (
                            <ExpandMore />
                        )
                    ) : (
                        <Box sx={{ width: 29 }} />
                    )}
                </ListItemButton>
            </ListItem>
            <Collapse in={subitem.showSubitems} timeout="auto" mountOnEnter={true} unmountOnExit={true}>
                {subitem.hasSubitems ? (
                    <List
                        disablePadding
                        dense
                        sx={{
                            background: "#00000008",
                        }}
                    >
                        {subitem.Subitems.map((subSubitem, index) => {
                            return <SidebarSubitems key={index} dispatch={dispatch} subitem={subSubitem} />;
                        })}
                    </List>
                ) : null}
            </Collapse>
        </>
    );
};
export default SidebarSubitems;
