"use strict";

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

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

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

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

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

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

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

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

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _Common = require("../../Common");

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

var _templateObject, _templateObject2;

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

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

  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(),
      facebookLogin = _authApi$useLoginFace[0];

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

  var facebookLoginCallback = /*#__PURE__*/function () {
    var _ref = (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.state === 'unknown')) {
                _context.next = 3;
                break;
              }

              return _context.abrupt("return");

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

            case 5:
              /* eslint-enable */
              changeBasket();

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

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

  var googleLoginCallback = /*#__PURE__*/function () {
    var _ref2 = (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:
              /* eslint-enable */
              changeBasket();

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

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

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

  return /*#__PURE__*/_react["default"].createElement(Wrapper, null, /*#__PURE__*/_react["default"].createElement(SocialButtonWrap, null, /*#__PURE__*/_react["default"].createElement(_Common.Button, {
    bordered: true,
    fullWidth: true,
    type: "button",
    icon: /*#__PURE__*/_react["default"].createElement(_Common.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(SocialButtonWrap, null, /*#__PURE__*/_react["default"].createElement(_Common.Button, {
    bordered: true,
    fullWidth: true,
    type: "button",
    icon: /*#__PURE__*/_react["default"].createElement(_Common.IconGoogleLogin, null),
    iconPosition: "left",
    onClick: function onClick() {
      return setShowGoogleLogin(true);
    }
  }, 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'
  }))));
};

exports.SocialLogin = SocialLogin;

var Wrapper = _styledComponents["default"].div.attrs({
  className: 'd-flex flex-column justify-content-center'
})(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2["default"])(["\n  width: 100%;\n"])));

var SocialButtonWrap = _styledComponents["default"].div(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2["default"])(["\n  &:not(:last-child) {\n    margin-bottom: 1rem;\n  }\n"])));
//# sourceMappingURL=index.js.map