!function(e,t){"function"==typeof define&&define.amd?define(["bluebird","facebook","lodash"],function(e,i,n){return t(e,i,n)}):"object"==typeof module&&module.exports?module.exports=t(require("bluebird"),require("facebook"),require("lodash")):e.FBExplorer=t(e.Promise,e.FB,e._)}(this,function(e,t,i){"use strict";var n,r,s,o={types:{EVENT:"event",PAGE:"page",PLACE:"place"},profiles:{BASIC:"basic",BRIEF:"brief",EXTENDED:"extended",FULL:"full"},sort:{DISTANCE:"distance",NAME:"name",TIME:"time"},order:{ASC:"asc",DESC:"desc"},methods:{MULTIPLE:"multiple",LEAST:"least",CHUNKED:"chunked"},fields:{event:{basic:["id","name"],brief:["category","cover","description","id","end_time","name","place{id,name,location{city,city_id,country,latitude,longitude,street,zip}}","start_time"],extended:["attending_count","category","cover","description","id","interested_count","end_time","maybe_count","name","place{id,name,location{city,city_id,country,latitude,longitude,street,zip}}","start_time","type"],full:["attending_count","can_guests_invite","can_viewer_post","category","cover","declined_count","description","end_time","id","interested_count","is_canceled","is_draft","is_page_owned","is_viewer_admin","maybe_count","name","noreply_count","owner{name,emails,contact_address,link}","parent_group","place{id,name,location{city,city_id,country,latitude,longitude,street,zip}}","start_time","ticket_uri","timezone","type","updated_time"]},page:{basic:["id","name"],brief:["category","cover","description","id","location","name"],extended:["category","contact_address","cover","description","fan_count","featured_video","founded","id","location","name","talking_about_count","website"],full:["about","app_links","best_page","can_checkin","category","category_list","contact_address","cover","description","display_subtext","emails","fan_count","featured_video","founded","general_info","id","impressum","is_community_page","is_verified","link","location","name","overall_star_rating","parent_page","phone","rating_count","start_info","supports_instant_articles","talking_about_count","website","were_here_count"]},place:{basic:["id","name"],brief:["category","description","id","location","name","picture"],extended:["category","description","hours","id","location","name","phone","picture","website"],full:["about","category","category_list","cover","checkins","description","hours","id","is_always_open","is_permanently_closed","is_verified","link","location","name","overall_star_rating","parking","payment_options","phone","photos","picture","price_range","rating_count","restaurant_services","restaurant_specialties","single_line_address","website","workflows"]}}},c={days:30,limit:100,method:o.methods.CHUNKED,profile:o.profiles.BRIEF,since:"now",version:"v2.9"},a={init:function(i){return r=i.appId,Object.assign(c,i),s=new e(function(e,i){t.Event.subscribe("auth.statusChange",function(t){t&&"connected"==t.status?e(!0):i(!1)}.bind(this))}),t.init({appId:this.getAppId(),version:c.version}),this.getLoginStatus()},ready:function(){return s},getLoginStatus:function(){return new e(function(e,i){t.getLoginStatus(function(t){t&&t.authResponse&&t.authResponse.accessToken?(this.setToken(t.authResponse.accessToken),e()):i()}.bind(this),!0)}.bind(this))},getToken:function(){return n},setToken:function(e){n=e},getAppId:function(){return r},setAppId:function(e){r=e,this.init({appId:e})},findPlaces:function(e,t){if(e)return this.search(o.types.PLACE,e,t).bind(this).then(function(t){return t=this.sort(t,e.sort,e.order,e.center)})},findPages:function(e,t){if(e)return this.search(o.types.PAGE,e,t).bind(this).then(function(t){return t=this.sort(t,e.sort,e.order,e.center)})},findEvents:function(t,n){if(t){return t.until=t.until||this.__getDefaultUntilTime(),t.since=t.since||this.__getDefaultSinceTime(),t.method=this.__isMethodValid(t.method)?t.method:c.method,(t.method===o.methods.MULTIPLE?this.__findEventsUsingMultipleRequests(t,n):t.method===o.methods.LEAST?this.__findEventsUsingLeastRequests(t,n):t.method===o.methods.CHUNKED?this.__findEventsUsingChunkedRequests(t,n):e.resolve()).bind(this).then(function(e){return n&&n.apply(null,[[],!1]),this.sort(i.flatten(e),t.sort,t.order,t.center)})}},search:function(n,r,s){if(r){var o=[];return this.ready().bind(this).then(function(){var c="",a=function(){return new e(function(e,a){var d=c||"/search?type="+n+this.__getSearchQuery(n,r);t.api(d,function(t){if(!t||t.error)a(t.error);else{var n=i.differenceBy(t.data,o,"id");c=t.paging&&t.paging.next,o=o.concat(n),"function"==typeof s&&s.apply(null,[n,i.isUndefined(c)]),e(c)}}.bind(this))}.bind(this))}.bind(this);return this.__promiseWhile(function(){return i.isUndefined(c)},a,c).bind(this).then(function(e){return o})})}},sort:function(e,t,i,n){return t===o.sort.DISTANCE?e=this.__sortByDistance(e,i,n):t===o.sort.TIME?e=this.__sortByTime(e,i):t===o.sort.NAME&&(e=this.__sortByField(e,t,i)),e},getFieldsFromProfile:function(e,t){if(this.__isTypeValid(e))return t=this.__isProfileValid(t)?t:c.profile,o.fields[e][t].join(",")},__findEventsUsingMultipleRequests:function(t,n){var r=[];return this.findPlaces({center:t.center,distance:t.distance,cityId:t.cityId,fields:"id,events.fields(id).since("+t.since+").until("+t.until+").limit(1)"}).bind(this).then(function(s){for(var o=[],c=i.reject(s,function(e){return!e.events}),a=c.length;a--;){var d=this.__searchEventsByPlaceId({placeId:c[a].id,until:t.until,since:t.since,profile:t.profile,fields:t.fields}).bind(this).then(function(e){var s=i.differenceBy(e,r,"id"),o=t.cityId?this.__filterEventsByCityId(s,t.cityId):this.__filterEventsByDistance(s,t.center,t.distance);return n&&n.apply(null,[o,!1]),r=r.concat(o),o});o.push(d)}return e.all(o)})},__findEventsUsingLeastRequests:function(e,t){return this.findPlaces({center:e.center,distance:e.distance,cityId:e.cityId,fields:"id,events.fields("+this.getFieldsFromProfile(o.types.EVENT,e.profile)+").since("+e.since+").until("+e.until+").limit(100)"}).bind(this).then(function(t){var n=i.map(t,function(e){return e.events?e.events.data:[]}),r=i.uniqBy(i.flatten(n),"id");return e.cityId?this.__filterEventsByCityId(r,e.cityId):this.__filterEventsByDistance(r,e.center,e.distance)})},__findEventsUsingChunkedRequests:function(t,n){return this.findPlaces({center:t.center,distance:t.distance,cityId:t.cityId,fields:"id,events.fields(id,place{location{city_id,latitude,longitude}}).since("+t.since+").until("+t.until+").limit(100)"}).bind(this).then(function(r){for(var s=[],o=i.map(r,function(e){return e.events?e.events.data:[]}),c=i.uniqBy(i.flatten(o),"id"),a=t.cityId?this.__filterEventsByCityId(c,t.cityId):this.__filterEventsByDistance(c,t.center,t.distance),d=i.chunk(a,50),u=d.length;u--;){var l=this.__searchEventsByIds({ids:i.map(d[u],function(e){return e.id}).join(),profile:t.profile,fields:t.fields},n);s.push(l)}return e.all(s)})},__searchEventsByPlaceId:function(n,r){if(n&&n.placeId){var s=[];return this.ready().bind(this).then(function(){var c="",a=function(){return new e(function(e,a){var d=c||"/"+n.placeId+"/events?"+this.__getSearchQuery(o.types.EVENT,n);t.api(d,function(t){if(!t||t.error)a(t.error);else{var n=i.differenceBy(t.data,s,"id");c=t.paging&&t.paging.next,s=s.concat(n),"function"==typeof r&&r.apply(null,[n,!0]),e(c)}}.bind(this))}.bind(this))}.bind(this);return this.__promiseWhile(function(){return i.isUndefined(c)},a,c).bind(this).then(function(e){return s})})}},__searchEventsByIds:function(n,r){if(n&&n.ids){var s=[];return this.ready().bind(this).then(function(){var c="",a=function(){return new e(function(e,a){var d=c||"/?ids="+n.ids+this.__getSearchQuery(o.types.EVENT,n);t.api(d,function(t){t&&!t.error||a(t.error);var n=i.map(t,function(e){return e}),o=i.differenceBy(n,s,"id");c=t.paging&&t.paging.next,s=s.concat(o),"function"==typeof r&&r.apply(null,[o,!0]),e(c)}.bind(this))}.bind(this))}.bind(this);return this.__promiseWhile(function(){return i.isUndefined(c)},a,c).bind(this).then(function(e){return s})})}},__getSearchQuery:function(e,t){var i="";return i+="&q="+(t.keywords||""),i+="&fields="+(t.fields||this.getFieldsFromProfile(e,t.profile)),i+="&limit="+c.limit,e===o.types.EVENT&&(t.since&&(i+="&since="+t.since),t.until&&(i+="&until="+t.until)),e===o.types.PLACE&&t.cityId&&(i+="&address=*",i+="&city="+t.cityId),t.center&&(i+="¢er="+t.center.latitude+","+t.center.longitude),t.center&&(i+="&distance="+t.distance),i+="&access_token="+this.getToken()},__sortByDistance:function(e,t,i){return e.sort(function(e,n){var r=e&&(e.location||e.place&&e.place.location),s=n&&(n.location||n.place&&n.place.location);if(!r&&!s)return 0;if(!r)return-1;if(!s)return 1;var c=this.__calculateDistanceInMetres(i,r),a=this.__calculateDistanceInMetres(i,s);return t===o.order.ASC?c-a:a-c}.bind(this))},__calculateDistanceInMetres:function(e,t){if(this.__arePointsValid(e,t)){if(this.__arePointsTheSame(e,t))return 0;var i=e.latitude,n=e.longitude,r=t.latitude,s=t.longitude,o=.017453292519943295,c=.5-Math.cos((r-i)*o)/2+Math.cos(i*o)*Math.cos(r*o)*(1-Math.cos((s-n)*o))/2;return 12742*Math.asin(Math.sqrt(c))*1e3}},__arePointsTheSame:function(e,t){return e.longitude.toString()===t.longitude.toString()&&e.latitude.toString()===t.latitude.toString()},__arePointsValid:function(e,t){return e&&t&&!i.isNil(e.latitude)&&!i.isNil(e.longitude)&&!i.isNil(t.latitude)&&!i.isNil(t.longitude)},__sortByTime:function(e,t){return e.sort(function(e,i){var n=new Date(e.start_time),r=new Date(i.start_time);return t===o.order.ASC?n-r:r-n})},__sortByField:function(e,t,n){return i.orderBy(e,[t],[n])},__isProfileValid:function(e){return e&&i.includes(o.profiles,e)},__isMethodValid:function(e){return e&&i.includes(o.methods,e)},__isTypeValid:function(e){return e&&i.includes(o.types,e)},__filterEventsByDistance:function(e,t,n){return i.filter(e,function(e){return e.place&&e.place.location&&this.__calculateDistanceInMetres(e.place.location,t)