/*! phoscon-app 2024-10-07 */

initApp();const form=document.querySelector("#fileInfo");var submitBtn=document.querySelector("#submitDDF"),input=document.getElementById("fileInput"),importResult=document.getElementById("importResult"),filesToSend=[];importResult.classList.remove("error"),importResult.classList.remove("success"),importResult.innerHTML="",input.addEventListener("change",function(event){importResult.classList.remove("error"),importResult.classList.remove("success"),importResult.innerHTML="";let count=0;filesToSend=[];for(let i=0;i<input.files.length;i++){const f=input.files[i];if(-1!=f.name.indexOf(".zip")){count++;var reader=new FileReader;reader.onload=(e=>{const zipped=new Uint8Array(e.target.result),files=fflate.unzipSync(zipped,{filter:file=>(-1!=file.name.indexOf(".ddf")||-1!=file.name.indexOf(".ddb"))&&file.originalSize<=2e6});for(const fileName in files)filesToSend.push(new Blob([files[fileName]]))}),reader.readAsArrayBuffer(input.files[0])}else-1==f.name.indexOf(".ddf")&&-1==f.name.indexOf(".ddb")||(filesToSend.push(f),count++)}0!==count?submitBtn.classList.remove("disabled"):submitBtn.classList.add("disabled")}),form.addEventListener("submit",async event=>{if(event.preventDefault(),submitBtn.classList.contains("disabled"))return;let done=0;for(const fileOrBlob of filesToSend)try{const formData=new FormData;formData.append("file",fileOrBlob),(await fetch("//"+app.session.gw+"/api/"+app.apikey+"/ddf/bundles",{method:"POST",body:formData})).ok&&done++}catch(err){console.log(err)}0!=done&&done==filesToSend.length?(importResult.classList.remove("error"),importResult.classList.add("success"),importResult.innerHTML=translate("success_import")):(importResult.classList.add("error"),importResult.classList.remove("success"),importResult.innerHTML=translate("fail_import")),filesToSend=[]});