"use strict";

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

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

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

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

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

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

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

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

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

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

var _mathToolbar = require("@pie-lib/pie-toolbox/math-toolbar");

var _mathInput = require("@pie-lib/pie-toolbox/math-input");

var _classnames = _interopRequireDefault(require("classnames"));

var _styles = require("@material-ui/core/styles");

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

var _renderUi = require("@pie-lib/pie-toolbox/render-ui");

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var SimpleQuestionBlockRaw = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(SimpleQuestionBlockRaw, _React$Component);

  var _super = _createSuper(SimpleQuestionBlockRaw);

  function SimpleQuestionBlockRaw(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, SimpleQuestionBlockRaw);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "mathToolBarContainsTarget", function (e) {
      return document.getElementById(_this.mathToolBarId).contains(e.target);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleClick", function (e) {
      try {
        if (!_this.mathToolBarContainsTarget(e)) {
          _this.setState({
            showKeypad: false
          });
        }
      } catch (e) {// console.log(e.toString());
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFocus", function () {
      var onSubFieldFocus = _this.props.onSubFieldFocus;
      onSubFieldFocus(_this.mathToolBarId);
    });
    _this.mathToolBarId = "math-toolbar-".concat(new Date().getTime());
    return _this;
  }

  (0, _createClass2["default"])(SimpleQuestionBlockRaw, [{
    key: "render",
    value: function render() {
      var _cx;

      var _this$props = this.props,
          classes = _this$props.classes,
          model = _this$props.model,
          showCorrect = _this$props.showCorrect,
          session = _this$props.session,
          emptyResponse = _this$props.emptyResponse,
          onSimpleResponseChange = _this$props.onSimpleResponseChange,
          showKeypad = _this$props.showKeypad;

      var _ref = model || {},
          config = _ref.config,
          disabled = _ref.disabled,
          correctness = _ref.correctness;

      if (!config) {
        return;
      }

      var correct = correctness && correctness.correct;
      var responses = config.responses,
          equationEditor = config.equationEditor;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.expression,
        "data-keypad": true
      }, showCorrect || disabled ? /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(classes["static"], (_cx = {}, (0, _defineProperty2["default"])(_cx, classes.incorrect, !emptyResponse && !correct && !showCorrect), (0, _defineProperty2["default"])(_cx, classes.correct, !emptyResponse && (correct || showCorrect)), _cx))
      }, /*#__PURE__*/_react["default"].createElement(_mathInput.mq.Static, {
        latex: showCorrect ? responses && responses.length && responses[0].answer : session.response || ''
      })) : /*#__PURE__*/_react["default"].createElement("div", {
        id: this.mathToolBarId
      }, /*#__PURE__*/_react["default"].createElement(_mathToolbar.MathToolbar, {
        classNames: {
          editor: classes.responseEditor
        },
        latex: session.response,
        keypadMode: equationEditor,
        onChange: onSimpleResponseChange,
        onDone: function onDone() {},
        onFocus: this.onFocus,
        controlledKeypad: true,
        showKeypad: showKeypad,
        hideDoneButton: true
      })));
    }
  }]);
  return SimpleQuestionBlockRaw;
}(_react["default"].Component);

exports.SimpleQuestionBlockRaw = SimpleQuestionBlockRaw;
(0, _defineProperty2["default"])(SimpleQuestionBlockRaw, "propTypes", {
  classes: _propTypes["default"].object,
  onSimpleResponseChange: _propTypes["default"].func,
  model: _propTypes["default"].object.isRequired,
  emptyResponse: _propTypes["default"].bool,
  session: _propTypes["default"].object.isRequired,
  showCorrect: _propTypes["default"].bool,
  onSubFieldFocus: _propTypes["default"].func.isRequired,
  showKeypad: _propTypes["default"].bool.isRequired
});
var SimpleQuestionBlock = (0, _styles.withStyles)(function (theme) {
  return {
    responseEditor: {
      display: 'flex',
      justifyContent: 'center',
      width: 'auto',
      maxWidth: 'fit-content',
      height: 'auto',
      textAlign: 'left',
      padding: theme.spacing.unit,
      '&.mq-math-mode': {
        border: "1px solid ".concat(_renderUi.color.primaryLight())
      }
    },
    expression: {
      marginTop: theme.spacing.unit * 2,
      marginBottom: theme.spacing.unit * 2,
      padding: theme.spacing.unit
    },
    "static": {
      color: _renderUi.color.text(),
      background: _renderUi.color.background(),
      border: "1px solid ".concat(_renderUi.color.primaryLight()),
      width: 'fit-content',
      fontSize: '1rem',
      padding: theme.spacing.unit / 2,
      '& > .mq-math-mode': {
        '& > .mq-hasCursor': {
          '& > .mq-cursor': {
            display: 'none'
          }
        }
      }
    },
    correct: {
      border: "2px solid ".concat(_renderUi.color.correct(), " !important"),
      padding: theme.spacing.unit,
      letterSpacing: '0.5px'
    },
    incorrect: {
      border: "2px solid ".concat(_renderUi.color.incorrect(), " !important"),
      padding: theme.spacing.unit,
      letterSpacing: '0.5px'
    }
  };
})(SimpleQuestionBlockRaw);
var _default = SimpleQuestionBlock;
exports["default"] = _default;
//# sourceMappingURL=simple-question-block.js.map