'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _dec, _class, _class2, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Button = require('@instructure/ui-buttons/lib/components/Button');

var _Button2 = _interopRequireDefault(_Button);

var _lib = require('@instructure/ui-themeable/lib');

var _lib2 = _interopRequireDefault(_lib);

var _formatMessage = require('../../../../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _noop = require('../../../../util/noop');

var _noop2 = _interopRequireDefault(_noop);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.EntrySave__styles__cancelButton,\n.EntrySave__styles__doneButton {\n  display: inline-flex;\n  height: 100%;\n  margin-left: ' + theme.buttonMargin + ';\n}\n\n.EntrySave__styles__wrapper {\n  display: flex;\n}\n';
  },
  'cancelButton': 'EntrySave__styles__cancelButton',
  'doneButton': 'EntrySave__styles__doneButton',
  'wrapper': 'EntrySave__styles__wrapper'
};
var EntrySave = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(EntrySave, _Component);

  function EntrySave() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, EntrySave);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EntrySave.__proto__ || Object.getPrototypeOf(EntrySave)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      canSubmit: true
    }, _this.onSubmit = function (e) {
      var displayPosition = _this.props.displayPosition;

      e.preventDefault();
      if (_this.props.isValid) {
        _this.successfulSubmit();
      } else {
        var msg = void 0;
        if (displayPosition === null) {
          msg = 'This item cannot be saved due to invalid input data.';
        } else {
          msg = 'Item ' + displayPosition + ' cannot be saved due to invalid input data.';
        }
        _this.props.screenreaderNotification(msg);
        _this.props.showError(_this.props.guid);
        _this.props.clearNextQuizEntry();
      }
    }, _this.clearChanges = function () {
      _this.props.switchOffEditing();
      _this.props.hideError(_this.props.guid);
      _this.props.clearChanges();
    }, _this.onCancel = function () {
      _this.clearChanges();
      _this.props.cancelHandler();
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(EntrySave, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      this.props.switchOnEditing(this.props.initialFocusId);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.clearChanges();
      var msg = 'Item editing mode turned off.';
      this.props.screenreaderNotification(msg);
    }
  }, {
    key: 'successfulSubmit',
    value: function successfulSubmit() {
      this.setState({ canSubmit: false });
      this.props.switchOffEditing();
      this.props.hideError(this.props.guid);
      this.props.submitHandler();
      var msg = 'Changes to item saved.';
      this.props.screenreaderNotification(msg);
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: styles.wrapper },
        _react2.default.createElement(
          'div',
          { className: styles.cancelButton },
          _react2.default.createElement(
            _Button2.default,
            {
              type: 'button',
              onClick: this.onCancel,
              'data-automation': 'sdk-cancel-button'
            },
            'Cancel'
          )
        ),
        _react2.default.createElement(
          'div',
          { className: styles.doneButton },
          _react2.default.createElement(
            _Button2.default,
            {
              type: 'button',
              variant: 'primary',
              onClick: this.onSubmit,
              disabled: !this.state.canSubmit || this.props.disableSubmit,
              'data-automation': 'sdk-done-button'
            },
            'Done'
          )
        )
      );
    }
  }]);
  EntrySave.displayName = 'EntrySave'
  ;
  return EntrySave;
}(_react.Component), _class2.propTypes = {
  displayPosition: _propTypes2.default.number,
  disableSubmit: _propTypes2.default.bool,
  guid: _propTypes2.default.string,
  submitHandler: _propTypes2.default.func.isRequired,
  cancelHandler: _propTypes2.default.func,
  clearChanges: _propTypes2.default.func.isRequired,
  // From connected component
  clearNextQuizEntry: _propTypes2.default.func.isRequired,
  hideError: _propTypes2.default.func.isRequired,
  isValid: _propTypes2.default.bool.isRequired,
  showError: _propTypes2.default.func.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  // from makescrollable / from props being passed in occasionally
  switchOnEditing: _propTypes2.default.func.isRequired,
  switchOffEditing: _propTypes2.default.func.isRequired,
  initialFocusId: _propTypes2.default.string
}, _class2.defaultProps = {
  displayPosition: null,
  cancelHandler: _noop2.default,
  disableSubmit: false,
  guid: null,
  initialFocusId: null
}, _temp2)) || _class);
exports.default = EntrySave;