import { jsx } from 'react/jsx-runtime';
import { useState, useRef } from 'react';
import Toaster from '../Toast/Toast.js';

let openToast;
const Toastify = () => {
    const [toastProps, setToastProps] = useState({
        isOpen: false,
        variant: 'info',
        toastTitle: '',
        toastMessage: '',
        toastId: '',
    });
    const timeoutRef = useRef(null);
    const showTimeoutRef = useRef(null);
    const isManuallyClosed = useRef(false);
    openToast = (variant, arg1, arg2) => {
        // Immediately reset manual close flag
        isManuallyClosed.current = false;
        // Clear existing timeouts
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
            timeoutRef.current = null;
        }
        if (showTimeoutRef.current) {
            clearTimeout(showTimeoutRef.current);
            showTimeoutRef.current = null;
        }
        const defaultTitles = {
            success: 'Success',
            danger: 'Error',
            warning: 'Warning',
            info: 'Info',
            alert: 'Alert',
        };
        let title = defaultTitles[variant];
        let message = '';
        const formatMessage = (input) => {
            if (input && typeof input === 'object') {
                return JSON.stringify(input, null, 2);
            }
            else if (typeof input === 'function') {
                return input.toString();
            }
            return String(input);
        };
        // If arg2 exists, use it as the message
        if (arg2 !== undefined) {
            title = formatMessage(arg1);
            message = formatMessage(arg2);
        }
        else {
            message = formatMessage(arg1);
        }
        // Close the existing toast if open, and then immediately show the new one
        setToastProps((prev) => ({ ...prev, isOpen: false }));
        // Delay showing the new toast, if not cancelled
        showTimeoutRef.current = setTimeout(() => {
            //  check AGAIN here
            if (isManuallyClosed.current)
                return;
            setToastProps({
                isOpen: true,
                variant,
                toastTitle: title,
                toastMessage: message,
            });
            timeoutRef.current = setTimeout(() => {
                if (!isManuallyClosed.current) {
                    setToastProps((prev) => ({ ...prev, isOpen: false }));
                }
            }, 3000);
        }, 10);
    };
    const handleCancelClick = () => {
        //  Mark it as manually closed BEFORE clearing show timeout
        isManuallyClosed.current = true;
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
            timeoutRef.current = null;
        }
        if (showTimeoutRef.current) {
            clearTimeout(showTimeoutRef.current);
            showTimeoutRef.current = null;
        }
        setToastProps((prev) => ({ ...prev, isOpen: false }));
    };
    return (jsx(Toaster, { isOpen: toastProps.isOpen, variant: toastProps.variant, toastTitle: toastProps.toastTitle, toastMessage: toastProps.toastMessage, zIndex: 1000000000, onCancelClick: handleCancelClick }));
};
const toast = {
    success: (arg1, arg2) => openToast('success', arg1, arg2),
    error: (arg1, arg2) => openToast('danger', arg1, arg2),
    warning: (arg1, arg2) => openToast('warning', arg1, arg2),
    info: (arg1, arg2) => openToast('info', arg1, arg2),
    alert: (arg1, arg2) => openToast('alert', arg1, arg2),
};

export { Toastify, Toastify as default, toast };
//# sourceMappingURL=Toastify.js.map
