import{DEFAULT_WMS_VERSION}from"./common.js";import EventType from"../events/EventType.js";import ImageSource,{defaultImageLoadFunction}from"./Image.js";import ImageWrapper from"../Image.js";import WMSServerType from"./WMSServerType.js";import{appendParams}from"../uri.js";import{assert}from"../asserts.js";import{assign}from"../obj.js";import{calculateSourceResolution}from"../reproj.js";import{compareVersions}from"../string.js";import{containsExtent,getCenter,getForViewAndSize,getHeight,getWidth}from"../extent.js";import{get as getProjection,transform}from"../proj.js";const GETFEATUREINFO_IMAGE_SIZE=[101,101];class ImageWMS extends ImageSource{constructor(e){e=e||{};super({attributions:e.attributions,imageSmoothing:e.imageSmoothing,projection:e.projection,resolutions:e.resolutions}),this.crossOrigin_=void 0!==e.crossOrigin?e.crossOrigin:null,this.url_=e.url,this.imageLoadFunction_=void 0!==e.imageLoadFunction?e.imageLoadFunction:defaultImageLoadFunction,this.params_=e.params||{},this.v13_=!0,this.updateV13_(),this.serverType_=e.serverType,this.hidpi_=void 0===e.hidpi||e.hidpi,this.image_=null,this.imageSize_=[0,0],this.renderedRevision_=0,this.ratio_=void 0!==e.ratio?e.ratio:1.5}getFeatureInfoUrl(e,t,i,r){if(void 0!==this.url_){var i=getProjection(i),s=this.getProjection(),a=(s&&s!==i&&(t=calculateSourceResolution(s,i,e,t),e=transform(e,i,s)),getForViewAndSize(e,t,0,GETFEATUREINFO_IMAGE_SIZE));const o={SERVICE:"WMS",VERSION:DEFAULT_WMS_VERSION,REQUEST:"GetFeatureInfo",FORMAT:"image/png",TRANSPARENT:!0,QUERY_LAYERS:this.params_.LAYERS};assign(o,this.params_,r);r=Math.floor((e[0]-a[0])/t),e=Math.floor((a[3]-e[1])/t);return o[this.v13_?"I":"X"]=r,o[this.v13_?"J":"Y"]=e,this.getRequestUrl_(a,GETFEATUREINFO_IMAGE_SIZE,1,s||i,o)}}getLegendUrl(e,t){if(void 0!==this.url_){const r={SERVICE:"WMS",VERSION:DEFAULT_WMS_VERSION,REQUEST:"GetLegendGraphic",FORMAT:"image/png"};if(void 0===t||void 0===t.LAYER){var i=this.params_.LAYERS;if(!(!Array.isArray(i)||1===i.length))return;r.LAYER=i}return void 0!==e&&(i=this.getProjection()?this.getProjection().getMetersPerUnit():1,r.SCALE=e*i*39.37*(25.4/.28)),assign(r,t),appendParams(this.url_,r)}}getParams(){return this.params_}getImageInternal(e,t,i,r){if(void 0===this.url_)return null;var s=(t=this.findNearestResolution(t))/(i=1==i||this.hidpi_&&void 0!==this.serverType_?i:1),a=getCenter(e),o=Math.ceil(getWidth(e)/s),n=Math.ceil(getHeight(e)/s),o=getForViewAndSize(a,s,0,[o,n]),n=Math.ceil(this.ratio_*getWidth(e)/s),e=Math.ceil(this.ratio_*getHeight(e)/s),a=getForViewAndSize(a,s,0,[n,e]);const g=this.image_;if(g&&this.renderedRevision_==this.getRevision()&&g.getResolution()==t&&g.getPixelRatio()==i&&containsExtent(g.getExtent(),o))return g;n={SERVICE:"WMS",VERSION:DEFAULT_WMS_VERSION,REQUEST:"GetMap",FORMAT:"image/png",TRANSPARENT:!0},assign(n,this.params_),this.imageSize_[0]=Math.round(getWidth(a)/s),this.imageSize_[1]=Math.round(getHeight(a)/s),e=this.getRequestUrl_(a,this.imageSize_,i,r,n);return this.image_=new ImageWrapper(a,t,i,e,this.crossOrigin_,this.imageLoadFunction_),this.renderedRevision_=this.getRevision(),this.image_.addEventListener(EventType.CHANGE,this.handleImageChange.bind(this)),this.image_}getImageLoadFunction(){return this.imageLoadFunction_}getRequestUrl_(e,t,i,r,s){if(assert(void 0!==this.url_,9),s[this.v13_?"CRS":"SRS"]=r.getCode(),"STYLES"in this.params_||(s.STYLES=""),1!=i)switch(this.serverType_){case WMSServerType.GEOSERVER:var a=90*i+.5|0;"FORMAT_OPTIONS"in s?s.FORMAT_OPTIONS+=";dpi:"+a:s.FORMAT_OPTIONS="dpi:"+a;break;case WMSServerType.MAPSERVER:s.MAP_RESOLUTION=90*i;break;case WMSServerType.CARMENTA_SERVER:case WMSServerType.QGIS:s.DPI=90*i;break;default:assert(!1,8)}s.WIDTH=t[0],s.HEIGHT=t[1];const o=r.getAxisOrientation();let n;return n=this.v13_&&"ne"==o.substr(0,2)?[e[1],e[0],e[3],e[2]]:e,s.BBOX=n.join(","),appendParams(this.url_,s)}getUrl(){return this.url_}setImageLoadFunction(e){this.image_=null,this.imageLoadFunction_=e,this.changed()}setUrl(e){e!=this.url_&&(this.url_=e,this.image_=null,this.changed())}updateParams(e){assign(this.params_,e),this.updateV13_(),this.image_=null,this.changed()}updateV13_(){var e=this.params_.VERSION||DEFAULT_WMS_VERSION;this.v13_=0<=compareVersions(e,"1.3")}}export default ImageWMS;