import{a as e}from"./rolldown-runtime-B0Z9INg1.js";import{$ as t}from"./vendor-charts-iXlq6Rne.js";import{c as n}from"./api-CqQWIBIg.js";import{t as r}from"./star-C2msZNg8.js";import{t as i}from"./upload-CE-0Hc0c.js";import{_ as a}from"./vendor-flow-IzlrcDx6.js";import{Rn as o,Sn as s,rn as c}from"./index-HkVQGc9M.js";import{t as l}from"./extractErrorMessage-DBWk03ld.js";import{Dn as u}from"./mastersRtkApi-BlaMikAe.js";var d=e(t(),1),f=a(),p=10;function m({value:e,onChange:t,multiple:a=!1,folder:m=`uploads`,label:h,hint:g,max:_=8}){let[v]=u(),[y,b]=(0,d.useState)(0),[x,S]=(0,d.useState)(null),C=(0,d.useRef)(null),w=e=>e.map((e,t)=>({...e,sort_order:t})),T=e=>!e.length||e.some(e=>e.is_primary)?e:e.map((e,t)=>({...e,is_primary:t===0})),E=e=>t(T(w(e))),D=()=>C.current?.click(),O=async t=>{if(!t?.length)return;let r=Array.from(t),i=[];for(let e of r){if(!e.type.startsWith(`image/`)){n.error(`${e.name}: not an image`);continue}if(e.size>p*1024*1024){n.error(`${e.name}: larger than ${p} MB`);continue}b(e=>e+1);try{let t=new FormData;t.append(`file`,e),t.append(`folder`,m);let n=await v(t).unwrap();i.push({url:n.url,filename:n.filename,size_bytes:n.size_bytes,content_type:e.type})}catch(e){n.error(l(e))}finally{b(e=>Math.max(0,e-1))}}i.length&&E(a?[...e,...i].slice(0,_):i.slice(-1))},k=t=>E(e.filter((e,n)=>n!==t)),A=n=>t(e.map((e,t)=>({...e,is_primary:t===n}))),j=n=>{if(x===null||x===n){S(null);return}let r=[...e],[i]=r.splice(x,1);r.splice(n,0,i),S(null),t(w(r))},M=a?e.length<_:e.length===0,N=y>0;return(0,f.jsxs)(`div`,{className:`space-y-1.5`,children:[h&&(0,f.jsx)(`label`,{className:`block text-[13px] font-medium text-[var(--color-text-primary)]`,children:h}),(0,f.jsx)(`input`,{ref:C,type:`file`,accept:`image/*`,multiple:a,className:`hidden`,onChange:e=>{O(e.target.files),e.target.value=``}}),(0,f.jsxs)(`div`,{className:`flex flex-wrap gap-3`,children:[e.map((e,t)=>(0,f.jsxs)(`div`,{draggable:a,onDragStart:()=>S(t),onDragOver:e=>{a&&e.preventDefault()},onDrop:()=>j(t),onClick:a?void 0:D,className:c(`relative w-24 h-24 rounded-lg overflow-hidden border bg-[var(--color-background)] group`,e.is_primary?`border-[var(--color-accent)] ring-2 ring-[var(--color-accent)]/40`:`border-[var(--color-border)]`,a?`cursor-move`:`cursor-pointer`),children:[(0,f.jsx)(`img`,{src:e.url,alt:e.alt_text||``,className:`w-full h-full object-cover`,onError:e=>{e.target.style.opacity=`0.25`}}),(0,f.jsx)(`button`,{type:`button`,onClick:e=>{e.stopPropagation(),k(t)},className:`absolute top-1 right-1 w-5 h-5 rounded-full bg-black/60 text-white grid place-items-center opacity-0 group-hover:opacity-100 transition`,title:`Remove`,children:(0,f.jsx)(s,{className:`w-3 h-3`})}),a&&(0,f.jsx)(`button`,{type:`button`,onClick:e=>{e.stopPropagation(),A(t)},title:e.is_primary?`Primary image`:`Set as primary`,className:c(`absolute top-1 left-1 w-5 h-5 rounded-full grid place-items-center transition`,e.is_primary?`bg-[var(--color-accent)] text-[var(--color-primary)]`:`bg-black/50 text-white opacity-0 group-hover:opacity-100`),children:(0,f.jsx)(r,{className:c(`w-3 h-3`,e.is_primary&&`fill-current`)})}),a&&e.is_primary&&(0,f.jsx)(`span`,{className:`absolute bottom-0 inset-x-0 bg-[var(--color-accent)] text-[var(--color-primary)] text-[9px] font-bold text-center py-0.5 tracking-wide`,children:`PRIMARY`})]},`${e.url}-${t}`)),M&&(0,f.jsx)(`button`,{type:`button`,onClick:D,disabled:N,className:`w-24 h-24 rounded-lg border-2 border-dashed border-[var(--color-border)] grid place-items-center text-[var(--color-text-muted)] hover:border-[var(--color-accent)] hover:text-[var(--color-accent-ink)] transition disabled:opacity-60`,children:N?(0,f.jsx)(o,{className:`w-5 h-5 animate-spin`}):(0,f.jsxs)(`span`,{className:`flex flex-col items-center gap-1`,children:[(0,f.jsx)(i,{className:`w-5 h-5`}),(0,f.jsx)(`span`,{className:`text-[10px] font-medium`,children:a?`Add`:`Upload`})]})}),N&&!M&&(0,f.jsx)(`div`,{className:`w-24 h-24 rounded-lg border border-[var(--color-border)] grid place-items-center bg-[var(--color-background)]`,children:(0,f.jsx)(o,{className:`w-5 h-5 animate-spin text-[var(--color-text-muted)]`})})]}),(0,f.jsx)(`p`,{className:`text-[12px] text-[var(--color-text-muted)]`,children:g??`${a?`Up to ${_} images · drag to reorder · ★ sets the primary · max ${p} MB each`:`One image · max ${p} MB`} · stored in the active storage backend`})]})}export{m as t};