import{g as h,b as g,c as C,o as w,w as l,a as o,p as N,e as v,t as r,H as _,l as p,f as s,n as b}from"./index-CTxlYqj4.js";import{V as k,r as y}from"./VForm-CXf-xKYz.js";import{c as F,V as U,a as B,b as T}from"./VCard-DRptUN8Y.js";import{V as L}from"./VAlert-BiAap6Bx.js";import{V as R}from"./VFileInput-C7DE4B9B.js";import{V as $}from"./VTextField-CWm47OUI.js";import{V as j}from"./VSwitch-HiyBjUWI.js";import{V as q}from"./VSpacer-BcQnH0oL.js";import{V as x}from"./VAvatar-x_79C0tA.js";const D={class:"headline"},I={class:"m-label"},A={class:"m-label"},K={__name:"ImportForm",props:{helpText:{type:String,default:""},title:{type:String,default:""}},emits:["close","submit"],setup(f,{expose:V,emit:i}){const n=i,t=h({}),u=h(),{$gettext:e}=g();function c(){t.value={},n("close")}async function S(){const{valid:d}=await u.value.validate();if(!d)return;const a=new FormData;a.append("sourcefile",t.value.sourcefile[0]),t.value.sepchar&&a.append("sepchar",t.value.sepchar),t.value.continue_if_exists&&a.append("continue_if_exists",t.value.continue_if_exists),n("submit",a,t.value)}return V({close:c}),(d,a)=>(w(),C(T,null,{default:l(()=>[o(k,{ref_key:"vform",ref:u,onSubmit:N(S,["prevent"])},{default:l(()=>[o(F,null,{default:l(()=>[v("span",D,r(f.title),1)]),_:1}),o(U,null,{default:l(()=>[o(L,{variant:"tonal",type:"info",class:"mb-2"},{default:l(()=>[p(r(s(e)("Provide a CSV file where lines respect one of the following formats:"))+" ",1),_(d.$slots,"help"),p(" "+r(s(e)("The first element of each line is mandatory and must be equal to one of the previous values."))+" "+r(s(e)("You can use a different character as separator.")),1)]),_:3}),v("label",I,r(s(e)("Select file")),1),o(R,{modelValue:t.value.sourcefile,"onUpdate:modelValue":a[0]||(a[0]=m=>t.value.sourcefile=m),accept:"text/csv","truncate-length":"15",variant:"outlined",density:"compact",rules:[s(y).required]},null,8,["modelValue","rules"]),v("label",A,r(s(e)("Separator")),1),o($,{modelValue:t.value.sepchar,"onUpdate:modelValue":a[1]||(a[1]=m=>t.value.sepchar=m),rules:[s(y).required],variant:"outlined",density:"compact"},null,8,["modelValue","rules"]),o(j,{modelValue:t.value.continue_if_exists,"onUpdate:modelValue":a[2]||(a[2]=m=>t.value.continue_if_exists=m),label:s(e)("Continue on error"),color:"primary",density:"compact",hint:s(e)("Don't treat duplicated objects as errors"),"persistent-hint":""},null,8,["modelValue","label","hint"]),_(d.$slots,"extraFields",{form:t.value})]),_:3}),o(B,null,{default:l(()=>[o(q),o(x,{onClick:c},{default:l(()=>[p(r(s(e)("Close")),1)]),_:1}),o(x,{color:"primary",type:"submit"},{default:l(()=>[p(r(s(e)("Import")),1)]),_:1})]),_:1})]),_:3},512)]),_:3}))}},Q=()=>{function f(i,n,t){if(!i&&!i.length){b().displayNotification({msg:t("No data to export"),type:"warning"});return}const u=new Blob([i],{type:"text/csv"}),e=document.createElement("a");e.href=URL.createObjectURL(u),e.download=`modoboa-${n}.csv`,e.click(),URL.revokeObjectURL(e.href)}function V(i,n,t,u){i.importFromCSV(n).then(e=>{const c=b();e.data.status?(c.displayNotification({msg:e.data.message}),t.value.close()):c.displayNotification({msg:e.data.message,type:"error"})}).catch(()=>{b().displayNotification({msg:u("CSV seems to be badly formatted"),type:"error"})})}return{exportContent:f,importContent:V}};export{K as _,Q as i};
