"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-common/lib/format-message"));

var _noop = _interopRequireDefault(require("../../../../util/noop"));

var _theme = _interopRequireDefault(require("./theme"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'fsRBD',
  template: function template(theme) {
    return "\n\n.EntrySave__styles__cancelButton,\n.EntrySave__styles__doneButton {\n  display: inline-flex;\n  height: 100%;\n  margin-left: ".concat(theme.buttonMargin || 'inherit', ";\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, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(EntrySave, _Component);

  function EntrySave() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(EntrySave)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      canSubmit: true
    };

    _this.onSubmit = function (e) {
      var displayPosition = _this.props.displayPosition;
      e.preventDefault();

      if (_this.props.isValid) {
        _this.successfulSubmit();
      } else {
        var msg;

        if (displayPosition === null) {
          msg = "Impossibile salvare questo elemento a causa dei dati di input non validi.";
        } else {
          msg = "Impossibile salvare l\u2019elemento " + displayPosition + " a causa dei dati di input non validi.";
        }

        _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();
    };

    return _this;
  }

  (0, _createClass2.default)(EntrySave, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      this.props.switchOnEditing(this.props.initialFocusId);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.clearChanges();
      var msg = "Modalit\xE0 di modifica elementi disattivata.";
      this.props.screenreaderNotification(msg);
    }
  }, {
    key: "successfulSubmit",
    value: function () {
      var _successfulSubmit = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                this.setState({
                  canSubmit: false
                });
                _context.prev = 1;
                _context.next = 4;
                return this.props.submitHandler();

              case 4:
                _context.next = 10;
                break;

              case 6:
                _context.prev = 6;
                _context.t0 = _context["catch"](1);
                this.setState({
                  canSubmit: true
                });
                return _context.abrupt("return");

              case 10:
                this.props.switchOffEditing();
                this.props.hideError(this.props.guid);
                this.props.screenreaderNotification("Modifiche all\u2019elemento salvate.");

              case 13:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[1, 6]]);
      }));

      function successfulSubmit() {
        return _successfulSubmit.apply(this, arguments);
      }

      return successfulSubmit;
    }()
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        className: styles.wrapper
      }, _react.default.createElement("div", {
        className: styles.cancelButton
      }, _react.default.createElement(_Button.default, {
        type: "button",
        onClick: this.onCancel,
        "data-automation": "sdk-cancel-button"
      }, "Annulla")), _react.default.createElement("div", {
        className: styles.doneButton
      }, _react.default.createElement(_Button.default, {
        type: "button",
        variant: "primary",
        onClick: this.onSubmit,
        disabled: !this.state.canSubmit || this.props.disableSubmit,
        "data-automation": "sdk-done-button"
      }, "Fatto")));
    }
  }]);
  EntrySave.displayName = "EntrySave";
  return EntrySave;
}(_react.Component), _class2.propTypes = {
  displayPosition: _propTypes.default.number,
  disableSubmit: _propTypes.default.bool,
  guid: _propTypes.default.string,
  submitHandler: _propTypes.default.func.isRequired,
  cancelHandler: _propTypes.default.func,
  clearChanges: _propTypes.default.func.isRequired,
  // From connected component
  clearNextQuizEntry: _propTypes.default.func.isRequired,
  hideError: _propTypes.default.func.isRequired,
  isValid: _propTypes.default.bool.isRequired,
  showError: _propTypes.default.func.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  // from makescrollable / from props being passed in occasionally
  switchOnEditing: _propTypes.default.func.isRequired,
  switchOffEditing: _propTypes.default.func.isRequired,
  initialFocusId: _propTypes.default.string
}, _class2.defaultProps = {
  displayPosition: null,
  cancelHandler: _noop.default,
  disableSubmit: false,
  guid: null,
  initialFocusId: null
}, _temp)) || _class);
exports.default = EntrySave;