// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/opsdashboard/core/ExtensionBase","dojo/_base/declare dojo/_base/lang dojo/Deferred dojo/_base/Deferred ../../IdentityManager ../../Credential ../../request ./messageHandler ./errorMessages ./MessageReceiver ../MapWidgetProxy ../DataSourceProxy".split(" "),function(m,d,c,n,h,p,q,f,g,r,l,k){function t(a){function b(a){return f._sendMessageWithReply({functionName:"getCredential",args:{url:a}})}a&&(h.useSignInPage=!1,q.setRequestPreCallback(function(a){a.failOk=!0;return a}),h.signIn=function(a,
c){var e=new n;b(a).then(d.hitch(this,function(a){a=new p(a.credential);a.refreshToken=function(){return b(this.server).then(d.hitch(this,function(a){this.token=a.credential.token;this.expires=a.credential.expires?Number(a.credential.expires):null;this.creationTime=a.credential.creationTime;this.validity=a.credential.validity;this.onTokenChange()}))};e.callback(a)}),function(a){e.errback(a)});return e},h.setProtocolErrorHandler(function(){return!0}))}return m([r],{drawingType:{POINT:"point",LINE:"line",
POLYLINE:"polyline",FREEHAND_POLYLINE:"freehandpolyline",EXTENT:"extent",CIRCLE:"circle",POLYGON:"polygon",FREEHAND_POLYGON:"freehandpolygon"},_hostInitialized:!1,isNative:null,portalUrl:null,constructor:function(){this._dataSourceProxies={};this._mapWidgetProxies=[];f._sendMessageWithReply({functionName:"initialize"}).then(d.hitch(this,this._initializeResponseReceived)).then(d.hitch(this,this._hostReady)).then(function(){f._sendMessage({functionName:"afterInitialize"})}).otherwise(d.hitch(this,this._hostInitializationError))},
__messageReceived:function(a){switch(a.functionName.toLowerCase()){case "datasourceadded":return this._dataSourceAdded(a.args.dataSource);case "datasourceremoved":return this._dataSourceRemoved(a.args.dataSourceId);case "mapwidgetadded":return this._mapWidgetAdded(a.args.mapWidget);case "mapwidgetremoved":return this._mapWidgetRemoved(a.args.mapWidgetId);default:return this._messageReceived(a)}},_initializeResponseReceived:function(a){this.isNative=f.isNative;this._hostInitialized=!0;this.portalHelperServices=
a.helperServices;this.portalUrl=a.portalUrl;t(a.usePortalServices);this._setConfig(a.configuration);return(new c).resolve()},_isHostInitialized:function(){return this._hostInitialized},_hostReady:function(){this.hostReady();this.emit("host-ready")},hostReady:function(){},_hostInitializationError:function(a){this._hostInitialized=!1;this.hostInitializationError(a);this.emit("initialization-error",{error:a})},hostInitializationError:function(a){},getMapWidgetProxies:function(){return this._isHostInitialized()?
this._mapWidgetProxies&&0<this._mapWidgetProxies.length?(new c).resolve(this._mapWidgetProxies):f._sendMessageWithReply({functionName:"getMapWidgets"}).then(d.hitch(this,function(a){return this._mapWidgetProxies=a.mapWidgets.map(function(a){return new l(a)},this)})):(new c).reject(Error(g.hostNotReady))},getMapWidgetProxy:function(a){return this._isHostInitialized()?a?this.getMapWidgetProxies().then(function(b){for(var e=0;e<b.length;e++)if(b[e].id===a)return b[e];return null}):(new c).reject(Error(g.invalidArguments)):
(new c).reject(Error(g.hostNotReady))},_mapWidgetRemoved:function(a){for(var b=0;b<this._mapWidgetProxies.length;b++)if(this._mapWidgetProxies[b].id===a){this._mapWidgetProxies.splice(b,1);break}this.mapWidgetRemoved(a);this.emit("map-widget-removed",{mapWidgetId:a})},mapWidgetRemoved:function(a){},_mapWidgetAdded:function(a){a=new l(a);this._mapWidgetProxies.push(a);this.mapWidgetAdded(a);this.emit("map-widget-added",{mapWidgetProxy:a})},mapWidgetAdded:function(a){},getDataSourceProxies:function(){return this._isHostInitialized()?
f._sendMessageWithReply({functionName:"getDataSources"}).then(d.hitch(this,function(a){this._dataSourceProxies={};return a.dataSources.map(function(a){var b=new k(a);return this._dataSourceProxies[a.id]=b},this)})):(new c).reject(Error(g.hostNotReady))},getDataSourceProxy:function(a){if(!this._isHostInitialized())return(new c).reject(Error(g.hostNotReady));if(!a)return(new c).reject(Error(g.invalidArguments));var b=this._dataSourceProxies[a];return b?(new c).resolve(b):f._sendMessageWithReply({functionName:"getDataSource",
args:{dataSourceId:a}}).then(d.hitch(this,function(a){var b=new k(a.dataSource);return this._dataSourceProxies[a.dataSource.id]=b}))},_dataSourceRemoved:function(a){for(var b=0;b<this._dataSourceProxies.length;b++)if(this._dataSourceProxies[b].id===a){this._dataSourceProxies.splice(b,1);break}this.dataSourceRemoved(a);this.emit("data-source-removed",{dataSourceId:a})},dataSourceRemoved:function(a){},_dataSourceAdded:function(a){var b=new k(a);this._dataSourceProxies[a.dataSourceId]=b;this.dataSourceAdded(b);
this.emit("data-source-added",{dataSourceProxy:b})},dataSourceAdded:function(a){}})});