'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _dec, _class, _class2, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _immutable = require('immutable');

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

var _TextInput2 = _interopRequireDefault(_TextInput);

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

var _FormFieldLabel2 = _interopRequireDefault(_FormFieldLabel);

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

var _lib2 = _interopRequireDefault(_lib);

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

var _FormFieldGroup2 = _interopRequireDefault(_FormFieldGroup);

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

var _Heading2 = _interopRequireDefault(_Heading);

var _RichContentInput = require('../../../RichContentInput');

var _RichContentInput2 = _interopRequireDefault(_RichContentInput);

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

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

var _Errors2 = _interopRequireDefault(_Errors);

var _formatMessage = require('../../../../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.StimulusEditInfo__styles__section {\n  padding-bottom: ' + theme.sectionPadding + ';\n}\n\n.StimulusEditInfo__styles__optionalLabel {\n  color: ' + theme.optionalLabelColor + ';\n  font-size: ' + theme.optionalLabelFontSize + ';\n  margin-left: ' + theme.urlLabelMargin + ';\n}\n';
  },
  'section': 'StimulusEditInfo__styles__section',
  'optionalLabel': 'StimulusEditInfo__styles__optionalLabel'
};
var StimulusEditInfo = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(StimulusEditInfo, _Component);

  function StimulusEditInfo() {
    var _ref;

    var _temp, _this, _ret;

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

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = StimulusEditInfo.__proto__ || Object.getPrototypeOf(StimulusEditInfo)).call.apply(_ref, [this].concat(args))), _this), _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, _ref2) {
      var editorContent = _ref2.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/' + resource + '/' + parentId + '/stimuli/' + stimulus.id + '/media_upload_url';
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(StimulusEditInfo, [{
    key: 'headerText',
    value: function headerText() {
      return this.props.stimulus.isTemporary() ? 'Agregar est\xEDmulo' : 'Editar est\xEDmulo';
    }
  }, {
    key: 'renderOptionalLabel',
    value: function renderOptionalLabel(labelName) {
      var optionalText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '(opcional)';

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'span',
          null,
          labelName
        ),
        _react2.default.createElement(
          'span',
          { className: styles.optionalLabel },
          optionalText
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var stimulus = this.props.workingStimulus;
      return _react2.default.createElement(
        _FormFieldGroup2.default,
        { messages: this.inputErrors('onSelf') },
        _react2.default.createElement(
          _Heading2.default,
          { level: 'h3' },
          this.headerText()
        ),
        _react2.default.createElement(
          'div',
          { className: styles.section },
          _react2.default.createElement(_TextInput2.default, {
            messages: this.inputErrors('title'),
            label: 'T\xEDtulo',
            type: 'text',
            ref: this.props.setTitleRef,
            onChange: this.onTextInputChange('title'),
            value: stimulus.get('title')
          })
        ),
        _react2.default.createElement(
          'div',
          { className: styles.section },
          _react2.default.createElement(_TextInput2.default, {
            label: this.renderOptionalLabel('Instrucciones'),
            type: 'text',
            onChange: this.onTextInputChange('instructions'),
            value: stimulus.get('instructions')
          })
        ),
        _react2.default.createElement(
          'div',
          { className: styles.section },
          _react2.default.createElement(
            _FormFieldLabel2.default,
            null,
            'Contenido'
          ),
          _react2.default.createElement(_RichContentInput2.default, {
            textareaId: 'stimulusBodyEdit_' + stimulus.id + '_' + this.props.isBankedContent,
            defaultContent: stimulus.get('body'),
            onKeyUp: this.onBodyChange,
            onBlur: this.onBodyChange,
            uploadPath: this.fileUploadUrl()
          }),
          _react2.default.createElement(_Errors2.default, { errorList: this.errorsList('body') })
        ),
        _react2.default.createElement(
          'div',
          { className: styles.section },
          _react2.default.createElement(_TextInput2.default, {
            label: this.renderOptionalLabel('URL de origen', '(Opcional; no visible para los alumnos)'),
            type: 'text',
            onChange: this.onTextInputChange('sourceUrl'),
            value: stimulus.get('sourceUrl')
          })
        )
      );
    }
  }]);
  StimulusEditInfo.displayName = 'StimulusEditInfo'
  ;
  return StimulusEditInfo;
}(_react.Component), _class2.propTypes = {
  isBankedContent: _propTypes2.default.bool,
  stimulus: _reactImmutableProptypes2.default.record.isRequired,
  workingStimulus: _reactImmutableProptypes2.default.record.isRequired,
  errorsShowing: _propTypes2.default.bool.isRequired,
  modifyStimulus: _propTypes2.default.func.isRequired,
  parentId: _propTypes2.default.string.isRequired,
  parentType: _propTypes2.default.string.isRequired,
  setTitleRef: _propTypes2.default.func
}, _class2.defaultProps = {
  isBankedContent: false,
  setTitleRef: function setTitleRef() {}
}, _temp2)) || _class);
exports.default = StimulusEditInfo;