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