import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import React, { useState } from 'react';
import FacebookLogin from 'react-facebook-login';
import GoogleLogin from 'react-google-login';
import { authApi, useConfigState, useGlobalLabels, useReleva } from '@sentecacommerce-theme/lib';
import { Button, IconFacebookLogin, IconGoogleLogin } from '../../../..';
import { Separator, SeparatorSpan, SocialButtonWrapper } from '../Styled';
import { AppleLogin } from './Components';
export var SocialLogin = (_ref) => {
  var {
    handleClose
  } = _ref;
  var {
    changeBasket
  } = useReleva();
  var [showFacebookLogin, setShowFacebookLogin] = useState(false);
  var [showGoogleLogin, setShowGoogleLogin] = useState(false);
  var [facebookLogin] = authApi.useLoginFacebook(handleClose);
  var [googleLogin] = authApi.useLoginGoogle(handleClose);
  var [appleLogin] = authApi.useLoginApple(handleClose);
  var {
    buttons
  } = useGlobalLabels();

  var facebookLoginCallback = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator(function* (response) {
      setShowFacebookLogin(false);
      if (response.status === 'unknown') return; // eslint-disable-next-line @typescript-eslint/camelcase

      yield facebookLogin({
        access_token: response.accessToken
      });
      changeBasket();
    });

    return function facebookLoginCallback(_x) {
      return _ref2.apply(this, arguments);
    };
  }();

  var googleLoginCallback = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator(function* (response) {
      // eslint-disable-next-line @typescript-eslint/camelcase
      yield googleLogin({
        access_token: response.tokenId
      });
      changeBasket();
    });

    return function googleLoginCallback(_x2) {
      return _ref3.apply(this, arguments);
    };
  }();

  var appleLoginCallback = /*#__PURE__*/function () {
    var _ref4 = _asyncToGenerator(function* (response) {
      var _response$authorizati, _response$user, _response$user$name, _response$user2, _response$user2$name;

      appleLogin({
        // eslint-disable-next-line @typescript-eslint/camelcase
        id_token: (_response$authorizati = response.authorization) == null ? void 0 : _response$authorizati.id_token,
        firstName: (_response$user = response.user) == null ? void 0 : (_response$user$name = _response$user.name) == null ? void 0 : _response$user$name.firstName,
        lastName: (_response$user2 = response.user) == null ? void 0 : (_response$user2$name = _response$user2.name) == null ? void 0 : _response$user2$name.lastName
      });
      changeBasket();
    });

    return function appleLoginCallback(_x3) {
      return _ref4.apply(this, arguments);
    };
  }();

  var {
    facebook,
    google
  } = useConfigState();
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Separator, null, /*#__PURE__*/React.createElement(SeparatorSpan, null, "or")), /*#__PURE__*/React.createElement(SocialButtonWrapper, null, /*#__PURE__*/React.createElement(Button, {
    bordered: true,
    fullWidth: true,
    type: "button",
    icon: /*#__PURE__*/React.createElement(IconFacebookLogin, null),
    iconPosition: "left",
    onClick: () => setShowFacebookLogin(true)
  }, buttons.facebookLogin), showFacebookLogin && /*#__PURE__*/React.createElement("div", {
    className: "d-none"
  }, /*#__PURE__*/React.createElement(FacebookLogin, {
    appId: (facebook == null ? void 0 : facebook.appId) || '',
    fields: "name,email",
    callback: facebookLoginCallback,
    autoLoad: true,
    version: "9.0"
  }))), /*#__PURE__*/React.createElement(SocialButtonWrapper, null, /*#__PURE__*/React.createElement(Button, {
    bordered: true,
    fullWidth: true,
    type: "button",
    onClick: () => setShowGoogleLogin(true),
    icon: /*#__PURE__*/React.createElement(IconGoogleLogin, null),
    iconPosition: "left"
  }, buttons.googleLogin), showGoogleLogin && /*#__PURE__*/React.createElement("div", {
    className: "d-none"
  }, /*#__PURE__*/React.createElement(GoogleLogin, {
    clientId: (google == null ? void 0 : google.id) || '',
    autoLoad: true,
    onSuccess: googleLoginCallback,
    cookiePolicy: 'single_host_origin'
  }))), /*#__PURE__*/React.createElement(SocialButtonWrapper, null, /*#__PURE__*/React.createElement(AppleLogin, {
    clientId: "bg.districtshoes.auth",
    redirectURI: "https://staging.districtshoesbg.com/",
    responseType: "id_token",
    callback: appleLoginCallback,
    scope: 'name email',
    responseMode: 'form_post',
    usePopup: true
  })));
};
//# sourceMappingURL=index.js.map