import React__default, { useRef, useState, useEffect } from 'react';
import { Toast } from '../../Toast/index.js';
import { ToastCompact } from '../../Toast/ToastCompact.js';
import { TOAST_TYPE_DEFAULT } from '../constants.js';
import { AnimatedToastMessage } from '../styles.js';
import { getId } from '../../../../utils/index.js';

function MessageProviderToast(props) {
    const { message, toastType, action, variant, autoHideDuration, onClose, icon, primaryAction, dismissAction, position, } = props;
    const timer = useRef(autoHideDuration);
    const [entering, setEntering] = useState(true);
    useEffect(() => {
        if (autoHideDuration) {
            timer.current = window.setTimeout(hideMessage, autoHideDuration);
        }
        return () => {
            if (timer.current) {
                clearTimeout(timer.current);
            }
        };
    }, [autoHideDuration]);
    const hideMessage = () => {
        setEntering(false);
    };
    const animationEnd = () => {
        if (!entering) {
            onClose();
            setEntering(true);
        }
    };
    const getClickHandle = (action) => () => {
        if (action) {
            action.onClick();
        }
        hideMessage();
    };
    const getToastAction = (defaultText, action) => {
        return {
            text: action ? action.text : defaultText,
            onClick: getClickHandle(action),
        };
    };
    const getPrimaryAction = (action) => getToastAction('OK', action);
    const getDismissAction = (action) => getToastAction('Dismiss', action);
    const getTestId = getId(props['data-testid']);
    const getTrackId = getId(props['data-trackid']);
    return (React__default.createElement(AnimatedToastMessage, { entering: entering, position: position, onAnimationEnd: animationEnd }, toastType === TOAST_TYPE_DEFAULT ? (React__default.createElement(Toast, { message: message, action: action, variant: variant, icon: icon, primaryAction: getPrimaryAction(primaryAction), dismissAction: getDismissAction(dismissAction), "data-testid": getTestId(), "data-trackid": getTrackId(), "aria-label": props['aria-label'], "aria-checked": props['aria-checked'] })) : (React__default.createElement(ToastCompact, { message: message, variant: variant, primaryAction: getPrimaryAction(primaryAction), "data-testid": getTestId(), "data-trackid": getTrackId(), "aria-label": props['aria-label'], "aria-checked": props['aria-checked'] }))));
}

export { MessageProviderToast };
