"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react"),s=require("./App-1bae051a.js");require("react-dom");require("@feedloop/formula-editor");require("@feedloop/expression-parser");function K(){const G=s.useSDKContext(),p=s.GlobalContext.useSelectState(e=>e.filterList),f=s.GlobalContext.useSelectState(e=>e.sortList),{table:n,insight:r}=s.useDDL(),y=s.GlobalContext.useSelectState(e=>e.rowsContext),C=s.GlobalContext.useSelectState(e=>e.columnsContext),x=s.GlobalContext.useSelectState(e=>e.intervalDateInsight.start),S=s.GlobalContext.useSelectState(e=>e.intervalDateInsight.end),b=s.GlobalContext.useSelectState(e=>e.parameters),{setRowsContext:P,setColumnsContext:B,setRevalidateRow:j,setInsightConfig:k,addFilterList:q,addSortList:L,setParameters:M,setRevalidateRowIsValidating:N}=s.GlobalContext.useActions(),i=s.useParams(),w=a.useMemo(()=>s.lodashExports.reduce(b,(e,t)=>(e[t.options.name]=t.options.value,e),{}),[b]),O=a.useCallback(e=>{var u;const t=(e==null?void 0:e.registeredParams)||{};if(!t)return;const o=e!=null&&e.registeredParams?Object.keys(e==null?void 0:e.registeredParams):[],d=(u=e==null?void 0:e.params)==null?void 0:u.filter(l=>o.length?o.find(g=>g!==l):!0);return d==null||d.forEach(l=>{e&&(t[l]={default:"",type:""})}),t},[]),c=a.useMemo(()=>{const e=n==null?void 0:n.insights.find(o=>o.name===i.insightName);return{orderBy:Object.fromEntries((e==null?void 0:e.sorts.map(o=>{const[d]=Object.keys(o).filter(u=>typeof o[u]=="string");return[d,o[d]]}))||[]),condition:e==null?void 0:e.baseCondition,params:O(e)}},[n==null?void 0:n.insights,i.insightName]);a.useEffect(()=>{q(c.condition),L(c.orderBy),c.params&&M(Object.entries(c.params).map(([e,t])=>({id:s.nanoid(),options:{name:e,default:t==null?void 0:t.default,...(t==null?void 0:t.enum)&&{enum:t==null?void 0:t.enum},value:(t==null?void 0:t.default)||""},type:t==null?void 0:t.type})))},[c.condition,c.params,c.orderBy,i.insightName]);const{data:m,mutate:D,isValidating:R}=s.useSWR([G,r,x,S,p,f,w,"insight-row"],async e=>{try{return(await e.execute({operations:[{operation:"Insight",instruction:{name:`${r==null?void 0:r.name}`,insight:r==null?void 0:r.name,start:x,end:S,condition:p,params:w,orderBy:s.lodashExports.omit(f,["id"])}}]})).data}catch{return{results:null}}},{suspense:!0});a.useEffect(()=>{N(R)},[R]);const I=a.useMemo(()=>{const e=(r==null?void 0:r.columns.map((t,o)=>({type:t.type,accessorKey:t.name,description:t.description,isDisabled:!0,idx:o+1})))||[];return[{type:"rowId",accessorKey:"rowId",size:90,isDisabled:!0},...e]},[n,i.insightName]);a.useEffect(()=>{B(I.map(e=>({...e,isShown:!0}))),r!=null&&r.insight&&k(r.insight)},[I]);const E=a.useMemo(()=>C.filter(e=>e.isShown===!0&&e.accessorKey),[C]);return a.useEffect(()=>{var e,t;P(((t=(e=m==null?void 0:m.results)==null?void 0:e[(r==null?void 0:r.name)||""])==null?void 0:t.map(o=>({...o,isCreated:!0})))||[]),j(D)},[m,D]),!y.length&&!E.length?s.jsx(s.DatabaseSkeleton,{}):s.jsx(s.DatabaseTable,{columns:s.indexingArray(E),data:y,tableName:n==null?void 0:n.name,variant:"showFooter",defaultColumn:s.defaultColumn})}exports.default=K;
