import{inject as I,ref as w,resolveComponent as _,createElementBlock as $,openBlock as u,createElementVNode as E,createBlock as C,createCommentVNode as L,renderSlot as W}from"/vue.js";import{m as t}from"./nc-main.DEgIQeOo.js";import{s as B}from"./nc-sql.DpfKNhOJ.js";import"./service-worker.DGZzV4eE.js";const R=["type","multiple"],S=Object.assign({name:"NcDroparea"},{__name:"nc-droparea",props:{icon:{type:String,default:"",required:!1},clickable:{type:Boolean,default:!0,required:!1},multiple:{type:Boolean,default:!1,required:!1}},emits:["import-file"],setup(i,{emit:d}){const g=i,p=d,o=I("state"),n=o.rec,l=w();function m(e){if(e[0]){if(e[0].size===0)t.setWarning(o,`Import from file '${e[0].name}' was not successful, file size is zero`);else if(e[0].name){const r=new FileReader;r.onloadend=function(s){p("import-file",e[0],s.target.result)},r.readAsDataURL(e[0])}return!0}return!1}function c(e){n.progress_bar&&(n.progress_bar.percent=0,n.progress_bar.message=""),e.target.value="",e.target.files=[]}function f(e){const r=e.dataTransfer.files||e.target.files;if(!r){c(e),t.setWarning(o,"Importing from file failed");return}const s=`Importing from file '${r[0].name}'...`;n.progress_bar?(n.progress_bar.percent=40,n.progress_bar.message=s):t.setInfo(o,s),setTimeout(async()=>{if(l.value.classList.remove("nc-droparea-hover"),typeof r!="object"||r[0]==null||r[0].name==null){c(e),t.setWarning(o,`Import from file '${r[0]}' was not successful`);return}(!e.target.files||e.target.files.length===0)&&(e.target.files=r),B().then(y=>{y.promise("SELECT * FROM FILE(?, {headers: true})",[e]).then(a=>{if(a.length==null){if(m(r))return;t.setWarning(o,`Import from file '${r[0].name}' did not succeed`)}else a.length>0?(t.setInfo(o,`Import from file '${r[0].name}' was successful, imported ${a.length} rows`),p("import-file",r[0],a)):t.setWarning(o,`Import from file '${r[0].name}' was not successful, imported 0 rows`)}).catch(a=>{m(r)||t.setWarning(o,`Selected file is not a valid import file, error: ${a?a.message.substring(0,300):"unknown error"}.`)}).finally(()=>{c(e)})})},100)}function h(e){g.clickable&&f(e)}function b(e){e.stopPropagation(),e.preventDefault(),f(e)}function D(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.dropEffect="copy",l.value.classList.add("nc-droparea-hover")}function k(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.dropEffect="none",l.value.classList.remove("nc-droparea-hover")}return(e,r)=>{const s=_("nc-icon");return u(),$("label",{class:"nc-droparea",ref_key:"droparea",ref:l,onDragover:D,onDragleave:k,onDrop:b},[E("input",{id:"import-file",type:i.clickable?"file":"hidden",multiple:i.multiple?"":void 0,onClick:h,onChange:f},null,40,R),i.icon?(u(),C(s,{key:0,class:"nc-droparea-icon fa-2x",icon:i.icon},null,8,["icon"])):L("",!0),W(e.$slots,"default")],544)}}});export{S as default};
