import Swal from 'sweetalert2'
import withReactContent from 'sweetalert2-react-content'

const MySwal = withReactContent(Swal)

const toastFn = (title: string, icon: 'success' | 'error' | 'warning' | 'info' = 'success', text?: string) => {
    const Toast = MySwal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true,
        didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
        }
    })

    Toast.fire({
        icon,
        title,
        text
    })
}

type ToastFunction = {
    (title: string, icon?: 'success' | 'error' | 'warning' | 'info', text?: string): void;
    success: (title: string, text?: string) => void;
    error: (title: string, text?: string) => void;
    warning: (title: string, text?: string) => void;
    info: (title: string, text?: string) => void;
}

export const showToast = toastFn as ToastFunction

showToast.success = (title: string, text?: string) => toastFn(title, 'success', text)
showToast.error = (title: string, text?: string) => toastFn(title, 'error', text)
showToast.warning = (title: string, text?: string) => toastFn(title, 'warning', text)
showToast.info = (title: string, text?: string) => toastFn(title, 'info', text)

export const showConfirm = async (title: string, text: string, confirmButtonText = 'Evet', cancelButtonText = 'İptal'): Promise<boolean> => {
    const result = await MySwal.fire({
        title,
        text,
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText,
        cancelButtonText
    })

    return result.isConfirmed
}
