'use strict';

exports.__esModule = true;
exports.Notification = undefined;

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 _recompose = require('recompose');

var _grommet = require('grommet');

var _utils = require('grommet/utils');

var _grommetIcons = require('grommet-icons');

var _hocs = require('grommet/components/hocs');

var _moment = require('../../utils/moment');

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var StatusIcons = {
  ok: _grommetIcons.StatusGood,
  warning: _grommetIcons.StatusWarning,
  error: _grommetIcons.StatusCritical,
  unknown: _grommetIcons.StatusUnknown,
  disabled: _grommetIcons.StatusDisabled,
  info: _grommetIcons.StatusInfo
};

var SIZE_LEVELS = {
  small: {
    level: 4,
    size: 'xsmall',
    meterSize: 'xsmall'
  },
  medium: {
    level: 3,
    size: 'small',
    meterSize: 'small'
  },
  large: {
    level: 1,
    size: 'medium',
    meterSize: 'large'
  }
};

var Notification = function (_Component) {
  _inherits(Notification, _Component);

  function Notification() {
    _classCallCheck(this, Notification);

    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }

  Notification.prototype.render = function render() {
    var _props = this.props,
        status = _props.status,
        message = _props.message,
        locale = _props.locale,
        closer = _props.closer,
        margin = _props.margin,
        reverse = _props.reverse,
        a11yTitle = _props.a11yTitle,
        background = _props.background,
        border = _props.border,
        timestamp = _props.timestamp,
        theme = _props.theme,
        icon = _props.icon,
        strong = _props.strong,
        round = _props.round,
        pad = _props.pad,
        size = _props.size,
        state = _props.state,
        onClose = _props.onClose,
        percentComplete = _props.percentComplete,
        rest = _objectWithoutProperties(_props, ['status', 'message', 'locale', 'closer', 'margin', 'reverse', 'a11yTitle', 'background', 'border', 'timestamp', 'theme', 'icon', 'strong', 'round', 'pad', 'size', 'state', 'onClose', 'percentComplete']);

    var sizeLevel = SIZE_LEVELS[size];
    var grommet = this.context.grommet;

    var Border = (0, _utils.deepMerge)(theme.notification ? theme.notification.border : {}, border);
    var Round = round || (theme.notification && theme.notification.border ? theme.notification.border.round : undefined);
    var closeBtn = void 0;
    if (onClose) {
      var closeIcon = closer || _react2.default.createElement(_grommetIcons.Close, { size: sizeLevel.size });
      closeBtn = _react2.default.createElement(
        _grommet.Box,
        { pad: pad },
        _react2.default.createElement(
          _grommet.Button,
          {
            a11yTitle: a11yTitle,
            onClick: onClose
          },
          closeIcon
        )
      );
    }
    var heading = message;
    if (strong) {
      heading = _react2.default.createElement(
        'strong',
        null,
        heading
      );
    }
    var statusIcon = void 0;
    if (icon) {
      var StatusIcon = StatusIcons[status];
      statusIcon = _react2.default.isValidElement(icon) ? icon : _react2.default.createElement(StatusIcon, { size: sizeLevel.size });
    }
    var progress = void 0;
    if (percentComplete || percentComplete === 0) {
      progress = _react2.default.createElement(
        _grommet.Box,
        { direction: 'row', align: 'center', pad: { between: 'medium' }, style: { whiteSpace: 'nowrap' } },
        _react2.default.createElement(_grommet.Meter, {
          size: sizeLevel.meterSize,
          thickness: 'small',
          values: [{
            label: percentComplete.toFixed(0),
            value: percentComplete,
            color: 'accent-1'
          }]
        }),
        _react2.default.createElement(
          _grommet.Text,
          { size: 'small' },
          ' ' + percentComplete + ' %'
        )
      );
    }
    var timeStamp = void 0;
    if (timestamp) {
      timeStamp = (0, _moment.longDate)(timestamp, locale);
    }
    var first = void 0;
    var second = void 0;
    if (reverse) {
      first = heading;
      second = statusIcon;
    } else {
      first = statusIcon;
      second = heading;
    }
    var content = _react2.default.createElement(
      _grommet.Box,
      { flex: 'grow', pad: pad },
      _react2.default.createElement(
        _grommet.Heading,
        { level: sizeLevel.level, margin: margin, style: { maxWidth: '100%' } },
        _react2.default.createElement(
          _grommet.Box,
          { direction: 'row', align: 'center', gap: 'small' },
          first,
          second
        )
      ),
      _react2.default.createElement(
        _grommet.Text,
        { size: sizeLevel.size },
        state
      ),
      _react2.default.createElement(
        _grommet.Text,
        { size: sizeLevel.size },
        timeStamp
      ),
      progress
    );
    if (reverse) {
      first = closeBtn;
      second = content;
    } else {
      first = content;
      second = closeBtn;
    }
    return _react2.default.createElement(
      _grommet.Box,
      _extends({
        direction: 'row',
        fill: 'horizontal',
        border: Border,
        round: Round,
        onClick: this.clickTag,
        role: 'checkbox',
        'aria-checked': true,
        background: background || 'status-' + status,
        reverse: reverse,
        theme: theme,
        grommet: grommet
      }, rest),
      first,
      second
    );
  };

  return Notification;
}(_react.Component);

Notification.contextTypes = {
  grommet: _propTypes2.default.object
};
Notification.defaultProps = {
  status: 'info',
  size: 'medium',
  message: 'Notification...',
  icon: true,
  animation: 'fadeIn',
  closer: undefined,
  pad: 'small',
  margin: 'small',
  locale: 'en-us'
};


var NotificationDoc = void 0;
if (process.env.NODE_ENV !== 'production') {
  NotificationDoc = require('./doc').default(Notification); // eslint-disable-line global-require
}

var NotificationWrapper = (0, _recompose.compose)(_hocs.withTheme)(NotificationDoc || Notification);

exports.Notification = NotificationWrapper;