"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("react"),o=require("./App-ea99c22a.js");require("@emotion/react");require("react-dom");require("@emotion/styled");require("@feedloop/formula-editor");require("@feedloop/expression-parser");require("base-64");require("react-mentions");const B=new Set(["id","created_at","updated_at","auth_provider"]),A=u.lazy(()=>Promise.resolve().then(()=>require("./App-ea99c22a.js")).then(d=>d.DatabaseTable$1)),j=d=>{var i;if(!d.query)return;const m=d.query.registeredParams||{},y=d.query.registeredParams?Object.keys(d.query.registeredParams):[],p=(i=d.query.paramsRequired)==null?void 0:i.filter(r=>y.length?y.find(n=>n!==r):!0);return p==null||p.forEach(r=>{d.query&&(m[r]={default:"",type:""})}),m};function w(){const d=o.GlobalContext.useSelectState(e=>e.sortList),m=o.GlobalContext.useSelectState(e=>e.filterList),y=o.useSDKContext(),p=o.useConfig(),i=o.useParams(),{table:r,grid:n}=o.useDDL(),b=o.GlobalContext.useSelectState(e=>e.rowsContext),D=o.GlobalContext.useSelectState(e=>e.columnsContext),{limit:S,offset:x,setIsScrollTable:E}=o.useTableProps(),K=o.GlobalContext.useSelectState(e=>e.rowsDraft),P=o.GlobalContext.useSelectState(e=>e.parameters),q=o.GlobalContext.useSelectState(e=>e.viewFields),{setColumnsContext:G,setRowsContext:F,setRevalidateRow:T,setRevalidateRowIsValidating:I,setParameters:L,addFilterList:M,addSortList:O,setViewFields:V}=o.GlobalContext.useActions(),h=u.useMemo(()=>o.lodashExports.reduce(P,(e,a)=>(e[a.options.name]=a.options.value,e),{}),[P]),t=u.useMemo(()=>{var e,a,s,c;if(n)return{orderBy:(e=n.query)==null?void 0:e.orderBy,condition:(a=n.query)==null?void 0:a.condition,fields:(s=n.query)==null?void 0:s.fields,populate:(c=n.query)==null?void 0:c.populate,params:j(n)}},[r==null?void 0:r.views,i.viewName,n]),f=u.useMemo(()=>{const e=(r==null?void 0:r.columns.map((a,s)=>{var c;return{type:a.references?"relation":a.type,accessorKey:a.name,references:a.references,definition:a.definition,description:a.description,isDisabled:!!B.has(a.name),size:((c=a==null?void 0:a.metadata)==null?void 0:c.width)||200,idx:s+1}}))||[];return[{type:"rowId",accessorKey:"rowId",size:90,isDisabled:!1,idx:0},...e]},[r]),{data:l,mutate:N,isValidating:R}=o.useSWR([y,d,m,S,x,h,r==null?void 0:r.name,n==null?void 0:n.name,i.tableName,i.viewName,"data-grid"],async e=>{if((r==null?void 0:r.name)!==i.tableName||(n==null?void 0:n.name)!==i.viewName)return{results:null};try{const a=r!=null&&r.name?{name:"data",orderBy:d,condition:m,limit:S,offset:x,params:h,table:r==null?void 0:r.name,view:n==null?void 0:n.name}:{name:"data",orderBy:d,condition:m,limit:S,offset:x,params:h,table:r==null?void 0:r.name};return(await e.execute({operations:[{operation:"Select",instruction:a}]})).data}catch{return{results:null}}},{suspense:!0});u.useEffect(()=>{I(R)},[R]),u.useEffect(()=>{let e=[];return i.viewName&&(M(t==null?void 0:t.condition),O(t==null?void 0:t.orderBy),e=(t==null?void 0:t.fields)||[],t!=null&&t.params&&L(Object.entries(t==null?void 0:t.params).map(([a,s])=>({id:o.nanoid(),options:{name:a,default:s==null?void 0:s.default,...(s==null?void 0:s.enum)&&{enum:s.enum},...(s==null?void 0:s.inferredWith)&&{inferredWith:s.inferredWith},value:(s==null?void 0:s.default)||""},type:s.type==="float"||s.type==="integer"?"number":s==null?void 0:s.type})))),V(e),()=>{E(!1)}},[t==null?void 0:t.condition,t==null?void 0:t.params,t==null?void 0:t.fields,t==null?void 0:t.orderBy,t==null?void 0:t.populate,i.tableName,i.viewName]),u.useEffect(()=>{const e=l!=null&&l.results?l==null?void 0:l.results.data.map(a=>({...a,isCreated:!0})):[];e.length>S-1&&e.pop(),F(e),T(N)},[l,N]),u.useEffect(()=>{if(t!=null&&t.fields){const e=q.filter(s=>f.find(c=>c.accessorKey===s)).map(s=>f.find(c=>c.accessorKey===s)),a=o.lodashExports.differenceBy(f,e,"accessorKey");a.splice(1,0,e),G(a.flat().map(s=>({...s,isShown:!!q.find(c=>c===s.accessorKey||s.accessorKey==="rowId")})))}else G(f.map(e=>({...e,isShown:!["id","updated_at","created_at"].includes(e.accessorKey)})))},[f,q,t==null?void 0:t.fields]);const W=u.useMemo(()=>[b,K].flat(),[b,K]),_=u.useMemo(()=>D.filter(e=>{var a;return((a=p.options)==null?void 0:a.mode)==="table"?["datetime","text","rowId"].includes(e.type):!0}).filter(e=>e.isShown===!0&&e.accessorKey),[D,p]);return o.jsx(A,{columns:o.indexingArray(_),data:W,tableName:r==null?void 0:r.name,variant:"showFooterWithOperationTable",defaultColumn:o.defaultColumn})}exports.default=w;exports.disableUpdateField=B;
