'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _learnosityLoader = require('./learnosity-loader');

var _learnosityLoader2 = _interopRequireDefault(_learnosityLoader);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _Button2 = _interopRequireDefault(_Button);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

var _IconCalculator = require('@instructure/ui-icons/lib/Line/IconCalculator');

var _IconCalculator2 = _interopRequireDefault(_IconCalculator);

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

var screenReaderOptions = ['\u0641\u062A\u062D \u0627\u0644\u062D\u0627\u0633\u0628\u0629', '\u0625\u063A\u0644\u0627\u0642 \u0627\u0644\u062D\u0627\u0633\u0628\u0629'];

var Calculator = (_temp = _class = function (_Component) {
  (0, _inherits3.default)(Calculator, _Component);

  function Calculator(props) {
    (0, _classCallCheck3.default)(this, Calculator);

    var _this = (0, _possibleConstructorReturn3.default)(this, (Calculator.__proto__ || Object.getPrototypeOf(Calculator)).call(this, props));

    _this.state = { featureId: 'calc_' + new Date().getTime(), calculatorStatus: 0 };
    _this.clickLearnosityButton = _this.clickLearnosityButton.bind(_this);
    return _this;
  }

  (0, _createClass3.default)(Calculator, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (this.props.calculatorType !== 'none') {
        this.loadLearnosityScripts();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      if (this.questionsApp) {
        this.questionsApp.feature(this.state.featureId).destroy();
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      if (this.props.calculatorType !== 'none' && prevProps.calculatorType === 'none') {
        this.loadLearnosityScripts();
      } else if (this.props.calculatorType === 'none' && prevProps.calculatorType !== 'none' && this.questionsApp) {
        this.questionsApp.feature(this.state.featureId).destroy();
      }
    }
  }, {
    key: 'loadLearnosityScripts',
    value: function () {
      var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return (0, _learnosityLoader2.default)(this.props.quizSessionId);

              case 2:
                this.questionsApp = _context.sent;

                this.questionsApp.append({ features: [{ type: 'calculator', mode: this.props.calculatorType, feature_id: this.state.featureId }] });

              case 4:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function loadLearnosityScripts() {
        return _ref.apply(this, arguments);
      }

      return loadLearnosityScripts;
    }()
  }, {
    key: 'updateCalculatorState',
    value: function updateCalculatorState() {
      this.setState(function (prevState) {
        return { calculatorStatus: (prevState.calculatorStatus + 1) % 2 };
      });
    }
  }, {
    key: 'clickLearnosityButton',
    value: function clickLearnosityButton() {
      var calcButton = document.getElementById('learnosityButton').getElementsByClassName('lrn_btn lrn-calc-toggle')[0];
      if (this.questionsApp && calcButton) {
        calcButton.click();
        this.updateCalculatorState();
      }
    }
  }, {
    key: 'renderCalculator',
    value: function renderCalculator() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Button2.default,
          {
            icon: _IconCalculator2.default,
            onClick: this.clickLearnosityButton },
          _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            screenReaderOptions[this.state.calculatorStatus]
          )
        ),
        _react2.default.createElement(
          'div',
          { hidden: true, id: 'learnosityButton' },
          _react2.default.createElement('span', { className: 'learnosity-feature feature-' + this.state.featureId })
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      if (this.props.calculatorType === 'none') {
        return null;
      } else {
        return this.renderCalculator();
      }
    }
  }]);
  Calculator.displayName = 'Calculator'
  ;
  return Calculator;
}(_react.Component), _class.propTypes = {
  calculatorType: _propTypes2.default.string.isRequired,
  quizSessionId: _propTypes2.default.number
}, _class.defaultProps = {
  calculatorType: 'none',
  quizSessionId: 0
}, _temp);
exports.default = Calculator;