import{r,j as e}from"./router.chunk.YsRm4bSN.js";import{s as sprintf,_ as __}from"./wordpress.chunk.BVds2GcU.js";import{u as o}from"./forms.chunk.Bz8aaTWe.js";import{S as n,U as a,V as d,X as s,c as l,i as u,Y as i,B as f,k as t,F as g,a as c,p as w,b as m,M as p,d as b,e as h}from"./main.chunk.N-eZ2ybB.js";import{u as x,a as j}from"./media.chunk.BUcAGRSV.js";import{a2 as v,aR as y,aA as k}from"./icons.chunk.BwnSC3zE.js";function N({className:o,value:g,onChange:c,onError:w,maxSize:m=s(10),accept:p,disabled:b=!1,uploadButtonLabel:h,dropzoneLabel:N}){const{openMediaModal:z}=n(),{mutateAsync:C,isPending:S}=x(),F=r.useMemo(()=>p?p.reduce((r,e)=>(r[e]=d(e),r),{}):a,[p]),M=r.useCallback(async(r,e)=>{if(e.length>0){const r=e.map(r=>"file-too-large"===r.errors[0]?.code?sprintf(
/* translators: 1: file name, 2: max size in MB */
__("File %1$s is too large. Max size is %2$sMB.","growfund"),r.file.name,m/1048576):"file-invalid-type"===r.errors[0]?.code?sprintf(__("File %s has an invalid file type.","growfund"),r.file.name):sprintf(__("File %s could not be uploaded.","growfund"),r.file.name));return void(w&&w(r[0]))}w&&w(null);try{const e=await C(r);e.data.media.length>0?c(e.data.media[0]):w?.(__("Failed to upload media","growfund"))}catch(o){console.error("Error uploading media:",o),w?.(__("Failed to upload media","growfund"))}},[w,C,m,c]),E=r.useCallback((r,e)=>{M(r,e)},[M]),{getRootProps:L,isDragActive:U,isDragReject:B}=j({onDrop:E,maxSize:m,accept:F,disabled:b,multiple:!1});return e.jsx("div",{className:l("growfund-border growfund-border-border growfund-bg-background-surface-secondary growfund-rounded-md",U&&"growfund-border-primary growfund-border-dashed growfund-bg-muted/50",b&&"growfund-opacity-50 growfund-cursor-not-allowed",B&&"growfund-border-border-critical growfund-bg-background-fill-critical-secondary/60",o),children:u(g)?e.jsx("div",{className:l("growfund-flex growfund-gap-2 growfund-flex-wrap"),children:(A=g,e.jsxs("div",{className:"growfund-group growfund-relative growfund-flex growfund-justify-center growfund-items-center growfund-p-5 growfund-w-full growfund-min-h-64",children:[e.jsx("div",{className:"group-hover:growfund-visible growfund-z-positive growfund-invisible growfund-absolute growfund-left-0 growfund-top-0 growfund-size-full growfund-rounded-md growfund-bg-background-inverse/60",children:e.jsxs("div",{className:"growfund-flex growfund-gap-2 growfund-h-full growfund-items-end growfund-justify-center growfund-pb-6",children:[e.jsx(f,{variant:"ghost",size:"icon",className:"growfund-bg-background-fill",onClick:()=>{(()=>{try{z({title:__("Upload media","growfund"),button_text:__("Upload","growfund"),types:["image"],onSelect:r=>{u(r)&&0!==r.length&&c(r[0])}})}catch(r){console.error("Error replacing file:",r),w?.(__("Failed to replace file","growfund"))}})()},children:e.jsx(y,{})}),e.jsx(f,{variant:"ghost",size:"icon",className:"growfund-bg-background-fill",onClick:()=>{c(null)},children:e.jsx(k,{})})]})}),e.jsx(t,{src:A.url,alt:sprintf(__("Preview %s","growfund"),A.filename),className:"growfund-rounded-md growfund-border-transparent growfund-shadow-none growfund-bg-transparent growfund-max-h-64 growfund-h-full",fit:"contain",aspectRatio:"square"})]}))}):e.jsx("div",{...L(),className:"growfund-flex growfund-flex-col growfund-items-center growfund-justify-center growfund-py-10 growfund-gap-2 growfund-text-center growfund-rounded-md growfund-min-h-[10rem]",children:S?e.jsx(i,{}):e.jsxs(e.Fragment,{children:[e.jsxs(f,{variant:"outline",className:"growfund-text-fg-primary growfund-typo-sm growfund-font-medium growfund-border-none",disabled:b,onClick:()=>{z({title:__("Upload media","growfund"),button_text:__("Upload","growfund"),types:["image"],onSelect:r=>{c(r[0]??null)}})},children:[e.jsx(v,{className:"growfund-h-4 growfund-w-4"}),h]}),e.jsx("p",{className:"growfund-typo-sm growfund-text-fg-secondary",children:N})]})})});var A}function z({control:r,name:n,label:a,description:d,disabled:i=!1,className:f,uploadButtonLabel:t=__("Upload media","growfund"),dropzoneLabel:x=__("Drag and drop, or upload images","growfund"),accept:j=[p.IMAGES],maxSize:v=s(10)}){const y=o();return e.jsx(g,{control:r,name:n,render:({field:r,fieldState:o})=>e.jsxs(c,{className:"growfund-w-full growfund-space-y-2",children:[u(a)&&e.jsx("div",{className:"growfund-h-5",children:e.jsx(w,{className:"growfund-flex-shrink-0",children:a})}),e.jsx(m,{children:e.jsx(N,{value:r.value??null,onChange:e=>{r.onChange(e)},disabled:i,className:l(o.error&&"growfund-border-border-critical",f),uploadButtonLabel:t,dropzoneLabel:x,accept:j,onError:r=>{u(r)?y.setError(n,{type:"manual",message:r}):y.clearErrors(n)},maxSize:v})}),u(d)&&e.jsx(b,{children:d}),e.jsx(h,{})]})})}export{z as M};
