'use strict';

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

var _extends = require('@babel/runtime/helpers/extends');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var classNames = require('clsx');
var reactTransitionGroup = require('react-transition-group');
var Portal = require('@prom-ui/core/Portal');
var useForwardRef = require('@prom-ui/core/useForwardRef');

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

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var _extends__default = /*#__PURE__*/_interopDefaultCompat(_extends);
var _defineProperty__default = /*#__PURE__*/_interopDefaultCompat(_defineProperty);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultCompat(_objectWithoutProperties);
var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var classNames__default = /*#__PURE__*/_interopDefaultCompat(classNames);

var css = {"root":"SnackBar__root___KYIW0","content":"SnackBar__content___F8zNR","root_position_top-center":"SnackBar__root_position_top-center___I5Lmf","root_position_bottom-center":"SnackBar__root_position_bottom-center___QB4Ps","root_position_top-left":"SnackBar__root_position_top-left___QJETh","root_position_top-right":"SnackBar__root_position_top-right___vHlAp","root_position_bottom-left":"SnackBar__root_position_bottom-left___cU3cS","root_position_bottom-right":"SnackBar__root_position_bottom-right___dlDmS","enterActive":"SnackBar__enterActive___mLVVo","exitActive":"SnackBar__exitActive___5JFKS","enter":"SnackBar__enter___Rlx0S","exit":"SnackBar__exit___B32cZ"};

var _excluded = ["isOpen", "onClose", "onOpened", "onClosed", "children", "className", "style", "position", "zIndex", "padding", "autoCloseDelay", "isRenderPortal"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
 * Компонент для кратких уведомлений о процессах
 */
var SnackBar = /*#__PURE__*/React__namespace.forwardRef(function (_ref, ref) {
  var isOpen = _ref.isOpen,
    onClose = _ref.onClose,
    onOpened = _ref.onOpened,
    onClosed = _ref.onClosed,
    children = _ref.children,
    className = _ref.className,
    style = _ref.style,
    _ref$position = _ref.position,
    position = _ref$position === void 0 ? 'bottom-center' : _ref$position,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 5000 : _ref$zIndex,
    _ref$padding = _ref.padding,
    padding = _ref$padding === void 0 ? '12px 16px' : _ref$padding,
    _ref$autoCloseDelay = _ref.autoCloseDelay,
    autoCloseDelay = _ref$autoCloseDelay === void 0 ? 2000 : _ref$autoCloseDelay,
    _ref$isRenderPortal = _ref.isRenderPortal,
    isRenderPortal = _ref$isRenderPortal === void 0 ? true : _ref$isRenderPortal,
    props = _objectWithoutProperties__default.default(_ref, _excluded);
  var nodeRef = useForwardRef.useForwardRef(ref, null);
  var isAutoClose = React__namespace.useRef(false);
  var refTimer = React__namespace.useRef(undefined);
  var positionClassName = position ? css["root_position_".concat(position)] : undefined;
  var destroyAutoClose = function destroyAutoClose() {
    clearTimeout(refTimer.current);
  };
  var createAutoClose = function createAutoClose() {
    isAutoClose.current = false;
    destroyAutoClose();
    if (autoCloseDelay > 0) {
      refTimer.current = setTimeout(function () {
        isAutoClose.current = true;
        if (typeof onClose === 'function') {
          onClose();
        }
      }, autoCloseDelay);
    }
  };
  var handleOnExit = function handleOnExit() {
    destroyAutoClose();
  };
  var handleOnEntered = function handleOnEntered() {
    if (typeof onOpened === 'function') {
      onOpened();
    }
    createAutoClose();
  };
  React__namespace.useEffect(function () {
    if (isOpen) {
      handleOnEntered();
    }
    return function () {
      handleOnExit();
    };
  }, []);
  return /*#__PURE__*/React__namespace.createElement(reactTransitionGroup.CSSTransition, {
    nodeRef: nodeRef,
    "in": isOpen,
    timeout: 125,
    classNames: css,
    unmountOnExit: true,
    onEntered: handleOnEntered,
    onExit: handleOnExit,
    onExited: function onExited() {
      if (typeof onClosed === 'function') {
        onClosed({
          isAutoClose: isAutoClose.current
        });
      }
    }
  }, /*#__PURE__*/React__namespace.createElement(Portal.Portal, {
    isActive: isRenderPortal
  }, /*#__PURE__*/React__namespace.createElement("div", _extends__default.default({
    className: classNames__default.default(css.root, positionClassName),
    ref: nodeRef,
    style: {
      zIndex: zIndex
    },
    role: "presentation",
    "data-testid": "snackbar",
    "data-qaid": "snackbar",
    "data-pds-color-scheme": "light"
  }, props), /*#__PURE__*/React__namespace.createElement("div", {
    className: classNames__default.default(css.content, className),
    style: _objectSpread({
      padding: padding
    }, style),
    role: "alert",
    onMouseEnter: destroyAutoClose,
    onMouseLeave: createAutoClose
  }, children))));
});
SnackBar.displayName = 'SnackBar';

exports.SnackBar = SnackBar;

require('./style.development.v2.css');
