import{r as f,a as C,j as n}from"./app-BBZbvTSO.js";import A from"./FieldBase-C0gzKbbb.js";import{B as w}from"./button-CZmOQP7j.js";import N from"./RelationModal-CHnhLkJe.js";import{R as B}from"./relation-entries-table-032v1tqy.js";import{P as M}from"./plus-D8fMMvfS.js";import{T as O}from"./trash-O7-xigMm.js";/* empty css            */import"./label-BPDCCRHl.js";import"./index-13DH8-b5.js";import"./utils-YhN_nlHn.js";import"./input-error-Bz7f2c6i.js";import"./moment-C5S46NFB.js";import"./dialog-D_v2qgP5.js";import"./index-C0VDnRYN.js";import"./index-CbDI-tXU.js";import"./Combination-9YgcTppv.js";import"./index-C1VwruLz.js";import"./data-table-Bvm7bdmo.js";import"./input-dmj4QjAp.js";import"./table-BDtwArHp.js";import"./pagination-tuDq0xKj.js";import"./chevron-left-CbuOobkA.js";import"./chevron-right-fDICdnKB.js";import"./select-KQeGfQXO.js";import"./scroll-area-hNGxDuJk.js";import"./index-BosTFUDi.js";import"./index-IvSACaye.js";import"./index-hDbW6ayQ.js";import"./floating-ui.dom-DtF1XweH.js";import"./index-AiAmtg3H.js";import"./index-BCYyBCUm.js";import"./tooltip-CRkL5R4V.js";import"./chevron-down-DDM9YQ48.js";import"./check-yuW7wFtQ.js";import"./search-bar-FrEZMnbC.js";import"./checkbox-B0_9jnY6.js";import"./date-picker-D96g6NFs.js";import"./calendar-DoGBS4zB.js";import"./clock-gA1FmRyJ.js";import"./format-C_TaVLmE.js";import"./switch-9bH0NbTy.js";import"./loader-circle-BqAFTH1R.js";import"./badge-BR_GnIty.js";import"./file-text-DfpLAAwR.js";function wt({field:o,value:s,onChange:j,processing:y,errors:F}){var E,R,S,$;const[u,x]=f.useState(!1),[p,a]=f.useState([]),[_,b]=f.useState([]),I=(t,i)=>{var m,d;if(((d=(m=o.options)==null?void 0:m.relation)==null?void 0:d.type)===1){const e=typeof t=="object"?t.id:t;j(o,e),typeof t=="object"&&a([t])}else{let e=[];if(Array.isArray(t)){e=t.map(l=>typeof l=="object"?l.id:l);const r=t.filter(l=>typeof l=="object");a(r)}else e=[typeof t=="object"?t.id:t],typeof t=="object"&&a([t]);j(o,e)}const c=i.filter(e=>{var r;return!((r=e.options)!=null&&r.hideInContentList)&&e.type!=="password"&&e.type!=="json"});b(c)};return f.useEffect(()=>{var m,d;if(!s||p.length>0)return;let t=[];if(Array.isArray(s)?t=s:typeof s=="number"&&(t=[s]),t.length===0)return;const i=(d=(m=o.options)==null?void 0:m.relation)==null?void 0:d.collection;if(!i)return;let c;if(typeof route=="function")try{c=route("projects.collections.content.find",{project:o.project_id,collection:i})}catch{c=`/projects/${o.project_id}/collections/${i}/content/find`}else c=`/projects/${o.project_id}/collections/${i}/content/find`;C.get(c,{params:{ids:t.join(",")}}).then(e=>{a(e.data);let r;if(typeof route=="function")try{r=route("projects.collections.content.getRelationCollection",{project:o.project_id,collection:i})}catch{r=`/projects/${o.project_id}/collections/${i}/content/relation-collection`}else r=`/projects/${o.project_id}/collections/${i}/content/relation-collection`;return C.get(r)}).then(e=>{const l=(e.data.fields||[]).filter(h=>{var g;return!((g=h.options)!=null&&g.hideInContentList)&&h.type!=="password"&&h.type!=="json"});b(l)}).catch(()=>{})},[s]),n.jsxs(A,{field:o,value:s,onChange:j,processing:y,errors:F,children:[n.jsxs("div",{className:"flex flex-col gap-2",children:[n.jsxs("div",{className:"flex gap-2",children:[n.jsxs(w,{type:"button",variant:"outline",disabled:y,onClick:()=>x(!0),children:[n.jsx(M,{className:"w-4 h-4"}),p.length>0?((R=(E=o.options)==null?void 0:E.relation)==null?void 0:R.type)===1?"Change Relation Entry":"Change Relation Entries":(($=(S=o.options)==null?void 0:S.relation)==null?void 0:$.type)===1?"Select Relation Entry":"Select Relation Entries"]}),p.length>0&&n.jsxs(w,{type:"button",variant:"outline",disabled:y,onClick:()=>{a([]),j(o,[])},children:[n.jsx(O,{className:"w-4 h-4"}),"Clear Selected"]})]}),p.length>0&&n.jsx(B,{fields:_,entries:p,showStatus:!1,showCreated:!1})]}),u&&n.jsx(N,{isOpen:u,onClose:()=>x(!1),field:o,value:p,onSelect:I})]})}export{wt as default};
