/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as t}from"tslib";import{ClonableMixin as e}from"../../core/Clonable.js";import{wrapImageData as a}from"../../core/imageUtils.js";import{JSONSupport as r}from"../../core/JSONSupport.js";import{property as n,subclass as s}from"../../core/accessorSupport/decorators.js";import{ensureClass as o}from"../../core/accessorSupport/ensureType.js";import{getMeshTextureCloneContext as i}from"./meshUtils/meshCloneUtils.js";import{t as l,w as c}from"../../chunks/persistableUrlUtils.js";import{writer as p}from"../../core/accessorSupport/decorators/writer.js";import{reader as d}from"../../core/accessorSupport/decorators/reader.js";var h;const u=new WeakMap;let m=0,g=class extends(e(r)){static{h=this}constructor(t){super(t),this.wrap="repeat"}get url(){return this._get("url")||null}set url(t){this._set("url",t),t&&this._set("data",null)}get data(){return this._get("data")||null}set data(t){this._set("data",t),t&&this._set("url",null)}writeData(t,e,a,r){if(t instanceof HTMLImageElement){const n={type:"image-element",src:l(t.src,r),crossOrigin:t.crossOrigin};e[a]=n}else if(t instanceof HTMLCanvasElement){const r={type:"canvas-element",imageData:f(t.getContext("2d").getImageData(0,0,t.width,t.height))};e[a]=r}else if(t instanceof HTMLVideoElement){const n={type:"video-element",src:l(t.src,r),autoplay:t.autoplay,loop:t.loop,muted:t.muted,crossOrigin:t.crossOrigin,preload:t.preload};e[a]=n}else if(t instanceof ImageData){const r={type:"image-data",imageData:f(t)};e[a]=r}}readData(t){switch(t.type){case"image-element":{const e=new Image;return e.src=t.src,e.crossOrigin=t.crossOrigin,e}case"canvas-element":{const e=w(t.imageData),a=document.createElement("canvas");return a.width=e.width,a.height=e.height,a.getContext("2d").putImageData(e,0,0),a}case"image-data":return w(t.imageData);case"video-element":{const e=document.createElement("video");return e.src=t.src,e.crossOrigin=t.crossOrigin,e.autoplay=t.autoplay,e.loop=t.loop,e.muted=t.muted,e.preload=t.preload,e}default:return}}get transparent(){const{data:t,url:e}=this,a=t=>t?.toLowerCase().endsWith(".png")||t?.toLocaleLowerCase().startsWith("data:image/png;");return t instanceof HTMLCanvasElement?y(t.getContext("2d").getImageData(0,0,t.width,t.height)):t instanceof ImageData?y(t):!(!e||!a(e))||!!(t instanceof HTMLImageElement&&a(t.src))}set transparent(t){this._overrideIfSome("transparent",t)}get contentHash(){const t="string"==typeof this.wrap?this.wrap:"object"==typeof this.wrap?`${this.wrap.horizontal}/${this.wrap.vertical}`:"",e=(e="")=>`d:${e},t:${this.transparent},w:${t}`;return null!=this.url?e(this.url):null!=this.data?this.data instanceof HTMLImageElement||this.data instanceof HTMLVideoElement?e(this.data.src):(u.has(this.data)||u.set(this.data,++m),e(u.get(this.data))):e()}get memoryUsage(){let t=0;if(t+=null!=this.url?this.url.length:0,null!=this.data){const e=this.data;"data"in e?t+=e.data.byteLength:e instanceof HTMLImageElement?t+=e.naturalWidth*e.naturalHeight*3:e instanceof HTMLCanvasElement&&(t+=e.width*e.height*3)}return t}clone(t){const e=i(t),a=e?.textureMap?.get(this);if(a)return a;const r=super.clone(t);return e?.textureMap?.set(this,r),r}static from(t){return"string"==typeof t?new h({url:t}):t instanceof HTMLImageElement||t instanceof HTMLCanvasElement||t instanceof ImageData||t instanceof HTMLVideoElement?new h({data:t}):o(h,t)}};function f(t){let e="";for(let a=0;a<t.data.length;a++)e+=String.fromCharCode(t.data[a]);return{data:btoa(e),width:t.width,height:t.height}}function w(t){const e=atob(t.data),r=new Uint8ClampedArray(e.length);for(let a=0;a<e.length;a++)r[a]=e.charCodeAt(a);return a(r,t.width,t.height)}function y(t){for(let e=3;e<t.data.length;e+=4)if(255!==t.data[e])return!0;return!1}t([n({type:String,json:{write:c}})],g.prototype,"url",null),t([n({clonable:"reference",json:{write:{overridePolicy(){return{enabled:!this.url}}}}})],g.prototype,"data",null),t([p("data")],g.prototype,"writeData",null),t([d("data")],g.prototype,"readData",null),t([n({type:Boolean,json:{write:{overridePolicy(){return{enabled:this._isOverridden("transparent")}}}}})],g.prototype,"transparent",null),t([n({json:{write:!0}})],g.prototype,"wrap",void 0),t([n({readOnly:!0})],g.prototype,"contentHash",null),g=h=t([s("esri.geometry.support.MeshTexture")],g);export{g as default};
