// 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/arcgis/AppProxyManager","dojo/_base/declare dojo/_base/lang ../kernel dojo/uacss dojo/Deferred dojo/promise/all dojo/json dojo/Evented dojo/Stateful ../request ./utils".split(" "),function(h,f,p,q,k,v,l,r,t,m,u){h=h([r,t],{constructor:function(a){this.defaults={appid:"",referrers:[],proxies:[]};a=f.mixin({},this.defaults,a);this.set(a);this._init()},updateProxy:function(a){var b=new k;if(!a||!a.proxyId)return b.reject("A proxyId is required to update a proxy"),b.promise;var c={sourceUrl:a.sourceUrl};
a.intervalSeconds&&!isNaN(a.intervalSeconds)&&(c.intervalSeconds=a.intervalSeconds);a.hitsPerInterval&&!isNaN(a.hitsPerInterval)&&(c.hitsPerInterval=a.hitsPerInterval);var d=this._sharingBaseUrl+"content/users/"+this._owner+"/";this._folderId&&(d+=this._folderId+"/");d+="items/"+this.appid+"/proxies/"+a.proxyId+"/update";m({url:d,content:{proxy:l.stringify(c),f:"json"},callbackParamName:"callback"},{usePost:!0}).then(f.hitch(this,function(a){a=a&&a.appProxies||[];this.set("proxies",a);b.resolve(a)}),
b.reject);return b.promise},createProxies:function(a){var b=new k;this._registerApp(this._appItem).then(f.hitch(this,function(){for(var c={referrers:this.referrers},d=[],e=0;e<a.length;e++){var g=a[e],n={sourceUrl:g.sourceUrl};g.intervalSeconds&&!isNaN(g.intervalSeconds)&&(n.intervalSeconds=g.intervalSeconds);g.hitsPerInterval&&!isNaN(g.hitsPerInterval)&&(n.hitsPerInterval=g.hitsPerInterval);d.push(n)}e=this._sharingBaseUrl+"content/users/"+this._owner+"/";this._folderId&&(e+=this._folderId+"/");
e+="items/"+this.appid+"/createProxies";m({url:e,content:{proxies:l.stringify(d),serviceProxyParams:l.stringify(c),f:"json"},callbackParamName:"callback"},{usePost:!0}).then(f.hitch(this,function(a){a=a&&a.appProxies||[];this.set("proxies",a);b.resolve(a)}),b.reject)}),b.reject);return b.promise},deleteProxies:function(a){var b=new k,c={f:"json"},d=[];if(a&&a.length)for(var e=0;e<a.length;e++)d.push(a[e].proxyId);d&&d.length&&(c.proxies=d.toString());a=this._sharingBaseUrl+"content/users/"+this._owner+
"/";this._folderId&&(a+=this._folderId+"/");a+="items/"+this.appid+"/deleteProxies";m({url:a,content:c,callbackParamName:"callback"},{usePost:!0}).then(f.hitch(this,function(a){var c=this.proxies.slice();a=a&&a.results||[];if(c&&c.length&&a&&a.length)for(var d=0;d<c.length;d++)for(var e=c[d],g=e.proxyId,f=0;f<a.length;f++){var h=a[f].proxyId;h&&g&&h===g&&(e.proxied=!1)}this.set("proxies",c);b.resolve(c)}),b.reject);return b.promise},_parseUrl:function(a){var b=document.createElement("a");b.href=a;
return b},_init:function(){this.appid?u.getItem(this.appid).then(f.hitch(this,function(a){this._appItem=a;this._folderId=a.item.ownerFolder;var b=this._parseUrl(a.item.url);this._sharingBaseUrl="https://"+b.hostname+"/sharing/rest/";var c=b.pathname.substring(0,b.pathname.lastIndexOf("/")),c="/"===c.charAt(0)?c:"/"+c,d=b.hostname+c;this.referrers.push("http://"+d,"https://"+d,"http://www.arcgis.com"+c,"https://www.arcgis.com"+c);-1!==b.hostname.indexOf("mapsdevext.arcgis.com")&&this.referrers.push("http://devext.arcgis.com"+
c,"https://devext.arcgis.com"+c);-1!==b.hostname.indexOf("mapsqaext.arcgis.com")&&this.referrers.push("http://qaext.arcgis.com"+c,"https://qaext.arcgis.com"+c);this._owner=a.item.owner;-1!==a.item.typeKeywords.indexOf("App Proxy")&&a.item.appProxies&&this.set("proxies",a.item.appProxies);this.emit("load",{});this.set("loaded",!0)})):console.error("AppProxyManager: appid required.")},_registerApp:function(a){var b=new k,c=a.item.id,d=this._sharingBaseUrl+"oauth2/",e=this.referrers;this._registered&&
this._registered===this.appid?b.resolve(a):-1!==a.item.typeKeywords.indexOf("Registered App")?b.resolve(a):m({url:d+"/registerApp",content:{itemId:c,appType:"browser",redirect_uris:l.stringify(e),f:"json"},callbackParamName:"callback"},{usePost:!0}).then(f.hitch(this,function(a){this._registered=this.appid;b.resolve(a)}),b.reject);return b.promise}});q("extend-esri")&&f.setObject("arcgis.AppProxyManager",h,p);return h});