"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styles = require("@material-ui/core/styles");
var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));
var _Grid2 = _interopRequireDefault(require("@material-ui/core/Grid"));
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
var _i18n = require("../../../../Framework/i18n");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var styles = function styles(theme) {
  return {
    Grid: _defineProperty({}, theme.breakpoints.up('sm'), {
      paddingRight: theme.spacing(2)
    }),
    MuiFormControlRoot: {
      marginTop: 0
    },
    outlinedInput: _defineProperty({
      padding: 10,
      fontSize: 15
    }, theme.breakpoints.down('xs'), {
      padding: 19
    }),
    outlinedLabel: {
      fontSize: 15
    }
  };
};
var getErrorMessage = function getErrorMessage(error) {
  return error ? (0, _i18n.t)(error) : null;
};
var validateTextInput = function validateTextInput(name, value) {
  var emailRegexp = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i;
  if (value === '') {
    return 'receiveOfferForm.errors.required';
  }
  if (name === 'email' && !emailRegexp.test(value)) {
    return 'receiveOfferForm.errors.invalidEmail';
  }
  return null;
};
var ReceiveOfferForm = /*#__PURE__*/function (_React$Component) {
  _inherits(ReceiveOfferForm, _React$Component);
  var _super = _createSuper(ReceiveOfferForm);
  function ReceiveOfferForm(props) {
    var _this;
    _classCallCheck(this, ReceiveOfferForm);
    _this = _super.call(this, props);
    _defineProperty(_assertThisInitialized(_this), "setInputRef", function (ref) {
      if (ref) {
        _this.inputRefs[ref.name] = ref;
      }
    });
    _this.state = {
      email: {
        value: '',
        error: null
      },
      name: {
        value: '',
        error: null
      },
      company: {
        value: '',
        error: null
      },
      phonenumber: {
        value: '',
        error: null
      },
      zip: {
        value: '',
        error: null
      },
      city: {
        value: '',
        error: null
      },
      street: {
        value: '',
        error: null
      },
      country: {
        value: '',
        error: null
      }
    };
    _this.inputRefs = {};
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    _this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this));
    _this.setInputRef = _this.setInputRef.bind(_assertThisInitialized(_this));
    return _this;
  }
  _createClass(ReceiveOfferForm, [{
    key: "handleChange",
    value: function handleChange(event) {
      this.setState(_defineProperty({}, event.target.name, {
        value: event.target.type === 'checkbox' ? event.target.checked : event.target.value,
        error: null
      }));
    }
  }, {
    key: "handleBlur",
    value: function handleBlur(event) {
      var value = (0, _cloneDeep["default"])(this.state[event.target.name]);
      value.error = validateTextInput(event.target.name, event.target.value);
      this.setState(_defineProperty({}, event.target.name, value));
    }
  }, {
    key: "validate",
    value: function validate() {
      var formErrors = false;
      var approvalNotSet = null;
      var values = {};
      var state = (0, _cloneDeep["default"])(this.state);
      var inputFields = ['email', 'name', 'company', 'phonenumber', 'zip', 'city', 'street', 'country'];
      var errorInputRef = null;
      for (var _i = 0, _inputFields = inputFields; _i < _inputFields.length; _i++) {
        var inputField = _inputFields[_i];
        values[inputField] = state[inputField].value;
        var error = validateTextInput(inputField, state[inputField].value);
        if (error !== null) {
          formErrors = true;
        }
        if (error && !errorInputRef) {
          errorInputRef = this.inputRefs[inputField];
        }
        state[inputField].error = error;
      }
      this.setState(state);
      var hasError = formErrors || approvalNotSet === true;
      if (hasError) {
        var result = {
          hasError: hasError,
          formErrors: formErrors
        };
        if (approvalNotSet !== null) {
          result.approvalNotSet = approvalNotSet;
        }
        return result;
      }
      return _objectSpread({
        hasError: hasError
      }, values);
    }
  }, {
    key: "render",
    value: function render() {
      var classes = this.props.classes;
      var _this$state = this.state,
        email = _this$state.email,
        name = _this$state.name,
        company = _this$state.company,
        phonenumber = _this$state.phonenumber,
        zip = _this$state.zip,
        city = _this$state.city,
        street = _this$state.street,
        country = _this$state.country;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "email",
        inputRef: this.setInputRef,
        value: email.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.email'),
        margin: "dense",
        variant: "outlined",
        fullWidth: true,
        error: !!email.error,
        helperText: getErrorMessage(email.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        },
        classes: {
          root: classes.MuiFormControlRoot
        }
      }), /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "name",
        inputRef: this.setInputRef,
        value: name.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.name'),
        margin: "dense",
        variant: "outlined",
        translate: "no",
        fullWidth: true,
        error: !!name.error,
        helperText: getErrorMessage(name.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }), /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "company",
        inputRef: this.setInputRef,
        value: company.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.company'),
        margin: "dense",
        variant: "outlined",
        translate: "no",
        fullWidth: true,
        error: !!company.error,
        helperText: getErrorMessage(company.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }), /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "phonenumber",
        inputRef: this.setInputRef,
        value: phonenumber.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.phonenumber'),
        margin: "dense",
        variant: "outlined",
        fullWidth: true,
        error: !!phonenumber.error,
        helperText: getErrorMessage(phonenumber.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }), /*#__PURE__*/_react["default"].createElement(_Grid2["default"], {
        container: true
      }, /*#__PURE__*/_react["default"].createElement(_Grid2["default"], {
        item: true,
        xs: 12,
        sm: 4,
        className: classes.Grid
      }, /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "zip",
        inputRef: this.setInputRef,
        value: zip.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.zip'),
        margin: "dense",
        variant: "outlined",
        fullWidth: true,
        error: !!zip.error,
        helperText: getErrorMessage(zip.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      })), /*#__PURE__*/_react["default"].createElement(_Grid2["default"], {
        item: true,
        xs: 12,
        sm: 8
      }, /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "city",
        inputRef: this.setInputRef,
        value: city.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.city'),
        margin: "dense",
        variant: "outlined",
        translate: "no",
        fullWidth: true,
        error: !!city.error,
        helperText: getErrorMessage(city.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }))), /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "street",
        inputRef: this.setInputRef,
        value: street.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.street'),
        margin: "dense",
        variant: "outlined",
        translate: "no",
        fullWidth: true,
        error: !!street.error,
        helperText: getErrorMessage(street.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }), /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        name: "country",
        inputRef: this.setInputRef,
        value: country.value,
        onChange: this.handleChange,
        onBlur: this.handleBlur,
        label: (0, _i18n.t)('receiveOfferForm.country'),
        margin: "dense",
        variant: "outlined",
        translate: "no",
        fullWidth: true,
        error: !!country.error,
        helperText: getErrorMessage(country.error),
        InputProps: {
          classes: {
            input: classes.outlinedInput
          }
        },
        InputLabelProps: {
          classes: {
            outlined: classes.outlinedLabel
          }
        }
      }));
    }
  }]);
  return ReceiveOfferForm;
}(_react["default"].Component);
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'ReceiveOfferForm'
})(ReceiveOfferForm);