globalThis.process??={},globalThis.process.env??={};import{i as e,t}from"./react.DbQyqGaC.js";import{t as n}from"./jsx-runtime.Cwe9agoV.js";var r=e(t(),1),i=n(),a=class extends Error{reason;status;name=`UploadRejectedError`;isUploadRejectedError=!0;constructor(e,t){super(`upload rejected (${t}): ${e}`),this.reason=e,this.status=t}},o=class extends Error{name=`UploadInFlightError`;isUploadInFlightError=!0;constructor(){super(`an upload is already in progress`)}};function s(e){let[t,n]=(0,r.useState)(!1),[i,a]=(0,r.useState)(0),[s,c]=(0,r.useState)(null),l=(0,r.useRef)(e);l.current=e;let u=(0,r.useRef)(!1);return{upload:(0,r.useCallback)(async(e,t)=>{if(u.current)throw new o;u.current=!0,n(!0),a(0),c(null);try{let n=await l.current.upload(e,{signal:t?.signal,onProgress:e=>a(e)});return a(1),n}catch(e){let t=e instanceof Error?e:Error(String(e));throw c(t),t}finally{u.current=!1,n(!1)}},[]),uploading:t,progress:i,error:s,reset:(0,r.useCallback)(()=>{c(null),a(0)},[])}}function c(e,t){let[n,i]=(0,r.useState)([]),[a,o]=(0,r.useState)(!0),[s,c]=(0,r.useState)(null),[l,u]=(0,r.useState)(void 0),[d,f]=(0,r.useState)(!1),p=(0,r.useRef)(e);p.current=e;let m=(0,r.useRef)(!1),h=t?.limit,g=(0,r.useCallback)(async(e,t)=>{if(!m.current){m.current=!0,o(!0),c(null);try{let n=await p.current.list({cursor:t,limit:h});i(t=>e?n.items:[...t,...n.items]),u(n.cursor),f(!!n.cursor)}catch(e){c(e instanceof Error?e:Error(String(e)))}finally{m.current=!1,o(!1)}}},[h]);return(0,r.useEffect)(()=>{g(!0,void 0)},[g]),{items:n,loading:a,error:s,hasMore:d,loadMore:(0,r.useCallback)(async()=>{l&&await g(!1,l)},[l,g]),refresh:(0,r.useCallback)(async()=>{await g(!0,void 0)},[g]),remove:(0,r.useCallback)(async e=>{try{await p.current.remove(e),i(t=>t.filter(t=>t.key!==e))}catch(e){c(e instanceof Error?e:Error(String(e)))}},[])}}function l({client:e,onSelect:t,className:n,allowDelete:a=!0}){let o=c(e),l=s(e),u=(0,r.useRef)(null),d=async e=>{let t=e.target.files?.[0];if(t)try{await l.upload(t),await o.refresh()}catch{}finally{u.current&&(u.current.value=``)}},f=async e=>{await o.remove(e),u.current?.focus()};return(0,i.jsxs)(`section`,{"aria-label":`Media library`,className:n,style:{containerType:`inline-size`},children:[(0,i.jsxs)(`div`,{children:[(0,i.jsxs)(`label`,{children:[(0,i.jsx)(`span`,{children:`Upload image`}),(0,i.jsx)(`input`,{ref:u,type:`file`,accept:`image/*`,onChange:d,disabled:l.uploading})]}),l.error?(0,i.jsx)(`p`,{role:`alert`,children:l.error.message}):null]}),o.error?(0,i.jsx)(`p`,{role:`alert`,children:o.error.message}):null,!o.loading&&!o.error&&o.items.length===0?(0,i.jsx)(`p`,{children:`No media yet. Upload an image to begin.`}):(0,i.jsx)(`ul`,{style:{display:`grid`,gridTemplateColumns:`repeat(auto-fill, minmax(min(12cqi, 100%), 1fr))`,gap:`0.75rem`,listStyle:`none`,margin:0,padding:0},children:o.items.map(e=>(0,i.jsxs)(`li`,{children:[(0,i.jsx)(`button`,{type:`button`,"aria-label":`Select ${e.key}`,onClick:()=>t(e),style:{display:`block`,width:`100%`,border:`none`,padding:0,background:`none`,cursor:`pointer`},children:(0,i.jsx)(`img`,{src:e.url,alt:``,width:e.width,height:e.height,style:{width:`100%`,height:`auto`,display:`block`}})}),a?(0,i.jsx)(`button`,{type:`button`,"aria-label":`Delete ${e.key}`,onClick:()=>void f(e.key),children:`Delete`}):null]},e.key))}),o.hasMore?(0,i.jsx)(`button`,{type:`button`,onClick:()=>void o.loadMore(),disabled:o.loading,children:`Load more`}):null]})}async function u(e){try{return(await e.json()).error?.code??`http_${e.status}`}catch{return`http_${e.status}`}}function d(){return{async upload(e,t){let n=new FormData;n.set(`file`,e);let r=await fetch(`/api/admin/media`,{method:`POST`,body:n,signal:t?.signal});if(!r.ok)throw new a(await u(r),r.status);return(await r.json()).item},async list(e){let t=new URLSearchParams;e?.cursor&&t.set(`cursor`,e.cursor),e?.limit&&t.set(`limit`,String(e.limit));let n=await fetch(`/api/admin/media?${t.toString()}`);if(!n.ok)throw Error(`list failed: ${n.status}`);return await n.json()},async remove(e){let t=e.split(`/`).map(encodeURIComponent).join(`/`),n=await fetch(`/api/admin/media/${t}`,{method:`DELETE`});if(!n.ok&&n.status!==404)throw Error(`delete failed: ${n.status}`)}}}export{l as n,d as t};