"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 _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

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

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

var _Checkbox = _interopRequireDefault(require("@instructure/ui-forms/lib/components/Checkbox"));

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

var _RadioInputGroup = _interopRequireDefault(require("@instructure/ui-forms/lib/components/RadioInputGroup"));

var _RadioInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/RadioInput"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

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

var _Bank = _interopRequireDefault(require("../../../common/records/Bank"));

var _BankSelection = _interopRequireDefault(require("../../../common/components/resources/BankSelection"));

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

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

var _ref =
/*#__PURE__*/
_react.default.createElement("br", null);

var _ref2 =
/*#__PURE__*/
_react.default.createElement("br", null);

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

  function CopyMoveBankEntryModal() {
    var _getPrototypeOf2;

    var _this;

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

    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)(CopyMoveBankEntryModal)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      bankType: 'existing',
      newBankTitle: ''
    };

    _this.copyOrMove = function (newBankId) {
      if (_this.props.keepCopy) {
        _this.copyBankEntry(newBankId);
      } else {
        _this.moveBankEntry(newBankId);
      }

      _this.closeAction();
    };

    _this.continueAction = function () {
      if (_this.state.bankType === 'new') {
        var newBank = _Bank.default.create({
          title: _this.state.newBankTitle,
          language: _this.context.locale
        });

        _this.props.createBank(newBank).then(function (bankData) {
          _this.copyOrMove(bankData.id);
        });
      } else {
        _this.copyOrMove(_this.props.selectedBank.id);
      }
    };

    _this.moveBankEntry = function (newBankId) {
      return _this.props.updateBankEntry(_this.props.sourceBankId, _this.props.sourceBankEntryId, {
        bankId: newBankId
      }).then(function (bankEntry) {
        _this.props.disassociateBankEntry(_this.props.sourceBankId, _this.props.sourceBankEntryId);

        return bankEntry;
      }).then(_this.props.onAdd);
    };

    _this.copyBankEntry = function (newBankId) {
      return _this.props.createEntry().then(function (entry) {
        return _this.props.createBankEntry(newBankId, _this.props.entryType, entry.id);
      }).then(_this.props.onAdd);
    };

    _this.closeAction = function () {
      if (_this.props.modalTrigger) {
        _this.props.modalTrigger.focus();
      }

      _this.props.closeModal();
    };

    _this.onBankSelection = function (e, option) {
      _this.props.uiSet(_constants.COPY_MOVE_BANK_ENTRY_MODAL_BANK_SELECTION, option.value);
    };

    _this.onKeepCopyChange = function (e) {
      _this.props.uiSet(_constants.COPY_MOVE_BANK_ENTRY_MODAL_KEEP_COPY, e.target.checked);
    };

    _this.handleRadioChange = function (event, value) {
      _this.setState({
        bankType: value
      });
    };

    _this.onTitleChange = function (e) {
      _this.setState({
        newBankTitle: e.target.value
      });
    };

    return _this;
  }

  (0, _createClass2.default)(CopyMoveBankEntryModal, [{
    key: "cancelButton",
    value: function cancelButton() {
      return _react.default.createElement(_Button.default, {
        onClick: this.closeAction,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, "Cancel");
    }
  }, {
    key: "addToBankButton",
    value: function addToBankButton() {
      var _this$state = this.state,
          bankType = _this$state.bankType,
          newBankTitle = _this$state.newBankTitle;
      var disabled = bankType === 'existing' && !this.props.selectedBank || bankType === 'new' && (!newBankTitle || !newBankTitle.trim());
      return _react.default.createElement(_Button.default, {
        variant: "primary",
        onClick: this.continueAction,
        disabled: disabled,
        margin: _constants.XSMALL_SIDE_MARGIN,
        "data-automation": "sdk-add-to-bank-button"
      }, "Add");
    }
  }, {
    key: "renderBankTypeContent",
    value: function renderBankTypeContent() {
      if (this.state.bankType === 'existing') {
        return _react.default.createElement(_BankSelection.default, {
          onBankSelection: this.onBankSelection,
          selectedBank: this.props.selectedBank,
          "data-automation": "sdk-bank-selection",
          visibleOptionsCount: 3
        });
      }

      return _react.default.createElement(_TextInput.default, {
        onChange: this.onTitleChange,
        value: this.state.newBankTitle,
        label: "New bank title",
        type: "text"
      });
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      return _react.default.createElement("div", null, _react.default.createElement(_RadioInputGroup.default, {
        onChange: this.handleRadioChange,
        name: "Bank type",
        defaultValue: this.state.bankType,
        description: _react.default.createElement(_ScreenReaderContent.default, null, "Bank type")
      }, _react.default.createElement(_RadioInput.default, {
        value: "existing",
        label: "Existing item bank"
      }), _react.default.createElement(_RadioInput.default, {
        value: "new",
        label: "New item bank"
      })), _ref, this.renderBankTypeContent(), _ref2, _react.default.createElement(_Checkbox.default, {
        label: "Keep a copy in this bank",
        value: "medium",
        checked: this.props.keepCopy,
        onChange: this.onKeepCopyChange,
        "data-automation": "sdk-keep-copy-in-bank-checkbox"
      }));
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement(_Modal.default, {
        open: this.props.modalOpen,
        onDismiss: this.closeAction,
        size: "small",
        label: this.props.label,
        "data-automation": "sdk-copy-move-bank-entry-modal"
      }, _react.default.createElement(_Modal.ModalHeader, null, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, this.props.label), _react.default.createElement(_CloseButton.default, {
        onClick: this.closeAction,
        placement: "end",
        offset: "medium",
        variant: "icon"
      }, "Close")), _react.default.createElement(_Modal.ModalBody, null, this.renderContent()), _react.default.createElement(_Modal.ModalFooter, null, this.cancelButton(), this.addToBankButton()));
    }
  }]);
  CopyMoveBankEntryModal.displayName = "CopyMoveBankEntryModal";
  return CopyMoveBankEntryModal;
}(_react.Component);

exports.default = CopyMoveBankEntryModal;
CopyMoveBankEntryModal.propTypes = {
  closeModal: _propTypes.default.func.isRequired,
  createBank: _propTypes.default.func.isRequired,
  createBankEntry: _propTypes.default.func.isRequired,
  // Create the bank entry's entry, or clone it if it already exists. Should
  // always be provided when a source is provided.
  createEntry: _propTypes.default.func.isRequired,
  disassociateBankEntry: _propTypes.default.func.isRequired,
  // eslint-disable-next-line react/no-unused-prop-types
  entryId: _propTypes.default.string.isRequired,
  // Only present for use in modalOpen key via this component's connected index
  entryType: _propTypes.default.string.isRequired,
  // Resolve to the bank entry's entry, or create it if it does not yet exist.
  keepCopy: _propTypes.default.bool,
  label: _propTypes.default.string.isRequired,
  modalOpen: _propTypes.default.bool.isRequired,
  modalTrigger: _propTypes.default.instanceOf(Element),
  onAdd: _propTypes.default.func,
  selectedBank: _reactImmutableProptypes.default.record,
  sourceBankEntryId: _propTypes.default.string.isRequired,
  sourceBankId: _propTypes.default.string.isRequired,
  uiSet: _propTypes.default.func.isRequired,
  updateBankEntry: _propTypes.default.func.isRequired
};
CopyMoveBankEntryModal.contextTypes = {
  locale: _propTypes.default.string
};
CopyMoveBankEntryModal.defaultProps = {
  keepCopy: true,
  modalTrigger: null,
  onAdd: function onAdd() {},
  selectedBank: null
};