import{u as j,a as N,p as d,gk as T,F as m,j as s,c2 as S,gl as k,cz as E,I,V as P,T as $,a4 as B,r as z,e as W,ac as A,ae as U,cP as L,C as M}from"./bundle.CHKSNOJF.js";const R=B.div`
  border-color: ${t=>t.theme.borderColor};
  &:hover {
    border-color: ${t=>t.theme.hoverBorderColor};
  }
`;function O(t){if(t.name.toLowerCase().endsWith(".xml"))return!0;const o=t.type.toLowerCase().split(";")[0]?.trim()??"";return o.startsWith("application/xml")||o.startsWith("text/xml")||o.endsWith("+xml")}function X(t){return new Promise(r=>{try{const o=new FileReader;o.onload=l=>{const n=l.target?.result??"";if(!n.trim()){r(!1);return}const i=new DOMParser().parseFromString(n,"application/xml");r(i.getElementsByTagName("parsererror").length===0)},o.onerror=()=>r(!1),o.readAsText(t)}catch{r(!1)}})}function q(t){const[r]=j(),{entity:o}=t,l=N(),[n,i]=d.useState([]),[x,f]=d.useState(),[c,p]=d.useState(new FormData),[h,g]=d.useState(!1),y=()=>{if(!h){if(!n.length){m.error("select_file");return}return m.processing(),g(!0),f(void 0),z("POST",W("/api/v1/edocument/upload"),c).then(e=>{m.success(e?.data?.message??"success")}).catch(e=>{e.response?.status===422&&(m.dismiss(),f(e.response.data))}).finally(()=>{i([]),g(!1),p(new FormData)})}},F=()=>{n.forEach(e=>{c.append("documents[]",e)}),p(c)},w=e=>{const a=n.filter((K,_)=>e!==_),u=new FormData;u.append("import_type",o),u.append("_method","PUT"),i(a),p(u)},b=async e=>{for(let a=0;a<e.length;a++)if(!await X(e[a]))return!1;return!0},{getRootProps:v,getInputProps:C,isDragActive:D}=T({accept:{"application/xml":[".xml"]},onDrop:async e=>{await b(e)?e.every(O)?(i([...e]),c.append("import_type",o),c.append("_method","PUT"),p(c)):m.error("wrong_file_extension"):m.error("invalid_file")}});return d.useEffect(()=>{F()},[n]),d.useEffect(()=>()=>{i([]),p(new FormData)},[]),s.jsxs("div",{className:"flex flex-col space-y-4",children:[s.jsx(S,{leftSide:r("xml_file"),children:n.length?s.jsx("ul",{className:"grid xs:grid-rows-6 lg:grid-cols-2 gap-3",children:n.map((e,a)=>s.jsxs("li",{className:"flex items-center justify-between cursor-pointer p-2",style:{backgroundColor:l.$4},children:[e.name," - ",(e.size/1024).toPrecision(2)," KB"," ",s.jsx(I,{element:P,size:19,className:"cursor-pointer",onClick:()=>w(a)})]},a))}):s.jsxs("div",{...v(),className:"flex flex-col md:flex-row md:items-center",children:[s.jsxs(R,{className:"relative block w-full border-2 border-dashed rounded-lg p-12 text-center",theme:{borderColor:l.$21,hoverBorderColor:l.$17},children:[s.jsx("input",{...C()}),s.jsx("div",{className:"flex justify-center",children:s.jsx(k,{size:"2.3rem",color:l.$3})}),s.jsx("span",{className:"mt-2 block text-sm font-medium",style:{color:l.$3,colorScheme:l.$0},children:r(D?"drop_file_here":"dropzone_default_message")})]}),x&&Object.keys(x.errors).map((e,a)=>s.jsx(E,{children:x.errors[e]},a))]})}),s.jsx("div",{className:"flex justify-end pr-5",children:s.jsx($,{behavior:"button",onClick:y,disableWithoutIcon:!0,disabled:!n.length||h,children:r("import")})})]})}function G(){const{t}=j(),{documentTitle:r}=A("import"),o=[{name:t("expenses"),href:"/expenses"},{name:t("import"),href:"/expenses/import"}];return s.jsx(U,{title:r,breadcrumbs:o,children:s.jsx("div",{className:"grid grid-cols-12",children:s.jsxs("div",{className:"col-span-12 xl:col-span-10 space-y-4",children:[s.jsx(L,{entity:"expense",onSuccess:!1,type:"csv"}),s.jsx(M,{children:s.jsx(q,{entity:"expense",type:"xml"})})]})})})}export{G as default};
