"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _immutable = _interopRequireDefault(require("immutable"));
var _reactIntl = require("react-intl");
var _classnames = _interopRequireDefault(require("classnames"));
var _cspaceInput = require("cspace-input");
var _ErrorBadge = _interopRequireDefault(require("./ErrorBadge"));
var _validationHelpers = require("../../helpers/validationHelpers");
var _SaveButton = _interopRequireDefault(require("../../../styles/cspace-ui/SaveButton.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const {
  Button
} = _cspaceInput.components;
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: _propTypes.default.string,
  intl: _reactIntl.intlShape,
  isModified: _propTypes.default.bool,
  isSavePending: _propTypes.default.bool,
  label: _propTypes.default.node,
  readOnly: _propTypes.default.bool,
  validationErrors: _propTypes.default.instanceOf(_immutable.default.Map),
  onClick: _propTypes.default.func,
  onErrorBadgeClick: _propTypes.default.func
};
function SaveButton(props) {
  const {
    className,
    intl,
    isModified,
    isSavePending,
    readOnly,
    validationErrors,
    onClick,
    onErrorBadgeClick
  } = props;
  if (readOnly) {
    return null;
  }
  let statusClass;
  if (isSavePending) {
    statusClass = _SaveButton.default.pending;
  } else if (isModified) {
    statusClass = _SaveButton.default.normal;
  } else {
    statusClass = _SaveButton.default.done;
  }
  const classes = (0, _classnames.default)(className, statusClass);
  let {
    label
  } = props;
  if (!label) {
    label = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.label);
  }
  const isValidationBlocked = (0, _validationHelpers.hasBlockingError)(validationErrors);
  const errorBadge = isValidationBlocked ? /*#__PURE__*/_react.default.createElement(_ErrorBadge.default, {
    onClick: onErrorBadgeClick
  }) : null;
  const title = isValidationBlocked ? intl.formatMessage(messages.validationErrors) : '';
  const button = /*#__PURE__*/_react.default.createElement(Button, {
    className: classes,
    disabled: isValidationBlocked || isSavePending,
    icon: true,
    name: "save",
    title: title,
    onClick: onClick
  }, label);
  if (errorBadge) {
    return /*#__PURE__*/_react.default.createElement("div", {
      style: {
        display: 'inline-block',
        position: 'relative'
      }
    }, button, errorBadge);
  }
  return button;
}
SaveButton.propTypes = propTypes;
var _default = (0, _reactIntl.injectIntl)(SaveButton);
exports.default = _default;