'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 _CFade = require('./CFade');

var _CFade2 = _interopRequireDefault(_CFade);

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 / 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 = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, 'alert', 'alert-' + color, { 'alert-dismissible': toggle }), cssModule);

  var closeClasses = (0, _helper.mapToCssModules)((0, _classnames2.default)('close', closeClassName), cssModule);

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

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

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

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

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

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

//export
exports.default = CAlert;
module.exports = exports['default'];