"use client";import{a as S,d as w}from"./chunk-PGMIO4IN.js";import{a as x}from"./chunk-F26IQ5RE.js";import{b as E}from"./chunk-CUIBIFW2.js";import{jsx as l,jsxs as h}from"react/jsx-runtime";import{useLexicalComposerContext as W}from"@lexical/react/LexicalComposerContext.js";import{useLexicalNodeSelection as Y}from"@lexical/react/useLexicalNodeSelection.js";import{mergeRegister as H}from"@lexical/utils";import{getTranslation as U}from"@payloadcms/translations";import{Button as A,useConfig as q,usePayloadAPI as z,useTranslation as F}from"@payloadcms/ui";import{$getNodeByKey as G,$getSelection as J,$isNodeSelection as Q,CLICK_COMMAND as V,COMMAND_PRIORITY_LOW as C,KEY_BACKSPACE_COMMAND as X,KEY_DELETE_COMMAND as Z}from"lexical";import ee,{useCallback as _,useEffect as te,useReducer as oe,useRef as se,useState as le}from"react";var o="lexical-relationship",T={depth:0},ne=u=>{let{data:{relationTo:M,value:r},nodeKey:n}=u;if(typeof r=="object")throw new Error("Relationship value should be a string or number. The Lexical Relationship component should not receive the populated value object.");let m=se(null),[e]=W(),[s,c,d]=Y(n),{fieldProps:{readOnly:g}}=E(),{config:{collections:y,routes:{api:L},serverURL:O}}=q(),[i,ie]=le(()=>y.find(t=>t.slug===M)),{i18n:B,t:p}=F(),[R,I]=oe(t=>t+1,0),[{data:b},{setParams:N}]=z(`${O}${L}/${i.slug}/${r}`,{initialParams:T}),{closeDocumentDrawer:v,DocumentDrawer:P,DocumentDrawerToggler:j}=x({id:r,collectionSlug:i.slug}),K=_(()=>{e.update(()=>{G(n)?.remove()})},[e,n]),k=ee.useCallback(()=>{N({...T,cacheBust:R}),v(),I()},[R,N,v]),f=_(t=>{let a=J();return s&&Q(a)&&(t.preventDefault(),e.update(()=>{a.getNodes().forEach($=>{w($)&&$.remove()})})),!1},[e,s]),D=_(t=>{let a=t;return a.target===m.current||m.current?.contains(a.target)?(a.shiftKey?c(!s):s||(d(),c(!0)),!0):!1},[s,c,d]);return te(()=>H(e.registerCommand(V,D,C),e.registerCommand(Z,f,C),e.registerCommand(X,f,C)),[d,e,s,n,f,c,D]),h("div",{className:[o,s&&`${o}--selected`].filter(Boolean).join(" "),contentEditable:!1,ref:m,children:[h("div",{className:`${o}__wrap`,children:[l("p",{className:`${o}__label`,children:p("fields:labelRelationship",{label:i.labels?.singular?U(i.labels?.singular,B):i.slug})}),l(j,{className:`${o}__doc-drawer-toggler`,children:l("p",{className:`${o}__title`,children:b?b[i?.admin?.useAsTitle||"id"]:r})})]}),e.isEditable()&&h("div",{className:`${o}__actions`,children:[l(A,{buttonStyle:"icon-label",className:`${o}__swapButton`,disabled:g,el:"button",icon:"swap",onClick:()=>{n&&e.dispatchCommand(S,{replace:{nodeKey:n}})},round:!0,tooltip:p("fields:swapRelationship")}),l(A,{buttonStyle:"icon-label",className:`${o}__removeButton`,disabled:g,icon:"x",onClick:t=>{t.preventDefault(),K()},round:!0,tooltip:p("fields:removeRelationship")})]}),!!r&&l(P,{onSave:k})]})},be=u=>l(ne,{...u});export{be as RelationshipComponent};
//# sourceMappingURL=RelationshipComponent-F77EQHRG.js.map
