'use strict';

exports.__esModule = true;

var _extends = Object.assign || 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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

var _Slot = require('./Shared/Slot');

var _Slot2 = _interopRequireDefault(_Slot);

var _CToastHeader = require('./CToastHeader');

var _CToastHeader2 = _interopRequireDefault(_CToastHeader);

var _CToastBody = require('./CToastBody');

var _CToastBody2 = _interopRequireDefault(_CToastBody);

var _CButtonClose = require('./CButtonClose');

var _CButtonClose2 = _interopRequireDefault(_CButtonClose);

var _CFade = require('./CFade');

var _CFade2 = _interopRequireDefault(_CFade);

var _CToastModule = require('./CToast.module.css');

var _CToastModule2 = _interopRequireDefault(_CToastModule);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

//component - CoreUI / CToast

var CToast = function CToast(props) {
  var Tag = props.tag,
      className = props.className,
      cssModule = props.cssModule,
      custom = props.custom,
      innerRef = props.innerRef,
      show = props.show,
      header = props.header,
      headerSlot = props.headerSlot,
      autohide = props.autohide,
      closeButton = props.closeButton,
      fade = props.fade,
      transition = props.transition,
      attributes = _objectWithoutProperties(props, ['tag', 'className', 'cssModule', 'custom', 'innerRef', 'show', 'header', 'headerSlot', 'autohide', 'closeButton', 'fade', 'transition']);

  var _useState = (0, _react.useState)(show),
      shown = _useState[0],
      setShown = _useState[1];

  var transitionPar = _extends({}, _CFade2.default.defaultProps, transition, {
    baseClass: fade ? transition.baseClass : '',
    timeout: fade ? transition.timeout : 0
  });

  var timeout = void 0;

  var setAutohide = function setAutohide() {
    timeout = setTimeout(function () {
      setShown(false);
    }, autohide);
  };

  var onMouseOver = function onMouseOver() {
    clearTimeout(timeout);
  };
  var onMouseOut = function onMouseOut() {
    if (autohide) setAutohide();
  };
  var onClick = function onClick() {
    setShown(false);
  };

  if (autohide) {
    setAutohide();
  }

  // render

  var classes = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, 'toast', 'mytoast'), Object.assign(_CToastModule2.default, cssModule));

  if (!shown) return null;

  if (!custom) return _react2.default.createElement(
    _CFade2.default,
    _extends({}, attributes, transitionPar, {
      tag: Tag,
      className: classes,
      'in': true,
      role: 'alert',
      innerRef: innerRef
    }),
    _react2.default.createElement(
      Tag,
      _extends({}, attributes, {
        'aria-live': 'assertive',
        'aria-atomic': 'true',
        onMouseOver: onMouseOver,
        onMouseOut: onMouseOut,
        className: classes
      }),
      _react2.default.createElement(
        _CToastHeader2.default,
        null,
        headerSlot || header ? _react2.default.createElement(
          _Slot2.default,
          { content: headerSlot },
          _react2.default.createElement(
            'strong',
            { className: 'mr-auto' },
            header
          )
        ) : '',
        closeButton ? _react2.default.createElement(_CButtonClose2.default, { className: 'ml-2 mb-1', onClick: onClick }) : ''
      ),
      _react2.default.createElement(
        _CToastBody2.default,
        null,
        attributes.children
      )
    )
  );

  return _react2.default.createElement(Tag, _extends({}, attributes, { onMouseOver: onMouseOver, onMouseOut: onMouseOut, className: classes, ref: innerRef }));
};

CToast.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _helper.tagPropType,
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  custom: _propTypes2.default.bool,
  //
  headerSlot: _propTypes2.default.node,
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string, _propTypes2.default.object]),
  show: _propTypes2.default.bool,
  header: _propTypes2.default.string,
  autohide: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.bool]),
  closeButton: _propTypes2.default.bool,
  fade: _propTypes2.default.bool,
  transition: _propTypes2.default.shape(_CFade2.default.propTypes),
  role: _propTypes2.default.string
} : {};

CToast.defaultProps = {
  tag: 'div',
  autohide: 1500,
  closeButton: true,
  fade: true,
  transition: _extends({}, _CFade2.default.defaultProps, {
    unmountOnExit: true
  }),
  role: 'alert'
};

exports.default = CToast;
module.exports = exports['default'];