'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _cspaceInput = require('cspace-input');

var _ErrorBadge = require('./ErrorBadge');

var _ErrorBadge2 = _interopRequireDefault(_ErrorBadge);

var _SaveButton = require('../../../styles/cspace-ui/SaveButton.css');

var _SaveButton2 = _interopRequireDefault(_SaveButton);

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

const Button = _cspaceInput.components.Button;


const messages = (0, _reactIntl.defineMessages)({
  label: {
    'id': 'saveButton.label',
    'defaultMessage': 'Save'
  },
  validationErrors: {
    'id': 'saveButton.validationErrors',
    'defaultMessage': 'Field validation errors must be corrected before this record can be saved.'
  }
});

const propTypes = {
  className: _propTypes2.default.string,
  intl: _reactIntl.intlShape,
  isModified: _propTypes2.default.bool,
  isSavePending: _propTypes2.default.bool,
  label: _propTypes2.default.node,
  readOnly: _propTypes2.default.bool,
  validationErrors: _propTypes2.default.instanceOf(_immutable2.default.Map),
  onClick: _propTypes2.default.func,
  onErrorBadgeClick: _propTypes2.default.func
};

function SaveButton(props) {
  const className = props.className,
        intl = props.intl,
        isModified = props.isModified,
        isSavePending = props.isSavePending,
        readOnly = props.readOnly,
        validationErrors = props.validationErrors,
        onClick = props.onClick,
        onErrorBadgeClick = props.onErrorBadgeClick;


  if (readOnly) {
    return null;
  }

  let statusClass;

  if (isSavePending) {
    statusClass = _SaveButton2.default.pending;
  } else if (isModified) {
    statusClass = _SaveButton2.default.normal;
  } else {
    statusClass = _SaveButton2.default.done;
  }

  const classes = (0, _classnames2.default)(className, statusClass);

  let label = props.label;


  if (!label) {
    label = _react2.default.createElement(_reactIntl.FormattedMessage, messages.label);
  }

  const errorBadge = validationErrors ? _react2.default.createElement(_ErrorBadge2.default, { onClick: onErrorBadgeClick }) : null;

  const title = validationErrors ? intl.formatMessage(messages.validationErrors) : '';

  const button = _react2.default.createElement(
    Button,
    {
      className: classes,
      disabled: validationErrors || isSavePending,
      icon: true,
      name: 'save',
      title: title,
      onClick: onClick
    },
    label
  );

  if (errorBadge) {
    return _react2.default.createElement(
      'div',
      { style: { display: 'inline-block', position: 'relative' } },
      button,
      errorBadge
    );
  }

  return button;
}

SaveButton.propTypes = propTypes;

exports.default = (0, _reactIntl.injectIntl)(SaveButton);