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

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _partial = require('lodash/partial');

var _partial2 = _interopRequireDefault(_partial);

var _immutable = require('immutable');

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 _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 _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 _lib = require('@instructure/ui-themeable/lib');

var _lib2 = _interopRequireDefault(_lib);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _extractTextFromHtml = require('quiz-interactions/lib/util/extractTextFromHtml');

var _extractTextFromHtml2 = _interopRequireDefault(_extractTextFromHtml);

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

var _Modal2 = _interopRequireDefault(_Modal);

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

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.DeleteStimulusModal__styles__deleteStimulusModal {\n}\n\n.DeleteStimulusModal__styles__checkboxWrapper {\n  padding: ' + theme.checkboxWrapperPadding + ' 0;\n}\n\n.DeleteStimulusModal__styles__checkboxesWrapper {\n  width: 95%;\n  margin-left: auto;\n}\n\n.DeleteStimulusModal__styles__stimulusTitle {\n  padding-bottom: ' + theme.stimulusTitleBottomPadding + ';\n  font-size: ' + theme.stimulusTitleFontSize + ';\n}\n\n.DeleteStimulusModal__styles__radioSpace {\n  padding-bottom: ' + theme.radioSpaceBottomPadding + ';\n}\n';
  },
  'deleteStimulusModal': 'DeleteStimulusModal__styles__deleteStimulusModal',
  'checkboxWrapper': 'DeleteStimulusModal__styles__checkboxWrapper',
  'checkboxesWrapper': 'DeleteStimulusModal__styles__checkboxesWrapper',
  'stimulusTitle': 'DeleteStimulusModal__styles__stimulusTitle',
  'radioSpace': 'DeleteStimulusModal__styles__radioSpace'
};
var DeleteStimulusModal = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(DeleteStimulusModal, _Component);

  function DeleteStimulusModal() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = DeleteStimulusModal.__proto__ || Object.getPrototypeOf(DeleteStimulusModal)).call.apply(_ref, [this].concat(args))), _this), _this.handleSubmit = function (e) {
      _this.closeAction();
      var _this$props = _this.props,
          quizId = _this$props.quizId,
          stimulusEntry = _this$props.stimulusEntry,
          childEntries = _this$props.childEntries;


      if (_this.props.deletionMode === 'chooseWhich') {
        _this.removeStimulus(_this.props.selectedChildren.toArray());
      } else if (_this.props.deletionMode === 'deleteAll') {
        _this.props.removeStimulus(quizId, stimulusEntry, childEntries);
      } else if (_this.props.deletionMode === 'deleteNone' || childEntries.size === 0) {
        _this.removeStimulus();
      }
    }, _this.closeAction = function () {
      _this.props.closeModal();
      _this.props.setUi(_constants.DELETE_STIMULUS_MODAL_DELETION_MODE, null);
      _this.props.setUi(_constants.DELETE_STIMULUS_MODAL_SELECTED_CHILDREN, _immutable.Set.of());
    }, _this.setDeletionMode = function (event, value) {
      _this.props.setUi(_constants.DELETE_STIMULUS_MODAL_DELETION_MODE, value);
    }, _this.toggleChildSelected = function (childEntry) {
      return function (e) {
        var selectedChildren = _this.props.selectedChildren;

        var newSelectedChildren = e.target.checked ? selectedChildren.add(childEntry) : selectedChildren.delete(childEntry);

        _this.props.setUi(_constants.DELETE_STIMULUS_MODAL_SELECTED_CHILDREN, newSelectedChildren);
      };
    }, _this.renderCheckboxes = function () {
      if (_this.props.childEntries.size <= 1) {
        return;
      }
      if (_this.props.deletionMode !== 'chooseWhich') {
        return _react2.default.createElement('div', { className: styles.radioSpace });
      }

      var checkboxes = _this.props.childEntries.toArray().map(function (childEntry) {
        var isChecked = _this.props.selectedChildren.has(childEntry);
        var entry = childEntry.getEntry();
        var itemBody = entry.itemBody;
        if (entry.quizEntry.isBankEntry) {
          itemBody = entry.getEntry().itemBody;
        } else if (entry.quizEntry.isBank) {
          itemBody = 'Elementbank: ' + entry.title;
        }

        return _react2.default.createElement(
          'div',
          { key: childEntry.id, className: styles.checkboxWrapper },
          _react2.default.createElement(_Checkbox2.default, {
            checked: isChecked,
            label: (0, _extractTextFromHtml2.default)(itemBody),
            onChange: _this.toggleChildSelected(childEntry),
            value: 'true',
            variant: 'simple'
          })
        );
      });

      return _react2.default.createElement(
        'div',
        { key: 'checkboxes', className: styles.checkboxesWrapper },
        _react2.default.createElement('hr', null),
        checkboxes,
        _react2.default.createElement('hr', null)
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(DeleteStimulusModal, [{
    key: 'confirmRemoveData',
    value: function confirmRemoveData() {
      var _s, _n, _s2, _n2;

      var numKeepers = this.props.childEntries.size - this.props.selectedChildren.size;

      var text = 'Det/de resterende ' + (_s = numKeepers, _n = +_s, _n === 1 ? 'element' : ('elementer')) + ' bliver klonet for at fjerne stimulus-associering, da eleverne allerede er begyndt at svare p\xE5 ' + (_s = numKeepers, _n = +_s, _n === 1 ? 'det' : ('dem')) + '.';
      var title = 'Klon ' + (_s2 = numKeepers, _n2 = +_s2, _n2 === 1 ? 'element' : ('elementer')) + '?';
      return {
        title: title,
        text: text,
        continueText: 'Bekr\xE6ft',
        cancelText: 'Annuller'
      };
    }
  }, {
    key: 'anyImmutableKeepers',
    value: function anyImmutableKeepers() {
      var keepers = (0, _immutable.Set)(this.props.childEntries).subtract(this.props.selectedChildren);
      return keepers.some(function (keeper) {
        return keeper.isImmutable();
      });
    }
  }, {
    key: 'removeStimulus',
    value: function removeStimulus() {
      var childEntriesToDelete = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var _props = this.props,
          removeStimulus = _props.removeStimulus,
          quizId = _props.quizId,
          stimulusEntry = _props.stimulusEntry;

      if (this.anyImmutableKeepers()) {
        this.props.withConfirm((0, _partial2.default)(removeStimulus, quizId, stimulusEntry, childEntriesToDelete), this.confirmRemoveData());
      } else {
        this.props.removeStimulus(quizId, stimulusEntry, childEntriesToDelete);
      }
    }
  }, {
    key: 'cancelButton',
    value: function cancelButton() {
      return _react2.default.createElement(
        _Button2.default,
        { onClick: this.closeAction, margin: _constants.XSMALL_SIDE_MARGIN },
        'Annuller'
      );
    }
  }, {
    key: 'doneButton',
    value: function doneButton() {
      var _props2 = this.props,
          deletionMode = _props2.deletionMode,
          childEntries = _props2.childEntries;

      var disabled = !deletionMode && childEntries.size > 0;
      return _react2.default.createElement(
        _Button2.default,
        {
          disabled: disabled,
          variant: 'primary',
          onClick: this.handleSubmit,
          margin: _constants.XSMALL_SIDE_MARGIN,
          'data-automation': 'sdk-stimulus-doneButton'
        },
        'Aflever'
      );
    }
  }, {
    key: 'renderChooseWhich',
    value: function renderChooseWhich() {
      if (this.props.childEntries.size <= 1) {
        return;
      }

      return _react2.default.createElement(_RadioInput2.default, {
        key: 'chooseWhich',
        value: 'chooseWhich',
        label: 'Fjern stimulus, og v\xE6lg, hvilke sp\xF8rgsm\xE5l der skal fjernes.'
      });
    }
  }, {
    key: 'renderMainMenu',
    value: function renderMainMenu() {
      return _react2.default.createElement(
        _RadioInputGroup2.default,
        {
          name: 'deletionMode',
          defaultValue: this.props.deletionMode,
          onChange: this.setDeletionMode
        },
        _react2.default.createElement(_RadioInput2.default, {
          key: 'deleteAll',
          value: 'deleteAll',
          label: 'Fjern stimulus og alle vedh\xE6ftede sp\xF8rgsm\xE5l.'
        }),
        _react2.default.createElement('div', { className: styles.radioSpace }),
        this.renderChooseWhich(),
        this.renderCheckboxes(),
        _react2.default.createElement(_RadioInput2.default, {
          key: 'deleteNone',
          value: 'deleteNone',
          label: 'Fjern stimulus alene. Sp\xF8rgsm\xE5l vil blive frigivet til vurdering.'
        }),
        _react2.default.createElement('div', { className: styles.radioSpace })
      );
    }
  }, {
    key: 'renderContent',
    value: function renderContent() {
      var content = this.props.childEntries.size === 0 ? _react2.default.createElement(
        'span',
        null,
        'Er du sikker p\xE5, du vil fjerne denne stimulus?'
      ) : this.renderMainMenu();
      var stimulusTitle = this.props.stimulusEntry && this.props.stimulusEntry.getEntry().title;
      return _react2.default.createElement(
        'div',
        { className: styles.deleteStimulusModal },
        _react2.default.createElement(
          'div',
          { className: styles.stimulusTitle },
          stimulusTitle
        ),
        content
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        _Modal2.default,
        {
          open: this.props.modalOpen,
          onDismiss: this.closeAction,
          size: 'small',
          label: 'Bekr\xE6ft fjernelse af stimulus'
        },
        _react2.default.createElement(
          _Modal.ModalHeader,
          null,
          _react2.default.createElement(
            _Heading2.default,
            { level: 'reset', as: 'h2' },
            'Bekr\xE6ft fjernelse af stimulus'
          ),
          _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.doneButton()
        )
      );
    }
  }]);
  DeleteStimulusModal.displayName = 'DeleteStimulusModal'
  ;
  return DeleteStimulusModal;
}(_react.Component), _class2.propTypes = {
  childEntries: _reactImmutableProptypes2.default.list.isRequired,
  closeModal: _propTypes2.default.func.isRequired,
  deletionMode: _propTypes2.default.string,
  modalOpen: _propTypes2.default.bool.isRequired,
  removeStimulus: _propTypes2.default.func.isRequired,
  selectedChildren: _reactImmutableProptypes2.default.set,
  setUi: _propTypes2.default.func.isRequired,
  withConfirm: _propTypes2.default.func.isRequired,
  stimulusEntry: _reactImmutableProptypes2.default.record,
  quizId: _propTypes2.default.string
}, _class2.defaultProps = {
  selectedChildren: _immutable.Set.of(),
  deletionMode: '',
  stimulusEntry: void 0,
  quizId: void 0
}, _temp2)) || _class);
exports.default = DeleteStimulusModal;