var _excluded = ["component", "isRequired"];
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); }
import React from "react";
import { reduxForm } from "redux-form";
import { startCase } from "lodash";
import { withProps } from "recompose";
import { InputField, wrapDialog } from "teselagen-react-components";
import { compose } from "redux";
import { Button, Intent, Classes } from "@blueprintjs/core";
import classNames from "classnames";
import "./simpleDialog.css";
import { tryToRefocusEditor } from "../utils/editorUtils";

// 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.createElement("form", {
      onSubmit: handleSubmit(function (data) {
        if (onSubmit) onSubmit(data);
        hideModal();
        tryToRefocusEditor();
      }),
      className: classNames(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 || InputField;
      var fieldProps = _extends({
        autoFocus: i === 0
      }, props, extraProps[props.name]);
      fieldProps.label = fieldProps.label || startCase(fieldProps.name) + ":";
      if (isRequired) fieldProps.validate = required;
      return /*#__PURE__*/React.createElement(FieldComp, _extends({
        key: field.name
      }, fieldProps));
    }), /*#__PURE__*/React.createElement("div", {
      className: "dialog-buttons"
    }, showCancel && /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        hideModal();
        tryToRefocusEditor();
      },
      text: "Cancel"
    }), /*#__PURE__*/React.createElement(Button, {
      type: "submit",
      intent: Intent.PRIMARY,
      text: buttonText,
      disabled: invalid
    })));
  };
  return SimpleGenericDialogForm;
}(React.Component);
function required(val) {
  if (!val) return "Required";
}
export default function createSimpleDialog(props) {
  return compose(wrapDialog(_extends({
    isDraggable: true,
    width: 400
  }, props.withDialogProps)), reduxForm({
    form: props.formName
  }), withProps(props))(SimpleGenericDialogForm);
}