import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";

var _templateObject, _templateObject2;

import React from 'react';
import styled from 'styled-components';
import { useForm, Controller } from 'react-hook-form';
import { usersApi, useGlobalLabels, useChangeEmail } from '@sentecacommerce-theme/lib';
import { FormError, TextInput, ModalForm } from '../../../../../Common';
export var VerifyEmailModal = (_ref) => {
  var {
    handleClose
  } = _ref;
  var [mutate] = useChangeEmail({
    onSuccess: handleClose
  });
  var {
    data,
    isLoading
  } = usersApi.useGet();
  var {
    forms
  } = useGlobalLabels();

  var onSubmit = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator(function* (_ref2) {
      var {
        email
      } = _ref2;
      mutate({
        email
      });
    });

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

  var {
    handleSubmit,
    errors,
    control
  } = useForm({
    defaultValues: {
      email: ''
    },
    mode: 'onChange',
    reValidateMode: 'onChange'
  });
  var Inputs = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(InputWrapper, null, /*#__PURE__*/React.createElement(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.createElement(TextInput, {
      label: forms.emailLabel,
      id: "update-email-field",
      isRequired: true,
      errors: errors,
      name: "email"
    })
  })), /*#__PURE__*/React.createElement(ErrorContainer, null, /*#__PURE__*/React.createElement(FormError, {
    errors: errors,
    fieldName: "phone"
  })));
  return /*#__PURE__*/React.createElement(ModalForm, {
    handleClose: handleClose,
    title: forms.updateEmail,
    subtitle: forms.emailSecurityMsg,
    onSubmit: handleSubmit(onSubmit),
    Inputs: Inputs,
    buttonLabel: "Save",
    disableSubmit: isLoading
  });
};
var InputWrapper = styled.div.attrs({
  className: 'mt-1 mb-2 pb-1'
})(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  position: relative;\n  display: flex;\n  align-items: center;\n"])));
var ErrorContainer = styled.div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n  margin-top: -1.4rem;\n"])));
//# sourceMappingURL=index.js.map