"use strict";

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

var React = _interopRequireWildcard(require("react"));

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

var _reactTippy = require("react-tippy");

var _formik = require("formik");

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var Yup = _interopRequireWildcard(require("yup"));

var _ButtonBase = _interopRequireDefault(require("@material-ui/core/ButtonBase"));

var _withStyles = _interopRequireDefault(require("../../hoc/withStyles"));

var _ErrorBoundary = _interopRequireDefault(require("../../hoc/ErrorBoundary"));

var _TextField = _interopRequireDefault(require("../../bosons/TextField"));

var _Button = _interopRequireDefault(require("../../bosons/Button"));

var _FieldLabel = _interopRequireDefault(require("../../bosons/FieldLabel"));

var _styles = _interopRequireDefault(require("./styles"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var EmailInputFields =
/*#__PURE__*/
function (_React$Component) {
  _inherits(EmailInputFields, _React$Component);

  function EmailInputFields() {
    _classCallCheck(this, EmailInputFields);

    return _possibleConstructorReturn(this, _getPrototypeOf(EmailInputFields).apply(this, arguments));
  }

  _createClass(EmailInputFields, [{
    key: "_renderToField",
    value: function _renderToField() {
      var _this = this;

      return React.createElement(EmailInputField, {
        label: "To",
        onEmailChange: function onEmailChange(emails) {
          _this.props.onEmailChange({
            to: emails
          });
        }
      });
    }
  }, {
    key: "_renderCCField",
    value: function _renderCCField() {
      var _this2 = this;

      return React.createElement(EmailInputField, {
        label: "CC",
        onEmailChange: function onEmailChange(emails) {
          _this2.props.onEmailChange({
            cc: emails
          });
        }
      });
    }
  }, {
    key: "_renderBCCField",
    value: function _renderBCCField() {
      var _this3 = this;

      return React.createElement(EmailInputField, {
        label: "BCC",
        onEmailChange: function onEmailChange(emails) {
          _this3.props.onEmailChange({
            bcc: emails
          });
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          classes = _this$props.classes,
          showTo = _this$props.showTo,
          showCC = _this$props.showCC,
          showBCC = _this$props.showBCC;
      return React.createElement(_ErrorBoundary.default, null, React.createElement("div", {
        className: classes.component
      }, showTo && this._renderToField(), showCC && this._renderCCField(), showBCC && this._renderBCCField()));
    }
  }]);

  return EmailInputFields;
}(React.Component);

_defineProperty(EmailInputFields, "propTypes", {
  classes: _propTypes.default.object,
  // will come from withStyles HOC
  onEmailChange: _propTypes.default.func,
  showTo: _propTypes.default.bool,
  showBCC: _propTypes.default.bool,
  showCC: _propTypes.default.bool
});

_defineProperty(EmailInputFields, "defaultProps", {
  onEmailChange: function onEmailChange() {},
  showTo: true,
  showCC: true,
  showBCC: true
});

var _default = (0, _withStyles.default)(_styles.default)(EmailInputFields);

exports.default = _default;

var BaseEmailInputField =
/*#__PURE__*/
function (_React$Component2) {
  _inherits(BaseEmailInputField, _React$Component2);

  function BaseEmailInputField() {
    var _getPrototypeOf2;

    var _this4;

    _classCallCheck(this, BaseEmailInputField);

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

    _this4 = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(BaseEmailInputField)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this4)), "addEmailForm", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this4)), "state", {
      emails: {}
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this4)), "_onAddEmail", function (values) {
      var emails = _this4.state.emails;

      _this4.setState({
        emails: _objectSpread({}, emails, _defineProperty({}, values.email, {
          name: values.name,
          email: values.email
        }))
      }, function () {
        _this4.addEmailForm.resetForm({
          name: '',
          email: ''
        });

        _this4.props.onEmailChange(Object.values(_this4.state.emails));
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this4)), "_onRemoveEmail", function (_ref) {
      var name = _ref.name,
          email = _ref.email;
      var emails = _this4.state.emails;
      var newEmails = emails;
      delete newEmails[email];

      _this4.setState({
        emails: newEmails
      }, function () {
        _this4.props.onEmailChange(Object.values(_this4.state.emails));
      });
    });

    return _this4;
  }

  _createClass(BaseEmailInputField, [{
    key: "render",
    value: function render() {
      var _this5 = this;

      var _this$props2 = this.props,
          classes = _this$props2.classes,
          label = _this$props2.label;
      var emails = this.state.emails;
      return React.createElement("div", {
        className: classes.emailInputField
      }, React.createElement(_FieldLabel.default, {
        label: label
      }), !(0, _isEmpty.default)(emails) && React.createElement("div", {
        className: classes.listEmails
      }, Object.values(emails).map(function (email, index) {
        return React.createElement("div", {
          className: classes.addedEmailWrapper,
          key: index
        }, React.createElement(_reactTippy.Tooltip, {
          title: email.email,
          interactive: true,
          animation: "fade",
          position: "top",
          arrow: true,
          arrowType: "round",
          trigger: "mouseenter focus"
        }, React.createElement("div", {
          className: classes.addedEmail
        }, email.name)), React.createElement(_ButtonBase.default, {
          classes: {
            root: classes.removeEmailIcon
          },
          onClick: function onClick() {
            _this5._onRemoveEmail(email);
          }
        }, React.createElement("i", {
          className: "fa fa-times"
        })));
      })), React.createElement("div", {
        className: classes.emailInput
      }, React.createElement(_formik.Formik, {
        ref: function ref(_ref2) {
          return _this5.addEmailForm = _ref2;
        },
        initialValues: {
          name: '',
          email: ''
        },
        validationSchema: Yup.object({
          email: Yup.string().required('Email is required').email('Email is not valid'),
          name: Yup.string().required('Name is required')
        }),
        onSubmit: this._onAddEmail
      }, function (formProps) {
        return React.createElement("form", {
          className: classes.addEmailForm,
          onSubmit: formProps.handleSubmit
        }, React.createElement(_TextField.default, {
          placeholder: "Name",
          classes: {
            component: classes.textInput
          },
          value: formProps.values.name,
          error: formProps.touched.name && formProps.errors.name,
          onChange: formProps.handleChange,
          onBlur: formProps.handleBlur,
          name: "name"
        }), React.createElement(_TextField.default, {
          placeholder: "Email",
          classes: {
            component: classes.textInput
          },
          value: formProps.values.email,
          error: formProps.touched.email && formProps.errors.email,
          onChange: formProps.handleChange,
          onBlur: formProps.handleBlur,
          name: "email"
        }), React.createElement(_Button.default, {
          title: "\u2713",
          type: "submit",
          isDisabled: Boolean(formProps.touched.name && formProps.errors.name) || Boolean(formProps.touched.email && formProps.errors.email),
          classes: {
            component: classes.addEmailButton
          }
        }));
      })));
    }
  }]);

  return BaseEmailInputField;
}(React.Component);

_defineProperty(BaseEmailInputField, "propTypes", {
  classes: _propTypes.default.object,
  // will come from withStyles HOC
  onEmailChange: _propTypes.default.func,
  label: _propTypes.default.string
});

var EmailInputField = (0, _withStyles.default)(_styles.default)(BaseEmailInputField);