import{j as N}from"./cm-1.0.63-CSIRHMXC.js";import{r as a}from"./redux-1.0.63-Df-eBzJn.js";import{l as t,a as V,ci as U}from"./lib-1.0.63-DJZPtMem.js";import{l as O}from"./ActionsDml-1.0.63-nhbCczlS.js";import{u as X,a as Y}from"./hooks-1.0.63-CWs3kTdT.js";import{u as Z}from"./useMetadata-1.0.63-SO3BYgAj.js";import{f as $}from"./tab-1.0.63-Dmjvx_Wl.js";import{i as D}from"./index-1.0.63-CrdGWoQq.js";import{F as M,T as R}from"./TextField-1.0.63-C35Mfi1Q.js";import{A as I}from"./Autocomplete-1.0.63-pp6oOzlN.js";import"./vendor-1.0.63-CN03Eozo.js";import"./loglevel-1.0.63-BZ7XahX3.js";import"./lodash-1.0.63-hMxolcpJ.js";import"./moment-1.0.63-C5S46NFB.js";import"./RestApi-1.0.63-BUfzocgq.js";import"./Typography-1.0.63-mecuj_Wl.js";import"./ButtonBase-1.0.63-CF10PFMS.js";import"./useEnhancedEffect-1.0.63-DnR0ZFqV.js";import"./Select-1.0.63-BMejr8a7.js";import"./Menu-1.0.63-DLh4HkF0.js";import"./ThemeProvider-1.0.63-Cq0KMgkh.js";import"./index-1.0.63-oIDff4nA.js";import"./Modal-1.0.63-fMXTbfks.js";import"./notistack-1.0.63-B49ULGrj.js";import"./iconBase-1.0.63-CFvHhC4t.js";import"./createSvgIcon-1.0.63-DWL3J-DK.js";import"./Close-1.0.63-Dvbopfj5.js";import"./Chip-1.0.63-QLWMq6MQ.js";const w=(r,e)=>{const m=X(f=>f.lookups[r]!==void 0?f.lookups[r][e]:void 0);return t.debug("lookup",m),m},Vo=({value:r,columnState:e,columnMetaData:m,setValue:f,saveChanges:T,appId:p,currentRow:c,storeRow:h,row:P})=>{var L,j;t.debug(r,e,m,p,c,h,P);const F=Y(),v=Z(p),[q,W]=a.useState(!1),[x,_]=a.useState(r);a.useEffect(()=>{t.debug("useEffect",e.columnName,x,r,c[e.columnName]),h!==void 0&&h[e.columnName]!==void 0&&x!==r&&(T(),_(r))},[r]);const k=w(p,e.columnName),[g,b]=a.useState([]);t.debug("lov",g);const[d,H]=a.useState(Array.isArray((L=e.lookup)==null?void 0:L.dynamicConditions)?(j=e.lookup)==null?void 0:j.dynamicConditions.map(o=>({columnName:o.lookupColumnName,columnValue:c[o.tableColumnName]??""})):[]);t.debug("conditions",d),a.useEffect(()=>{if(k!==void 0)if(Object.keys(k).length>0){const o=[];for(const n in k)o.push({key:n,value:k[n]});b(o)}else d.length===0&&z();else b([])},[k]);const z=()=>{if(!q&&g.length===0){W(!0);const o=e.lookup;if(o!==void 0){const n=v.isRelationSelectionTable===!0?V.relationTablePrefix+e.columnName:e.columnName;O(p,"table",n,o.key,o.value,{},{},l=>{t.debug(l.data);const s={};for(let i=0;i<l.data.length;i++)if(!o.value.includes(","))s[l.data[i].key]=l.data[i].value;else{const u=o.value.split(",");s[l.data[i].key]=u.map(y=>l.data[i][y])}t.debug(s),F(U({appId:p,columnName:e.columnName,lookup:s}))},l=>{console.error(l)})}}else console.error("Error loading lookup - please check server response")};a.useEffect(()=>{var o,n;if(Object.keys(d).length>0&&d.length>0){const l=Array.isArray((o=e.lookup)==null?void 0:o.dynamicConditions)?(n=e.lookup)==null?void 0:n.dynamicConditions.map(i=>({columnName:i.lookupColumnName,columnValue:c[i.tableColumnName]??""})):[];t.debug("reload lookup?",d,l);const s={};if(l.length>0&&l.map(i=>{s[i.columnName]=i.columnValue}),D(d,l))t.debug("reloading dynamic lookup",e.columnName,l,s),A(s);else{const i={};l.length>0&&l.map(u=>{i[u.columnName]=u.columnValue}),H(l),l.filter(u=>u.columnValue===""||u.columnValue===null).length===0?A(i):b([])}}},[c]);const A=(o={})=>{const n=e.lookup;if(n!==void 0){const l=v.isRelationSelectionTable===!0?V.relationTablePrefix+e.columnName:e.columnName;O(p,"table",l,n.key,n.value,o,{},s=>{var i,u,y;if(t.debug(s.data),!((i=e.lookup)!=null&&i.value.includes(",")))b(s.data);else{const J=(u=e.lookup)==null?void 0:u.value.split(","),E=[];for(let C=0;C<s.data.length;C++){const K=J.map(Q=>s.data[C][Q]).join(((y=e.lookup)==null?void 0:y.delimiter)??"");E.push({key:s.data[C].key,value:K})}b(E)}},s=>{console.error(s)})}},B=o=>{var n;return Array.isArray(o.value)?o.value.join(((n=e.lookup)==null?void 0:n.delimiter)??""):o.value},G=a.useMemo(()=>g.filter(o=>o.key===r)[0]??null,[g,r]);return N.jsx("div",{onClick:o=>{o.stopPropagation()},children:N.jsx(M,{fullWidth:!0,onClick:o=>{o.stopPropagation()},children:N.jsx(I,{className:"pp-inline-editing",multiple:!1,fullWidth:!0,autoHighlight:!0,disableClearable:m.is_nullable==="NO",options:g,getOptionLabel:o=>B(o),value:G,onChange:(o,n)=>{f(n!==null?n.key:null),o.stopPropagation()},renderInput:o=>N.jsx(R,{label:m.formLabel,required:m.is_nullable==="NO",...o,sx:$(e)})})})})};export{Vo as default};
