/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/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}from"../../core/promiseUtils.js";import{property as l,subclass as n}from"../../core/accessorSupport/decorators.js";import{equalsWithProjectionEngine as a}from"../../geometry/support/spatialReferenceUtils.js";import{uploadProgressWeights as p}from"../../layers/graphics/sources/support/uploadProgressWeights.js";import{getSupportedExtensions as c}from"../../layers/support/infoFor3D.js";import{makeProgressManager as u,estimatedTransferTime as d}from"../../support/progressUtils.js";let f=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 m(e);i(r),this.progress=0,this.files=o;const s=u(p.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:c}=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 f=l.reduce((e,r)=>e+r.size,0),h=s.simulate("loadMesh",d(f));try{await n.load()}finally{h.remove()}if(c){return{type:!await a(e.spatialReference,c.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([l()],f.prototype,"files",void 0),e([l()],f.prototype,"progress",void 0),e([l()],f.prototype,"state",null),e([l()],f.prototype,"result",null),e([l()],f.prototype,"error",null),e([l()],f.prototype,"_uploadTask",void 0),e([l()],f.prototype,"_layer",void 0),f=e([n("esri.widgets.Editor.Upload")],f);let h=null;async function m(e){const{resolve:r,promise:o}=Promise.withResolvers(),t=document.createElement("input");t.type="file",t.multiple=!1,t.accept=[...c(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 h?h(t):t.click(),o.finally(()=>{i.remove(),t.remove()})}function y(e,r){h=o=>{r?.(),Promise.resolve().then(()=>e).then(e=>{if(!h)return;const r=new DataTransfer;e.forEach(e=>r.items.add(e)),o.files=r.files,o.dispatchEvent(new Event("change"))})}}function g(){h=null}export{f as Upload,g as clearPromptForFilesStub,y as stubFilePickerToSelect};
