"use strict";

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

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

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

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 _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _immutable = require("immutable");

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

var _FormFieldLabel = _interopRequireDefault(require("@instructure/ui-forms/lib/components/FormField/FormFieldLabel"));

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

var _FormFieldGroup = _interopRequireDefault(require("@instructure/ui-forms/lib/components/FormFieldGroup"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

var _RichContentInput = _interopRequireDefault(require("../../../RichContentInput"));

var _instUIMessages = require("../../../../util/instUIMessages");

var _Errors = _interopRequireDefault(require("../../../shared/errors/Errors"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _theme = _interopRequireDefault(require("./theme"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'doNBF',
  template: function template(theme) {
    return "\n\n.doNBF_daCg{padding-bottom:".concat(theme.sectionPadding || 'inherit', "}\n\n.doNBF_fvFs{color:").concat(theme.optionalLabelColor || 'inherit', ";font-size:").concat(theme.optionalLabelFontSize || 'inherit', ";margin-left:").concat(theme.urlLabelMargin || 'inherit', "}");
  },
  'section': 'doNBF_daCg',
  'optionalLabel': 'doNBF_fvFs'
};
var StimulusEditInfo = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(StimulusEditInfo, _Component);

  function StimulusEditInfo() {
    var _getPrototypeOf2;

    var _this;

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

    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)(StimulusEditInfo)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.inputErrors = function (field) {
      if (_this.props.errorsShowing) {
        var errorMap = _this.props.workingStimulus.getErrors().toJS() || {};
        return (0, _instUIMessages.toErrors)(errorMap[field] || []);
      }
    };

    _this.onTextInputChange = function (field) {
      return function (e) {
        var newData = _this.props.workingStimulus.set(field, e.target.value);

        _this.props.modifyStimulus(newData);
      };
    };

    _this.onBodyChange = function (e, _ref) {
      var editorContent = _ref.editorContent;

      var newData = _this.props.workingStimulus.set('body', editorContent);

      _this.props.modifyStimulus(newData);
    };

    _this.errorsList = function (field) {
      if (_this.props.errorsShowing) {
        var errorMap = _this.props.workingStimulus.getErrors() || (0, _immutable.Map)();
        return errorMap.get(field) || (0, _immutable.List)();
      }
    };

    _this.fileUploadUrl = function () {
      var _this$props = _this.props,
          stimulus = _this$props.stimulus,
          parentId = _this$props.parentId,
          parentType = _this$props.parentType;
      var resource = parentType === 'bank' ? 'banks' : 'quizzes';
      return "/api/".concat(resource, "/").concat(parentId, "/stimuli/").concat(stimulus.id, "/media_upload_url");
    };

    return _this;
  }

  (0, _createClass2.default)(StimulusEditInfo, [{
    key: "headerText",
    value: function headerText() {
      return this.props.stimulus.isTemporary() ? "L\xE4gg till stimulans" : "Redigera stimulans";
    }
  }, {
    key: "renderOptionalLabel",
    value: function renderOptionalLabel(labelName) {
      var optionalText = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "(alternativ)";
      return _react.default.createElement("div", null, _react.default.createElement("span", null, labelName), _react.default.createElement("span", {
        className: styles.optionalLabel
      }, optionalText));
    }
  }, {
    key: "render",
    value: function render() {
      var stimulus = this.props.workingStimulus;
      return _react.default.createElement(_FormFieldGroup.default, {
        messages: this.inputErrors('onSelf'),
        description: _react.default.createElement(_ScreenReaderContent.default, null, "Redigera stimulans")
      }, _react.default.createElement(_Heading.default, {
        level: "h3"
      }, this.headerText()), _react.default.createElement("div", {
        className: styles.section
      }, _react.default.createElement(_TextInput.default, {
        messages: this.inputErrors('title'),
        label: "Titel",
        type: "text",
        ref: this.props.setTitleRef,
        onChange: this.onTextInputChange('title'),
        value: stimulus.get('title')
      })), _react.default.createElement("div", {
        className: styles.section
      }, _react.default.createElement(_TextInput.default, {
        label: this.renderOptionalLabel("Instruktioner"),
        type: "text",
        onChange: this.onTextInputChange('instructions'),
        value: stimulus.get('instructions')
      })), _react.default.createElement("div", {
        className: styles.section
      }, _react.default.createElement(_FormFieldLabel.default, null, "Inneh\xE5ll"), _react.default.createElement(_RichContentInput.default, {
        label: "" // eslint-disable-next-line react/jsx-no-literals
        ,
        textareaId: "stimulusBodyEdit_".concat(stimulus.id, "_").concat(this.props.isBankedContent),
        defaultContent: stimulus.get('body'),
        onKeyUp: this.onBodyChange,
        onBlur: this.onBodyChange,
        uploadPath: this.fileUploadUrl()
      }), _react.default.createElement(_Errors.default, {
        errorList: this.errorsList('body')
      })), _react.default.createElement("div", {
        className: styles.section
      }, _react.default.createElement(_TextInput.default, {
        label: this.renderOptionalLabel("URL k\xE4lla", "(Alternativ - ej synlig f\xF6r studenter)"),
        type: "text",
        onChange: this.onTextInputChange('sourceUrl'),
        value: stimulus.get('sourceUrl')
      })));
    }
  }]);
  StimulusEditInfo.displayName = "StimulusEditInfo";
  return StimulusEditInfo;
}(_react.Component), _class2.propTypes = {
  isBankedContent: _propTypes.default.bool,
  stimulus: _reactImmutableProptypes.default.record.isRequired,
  workingStimulus: _reactImmutableProptypes.default.record.isRequired,
  errorsShowing: _propTypes.default.bool.isRequired,
  modifyStimulus: _propTypes.default.func.isRequired,
  parentId: _propTypes.default.string.isRequired,
  parentType: _propTypes.default.string.isRequired,
  setTitleRef: _propTypes.default.func
}, _class2.defaultProps = {
  isBankedContent: false,
  setTitleRef: function setTitleRef() {}
}, _temp)) || _class);
exports.default = StimulusEditInfo;