// 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/layers/WCSConnection","dojo/_base/declare dojo/_base/lang dojo/io-query dojo/has dojo/_base/Deferred dojo/DeferredList ../deferredUtils ../request ../kernel ./WCSCapabilities ./WCSCoverageDescription".split(" "),function(f,e,r,t,k,u,y,p,v,w,x){var q=function(d,c,b){var a;if(b)for(a=0;a<d.length;a++){if(d[a][b].toLowerCase()===c.toLowerCase())return d[a]}else for(a=0;a<d.length;a++)if(d[a].toLowerCase()===c.toLowerCase())return d[a]};f=f(null,{declaredClass:"esri.layers.WCSConnection",
url:null,supportedVersions:["1.0.0","1.1.0","1.1.1","2.0.1"],version:null,name:null,onlineResources:null,coverages:null,supportedFormats:null,profiles:null,supportedInterpolations:null,onConnected:null,constructor:function(d,c){if(d){var b=d.indexOf("?");-1<b&&b<=d.length-1?(this.optionalParameters=r.queryToObject(d.substring(b+1,d.length)),this.url=d.substring(0,b+1)):this.url=d}this.optionalParameters=this.optionalParameters||{};c&&e.mixin(this.optionalParameters,c);this.optionalParameters.version&&
(this.version=this.optionalParameters.version);var a=this.optionalParameters;Object.keys(a).forEach(function(b){a[b]||delete a[b]});this.connect=e.hitch(this,this.connect);this.url&&this.connect(this.version)},_getCapabilities:function(d){var c=new k,b=e.mixin({},this.optionalParameters),a=["service","request","coverageId","coverage","identifier"];Object.keys(b).forEach(function(d){a.some(function(a){return a.toLowerCase()===d.toLowerCase()})&&delete b[d]});p({url:this.url,content:e.mixin({request:"GetCapabilities",
service:"WCS",version:d,AcceptVersions:d},b),handleAs:"xml"}).then(e.hitch(this,function(a){a=new w(a);c.resolve(a)}),function(a){c.reject(a)});return c.promise},_getCapabilitiesHelper:function(d){var c;if(d)c=this._getCapabilities(d);else{var b=new k;c=b.promise;d=-1<this.url.toLowerCase().indexOf("wcsserver")?"2.0.1":"1.0.0";this._getCapabilities(d).then(e.hitch(this,function(a){!this.version&&-1<this.url.toLowerCase().indexOf("wcsserver")?1===a.supportedVersions.length&&"2.0.1"===a.supportedVersions[0]?
this._getCapabilities().then(e.hitch(this,function(a){b.resolve(a)}),e.hitch(this,function(a){b.reject(a)})):b.resolve(a):b.resolve(a)}),e.hitch(this,function(a){b.reject(a)}))}return c},_describeCoverage:function(){var d=new k,c=this.coverageId||this.optionalParameters.coverage||this.optionalParameters.coverageId||this.optionalParameters.identifiers||this.coverages.map(function(a){return a.id}).join(","),b=this.version,a={request:"DescribeCoverage",service:"WCS",version:b};switch(b){case "1.0.0":a=
e.mixin(a,{coverage:c});break;case "1.1.0":case "1.1.1":case "1.1.2":a=e.mixin(a,{identifiers:c});break;case "2.0.1":a=e.mixin(a,{coverageId:c})}var g=e.mixin({},this.optionalParameters),h=["service","request"];Object.keys(g).forEach(function(a){h.some(function(b){return b.toLowerCase()===a.toLowerCase()})&&delete g[a]});var f=p({url:this._onlineResources?this._onlineResources.describeCoverage:this.url,content:e.mixin(a,g),handleAs:"xml"});"2.0.1"===b&&(b=c,-1<c.indexOf(",")?b=c.split(",").map(function(a){return 0===
a.toLowerCase().indexOf("coverage")?a.slice(8):a}).join(","):0===c.toLowerCase().indexOf("coverage")&&(b=c.slice(8)),c=p({url:this._onlineResources?this._onlineResources.describeCoverage:this.url,content:e.mixin(a,g,{identifiers:b,version:"1.1.1"}),handleAs:"xml"}),f=new u([f,c]));f.then(e.hitch(this,function(a){var b;if("2.0.1"===this.version){if(b=a[0][0]&&a[0][1]?this._parseCoverageDescriptions(a[0][1],this.version):null,(a=a[1][0]&&a[1][1]?this._parseCoverageDescriptions(a[1][1],"1.1.1"):null)&&
a.length){var c,e,g,f,h,k,l,m,n;for(l=0;l<b.length;l++)if(c=b[l].multidimensionalInfo,e=a[l].multidimensionalInfo,c&&e)for(n=0;n<c.variables.length;n++)if(g=c.variables[n],f=q(e.variables,g.name,"name"))for(m=0;m<g.dimensions.length;m++)if(h=g.dimensions[m],k=q(f.dimensions,h.name,"name"))h.values=h.values||k.values,h.values&&(h.hasRegularIntervals=!1)}}else b=this._parseCoverageDescriptions(a,this.version);d.resolve(b)}),function(a){d.reject(a)});return d.promise},_parseCoverageDescriptions:function(d,
c){var b,a,e=[];b="1.0.0"===c?d.getElementsByTagNameNS("*","CoverageOffering"):d.getElementsByTagNameNS("*","CoverageDescription");for(a=0;a<b.length;a++)e.push(new x(b[a],c));return e},connect:function(d){var c=new k(function(){return"cancelled"});this._connectPromise&&this._connectPromise.cancel();this.version=(d=d||this.version)||this.version;var b=function(a){c.reject(a);this._connectPromise=null;if(this.onConnectionFailed)this.onConnectionFailed(a)};this._getCapabilitiesHelper(d).then(e.hitch(this,
function(a){if(!c.isCanceled()){this.supportedInterpolations=a.supportedInterpolations;this.supportedVersions=a.supportedVersions;-1<this.url.toLowerCase().indexOf("wcsserver")?("2.0.1"===this.supportedVersions[0]&&1===this.supportedVersions.length&&(this.supportedVersions=["1.0.0","1.1.0","1.1.1","2.0.1"]),this.version=this.version||this.supportedVersions.reduce(function(a,b){return a>b?a:b})):this.version=this.version||"1.0.0";this.name=this.name||a.name;this.version=this.version||a.version;this.onlineResources=
this.onlineResources||a.onlineResources;this.coverages=this.coverages||a.coverages;if(void 0===this.coverages||null===this.coverages||0===this.coverages.length)throw"No valid coverages";this.supportedFormats=this.supportedFormats||a.supportedFormats;this.profiles=this.profiles||a.profiles;this._describeCoverage().then(e.hitch(this,function(a){if(!c.isCanceled())if(a){if(a.forEach(e.hitch(this,function(a){a.lonLatEnvelope=q(this.coverages,a.id,"id").lonLatEnvelope||a.lonLatEnvelope})),this.coverages=
a,c.resolve(this),this._connectPromise=null,this.onConnected)this.onConnected()}else b("cannot parse coverage descriptions")}),b)}}),b);return this._connectPromise=c.promise}});t("extend-esri")&&e.setObject("layers.WCSConnection",f,v);return f});