"use strict";

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

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

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

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

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

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

var _reactFacebookLogin = _interopRequireDefault(require("react-facebook-login"));

var _reactGoogleLogin = _interopRequireDefault(require("react-google-login"));

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

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

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

var _Components = require("./Components");

var SocialLogin = function SocialLogin(_ref) {
  var handleClose = _ref.handleClose;

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

  var _useState = (0, _react.useState)(false),
      showFacebookLogin = _useState[0],
      setShowFacebookLogin = _useState[1];

  var _useState2 = (0, _react.useState)(false),
      showGoogleLogin = _useState2[0],
      setShowGoogleLogin = _useState2[1];

  var _authApi$useLoginFace = _lib.authApi.useLoginFacebook(handleClose),
      facebookLogin = _authApi$useLoginFace[0];

  var _authApi$useLoginGoog = _lib.authApi.useLoginGoogle(handleClose),
      googleLogin = _authApi$useLoginGoog[0];

  var _authApi$useLoginAppl = _lib.authApi.useLoginApple(handleClose),
      appleLogin = _authApi$useLoginAppl[0];

  var _useGlobalLabels = (0, _lib.useGlobalLabels)(),
      buttons = _useGlobalLabels.buttons;

  var facebookLoginCallback = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(response) {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              setShowFacebookLogin(false);

              if (!(response.status === 'unknown')) {
                _context.next = 3;
                break;
              }

              return _context.abrupt("return");

            case 3:
              _context.next = 5;
              return facebookLogin({
                access_token: response.accessToken
              });

            case 5:
              changeBasket();

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

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

  var googleLoginCallback = /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(response) {
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return googleLogin({
                access_token: response.tokenId
              });

            case 2:
              changeBasket();

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

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

  var appleLoginCallback = /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(response) {
      var _response$authorizati, _response$user, _response$user$name, _response$user2, _response$user2$name;

      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              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();

            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }));

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

  var _useConfigState = (0, _lib.useConfigState)(),
      facebook = _useConfigState.facebook,
      google = _useConfigState.google;

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

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