"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _View = _interopRequireDefault(require("@instructure/ui-layout/lib/components/View"));

var _learnosity = require("../../../taking/learnosity");

var LearnosityCalculator =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(LearnosityCalculator, _Component);

  function LearnosityCalculator() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, LearnosityCalculator);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(LearnosityCalculator)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      learnosityCalculatorReady: false,
      featureId: _this.props.featureId || "calc_".concat(new Date().getTime())
    };

    _this.handleClick = function () {
      _this.toggle();

      _this.props.setActiveCalculatorId(_this.state.featureId);
    };

    return _this;
  }

  (0, _createClass2.default)(LearnosityCalculator, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.calculatorType !== 'none') {
        this.initializeLearnosity();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.destroyLearnosity();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.props.calculatorType !== 'none' && (prevProps.calculatorType === 'none' || this.props.quizSessionId && !prevProps.quizSessionId)) {
        this.initializeLearnosity();
      } else if (this.props.calculatorType === 'none' && prevProps.calculatorType !== 'none') {
        this.destroyLearnosity();
      } else if (this.props.activeCalculatorId !== prevProps.activeCalculatorId && this.props.activeCalculatorId !== this.state.featureId) {
        this.toggle(false);
      }
    }
  }, {
    key: "initializeLearnosity",
    value: function () {
      var _initializeLearnosity = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (this.props.quizSessionId) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt("return");

              case 2:
                _context.next = 4;
                return this.props.appendFeature(this.props.quizSessionId, {
                  type: 'calculator',
                  mode: this.props.calculatorType,
                  feature_id: this.state.featureId
                });

              case 4:
                if (!(this.props.calculatorType === 'none')) {
                  _context.next = 6;
                  break;
                }

                return _context.abrupt("return");

              case 6:
                this.setState({
                  learnosityCalculatorReady: true
                });

              case 7:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function initializeLearnosity() {
        return _initializeLearnosity.apply(this, arguments);
      }

      return initializeLearnosity;
    }()
  }, {
    key: "destroyLearnosity",
    value: function destroyLearnosity() {
      this.props.destroyFeature(this.state.featureId);
    }
  }, {
    key: "toggle",
    value: function toggle(onOff) {
      this.props.toggleFeature(this.state.featureId, onOff);
    }
  }, {
    key: "renderCalculator",
    value: function renderCalculator() {
      return _react.default.createElement(_View.default, {
        as: "div"
      }, this.props.render({
        toggleCalculator: this.handleClick,
        enabled: this.state.learnosityCalculatorReady
      }), _react.default.createElement("div", {
        hidden: true
      }, _react.default.createElement("span", {
        className: // eslint-disable-next-line react/jsx-no-literals
        "learnosity-feature feature-".concat(this.state.featureId)
      })));
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.props.quizSessionId || this.props.calculatorType === 'none') {
        return null;
      } else {
        return this.renderCalculator();
      }
    }
  }]);
  LearnosityCalculator.displayName = "LearnosityCalculator";
  return LearnosityCalculator;
}(_react.Component);

exports.default = LearnosityCalculator;
LearnosityCalculator.propTypes = {
  activeCalculatorId: _propTypes.default.string,
  appendFeature: _propTypes.default.func.isRequired,
  calculatorType: _propTypes.default.oneOf(['none', 'basic', 'scientific']),
  destroyFeature: _propTypes.default.func.isRequired,
  featureId: _propTypes.default.string,
  quizSessionId: _propTypes.default.string,
  render: _propTypes.default.func.isRequired,
  setActiveCalculatorId: _propTypes.default.func,
  toggleFeature: _propTypes.default.func.isRequired
};
LearnosityCalculator.defaultProps = {
  activeCalculatorId: null,
  appendFeature: _learnosity.appendFeature,
  calculatorType: 'none',
  destroyFeature: _learnosity.destroyFeature,
  featureId: null,
  quizSessionId: null,
  setActiveCalculatorId: Function.prototype,
  toggleFeature: _learnosity.toggleFeature
};