"use strict";

exports.__esModule = true;
exports["default"] = createSimpleDialog;
var _react = _interopRequireDefault(require("react"));
var _reduxForm = require("redux-form");
var _lodash = require("lodash");
var _recompose = require("recompose");
var _teselagenReactComponents = require("teselagen-react-components");
var _redux = require("redux");
var _core = require("@blueprintjs/core");
var _classnames = _interopRequireDefault(require("classnames"));
require("./simpleDialog.css");
var _editorUtils = require("../utils/editorUtils");
var _excluded = ["component", "isRequired"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
// TODO: move to TRC
var SimpleGenericDialogForm = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(SimpleGenericDialogForm, _React$Component);
  function SimpleGenericDialogForm() {
    return _React$Component.apply(this, arguments) || this;
  }
  var _proto = SimpleGenericDialogForm.prototype;
  _proto.render = function render() {
    var _this$props = this.props,
      hideModal = _this$props.hideModal,
      handleSubmit = _this$props.handleSubmit,
      fields = _this$props.fields,
      _this$props$buttonTex = _this$props.buttonText,
      buttonText = _this$props$buttonTex === void 0 ? "OK" : _this$props$buttonTex,
      _this$props$showCance = _this$props.showCancel,
      showCancel = _this$props$showCance === void 0 ? true : _this$props$showCance,
      onSubmit = _this$props.onSubmit,
      invalid = _this$props.invalid,
      _this$props$extraProp = _this$props.extraProps,
      extraProps = _this$props$extraProp === void 0 ? {} : _this$props$extraProp;
    return /*#__PURE__*/_react["default"].createElement("form", {
      onSubmit: handleSubmit(function (data) {
        if (onSubmit) onSubmit(data);
        hideModal();
        (0, _editorUtils.tryToRefocusEditor)();
      }),
      className: (0, _classnames["default"])(_core.Classes.DIALOG_BODY, "tg-min-width-dialog simple-dialog")
    }, fields.map(function (field, i) {
      var component = field.component,
        isRequired = field.isRequired,
        props = _objectWithoutPropertiesLoose(field, _excluded);
      var FieldComp = component || _teselagenReactComponents.InputField;
      var fieldProps = _extends({
        autoFocus: i === 0
      }, props, extraProps[props.name]);
      fieldProps.label = fieldProps.label || (0, _lodash.startCase)(fieldProps.name) + ":";
      if (isRequired) fieldProps.validate = required;
      return /*#__PURE__*/_react["default"].createElement(FieldComp, _extends({
        key: field.name
      }, fieldProps));
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "dialog-buttons"
    }, showCancel && /*#__PURE__*/_react["default"].createElement(_core.Button, {
      onClick: function onClick() {
        hideModal();
        (0, _editorUtils.tryToRefocusEditor)();
      },
      text: "Cancel"
    }), /*#__PURE__*/_react["default"].createElement(_core.Button, {
      type: "submit",
      intent: _core.Intent.PRIMARY,
      text: buttonText,
      disabled: invalid
    })));
  };
  return SimpleGenericDialogForm;
}(_react["default"].Component);
function required(val) {
  if (!val) return "Required";
}
function createSimpleDialog(props) {
  return (0, _redux.compose)((0, _teselagenReactComponents.wrapDialog)(_extends({
    isDraggable: true,
    width: 400
  }, props.withDialogProps)), (0, _reduxForm.reduxForm)({
    form: props.formName
  }), (0, _recompose.withProps)(props))(SimpleGenericDialogForm);
}
module.exports = exports.default;