"use strict";

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

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

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

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 _CloseButton = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/CloseButton"));

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

var _Text = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Text"));

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

var _constants = require("@instructure/quiz-common/lib/constants");

var _Modal = _interopRequireWildcard(require("@instructure/ui-overlays/lib/components/Modal"));

var ConfirmationModal =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(ConfirmationModal, _Component);

  function ConfirmationModal() {
    var _getPrototypeOf2;

    var _this;

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

    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)(ConfirmationModal)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      action: null
    };

    _this.continueAction = function () {
      _this.setState({
        action: 'continue'
      }, _this.props.closeModal);
    };

    _this.cancelAction = function () {
      _this.setState({
        action: 'cancel'
      }, _this.props.closeModal);
    };

    _this.doAction = function () {
      var _this$props = _this.props,
          onContinue = _this$props.onContinue,
          onCancel = _this$props.onCancel;

      if (_this.state.action === 'continue') {
        onContinue && onContinue();
      } else if (_this.state.action === 'cancel') {
        onCancel && onCancel();
      }

      _this.setState({
        action: null
      });
    };

    return _this;
  }

  (0, _createClass2.default)(ConfirmationModal, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          title = _this$props2.title,
          text = _this$props2.text,
          cancelText = _this$props2.cancelText,
          continueText = _this$props2.continueText,
          size = _this$props2.size,
          shouldReturnFocus = _this$props2.shouldReturnFocus,
          continueVariant = _this$props2.continueVariant;
      return _react.default.createElement(_Modal.default, {
        open: this.props.modalOpen,
        size: size || 'small',
        label: title || (0, _formatMessage.default)('Confirm'),
        onDismiss: this.cancelAction,
        "data-automation": "sdk-confirmation-modal",
        shouldReturnFocus: shouldReturnFocus,
        onClose: this.doAction
      }, _react.default.createElement(_Modal.ModalHeader, null, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, title || (0, _formatMessage.default)('Confirm')), _react.default.createElement(_CloseButton.default, {
        onClick: this.props.closeModal,
        placement: "end",
        offset: "medium",
        variant: "icon"
      }, (0, _formatMessage.default)('Close'))), _react.default.createElement(_Modal.ModalBody, null, _react.default.createElement(_Text.default, null, text || (0, _formatMessage.default)('Are you sure?'))), _react.default.createElement(_Modal.ModalFooter, null, _react.default.createElement(_Button.default, {
        onClick: this.cancelAction,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, cancelText || (0, _formatMessage.default)('Cancel')), _react.default.createElement(_Button.default, {
        variant: continueVariant || 'primary',
        onClick: this.continueAction,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, continueText || (0, _formatMessage.default)('Confirm'))));
    }
  }]);
  ConfirmationModal.displayName = "ConfirmationModal";
  return ConfirmationModal;
}(_react.Component);

exports.default = ConfirmationModal;
ConfirmationModal.propTypes = {
  closeModal: _propTypes.default.func.isRequired,
  modalOpen: _propTypes.default.bool.isRequired,
  title: _propTypes.default.string,
  text: _propTypes.default.string,
  cancelText: _propTypes.default.string,
  continueText: _propTypes.default.string,
  continueVariant: _propTypes.default.string,
  size: _propTypes.default.string,
  shouldReturnFocus: _propTypes.default.bool,
  onContinue: _propTypes.default.func,
  onCancel: _propTypes.default.func
};
ConfirmationModal.defaultProps = {
  title: null,
  text: null,
  cancelText: null,
  continueText: null,
  continueVariant: null,
  size: null,
  shouldReturnFocus: true,
  onContinue: null,
  onCancel: null
};