import {NextLinkComposed} from "@/core/components/LinkRouting";
import ExpandLess from "@mui/icons-material/ExpandLess";
import ExpandMore from "@mui/icons-material/ExpandMore";
import {ListItem, ListItemButton, ListItemIcon, ListItemText,} from "@mui/material";
import {useTranslations} from "next-intl";
import {Fragment} from "react";
import SidebarListSubItem from "./SidebarListSubItem";

const SidebarListItem = ({item, dispatch, handleDrawerToggle}) => {
    const t = useTranslations();

    return (
        <Fragment key={item.key}>
            <ListItem disablePadding sx={{display: "block"}}>
                <ListItemButton
                    selected={item.selected}
                    {...(item.type == "page" && {
                        component: NextLinkComposed,
                        to: {
                            pathname: item.route,
                        },
                    })}
                    onClick={() => {
                        if (item.type == "menu") {
                            dispatch({type: "COLLAPSE_MENU", key: item.key});
                        }
                        handleDrawerToggle();
                    }}
                    sx={{
                        minHeight: 48,
                    }}
                >
                    <ListItemIcon
                        sx={{
                            minWidth: 0,
                            justifyContent: "center",
                            color: "primary.main",
                            pr: 2,
                        }}
                    >
                        {item.icon}
                    </ListItemIcon>
                    <ListItemText primary={t(item.key)}/>

                    {item.type == "menu" &&
                        (item.showSubItem ? <ExpandLess/> : <ExpandMore/>)}
                </ListItemButton>
            </ListItem>
            {item.subItem && (
                <SidebarListSubItem
                    item={item}
                    handleDrawerToggle={handleDrawerToggle}
                />
            )}
        </Fragment>
    );
};

export default SidebarListItem;
