(function(l,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(l=typeof globalThis<"u"?globalThis:l||self,u(l.maplibreglRasterReprojection={}))})(this,function(l){"use strict";var st=Object.defineProperty;var it=(l,u,p)=>u in l?st(l,u,{enumerable:!0,configurable:!0,writable:!0,value:p}):l[u]=p;var v=(l,u,p)=>it(l,typeof u!="symbol"?u+"":u,p);const u={maxCache:10};class p{constructor(t){v(this,"options");v(this,"items",{});v(this,"lru",[]);this.options={...u,...t}}removeLeastRecentlyUsed(){let t=0;for(;t=this.options.maxCache;){const o=this.lru[t];if(this.items[o].listeners){t+=1;continue}delete this.items[o],this.lru.splice(t,1)}}touchItem(t){const o=this.lru.indexOf(t);o>-1&&(this.lru.splice(o,1),this.lru.push(t))}pushItem(t,o){this.items[t]=o,this.lru.push(t)}publish(t,o,e){var i;this.items[t].item=o,e&&(this.items[t].error=`${e}`),(i=this.items[t].listeners)==null||i.forEach(n=>n(o,e)),delete this.items[t].listeners,this.removeLeastRecentlyUsed()}subscribe(t,o){const e=this.items[t];(e==null?void 0:e.item)!==void 0?(this.touchItem(t),o(e.item,e.error)):e!=null&&e.listeners?(this.touchItem(t),e.listeners.push(o)):(this.pushItem(t,{listeners:[o]}),this.options.fetchTile(t).then(i=>this.publish(t,i)).catch(i=>this.publish(t,null,i)))}async getTile(t){return new Promise((o,e)=>{this.subscribe(t,(i,n)=>{if(n)return e(n);o(i)})})}}const O=(s,t)=>{const o=document.createElement("canvas");o.width=s,o.height=t;const e=o.getContext("2d");if(!e)throw new Error("Failed to create canvas context 2d");return e},A=async s=>await new Promise((t,o)=>{s.toBlob(async e=>{if(!e)return t(null);try{const i=await e.arrayBuffer();t(i)}catch(i){o(i)}})}),B=async s=>{const t=new Image;return t.crossOrigin="",t.src=s,await t.decode(),t},H=(s,t,o)=>{const{destinationTileSize:e,sourceTileSize:i,interval:n,sourceToPixel:a,destinationToSource:r,pixelToDestination:c,destinationToPixel:h}=s,m=O(e,e),T=o.tile[2],f=h([o.bbox[0],o.bbox[1]],T,e),j=h([o.bbox[2],o.bbox[3]],T,e),g=[Math.min(f[0],j[0]),Math.min(f[1],j[1])];for(let x=0;x{const o=t[0].tile[2],e=s.sourceToPixel([Math.min(...t.map(r=>r.bbox[0])),Math.min(...t.map(r=>r.bbox[1]))],o,s.sourceTileSize),i=s.sourceToPixel([Math.max(...t.map(r=>r.bbox[2])),Math.max(...t.map(r=>r.bbox[3]))],o,s.sourceTileSize),n=[Math.min(e[0],i[0]),Math.min(e[1],i[1]),Math.max(e[0],i[0]),Math.max(e[1],i[1])],a=O(n[2]-n[0],n[3]-n[1]);for(const{bbox:r,image:c}of t){if(!c)continue;const h=s.sourceToPixel([r[0],r[1]],o,s.sourceTileSize),m=s.sourceToPixel([r[2],r[3]],o,s.sourceTileSize),T=[Math.min(h[0],m[0]),Math.min(h[1],m[1]),Math.max(h[0],m[0]),Math.max(h[1],m[1])];a.drawImage(c,T[0]-n[0],T[1]-n[1])}return{translate:[n[0],n[1]],zoom:o,canvas:a.canvas}},F=(s,t,o)=>{const e=_(s,t);return H(s,e,o)},N=async({ctx:s,destinationRequest:t,sourceRequests:o,checkCanceled:e})=>{if(e())return null;let i=await Promise.all(o.map(async n=>{const a=await s.cache.getTile(n.url).catch(()=>null);return{...n,image:a}}));return i=i.filter(n=>n.image),!(i!=null&&i.length)||e()?null:F(s,i,t)},q=s=>{const[,t,...o]=s.split(/:\/\//),e=o.join("://"),i=new URLSearchParams(t),n=[+(i.get("x")??0),+(i.get("y")??0),+(i.get("z")??0)],a=(i.get("bbox")??"").split(",").map(f=>+f),r=(i.get("interval")??"").split(",").map(f=>+f),c=+(i.get("size")??0),h=+(i.get("ssize")??c),m=+(i.get("dsize")??c),T=+(i.get("zoffset")??0);return{bbox:a,tile:n,urlTemplate:e,interval:i.has("interval")?r:void 0,sourceTileSize:i.has("ssize")||i.has("size")?h:void 0,destinationTileSize:i.has("dsize")||i.has("size")?m:void 0,zoomOffset:i.has("zoffset")?T:void 0}},G=(s,t,o)=>s.replace("{sbbox}",`${o.join(",")}`).replace("{sxmin}",`${o[0]}`).replace("{symin}",`${o[1]}`).replace("{sxmax}",`${o[2]}`).replace("{symax}",`${o[3]}`).replace("{sx}",`${t[0]}`).replace("{sy}",`${t[1]}`).replace("{sz}",`${t[2]}`),W=async(s,t,o)=>{const e=q(t.url),i={...s,sourceTileSize:e.sourceTileSize??s.sourceTileSize,destinationTileSize:e.destinationTileSize??s.destinationTileSize,interval:e.interval??s.interval,zoomOffset:e.zoomOffset??s.zoomOffset},n=i.destinationTileToSourceTiles({bbox:e.bbox,tile:e.tile},i.zoomOffset).map(c=>({...c,url:G(e.urlTemplate,c.tile,c.bbox)})),a=await N({ctx:i,sourceRequests:n,destinationRequest:{tile:e.tile,bbox:e.bbox},checkCanceled:()=>o.signal.aborted});return a?{data:await A(a.canvas)}:{data:null}},Z={protocol:"reproject",cacheSize:10,interval:[1,1],tileSize:256},J=s=>{const t={...s,...Z},e={cache:new p({fetchTile:i=>B(i),maxCache:t.cacheSize}),destinationTileSize:(t==null?void 0:t.destinationTileSize)??t.tileSize,destinationTileToSourceTiles:t.destinationTileToSourceTiles,destinationToPixel:t.destinationToPixel,destinationToSource:t.destinationToSource,interval:t.interval,pixelToDestination:t.pixelToDestination,sourceTileSize:(t==null?void 0:t.sourceTileSize)??t.tileSize,sourceToPixel:t.sourceToPixel,zoomOffset:t.zoomOffset};return{protocol:t.protocol,loader:(...i)=>W(e,...i)}},C=s=>s/360+.5,y=s=>-s/180+.5,w=s=>{const t=Math.floor(s);return[2<{const[e,i]=w(o),n=C(s),a=y(t),r=n*e,c=a*i;return[r,c,o]},K=(s,t)=>{const o=R(s[0],s[3],t),e=R(s[2],s[1],t),i=[];for(let n=Math.floor(o[0]);n{const[e,i]=w(o),n=360/e,a=180/i,r=s/e,c=t/i,h=r*360-180,m=-c*180+90;return[h,m-a,h+n,m]},V=Math.PI/180,X=180/Math.PI,E=6378137,Y=Math.PI*2*E,d=Math.PI*E,$=(s,t)=>Y/t/(1<{const o=s/d*180;let e=t/d*180;return e=X*(2*Math.atan(Math.exp(e*V))-Math.PI/2),[o,e]},k=([s,t],o,e)=>{const i=$(o,e),n=(s+d)/i,a=(t+d)/i;return[n,a]},tt=([s,t],o,e)=>{const i=$(o,e),n=s*i-d,a=t*i-d;return[n,a]},D=([s,t],o,e)=>[s,(e<{const e=k(s,t,o);return D(e,t,o)},pixelToDestination:(s,t,o)=>{const e=et(s,t,o);return tt(e,t,o)},destinationToSource:([s,t])=>I([s,t]),sourceToPixel:(s,t,o)=>{const e=w(t);return[C(s[0])*e[0]*o,y(s[1])*e[1]*o]},destinationTileToSourceTiles:(s,t=0)=>{const o=[...I([s.bbox[0],s.bbox[1]]),...I([s.bbox[2],s.bbox[3]])],e=s.tile[2];return K(o,e+t).map(n=>{const a=Q(n);return{tile:n,bbox:a}})},zoomOffset:-1};l.createProtocol=J,l.epsg4326ToEpsg3857Presets=ot,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});