/*jslint node: true */
/*global module, require, google, gapi, auth, authorize, access_token*/
'use strict';
var newCallback = require('./../construct/newCallback.js');
/**
* This function requests authorization to use a Google API, and if received, loads that API client. For more information
* on Google APIs, see {@link http://developers.google.com/api-client-library/javascript/reference/referencedocs}.
*
* @function fetchGoogleAuth
* @memberof socioscapes
* @param {Object} config - An object with configuration options for Google APIs.
* @param {Object} config.auth - Configuration options for the auth request (eg. .client_id, .scope, .immediate)
* @param {Object} config.client.name - The name of the Google API client to load.
* @param {Object} config.client.version - The version of the Google API client to load.
* @return this {Object}
*/
function fetchGoogleAuth(config) {
var callback = newCallback(arguments);
gapi.auth.authorize(config.auth, function (token) {
if (token && token.access_token) {
gapi.client.load(config.client.name, config.client.version, function (result) {
callback(result);
return result;
});
}
});
}
module.exports = fetchGoogleAuth;