"use strict";

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

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

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

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 _debounce = _interopRequireDefault(require("lodash/debounce"));

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

var _maskMarkup = require("@pie-lib/pie-toolbox/mask-markup");

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

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

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

var _translator = _interopRequireDefault(require("@pie-lib/pie-toolbox/translator"));

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 translator = _translator["default"].translator;

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

  var _super = _createSuper(Main);

  function Main() {
    var _this;

    (0, _classCallCheck2["default"])(this, Main);

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

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", {
      showCorrectAnswer: _this.props.alwaysShowCorrect || false,
      value: _this.props.value
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getChangeSession", function (maxLengthPerChoice) {
      return maxLengthPerChoice && maxLengthPerChoice.every(function (val, i, arr) {
        return val === arr[0] && val === 1;
      }) ? _this.props.onChange : (0, _debounce["default"])(_this.props.onChange, 1500, {
        maxWait: 1500
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "toggleShowCorrect", function () {
      _this.setState({
        showCorrectAnswer: !_this.state.showCorrectAnswer
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "changeSession", _this.getChangeSession(_this.props.maxLengthPerChoice));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChange", function (value) {
      _this.setState({
        value: value
      });

      _this.changeSession(value);
    });
    return _this;
  }

  (0, _createClass2["default"])(Main, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var _this$props = this.props,
          maxLengthPerChoice = _this$props.maxLengthPerChoice,
          language = _this$props.language;
      var note = this.props.note;

      if ((0, _isEmpty["default"])(nextProps.feedback)) {
        this.setState({
          showCorrectAnswer: false
        });
      }

      if (nextProps.alwaysShowCorrect) {
        this.setState({
          showCorrectAnswer: true
        });
      }

      if (maxLengthPerChoice && !(0, _isEqual["default"])(maxLengthPerChoice, nextProps.maxLengthPerChoice)) {
        this.changeSession = this.getChangeSession(nextProps.maxLengthPerChoice);
      } // check if the note is the default one for prev language and change to the default one for new language
      // this check is necessary in order to diferanciate between default and authour defined note
      // and only change between languages for default ones


      if (note && language && language !== nextProps.language && note === translator.t('common:commonCorrectAnswerWithAlternates', {
        lng: language
      })) {
        note = translator.t('common:commonCorrectAnswerWithAlternates', {
          lng: nextProps.language
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          showCorrectAnswer = _this$state.showCorrectAnswer,
          value = _this$state.value;
      var _this$props2 = this.props,
          alwaysShowCorrect = _this$props2.alwaysShowCorrect,
          animationsDisabled = _this$props2.animationsDisabled,
          classes = _this$props2.classes,
          mode = _this$props2.mode,
          displayType = _this$props2.displayType,
          role = _this$props2.role,
          language = _this$props2.language,
          maxLengthPerChoice = _this$props2.maxLengthPerChoice,
          maxLengthPerChoiceEnabled = _this$props2.maxLengthPerChoiceEnabled,
          note = _this$props2.note,
          playerSpellCheckEnabled = _this$props2.playerSpellCheckEnabled,
          prompt = _this$props2.prompt,
          rationale = _this$props2.rationale,
          showNote = _this$props2.showNote,
          teacherInstructions = _this$props2.teacherInstructions,
          responseAreaInputConfiguration = _this$props2.responseAreaInputConfiguration,
          model = _this$props2.model;

      var _ref = model || {},
          extraCSSRules = _ref.extraCSSRules;

      var displayNote = (showCorrectAnswer || mode === 'view' && role === 'instructor') && showNote && note;
      var mainClasses = (0, _classnames["default"])([classes.mainContainer, (0, _defineProperty2["default"])({}, classes.noBorderColor, alwaysShowCorrect)]);

      var teacherInstructionsDiv = /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        defaultClassName: "teacher-instructions",
        prompt: teacherInstructions
      });

      var rationaleDiv = /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: rationale
      });

      return /*#__PURE__*/_react["default"].createElement(_renderUi.UiLayout, {
        extraCSSRules: extraCSSRules,
        className: mainClasses,
        style: {
          display: "".concat(displayType)
        }
      }, mode === 'gather' && /*#__PURE__*/_react["default"].createElement("h2", {
        className: classes.srOnly
      }, "Fill in the Blank Question"), teacherInstructions && (0, _renderUi.hasText)(teacherInstructions) && /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.collapsible
      }, !animationsDisabled ? /*#__PURE__*/_react["default"].createElement(_renderUi.Collapsible, {
        labels: {
          hidden: 'Show Teacher Instructions',
          visible: 'Hide Teacher Instructions'
        }
      }, teacherInstructionsDiv) : teacherInstructionsDiv), prompt && /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
        prompt: prompt
      }), !alwaysShowCorrect && /*#__PURE__*/_react["default"].createElement(_correctAnswerToggle.CorrectAnswerToggle, {
        show: mode === 'evaluate',
        toggled: showCorrectAnswer,
        onToggle: this.toggleShowCorrect,
        language: language
      }), /*#__PURE__*/_react["default"].createElement(_maskMarkup.ConstructedResponse, (0, _extends2["default"])({}, this.props, {
        onChange: this.onChange,
        showCorrectAnswer: showCorrectAnswer,
        value: value,
        maxLength: maxLengthPerChoice,
        adjustedLimit: maxLengthPerChoiceEnabled,
        spellCheck: playerSpellCheckEnabled,
        pluginProps: responseAreaInputConfiguration
      })), displayNote && /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(classes.note, 'note'),
        dangerouslySetInnerHTML: {
          __html: note
        }
      }), rationale && (0, _renderUi.hasText)(rationale) && /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.collapsible
      }, !animationsDisabled ? /*#__PURE__*/_react["default"].createElement(_renderUi.Collapsible, {
        labels: {
          hidden: 'Show Rationale',
          visible: 'Hide Rationale'
        }
      }, rationaleDiv) : rationaleDiv));
    }
  }]);
  return Main;
}(_react["default"].Component);

