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; };

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; }

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { tagPropType, mapToCssModules } from './Shared/helper.js';
import Slot from './Shared/Slot';
import CToastHeader from './CToastHeader';
import CToastBody from './CToastBody';
import CButtonClose from './CButtonClose';
import CFade from './CFade';
import style from './CToast.module.css';

//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 = useState(show),
      shown = _useState[0],
      setShown = _useState[1];

  var transitionPar = _extends({}, CFade.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 = mapToCssModules(classNames(className, 'toast', 'mytoast'), Object.assign(style, cssModule));

  if (!shown) return null;

  if (!custom) return React.createElement(
    CFade,
    _extends({}, attributes, transitionPar, {
      tag: Tag,
      className: classes,
      'in': true,
      role: 'alert',
      innerRef: innerRef
    }),
    React.createElement(
      Tag,
      _extends({}, attributes, {
        'aria-live': 'assertive',
        'aria-atomic': 'true',
        onMouseOver: onMouseOver,
        onMouseOut: onMouseOut,
        className: classes
      }),
      React.createElement(
        CToastHeader,
        null,
        headerSlot || header ? React.createElement(
          Slot,
          { content: headerSlot },
          React.createElement(
            'strong',
            { className: 'mr-auto' },
            header
          )
        ) : '',
        closeButton ? React.createElement(CButtonClose, { className: 'ml-2 mb-1', onClick: onClick }) : ''
      ),
      React.createElement(
        CToastBody,
        null,
        attributes.children
      )
    )
  );

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

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

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

export default CToast;