import { useMoveToArchive, useUnArchiveMessage } from '../hooks/useEmailData'
import { ArchiveTick } from 'iconsax-react'
import { FC, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'

const MessageArchive: FC<{ isActiveArchive: boolean }> = ({ isActiveArchive }) => {

    const navigate = useNavigate()
    const [isArchive, setIsArchive] = useState<boolean>(isActiveArchive)
    const { id, mailbox } = useParams<{ id: string, mailbox: string }>()
    const { mutate: moveToArchive } = useMoveToArchive()
    const { mutate: unArchiveMessage } = useUnArchiveMessage()
    return (
        <ArchiveTick onClick={() => {
            if (isArchive) {
                unArchiveMessage({ messageId: id || '', mailbox: mailbox || '' }, {
                    onSuccess: () => {
                        navigate(-1)
                    }
                })
            } else {
                moveToArchive({ messageId: id || '', mailbox: mailbox || '' }, {
                    onSuccess: () => {
                        navigate(-1)
                    }
                })
            }
            setIsArchive(!isArchive)
        }} className='lg:size-[18px] size-[20px]' variant={isArchive ? 'Bold' : 'Outline'} color='black' />
    )
}

export default MessageArchive