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