/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as i}from"tslib";import{on as t}from"../../../core/events.js";import{rad2deg as o}from"../../../core/mathUtils.js";import{EsriPromise as e}from"../../../core/Promise.js";import{throwIfAborted as s,createResolver as r,onAbort as a}from"../../../core/promiseUtils.js";import{property as h}from"../../../core/accessorSupport/decorators/property.js";import{subclass as n}from"../../../core/accessorSupport/decorators/subclass.js";import{whenVideoPlayable as l}from"../../../layers/support/videoUtils.js";import{fixedImageSize as m}from"../../OrientedImageryViewer/constants.js";import c from"./PanoramicMediaError.js";const d=0,p=0,u=0,z=new Set(["JPG","JPEG","TIFF","TIF","MRF"]),w=new Set(["MRF","TIFF","TIF"]);let y=class extends e{constructor(i){super(i),this._size=[0,0],this._cache=null,this.autoLoad=!1,this.cameraHeading=null,this.extension=null,this.imagePyramid=null,this.type="image",this.uiStrings={}}destroy(){this._cache=null}get horizonPitch(){const{matrix:i}=this;return i?o(Math.asin(-i[6])):d}get horizonRoll(){const{matrix:i}=this;return i?o(Math.atan(i[7]/i[8])):u}get horizonYaw(){const{matrix:i,cameraHeading:t}=this;return i?o(Math.atan2(i[3],i[0])):t?-t:p}get size(){return this._size}async load(i){const{guessExtensionFromURI:t}=await import("../../OrientedImageryViewer/support/urlUtils.js");if(s(i),"video"===this.type)return this.loadVideoMesh(i);let o=this.extension;if(o||(o=t(this.url)),!o||!z.has(o))throw new c("unsupported-format",this.uiStrings["unsupported-format"]??"Unsupported image format: {extension}",{datasetFormat:o??"UNKNOWN FORMAT"});return w.has(o)?this.loadOptimizedImageMesh(i):this.loadImageMesh(i)}async loadOptimizedImageMesh(i){const{loadMeshes:t,loadPyramid:o,loadRaster:e}=await import("../utils.js"),r=await e(this.url,i);s(i),this.imagePyramid=await o(r,{pitch:this.horizonPitch,yaw:this.horizonYaw,roll:this.horizonRoll},i),s(i);const a=this.imagePyramid.getLowResolutionTiles(),h=await t(a,i);return s(i),this._size=this.imagePyramid.imageSize,{meshOrMeshes:h,size:this._size,tileKeys:a?.map(i=>i.key)}}async loadVideoMesh(i){const{createImageSphere:t}=await import("../utils.js");if(s(i),this._cache){const o=await t({data:this._cache,horizonPitch:this.horizonPitch,horizonRoll:this.horizonRoll,horizonYaw:this.horizonYaw}).load(i);return s(i),{meshOrMeshes:o,size:this._size}}const o=await this._resolveMedia(i);s(i),this._cache=o.media,this._size=[...m];const e=await t({data:this._cache,horizonPitch:this.horizonPitch,horizonRoll:this.horizonRoll,horizonYaw:this.horizonYaw}).load(i);return s(i),{meshOrMeshes:e,size:this._size}}async loadImageMesh(i){const{createImageSphere:t}=await import("../utils.js");if(s(i),this._cache){const o=await t({data:this._cache,horizonPitch:this.horizonPitch,horizonRoll:this.horizonRoll,horizonYaw:this.horizonYaw}).load(i);return s(i),{meshOrMeshes:o,size:this._size}}const o=await this._resolveMedia(i);s(i),this._cache=o.media;const e=o.mesh;return this._size=[o.media.width,o.media.height],{meshOrMeshes:e,size:this._size}}async _resolveMedia(i){const o=r(),e=a(i?.signal,o.resolve),h="video"===this.type?document.createElement("video"):document.createElement("img");h.crossOrigin="anonymous",h.src=this.url;let n=null;const m="video"===this.type?l(h,this.addHandles):await new Promise((i,o)=>{n=[t(h,"error",i=>{o(i)}),t(h,"load",i)]}).finally(()=>{n?.forEach(i=>i.remove()),n=null}),d=Promise.race([m,o.promise]);this.addResolvingPromise(d);try{await d}finally{e?.remove()}s(i);const p=h;if(!p)throw new c("unknown","Could not load media",{url:this.url});const{createImageSphere:u}=await import("../utils.js");s(i);const z=await u({data:h,horizonPitch:this.horizonPitch,horizonRoll:this.horizonRoll,horizonYaw:this.horizonYaw}).load(i);return s(i),{media:p,mesh:z}}};i([h()],y.prototype,"_size",void 0),i([h()],y.prototype,"autoLoad",void 0),i([h()],y.prototype,"cameraHeading",void 0),i([h()],y.prototype,"extension",void 0),i([h()],y.prototype,"horizonPitch",null),i([h()],y.prototype,"horizonRoll",null),i([h()],y.prototype,"horizonYaw",null),i([h()],y.prototype,"id",void 0),i([h()],y.prototype,"imagePyramid",void 0),i([h()],y.prototype,"matrix",void 0),i([h()],y.prototype,"size",null),i([h()],y.prototype,"type",void 0),i([h()],y.prototype,"uiStrings",void 0),i([h()],y.prototype,"url",void 0),y=i([n("esri.widgets.PanoramicViewer.support.PanoramicMedia")],y);const g=y;export{g as default};
