import{t as g,$ as p,K as e,b as w,X as j,a9 as n,o as y}from"./index-CqfREQhZ.js";const b=[["path",{d:"M12 3v12",key:"1x0j5s"}],["path",{d:"m17 8-5-5-5 5",key:"7q97r8"}],["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",key:"ih7n3h"}]],m=g("upload",b);function k({value:r,onChange:c,folder:i="uploads"}){const d=p.useRef(null),[u,l]=p.useState(!1),x=async(t,s)=>{try{const a=new FormData;a.append("file",t),a.append("folder",s);const o=await y("/api/upload",{method:"POST",body:a});if(!o.ok){const h=await o.json().catch(()=>({error:o.statusText}));return console.warn("Server upload failed:",h),null}return(await o.json()).url}catch(a){return console.warn("Server upload exception:",a),null}},f=async t=>{if(!t.type.startsWith("image/")){n.error("Please choose an image file");return}if(t.size>5*1024*1024){n.error("Image must be under 5 MB");return}l(!0);const s=t.name.split(".").pop()||"jpg";`${i}${Date.now()}${Math.random().toString(36).slice(2,8)}${s}`;const a=await x(t,i);if(!a){n.error("Failed to upload image. Please try again."),l(!1);return}c(a),l(!1),n.success("Image uploaded successfully")};return e.jsxs("div",{className:"flex flex-col sm:flex-row items-start sm:items-center gap-3",children:[e.jsx("input",{ref:d,type:"file",accept:"image/*",className:"hidden",onChange:t=>{const s=t.target.files?.[0];s&&f(s),t.currentTarget.value=""}}),e.jsx("div",{className:"w-20 h-20 rounded-xl bg-secondary overflow-hidden border border-border grid place-items-center shrink-0",children:r?e.jsx("img",{src:r,alt:"",className:"w-full h-full object-cover"}):e.jsx(m,{className:"w-5 h-5 text-muted-foreground"})}),e.jsxs("div",{className:"flex-1 min-w-0 flex flex-col gap-1.5",children:[e.jsxs("button",{type:"button",disabled:u,onClick:()=>d.current?.click(),className:"w-full sm:w-auto inline-flex items-center justify-center gap-2 h-9 px-3 rounded-lg bg-[#002D72] text-white text-xs font-semibold hover:opacity-90 disabled:opacity-60",children:[u?e.jsx(w,{className:"w-4 h-4 animate-spin"}):e.jsx(m,{className:"w-4 h-4"}),r?"Replace image":"Upload image"]}),r&&e.jsxs("button",{type:"button",onClick:()=>c(""),className:"inline-flex items-center gap-1 text-xs text-destructive hover:underline",children:[e.jsx(j,{className:"w-3 h-3"})," Remove"]}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"PNG/JPG, up to 5 MB"})]})]})}export{k as I,m as U};
