'use strict';

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

var React = require('react');
var classNames = require('clsx');
var reactTransitionGroup = require('react-transition-group');
var Portal = require('@prom-ui/core/Portal');

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 React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var classNames__default = /*#__PURE__*/_interopDefaultCompat(classNames);

function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly && (symbols = symbols.filter(function (sym) {
      return Object.getOwnPropertyDescriptor(object, sym).enumerable;
    })), keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread2(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
      _defineProperty(target, key, source[key]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
      Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
    });
  }
  return target;
}
function _defineProperty(obj, key, value) {
  key = _toPropertyKey(key);
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _toPrimitive(input, hint) {
  if (typeof input !== "object" || input === null) return input;
  var prim = input[Symbol.toPrimitive];
  if (prim !== undefined) {
    var res = prim.call(input, hint || "default");
    if (typeof res !== "object") return res;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
  var key = _toPrimitive(arg, "string");
  return typeof key === "symbol" ? key : String(key);
}

var css = {"root":"SnackBar__root___L79vP","content":"SnackBar__content___iYzO5","root_position_top-center":"SnackBar__root_position_top-center___dpKTy","root_position_bottom-center":"SnackBar__root_position_bottom-center___i5-KH","root_position_top-left":"SnackBar__root_position_top-left___IWQhD","root_position_top-right":"SnackBar__root_position_top-right___eg0lI","root_position_bottom-left":"SnackBar__root_position_bottom-left___kn1Ep","root_position_bottom-right":"SnackBar__root_position_bottom-right___pH0zF","enterActive":"SnackBar__enterActive___mFMbv","exitActive":"SnackBar__exitActive___Lga9O","enter":"SnackBar__enter___a7Z-S","exit":"SnackBar__exit___4JiHm"};

var _excluded = ["isOpen", "onClose", "onOpened", "onClosed", "children", "className", "style", "position", "zIndex", "padding", "autoCloseDelay", "isRenderPortal"];
/**
 * Компонент для кратких уведомлений о процессах
 */
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(_ref, _excluded);
  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() {
    destroyAutoClose();
    if (autoCloseDelay > 0) {
      refTimer.current = setTimeout(function () {
        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, {
    "in": isOpen,
    timeout: 125,
    classNames: css,
    unmountOnExit: true,
    onEntered: handleOnEntered,
    onExit: handleOnExit,
    onExited: onClosed
  }, /*#__PURE__*/React__namespace.createElement(Portal.Portal, {
    isActive: isRenderPortal
  }, /*#__PURE__*/React__namespace.createElement("div", _extends({
    className: classNames__default.default(css.root, positionClassName),
    ref: ref,
    style: {
      zIndex: zIndex
    },
    role: "presentation",
    "data-testid": "snackbar",
    "data-qaid": "snackbar"
  }, props), /*#__PURE__*/React__namespace.createElement("div", {
    className: classNames__default.default(css.content, className),
    style: _objectSpread2({
      padding: padding
    }, style),
    role: "alert",
    onMouseEnter: destroyAutoClose,
    onMouseLeave: createAutoClose
  }, children))));
});
SnackBar.displayName = 'SnackBar';

exports.SnackBar = SnackBar;

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