/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/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,onAbort as r}from"../../../core/promiseUtils.js";import{property as a}from"../../../core/accessorSupport/decorators/property.js";import{subclass as h}from"../../../core/accessorSupport/decorators/subclass.js";import{whenVideoPlayable as n}from"../../../layers/support/videoUtils.js";import{fixedImageSize as l}from"../../OrientedImageryViewer/constants.js";import m from"./PanoramicMediaError.js";const c=0,d=0,p=0,u=new Set(["JPG","JPEG","TIFF","TIF","MRF"]),z=new Set(["MRF","TIFF","TIF"]);let w=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])):c}get horizonRoll(){const{matrix:i}=this;return i?o(Math.atan(i[7]/i[8])):p}get horizonYaw(){const{matrix:i,cameraHeading:t}=this;return i?o(Math.atan2(i[3],i[0])):t?-t:d}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||!u.has(o))throw new m("unsupported-format",this.uiStrings["unsupported-format"]??"Unsupported image format: {extension}",{datasetFormat:o??"UNKNOWN FORMAT"});return z.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=[...l];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=Promise.withResolvers(),e=r(i?.signal,o.resolve),a="video"===this.type?document.createElement("video"):document.createElement("img");a.crossOrigin="anonymous",a.src=this.url;let h=null;const l="video"===this.type?n(a,this.addHandles):await new Promise((i,o)=>{h=[t(a,"error",i=>{o(i)}),t(a,"load",i)]}).finally(()=>{h?.forEach(i=>i.remove()),h=null}),c=Promise.race([l,o.promise]);this.addResolvingPromise(c);try{await c}finally{e?.remove()}s(i);const d=a;if(!d)throw new m("unknown","Could not load media",{url:this.url});const{createImageSphere:p}=await import("../utils.js");s(i);const u=await p({data:a,horizonPitch:this.horizonPitch,horizonRoll:this.horizonRoll,horizonYaw:this.horizonYaw}).load(i);return s(i),{media:d,mesh:u}}};i([a()],w.prototype,"_size",void 0),i([a()],w.prototype,"autoLoad",void 0),i([a()],w.prototype,"cameraHeading",void 0),i([a()],w.prototype,"extension",void 0),i([a()],w.prototype,"horizonPitch",null),i([a()],w.prototype,"horizonRoll",null),i([a()],w.prototype,"horizonYaw",null),i([a()],w.prototype,"id",void 0),i([a()],w.prototype,"imagePyramid",void 0),i([a()],w.prototype,"matrix",void 0),i([a()],w.prototype,"size",null),i([a()],w.prototype,"type",void 0),i([a()],w.prototype,"uiStrings",void 0),i([a()],w.prototype,"url",void 0),w=i([h("esri.widgets.PanoramicViewer.support.PanoramicMedia")],w);const y=w;export{y as default};
