"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react");function s(t){return t/1024}function f(t){return{...JSON.parse(JSON.stringify(t.blob)),[t.name]:t.data}}const b=({fileType:t,handleError:d})=>{try{throw d({error:{message:"Invalid file Type",fileTypes:t}}),new Error("Invalid file Type")}catch(e){console.error(e)}},g=t=>{try{throw t.handleError({error:{message:"Exceeded File Limit",limit:t.maxFile}}),Error("Exceeded File Limit")}catch(d){console.log(d)}},y=t=>{try{throw t.handleError({error:{message:t.message}}),Error(t.message)}catch(d){console.log(d)}},w=({multiple:t=!1,maxfileSize:d=200,...e})=>{const[l,c]=x.useState({});return{handleChange:a=>{const r=new FileReader,i=a.target.files,F=Number(s(i[0].size));return a.target.name?e.fileType&&!e.fileType.includes(i[0].type)?b({fileType:e.fileType,event:a,handleError:e.handleError}):(e.handleChange&&e.handleChange(i),F>d?y({message:"File too large",handleError:e.handleError}):(r.readAsDataURL(i[0]),r.onload=()=>{if(t)if(l==null?void 0:l[a.target.name]){const n=[...l[a.target.name],{blob:r.result,name:i[0].name,fileSize:Number(s(i[0].size))}],h=f({blob:l,name:a.target.name,data:n});if(e.maxFile&&h[a.target.name].length>e.maxFile)return g({fileState:h,handleError:e.handleError,maxFile:e.maxFile,name:a.target.name});c(h),a.target.value=""}else{const n=f({blob:l,name:a.target.name,data:[{blob:r.result,name:i[0].name,fileSize:Number(s(i[0].size))}]});if(e.maxFile&&n[a.target.name].length>e.maxFile)return g({fileState:n,handleError:e.handleError,maxFile:e.maxFile,name:a.target.name});c(n),a.target.value=""}else{const u=f({blob:l,name:a.target.name,data:[{blob:r.result,name:i[0].name,fileSize:Number(s(i[0].size))}]});if(e.maxFile&&u[a.target.name].length>e.maxFile)return g({fileState:u,handleError:e.handleError,maxFile:e.maxFile,name:a.target.name});c(u),a.target.value=""}},a)):y({message:"Add a name attribute to the input ",handleError:e.handleError})},fileData:l,handleOnDrop:async a=>{a.preventDefault();const r=a.currentTarget.getAttribute("data-type"),i=a.dataTransfer.files,F=Array.from(i);if(!r)return e.handleError({error:{message:"Error: add a dataType prop to the dropzone element "}});e.handleChange&&e.handleChange(i);const u=F.map(n=>new Promise((h,o)=>{const m=new FileReader;if(d{h({blob:m.result,name:n.name,fileSize:n.size})},m.onerror=()=>{e.handleError({message:"Error occurred while reading the file."}),o(new Error("Error occurred while reading the file."))},m.readAsDataURL(n)}));try{const h=(await Promise.allSettled(u)).filter(o=>o.status==="fulfilled").map(o=>o.value);if(t)if(l==null?void 0:l[r]){const m=[...l[r],...h],E=f({blob:l,name:r,data:m});if(e.maxFile&&E[r].length>e.maxFile)return g({fileState:E,handleError:e.handleError,maxFile:e.maxFile,name:r});c(E)}else{const m=f({blob:l,name:r,data:h});if(e.maxFile&&m[r].length>e.maxFile)return g({fileState:m,handleError:e.handleError,maxFile:e.maxFile,name:r});c(m)}else{const o=f({blob:l,name:r,data:[h[0]]});if(e.maxFile&&o[r].length>e.maxFile)return g({fileState:o,handleError:e.handleError,maxFile:e.maxFile,name:r});c(o)}}catch{console.error()}return a},setFiles:c}};exports.useUploadFile=w;