"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Configure = 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 _styles = require("@material-ui/core/styles");

var _configUi = require("@pie-lib/pie-toolbox/config-ui");

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

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

var _editableHtml = require("@pie-lib/pie-toolbox/editable-html");

var _generalConfigBlock = _interopRequireDefault(require("./general-config-block"));

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

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-element:math-inline:configure');
var Panel = _configUi.settings.Panel,
    toggle = _configUi.settings.toggle,
    radio = _configUi.settings.radio,
    dropdown = _configUi.settings.dropdown;

var styles = function styles(theme) {
  return {
    promptHolder: {
      width: '100%',
      paddingTop: theme.spacing.unit * 2,
      marginBottom: theme.spacing.unit * 2
    },
    errorText: {
      fontSize: theme.typography.fontSize - 2,
      color: theme.palette.error.main,
      paddingTop: theme.spacing.unit
    }
  };
};

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

  var _super = _createSuper(Configure);

  function Configure() {
    var _this;

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

    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), "onChange", function (model) {
      _this.props.onModelChanged(model);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "changeTeacherInstructions", function (teacherInstructions) {
      _this.props.onModelChanged(_objectSpread(_objectSpread({}, _this.props.model), {}, {
        teacherInstructions: teacherInstructions
      }));
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFeedbackChange", function (feedback) {
      var _this$props = _this.props,
          model = _this$props.model,
          onModelChanged = _this$props.onModelChanged;
      model.feedback = feedback;
      onModelChanged(model);
    });
    return _this;
  }

  (0, _createClass2["default"])(Configure, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          classes = _this$props2.classes,
          model = _this$props2.model,
          imageSupport = _this$props2.imageSupport,
          onModelChanged = _this$props2.onModelChanged,
          configuration = _this$props2.configuration,
          onConfigurationChanged = _this$props2.onConfigurationChanged,
          uploadSoundSupport = _this$props2.uploadSoundSupport;

      var _ref = configuration || {},
          _ref$allowTrailingZer = _ref.allowTrailingZeros,
          allowTrailingZeros = _ref$allowTrailingZer === void 0 ? {} : _ref$allowTrailingZer,
          _ref$baseInputConfigu = _ref.baseInputConfiguration,
          baseInputConfiguration = _ref$baseInputConfigu === void 0 ? {} : _ref$baseInputConfigu,
          _ref$contentDimension = _ref.contentDimensions,
          contentDimensions = _ref$contentDimension === void 0 ? {} : _ref$contentDimension,
          _ref$feedback = _ref.feedback,
          feedback = _ref$feedback === void 0 ? {} : _ref$feedback,
          _ref$ignoreOrder = _ref.ignoreOrder,
          ignoreOrder = _ref$ignoreOrder === void 0 ? {} : _ref$ignoreOrder,
          _ref$maxImageWidth = _ref.maxImageWidth,
          maxImageWidth = _ref$maxImageWidth === void 0 ? {} : _ref$maxImageWidth,
          _ref$maxImageHeight = _ref.maxImageHeight,
          maxImageHeight = _ref$maxImageHeight === void 0 ? {} : _ref$maxImageHeight,
          _ref$prompt = _ref.prompt,
          prompt = _ref$prompt === void 0 ? {} : _ref$prompt,
          _ref$rationale = _ref.rationale,
          rationale = _ref$rationale === void 0 ? {} : _ref$rationale,
          _ref$responseType = _ref.responseType,
          responseType = _ref$responseType === void 0 ? {} : _ref$responseType,
          _ref$scoringType = _ref.scoringType,
          scoringType = _ref$scoringType === void 0 ? {} : _ref$scoringType,
          settingsPanelDisabled = _ref.settingsPanelDisabled,
          _ref$spellCheck = _ref.spellCheck,
          spellCheck = _ref$spellCheck === void 0 ? {} : _ref$spellCheck,
          _ref$studentInstructi = _ref.studentInstructions,
          studentInstructions = _ref$studentInstructi === void 0 ? {} : _ref$studentInstructi,
          _ref$teacherInstructi = _ref.teacherInstructions,
          teacherInstructions = _ref$teacherInstructi === void 0 ? {} : _ref$teacherInstructi,
          _ref$withRubric = _ref.withRubric,
          withRubric = _ref$withRubric === void 0 ? {} : _ref$withRubric,
          _ref$mathMlOptions = _ref.mathMlOptions,
          mathMlOptions = _ref$mathMlOptions === void 0 ? {} : _ref$mathMlOptions,
          _ref$language = _ref.language,
          language = _ref$language === void 0 ? {} : _ref$language,
          _ref$languageChoices = _ref.languageChoices,
          languageChoices = _ref$languageChoices === void 0 ? {} : _ref$languageChoices;

      var _ref2 = model || {},
          _ref2$errors = _ref2.errors,
          errors = _ref2$errors === void 0 ? {} : _ref2$errors,
          extraCSSRules = _ref2.extraCSSRules,
          feedbackEnabled = _ref2.feedbackEnabled,
          promptEnabled = _ref2.promptEnabled,
          rationaleEnabled = _ref2.rationaleEnabled,
          spellCheckEnabled = _ref2.spellCheckEnabled,
          teacherInstructionsEnabled = _ref2.teacherInstructionsEnabled,
          toolbarEditorPosition = _ref2.toolbarEditorPosition;

      var teacherInstructionsError = errors.teacherInstructions;
      log('[render] model', model);
      var toolbarOpts = {
        position: toolbarEditorPosition === 'top' ? 'top' : 'bottom'
      };
      var defaultImageMaxWidth = maxImageWidth && maxImageWidth.prompt;
      var defaultImageMaxHeight = maxImageHeight && maxImageHeight.prompt;
      var panelSettings = {
        responseType: responseType.settings && radio(responseType.label, [_utils.ResponseTypes.simple, _utils.ResponseTypes.advanced]),
        feedbackEnabled: feedback.settings && toggle(feedback.label),
        promptEnabled: prompt.settings && toggle(prompt.label),
        'language.enabled': language.settings && toggle(language.label, true),
        language: language.settings && language.enabled && dropdown(languageChoices.label, languageChoices.options)
      };
      var panelProperties = {
        teacherInstructionsEnabled: teacherInstructions.settings && toggle(teacherInstructions.label),
        studentInstructionsEnabled: studentInstructions.settings && toggle(studentInstructions.label),
        rationaleEnabled: rationale.settings && toggle(rationale.label),
        spellCheckEnabled: spellCheck.settings && toggle(spellCheck.label),
        scoringType: scoringType.settings && radio(scoringType.label, ['auto', 'rubric']),
        'ignoreOrder.enabled': ignoreOrder.settings && toggle(ignoreOrder.label),
        'allowTrailingZeros.enabled': allowTrailingZeros.settings && toggle(allowTrailingZeros.label),
        rubricEnabled: (withRubric === null || withRubric === void 0 ? void 0 : withRubric.settings) && toggle(withRubric === null || withRubric === void 0 ? void 0 : withRubric.label)
      };
      return /*#__PURE__*/_react["default"].createElement(_configUi.layout.ConfigLayout, {
        extraCSSRules: extraCSSRules,
        dimensions: contentDimensions,
        hideSettings: settingsPanelDisabled,
        settings: /*#__PURE__*/_react["default"].createElement(Panel, {
          model: model,
          configuration: configuration,
          onChangeModel: function onChangeModel(model) {
            return onModelChanged(model);
          },
          onChangeConfiguration: function onChangeConfiguration(config) {
            return onConfigurationChanged(config);
          },
          groups: {
            Settings: panelSettings,
            Properties: panelProperties
          }
        })
      }, teacherInstructionsEnabled && /*#__PURE__*/_react["default"].createElement(_configUi.InputContainer, {
        label: teacherInstructions.label,
        className: classes.promptHolder
      }, /*#__PURE__*/_react["default"].createElement(_editableHtml.EditableHtml, {
        className: classes.prompt,
        markup: model.teacherInstructions || '',
        onChange: this.changeTeacherInstructions,
        imageSupport: imageSupport,
        nonEmpty: false,
        error: teacherInstructionsError,
        toolbarOpts: toolbarOpts,
        pluginProps: (0, _utils.getPluginProps)(teacherInstructions === null || teacherInstructions === void 0 ? void 0 : teacherInstructions.inputConfiguration, baseInputConfiguration),
        spellCheck: spellCheckEnabled,
        maxImageWidth: maxImageWidth && maxImageWidth.teacherInstructions || defaultImageMaxWidth,
        maxImageHeight: maxImageHeight && maxImageHeight.teacherInstructions || defaultImageMaxHeight,
        uploadSoundSupport: uploadSoundSupport,
        languageCharactersProps: [{
          language: 'spanish'
        }, {
          language: 'special'
        }],
        mathMlOptions: mathMlOptions
      }), teacherInstructionsError && /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.errorText
      }, teacherInstructionsError)), /*#__PURE__*/_react["default"].createElement(_generalConfigBlock["default"], {
        imageSupport: imageSupport,
        uploadSoundSupport: uploadSoundSupport,
        model: model,
        configuration: configuration,
        onChange: this.onChange,
        rationaleEnabled: rationaleEnabled,
        promptEnabled: promptEnabled,
        toolbarOpts: toolbarOpts,
        spellCheck: spellCheckEnabled
      }), feedbackEnabled && /*#__PURE__*/_react["default"].createElement(_configUi.FeedbackConfig, {
        feedback: model.feedback,
        onChange: this.onFeedbackChange,
        toolbarOpts: toolbarOpts
      }));
    }
  }]);
  return Configure;
}(_react["default"].Component);

exports.Configure = Configure;
(0, _defineProperty2["default"])(Configure, "propTypes", {
  onModelChanged: _propTypes["default"].func,
  onConfigurationChanged: _propTypes["default"].func,
  classes: _propTypes["default"].object,
  model: _propTypes["default"].object.isRequired,
  configuration: _propTypes["default"].object.isRequired,
  imageSupport: _propTypes["default"].object,
  uploadSoundSupport: _propTypes["default"].object
});

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

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