"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Main = 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 _propTypes = _interopRequireDefault(require("prop-types"));

var _textSelect = require("@pie-lib/pie-toolbox/text-select");

var _correctAnswerToggle = require("@pie-lib/pie-toolbox/correct-answer-toggle");

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

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

var _utils = _interopRequireDefault(require("./utils"));

var _debug = _interopRequireDefault(require("debug"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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 log = (0, _debug["default"])('@pie-ui:select-text');
var Types = {
  model: _propTypes["default"].object,
  session: _propTypes["default"].object,
  onSelectionChange: _propTypes["default"].func.isRequired
};

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

  var _super = _createSuper(Main);

  function Main(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Main);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "toggleShowCorrect", function () {
      _this.setState({
        showCorrectAnswer: !_this.state.showCorrectAnswer
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "correctAnswer", function () {
      var model = _this.state.model;
      return model.tokens.filter(function (t) {
        return t.correct;
      });
    });
    _this.state = {
      showCorrectAnswer: _this.props.model.alwaysShowCorrect || false,
      model: (0, _utils["default"])(props.model)
    };
    return _this;
  }

  (0, _createClass2["default"])(Main, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      this.setState({
        showCorrectAnswer: !!nextProps.model.alwaysShowCorrect,
        model: (0, _utils["default"])(nextProps.model)
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          session = _this$props.session,
          onSelectionChange = _this$props.onSelectionChange,
          classes = _this$props.classes;
      var _this$state = this.state,
          showCorrectAnswer = _this$state.showCorrectAnswer,
          model = _this$state.model;
      var env = model.env;

      var _ref = env || {},
          mode = _ref.mode;

      var selectedTokens = showCorrectAnswer ? this.correctAnswer() : session.selectedTokens;
      log('[render] selectedTokens:', selectedTokens);
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.mainContainer
      }, model.teacherInstructions && (0, _renderUi.hasText)(model.teacherInstructions) && (!model.animationsDisabled ? /*#__PURE__*/_react["default"].createElement(_renderUi.Collapsible, {
        labels: {
          hidden: 'Show Teacher Instructions',
          visible: 'Hide Teacher Instructions'
        },
        className: classes.collapsible
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: model.teacherInstructions
      })) : /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.prompt
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: model.teacherInstructions
      }))), /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.prompt
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: model.prompt
      })), !model.alwaysShowCorrect && /*#__PURE__*/_react["default"].createElement(_correctAnswerToggle.CorrectAnswerToggle, {
        show: model.disabled && model.incorrect,
        toggled: showCorrectAnswer,
        onToggle: this.toggleShowCorrect,
        language: model.language
      }), /*#__PURE__*/_react["default"].createElement(_textSelect.TextSelect, {
        className: classes.textSelect,
        disabled: model.disabled,
        text: model.text,
        tokens: model.tokens,
        selectedTokens: selectedTokens,
        onChange: function onChange(selection) {
          var newSelections = selection.map(function (select) {
            var token = model.tokens.find(function (_ref2) {
              var start = _ref2.start,
                  end = _ref2.end;
              return select.start === start && select.end === end;
            }); // needed for getScore when tokens position is recalculated, to keep oldStart and oldEnd

            if (token) {
              return token;
            }

            return select;
          });
          onSelectionChange(newSelections);
        },
        highlightChoices: model.highlightChoices,
        maxNoOfSelections: model.maxSelections,
        animationsDisabled: model.animationsDisabled
      }), mode === 'evaluate' && /*#__PURE__*/_react["default"].createElement(_textSelect.Legend, {
        language: model.language
      }), model.rationale && (0, _renderUi.hasText)(model.rationale) && (!model.animationsDisabled ? /*#__PURE__*/_react["default"].createElement(_renderUi.Collapsible, {
        labels: {
          hidden: 'Show Rationale',
          visible: 'Hide Rationale'
        },
        className: classes.collapsible
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: model.rationale
      })) : /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.prompt
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: model.rationale
      }))), model.correctness && model.feedback && !showCorrectAnswer && /*#__PURE__*/_react["default"].createElement(_renderUi.Feedback, {
        correctness: model.correctness,
        feedback: model.feedback
      }));
    }
  }]);
  return Main;
}(_react["default"].Component);

exports.Main = Main;
(0, _defineProperty2["default"])(Main, "propTypes", _objectSpread(_objectSpread({}, Types), {}, {
  classes: _propTypes["default"].object.isRequired
}));
(0, _defineProperty2["default"])(Main, "defaultProps", {});
var StyledMain = (0, _styles.withStyles)(function (theme) {
  return {
    mainContainer: {
      color: _renderUi.color.text(),
      backgroundColor: _renderUi.color.background()
    },
    textSelect: {
      marginBottom: theme.spacing.unit,
      marginTop: theme.spacing.unit * 2,
      whiteSpace: 'normal'
    },
    prompt: {
      verticalAlign: 'middle',
      marginBottom: theme.spacing.unit
    },
    collapsible: {
      marginBottom: theme.spacing.unit * 2
    }
  };
})(Main);

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

  var _super2 = _createSuper(Stateful);

  function Stateful(props) {
    var _this2;

    (0, _classCallCheck2["default"])(this, Stateful);
    _this2 = _super2.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this2), "change", function (selectedTokens) {
      var session = _objectSpread(_objectSpread({}, _this2.state.session), {}, {
        selectedTokens: selectedTokens
      });

      _this2.setState({
        session: session
      }, function () {
        _this2.props.onSelectionChange(_this2.state.session.selectedTokens);
      });
    });
    _this2.state = {
      model: props.model,
      session: props.session
    };
    return _this2;
  }

  (0, _createClass2["default"])(Stateful, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      this.setState({
        model: nextProps.model,
        session: nextProps.session
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state2 = this.state,
          model = _this$state2.model,
          session = _this$state2.session;
      return /*#__PURE__*/_react["default"].createElement(StyledMain, {
        model: model,
        session: session,
        onSelectionChange: this.change
      });
    }
  }]);
  return Stateful;
}(_react["default"].Component);

exports["default"] = Stateful;
(0, _defineProperty2["default"])(Stateful, "propTypes", Types);
//# sourceMappingURL=main.js.map