function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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 _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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import ConfigProvider from '../config-provider';
import { TransitionGroup } from 'react-transition-group';
import ToastItem from './toast-item.component';
import { func } from '../util';

var ToastContainer =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ToastContainer, _React$Component);

  _createClass(ToastContainer, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (state && state.tmpl && props.tmpl !== state.tmpl) {
        return {
          tmpl: props.tmpl
        };
      }

      return null;
    }
  }]);

  function ToastContainer(props, context) {
    var _this;

    _classCallCheck(this, ToastContainer);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ToastContainer).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "findToast", function (key) {
      var toasts = _this.props.toasts;

      if (!Array.isArray(toasts)) {
        return null;
      }

      var toastIds = toasts.map(function (toast) {
        return toast.key;
      });
      var index = toastIds.indexOf(key);
      return index > -1 ? toasts[index] : null;
    });

    _defineProperty(_assertThisInitialized(_this), "handleClose", function (key) {
      var toast = _this.findToast(key);

      if (toast) {
        var clearTimeout = toast.clearTimeout,
            onClose = toast.onClose;
        clearTimeout();

        if (func.isFunction(onClose)) {
          onClose(key);
        }
      }

      _this.props.onClose(key);
    });

    _defineProperty(_assertThisInitialized(_this), "handlePause", function (key) {
      var toast = _this.findToast(key);

      if (toast) {
        var clearTimeout = toast.clearTimeout,
            onPause = toast.onPause;
        clearTimeout();

        if (func.isFunction(onPause)) {
          onPause(key);
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleRemove", function (componentProps) {
      var key = componentProps.key,
          onRemove = componentProps.onRemove;

      _this.props.onClose(key);

      if (func.isFunction(onRemove)) {
        onRemove(key);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleStart", function (key) {
      var toast = _this.findToast(key);

      if (toast) {
        var startTimeout = toast.startTimeout,
            onStart = toast.onStart;
        startTimeout();

        if (func.isFunction(onStart)) {
          onStart(key);
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "renderToast", function (componentProps, toastPrefix) {
      var _cx;

      var clearTimeout = componentProps.clearTimeout,
          className = componentProps.className,
          key = componentProps.key,
          startTimeout = componentProps.startTimeout,
          onClose = componentProps.onClose,
          onPause = componentProps.onPause,
          onStart = componentProps.onStart,
          restProps = _objectWithoutProperties(componentProps, ["clearTimeout", "className", "key", "startTimeout", "onClose", "onPause", "onStart"]);

      var cls = cx((_cx = {}, _defineProperty(_cx, "".concat(toastPrefix, "-container-child"), true), _defineProperty(_cx, className, !!className), _defineProperty(_cx, "".concat(toastPrefix, "-item"), true), _cx));

      var childProps = _objectSpread({}, restProps, {
        className: cls,
        key: key,
        onRemove: _this.handleRemove.bind(_assertThisInitialized(_this), componentProps)
      });

      if (componentProps.closeable) {
        childProps = _objectSpread({}, childProps, {
          onClose: _this.handleClose.bind(_assertThisInitialized(_this), key)
        });
      }

      if (componentProps.pauseable) {
        childProps = _objectSpread({}, childProps, {
          onStart: _this.handleStart.bind(_assertThisInitialized(_this), key),
          onPause: _this.handlePause.bind(_assertThisInitialized(_this), key)
        });
      }

      var Tmpl = _this.state.tmpl;
      return React.createElement(Tmpl, childProps);
    });

    _this.state = {
      tmpl: props.tmpl
    };
    return _this;
  }

  _createClass(ToastContainer, [{
    key: "replaceTemplate",
    value: function replaceTemplate(tmpl) {
      this.setState({
        tmpl: tmpl
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _cx2,
          _this2 = this;

      var _this$props = this.props,
          prefix = _this$props.prefix,
          toasts = _this$props.toasts,
          className = _this$props.className,
          placement = _this$props.placement;
      var toastPrefix = "".concat(prefix, "toast");
      var cls = cx((_cx2 = {}, _defineProperty(_cx2, "".concat(toastPrefix, "-container"), true), _defineProperty(_cx2, "".concat(toastPrefix, "-container-").concat(placement), true), _defineProperty(_cx2, className, !!className), _cx2));
      var toastGroup = toasts.map(function (toast) {
        return _this2.renderToast(toast, toastPrefix);
      });
      return React.createElement(TransitionGroup, {
        component: "div",
        className: cls
      }, toastGroup);
    }
  }]);

  return ToastContainer;
}(React.Component);

_defineProperty(ToastContainer, "propTypes", {
  prefix: PropTypes.string,
  toasts: PropTypes.array,
  placement: PropTypes.string,
  tmpl: PropTypes.any,
  onClose: PropTypes.func,
  className: PropTypes.string
});

_defineProperty(ToastContainer, "defaultProps", {
  prefix: 'next-',
  placement: 'bottomLeft',
  tmpl: ToastItem
});

export default ConfigProvider.config(ToastContainer);