"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.LoginForm = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

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

var _reactHookForm = require("react-hook-form");

var _ = require("../../../..");

var _lib = require("@sentecacommerce-theme/lib");

var _Styled = require("../Styled");

var _SocialLogin = require("../SocialLogin");

var LoginForm = function LoginForm(_ref) {
  var handleClose = _ref.handleClose,
      openForgotPassword = _ref.openForgotPassword;

  var _useReleva = (0, _lib.useReleva)(),
      changeBasket = _useReleva.changeBasket;

  var _useLogin = (0, _lib.useLogin)({
    onSuccess: function onSuccess() {
      changeBasket();
      handleClose();
    }
  }),
      mutate = _useLogin[0],
      _useLogin$ = _useLogin[1],
      isLoading = _useLogin$.isLoading,
      error = _useLogin$.error;

  var _useGlobalLabels = (0, _lib.useGlobalLabels)(),
      errorMessages = _useGlobalLabels.errorMessages;
  /* eslint-disable */


  var apiErrors = (0, _lib.useErrors)(error, {
    invalid_client: errorMessages == null ? void 0 : errorMessages.somethingWentWrong,
    invalid_credentials: errorMessages.invalidCredentials,
    "default": errorMessages == null ? void 0 : errorMessages.somethingWentWrong
  });
  /* eslint-enable */

  var _useState = (0, _react.useState)(''),
      serverError = _useState[0];

  var onSubmit = /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref2) {
      var email, password;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              email = _ref2.email, password = _ref2.password;
              mutate({
                email: email,
                password: password
              });

            case 2:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onSubmit(_x) {
      return _ref3.apply(this, arguments);
    };
  }();

  var _useForm = (0, _reactHookForm.useForm)({
    defaultValues: {
      email: '',
      password: ''
    },
    mode: 'onChange'
  }),
      handleSubmit = _useForm.handleSubmit,
      errors = _useForm.errors,
      control = _useForm.control;

  var hasErrors = !Object.keys(errors).length;

  var _useGlobalLabels2 = (0, _lib.useGlobalLabels)(),
      buttons = _useGlobalLabels2.buttons,
      commonMessages = _useGlobalLabels2.commonMessages,
      forms = _useGlobalLabels2.forms,
      forgotPassword = _useGlobalLabels2.forgotPassword;

  return /*#__PURE__*/_react["default"].createElement("form", {
    onSubmit: handleSubmit(onSubmit)
  }, /*#__PURE__*/_react["default"].createElement(_.InputLabel, null, commonMessages.emailAddress, /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "email",
    rules: {
      required: forms.requiredField,
      pattern: {
        value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
        message: forms.invalidEmail
      }
    },
    as: /*#__PURE__*/_react["default"].createElement(_.Input, {
      id: "login-email"
    })
  }), /*#__PURE__*/_react["default"].createElement(_.FormError, {
    errors: errors,
    fieldName: "email"
  })), /*#__PURE__*/_react["default"].createElement(_.InputLabel, null, commonMessages.password, /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "password",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_.Input, {
      id: "login-pass",
      type: "password"
    })
  }), /*#__PURE__*/_react["default"].createElement(_.FormError, {
    errors: errors,
    fieldName: "password"
  })), /*#__PURE__*/_react["default"].createElement(_Styled.UnderlineCTA, {
    className: "pt-1",
    onClick: openForgotPassword
  }, forgotPassword.forgotYourPass), /*#__PURE__*/_react["default"].createElement(_Styled.CTA, {
    className: "mt-4"
  }, /*#__PURE__*/_react["default"].createElement(_.Button, {
    iconPosition: "right",
    fullWidth: true,
    icon: /*#__PURE__*/_react["default"].createElement(_.LockIcon, null),
    variant: hasErrors ? 'primary' : 'disabled',
    disabled: isLoading
  }, buttons.login)), /*#__PURE__*/_react["default"].createElement(_.FormError, {
    errors: apiErrors,
    fieldName: "invalid_client"
  }), /*#__PURE__*/_react["default"].createElement(_.FormError, {
    errors: apiErrors,
    fieldName: "invalid_credentials"
  }), /*#__PURE__*/_react["default"].createElement(_.FormError, {
    errors: apiErrors,
    fieldName: "default"
  }), serverError && /*#__PURE__*/_react["default"].createElement(_.FormError, {
    center: true,
    text: serverError
  }), /*#__PURE__*/_react["default"].createElement(_SocialLogin.SocialLogin, {
    handleClose: handleClose
  }));
};

exports.LoginForm = LoginForm;
//# sourceMappingURL=index.js.map