import { FC, ReactNode } from 'react'
import { Link } from 'react-router-dom'
import { clx } from '../helpers/utils'
import { Pages } from '../config/Pages'
import { useSharedStore } from './store/sharedStore'

type Props = {
    icon: ReactNode,
    title: string,
    isActive: boolean,
    link: string,
    isLogout?: boolean,
    name?: string
}

const SideBarItem: FC<Props> = (props: Props) => {

    const { hasSubMenu, setSubMenuName, setSubtMenu } = useSharedStore()
    const handleLogout = () => {
        localStorage.removeItem(import.meta.env.VITE_TOKEN_NAME)
        window.location.href = Pages.auth.login
    }

    const handleClick = () => {
        if (props.name) {
            setSubMenuName(props.name)
            setSubtMenu(true)
        }
    }

    return (
        <Link onClick={props.isLogout ? handleLogout : handleClick} to={props.link} className={clx(
            'flex text-xs gap-9 mt-4',
            hasSubMenu && 'flex-col',
        )}>
            <div className={clx(
                'w-1 bg-black h-6',
                !props.isActive && 'invisible',
                hasSubMenu && 'hidden',
            )}></div>
            <div className={clx(
                'flex gap-3 items-center',
                hasSubMenu && 'flex-col text-[10px]',
            )}>
                {props.icon}
                <div className={props.isActive ? 'text-black' : ''}>
                    {props.title}
                </div>
            </div>

        </Link>
    )
}

export default SideBarItem