'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,
    _default_enAU_number = Intl.NumberFormat('en-AU', { 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 _util = require('quiz-interactions/lib/components/formula/common/util');

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _generateDisplayPositions = require('../../../../common/util/generateDisplayPositions');

var _generateDisplayPositions2 = _interopRequireDefault(_generateDisplayPositions);

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

var _extractTextFromHtml2 = _interopRequireDefault(_extractTextFromHtml);

var _SidebarItem = require('./SidebarItem');

var _SidebarItem2 = _interopRequireDefault(_SidebarItem);

var _Stimulus = require('../../../../common/components/layout/sidebar/Stimulus');

var _Stimulus2 = _interopRequireDefault(_Stimulus);

var _Sidebar = require('../../../../common/components/layout/sidebar/Sidebar');

var _Sidebar2 = _interopRequireDefault(_Sidebar);

var _windowChecks = require('../../../../common/util/windowChecks');

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

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

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

  function TakingSidebar() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = TakingSidebar.__proto__ || Object.getPrototypeOf(TakingSidebar)).call.apply(_ref, [this].concat(args))), _this), _this.scrollToItem = function (position) {
      return function (itemId) {
        if (_this.props.isOneQuestionAtATime) {
          var message = 'Current item changed to item ' + _default_enAU_number(position);

          _this.props.goToItem(position || 1);
          _this.props.screenreaderNotification(message);
        } else {
          _this.props.scrollToItem(itemId);
        }
        if ((0, _windowChecks.onPhone)() && _this.props.sidebarOpen) {
          _this.props.toggleSidebar();
        }
      };
    }, _this.renderSidebarItem = function (sessionItem, _displayPosition) {
      var item = sessionItem.getItem();
      var key = _this.generateItemKey(item.id);
      var sidebarOpen = _this.props.sidebarOpen;

      return _react2.default.createElement(_SidebarItem2.default, {
        inStimulus: sessionItem.isStimulus,
        itemBody: _this.getItemBody(item),
        itemId: item.id,
        itemName: item.getInteractionType().name,
        key: key,
        pointsPossible: sessionItem.pointsPossible,
        position: sessionItem.position,
        quizEntryId: sessionItem.id,
        ref: _this.generateItemKey(item.id),
        scrollToItem: _this.scrollToItem(sessionItem.position),
        sessionItem: sessionItem,
        sidebarOpen: sidebarOpen
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(TakingSidebar, [{
    key: 'generateItemKey',
    value: function generateItemKey(id) {
      return _constants.SIDEBAR_ITEM_REF + '_' + id;
    }
  }, {
    key: 'setRef',
    value: function setRef(key) {
      var _this2 = this;

      return function (node) {
        _this2[key] = node;
      };
    }
  }, {
    key: 'getItemBody',
    value: function getItemBody(item) {
      var itemBody = item.itemBody;
      if (item.getInteractionType().slug === _constants.FORMULA_SLUG) {
        var variables = item.toJS().interactionData.variables;
        itemBody = (0, _util.substituteVars)(itemBody, variables);
      }
      return (0, _extractTextFromHtml2.default)(itemBody || '');
    }
  }, {
    key: 'renderSidebarItems',
    value: function renderSidebarItems() {
      var _this3 = this;

      var sessionItems = this.props.sessionItems;

      return (0, _generateDisplayPositions2.default)(sessionItems).map(function (si) {
        var sessionItem = si.record;
        var sidebarOpen = _this3.props.sidebarOpen;


        if (sessionItem.isStimulus) {
          var key = _this3.generateItemKey(si.record.id);
          return _react2.default.createElement(_Stimulus2.default, {
            displayPosition: si.displayPosition,
            itemId: sessionItem.id,
            key: key,
            ref: _this3.setRef(key),
            renderSidebarItem: _this3.renderSidebarItem,
            scrollToItem: _this3.scrollToItem(sessionItem.position),
            sidebarOpen: sidebarOpen,
            stimulusParent: sessionItem
          });
        }

        return _react2.default.createElement(
          'div',
          { key: sessionItem.id },
          _this3.renderSidebarItem(sessionItem)
        );
      });
    }
  }, {
    key: 'render',
    value: function render() {
      if (this.props.sessionNotLoaded || this.props.isOneQuestionAtATime && !this.props.allowBacktracking) {
        return null;
      }

      return _react2.default.createElement(
        _Sidebar2.default,
        {
          taking: true,
          role: this.props.role,
          title: this.props.quizTitle,
          pinnedItems: this.props.pinnedItems,
          scrollToItem: this.scrollToItem,
          sidebarOpen: this.props.sidebarOpen,
          toggleSidebar: this.props.toggleSidebar,
          screenreaderNotification: this.props.screenreaderNotification
        },
        this.renderSidebarItems()
      );
    }
  }]);
  TakingSidebar.displayName = 'TakingSidebar'
  ;
  return TakingSidebar;
}(_react.Component), _class.propTypes = {
  allowBacktracking: _propTypes2.default.bool.isRequired,
  quizTitle: _propTypes2.default.string.isRequired,
  goToItem: _propTypes2.default.func.isRequired,
  isOneQuestionAtATime: _propTypes2.default.bool.isRequired,
  pinnedItems: _reactImmutableProptypes2.default.listOf(_propTypes2.default.shape({
    sessionItemId: _propTypes2.default.string.isRequired,
    position: _propTypes2.default.number.isRequired
  })).isRequired,
  role: _propTypes2.default.string,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  scrollToItem: _propTypes2.default.func.isRequired,
  sessionItems: _reactImmutableProptypes2.default.list.isRequired,
  sessionNotLoaded: _propTypes2.default.bool.isRequired,
  sidebarOpen: _propTypes2.default.bool,
  toggleSidebar: _propTypes2.default.func.isRequired
}, _class.defaultProps = {
  role: 'navigation',
  sidebarOpen: false
}, _temp2);
exports.default = TakingSidebar;