import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import ModalConfrim from '@/components/ModalConfrim'
import { useDeleteAddress } from '../hooks/useAddressData'
import { AddressData } from '../types/Types'
import { toast } from '@/components/Toast'
import { ErrorType } from '@/helpers/types'

interface AddressDeleteModalProps {
    isOpen: boolean
    addressToDelete: AddressData | null
    onClose: () => void
    onSuccess: () => void
}

const AddressDeleteModal: FC<AddressDeleteModalProps> = ({
    isOpen,
    addressToDelete,
    onClose,
    onSuccess
}) => {
    const { t } = useTranslation()
    const { mutate: deleteAddress, isPending } = useDeleteAddress()

    const handleConfirmDelete = () => {
        if (addressToDelete) {
            deleteAddress(addressToDelete.id, {
                onSuccess: (data) => {
                    toast(data?.data?.message || 'آدرس با موفقیت حذف شد', 'success')
                    onSuccess()
                    onClose()
                },
                onError: (error: ErrorType) => {
                    toast(error.response?.data?.error?.message[0] || 'خطا در حذف', 'error')
                    onClose()
                }
            })
        }
    }

    return (
        <ModalConfrim
            isOpen={isOpen}
            close={onClose}
            onConfrim={handleConfirmDelete}
            title={t('confrim.delete_address_title')}
            label={
                addressToDelete
                    ? t('confrim.delete_address_message').replace('{email}', addressToDelete.emailAddress)
                    : ''
            }
            isLoading={isPending}
        />
    )
}

export default AddressDeleteModal 