'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 _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

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

var _Button2 = _interopRequireDefault(_Button);

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

var _CloseButton2 = _interopRequireDefault(_CloseButton);

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

var _Checkbox2 = _interopRequireDefault(_Checkbox);

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

var _Heading2 = _interopRequireDefault(_Heading);

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

var _RadioInputGroup2 = _interopRequireDefault(_RadioInputGroup);

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

var _RadioInput2 = _interopRequireDefault(_RadioInput);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

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

var _TextInput2 = _interopRequireDefault(_TextInput);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _Bank2 = _interopRequireDefault(_Bank);

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

var _BankSelection2 = _interopRequireDefault(_BankSelection);

var _Modal = require('../../../common/components/Modal');

var _Modal2 = _interopRequireDefault(_Modal);

var _constants = require('../../../constants');

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

var CopyMoveBankEntryModal = (_temp2 = _class = function (_Component) {
  (0, _inherits3.default)(CopyMoveBankEntryModal, _Component);

  function CopyMoveBankEntryModal() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = CopyMoveBankEntryModal.__proto__ || Object.getPrototypeOf(CopyMoveBankEntryModal)).call.apply(_ref, [this].concat(args))), _this), _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 = _Bank2.default.create({ title: _this.state.newBankTitle });
        _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 });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(CopyMoveBankEntryModal, [{
    key: 'cancelButton',
    value: function cancelButton() {
      return _react2.default.createElement(
        _Button2.default,
        { onClick: this.closeAction, margin: _constants.XSMALL_SIDE_MARGIN },
        'Cancel'
      );
    }
  }, {
    key: 'addToBankButton',
    value: function addToBankButton() {
      var _state = this.state,
          bankType = _state.bankType,
          newBankTitle = _state.newBankTitle;

      var disabled = bankType === 'existing' && !this.props.selectedBank || bankType === 'new' && (!newBankTitle || !newBankTitle.trim());

      return _react2.default.createElement(
        _Button2.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 _react2.default.createElement(_BankSelection2.default, {
          onBankSelection: this.onBankSelection,
          selectedBank: this.props.selectedBank,
          'data-automation': 'sdk-bank-selection'
        });
      }
      return _react2.default.createElement(_TextInput2.default, {
        onChange: this.onTitleChange,
        value: this.state.newBankTitle,
        label: 'New bank title',
        type: 'text'
      });
    }
  }, {
    key: 'renderContent',
    value: function renderContent() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _RadioInputGroup2.default,
          {
            onChange: this.handleRadioChange,
            name: 'Bank type',
            defaultValue: this.state.bankType,
            description: _react2.default.createElement(
              _ScreenReaderContent2.default,
              null,
              'Bank type'
            )
          },
          _react2.default.createElement(_RadioInput2.default, { value: 'existing', label: 'Existing item bank' }),
          _react2.default.createElement(_RadioInput2.default, { value: 'new', label: 'New item bank' })
        ),
        _react2.default.createElement('br', null),
        this.renderBankTypeContent(),
        _react2.default.createElement('br', null),
        _react2.default.createElement(_Checkbox2.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 _react2.default.createElement(
        _Modal2.default,
        {
          open: this.props.modalOpen,
          onDismiss: this.closeAction,
          size: 'small',
          label: this.props.label,
          'data-automation': 'sdk-copy-move-bank-entry-modal'
        },
        _react2.default.createElement(
          _Modal.ModalHeader,
          null,
          _react2.default.createElement(
            _Heading2.default,
            { level: 'reset', as: 'h2' },
            this.props.label
          ),
          _react2.default.createElement(_CloseButton2.default, {
            onClick: this.closeAction,
            placement: 'end',
            offset: 'medium',
            variant: 'icon'
          })
        ),
        _react2.default.createElement(
          _Modal.ModalBody,
          null,
          this.renderContent()
        ),
        _react2.default.createElement(
          _Modal.ModalFooter,
          null,
          this.cancelButton(),
          this.addToBankButton()
        )
      );
    }
  }]);
  CopyMoveBankEntryModal.displayName = 'CopyMoveBankEntryModal'
  ;
  return CopyMoveBankEntryModal;
}(_react.Component), _class.propTypes = {
  closeModal: _propTypes2.default.func.isRequired,
  createBank: _propTypes2.default.func.isRequired,
  createBankEntry: _propTypes2.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: _propTypes2.default.func.isRequired,
  disassociateBankEntry: _propTypes2.default.func.isRequired,
  // eslint-disable-next-line react/no-unused-prop-types
  entryId: _propTypes2.default.string.isRequired, // Only present for use in modalOpen key via this component's connected index
  entryType: _propTypes2.default.string.isRequired,
  // Resolve to the bank entry's entry, or create it if it does not yet exist.
  keepCopy: _propTypes2.default.bool,
  label: _propTypes2.default.string.isRequired,
  modalOpen: _propTypes2.default.bool.isRequired,
  modalTrigger: _propTypes2.default.instanceOf(Element),
  onAdd: _propTypes2.default.func,
  selectedBank: _reactImmutableProptypes2.default.record,
  sourceBankEntryId: _propTypes2.default.string.isRequired,
  sourceBankId: _propTypes2.default.string.isRequired,
  uiSet: _propTypes2.default.func.isRequired,
  updateBankEntry: _propTypes2.default.func.isRequired
}, _class.defaultProps = {
  keepCopy: true,
  modalTrigger: null,
  onAdd: function onAdd() {},
  selectedBank: null
}, _temp2);
exports.default = CopyMoveBankEntryModal;