'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.QuizEntryShowRaw = 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,
    _default_enGB_number = Intl.NumberFormat('en-GB', { style: 'decimal' }).format;

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 _reactDnd = require('react-dnd');

var _IconBankLine = require('instructure-icons/lib/Line/IconBankLine');

var _IconBankLine2 = _interopRequireDefault(_IconBankLine);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

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

var _lib2 = _interopRequireDefault(_lib);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _Item = require('../../../../../common/records/Item');

var _Item2 = _interopRequireDefault(_Item);

var _Overlay = require('../../../../../common/components/shared/overlay/Overlay');

var _Overlay2 = _interopRequireDefault(_Overlay);

var _index = require('../../ActionButtons/index');

var _index2 = _interopRequireDefault(_index);

var _dragAndDropUtils = require('../../../../../common/components/shared/drag_and_drop/dragAndDropUtils');

var _PositionBox = require('../../../../../common/components/resources/positionBox/PositionBox');

var _PositionBox2 = _interopRequireDefault(_PositionBox);

var _DragAndDropZone = require('../../../../../common/components/shared/drag_and_drop/DragAndDropZone');

var _DragAndDropZone2 = _interopRequireDefault(_DragAndDropZone);

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.QuizEntryShow__styles__quizEntryWrapper {\n  transform: translate3d(0, 0, 0); /* for drag and drop preview to work */\n}\n\n.QuizEntryShow__styles__quizEntryShow {\n  padding: ' + theme.quizEntryShowPadding + ';\n  background-color: ' + theme.quizEntryShowBackgroundColor + ';\n}\n\n.QuizEntryShow__styles__header {\n  height: ' + theme.headerHeight + ';\n  display: flex;\n  flex-wrap: nowrap;\n  flex-direction: row;\n  align-items: stretch;\n  padding-right: ' + theme.headerRightPadding + ';\n}\n\n.QuizEntryShow__styles__dndBorder {\n  border: ' + theme.dndBorder + ';\n}\n\n.QuizEntryShow__styles__leftHeader {\n  flex: 8 0 0;\n  display: inline-flex;\n  overflow: hidden;\n  align-items: center;\n}\n\n.QuizEntryShow__styles__rightHeader {\n  flex: 0 0 auto;\n  display: inline-flex;\n  text-align: right;\n  z-index: ' + theme.rightHeaderZIndex + ';\n  color: ' + theme.rightHeaderColor + ';\n}\n\n[dir="ltr"] .QuizEntryShow__styles__rightHeader {\n  text-align: right;\n}\n\n[dir="rtl"] .QuizEntryShow__styles__rightHeader {\n  text-align: right;\n}\n\n.QuizEntryShow__styles__rightHeader button {\n  background-color: transparent;\n  background-color: initial;\n}\n\n.QuizEntryShow__styles__title,\n.QuizEntryShow__styles__interactionTypeName,\n.QuizEntryShow__styles__points {\n  color: ' + theme.titleInteractionTypeNamePointsColor + ';\n  font-size: ' + theme.titleInteractionTypeNamePointsFontSize + ';\n  margin-left: ' + theme.titleInteractionTypeNamePointsLeftMargin + ';\n  margin-right: ' + theme.titleInteractionTypeNamePointsRightMargin + ';\n  text-overflow: ellipsis;\n  overflow: hidden;\n  white-space: nowrap;\n}\n\n.QuizEntryShow__styles__itemHolder {\n  margin: ' + theme.itemHolderMargin + ';\n}\n\n.QuizEntryShow__styles__optionsButton,\n.QuizEntryShow__styles__dragAndDropHandle {\n  background-color: transparent;\n  border: none;\n  color: inherit;\n  font-size: ' + theme.optionsButtonDragDropHandleFontSize + ';\n  margin: ' + theme.optionsButtonDragDropHandleMargin + ';\n}\n\n.QuizEntryShow__styles__optionsButton {\n  cursor: pointer;\n}\n';
  },
  'quizEntryWrapper': 'QuizEntryShow__styles__quizEntryWrapper',
  'quizEntryShow': 'QuizEntryShow__styles__quizEntryShow',
  'header': 'QuizEntryShow__styles__header',
  'dndBorder': 'QuizEntryShow__styles__dndBorder',
  'leftHeader': 'QuizEntryShow__styles__leftHeader',
  'rightHeader': 'QuizEntryShow__styles__rightHeader',
  'title': 'QuizEntryShow__styles__title',
  'interactionTypeName': 'QuizEntryShow__styles__interactionTypeName',
  'points': 'QuizEntryShow__styles__points',
  'itemHolder': 'QuizEntryShow__styles__itemHolder',
  'optionsButton': 'QuizEntryShow__styles__optionsButton',
  'dragAndDropHandle': 'QuizEntryShow__styles__dragAndDropHandle'
};
var QuizEntryShow = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(QuizEntryShow, _Component);

  function QuizEntryShow() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizEntryShow.__proto__ || Object.getPrototypeOf(QuizEntryShow)).call.apply(_ref, [this].concat(args))), _this), _this.editingOn = function () {
      if (!_this.props.quizEntryWithErrorsShowing) {
        var msg = 'Item switched to editing mode.';
        _this.props.screenreaderNotification(msg);
        _this.props.switchOnEditing();
      }
    }, _this.removeItem = function (e) {
      _this.props.withConfirm((0, _partial2.default)(_this.props.removeQuizEntry, _this.props.quizId, _this.props.quizEntry), _this.removalDialogData());
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(QuizEntryShow, [{
    key: 'getPointsText',
    value: function getPointsText(points) {
      var _s, _n, _i, _v;

      return _s = points, _n = +_s, (_i = Math.floor(Math.abs(+_s)), _v = (_s + '.').split('.')[1].length, _i === 1 && _v === 0 ? _default_enGB_number(points) + ' point' : _default_enGB_number(points) + ' points');
    }
  }, {
    key: 'removalDialogData',
    value: function removalDialogData() {
      return {
        title: 'Remove Item?',
        text: 'Are you sure you want to remove this item?',
        continueText: 'Remove Item',
        cancelText: 'Keep Item'
      };
    }
  }, {
    key: 'shouldDisableDragHandle',
    value: function shouldDisableDragHandle() {
      return this.props.quizEntries.size === 1;
    }
  }, {
    key: 'renderPointsPossible',
    value: function renderPointsPossible() {
      if (this.props.entry instanceof _Item2.default) {
        var pointsPossible = this.props.quizEntry.pointsPossible;

        return _react2.default.createElement(
          'div',
          { className: styles.points, 'data-automation': 'sdk-points-possible-div' },
          this.getPointsText(pointsPossible || 0)
        );
      }
    }
  }, {
    key: 'renderInteractionType',
    value: function renderInteractionType() {
      if (this.props.entry instanceof _Item2.default) {
        var interactionTypeName = this.props.entry.getInteractionType().name;
        return _react2.default.createElement(
          'div',
          {
            className: styles.interactionTypeName,
            'data-automation': 'sdk-interaction-type-name-div'
          },
          interactionTypeName
        );
      }
    }
  }, {
    key: 'renderBankedContent',
    value: function renderBankedContent() {
      if (this.props.quizEntry.isBank || this.props.quizEntry.isBankEntry) {
        return _react2.default.createElement(_IconBankLine2.default, null);
      }
    }
  }, {
    key: 'renderLeftHeader',
    value: function renderLeftHeader() {
      return _react2.default.createElement(
        'div',
        { className: styles.leftHeader },
        this.renderBankedContent(),
        this.renderInteractionType(),
        this.renderPointsPossible(),
        _react2.default.createElement(
          'div',
          {
            className: styles.title,
            'data-automation': 'sdk-quiz-entry-title-div'
          },
          this.props.entry.title
        )
      );
    }
  }, {
    key: 'setInert',
    value: function setInert(node) {
      node && node.setAttribute('inert', '');
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          connectDragPreview = _props.connectDragPreview,
          displayPosition = _props.displayPosition,
          draggingOver = _props.draggingOver,
          draggingAboveHalf = _props.draggingAboveHalf;

      var positionToDisplay = this.props.quizEntry.positionToDisplay(displayPosition);

      var positionText = 'Preview of item at position ' + positionToDisplay;
      var srMsg = 'The following item is a preview for the teacher. To edit this item, navigate back to the "Edit Item" button and click.';
      var propsForDnDZone = { draggingOver: draggingOver, draggingAboveHalf: draggingAboveHalf

        // Overlay gets tabIndex === "-1" so that the drag handle is tabbable before
        // the Overlay get focus and opens the quizEntry in edit mode
      };return _react2.default.createElement(
        _Overlay2.default,
        {
          onClick: this.editingOn,
          wrapperClassName: styles.quizEntryWrapper,
          tabIndex: '-1'
        },
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'above' })),
        connectDragPreview(_react2.default.createElement(
          'div',
          { className: styles.quizEntryShow, 'data-automation-item-id': this.props.quizEntry.id },
          _react2.default.createElement(
            'div',
            { className: styles.header },
            _react2.default.createElement(_PositionBox2.default, {
              position: positionToDisplay,
              headingText: positionText
            }),
            this.renderLeftHeader(),
            _react2.default.createElement(_index2.default, {
              connectDragSource: this.props.connectDragSource,
              displayPosition: this.props.displayPosition,
              onDelete: this.removeItem,
              quizEntries: this.props.quizEntries,
              quizEntry: this.props.quizEntry,
              scrollKey: this.props.scrollKey,
              shouldDisableDragHandle: this.shouldDisableDragHandle(),
              switchOnEditing: this.editingOn,
              shouldFocusOnMount: this.props.editQuizEntryToggle
            }),
            _react2.default.createElement(
              _ScreenReaderContent2.default,
              null,
              srMsg
            )
          ),
          _react2.default.createElement(
            'div',
            { className: styles.itemHolder, ref: this.setInert },
            this.props.children
          )
        )),
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'below' }))
      );
    }
  }]);
  QuizEntryShow.displayName = 'QuizEntryShow'
  ;
  return QuizEntryShow;
}(_react.Component), _class2.propTypes = {
  children: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.node), _propTypes2.default.node]),
  connectDragPreview: _propTypes2.default.func.isRequired,
  connectDragSource: _propTypes2.default.func.isRequired,
  displayPosition: _propTypes2.default.number.isRequired,
  draggingOver: _propTypes2.default.bool.isRequired,
  draggingAboveHalf: _propTypes2.default.bool.isRequired,
  entry: _reactImmutableProptypes2.default.record.isRequired,
  quizEntryWithErrorsShowing: _reactImmutableProptypes2.default.record,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  quizEntry: _reactImmutableProptypes2.default.record.isRequired,
  quizId: _propTypes2.default.string.isRequired,
  removeQuizEntry: _propTypes2.default.func.isRequired, // from connected component
  screenreaderNotification: _propTypes2.default.func.isRequired, // from connected component
  scrollKey: _propTypes2.default.string,
  switchOnEditing: _propTypes2.default.func.isRequired, // from QuizEntry
  withConfirm: _propTypes2.default.func.isRequired,
  editQuizEntryToggle: _propTypes2.default.bool.isRequired
}, _class2.defaultProps = {
  children: null,
  draggingOver: false,
  draggingAboveHalf: false,
  quizEntryWithErrorsShowing: null,
  scrollKey: void 0,
  editQuizEntryToggle: false
}, _temp2)) || _class);
var QuizEntryShowRaw = exports.QuizEntryShowRaw = QuizEntryShow;

exports.default = (0, _reactDnd.DragSource)(_dragAndDropUtils.ItemTypes.QUIZ_ENTRY, _dragAndDropUtils.dragSource, _dragAndDropUtils.collectDrag)(QuizEntryShow);