text: "use strict";
  
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports["default"] = void 0;
  
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  
  var _user = _interopRequireDefault(require("../user"));
  
  var _container = require("./container");
  
  var UsersSocial = /*#__PURE__*/function () {
    function UsersSocial(users) {
      (0, _classCallCheck2["default"])(this, UsersSocial);
      this.users = users;
      this.app = users.app;
    }
  
    (0, _createClass2["default"])(UsersSocial, [{
      key: "loginWithFacebook",
      value: function () {
        var _loginWithFacebook = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(fieldsMapping, permissions, stayLoggedIn) {
          return _regenerator["default"].wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  return _context.abrupt("return", this.loginWithContainer('facebook', fieldsMapping, permissions, null, stayLoggedIn));
  
                case 1:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, this);
        }));
  
        function loginWithFacebook(_x, _x2, _x3) {
          return _loginWithFacebook.apply(this, arguments);
        }
  
        return loginWithFacebook;
      }()
    }, {
      key: "loginWithGooglePlus",
      value: function () {
        var _loginWithGooglePlus = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(fieldsMapping, permissions, container, stayLoggedIn) {
          return _regenerator["default"].wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  return _context2.abrupt("return", this.loginWithContainer('googleplus', fieldsMapping, permissions, container, stayLoggedIn));
  
                case 1:
                case "end":
                  return _context2.stop();
              }
            }
          }, _callee2, this);
        }));
  
        function loginWithGooglePlus(_x4, _x5, _x6, _x7) {
          return _loginWithGooglePlus.apply(this, arguments);
        }
  
        return loginWithGooglePlus;
      }()
    }, {
      key: "loginWithTwitter",
      value: function () {
        var _loginWithTwitter = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(fieldsMapping, stayLoggedIn) {
          return _regenerator["default"].wrap(function _callee3$(_context3) {
            while (1) {
              switch (_context3.prev = _context3.next) {
                case 0:
                  return _context3.abrupt("return", this.loginWithContainer('twitter', fieldsMapping, null, null, stayLoggedIn));
  
                case 1:
                case "end":
                  return _context3.stop();
              }
            }
          }, _callee3, this);
        }));
  
        function loginWithTwitter(_x8, _x9) {
          return _loginWithTwitter.apply(this, arguments);
        }
  
        return loginWithTwitter;
      }()
    }, {
      key: "loginWithFacebookSdk",
      value: function () {
        var _loginWithFacebookSdk = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(accessToken, fieldsMapping, stayLoggedIn) {
          return _regenerator["default"].wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  return _context4.abrupt("return", this.sendWithAccessToken('facebook', accessToken, fieldsMapping, stayLoggedIn));
  
                case 1:
                case "end":
                  return _context4.stop();
              }
            }
          }, _callee4, this);
        }));
  
        function loginWithFacebookSdk(_x10, _x11, _x12) {
          return _loginWithFacebookSdk.apply(this, arguments);
        }
  
        return loginWithFacebookSdk;
      }()
    }, {
      key: "loginWithGooglePlusSdk",
      value: function () {
        var _loginWithGooglePlusSdk = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(accessToken, fieldsMapping, stayLoggedIn) {
          return _regenerator["default"].wrap(function _callee5$(_context5) {
            while (1) {
              switch (_context5.prev = _context5.next) {
                case 0:
                  return _context5.abrupt("return", this.sendWithAccessToken('googleplus', accessToken, fieldsMapping, stayLoggedIn));
  
                case 1:
                case "end":
                  return _context5.stop();
              }
            }
          }, _callee5, this);
        }));
  
        function loginWithGooglePlusSdk(_x13, _x14, _x15) {
          return _loginWithGooglePlusSdk.apply(this, arguments);
        }
  
        return loginWithGooglePlusSdk;
      }()
    }, {
      key: "sendWithAccessToken",
      value: function () {
        var _sendWithAccessToken = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(socialType, accessToken, fieldsMapping, stayLoggedIn) {
          var _this = this;
  
          return _regenerator["default"].wrap(function _callee6$(_context6) {
            while (1) {
              switch (_context6.prev = _context6.next) {
                case 0:
                  if (!(!accessToken || typeof accessToken !== 'string')) {
                    _context6.next = 2;
                    break;
                  }
  
                  throw new Error('"accessToken" must be non empty string.');
  
                case 2:
                  if (typeof fieldsMapping === 'boolean') {
                    stayLoggedIn = fieldsMapping;
                    fieldsMapping = undefined;
                  }
  
                  return _context6.abrupt("return", this.app.request.post({
                    url: this.app.urls.userSocialLogin(socialType),
                    data: {
                      accessToken: accessToken,
                      fieldsMapping: fieldsMapping
                    }
                  }).then(function (data) {
                    return _this.users.setCurrentUser(data, stayLoggedIn);
                  }));
  
                case 4:
                case "end":
                  return _context6.stop();
              }
            }
          }, _callee6, this);
        }));
  
        function sendWithAccessToken(_x16, _x17, _x18, _x19) {
          return _sendWithAccessToken.apply(this, arguments);
        }
  
        return sendWithAccessToken;
      }()
    }, {
      key: "loginWithContainer",
      value: function () {
        var _loginWithContainer = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(socialType, fieldsMapping, permissions, container, stayLoggedIn) {
          var _this2 = this;
  
          var socialContainer, resolveContainer;
          return _regenerator["default"].wrap(function _callee7$(_context7) {
            while (1) {
              switch (_context7.prev = _context7.next) {
                case 0:
                  socialContainer = new _container.SocialContainer(socialType, container);
  
                  resolveContainer = function resolveContainer() {
                    return new Promise(function (resolve, reject) {
                      var onMessage = function onMessage(event) {
                        if (event.origin === _this2.app.serverURL) {
                          var result = JSON.parse(event.data);
  
                          if (result.fault) {
                            reject(new Error(result.fault));
                          } else {
                            resolve(result);
                          }
  
                          removeWindowEventListener('message', window, onMessage);
                          socialContainer.closeContainer();
                        }
                      };
  
                      addWindowEventListener('message', window, onMessage);
                    });
                  };
  
                  return _context7.abrupt("return", this.app.request.post({
                    url: this.app.urls.userSocialOAuth(socialType),
                    data: {
                      fieldsMapping: fieldsMapping || {},
                      permissions: permissions || []
                    }
                  }).then(function (authUrl) {
                    return socialContainer.doAuthorizationActivity(authUrl);
                  })["catch"](function (error) {
                    socialContainer.closeContainer();
                    throw error;
                  }).then(resolveContainer).then(function (data) {
                    return _this2.users.setCurrentUser(data, stayLoggedIn);
                  }));
  
                case 3:
                case "end":
                  return _context7.stop();
              }
            }
          }, _callee7, this);
        }));
  
        function loginWithContainer(_x20, _x21, _x22, _x23, _x24) {
          return _loginWithContainer.apply(this, arguments);
        }
  
        return loginWithContainer;
      }()
    }, {
      key: "loginWithOauth2",
      value: function () {
        var _loginWithOauth = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(providerCode, accessToken, guestUser, fieldsMapping, stayLoggedIn) {
          var _this3 = this;
  
          return _regenerator["default"].wrap(function _callee8$(_context8) {
            while (1) {
              switch (_context8.prev = _context8.next) {
                case 0:
                  if (!(!providerCode || typeof providerCode !== 'string')) {
                    _context8.next = 2;
                    break;
                  }
  
                  throw new Error('"providerCode" must be non empty string.');
  
                case 2:
                  if (!(!accessToken || typeof accessToken !== 'string')) {
                    _context8.next = 4;
                    break;
                  }
  
                  throw new Error('"accessToken" must be non empty string.');
  
                case 4:
                  if (guestUser && !(guestUser instanceof _user["default"])) {
                    stayLoggedIn = fieldsMapping;
                    fieldsMapping = guestUser;
                    guestUser = undefined;
                  }
  
                  if (typeof fieldsMapping === 'boolean') {
                    stayLoggedIn = fieldsMapping;
                    fieldsMapping = undefined;
                  }
  
                  return _context8.abrupt("return", this.app.request.post({
                    url: this.app.urls.userOAuthLogin(providerCode),
                    data: {
                      accessToken: accessToken,
                      fieldsMapping: fieldsMapping,
                      guestUser: guestUser
                    }
                  }).then(function (data) {
                    return _this3.users.setCurrentUser(data, stayLoggedIn);
                  }));
  
                case 7:
                case "end":
                  return _context8.stop();
              }
            }
          }, _callee8, this);
        }));
  
        function loginWithOauth2(_x25, _x26, _x27, _x28, _x29) {
          return _loginWithOauth.apply(this, arguments);
        }
  
        return loginWithOauth2;
      }()
    }, {
      key: "loginWithOauth1",
      value: function () {
        var _loginWithOauth2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(providerCode, accessToken, accessTokenSecret, guestUser, fieldsMapping, stayLoggedIn) {
          var _this4 = this;
  
          return _regenerator["default"].wrap(function _callee9$(_context9) {
            while (1) {
              switch (_context9.prev = _context9.next) {
                case 0:
                  if (!(!providerCode || typeof providerCode !== 'string')) {
                    _context9.next = 2;
                    break;
                  }
  
                  throw new Error('"providerCode" must be non empty string.');
  
                case 2:
                  if (!(!accessToken || typeof accessToken !== 'string')) {
                    _context9.next = 4;
                    break;
                  }
  
                  throw new Error('"accessToken" must be non empty string.');
  
                case 4:
                  if (!(!accessTokenSecret || typeof accessTokenSecret !== 'string')) {
                    _context9.next = 6;
                    break;
                  }
  
                  throw new Error('"accessTokenSecret" must be non empty string.');
  
                case 6:
                  if (guestUser && !(guestUser instanceof _user["default"])) {
                    stayLoggedIn = fieldsMapping;
                    fieldsMapping = guestUser;
                    guestUser = undefined;
                  }
  
                  if (typeof fieldsMapping === 'boolean') {
                    stayLoggedIn = fieldsMapping;
                    fieldsMapping = undefined;
                  }
  
                  return _context9.abrupt("return", this.app.request.post({
                    url: this.app.urls.userOAuthLogin(providerCode),
                    data: {
                      accessToken: accessToken,
                      accessTokenSecret: accessTokenSecret,
                      fieldsMapping: fieldsMapping,
                      guestUser: guestUser
                    }
                  }).then(function (data) {
                    return _this4.users.setCurrentUser(data, stayLoggedIn);
                  }));
  
                case 9:
                case "end":
                  return _context9.stop();
              }
            }
          }, _callee9, this);
        }));
  
        function loginWithOauth1(_x30, _x31, _x32, _x33, _x34, _x35) {
          return _loginWithOauth2.apply(this, arguments);
        }
  
        return loginWithOauth1;
      }()
    }]);
    return UsersSocial;
  }();
  
  exports["default"] = UsersSocial;
  
  function addWindowEventListener(event, elem, callback) {
    if (elem.addEventListener) {
      elem.addEventListener(event, callback, false);
    } else if (elem.attachEvent) {
      elem.attachEvent('on' + event, callback);
    } else {
      elem[event] = callback;
    }
  }
  
  function removeWindowEventListener(event, elem, callback) {
    if (elem.removeEventListener) {
      elem.removeEventListener(event, callback, false);
    } else if (elem.detachEvent) {
      elem.detachEvent('on' + event, callback);
    }
  
    elem[event] = null;
  }