'use strict';

Object.defineProperty(exports, "__esModule", {
  value: 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; };

exports.default = RecordHistory;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _reactIntl = require('react-intl');

var _cspaceLayout = require('cspace-layout');

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _AccountLabel = require('../user/AccountLabel');

var _AccountLabel2 = _interopRequireDefault(_AccountLabel);

var _RecordHistory = require('../../../styles/cspace-ui/RecordHistory.css');

var _RecordHistory2 = _interopRequireDefault(_RecordHistory);

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

var messages = (0, _reactIntl.defineMessages)({
  updated: {
    'id': 'recordHistory.updated',
    'defaultMessage': '{style, select, full {Updated {date} at {time} by {user}} dateTime {Updated {date} at {time}}}'
  },
  created: {
    'id': 'recordHistory.created',
    'defaultMessage': '{style, select, full {Created {date} at {time} by {user}} dateTime {Created {date} at {time}}}'
  },
  editing: {
    'id': 'recordHistory.editing',
    'defaultMessage': 'Editing'
  },
  saving: {
    'id': 'recordHistory.saving',
    'defaultMessage': 'Saving'
  }
});

var formatDate = function formatDate(timestamp) {
  return timestamp ? _react2.default.createElement(_reactIntl.FormattedDate, { value: timestamp, day: 'numeric', month: 'short', year: 'numeric' }) : null;
};

var formatTime = function formatTime(timestamp) {
  return timestamp ? _react2.default.createElement(_reactIntl.FormattedTime, { value: timestamp }) : null;
};

var formatUserId = function formatUserId(userId) {
  return userId ? _react2.default.createElement(_AccountLabel2.default, { userId: userId }) : '';
};

var propTypes = {
  data: _react.PropTypes.instanceOf(_immutable2.default.Map),
  isModified: _react.PropTypes.bool,
  isSavePending: _react.PropTypes.bool
};

function RecordHistory(props) {
  var data = props.data,
      isModified = props.isModified,
      isSavePending = props.isSavePending;


  var updatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(data);
  var updatedUserId = (0, _recordDataHelpers.getUpdatedUser)(data);

  var updated = null;

  if (updatedTimestamp || updatedUserId) {
    updated = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.updated, {
      key: 'updated',
      values: {
        date: formatDate(updatedTimestamp),
        time: formatTime(updatedTimestamp),
        user: formatUserId(updatedUserId),
        style: updatedUserId ? 'full' : 'dateTime'
      }
    }));
  }

  var createdTimestamp = (0, _recordDataHelpers.getCreatedTimestamp)(data);
  var createdUserId = (0, _recordDataHelpers.getCreatedUser)(data);

  var created = null;

  if (createdTimestamp || createdUserId) {
    created = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.created, {
      key: 'created',
      values: {
        date: formatDate(createdTimestamp),
        time: formatTime(createdTimestamp),
        user: formatUserId(createdUserId),
        style: createdUserId ? 'full' : 'dateTime'
      }
    }));
  }

  var currentState = void 0;

  if (isSavePending) {
    currentState = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.saving, { key: 'current' }));
  } else if (isModified) {
    currentState = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.editing, { key: 'current' }));
  }

  var items = [currentState, updated, created].filter(function (item) {
    return !!item;
  });
  var history = void 0;

  if (items.length > 1) {
    history = _react2.default.createElement(
      _cspaceLayout.Popover,
      { header: items[0], align: 'right' },
      _react2.default.createElement(
        'ul',
        null,
        items.map(function (item) {
          return _react2.default.createElement(
            'li',
            { key: item.key },
            item
          );
        })
      )
    );
  } else if (items.length > 0) {
    history = items[0];
  }

  return _react2.default.createElement(
    'div',
    { className: _RecordHistory2.default.common },
    history
  );
}

RecordHistory.propTypes = propTypes;