import { FC, ReactNode } from 'react'
import { Link } from 'react-router-dom'
import { clx } from '../helpers/utils'
import { Pages } from '../config/Pages'
import { useLogout } from '../pages/auth/hooks/useAuthData'
import { removeToken } from '../config/func'
import { removeRefreshToken } from '../config/func'

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

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

    const logout = useLogout()

    const handleLogout = async () => {
        await logout.mutateAsync()
        removeToken()
        removeRefreshToken()
        window.location.href = Pages.auth.login
    }

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

        </Link>
    )
}

export default SideBarItem