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 CFade from './CFade';

//component - CoreUI / CAlert

var CAlert = function CAlert(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      custom = props.custom,
      toggle = props.toggle,
      transition = props.transition,
      closeAriaLabel = props.closeAriaLabel,
      closeClassName = props.closeClassName,
      color = props.color,
      fade = props.fade,
      show = props.show,
      iconSlot = props.iconSlot,
      closeProps = props.closeProps,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'custom', 'toggle', 'transition', 'closeAriaLabel', 'closeClassName', 'color', 'fade', 'show', 'iconSlot', 'closeProps']);

  //render

  var classes = mapToCssModules(classNames(className, 'alert', 'alert-' + color, { 'alert-dismissible': toggle }), cssModule);

  var closeClasses = mapToCssModules(classNames('close', closeClassName), cssModule);

  var alertTransition = _extends({}, CFade.defaultProps, transition, {
    baseClass: fade ? transition.baseClass : '',
    timeout: fade ? transition.timeout : 0
  });

  var _useState = useState(true),
      isOpen = _useState[0],
      setIsOpen = _useState[1];

  if (!custom) {
    var userToggle = toggle;
    toggle = function toggle() {
      setIsOpen(!isOpen);
      if (userToggle) userToggle();
    };
    show = isOpen;
  }

  return React.createElement(
    CFade,
    _extends({}, attributes, alertTransition, { tag: Tag, className: classes, 'in': show, role: 'alert' }),
    !custom ? React.createElement(
      'button',
      _extends({ type: 'button', className: closeClasses, 'aria-label': closeAriaLabel, onClick: toggle }, closeProps),
      React.createElement(
        'span',
        { 'aria-hidden': 'true' },
        iconSlot
      )
    ) : null,
    children
  );
};

CAlert.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: tagPropType,
  children: PropTypes.node,
  className: PropTypes.string,
  cssModule: PropTypes.object,
  custom: PropTypes.bool,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  toggle: PropTypes.func,
  closeClassName: PropTypes.string,
  closeProps: PropTypes.object,
  closeAriaLabel: PropTypes.string,
  color: PropTypes.string,
  fade: PropTypes.bool,
  show: PropTypes.bool,
  transition: PropTypes.shape(CFade.propTypes),
  iconSlot: PropTypes.node
} : {};

CAlert.defaultProps = {
  tag: 'div',
  //
  color: 'success',
  show: true,
  closeAriaLabel: 'Close',
  fade: true,
  transition: _extends({}, CFade.defaultProps, {
    unmountOnExit: true
  }),
  iconSlot: React.createElement(
    React.Fragment,
    null,
    '\xD7'
  )
};

//export
export default CAlert;