/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import"../../../../core/has.js";import{rasterizeDash as t}from"../../../../symbols/cim/rasterizingUtils.js";import{log2 as e}from"./GeometryUtils.js";import i from"../webgl/Rect.js";import s from"../webgl/RectangleBinPack.js";import a from"../../../webgl/Texture.js";import{TextureDescriptor as h}from"../../../webgl/TextureDescriptor.js";const r="dasharray-";class o{constructor(t,e,i=0){this._mosaicPages=[],this._currentPage=0,this._maxItemSize=0,this._pageWidth=0,this._pageHeight=0,this._mosaicRects={},this.pixelRatio=1,e<=0&&console.error("Sprites mosaic defaultWidth and defaultHeight must be greater than zero!"),this._pageWidth=t,this._pageHeight=e,i>0&&(this._maxItemSize=i),this._binPack=new s(t-4,e-4),this._mosaicPages.push({mosaicsData:{type:"static",data:new Uint32Array(this._pageWidth*this._pageHeight)},size:[this._pageWidth,this._pageHeight],dirty:!0,texture:void 0})}destroy(){this.dispose()}dispose(){if(this._binPack=null,null!==this._mosaicPages){for(const t of this._mosaicPages){const e=t.texture;e&&e.dispose()}this._mosaicPages=null}}getWidth(t){return t<0||t>=this._mosaicPages.length?-1:this._mosaicPages[t].size[0]}getHeight(t){return t<0||t>=this._mosaicPages.length?-1:this._mosaicPages[t].size[1]}getPageSize(t){return t<0||t>=this._mosaicPages.length?null:this._mosaicPages[t].size}getTexture(t){return t<0||t>=this._mosaicPages.length?null:this._mosaicPages[t].texture||null}setSpriteSource(t){for(const e of this._mosaicPages)e.texture&&(e.texture.dispose(),e.texture=null),e.dirty=!0;if(0===this._mosaicPages.length){this._binPack=new s(this._pageWidth-4,this._pageHeight-4);const t=Math.floor(this._pageWidth)*Math.floor(this._pageHeight),e=new Uint32Array(t);for(let i=0;i<t;i++)e[i]=0;this._mosaicPages.push({mosaicsData:{type:"static",data:e},size:[this._pageWidth,this._pageHeight],dirty:!0,texture:null})}this._sprites=t}getSpriteItem(t,e=!1){let i,s,a=this._mosaicRects[t];if(a)return a;if(!this._sprites||"loaded"!==this._sprites.loadStatus)return null;if(t&&t.startsWith(r)?([i,s]=this._rasterizeDash(t),e=!0):i=this._sprites.getSpriteInfo(t),!i?.width||!i.height||i.width<0||i.height<0)return null;const h=i.width,o=i.height,[n,g,c]=this._allocateImage(h,o);return n.width<=0?null:(this._copy(n,i,g,c,e,s),a={type:"sprite",rect:n,width:h,height:o,sdf:i.sdf,simplePattern:!1,rasterizationScale:i.pixelRatio??1,samplingMode:"Linear",page:g},this._mosaicRects[t]=a,a)}getSpriteItems(t){const e={};for(const i of t)e[i.name]=this.getSpriteItem(i.name,i.repeat);return e}getMosaicItemPosition(t,e){const i=this.getSpriteItem(t,e),s=i?.rect;if(!s)return null;s.width=i.width,s.height=i.height;const a=i.width,h=i.height,r=2;return{tl:[s.x+r,s.y+r],br:[s.x+r+a,s.y+r+h],page:i.page}}bind(t,e,i=0,s=0){if(i>=this._mosaicPages.length||i>=this._mosaicPages.length)return;const r=this._mosaicPages[i];let o=r.texture;if(!o){const e=new h(r.size[0],r.size[1]);e.wrapMode=33071,o=new a(t,e),r.texture=o}if(o.setSamplingMode(e),t.bindTexture(o,s),r.dirty){const t=r.mosaicsData.data;o.setData(new Uint8Array(t.buffer))}r.dirty=!1}static _copyBits(t,e,i,s,a,h,r,o,n,g,c){let l=s*e+i,_=o*h+r;if(c){_-=h;for(let r=-1;r<=g;r++,l=((r+g)%g+s)*e+i,_+=h)for(let e=-1;e<=n;e++)a[_+e]=t[l+(e+n)%n]}else for(let m=0;m<g;m++){for(let e=0;e<n;e++)a[_+e]=t[l+e];l+=e,_+=h}}_copy(t,e,i,s,a,h){if(!this._sprites||"loaded"!==this._sprites.loadStatus||i>=this._mosaicPages.length)return;const r=this._mosaicPages[i],n=r.mosaicsData.data,g=new Uint32Array(h?h.buffer:this._sprites.image.buffer),c=2,l=h?e.width:this._sprites.width;o._copyBits(g,l,e.x,e.y,n,s[0],t.x+c,t.y+c,e.width,e.height,a),r.dirty=!0}_allocateImage(t,a){t+=2,a+=2;const h=Math.max(t,a);if(this._maxItemSize&&this._maxItemSize<h){const s=2**Math.ceil(e(t)),h=2**Math.ceil(e(a)),r=new i(0,0,t,a);return this._mosaicPages.push({mosaicsData:{type:"static",data:new Uint32Array(s*h)},size:[s,h],dirty:!0,texture:null}),[r,this._mosaicPages.length-1,[s,h]]}let r=t%4?4-t%4:4,o=a%4?4-a%4:4;1===r&&(r=5),1===o&&(o=5);const n=this._binPack.allocate(t+r,a+o);if(n.width<=0){const e=this._mosaicPages[this._currentPage];return e.dirty||(e.mosaicsData.data=null),this._currentPage=this._mosaicPages.length,this._mosaicPages.push({mosaicsData:{type:"static",data:new Uint32Array(this._pageWidth*this._pageHeight)},size:[this._pageWidth,this._pageHeight],dirty:!0,texture:null}),this._binPack=new s(this._pageWidth-4,this._pageHeight-4),this._allocateImage(t,a)}return[n,this._currentPage,[this._pageWidth,this._pageHeight]]}_rasterizeDash(e){const i=/\[(.*?)\]/,s=e.match(i);if(!s)return null;const a=s[1].split(",").map(Number),h=e.slice(e.lastIndexOf("-")+1),[r,o,n]=t(a,h);return[{x:0,y:0,width:o,height:n,sdf:!0,pixelRatio:1},new Uint8Array(r.buffer)]}}export{o as default};
