/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","../../../../core/maybe","../../../../libs/basisu/BasisU","../../../../libs/basisu/TextureFormat","../../../webgl/enums","../../../webgl/Texture","../../../webgl/Util"],(function(e,t,r,n,s,i,a,o){"use strict";let u=null,l=null;function c(){return g.apply(this,arguments)}function g(){return(g=t._asyncToGenerator((function*(){return r.isNone(l)&&(l=n.getBasisTranscoder(),u=yield l),l}))).apply(this,arguments)}function m(e,t){if(r.isNone(u))return e.byteLength;const n=new u.BasisFile(new Uint8Array(e)),s=d(n)?p(n.getNumLevels(0),n.getHasAlpha(),n.getImageWidth(0,0),n.getImageHeight(0,0),t):0;return n.close(),n.delete(),s}function T(e,t){if(r.isNone(u))return e.byteLength;const n=new u.KTX2File(new Uint8Array(e)),s=h(n)?p(n.getLevels(),n.getHasAlpha(),n.getWidth(),n.getHeight(),t):0;return n.close(),n.delete(),s}function p(e,t,r,n,s){const a=o.getBytesPerElementFormat(t?i.CompressedTextureFormat.COMPRESSED_RGBA8_ETC2_EAC:i.CompressedTextureFormat.COMPRESSED_RGB8_ETC2),u=s&&e>1?(4**e-1)/(3*4**(e-1)):1;return Math.ceil(r*n*a*u)}function d(e){return e.getNumImages()>=1&&!e.isUASTC()}function h(e){return e.getFaces()>=1&&e.isETC1S()}function _(e,t,r){return C.apply(this,arguments)}function C(){return(C=t._asyncToGenerator((function*(e,t,n){r.isNone(u)&&(u=yield c());const s=new u.BasisFile(new Uint8Array(n));if(!d(s))return null;s.startTranscoding();const i=B(e,t,s.getNumLevels(0),s.getHasAlpha(),s.getImageWidth(0,0),s.getImageHeight(0,0),((e,t)=>s.getImageTranscodedSizeInBytes(0,e,t)),((e,t,r)=>s.transcodeImage(r,0,e,t,0,0)));return s.close(),s.delete(),i}))).apply(this,arguments)}function E(e,t,r){return y.apply(this,arguments)}function y(){return(y=t._asyncToGenerator((function*(e,t,n){r.isNone(u)&&(u=yield c());const s=new u.KTX2File(new Uint8Array(n));if(!h(s))return null;s.startTranscoding();const i=B(e,t,s.getLevels(),s.getHasAlpha(),s.getWidth(),s.getHeight(),((e,t)=>s.getImageTranscodedSizeInBytes(e,0,0,t)),((e,t,r)=>s.transcodeImage(r,e,0,0,t,0,-1,-1)));return s.close(),s.delete(),i}))).apply(this,arguments)}function B(e,t,r,n,o,u,l,c){const{compressedTextureETC:g,compressedTextureS3TC:m}=e.capabilities,[T,p]=g?n?[s.TextureFormat.ETC2_RGBA,i.CompressedTextureFormat.COMPRESSED_RGBA8_ETC2_EAC]:[s.TextureFormat.ETC1_RGB,i.CompressedTextureFormat.COMPRESSED_RGB8_ETC2]:m?n?[s.TextureFormat.BC3_RGBA,i.CompressedTextureFormat.COMPRESSED_RGBA_S3TC_DXT5_EXT]:[s.TextureFormat.BC1_RGB,i.CompressedTextureFormat.COMPRESSED_RGB_S3TC_DXT1_EXT]:[s.TextureFormat.RGBA32,i.PixelFormat.RGBA],d=t.hasMipmap?r:Math.min(1,r),h=[];for(let s=0;s<d;s++)h.push(new Uint8Array(l(s,T))),c(s,T,h[s]);const _=h.length>1,C=_?i.TextureSamplingMode.LINEAR_MIPMAP_LINEAR:i.TextureSamplingMode.LINEAR,E={...t,samplingMode:C,hasMipmap:_,internalFormat:p,width:o,height:u};return new a.Texture(e,E,{type:"compressed",levels:h})}e.createTextureBasis=_,e.createTextureKTX2=E,e.estimateMemoryBasis=m,e.estimateMemoryKTX2=T,e.loadBasis=c,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
