import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/web.timers.js";
import { useEffect, useRef, useState } from 'react';
import './index.css';
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var NotificationToast = function NotificationToast(_ref) {
  var onClose = _ref.onClose,
    data = _ref.data,
    icon = _ref.icon,
    _ref$autoClose = _ref.autoClose,
    autoClose = _ref$autoClose === void 0 ? true : _ref$autoClose,
    iconColors = _ref.iconColors;
  var _useState = useState(6),
    _useState2 = _slicedToArray(_useState, 2),
    time = _useState2[0],
    setTime = _useState2[1];
  var timer = useRef();
  var handleClose = function handleClose() {
    onClose && onClose();
  };
  useEffect(function () {
    if (!autoClose) {
      return;
    }
    timer.current = setInterval(function () {
      setTime(function (prevState) {
        if (prevState === 1) {
          handleClose();
          clearInterval(timer.current);
        }
        return prevState - 1;
      });
    }, 1000);
    return function () {
      clearInterval(timer.current);
    };
  }, [autoClose]);
  return /*#__PURE__*/_jsxs("div", {
    className: "frc-notification-toast",
    children: [icon && /*#__PURE__*/_jsx(FcrIcon, {
      type: icon,
      size: 28,
      colors: iconColors,
      style: {
        marginRight: '8px'
      }
    }), /*#__PURE__*/_jsx("span", {
      children: data
    }), autoClose && /*#__PURE__*/_jsx("span", {
      className: "frc-notification-toast-countdown",
      children: "(".concat(time, "s)")
    }), !autoClose && /*#__PURE__*/_jsx("span", {
      className: "fcr-notification-toast-close-icon",
      onClick: handleClose,
      children: /*#__PURE__*/_jsx(FcrIcon, {
        type: FcrIconType.FCR_CLOSE,
        colors: {
          iconPrimary: 'var(--fcr_ui_scene_icontext1)'
        },
        size: 20
      })
    })]
  });
};
export default NotificationToast;