import{r as d,cd as c,j as t,ca as Z,d as R}from"./vendor-BKmmeuxg.js";import{B as ee}from"./button-BpNwv2aE.js";import{I as te}from"./input-DM0goJrn.js";import{t as se}from"./index-D9-QKOod.js";import{I as ae}from"./input-error-LaPr_4wd.js";const ce=({storage:z,isSubmit:F,courseId:X,sectionId:A,filetype:K,delayUpload:O=!1,onError:u,onCancelUpload:N,onFileSelected:p,onFileUploaded:_})=>{const[r,w]=d.useState(null),[E,M]=d.useState(null),[S,m]=d.useState(""),[B,h]=d.useState(0),[y,o]=d.useState("idle"),k=d.useRef(null),b=d.useRef(null),T=1024*1024*1024,v=5*1024*1024;d.useEffect(()=>{var a,i;c.defaults.withCredentials=!0;const s=(a=document.cookie.split("; ").find(n=>n.startsWith("XSRF-TOKEN=")))==null?void 0:a.split("=")[1];if(s)c.defaults.headers.common["X-XSRF-TOKEN"]=decodeURIComponent(s);else{const n=(i=document.querySelector('meta[name="csrf-token"]'))==null?void 0:i.getAttribute("content");n&&(c.defaults.headers.common["X-CSRF-TOKEN"]=n)}},[]),d.useEffect(()=>{F&&P()},[F]);const D=s=>{if(s.target.files&&s.target.files.length>0){const a=s.target.files[0];if(a.size>T){m(`File is too large. Maximum file size is ${T/(1024*1024)} MB`);return}w(a),m(""),o("idle"),h(0),O?p&&p(a):P()}},P=async()=>{var s,a,i,n;if(r){o("initializing"),m("");try{const l=Math.ceil(r.size/v),e=await c.post("/dashboard/uploads/chunked/initialize",{storage:z,filename:r.name,mimetype:r.type,filesize:(r.size||0)/1024,filetype:K,total_chunks:l,course_id:X,course_section_id:A},{timeout:12e4});if(e.data.success)M(e.data.upload_id),await U(e.data.upload_id,l);else throw new Error(e.data.message||"Failed to initialize upload")}catch(l){o("error"),m(((a=(s=l.response)==null?void 0:s.data)==null?void 0:a.message)||l.message||"Failed to initialize upload"),u&&u(((n=(i=l.response)==null?void 0:i.data)==null?void 0:n.message)||l.message||"Failed to initialize upload")}}},U=async(s,a)=>{var n,l,e,j;if(!r)return;o("uploading"),b.current=new AbortController;const i=b.current.signal;try{const f=[];let $=0;for(let g=0;g{x.onloadend=()=>{const Y=x.result;I(Y)},x.onerror=()=>V(x.error),x.readAsDataURL(J)}),C=await c.post(`/dashboard/uploads/chunked/${s}/chunk`,{storage:z,chunk_data:Q,part_number:g+1,filename:r.name,mimetype:r.type},{signal:i,timeout:12e4,maxContentLength:1/0,maxBodyLength:1/0});if(C.data.success){$++;const I=Math.round($/a*100);h(I),f.push({PartNumber:g+1,ETag:C.data.etag||""})}else throw new Error(C.data.message||"Failed to upload chunk")}if(i.aborted)return;await q(s)}catch(f){if(i.aborted){o("idle"),h(0);return}o("error"),m(((l=(n=f.response)==null?void 0:n.data)==null?void 0:l.message)||f.message||"Failed to upload file chunks"),u&&u(((j=(e=f.response)==null?void 0:e.data)==null?void 0:j.message)||f.message||"Failed to upload file chunks")}},q=async s=>{var a,i,n,l;o("completing");try{const e=await c.post(`/dashboard/uploads/chunked/${s}/complete`,{storage:z},{timeout:12e4});if(e.data.success){o("completed");const j={file_path:e.data.file_path,file_url:e.data.file_url,signed_url:e.data.signed_url,mime_type:e.data.mime_type,file_name:e.data.file_name,file_size:e.data.file_size};_==null||_(j),setTimeout(()=>{k.current&&(k.current.value=""),w(null),M(null),h(0),o("idle")},3e3)}else throw new Error(e.data.message||"Failed to complete upload")}catch(e){o("error"),m(((i=(a=e.response)==null?void 0:a.data)==null?void 0:i.message)||e.message||"Failed to complete upload"),u&&u(((l=(n=e.response)==null?void 0:n.data)==null?void 0:l.message)||e.message||"Failed to complete upload")}},W=async()=>{if(E&&y!=="idle"&&y!=="completed"){N==null||N(),b.current&&b.current.abort();try{await c.delete(`/dashboard/uploads/chunked/${E}/abort`)}catch(s){se.error("Error aborting upload:"+s)}o("idle"),h(0),k.current&&(k.current.value=""),w(null)}},G=()=>{switch(y){case"initializing":return t.jsxs("div",{className:"flex items-center",children:[t.jsx(R,{className:"mr-2 h-4 w-4 animate-spin"}),"Initializing upload..."]});case"uploading":return t.jsxs("div",{className:"flex items-center",children:[t.jsx(R,{className:"mr-2 h-4 w-4 animate-spin"}),"Uploading file chunks..."]});case"completing":return t.jsxs("div",{className:"flex items-center",children:[t.jsx(R,{className:"mr-2 h-4 w-4 animate-spin"}),"Finalizing upload..."]});case"completed":return t.jsxs("div",{className:"text-secondary-foreground flex items-center",children:[t.jsx(Z,{className:"mr-2 h-4 w-4"}),"Completed upload"]});default:return null}};return t.jsxs("div",{children:[t.jsxs("div",{className:"relative overflow-hidden rounded-sm",children:[t.jsx(te,{type:"file",name:"file",onChange:s=>{var i;D(s);const a=(i=s.target.files)==null?void 0:i[0];a&&(w(a),p==null||p(a))}}),y==="uploading"&&r&&t.jsxs("div",{className:"absolute top-0 left-0 z-10 flex h-full w-full items-center justify-between",children:[t.jsxs("div",{className:"relative h-full w-full overflow-hidden bg-gray-200",children:[t.jsx("div",{className:"bg-secondary absolute top-0 left-0 h-full transition-all duration-300 ease-in-out",style:{width:`${B}%`}}),t.jsxs("div",{className:"relative z-10 flex h-full items-center justify-between gap-2 px-2 text-xs",children:[t.jsxs("span",{children:[B,"%"]}),G(),t.jsxs("span",{className:"text-gray-800",children:["Size: (",(r?r.size/(1024*1024):0).toFixed(2)," MB)"]})]})]}),t.jsx("div",{className:"bg-gray-200",children:t.jsx(ee,{type:"button",variant:"destructive",onClick:W,className:"text-xs",children:"Cancel"})})]})]}),S&&t.jsx(ae,{message:S})]})};export{ce as C};