/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../request.js";import r from"../../core/Error.js";import{getImageData as o}from"../../core/imageUtils.js";import{isAbsolute as i,isDataProtocol as n,isBlobProtocol as s,dataToBlob as a,join as l}from"../../core/urlUtils.js";import{generateUUID as m}from"../../core/uuid.js";import{property as c,subclass as p}from"../../core/accessorSupport/decorators.js";import u from"./MediaElementBase.js";import{mediaElementUrlProperty as h,mediaTypeProperty as d}from"./mediaUtils.js";import{getResourceContentExtension as g}from"../../portal/support/resourceExtension.js";import{ImageElementSymbol as f}from"../../support/mediaLayerUtils.js";import{f as y,t as v,i as I}from"../../chunks/persistableUrlUtils.js";import{reader as j}from"../../core/accessorSupport/decorators/reader.js";import{writer as b}from"../../core/accessorSupport/decorators/writer.js";var w;let E=class extends u{static{w=f}constructor(t){super(t),this.animationOptions=null,this.content=null,this.image=null,this.type="image",this[w]=!0,this.image=null}load(){const t=this.image;if("string"==typeof t){const e=o(t).then(t=>{this._set("content",t)});this.addResolvingPromise(e)}else if(t instanceof HTMLImageElement){const e=t.decode().then(()=>{this._set("content",t)});this.addResolvingPromise(e)}else t?this._set("content",t):this.addResolvingPromise(Promise.reject(new r("image-element:invalid-image-type","Invalid image type",{image:t})));return Promise.resolve(this)}get contentWidth(){return null==this.content?0:this.content instanceof HTMLImageElement?this.content.naturalWidth:this.content.width}get contentHeight(){return null==this.content?0:this.content instanceof HTMLImageElement?this.content.naturalHeight:this.content.height}readImage(t,e,r){return y(e.url,r)}writeImage(t,e,r,o){if(null==t)return;const n=o?.portalItem,s=o?.resources;if(!n||!s)return void("string"==typeof t&&(e[r]=v(t,o)));const a=U(t)?t:null;if(a){if(null==I(a))return void(e[r]=a);const t=v(a,{...o,verifyItemRelativeUrls:o?.verifyItemRelativeUrls?{writtenUrls:o.verifyItemRelativeUrls.writtenUrls,rootPath:void 0}:void 0},1);if(n&&t&&!i(t))return s.toKeep.push({resource:n.resourceFromPath(t),compress:!1}),void(e[r]=t)}e[r]="<pending>",s.pendingOperations.push(H(t).then(t=>{const o=L(t,n);e[r]=o.itemRelativeUrl,s.toAdd.push({resource:o,content:{type:"blob",blob:t},compress:!1,finish:t=>{this.image=t.url}})}))}};function U(t){return"string"==typeof t&&!n(t)&&!s(t)}async function H(t){if("string"==typeof t){if(n(t))return a(t);return(await e(t,{responseType:"blob"})).data}return new Promise(e=>P(t).toBlob(e))}function P(t){if(t instanceof HTMLCanvasElement)return t;const e=t instanceof HTMLImageElement?t.naturalWidth:t.width,r=t instanceof HTMLImageElement?t.naturalHeight:t.height,o=document.createElement("canvas"),i=o.getContext("2d");return o.width=e,o.height=r,t instanceof HTMLImageElement?i.drawImage(t,0,0,t.width,t.height):t instanceof ImageData&&i.putImageData(t,0,0),o}function L(t,e){const r=m(),o=`${l("media",r)}.${g({type:"blob",blob:t})}`;return e.resourceFromPath(o)}t([c()],E.prototype,"animationOptions",void 0),t([c({readOnly:!0})],E.prototype,"content",void 0),t([c({readOnly:!0})],E.prototype,"contentWidth",null),t([c({readOnly:!0})],E.prototype,"contentHeight",null),t([c(h)],E.prototype,"image",void 0),t([j("image",["url"])],E.prototype,"readImage",null),t([b("image")],E.prototype,"writeImage",null),t([c(d)],E.prototype,"type",void 0),E=t([p("esri.layers.support.ImageElement")],E);export{E as default};
