"use client";import{a as M,b as A,c as T,f as O}from"./chunk-H3D3IU3G.js";import{a as y}from"./chunk-INBEEENE.js";import{a as U}from"./chunk-F26IQ5RE.js";import{b}from"./chunk-CUIBIFW2.js";import{jsx as e,jsxs as c}from"react/jsx-runtime";import{useLexicalComposerContext as ae}from"@lexical/react/LexicalComposerContext.js";import{useLexicalNodeSelection as le}from"@lexical/react/useLexicalNodeSelection.js";import{mergeRegister as re}from"@lexical/utils";import{getTranslation as F}from"@payloadcms/translations";import{Button as C,File as se,formatDrawerSlug as ie,useConfig as ne,useEditDepth as ce,usePayloadAPI as de,useTranslation as ue}from"@payloadcms/ui";import{$getNodeByKey as I,$getSelection as me,$isNodeSelection as pe,CLICK_COMMAND as fe,COMMAND_PRIORITY_LOW as w,KEY_BACKSPACE_COMMAND as ge,KEY_DELETE_COMMAND as he}from"lexical";import{useCallback as f,useEffect as _e,useId as De,useReducer as be,useRef as Ce,useState as we}from"react";var o="lexical-upload",K={depth:0},Ne=d=>{let{data:{fields:j,relationTo:N,value:n},nodeKey:l}=d;if(typeof n=="object")throw new Error("Upload value should be a string or number. The Lexical Upload component should not receive the populated value object.");let{config:{collections:B,routes:{api:k},serverURL:W}}=ne(),g=Ce(null),{uuid:Y}=b(),z=ce(),[a]=ae(),[r,h,x]=le(l),{editorConfig:H,fieldProps:{readOnly:_,schemaPath:q}}=b(),{i18n:S,t:u}=ue(),[$,G]=be(t=>t+1,0),[s]=we(()=>B.find(t=>t.slug===N)),J=De(),v=ie({slug:"lexical-upload-drawer-"+Y+J,depth:z}),{toggleDrawer:Q}=y(v,!0),{closeDocumentDrawer:E,DocumentDrawer:V,DocumentDrawerToggler:X}=U({id:n,collectionSlug:s.slug}),[{data:m},{setParams:R}]=de(`${W}${k}/${s.slug}/${n}`,{initialParams:K}),L=m?.thumbnailURL||m?.url,Z=f(()=>{a.update(()=>{I(l)?.remove()})},[a,l]),ee=f(t=>{R({...K,cacheBust:$}),G(),E()},[R,$,E]),D=f(t=>{let p=me();return r&&pe(p)&&(t.preventDefault(),a.update(()=>{p.getNodes().forEach(i=>{O(i)&&i.remove()})})),!1},[a,r]);_e(()=>re(a.registerCommand(fe,t=>t.target===g.current||g.current?.contains(t.target)?(t.shiftKey?h(!r):r||(x(),h(!0)),!0):!1,w),a.registerCommand(he,D,w),a.registerCommand(ge,D,w)),[x,a,r,l,D,h]);let P=H?.resolvedFeatureMap?.get("upload")?.sanitizedClientFeatureProps.collections?.[s.slug]?.hasExtraFields,te=f((t,p)=>{a.update(()=>{let i=I(l);if(i){let oe={...i.getData(),fields:p};i.setData(oe)}})},[a,l]);return c("div",{className:[o,r&&`${o}--selected`].filter(Boolean).join(" "),contentEditable:!1,ref:g,children:[c("div",{className:`${o}__card`,children:[c("div",{className:`${o}__topRow`,children:[e("div",{className:`${o}__thumbnail`,children:L?e("img",{alt:m?.filename,"data-lexical-upload-id":n,"data-lexical-upload-relation-to":N,src:L}):e(se,{})}),c("div",{className:`${o}__topRowRightPanel`,children:[e("div",{className:`${o}__collectionLabel`,children:F(s.labels.singular,S)}),a.isEditable()&&c("div",{className:`${o}__actions`,children:[P?e(C,{buttonStyle:"icon-label",className:`${o}__upload-drawer-toggler`,disabled:_,el:"button",icon:"edit",onClick:()=>{Q()},round:!0,tooltip:u("fields:editRelationship")}):null,e(C,{buttonStyle:"icon-label",className:`${o}__swap-drawer-toggler`,disabled:_,el:"button",icon:"swap",onClick:()=>{a.dispatchCommand(T,{replace:{nodeKey:l}})},round:!0,tooltip:u("fields:swapUpload")}),e(C,{buttonStyle:"icon-label",className:`${o}__removeButton`,disabled:_,icon:"x",onClick:t=>{t.preventDefault(),Z()},round:!0,tooltip:u("fields:removeUpload")})]})]})]}),e("div",{className:`${o}__bottomRow`,children:e(X,{className:`${o}__doc-drawer-toggler`,children:e("strong",{children:m?.filename})})})]}),n?e(V,{onSave:ee}):null,P?e(M,{data:j,drawerSlug:v,drawerTitle:u("general:editLabel",{label:F(s.labels.singular,S)}),featureKey:"upload",handleDrawerSubmit:te,schemaPath:q,schemaPathSuffix:s.slug}):null]})},Ke=d=>e(A,{...d,uploads:!0,children:e(Ne,{...d})});export{Ke as UploadComponent};
//# sourceMappingURL=component-NSAU5Y76.js.map
