'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var index$1 = require('../../Toast/index.js');
var ToastCompact = require('../../Toast/ToastCompact.js');
var constants = require('../constants.js');
var styles = require('../styles.js');
var index = require('../../../../utils/index.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function MessageProviderToast(props) {
    const { message, toastType, action, variant, autoHideDuration, onClose, icon, primaryAction, dismissAction, position, } = props;
    const timer = React.useRef(autoHideDuration);
    const [entering, setEntering] = React.useState(true);
    React.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 = index.getId(props['data-testid']);
    const getTrackId = index.getId(props['data-trackid']);
    return (React__default['default'].createElement(styles.AnimatedToastMessage, { entering: entering, position: position, onAnimationEnd: animationEnd }, toastType === constants.TOAST_TYPE_DEFAULT ? (React__default['default'].createElement(index$1.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['default'].createElement(ToastCompact.ToastCompact, { message: message, variant: variant, primaryAction: getPrimaryAction(primaryAction), "data-testid": getTestId(), "data-trackid": getTrackId(), "aria-label": props['aria-label'], "aria-checked": props['aria-checked'] }))));
}

exports.MessageProviderToast = MessageProviderToast;
