/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import r from"../../core/Accessor.js";import{createTask as o}from"../../core/asyncUtils.js";import t from"../../core/Error.js";import{on as s}from"../../core/events.js";import{throwIfAborted as i,createResolver as l}from"../../core/promiseUtils.js";import{property as n,subclass as a}from"../../core/accessorSupport/decorators.js";import{equalsWithProjectionEngine as p}from"../../geometry/support/spatialReferenceUtils.js";import{uploadProgressWeights as c}from"../../layers/graphics/sources/support/uploadProgressWeights.js";import{getSupportedExtensions as u}from"../../layers/support/infoFor3D.js";import{makeProgressManager as d,estimatedTransferTime as f}from"../../support/progressUtils.js";let h=class extends r{constructor(){super(...arguments),this.files=[],this.progress=0,this._uploadTask=null,this._layer=null}destroy(){this.cancel(),this.files=[],this._layer=null,this._uploadTask=null}get state(){const e=this._uploadTask;return e&&0!==this.files.length?e.finished?e.error?"error":"success":"pending":"default"}get result(){return this._uploadTask?.value??null}get error(){return this._uploadTask?.error??null}uploadTo(e){this.cancel(),this.files=[],this._layer=e,this._uploadTask=o(async r=>{const o=await y(e);i(r),this.progress=0,this.files=o;const s=d(c.upload,e=>{this.progress=e},"Upload.uploadTo");if(0===o.length)return null;i(r);const l=await e.extractAndFilterFiles(o);i(r),l.length>0&&(this.files=l);const{mesh:n,georeferenceInfo:a}=await e.convertMesh(l,{signal:r,onProgress:s.makeOnProgress("createFromFiles")});if(i(r),!n)throw new t("editor:upload","could not upload or convert model");const u=l.reduce((e,r)=>e+r.size,0),h=s.simulate("loadMesh",f(u));try{await n.load()}finally{h.remove()}if(a){return{type:!await p(e.spatialReference,a.origin.spatialReference)?"georeferenced-reprojected":"georeferenced",mesh:n}}return{type:"non-georeferenced",mesh:n}})}retry(){this._layer&&this.uploadTo(this._layer)}cancel(){this._uploadTask?.abort()}};e([n()],h.prototype,"files",void 0),e([n()],h.prototype,"progress",void 0),e([n()],h.prototype,"state",null),e([n()],h.prototype,"result",null),e([n()],h.prototype,"error",null),e([n()],h.prototype,"_uploadTask",void 0),e([n()],h.prototype,"_layer",void 0),h=e([a("esri.widgets.Editor.Upload")],h);let m=null;async function y(e){const{resolve:r,promise:o}=l(),t=document.createElement("input");t.type="file",t.multiple=!1,t.accept=[...u(e.infoFor3D),".zip"].join(","),t.style.display="none",document.body.appendChild(t);const i=s(t,"change",()=>r(t.files?Array.from(t.files):[]));return m?m(t):t.click(),o.finally(()=>{i.remove(),t.remove()})}function g(e,r){m=o=>{r?.(),Promise.resolve().then(()=>e).then(e=>{if(!m)return;const r=new DataTransfer;e.forEach(e=>r.items.add(e)),o.files=r.files,o.dispatchEvent(new Event("change"))})}}function v(){m=null}export{h as Upload,v as clearPromptForFilesStub,g as stubFilePickerToSelect};