exports.Main = Main;
(0, _defineProperty2["default"])(Main, "propTypes", {
  alwaysShowCorrect: _propTypes["default"].bool,
  animationsDisabled: _propTypes["default"].bool,
  classes: _propTypes["default"].object.isRequired,
  disabled: _propTypes["default"].bool,
  displayType: _propTypes["default"].string,
  feedback: _propTypes["default"].object,
  language: _propTypes["default"].string,
  markup: _propTypes["default"].string,
  maxLengthPerChoice: _propTypes["default"].array,
  maxLengthPerChoiceEnabled: _propTypes["default"].bool,
  mode: _propTypes["default"].string,
  note: _propTypes["default"].string,
  onChange: _propTypes["default"].func,
  playerSpellCheckEnabled: _propTypes["default"].bool,
  prompt: _propTypes["default"].string,
  rationale: _propTypes["default"].string,
  role: _propTypes["default"].string,
  showNote: _propTypes["default"].bool,
  teacherInstructions: _propTypes["default"].string,
  value: _propTypes["default"].object,
  responseAreaInputConfiguration: _propTypes["default"].object,
  model: _propTypes["default"].object.isRequired
});
(0, _defineProperty2["default"])(Main, "defaultProps", {
  value: {}
});

var styles = function styles(theme) {
  return {
    mainContainer: {
      color: _renderUi.color.text(),
      backgroundColor: _renderUi.color.background()
    },
    inlineDisplay: {
      display: 'inline-block'
    },
    blockDisplay: {
      display: 'block'
    },
    note: {
      marginBottom: theme.spacing.unit * 2
    },
    collapsible: {
      marginBottom: theme.spacing.unit * 2
    },
    noBorderColor: {
      '& *': {
        borderColor: "".concat(_renderUi.color.text(), " !important")
      }
    },
    srOnly: {
      position: 'absolute',
      left: '-10000px',
      top: 'auto',
      width: '1px',
      height: '1px',
      overflow: 'hidden'
    }
  };
};

var _default = (0, _styles.withStyles)(styles)(Main);

exports["default"] = _default;
//# sourceMappingURL=main.js.map