import{j as N}from"./tanstack-1.0.74-aU0azVkW.js";import{r as a}from"./redux-1.0.74-D8bIc6ki.js";import{l as t,b as V,ce as U,d5 as X}from"./lib-1.0.74-B5IEqfHg.js";import{l as O}from"./ActionsDml-1.0.74-C175Bq0Y.js";import{u as Y,a as Z}from"./hooks-1.0.74-NYCTWeI9.js";import{u as $}from"./useMetadata-1.0.74--jxfVI7W.js";import{i as D}from"./index-1.0.74-CwV9FnQf.js";import{F as M,T as R}from"./TextField-1.0.74-BW9Byijb.js";import{A as I}from"./Autocomplete-1.0.74-1_REDbpQ.js";import"./vendor-1.0.74-CN03Eozo.js";import"./utils-1.0.74-7xWhx9mc.js";import"./RestApi-1.0.74-5XKxNzpR.js";import"./CircularProgress-1.0.74-QfwfRJ7i.js";import"./ButtonBase-1.0.74-W6tAH7AI.js";import"./Typography-1.0.74-B4mhsevh.js";import"./useEnhancedEffect-1.0.74-B56M26dS.js";import"./Select-1.0.74-DHOnlEEH.js";import"./Menu-1.0.74-CdjCBJ4R.js";import"./ThemeProvider-1.0.74-4uzbYYLM.js";import"./index-1.0.74-DxwH6OqZ.js";import"./Portal-1.0.74-BrUPeMX9.js";import"./iconBase-1.0.74-Cs0vntJu.js";import"./isHostComponent-1.0.74-DVu5iVWx.js";import"./Modal-1.0.74-BfLid1bk.js";import"./createSvgIcon-1.0.74-Cw_ZFbxi.js";import"./Close-1.0.74-Ce0z5IFm.js";import"./Chip-1.0.74-BR6j83mk.js";const w=(u,e)=>{const m=Y(c=>c.lookups[u]!==void 0?c.lookups[u][e]:void 0);return t.debug("lookup",m),m},Eo=({value:u,columnState:e,columnMetaData:m,setValue:c,saveChanges:T,appId:p,currentRow:f,storeRow:h,row:P})=>{var L,j;t.debug(u,e,m,p,f,h,P);const F=Z(),v=$(p),[q,W]=a.useState(!1),[x,_]=a.useState(u);a.useEffect(()=>{t.debug("useEffect",e.columnName,x,u,f[e.columnName]),h!==void 0&&h[e.columnName]!==void 0&&x!==u&&(T(),_(u))},[u]);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:f[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 r=o.value.split(",");s[l.data[i].key]=r.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:f[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(r=>{i[r.columnName]=r.columnValue}),H(l),l.filter(r=>r.columnValue===""||r.columnValue===null).length===0?A(i):b([])}}},[f]);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,r,y;if(t.debug(s.data),!((i=e.lookup)!=null&&i.value.includes(",")))b(s.data);else{const J=(r=e.lookup)==null?void 0:r.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===u)[0]??null,[g,u]);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)=>{c(n!==null?n.key:null),o.stopPropagation()},renderInput:o=>N.jsx(R,{label:m.formLabel,required:m.is_nullable==="NO",...o,sx:X(e)})})})})};export{Eo as default};
