import { objectSpread2 as _objectSpread2, slicedToArray as _slicedToArray, toConsumableArray as _toConsumableArray, createForOfIteratorHelper as _createForOfIteratorHelper } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { useCallback, useState, useRef, useEffect } from 'react';
import { useToastContext } from './ToastContext.esm.js';
import { DEFAULT_TONE, DEFAULT_TIMEOUT } from './constants.esm.js';

var useShowToast = function useShowToast() {
  var _useToastContext = useToastContext(),
      showToast = _useToastContext.showToast;

  return showToast;
};
var useShowSuccessToast = function useShowSuccessToast() {
  var showToast = useShowToast();
  return useCallback(function (message) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    showToast(message, _objectSpread2({}, options, {
      tone: "SUCCESS"
    }));
  }, [showToast]);
};
var useToastActions = function useToastActions() {
  var _React$useState = useState([]),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      toasts = _React$useState2[0],
      setToasts = _React$useState2[1];

  var timeoutsRef = useRef(new Map());
  var removeToast = useCallback(function (toastId) {
    setToasts(function (prevToasts) {
      return prevToasts.filter(function (_ref) {
        var id = _ref.id;
        return id !== toastId;
      });
    });
    window.clearTimeout(timeoutsRef.current.get(toastId));
    timeoutsRef.current["delete"](toastId);
  }, []);
  var showToast = useCallback(function (message) {
    var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
        _ref2$tone = _ref2.tone,
        tone = _ref2$tone === void 0 ? DEFAULT_TONE : _ref2$tone,
        _ref2$timeout = _ref2.timeout,
        timeout = _ref2$timeout === void 0 ? DEFAULT_TIMEOUT : _ref2$timeout;

    var toastId = Symbol("toast");
    setToasts(function (prevToasts) {
      return [].concat(_toConsumableArray(prevToasts), [{
        id: toastId,
        message: message,
        tone: tone
      }]);
    });

    if (timeout > 0) {
      var timeOutId = window.setTimeout(function () {
        removeToast(toastId);
      }, timeout);
      timeoutsRef.current.set(toastId, timeOutId);
    }
  }, []);
  useEffect(function () {
    // Clear all timeouts
    return function () {
      var _iterator = _createForOfIteratorHelper(timeoutsRef.current.values()),
          _step;

      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var timeout = _step.value;
          window.clearTimeout(timeout);
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    };
  }, []);
  return {
    toasts: toasts,
    showToast: showToast,
    removeToast: removeToast
  };
};

export { useShowSuccessToast, useShowToast, useToastActions };
//# sourceMappingURL=hooks.esm.js.map
