react-semaphor 0.0.535 → 0.0.536

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -410,8 +410,8 @@
410
410
 
411
411
  ${d?`Python:
412
412
  ${d}`:""}
413
- `;b(z)}function F(){return e.jsxs(e.Fragment,{children:[r&&e.jsx("div",{className:"relative flex w-1/3 min-w-0 grow overflow-hidden text-xs",role:"editor-filter-container",children:e.jsx("div",{className:"mt-2 w-[99%]",children:e.jsx(Ka,{jsonString:JSON.stringify(m,null,2)})})}),m&&m.length>0&&e.jsx(s.Button,{variant:"ghost",onClick:()=>l(!r),className:"absolute bottom-0 right-6 size-7 p-1",children:r?e.jsx(s.X,{className:"size-3.5 text-muted-foreground"}):e.jsx(s.Filter,{className:"size-3.5 text-muted-foreground"})})]})}function P(){return e.jsxs(s.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(s.ResizablePanel,{id:"1",order:1,defaultSize:a?20:100,className:"relative flex grow",children:[e.jsx("div",{id:"sql-container",className:"relative flex w-[99%]",children:e.jsx(Wa,{})}),a&&e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"sql"})]}),a&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(s.ResizablePanel,{id:"2",order:2,defaultSize:80,className:"relative flex grow",children:[e.jsx("div",{id:"python-container",className:"relative flex w-[99%] pt-0",children:e.jsx(Ya,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function _(){return e.jsxs("div",{role:"editor-controls",className:"item-center flex flex-wrap justify-between gap-2 px-6 py-4",children:[e.jsxs("div",{className:"flex grow items-center gap-2",children:[e.jsxs(s.Button,{title:"Shift + Enter",disabled:D||w,onClick:()=>{v()},className:"h-8 shrink-0",size:"sm",children:[D||w?e.jsx(s.LoaderCircle,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(s.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),e.jsx(At,{sql:N==null?void 0:N.sql,error:y,onDebugWithAssistant:L}),e.jsx(s.EditorClearButton,{}),e.jsx(s.Toggle,{pressed:a,onPressedChange:z=>n(z),className:"",size:"sm",children:e.jsx(Ja,{className:"size-4 text-foreground/70"})}),e.jsx("button",{disabled:c,onClick:()=>{C(!0)},className:"h-9 w-full cursor-text px-3 text-left text-sm text-muted-foreground",children:c?"":"Type your question..."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.EditorCancelButton,{}),e.jsx(s.EditorAcceptButton,{})]})]})}return e.jsxs("section",{role:"editor-section",className:"flex grow flex-col justify-end",children:[e.jsxs("div",{className:"relative flex grow justify-between overflow-y-scroll",children:[P(),F()]}),_()]})}function Xa({data:t}){var c;const[a,n]=A.useState([]),l=Object.keys(t[0]).map(i=>({accessorKey:i,header:({column:d})=>{const m=d.getIsSorted();return e.jsx("div",{className:"flex gap-2",children:e.jsxs("div",{className:"flex cursor-pointer items-center",onClick:()=>d.toggleSorting(d.getIsSorted()==="asc"),children:[i,m==="asc"?e.jsx(s.TriangleUpIcon,{className:"ml-2 h-5 w-5"}):m==="desc"?e.jsx(s.TriangleDownIcon,{className:"ml-2 h-5 w-5"}):null]})})}})),o=s.useReactTable({data:t,columns:l,getCoreRowModel:s.getCoreRowModel(),getPaginationRowModel:s.getPaginationRowModel(),onSortingChange:n,getSortedRowModel:s.getSortedRowModel(),manualPagination:!0,state:{sorting:a}});return e.jsxs(s.CustomScrollArea,{className:"grow overflow-auto font-mono text-xs text-foreground/95",children:[e.jsxs("table",{className:"w-full",children:[e.jsx(s.TableHeader,{className:"sticky top-0 z-50 h-0 bg-muted",children:o.getHeaderGroups().map(i=>e.jsx(s.TableRow,{className:"",children:i.headers.map(d=>e.jsx(s.TableHead,{className:"h-11 font-semibold text-foreground",children:d.isPlaceholder?null:s.flexRender(d.column.columnDef.header,d.getContext())},d.id))},i.id))}),e.jsx(s.TableBody,{children:(c=o.getRowModel().rows)!=null&&c.length?o.getRowModel().rows.map(i=>e.jsx(s.TableRow,{className:"h-11 whitespace-nowrap py-2","data-state":i.getIsSelected()&&"selected",children:i.getVisibleCells().map(d=>e.jsx(s.TableCell,{className:"py-2",children:s.flexRender(d.column.columnDef.cell,d.getContext())},d.id))},i.id)):e.jsx(s.TableRow,{children:e.jsx(s.TableCell,{colSpan:l.length,className:"h-24 text-center",children:"No results."})})})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function Za({card:t}){var b,C,O,S,E,N,D,w,M,k,R;const[a,n]=A.useState(!1),[r,l]=A.useState([]),{authToken:o}=s.useSemaphorContext(),c=s.getTokenParams(o==null?void 0:o.accessToken),i=s.useDashboardStore(I=>I.themeStyle),d={colors:((C=(b=i==null?void 0:i.chart)==null?void 0:b.dataset)==null?void 0:C.backgroundColor)||[]};(O=t.customCardPreferences)!=null&&O.inputData;const{getCard:m}=s.useCustomVisual(((S=t.customCardPreferences)==null?void 0:S.url)||""),h=((E=t.customCardPreferences)==null?void 0:E.visualType)||"single",u=s.useEditorStore(I=>I.frame),{setCustomCardPreferences:p}=s.useEditorActions(),{data:f}=s.useDashboardCardQuery(t),j=A.useCallback((I,v)=>{l(y=>({...y,[I]:v}))},[]);if(!((N=t.customCardPreferences)!=null&&N.componentName))return null;const x=m((D=t.customCardPreferences)==null?void 0:D.componentName);function T(I){n(I)}const g=Object.keys(((w=t.customCardPreferences)==null?void 0:w.dataInputCardIds)||{}).map(I=>{var L,F,P;const v=(F=(L=t.customCardPreferences)==null?void 0:L.dataInputCardIds)==null?void 0:F[I].cardId,y=(P=u==null?void 0:u.cards)==null?void 0:P.find(_=>_.id===v);return y||null});return e.jsx("div",{className:"semaphor-custom mb-3 flex grow basis-0 flex-col space-y-2 overflow-y-auto",children:e.jsxs(A.Suspense,{fallback:e.jsx(e.Fragment,{}),children:[x&&h==="single"&&e.jsx(x,{editing:!0,params:c,theme:d,settings:(M=t.customCardPreferences)==null?void 0:M.settings,onDataChange:j,data:(f==null?void 0:f.records)||[]}),x&&h==="multiple"&&e.jsxs(e.Fragment,{children:[Object.keys(((k=t.customCardPreferences)==null?void 0:k.dataInputCardIds)||{}).map(I=>{var L,F,P;const v=(F=(L=t.customCardPreferences)==null?void 0:L.dataInputCardIds)==null?void 0:F[I].cardId,y=(P=u==null?void 0:u.cards)==null?void 0:P.find(_=>_.id===v);if(y)return e.jsx(s.MultiInputEmptyRenderer,{onLoadingStatus:T,onDataChange:j,dataInputIndex:I,card:y},y.id+I)}),e.jsx("div",{className:"flex flex-col gap-2",children:e.jsx(x,{theme:d,onDataChange:j,settings:g.map(I=>{var v;return(v=I==null?void 0:I.customCardPreferences)==null?void 0:v.settings}),data:g.map(I=>I!=null&&I.id?r==null?void 0:r[I.id]:[])})})]})]},(R=t.customCardPreferences)==null?void 0:R.componentName)})}function Rt({className:t,...a}){var b;const n=s.useEditorStore(C=>C.card),r=s.isExplorerCard(n),l=((b=n.customCardPreferences)==null?void 0:b.showCardHeader)??!0,o=s.useEditorStore(C=>C.frame),c=s.useDashboardStore(C=>C.dashboard.filters);s.useEditorStore(C=>C.actions.setRunSql);const{updateFilter:i}=s.useDashboardActions(),d=c==null?void 0:c.find(C=>C.id===o.filterId),{data:m,isLoading:h,isFetching:u,errorMessage:p}=s.useDashboardCardQuery(n),{setCard:f}=s.useEditorActions(),{data:j}=s.useTopoJson(n),x=A.useMemo(()=>!n.id||!(m!=null&&m.records)?null:s.createChartConfig({card:n,data:m==null?void 0:m.records,cardType:n.type,queryConfig:n.queryConfig,customCfg:n.customCfg,preferences:n.preferences,topoJson:j}),[n.id,m==null?void 0:m.records,n.type,n.queryConfig,n.customCfg,n.preferences,j]);function T(C){return e.jsx(Za,{card:C})}function g(){if(m!=null&&m.records&&(m==null?void 0:m.records.length)===0&&!h&&!u)return p?e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.SearchX,{className:"size-8 text-destructive"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:r?"Error fetching data. Please check your chart configuration":"Error fetching data. Please check your SQL or Python code."})]}):e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No results to display"})]});if(!n.id)return null;if(n.type==="custom")return T(n);if(!["table","custom"].includes(n.type)&&x)return e.jsx(e.Fragment,{children:h||u?e.jsx(e.Fragment,{}):e.jsx(s.ChartJsVisual,{cfg:x})});if(n.type==="table"&&(m!=null&&m.records)&&(m==null?void 0:m.records.length)>0)return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableVisual,{card:n,data:m==null?void 0:m.records})})}return e.jsxs(s.Card,{role:"editor-visual-card",className:s.cn("relative flex h-full flex-col rounded-none",t),...a,children:[l&&e.jsxs(s.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{children:[e.jsx(s.Editable,{enabled:!0,text:n.title,onSave:C=>f({...n,title:C}),children:e.jsx("div",{className:"flex items-center gap-5",children:e.jsx(s.CardTitle,{className:"text-base",children:n.title||"Add title"})})},n.title),e.jsx(s.Editable,{enabled:!0,text:n.description||"",onSave:C=>f({...n,description:C}),children:e.jsx(s.CardDescription,{children:n.description||"Add description"})},n.description)]}),d&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Editable,{enabled:!0,className:"my-1",text:d.title||d.column,onSave:C=>i({...d,title:C}),children:e.jsx(s.Label,{children:d.title})}),e.jsx(s.FilterComponent,{filter:d})]})]}),e.jsx(s.CardContent,{className:"flex grow flex-col",children:g()})]})}const ts=({language:t,value:a,onChange:n,readonly:r})=>e.jsx(s.Ft,{className:"h-full w-full overflow-hidden text-xs",theme:"default",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,lineHeight:16,renderLineHighlight:"none",readOnly:r},defaultLanguage:t,value:a,onChange:n}),er=()=>e.jsxs(s.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(s.DialogTitle,{children:"Customize Card"})}),e.jsxs(s.DialogDescription,{children:["See the configuration options and documentation",e.jsx("a",{target:"_blank",className:"ml-1 text-blue-500 underline",href:"https://www.chartjs.org/docs/latest/samples/bar/vertical.html",children:"here."})]})]}),sr=({customCode:t,cardCustomCfgString:a,mergedConfig:n,onConfigChange:r,onCodeChange:l})=>e.jsxs(s.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(s.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(s.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(s.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(s.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(s.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{language:"json",value:n,onChange:r})}),e.jsx(s.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{readonly:!0,language:"json",value:a,onChange:()=>{}})}),e.jsx(s.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{language:"javascript",value:t,onChange:l})})]}),tr=({error:t,hasCardCustomConfig:a,onReset:n,onApply:r,onClose:l})=>e.jsxs(s.DialogFooter,{className:"flex items-center",children:[e.jsx(s.Label,{className:"mr-2 text-red-500",children:t}),e.jsx(s.Button,{disabled:!a,onClick:n,variant:"outline",children:"Reset"}),e.jsx(s.Button,{onClick:r,variant:"outline",children:"Apply"}),e.jsx(s.Button,{onClick:l,variant:"outline",children:"Close"})]});function nr(){const[t,a]=A.useState(!1),[n,r]=A.useState(""),[l,o]=A.useState(""),[c,i]=A.useState(""),[d,m]=A.useState(""),h=s.useEditorStore(S=>S.card),u=s.useEditorStore(S=>S.card.customCfg),p=u?JSON.stringify(u,null,2):"",{data:f}=s.useDashboardCardQuery(h),{data:j}=s.useTopoJson(h),{setCardCustomCfg:x,setCustomVisualCode:T}=s.useEditorActions(),g=S=>{var E,N;return JSON.stringify({...S,data:{...S.data,datasets:(N=(E=S.data)==null?void 0:E.datasets)==null?void 0:N.map(D=>({...D,data:[]}))}},null,2)},b=()=>{var E,N;if(!((E=f==null?void 0:f.records)!=null&&E.length))return;const S=s.createChartConfig({card:h,data:f.records,cardType:h.type,queryConfig:h.queryConfig,preferences:h.preferences,topoJson:j});if(S){r(g(S)),i(((N=h.preferences)==null?void 0:N.customVisualCode)||"");const D=s.merge(S,u);o(g(D))}},C=()=>{try{if(c&&T(c),l){const S=JSON.parse(l),E=s.getObjectDiff(JSON.parse(n),S);x(E),m("")}}catch{m("Invalid JSON configuration")}},O=Object.keys(u||{}).length===0;return e.jsxs(s.Dialog,{open:t,onOpenChange:a,children:[e.jsx(s.DialogTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Customize Visual",onClick:b,className:O?"":"text-green-600 hover:text-green-700",children:e.jsx(s.Settings,{className:"h-4 w-4"})})}),e.jsxs(s.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(er,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackEditorCard,children:e.jsx(Rt,{className:"w-1/2 rounded shadow-none"})}),e.jsx(sr,{customCode:c,cardCustomCfgString:p,mergedConfig:l,onConfigChange:S=>o(S??""),onCodeChange:S=>i(S??"")})]}),e.jsx(tr,{error:d,hasCardCustomConfig:!!u,onReset:()=>{o(n),x(null)},onApply:C,onClose:()=>a(!1)})]})]})}function Ye(t){const[a,n]=A.useState(null);return a}function Cs(){const{authToken:t}=s.useSemaphorContext(),{data:a,isLoading:n,isFetching:r,isError:l}=s.useQuery({queryKey:["plugins",t==null?void 0:t.accessToken],queryFn:()=>s.getPlugins(t==null?void 0:t.accessToken)});return{data:a,isLoading:n,isFetching:r,isError:l}}function ar({onChartTypeChange:t}){const[a,n]=A.useState([]),{setCardCustomCfg:r,setCardPreferences:l,setCardType:o,setCustomCardPreferences:c}=s.useEditorActions(),{data:i}=Cs();function d(m,h){r(null),l({}),o(m),h&&c({type:"component",visualType:"single",url:h.url,componentName:h.name,icon:h.icon}),t==null||t()}return A.useEffect(()=>{(async()=>{var h;if(i){const p=(await Promise.all((h=i==null?void 0:i.plugins)==null?void 0:h.map(async f=>(await s.loadManifest(f.value)).visuals.map(T=>({name:T.name,icon:T.icon,url:f.value}))))).flat();n(p)}})()},[i]),a.length===0?null:e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-2"}),e.jsx("div",{className:"grid grid-cols-4 place-items-center",children:a.map((m,h)=>e.jsx(rr,{text:m.name,onClick:()=>d("custom",m),icon:m.icon},m.url+m.name+h))})]})}const rr=({text:t,icon:a,onClick:n})=>{const r=Ye(),l=t.charAt(0).toUpperCase();return e.jsx(s.IconButton,{onClick:n,tooltip:t,children:r?e.jsx(r,{className:"h-4 w-4"}):l})};function Ss(t){return Se({attr:{viewBox:"0 0 256 256",fill:"currentColor"},child:[{tag:"path",attr:{d:"M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm87.63,96H191.48A64.1,64.1,0,0,0,136,64.52V40.37A88.13,88.13,0,0,1,215.63,120ZM120,120H80.68A48.09,48.09,0,0,1,120,80.68Zm0,16v39.32A48.09,48.09,0,0,1,80.68,136Zm16,0h39.32A48.09,48.09,0,0,1,136,175.32Zm0-16V80.68A48.09,48.09,0,0,1,175.32,120ZM120,40.37V64.52A64.1,64.1,0,0,0,64.52,120H40.37A88.13,88.13,0,0,1,120,40.37ZM40.37,136H64.52A64.1,64.1,0,0,0,120,191.48v24.15A88.13,88.13,0,0,1,40.37,136ZM136,215.63V191.48A64.1,64.1,0,0,0,191.48,136h24.15A88.13,88.13,0,0,1,136,215.63Z"},child:[]}]})(t)}function lr(t){return Se({attr:{viewBox:"0 0 256 256",fill:"currentColor"},child:[{tag:"path",attr:{d:"M48,180c0,11,7.18,20,16,20a14.24,14.24,0,0,0,10.22-4.66A8,8,0,0,1,85.78,206.4,30.06,30.06,0,0,1,64,216c-17.65,0-32-16.15-32-36s14.35-36,32-36a30.06,30.06,0,0,1,21.78,9.6,8,8,0,0,1-11.56,11.06A14.24,14.24,0,0,0,64,160C55.18,160,48,169,48,180Zm79.6-8.69c-4-1.16-8.14-2.35-10.45-3.84-1.25-.81-1.23-1-1.12-1.9a4.57,4.57,0,0,1,2-3.67c4.6-3.12,15.34-1.73,19.82-.56A8,8,0,0,0,142,145.86c-2.12-.55-21-5.22-32.84,2.76a20.58,20.58,0,0,0-9,14.95c-2,15.88,13.65,20.41,23,23.11,12.06,3.49,13.12,4.92,12.78,7.59-.31,2.41-1.26,3.34-2.14,3.93-4.6,3.06-15.17,1.56-19.55.36A8,8,0,0,0,109.94,214a61.34,61.34,0,0,0,15.19,2c5.82,0,12.3-1,17.49-4.46a20.82,20.82,0,0,0,9.19-15.23C154,179,137.49,174.17,127.6,171.31Zm83.09-26.84a8,8,0,0,0-10.23,4.84L188,184.21l-12.47-34.9a8,8,0,0,0-15.07,5.38l20,56a8,8,0,0,0,15.07,0l20-56A8,8,0,0,0,210.69,144.47ZM216,88v24a8,8,0,0,1-16,0V96H152a8,8,0,0,1-8-8V40H56v72a8,8,0,0,1-16,0V40A16,16,0,0,1,56,24h96a8,8,0,0,1,5.66,2.34l56,56A8,8,0,0,1,216,88Zm-27.31-8L160,51.31V80Z"},child:[]}]})(t)}function ws(t){return Se({attr:{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"},child:[{tag:"path",attr:{d:"M6 16m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0"},child:[]},{tag:"path",attr:{d:"M16 19m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"},child:[]},{tag:"path",attr:{d:"M14.5 7.5m-4.5 0a4.5 4.5 0 1 0 9 0a4.5 4.5 0 1 0 -9 0"},child:[]}]})(t)}function Ts(t){return Se({attr:{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"},child:[{tag:"path",attr:{d:"M4.387 3h15.226a1 1 0 0 1 .948 1.316l-5.105 15.316a2 2 0 0 1 -1.898 1.368h-3.116a2 2 0 0 1 -1.898 -1.368l-5.104 -15.316a1 1 0 0 1 .947 -1.316"},child:[]},{tag:"path",attr:{d:"M5 9h14"},child:[]},{tag:"path",attr:{d:"M7 15h10"},child:[]}]})(t)}function ks(t){return Se({attr:{viewBox:"0 0 24 24"},child:[{tag:"path",attr:{fill:"none",d:"M0 0h24v24H0z"},child:[]},{tag:"path",attr:{d:"m2 19.99 7.5-7.51 4 4 7.09-7.97L22 9.92l-8.5 9.56-4-4-6 6.01-1.5-1.5zm1.5-4.5 6-6.01 4 4L22 3.92l-1.41-1.41-7.09 7.97-4-4L2 13.99l1.5 1.5z"},child:[]}]})(t)}function Ds(t){return Se({attr:{version:"1.2",baseProfile:"tiny",viewBox:"0 0 24 24"},child:[{tag:"path",attr:{d:"M4 18c-.552 0-1-.448-1-1v-6.382l-.553.276c-.495.248-1.095.046-1.342-.447-.247-.494-.046-1.094.448-1.342l2-1c.31-.155.678-.139.973.044.294.183.474.504.474.851v8c0 .552-.448 1-1 1zM13 18h-5c-.404 0-.769-.244-.924-.617-.155-.374-.069-.804.217-1.09l4-4c.254-.254.394-.591.394-.95 0-.358-.14-.695-.394-.949-.508-.508-1.39-.508-1.9.001-.253.252-.393.589-.393.948 0 .552-.448 1-1 1s-1-.448-1-1c0-.894.348-1.733.98-2.364 1.265-1.263 3.464-1.263 4.727.001.632.631.979 1.471.979 2.363 0 .893-.348 1.733-.979 2.364l-2.293 2.293h2.586c.552 0 1 .448 1 1s-.448 1-1 1zM20.955 12.377c.338-.457.545-1.016.545-1.627 0-1.517-1.234-2.75-2.75-2.75-1.031 0-1.966.569-2.44 1.484-.254.49-.063 1.094.428 1.348.49.254 1.094.062 1.348-.428.128-.249.383-.404.664-.404.414 0 .75.336.75.75s-.336.75-.75.75c-.552 0-1 .448-1 1s.448 1 1 1c.689 0 1.25.561 1.25 1.25s-.561 1.25-1.25 1.25-1.25-.561-1.25-1.25c0-.552-.448-1-1-1s-1 .448-1 1c0 1.792 1.458 3.25 3.25 3.25s3.25-1.458 3.25-3.25c0-.939-.406-1.779-1.045-2.373z"},child:[]}]})(t)}function Bt({onChartTypeChange:t,iconClassName:a}){const{setCardType:n,setCardPreferences:r,setIsDevMode:l}=s.useEditorStore(h=>h.actions),{setCardCustomCfg:o,setCustomCardPreferences:c,setQueryConfig:i}=s.useEditorActions();function d(h,u){o(null),r({}),n(h),["custom","text","map"].includes(h)&&(l(!0),i(void 0)),t==null||t()}const m=1e3;return e.jsxs("div",{className:"grid grid-cols-5 place-items-center",children:[e.jsx(s.IconButton,{openDelay:m,tooltip:"Table",onClick:()=>d("table"),className:"h-10 p-4",children:e.jsx(Je,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Bar Chart",onClick:()=>d("bar"),className:"h-10 p-4",children:e.jsx($e,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Line Chart",onClick:()=>d("line"),className:"h-10 p-4",children:e.jsx(Ge,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Stacked Bar Chart",onClick:()=>d("stackedBar"),className:"h-10 p-4",children:e.jsx(ms,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Stacked Line Chart",onClick:()=>d("stackedLine"),className:"h-10 p-4",children:e.jsx(ks,{className:s.cn("size-5",a),strokeWidth:0})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Scatter Chart",onClick:()=>d("scatter"),className:"h-10 p-4",children:e.jsx(xs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Bubble Chart",onClick:()=>d("bubble"),className:"h-10 p-4",children:e.jsx(ws,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Pie Chart",onClick:()=>d("pie"),className:"h-10 p-4",children:e.jsx(He,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Doughnut Chart",onClick:()=>d("doughnut"),className:"h-10 p-4",children:e.jsx(ps,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Funnel Chart",onClick:()=>d("funnel"),className:"h-10 p-4",children:e.jsx(Ts,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Radar Chart",onClick:()=>d("radar"),className:"h-10 p-4",children:e.jsx(js,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Polar Area Chart",onClick:()=>d("polarArea"),className:"h-10 p-4",children:e.jsx(Ss,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Tornado Chart",onClick:()=>d("tornado"),className:"h-10 p-4",children:e.jsx(ys,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{tooltip:"Range Chart",onClick:()=>d("range"),className:"h-10 p-4",children:e.jsx(hs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"KPI",onClick:()=>d("kpi"),className:"h-10 p-4",children:e.jsx(Ds,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Text",onClick:()=>d("text"),className:"h-10 p-4",children:e.jsx(Zs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{tooltip:"Map",onClick:()=>d("map"),className:"h-10 p-4",children:e.jsx(gs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Custom",onClick:()=>d("custom"),className:"h-10 p-4",children:e.jsx(Ae,{className:s.cn("size-5",a)})})]})}function or({chartType:t,className:a,customIcon:n,componentName:r}){const l=Ye();switch(t){case"bar":return e.jsx($e,{className:a});case"line":return e.jsx(Ge,{className:a});case"pie":return e.jsx(He,{className:a});case"radar":return e.jsx(js,{className:a});case"scatter":return e.jsx(xs,{className:a});case"bubble":return e.jsx(ws,{className:a});case"doughnut":return e.jsx(ps,{className:a});case"stackedBar":return e.jsx(ms,{className:a});case"stackedLine":return e.jsx(ks,{className:a});case"polarArea":return e.jsx(Ss,{className:a});case"funnel":return e.jsx(Ts,{className:a});case"tornado":case"pyramid":return e.jsx(ys,{className:a});case"range":return e.jsx(hs,{className:a});case"kpi":return e.jsx(Ds,{className:a});case"text":return e.jsx(it,{className:a});case"map":return e.jsx(gs,{className:a});case"custom":return n&&l?e.jsx(l,{className:a}):r?r==null?void 0:r.charAt(0).toUpperCase():e.jsx(Ae,{className:a});default:return e.jsx(Je,{className:a})}}function Ft({variant:t="ghost"}){var o,c;const[a,n]=A.useState(!1),r=s.useEditorStore(i=>i.card),l=r.type;return s.useDashboardStore(i=>i.isVisualEditing),e.jsxs(s.Popover,{open:a,onOpenChange:n,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{size:"sm",variant:t||"ghost",children:e.jsx(or,{className:"size-4",chartType:l,customIcon:(o=r.customCardPreferences)==null?void 0:o.icon,componentName:(c=r.customCardPreferences)==null?void 0:c.componentName})})}),e.jsxs(s.PopoverContentCustom,{onOpenAutoFocus:i=>i.preventDefault(),className:"w-72 p-1",children:[e.jsx(Bt,{onChartTypeChange:()=>n(!1)}),e.jsx(ar,{onChartTypeChange:()=>n(!1)})]})]})}const Oe="focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0";function Js(t,a){var r,l,o;const n=s.getNumberAxis(t,{});return["pie","doughnut","polarArea","funnel"].includes(a)?"category":((o=(l=(r=t==null?void 0:t.chartOptions)==null?void 0:r.scales)==null?void 0:l[n])==null?void 0:o.type)||"linear"}function ir(){var L,F,P,_,z,Q;const[t,a]=A.useState(!1),n=s.useEditorStore(Y=>{var V;return(V=Y.card)==null?void 0:V.preferences}),r=s.useEditorStore(Y=>{var V;return(V=Y.card)==null?void 0:V.type}),l=s.getNumberAxis(n,{}),[o,c]=A.useState(Js(n,r)),[i,d]=A.useState(((P=(F=(L=n==null?void 0:n.chartOptions)==null?void 0:L.scales)==null?void 0:F[l])==null?void 0:P.min)||""),[m,h]=A.useState(((Q=(z=(_=n==null?void 0:n.chartOptions)==null?void 0:_.scales)==null?void 0:z[l])==null?void 0:Q.max)||""),[u,p]=A.useState(i||m?"custom":"auto"),[f,j]=A.useState(""),[x,T]=A.useState(0),[g,b]=A.useState(""),[C,O]=A.useState(""),[S,E]=A.useState(""),N=s.useEditorStore(Y=>Y.actions.setCardPreferences),{updateCardInFrame:D}=s.useEditorActions(),w=s.useEditorStore(Y=>Y.card),M=["pie","doughnut","polarArea","funnel"].includes(r);A.useEffect(()=>{var Y,V,q,$,U,H,W,G,Z,se,ne,ee,ae,J;c(Js(n,r)),d(((q=(V=(Y=n==null?void 0:n.chartOptions)==null?void 0:Y.scales)==null?void 0:V[l])==null?void 0:q.min)===0?0:""),h(((H=(U=($=n==null?void 0:n.chartOptions)==null?void 0:$.scales)==null?void 0:U[l])==null?void 0:H.max)||""),j(((se=(Z=(G=(W=n==null?void 0:n.chartOptions)==null?void 0:W.scales)==null?void 0:G[l])==null?void 0:Z.ticks)==null?void 0:se.stepSize)||""),T(((ne=n==null?void 0:n.numberAxisFormat)==null?void 0:ne.decimalPlaces)||0),b(((ee=n==null?void 0:n.numberAxisFormat)==null?void 0:ee.suffix)||""),O(((ae=n==null?void 0:n.numberAxisFormat)==null?void 0:ae.currency)||""),E(((J=n==null?void 0:n.numberAxisFormat)==null?void 0:J.locale)||"")},[t,n,l,r]);function k(Y,V){return V===""||/^[0-9\b]+$/.test(V)?Number(V):Number(Y)}function R(){const Y=[...new Set(s.LOCALE_CURRENCY_PAIRS.map(V=>V.currency))];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-6 items-center gap-2",children:[e.jsx(s.Label,{className:"col-span-2 hover:cursor-pointer hover:underline",htmlFor:"currency",children:e.jsx("a",{target:"_blank",href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat",children:"Currency"})}),e.jsxs(s.Select,{value:C||"none",onValueChange:V=>O(V==="none"?"":V),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Currency"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),Y.map(V=>e.jsx(s.SelectItem,{value:V,children:V},V))]})})]}),e.jsxs(s.Select,{value:S,onValueChange:V=>E(V),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Locale"}),s.LOCALE_CURRENCY_PAIRS.map(V=>e.jsx(s.SelectItem,{value:V.locale,children:V.locale},V.locale))]})})]})]}),e.jsxs("div",{className:"grid grid-cols-9 items-center gap-2",children:[e.jsx(s.Label,{className:"col-span-3",htmlFor:"format",children:"Decimals"}),e.jsx(s.Input,{value:x,onChange:V=>T(Number(V.target.value)),id:"format",className:`col-span-6 h-8 w-full tracking-wide ${Oe}`})]})]})}function I(){return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mt-4 space-y-2",children:e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the scale for number axis"})}),e.jsxs("div",{className:"mt-2 grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"scale",children:"Type"}),e.jsxs(s.Select,{disabled:M,value:o,onValueChange:Y=>c(Y),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{id:"type",children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Option"}),e.jsx(s.SelectItem,{value:"linear",children:"Linear"}),e.jsx(s.SelectItem,{value:"logarithmic",children:"Logarithmic"}),e.jsx(s.SelectItem,{value:"category",children:"Category"})]})})]})]}),o=="linear"&&e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2 py-1",children:[e.jsx(s.Label,{htmlFor:"scale-option",children:"Setup"}),e.jsxs(s.RadioGroup,{disabled:M,id:"scale-option",value:u,onValueChange:Y=>p(Y),className:"col-span-2 flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"auto",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Auto"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"custom",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Custom"})]})]})]}),u==="custom"&&o=="linear"&&v()]})}function v(){return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"range",children:"Range"}),e.jsx(s.Input,{id:"min",value:i,onChange:Y=>d(V=>k(V,Y.target.value)),className:`col-span-1 h-8 w-full ${Oe}`}),e.jsx(s.Input,{value:m,onChange:Y=>h(V=>k(V,Y.target.value)),id:"max",className:`col-span-1 h-8 w-full ${Oe}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"step-size",children:"Step Size"}),e.jsx(s.Input,{value:f,onChange:Y=>j(V=>k(V,Y.target.value)),id:"step-size",className:`col-span-1 h-8 w-full ${Oe}`})]})]})}function y(){var $;let Y={};u==="custom"&&(Y={min:i,max:m,ticks:{stepSize:f}});const V={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,scales:{...($=n==null?void 0:n.chartOptions)==null?void 0:$.scales,[l]:{type:o,...o==="linear"?Y:{}}}},numberAxisFormat:{decimalPlaces:x,suffix:g,currency:C,locale:S}};console.log(V),N(V);const q={...w,preferences:V};D(q)}return e.jsxs(s.Popover,{onOpenChange:Y=>a(Y),children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Format Number",children:e.jsx(Dn,{className:"size-4"})})}),e.jsx(s.PopoverContentCustom,{align:"end",className:"max-w-65",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Format Options"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the format for number axis"})]}),e.jsxs("div",{className:"grid gap-2",children:[R(),I(),e.jsx(s.SelectSeparator,{}),e.jsx("div",{className:"grid grid-cols-3",children:e.jsx(s.Button,{onClick:y,variant:"secondary",className:"col-span-1 col-start-3 h-8 w-full",children:"Apply"})})]})]})})]})}function cr(){const t=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.position}),a=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.align}),n=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.display}),{setLegendOptions:r}=s.useEditorActions(),l=i=>{r({display:i,position:t,align:a})},o=i=>{r({display:n,position:i,align:a})},c=i=>{r({display:n,position:t,align:i})};return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Legend Settings",children:e.jsx(Pn,{className:"size-4"})})}),e.jsx(s.PopoverContentCustom,{align:"end",className:"w-72",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Legend Options"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the legend options for the chart."})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"displayLegend",children:"Display"}),e.jsx("div",{className:"col-span-2 flex h-8 items-center",children:e.jsx(s.SmallSwitch,{checked:n,onCheckedChange:l,id:"displayLegend"})})]}),n&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"position",children:"Position"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(s.IconButton,{tooltip:"Top",onClick:()=>o("top"),children:e.jsx(s.ArrowUp,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Left",onClick:()=>o("left"),children:e.jsx(hn,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Bottom",onClick:()=>o("bottom"),children:e.jsx(s.ArrowDown,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Right",onClick:()=>o("right"),children:e.jsx(xn,{className:"size-4"})})]})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"align",children:"Align"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(s.IconButton,{tooltip:"Start",onClick:()=>c("start"),children:e.jsx(on,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Center",onClick:()=>c("center"),children:e.jsx(ln,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"End",onClick:()=>c("end"),children:e.jsx(cn,{className:"size-4"})})]})]})]})]})]})})]})}function dr(){const t=s.useEditorStore(o=>{var c,i;return(i=(c=o.card.preferences)==null?void 0:c.chartOptions)==null?void 0:i.indexAxis}),a=s.useEditorStore(o=>o.card.type),{setChartOrientation:n}=s.useEditorStore(o=>o.actions);function r(){return e.jsx(s.IconButton,{onClick:()=>{n(t==="x"||t===void 0?"y":"x")},tooltip:"Chart Orientation",children:t==="x"||t===void 0?e.jsx(_s,{className:"size-4"}):e.jsx(Ps,{className:"size-4"})})}function l(){return e.jsx(s.IconButton,{onClick:()=>{n(t==="y"||t===void 0?"x":"y")},tooltip:"Chart Orientation",children:t==="y"||t===void 0?e.jsx(_s,{className:"size-4"}):e.jsx(Ps,{className:"size-4"})})}return e.jsx(e.Fragment,{children:a==="funnel"?l():r()})}function zt(){const t=s.useEditorStore(c=>c.card),a=s.useEditorStore(c=>c.isShowingVisual),n=s.useEditorStore(c=>{var i,d;return(d=(i=c.card)==null?void 0:i.preferences)==null?void 0:d.chartOptions}),r=s.getChartGridStyle(n),{setChartGrid:l}=s.useEditorActions();function o(){const c=["xy","none","x","y"],d=(c.indexOf(r)+1)%c.length;l(c[d])}return a?e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel"].includes(t.type)&&e.jsx(e.Fragment,{children:e.jsx(dr,{})}),!["kpi","text","custom","table"].includes(t.type)&&e.jsxs(e.Fragment,{children:[e.jsx(ir,{}),e.jsx(cr,{})]}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(t.type)&&e.jsxs(s.IconButton,{tooltip:"Grid Style",onClick:o,children:[(r==="xy"||!r)&&e.jsx(Bn,{className:"size-4"}),r==="x"&&e.jsx(wn,{className:"size-4"}),r==="y"&&e.jsx(Qn,{className:"size-4"}),r==="none"&&e.jsx(ta,{className:"size-4"})]})]}):null}function ur(){const t=s.useEditorStore(j=>j.frame),a=s.useEditorStore(j=>j.card),{setFrame:n,setActiveTabCardId:r,setCard:l,updateCardInFrame:o}=s.useEditorActions(),c=(j,x)=>{j.dataTransfer.setData("tabIndex",x.toString())},i=j=>{j.preventDefault()},d=(j,x)=>{const T=j.dataTransfer.getData("tabIndex");if(T===x.toString())return;const g=Array.from(t.cards),[b]=g.splice(parseInt(T),1);g.splice(x,0,b),n({...t,cards:g})};function m(j){const x=t.cards.filter(b=>b.id!==j),T=x[0],g={...t,cards:x,activeCardId:T.id};n(g),r(T.id),l(T)}function h(j,x){const T={...t,cards:[...t.cards,x],activeCardId:j.id};n(T),r(x.id)}function u(){const j={...a,id:s.v4(),title:`${a.title} Copy`,tabTitle:`${a.tabTitle||a.title} Copy`};h(a,j)}function p(j){const x={dataSource:j.dataSource,connectionId:j.connectionId,lastSelectedDatabase:j.lastSelectedDatabase,lastSelectedSchema:j.lastSelectedSchema,lastSelectedTable:j.lastSelectedTable,lastSelectedDatamodelId:j.lastSelectedDatamodelId,id:s.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[],mode:"explorer"};h(j,x)}function f(j,x){return j.cards.length===1&&(x.displayTab===!0||x.displayTab===void 0)||j.activeCardId!==x.id&&(x.displayTab===!0||x.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const T=x.displayTab===void 0?!1:!x.displayTab;l({...x,displayTab:T}),o({...x,displayTab:T})},children:x.displayTab===!0||x.displayTab===void 0?e.jsx(s.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(En,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}return e.jsx(s.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[t.cards.map((j,x)=>e.jsxs(s.TabsTrigger,{draggable:!0,onDragStart:T=>c(T,x),onDragOver:i,onDrop:T=>d(T,x),className:"",value:j.id,children:[f(t,j),e.jsx(s.Editable,{enabled:t.activeCardId===j.id,text:j.tabTitle||j.title,onSave:T=>{l({...a,tabTitle:T}),o({...a,tabTitle:T})},children:j.tabTitle||j.title},j.id),t.activeCardId===j.id&&t.cards.length>1&&e.jsx(s.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>m(j.id)})]},j.id)),e.jsx(s.Button,{className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",variant:"outline",onClick:()=>p(a),children:e.jsx(s.Plus,{className:"h-4 w-4"})}),e.jsx(s.Button,{className:"invisible ml-1 h-8 px-2 hover:bg-muted-foreground/50 group-hover:visible",variant:"outline",onClick:u,children:e.jsx(kn,{className:"h-4 w-4"})})]})})}function Pt(){const t=s.useEditorStore(x=>x.frame),a=s.useEditorStore(x=>x.card),n=s.useEditorStore(x=>x.isSqlRunning),r=s.useEditorStore(x=>x.isDevMode),{setFrame:l,setActiveTabCardId:o,setCard:c,setIsDevMode:i,setRunSql:d}=s.useEditorActions(),{data:m,isLoading:h,isFetching:u}=s.useDashboardCardQuery(a);function p(x){const T=t.cards.find(g=>g.id===x);if(T!=null&&T.sql){r||i(!0),o(x);return}if(T!=null&&T.queryConfig){r&&i(!1),f(x),o(x);return}r||f(x),o(x)}function f(x){const T={...t,cards:t.cards.map(b=>b.id===a.id?a:b),activeCardId:x};l(T);const g=T.cards.find(b=>b.id===x);g.sql&&d(!0),c(g)}function j(){return a.type==="kpi"?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.KPICard,{card:a,data:m==null?void 0:m.records,isPending:h})})}):a.type==="text"?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(s.TextCard,{isLoading:n,card:a,data:m==null?void 0:m.records})})})}):e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackEditorCard,children:e.jsx(Rt,{className:"grow border-none shadow-none"})})}return e.jsxs(s.Tabs,{onValueChange:p,value:t.activeCardId,className:"flex grow flex-col",children:[t.cards.map(x=>e.jsx(s.TabsContent,{className:"mt-0 grow",value:x.id,children:j()},x.id)),e.jsx(ur,{})]})}function Re({children:t}){s.useEditorStore(b=>b.frame);const a=s.useEditorStore(b=>b.card),n=s.useEditorStore(b=>b.isDevMode),r=s.useEditorStore(b=>b.isShowingVisual),l=s.useDashboardStore(b=>b.isVisualEditing),o=s.useEditorStore(b=>b.pythonStdOut);s.useEditorStore(b=>b.actions.setRunSql);const{setFrame:c,setCard:i,setActiveTabCardId:d,runAssistant:m,setIsDevMode:h}=s.useEditorActions(),{data:u,isLoading:p,isFetching:f,errorMessage:j}=s.useDashboardCardQuery(a);function x(){const b=JSON.stringify(u==null?void 0:u.records,null,2),C=new Blob([b],{type:"application/json"}),O=URL.createObjectURL(C),S=document.createElement("a");S.href=O,S.download=`${a.title}.json`,S.click()}function T(){const C=[Object.keys(u==null?void 0:u.records.reduce((N,D)=>({...N,...D}),{})).join(","),...((u==null?void 0:u.records)||[]).map(N=>Object.values(N).join(","))].join(`
414
- `),O=new Blob([C],{type:"text/csv"}),S=URL.createObjectURL(O),E=document.createElement("a");E.href=S,E.download=`${a.title}.csv`,E.click()}function g(){const b=`Can you get the documentation for ${a.type} chart?`;m(b)}return a?e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[(p||f)&&e.jsx("div",{className:"absolute bottom-0 left-0 right-0 top-0 z-50 m-auto flex h-10 w-10 items-center justify-center rounded-md bg-muted",children:e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-muted-foreground"})}),e.jsxs("div",{className:"z-10 mb-0 flex min-h-10 items-center justify-end gap-2 px-6",children:[r&&e.jsxs("div",{className:"flex grow items-center",children:[e.jsx(Ft,{}),l&&e.jsx(s.IconButton,{tooltip:"Visual Docs",onClick:g,children:e.jsx(jn,{className:"size-4"})}),!["table","custom"].includes(a.type)&&l&&e.jsx(nr,{})]}),!r&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:T,children:e.jsx(lr,{className:"size-4"})}),e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:x,children:e.jsx(An,{strokeWidth:1.5,className:"size-4"})}),e.jsx(s.CopyButton,{text:JSON.stringify(u==null?void 0:u.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[t,e.jsx(zt,{})]}),n&&e.jsx(hr,{})]}),r?e.jsx(Pt,{}):e.jsxs(s.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[o&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:o&&e.jsx(mr,{})}),e.jsx(s.ResizableHandle,{})]}),e.jsxs(s.ResizablePanel,{id:"2",order:2,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:[(u==null?void 0:u.records)&&(u==null?void 0:u.records.length)>0&&e.jsx(Xa,{data:(u==null?void 0:u.records)||[]}),j&&e.jsx(Et,{error:j})]})]})]}):e.jsx(e.Fragment,{children:"No Active Card"})}function mr(){const t=s.useEditorStore(n=>n.pythonStdOut);if(!t)return null;const a=t.replace(/\n/g,"<br>");return e.jsxs("div",{className:"relative flex w-full flex-col border-r",children:[e.jsx("div",{className:"sticky top-0 flex max-h-11 min-h-11 grow items-center border-b bg-muted px-3 font-mono text-xs font-semibold text-foreground/70",children:"Python Output"}),e.jsxs(s.ScrollArea,{className:"overflow-y-auto px-2 pt-1 font-mono text-xs text-foreground/90",children:[e.jsx("div",{dangerouslySetInnerHTML:{__html:a}}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function hr(){const t=s.useEditorStore(n=>n.isShowingVisual),a=s.useEditorStore(n=>n.actions.setIsShowingVisual);return e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Label,{htmlFor:"visualize",children:"Chart"}),e.jsx(s.SmallSwitch,{checked:t,onCheckedChange:()=>a(!t),id:"visualize",className:"data-[state=checked]:bg-foreground/50"})]})}function xr(){var x,T;const t=s.useEditorStore(g=>g.card.customCardPreferences),{data:a}=Cs(),n=(a==null?void 0:a.plugins)||[],r=s.useEditorStore(g=>g.card),l=s.useEditorStore(g=>g.frame),o=(t==null?void 0:t.visualType)||"single",{setCustomCardPreferences:c}=s.useEditorActions(),{components:i,manifest:d}=s.useCustomVisual((t==null?void 0:t.url)||""),m=(x=d==null?void 0:d.visuals)==null?void 0:x.find(g=>g.name===(t==null?void 0:t.componentName));function h(g,b){if(!t)return;const O={...(t==null?void 0:t.dataInputCardIds)||{},[g]:{cardId:b,hookRef:null}};c({...t,dataInputCardIds:O})}function u(g){var C,O;const b=(C=l==null?void 0:l.cards)==null?void 0:C.filter(S=>{var E;return((E=S==null?void 0:S.customCardPreferences)==null?void 0:E.visualType)!=="multiple"}).map(S=>({value:S.id,label:S.tabTitle||S.title}));return e.jsx("div",{className:"space-y-2",children:(O=g==null?void 0:g.dataInputs)==null?void 0:O.map((S,E)=>{var N,D;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:S}),e.jsx(ce,{className:"col-span-6 h-8 w-full justify-between",label:"",options:b||[],value:((D=(N=t==null?void 0:t.dataInputCardIds)==null?void 0:N[E])==null?void 0:D.cardId)||"",onValueChange:w=>h(E,w)})]},E+Math.random())})})}function p(g,b,C=[],O=""){var S,E;return t?g==="input"?e.jsx(s.Input,{className:`h-8 w-full ${s.removeRing}`,value:((S=t==null?void 0:t.settings)==null?void 0:S[b])||O,onChange:N=>{c({...t,settings:{...t==null?void 0:t.settings,[b]:N.target.value}})}},b):g==="select"?e.jsx(ce,{className:"h-8 w-full",label:"",options:C,value:((E=t==null?void 0:t.settings)==null?void 0:E[b])||O,onValueChange:N=>{c({...t,settings:{...t==null?void 0:t.settings,[b]:N}})}}):null:null}function f(g){const b=Object.entries((g==null?void 0:g.settings)||{});return e.jsx("div",{className:"space-y-3",children:b.map(([C,O])=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:O.title}),p(O.ui,C,O.options,O.defaultValue)]},C))})}function j(g){t&&c({...t,visualType:g,componentName:""})}return e.jsxs(s.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"px-6 py-2",children:[e.jsxs(s.AccordionItem,{value:"plugin-settings",children:[e.jsx(s.AccordionTrigger,{children:"Plugin"}),e.jsx(s.AccordionContent,{children:e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"space-y-4",children:[e.jsx(ce,{icon:e.jsx(gn,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:n,value:(t==null?void 0:t.url)||"",onValueChange:g=>c({...t||{url:"",componentName:""},url:g})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&e.jsx(s.Tabs,{value:o,onValueChange:j,children:e.jsxs(s.TabsList,{className:"h-9 w-full",children:[e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&t&&e.jsx(e.Fragment,{children:i&&e.jsx(ce,{icon:e.jsx(Tn,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(i==null?void 0:i.filter(g=>g.type===o).map(g=>({value:g.name,label:g.name})))||[],value:t.componentName,onValueChange:g=>{console.log("changing value",g),c({...t,componentName:g})}})})]})})]}),((T=r==null?void 0:r.customCardPreferences)==null?void 0:T.componentName)&&e.jsxs(s.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{children:[m&&m.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:u(m)})]}),m&&m.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:f(m)})})]})]})]})}function we(){const[t,a]=A.useState([]),n=s.useEditorStore(m=>m.card.preferences),{setFilterOnClickColumnIndex:r,setCardPreferences:l}=s.useEditorStore(m=>m.actions),o=s.useEditorStore(m=>m.queryResultColumns);s.useEditorStore(m=>{var h;return(h=m.card.preferences)==null?void 0:h.filterOnClickColumnIndex}),A.useEffect(()=>{o&&a(new Array(o.length).fill(!1))},[o]);function c(m,h){var u,p;h?n!=null&&n.onClickFilter&&((u=n==null?void 0:n.onClickFilter)==null?void 0:u.length)>0?l({...n,onClickFilter:[...n.onClickFilter,{columnIndex:m,expression:""}]}):l({...n,onClickFilter:[{columnIndex:m,expression:""}]}):(l({...n,onClickFilter:(p=n==null?void 0:n.onClickFilter)==null?void 0:p.filter(f=>f.columnIndex!==m)}),a(f=>{const j=[...f];return j[m]=!1,j}))}function i(m,h){var p;const u=(p=n==null?void 0:n.onClickFilter)==null?void 0:p.map(f=>f.columnIndex===m?{...f,expression:h.target.value}:f);l({...n,onClickFilter:u})}function d(m){var h,u,p,f;if(t!=null&&t[m]||(u=(h=n==null?void 0:n.onClickFilter)==null?void 0:h.find(j=>j.columnIndex===m))!=null&&u.expression)return e.jsx(s.Input,{value:(f=(p=n==null?void 0:n.onClickFilter)==null?void 0:p.find(j=>j.columnIndex===m))==null?void 0:f.expression,onChange:j=>i(m,j),type:"text",className:"h-9 w-full font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"Expression"})}return e.jsxs("div",{className:"max-h-[150px] space-y-2 overflow-y-auto",children:[e.jsx(s.Label,{children:"On Click"}),o==null?void 0:o.map((m,h)=>{var u,p;return e.jsx("div",{className:"group flex items-center justify-between space-y-2",children:e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex items-center space-x-2 py-2",children:[e.jsx(s.Checkbox,{checked:!!((u=n==null?void 0:n.onClickFilter)!=null&&u.find(f=>f.columnIndex===h)),onCheckedChange:f=>c(h,f)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:m}),e.jsx("div",{className:"flex items-center gap-2",children:((p=n==null?void 0:n.onClickFilter)==null?void 0:p.find(f=>f.columnIndex===h))&&e.jsx(s.Pencil,{onClick:()=>{a(f=>{const j=[...f];return j[h]=!f[h],j})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),d(h)]})},m)})]})}function pr({idx:t}){var i;const a=s.useEditorStore(d=>{var m;return(m=d.card)==null?void 0:m.preferences}),n=s.useEditorStore(d=>{var m;return(m=d.card.preferences)==null?void 0:m.datasetOptions}),r=s.useEditorStore(d=>d.actions.setCardPreferences),l=(i=a==null?void 0:a.chartOptions)==null?void 0:i.indexAxis,o=n==null?void 0:n.find(d=>d.idx===t);function c(d,m){(d==="end"||d==="start")&&s.getContrastColor(s.getCanvasBackgroundColor());const h={display:d!=="none",align:d,anchor:d,clamp:!0};if(!o)r({...a,datasetOptions:[...n||[],{idx:m,datalabels:h}]});else{const u={...o};u.datalabels=h;const p=n==null?void 0:n.map(f=>f.idx===m?u:f);r({...a,datasetOptions:p})}}return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{children:e.jsx(lt,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>c("none",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(s.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>c("start",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(_n,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})}),e.jsx("button",{onClick:()=>c("center",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Un,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})}),e.jsx("button",{onClick:()=>c("end",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Vn,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})})]})})]})}function fr(){const t=s.useEditorStore(o=>o.card),{data:a}=s.useDashboardCardQuery(t),{keys:n}=s.getKeys((a==null?void 0:a.records)||[]),r=n.slice(1).filter(o=>!s.METADATA_KEYS.includes(o));let l=r;if(["stackedBar","stackedLine"].includes(t.type)){const o=s.pivotData({dataArray:(a==null?void 0:a.records)||[],groupKey:n[0],pivotKey:n[1],valueKey:n[2]}),{keys:c}=s.getKeys(o);l=c.slice(1)}return{transformedMetricKeys:l,dimensionKeys:n.slice(0,1),keys:n,defaultMetricKeys:r}}function Es({children:t,className:a,delayDuration:n}){return e.jsx(s.TooltipProvider$1,{children:e.jsxs(s.Tooltip,{delayDuration:n,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:s.cn("overflow-hidden text-ellipsis whitespace-nowrap",a),children:t})}),e.jsx(s.TooltipContent,{children:t})]})})}function _t(){const t=s.useEditorStore(d=>d.card),a=s.useEditorStore(d=>{var m,h,u;return(u=(h=(m=d==null?void 0:d.card)==null?void 0:m.preferences)==null?void 0:h.chartOptions)==null?void 0:u.indexAxis}),{data:n}=s.useDashboardCardQuery(t),r=a==="y"?"Metric":"Dimension",l=a==="y"?"Dimension":"Metric",{dimensionKeys:o,transformedMetricKeys:c,keys:i}=fr();return i.length===0?null:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:r}),e.jsx("div",{className:"max-h-[125px] space-y-1 overflow-y-auto",children:o.map(d=>e.jsx("p",{className:"flex h-8 items-center rounded-md bg-muted/70 px-3 hover:bg-muted",title:d,children:Vt(d)},d))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:l}),e.jsx("div",{className:"my-1 max-h-[125px] space-y-1 overflow-y-auto",children:c.map((d,m)=>e.jsx(Ut,{className:"flex h-8 items-center bg-muted/70 px-3 hover:bg-muted",iconClassName:"invisible group-hover:visible",col:d,idx:m},d))})]})]})}function Vt(t,a=30){return t.length>a?t.slice(0,a)+"...":t}function Ut({col:t,idx:a,className:n,iconClassName:r,children:l}){const o=s.useEditorStore(h=>{var u;return(u=h.card)==null?void 0:u.preferences}),c=s.useEditorStore(h=>{var u,p;return(p=(u=h.card)==null?void 0:u.preferences)==null?void 0:p.datasetOptions}),i=c==null?void 0:c.find(h=>h.idx===a),{setCardPreferences:d}=s.useEditorStore(h=>h.actions);function m(h){if(!i)d({...o,datasetOptions:[...c||[],{idx:a,type:h==="area"?"line":h,fill:h==="area"?"origin":""}]});else{const u={...i};u.type=h==="area"?"line":h,u.fill=h==="area"?"origin":"";const p=c==null?void 0:c.map(f=>f.idx===a?u:f);d({...o,datasetOptions:p})}}return e.jsxs("div",{className:s.cn("group flex items-center justify-between gap-2 rounded-md",n),children:[l||e.jsx(Es,{className:"max-w-[100px]",title:t,children:Vt(t)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx($e,{onClick:()=>m("bar"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Ge,{onClick:()=>m("line"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(et,{onClick:()=>m("area"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(pr,{idx:a})]})]})}function qt(){const t=s.useEditorStore(f=>f.selectedConnectionId),a=s.useEditorStore(f=>f.selectedDatamodelId),n=s.useEditorStore(f=>f.selectedDatabaseName),r=s.useEditorStore(f=>f.selectedSchemaName),l=s.useEditorStore(f=>f.selectedTableName),o=s.useDashboardStore(f=>f.dashboard.filters)||[],c=s.useDashboardStore(f=>f.actions.addFilter),i=s.useDashboardStore(f=>f.actions.removeFilter),{removeFilterValues:d}=s.useDashboardActions(),{modelItems:m,tableName:h,connectionType:u}=s.useEditorAside();function p(f,j="dashboard"){var N;const x=s.fmt(f.column_name),T=s.fmt(n||""),g=s.fmt(r||""),b=s.fmt(h);let C=s.getQualifiedTableName({schemaName:g,tableName:b,connectionType:u,databaseName:T}),O=`${C}.${x}`;const S=(N=m==null?void 0:m.find(D=>D.id===a))==null?void 0:N.name;a&&a!=="none"&&(O=`${s.DATAMODEL_NAMESPCACE}.${S}.${x}`,C=`${s.DATAMODEL_NAMESPCACE}.${S}`);const E=o==null?void 0:o.find(D=>D.column===O&&D.table===b&&(D.database===T||D.database===n));if(E)d(E.id),i(E.id);else{const D=s.v4();return h==="api"&&t?(c({location:j,id:D,column:O,title:f.column_name,dataType:f.data_type,table:"api",database:"",connectionId:t,operation:"in",sql:s.getDefaultFilterSql(f.data_type,C,x)}),D):(c({id:D,location:j,column:O,title:f.column_name,dataType:f.data_type,table:l||"",database:n||"",connectionId:t||"",operation:"in",sql:s.getDefaultFilterSql(f.data_type,C,x)}),D)}}return{assignFilter:p}}function Ee(){const t=s.useDashboardStore(i=>i.dashboard.filters),a=s.useEditorStore(i=>i.frame),{setFrame:n}=s.useEditorActions(),{columns:r}=s.useEditorAside(),{assignFilter:l}=qt();function o(i){const d=l(i,"frame");n({...a,filterId:d})}const c=t==null?void 0:t.find(i=>i.id===a.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Card Filter"}),e.jsx(gr,{onSelect:o,columns:r}),c&&e.jsx(s.FilterHeader,{filter:c})]})}function gr({columns:t,onSelect:a}){var c;const[n,r]=ye.useState(!1),[l,o]=ye.useState("");return e.jsxs(s.Popover,{open:n,onOpenChange:r,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":n,className:"w-full justify-between font-normal",children:[l?(c=t.find(i=>i.column_name===l))==null?void 0:c.column_name:"Select Column...",e.jsx(s.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No framework found."}),e.jsx(s.CommandGroup,{children:t==null?void 0:t.map(i=>e.jsxs(s.CommandItem,{value:i.column_name,onSelect:d=>{const m=d===l?"":d;o(m),r(!1),m&&a(i)},children:[i.column_name,e.jsx(s.CheckIcon,{className:s.cn("ml-auto h-4 w-4",l===i.column_name?"opacity-100":"opacity-0")})]},i.column_name))})]})]})})]})}function jr({cardType:t}){return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} Chart`}),e.jsxs(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT x-axis, y-axis-1, y-axis-2,... FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"x-axis - category"}),e.jsx("li",{children:"y-axis1 - number"}),e.jsx("li",{children:"y-axis2 - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:a=>a.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/bar-line-area",children:"example"})," in the docs."]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsx(s.AccordionContent,{className:"space-y-4 px-2",children:e.jsx(_t,{})})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ke(){const t=s.useEditorStore(i=>i.card.type),a=s.useEditorStore(i=>i.isDevMode),n=s.useEditorStore(i=>i.card.preferences),{setCardPreferences:r}=s.useEditorStore(i=>i.actions);function l(i){var h;const d={datalabels:{display:i!=="none",align:i,anchor:i,clamp:!0}},m={...n,chartOptions:{...n==null?void 0:n.chartOptions,plugins:{...(h=n==null?void 0:n.chartOptions)==null?void 0:h.plugins,...d}}};r(m)}function o(){var i,d,m;return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(s.Select,{value:((m=(d=(i=n==null?void 0:n.chartOptions)==null?void 0:i.plugins)==null?void 0:d.datalabels)==null?void 0:m.anchor)||"none",onValueChange:l,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Data Labels"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"start",children:"Start"}),e.jsx(s.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"end",children:"End"})]})})]})]})}function c(){var i,d,m;return e.jsx("div",{children:e.jsxs(s.Select,{value:((m=(d=(i=n==null?void 0:n.chartOptions)==null?void 0:i.plugins)==null?void 0:d.datalabels)==null?void 0:m.anchor)||"none",onValueChange:l,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"start",children:"Start"}),e.jsx(s.SelectItem,{className:"text-xs",value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"end",children:"End"})]})})]})})}return e.jsx(e.Fragment,{children:a?o():c()})}function yr({cardType:t}){function a(n){return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:n==="bubble"?"SELECT label, [group], x-axis, y-axis, radius FROM table":"SELECT label, [group], x-axis, y-axis FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"label - category"}),e.jsx("li",{children:"group (optional) - category"}),e.jsx("li",{children:"x-axis - number"}),e.jsx("li",{children:"y-axis - number"}),n==="bubble"&&e.jsx("li",{children:"radius - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:o=>o.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/bubble",children:"example"})," in the docs."]})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} Chart`}),e.jsx(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:a(t)})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsx(s.AccordionContent,{className:"px-2",children:e.jsx(Ke,{})})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}const vr={datasets:[{type:"line",label:"Sum_sales",data:[]},{type:"bar",label:"Sum_profit",data:[]}]},br={datasets:[{fill:"origin",type:"line",label:"Sum_sales",data:[]}]};function Nr(){const t=s.useEditorStore(a=>a.card.type);return e.jsxs("div",{className:"space-y-6 p-6",children:[e.jsx(s.Accordion,{className:" ",type:"single",collapsible:!0,children:Cr(t)}),!["kpi"].includes(t)&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(Ke,{}),e.jsx(we,{}),e.jsx(Ee,{})]})]})}function Cr(t){if(["line","bar"].includes(t))return e.jsxs(e.Fragment,{children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsx("p",{children:"Chart Query"})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT x-axis, y-axis-1, y-axis-2, y-axis-3,... FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"X-axis - Category (ex. city)"}),e.jsx("li",{children:"Y-axis - Number (ex. population)"})]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:"Mixed Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-2 text-xs",children:[e.jsxs("p",{children:["Click ",e.jsx("b",{children:"Customize"})," and change the `type` property in the dataset."]}),e.jsx("pre",{children:JSON.stringify(vr,null,2)})]})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:"Area Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-2 text-xs",children:[e.jsxs("p",{children:["Click ",e.jsx("b",{children:"Customize"})," and add `fill` property to the dataset. See",e.jsx("a",{className:"mx-1 text-blue-500 hover:underline",href:"https://www.chartjs.org/docs/latest/samples/area/line-boundaries.html",target:"_blank",children:"docs"}),"for more options."]}),e.jsx("pre",{children:JSON.stringify(br,null,2)})]})]})]});if(t==="bubble")return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:t})," chart query?"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, x-axis, y-axis, radius FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Bubble Group (opt) - Category (ex. state)"}),e.jsx("li",{children:"X-axis - Number (ex. avg. age)"}),e.jsx("li",{children:"Y-axis - Number (ex. avg. weight)"}),e.jsx("li",{children:"Radius - Number (ex. population)"})]})]})]})});if(t==="scatter")return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:t})," chart query?"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, x-axis, y-axis FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Group (opt) - Category (ex. state)"}),e.jsx("li",{children:"X-axis - Number (ex. avg. age)"}),e.jsx("li",{children:"Y-axis - Number (ex. avg. weight)"})]})]})]})});if(["pie","doughnut","polarArea","funnel"].includes(t))return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:[e.jsx("span",{children:t==="polarArea"?"Polar":t.charAt(0).toUpperCase()+t.slice(1)})," ","chart query"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, measure FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Measure - Number (ex. population)"})]})]})]})})}function Sr(){return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{type:"single",collapsible:!0,children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"",children:"KPI Query"}),e.jsxs(s.AccordionContent,{className:"font-mono",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs",children:"SELECT current, previous FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"current - number"}),e.jsx("li",{children:"previous - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/kpi",children:"example"})," in the docs."]})]})]})}),e.jsx("section",{className:"",children:e.jsx(s.FormatNumberSetting,{})}),e.jsx("div",{}),e.jsx("div",{})]})}function ns({children:t,summary:a,open:n,className:r}){return e.jsxs("details",{open:n,className:s.cn("space-y-4 [&_summary>svg]:open:-rotate-180",r),children:[e.jsxs("summary",{className:"flex cursor-pointer list-none items-center justify-between gap-4",children:[e.jsx(s.Label,{children:a}),e.jsx(s.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:t})]})}const wr=[{label:"Mercator",value:"mercator"},{label:"Azimuthal Equidistant",value:"azimuthalEquidistant"},{label:"Gnomonic",value:"gnomonic"},{label:"Orthographic",value:"orthographic"},{label:"Stereographic",value:"stereographic"},{label:"Equal Earth",value:"equalEarth"},{label:"Albers",value:"albers"},{label:"Albers USA",value:"albersUsa"},{label:"Conic Conformal",value:"conicConformal"},{label:"Conic Equal Area",value:"conicEqualArea"},{label:"Conic Equidistant",value:"conicEquidistant"},{label:"Equirectangular",value:"equirectangular"},{label:"Transverse Mercator",value:"transverseMercator"},{label:"Natural Earth 1",value:"naturalEarth1"}],Tr=["blues","brBG","buGn","buPu","cividis","cool","cubehelixDefault","gnBu","greens","greys","inferno","magma","orRd","oranges","pRGn","piYG","plasma","puBu","puBuGn","puOr","puRd","purples","rainbow","rdBu","rdGy","rdPu","rdYlBu","rdYlGn","reds","sinebow","spectral","turbo","viridis","warm","ylGn","ylGnBu","ylOrBr","ylOrRd"],kr=Tr.map(t=>({label:t,value:t}));function Dr(){var d,m,h,u,p,f,j,x,T,g,b,C,O,S,E,N,D,w;const t=s.useEditorStore(M=>M.card),a=(m=(d=t==null?void 0:t.preferences)==null?void 0:d.mapVisualOptions)==null?void 0:m.topoJsonUrl,[n,r]=A.useState(((u=(h=t==null?void 0:t.preferences)==null?void 0:h.mapVisualOptions)==null?void 0:u.customTopoJsonUrl)||""),{setCardPreferences:l}=s.useEditorActions(),{data:o}=s.useTopoJson(t),c=Object.keys((o==null?void 0:o.objects)||{}).map(M=>({label:M,value:M}))||[],i=[{label:"US States 10m",value:"https://unpkg.com/us-atlas/states-10m.json"},{label:"Countries 50m",value:"https://unpkg.com/world-atlas/countries-50m.json"},{label:"Custom",value:"custom"}];return e.jsxs("div",{className:"p-6",children:[e.jsx(ns,{summary:"Map Chart",children:e.jsx("p",{className:"rounded-md bg-muted p-2 font-mono text-xs",children:"SELECT country, value FROM table"})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ns,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(ce,{className:"h-9",options:i,onValueChange:M=>{console.log("value",M),l({...t.preferences,mapVisualOptions:{topoJsonUrl:M}})},value:a||"",label:"Select Map"}),a==="custom"&&e.jsx(s.Input,{onChange:M=>{r(M.target.value)},onBlur:()=>{var M;console.log("onBlur",n),l({...t.preferences,mapVisualOptions:{...(M=t.preferences)==null?void 0:M.mapVisualOptions,customTopoJsonUrl:n}})},value:n,className:`h-9 w-full ${s.removeRing}`,placeholder:"Custom URL"}),o&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Map Object"}),e.jsx(ce,{className:"h-9",options:c,value:((f=(p=t.preferences)==null?void 0:p.mapVisualOptions)==null?void 0:f.objectKey)||"",label:"Map Object",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,objectKey:M}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Projection"}),e.jsx(ce,{className:"h-9",options:wr,value:((x=(j=t.preferences)==null?void 0:j.mapVisualOptions)==null?void 0:x.projection)||"",label:"Projection",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projection:M}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Colors"}),e.jsx(ce,{className:"h-9",options:kr,value:((g=(T=t.preferences)==null?void 0:T.mapVisualOptions)==null?void 0:g.colorScale)||"",label:"Select Color Scale",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,colorScale:M}})}})]})]})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ns,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:.1,value:((C=(b=t.preferences)==null?void 0:b.mapVisualOptions)==null?void 0:C.projectionScale)||1,onChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionScale:Number(M.target.value)}})}}),e.jsx(s.Label,{children:"X"}),e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((E=(S=(O=t.preferences)==null?void 0:O.mapVisualOptions)==null?void 0:S.projectionOffset)==null?void 0:E[0])||0,onChange:M=>{var k,R,I,v;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionOffset:[Number(M.target.value),((v=(I=(R=t.preferences)==null?void 0:R.mapVisualOptions)==null?void 0:I.projectionOffset)==null?void 0:v[1])||0]}})}}),e.jsx(s.Label,{children:"Y"}),e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((w=(D=(N=t.preferences)==null?void 0:N.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:w[1])||0,onChange:M=>{var k,R,I,v;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionOffset:[((v=(I=(R=t.preferences)==null?void 0:R.mapVisualOptions)==null?void 0:I.projectionOffset)==null?void 0:v[0])||0,Number(M.target.value)]}})}})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(we,{})]})}function Er(){var r;const t=s.useEditorStore(l=>l.card.preferences),{setCardPreferences:a}=s.useEditorStore(l=>l.actions);function n(l){a({...t,textVisualOptions:{...t==null?void 0:t.textVisualOptions,isDynamicText:l==="dynamic"}})}return e.jsxs(s.RadioGroup,{value:(r=t==null?void 0:t.textVisualOptions)!=null&&r.isDynamicText?"dynamic":"static",onValueChange:n,className:"flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"static",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Static"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function Be({cardType:t,docContent:a,suffix:n="Chart"}){let r="pie-donut-polar";return t==="funnel"&&(r="funnel"),e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} ${n}`}),e.jsx(s.AccordionContent,{children:a||e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT label, measure FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"label - category"}),e.jsx("li",{children:"measure - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:l=>l.stopPropagation(),className:"text-blue-700",href:`https://docs.semaphor.cloud/docs/charts/${r}`,children:"example"})," in the docs."]})]})})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[t!=="text"&&e.jsx(Ke,{}),t==="text"&&e.jsx(Er,{})]})]}),!["range","text"].includes(t)&&e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ir(){const t=s.useEditorStore(r=>{var l,o;return(o=(l=r==null?void 0:r.card)==null?void 0:l.preferences)==null?void 0:o.sortChart}),{setSortChart:a}=s.useEditorActions();function n(){return e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Sort"}),e.jsx(ce,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"ASC",value:"asc"},{label:"DESC",value:"desc"}],value:t||"none",onValueChange:r=>{a(r)}})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:"Stacked Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT x-axis, stack-by, y-axis FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"x-axis - category"}),e.jsx("li",{children:"stack by - category"}),e.jsx("li",{children:"y-axis - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:r=>r.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/stacked",children:"example"})," in the docs."]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[n(),e.jsx(_t,{})]})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ar({cardType:t="table"}){console.log("cardType",t);const a=s.useEditorStore(l=>{var o;return(o=l.card)==null?void 0:o.preferences}),n=s.useEditorStore(l=>l.actions.setCardPreferences),r=l=>{n({...a,allowDownload:l})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Checkbox,{onCheckedChange:r,id:"allow-download"}),e.jsx("label",{htmlFor:"allow-download",className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:"Allow Download"})]})]})}const Mr=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT dimension, cohort, metric FROM table"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/tornado",children:"example"})," in the docs."]})]}),Lr=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT y-axis, label, range-from, range-to, range-value FROM table"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/range",children:"example"})," in the docs."]})]}),Or=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT text FROM table limit 1"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/text",children:"example"})," in the docs."]})]});function Rr(){const t=s.useEditorStore(n=>n.card.type);function a(n){switch(n){case"stackedBar":case"stackedLine":return e.jsx(Ir,{});case"tornado":case"pyramid":return e.jsx(Be,{docContent:Mr,cardType:n});case"kpi":return e.jsx(Sr,{});case"line":case"bar":return e.jsx(jr,{cardType:n});case"range":return e.jsx(Be,{docContent:Lr,cardType:n});case"pie":case"doughnut":case"polarArea":case"radar":case"funnel":return e.jsx(Be,{cardType:n});case"bubble":case"scatter":return e.jsx(yr,{cardType:n});case"table":return e.jsx(Ar,{cardType:n});case"text":return e.jsx(Be,{docContent:Or,cardType:n,suffix:"Visual"});case"map":return e.jsx(Dr,{});case"custom":return e.jsx(xr,{});default:return e.jsx(Nr,{})}}return e.jsxs(s.ScrollArea,{className:"h-full w-full text-sm",children:[a(t),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function $t({label:t,column:a,functions:n,onFunctionChange:r}){let l=n[0];"aggregate"in a?l=n.find(c=>c.value===a.aggregate)||n[0]:"granularity"in a&&(l=n.find(c=>c.value===a.granularity)||n[0]);function o(c){r(c)}return e.jsx(ce,{className:"flex h-8 w-8 justify-center overflow-clip rounded-none border-none bg-muted px-0 [&>svg]:hidden",icon:l.logo,options:n,value:l.value,onValueChange:c=>o(c),label:t,showPlaceholderLabel:!1})}const Br=[{label:"Sum",value:"SUM",logo:e.jsx(rt,{className:"size-3.5"})},{label:"Count",value:"COUNT",logo:e.jsx(fs,{className:"size-3.5"})},{label:"Unique",value:"DISTINCT",logo:e.jsx(Ln,{className:"size-3.5"})},{label:"Average",value:"AVG",logo:e.jsx(mn,{className:"size-3.5"})},{label:"Min",value:"MIN",logo:e.jsx(s.ArrowDown,{className:"size-3.5"})},{label:"Max",value:"MAX",logo:e.jsx(s.ArrowUp,{className:"size-3.5"})}];function Fr({column:t}){const{updateMetricColumnOld:a}=s.useEditorActions();let n=Br;s.isTextDataType(t.type)&&(n=n.filter(l=>["COUNT","DISTINCT"].includes(l.value))),s.isNumberDataType(t.type)&&(n=n.filter(l=>!["DISTINCT"].includes(l.value)));function r(l){const o={...t,aggregate:l,label:Le(t.name,l)};a(t.id,o)}return e.jsx($t,{column:t,functions:n,onFunctionChange:l=>r(l),label:"Aggregation"})}const zr=[{label:"Day (D)",value:"day",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"D"})},{label:"Week (W)",value:"week",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"W"})},{label:"Month (M)",value:"month",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"M"})},{label:"Year (Y)",value:"year",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"Y"})}];function Pr({column:t}){const{updateGroupByColumnOld:a}=s.useEditorActions();function n(r){const l={...t,granularity:r,label:De(t.name,r)};a(t.id,l)}return e.jsx($t,{column:t,functions:zr,onFunctionChange:r=>n(r),label:"Granularity"})}function _r({col:t,onChange:a,droppableId:n}){const{columns:r}=s.useDataColumns(),l=A.useMemo(()=>{let i=[];(n===be||n===Ne||n===Ce)&&(i=r);const d=i==null?void 0:i.filter(m=>m.column_name!==t.name);return d==null?void 0:d.map(m=>({id:m.column_name,value:m.column_name}))},[r,n,t.name]),o=[{id:t.name,value:t.name}];function c(i){const d=r==null?void 0:r.find(m=>{var h;return m.column_name===((h=i[0])==null?void 0:h.id)});d&&a(d)}return l!=null&&l.length?e.jsx(s.ComboBox2,{multiple:!1,options:l,selectedOptions:o,onChange:c,selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"border-none bg-muted pl-0 pr-8"}):e.jsx("input",{disabled:!0,type:"text",className:"overflow-hidden truncate text-ellipsis whitespace-nowrap bg-muted text-sm",value:t.name})}function Gt({column:t}){const a=s.useEditorStore(d=>d.card.type),n=s.useEditorStore(d=>{var m,h;return(h=(m=d.card)==null?void 0:m.config)==null?void 0:h.metricColumns}),r=(n==null?void 0:n.map(d=>d.label))||[],l=r==null?void 0:r.find(d=>d===t.label||d===t.name),o=l?r.indexOf(l):-1;function c(){return e.jsx("div",{className:"pr-1",children:l&&e.jsx(Ut,{idx:o,col:l,children:e.jsx("div",{className:"w-[130px] text-xs text-muted-foreground",children:"Display Series As"})})})}function i(){return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Data Labels"}),e.jsx(Ke,{})]})}return e.jsxs("div",{className:"space-y-1",children:[["stackedBar","stackedLine","bar","line"].includes(a)&&c(),["pie","doughnut"].includes(a)&&i()]})}function Vr({column:t,type:a,onRemoveColumn:n,droppableId:r}){const[l,o]=A.useState(!1),{updateGroupByColumnOld:c,updateMetricColumnOld:i,updatePivotByColumnOld:d}=s.useEditorActions(),{attributes:m,listeners:h,setNodeRef:u,transform:p,transition:f,isDragging:j}=s.useSortable({id:t.id,data:{type:a}}),x=p?{opacity:j?.5:void 0,transform:s.CSS.Translate.toString(p),transition:f}:void 0,T=s.isTextDataType(t.type);function g(E){const N=s.isNumberDataType(E.data_type),D=s.isTextDataType(E.data_type),w=s.isDateDataType(E.data_type),M=N?"number":D?"string":w?"date":"string";if(r===be){if(D){const k={id:t.id,name:E.column_name,type:M,role:"groupby"};c(t.id,k)}if(w){const k={id:t.id,name:E.column_name,type:M,role:"groupby",label:De(E.column_name,"month"),granularity:"month"};c(t.id,k)}}if(r===Ce){if(D){const k={id:t.id,name:E.column_name,type:M,role:"groupby"};d(t.id,k)}if(w){const k={id:t.id,name:E.column_name,type:M,label:De(E.column_name,"month"),granularity:"month",role:"groupby"};d(t.id,k)}}if(r===Ne){const k={id:t.id,name:E.column_name,type:M,role:"metric",label:Le(E.column_name,"SUM"),aggregate:N?"SUM":"COUNT"};i(t.id,k)}}function b(){if(r===Ne)return e.jsx("div",{"data-setting-container":"",className:"mt-1.5 max-h-32 overflow-y-auto rounded-md border border-border p-3 text-sm",children:e.jsx(Gt,{column:t})})}function C(){const E=s.isDateDataType(t.type);if(r===Ne)return e.jsx(Fr,{column:t});if((r===be||r===Ce)&&E)return e.jsx(Pr,{column:t})}function O(){return e.jsx("div",{className:s.cn("grow",{"pl-3":(r===be||r===Ce)&&T}),children:e.jsx(_r,{col:t,onChange:g,droppableId:r})})}function S(){return e.jsx("div",{"data-setting-actions":"",className:"flex h-full items-center gap-2",children:e.jsx("button",{className:"h-full pl-2",onClick:()=>o(!l),children:l?e.jsx(ea,{className:s.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")}):e.jsx(tt,{className:s.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")})})})}return e.jsx("div",{...h,...m,ref:u,style:x,children:e.jsxs("div",{className:"group flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex h-8 grow items-center justify-between rounded-md bg-muted",children:[S(),C(),O()]}),l&&b()]}),e.jsx("div",{className:s.cn("opacity-0 transition-opacity duration-200 group-hover:opacity-100",{hidden:l}),children:e.jsx("button",{onClick:()=>n(t.id),children:e.jsx(s.X,{className:"size-3 cursor-pointer text-muted-foreground"})})})]})})}function as({id:t,label:a,columns:n,previewColumns:r,sortableType:l,onRemoveColumn:o}){var x,T;const{setNodeRef:c,isOver:i,over:d}=s.useDroppable({id:t}),m=s.useDndStore(g=>g.activeId),h=s.useEditorStore(g=>{var b;return(b=g.card)==null?void 0:b.type}),u=i||((T=(x=d==null?void 0:d.data)==null?void 0:x.current)==null?void 0:T.type)===l,p=m?r:n,f=l===_e?"Rows":l===Ve?"Pivot Columns":"Columns";function j(){if(!p||p.length===0)return e.jsx("div",{className:"mr-5 flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground/80 bg-background px-2 py-1 text-sm text-muted-foreground",children:e.jsx("span",{className:"invisible text-muted-foreground/70 group-hover:visible",children:"Drag here"})})}return e.jsx(s.SortableContext,{items:n==null?void 0:n.map(g=>g.id),children:e.jsxs("div",{className:s.cn("group space-y-2 rounded-md p-4",u&&"border border-muted-foreground/20"),ref:c,children:[e.jsxs("div",{className:"flex items-center justify-between pr-5",children:[e.jsx(s.Label,{className:"text-sm",children:a}),h!=="table"&&e.jsx("span",{className:"text-xs text-muted-foreground/50",children:f})]}),j(),e.jsx("div",{className:"space-y-2",children:p==null?void 0:p.map(g=>g.id===kt.id?e.jsx(Ur,{},g.id):e.jsx(Vr,{droppableId:t,type:l,column:g,onRemoveColumn:o},g.id))})]})})}function Ur(){return e.jsx("div",{className:"mr-5 flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground bg-background px-2 py-1 text-sm",children:e.jsx("span",{className:"text-muted-foreground/70",children:"Drop here"})})}function Ht({children:t,id:a,type:n}){const{attributes:r,listeners:l,setNodeRef:o,transform:c,transition:i,isDragging:d}=s.useSortable({id:a,data:{type:n}}),m=c?{opacity:d?.5:void 0,transform:s.CSS.Translate.toString(c),transition:i}:void 0;return e.jsx("div",{...l,...r,ref:o,style:m,children:t})}function qr(){const t=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.orderBy})||[],a=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.groupByColumns})||[],n=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.metricColumns})||[],{addOrderByColumn:r,removeOrderByColumn:l,updateOrderByColumn:o}=s.useEditorActions(),c=[...a,...n];if(c.length===0)return e.jsx(e.Fragment,{});const i=e.jsx(fn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),d=e.jsx(un,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),m=e.jsx(pn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),h=e.jsx(dn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),u=c==null?void 0:c.filter(g=>!t.some(b=>b.columnId===g.id)).map(g=>({id:g.id,value:g.label||g.name}));function p(g,b){const C=f(g);return C?C.role==="metric"?b==="asc"?m:h:b==="asc"?i:d:null}function f(g){return c.find(b=>b.id===g)}function j(g){const b=t.find(C=>C.columnId===g);if(b){const C={...b,direction:b.direction==="asc"?"desc":"asc"};o(C)}}const x=g=>{r({columnId:g.id,direction:"asc"})};function T(g){const b=t.find(C=>C.columnId===g);b&&l(b)}return e.jsx(s.SortableContext,{items:t.map(g=>Ie(g.columnId)),children:e.jsxs("div",{className:"space-y-2 rounded-md p-4 text-sm",children:[e.jsx(s.Label,{className:"text-sm",children:"Sort By"}),u.length>0&&e.jsx("div",{className:"mr-5",children:e.jsx(s.ComboBox2,{multiple:!1,options:u,selectedOptions:[],onChange:g=>x(g[0]),selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"h-8 bg-muted border-none"})}),t.map(g=>{var b,C;return e.jsx(Ht,{id:Ie(g.columnId),type:Ns,children:e.jsx("div",{className:"group flex h-8 items-center gap-2 rounded-sm p-0",children:e.jsxs("div",{className:"flex h-full grow items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex h-full grow items-center justify-between gap-2 rounded-sm bg-muted px-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(tt,{className:"size-3 cursor-move text-muted-foreground hover:text-foreground"}),e.jsx(Es,{className:"w-[175px] text-sm",children:((b=f(g.columnId))==null?void 0:b.label)||((C=f(g.columnId))==null?void 0:C.name)})]}),e.jsx("button",{title:g.direction==="asc"?"Ascending":"Descending",onClick:()=>j(g.columnId),className:"rounded-sm p-0",children:p(g.columnId,g.direction)})]}),e.jsx("div",{children:e.jsx("button",{onClick:()=>T(g.columnId),className:"rounded-sm p-0",children:e.jsx(s.X,{className:"size-3 text-muted-foreground opacity-0 transition-opacity duration-200 hover:text-foreground group-hover:opacity-100"})})})]})},g.columnId)},Ie(g.columnId))})]})})}function $r(){const t=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.groupByColumns)||[]}),a=s.useEditorStore(T=>T.card.type),n=s.useDndStore(T=>T.previewGroupByColumns),r=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.metricColumns)||[]}),l=s.useDndStore(T=>T.previewMetricColumns),o=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.pivotColumns)||[]}),c=s.useDndStore(T=>T.previewPivotColumns),{removeColumnFromGroupBy:i,removeColumnFromMetricColumns:d,removeColumnFromPivotBy:m}=s.useEditorActions();function h(T){i(T)}function u(T){d(T)}function p(T){m(T)}let f="Group By",j="Metrics",x="Stack By";return a==="table"&&(f="Rows",j="Metrics",x="Columns"),e.jsxs("div",{className:"flex h-full w-full flex-col gap-3 py-4",children:[e.jsx(Bt,{iconClassName:"size-[19px]"}),e.jsxs(s.CustomScrollArea,{className:"flex min-h-0 grow basis-0 flex-col gap-4 overflow-y-auto",children:[e.jsxs("div",{className:"flex flex-col gap-4 p-4",children:[e.jsx(as,{onRemoveColumn:h,sortableType:_e,id:be,label:f,columns:t,previewColumns:n}),e.jsx(as,{onRemoveColumn:p,sortableType:Ve,id:Ce,label:x,columns:o,previewColumns:c}),e.jsx(as,{onRemoveColumn:u,sortableType:ls,id:Ne,label:j,columns:r,previewColumns:l}),e.jsx(qr,{})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function Is({children:t,column:a,id:n,sortableType:r,className:l}){const[o,c]=A.useState(!1),{removeEditorFilter:i,clearEditorFilter:d}=s.useEditorActions(),m=s.useEditorStore(u=>{var p,f;return(f=(p=u.card.queryConfig)==null?void 0:p.filters)==null?void 0:f.some(j=>j.filterColumnName===a.name)});function h(u){i(u)}return e.jsx(Ht,{id:n,type:r,children:e.jsxs("div",{className:s.cn("group space-y-1 rounded-md bg-muted px-3 py-2",{"border-border":o}),children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-sm",children:a.name}),m&&e.jsx("button",{onClick:()=>d(a),children:e.jsx(s.FilterX,{className:"size-3 cursor-pointer items-center justify-center text-muted-foreground hover:text-foreground"})})]}),e.jsx("div",{"data-setting-actions":"",className:"invisible flex h-full items-center gap-2 group-hover:visible",children:e.jsx("button",{onClick:()=>h(a),children:e.jsx(s.X,{className:"size-3.5 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground"})})})]}),e.jsxs("div",{className:s.cn("group flex items-center rounded-t-md text-sm",!o&&"rounded-b-md",o&&"border border-border"),children:[e.jsx("div",{children:e.jsx(s.GripVertical,{className:"mr-2 size-3.5 cursor-move items-center justify-center p-0 text-muted-foreground/60 hover:text-foreground"})}),e.jsx("div",{className:s.cn("grow cursor-pointer overflow-hidden truncate text-ellipsis whitespace-nowrap",l),children:t})]})]})})}function Gr({column:t,sortableType:a}){const n=s.useDashboardStore(C=>C.themeStyle),r=s.useEditorStore(C=>{var O,S;return(S=(O=C.card)==null?void 0:O.queryConfig)==null?void 0:S.filters}),l=s.useEditorStore(C=>{var O,S;return(S=(O=C.card.queryConfig)==null?void 0:O.filterColumns)==null?void 0:S.find(E=>E.name===t.name)}),{selectedConnectionId:o,selectedDatabaseName:c,selectedTableName:i}=s.useEditorAside(),d=A.useMemo(()=>{var C;return(C=r==null?void 0:r.find(O=>O.filterColumnName===t.name))==null?void 0:C.filterValue},[r,t.name]),m=A.useMemo(()=>Array.isArray(d)?d.map(C=>({id:C,value:C})):[],[d]),{updateEditorFilterValue:h}=s.useEditorActions(),u={column:t.name,sql:t.sql,id:t.name,connectionId:o,dataType:t.type},{data:p,isLoading:f,isFetching:j,isError:x}=s.useFilterQuery(u),T=A.useMemo(()=>{var O;return u.column?(O=p==null?void 0:p.records)==null?void 0:O.map(S=>{let E=u.column.split(".").slice(-1)[0].replace(/"/g,"");return S[E]===void 0&&(E=E.toLowerCase()),{id:S[E],value:S[E]}}):[]},[p,u.column]);function g(C){const O=C.map(S=>S.id);h(t.name,O)}function b(){return f||j?e.jsx(s.Skeleton,{style:{borderRadius:n==null?void 0:n.controlInputRadius},className:"h-8 min-w-60 bg-foreground/10"}):x?e.jsx("div",{className:"flex h-8 min-w-60 items-center bg-muted px-2 text-sm",children:"Error"}):e.jsx(s.ComboBox2,{renderFilterModeButton:()=>e.jsx(Hr,{columnName:t.name}),showNegativeIndicator:(l==null?void 0:l.mode)==="exclude",options:T,onChange:g,selectedOptions:m,selectAllChecked:!1,onSelectAllCheckedChange:()=>{}})}return e.jsx(Is,{className:"w-60",id:t.id,sortableType:a,column:t,children:b()})}function Hr({columnName:t}){const{setEditorFilterMode:a}=s.useEditorActions(),n=s.useEditorStore(l=>{var o,c;return(c=(o=l.card.queryConfig)==null?void 0:o.filterColumns)==null?void 0:c.find(i=>i.name===t)});function r(){(n==null?void 0:n.mode)==="exclude"?a(t,"include"):a(t,"exclude")}return e.jsx(s.Button,{onClick:r,variant:"secondary",size:"sm",className:s.cn("h-5 text-xs hover:underline",{}),children:s.titleCase((n==null?void 0:n.mode)==="exclude"?"exclude":"include")})}function Jr({column:t,sortableType:a}){const[n,r]=A.useState(!1),{selectedConnectionId:l,selectedDatabaseName:o,selectedTableName:c}=s.useEditorAside(),i={column:t.name,sql:t.sql,id:t.name,connectionId:l,title:"",dataType:t.type},{isLoading:d,isFetching:m,isError:h,data:u}=s.useFilterQuery(i),p=u==null?void 0:u.records,{dateRange:f,setDateRange:j,initialDateRange:x}=s.useDateRangeFromRecords(p),{updateEditorFilterValue:T}=s.useEditorActions();function g(O){var E,N;const S={gte:(E=O==null?void 0:O.from)==null?void 0:E.toISOString(),lte:(N=O==null?void 0:O.to)==null?void 0:N.toISOString()};T(t.name,S)}function b(){T(t.name,null,!0),j(x)}function C(){return d||m?e.jsx("div",{children:e.jsx(s.Skeleton,{className:"h-8 w-72 bg-foreground/10"})}):h?e.jsx("div",{className:"h-8 w-72 bg-muted px-2",children:"Error"}):p?e.jsx(s.DateRangePopover,{title:i.title,open:n,inputClassName:"h-8",setOpen:r,dateRange:f,setDateRange:j,onApply:g,onClear:b}):e.jsx(s.Label,{className:"flex h-8 w-72 items-center bg-muted px-2",children:"No Data"})}return e.jsx(Is,{className:"w-72",id:t.id,sortableType:a,column:t,children:C()})}function Wr({id:t,label:a,columns:n,previewColumns:r,sortableType:l}){var j,x;const{setNodeRef:o,isOver:c,over:i}=s.useDroppable({id:t}),d=s.useDndStore(T=>T.activeId),m={},h=c||((x=(j=i==null?void 0:i.data)==null?void 0:j.current)==null?void 0:x.type)===l;function u(){if(!f||f.length===0)return e.jsx("div",{className:"flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground/80 bg-background px-2 py-1 text-sm text-muted-foreground",children:e.jsx("span",{className:"invisible text-muted-foreground/70 group-hover:visible",children:"Drag here"})})}function p(T){const g=s.isDateDataType(T.type);return T.id===kt.id?e.jsx(Is,{id:T.id,sortableType:l,column:T,children:e.jsx(Yr,{})},T.id):g?e.jsx(Jr,{droppableId:t,sortableType:l,column:T},T.id):e.jsx(Gr,{droppableId:t,sortableType:l,column:T},T.id)}const f=d?r:n;return e.jsx(s.SortableContext,{items:n==null?void 0:n.map(T=>T.id),children:e.jsxs("div",{className:s.cn("group space-y-2 rounded-md border-muted-foreground/50 p-4",h&&"border border-muted-foreground/20"),ref:o,style:m,children:[e.jsx(s.Label,{className:"text-sm",children:a}),u(),e.jsx("div",{className:"flex flex-wrap items-center gap-4",children:f==null?void 0:f.map(T=>p(T))})]})})}function Yr(){return e.jsx("div",{className:"flex h-8 w-60 items-center space-x-2 rounded-md border border-dashed border-muted-foreground bg-background px-2 py-1 text-sm text-muted-foreground",children:"Drop here"})}function Kr(){const t=s.useEditorStore(l=>{var o;return((o=l.card.queryConfig)==null?void 0:o.filterColumns)||[]}),a=s.useDndStore(l=>l.previewFilterColumns),{removeColumnFromFilterColumns:n}=s.useEditorActions();function r(l){n(l)}return e.jsx("section",{className:"size-full px-4 pb-4",children:e.jsx(Wr,{onRemoveColumn:r,sortableType:is,id:os,label:"Filters",columns:t,previewColumns:a})})}function Qr(){const t=s.useEditorStore(o=>o.card),a=s.useEditorStore(o=>o.showAIDialog),{setShowAIDialog:n}=s.useEditorActions(),{data:r,errorMessage:l}=s.useDashboardCardQuery(t);return e.jsxs("section",{className:"flex justify-between gap-2 p-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.EditorClearButton,{}),!a&&e.jsxs(s.Button,{onClick:()=>n(!0),variant:"secondary",size:"sm",children:[e.jsx(Jn,{className:"mr-2 h-4 w-4"}),"Assistant"]}),e.jsx(At,{sql:r==null?void 0:r.sql,error:l})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"rounded-full bg-green-300 px-3 py-1 text-xs font-medium text-black",children:"Preview"}),e.jsx(s.EditorCancelButton,{}),e.jsx(s.EditorAcceptButton,{variant:"default"})]})]})}function Xr(){const t=A.useRef(null),a=A.useRef(null),n=s.useEditorStore(i=>i.isShowingVisual),r=s.useEditorStore(i=>i.showAIDialog),l=s.useEditorStore(i=>i.isDevMode);A.useEffect(()=>{var i,d,m,h;r?((i=t.current)==null||i.resize(0),(d=a.current)==null||d.resize(0)):((m=t.current)==null||m.resize(25),(h=a.current)==null||h.resize(25))},[r]);function o(){return e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(Qa,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:n&&!r?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:t,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(Rr,{})})}),!r&&e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(Re,{})})]}):e.jsx(Re,{})})]})}function c(){return e.jsx(e.Fragment,{children:e.jsx("div",{className:"grow","data-biz-mode-container":"",children:n?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:a,defaultSize:25,maxSize:30,children:e.jsx($r,{})})}),e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{defaultSize:75,maxSize:100,children:e.jsxs(s.ResizablePanelGroup,{direction:"vertical",children:[e.jsxs(s.ResizablePanel,{className:"flex min-h-fit flex-col",minSize:10,defaultSize:20,children:[e.jsx(Qr,{}),e.jsx(Kr,{})]}),e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:60,defaultSize:80,children:e.jsx(Re,{})})]})})]}):e.jsx(Re,{})})})}return e.jsx(s.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:l?o():c()})}function Zr(t){const a=s.useAIInteractionStore(B=>B.currentAIContext),[n,r]=A.useState(""),[l,o]=A.useState(null),{authToken:c}=s.useSemaphorContext(),i=s.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.sql}),d=s.useEditorStore(B=>B.isDevMode),m=s.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.python}),{setCardPython:h,setCardSql:u,setRunSql:p,switchCardType:f,setQueryConfig:j}=s.useEditorActions(),x=s.useEditorStore(B=>B.currentAssitantScope)||[],T=s.useEditorStore(B=>B.userInputForAI),g=s.useEditorStore(B=>B.triggerAIRun),b=s.useEditorStore(B=>B.card),C=s.useEditorStore(B=>B.selectedDatabaseName),O=s.useEditorStore(B=>B.selectedTableName),S=s.useEditorStore(B=>B.selectedConnectionId),E=s.useEditorStore(B=>B.selectedSchemaName),N=s.useDashboardStore(B=>B.dashboard.aiScopeTables),{errorMessage:D,refetch:w,data:M}=s.useDashboardCardQuery(b),{setTriggerAIRun:k,setCurrentAssitantScope:R,setCardConfig:I}=s.useEditorActions(),{selectedDatamodelName:v,connectionType:y,selectedDatamodelId:L}=s.useEditorAside(),{messages:F,setMessages:P,reload:_,error:z,append:Q,input:Y,setInput:V,status:q,handleInputChange:$,handleSubmit:U,stop:H}=s.useChat({api:"https://semaphor.cloud/api/v1/assistant",headers:{Authorization:`Bearer ${c==null?void 0:c.accessToken}`},body:{random:Math.random(),assistantId:"editor-assistant",isDevMode:d,aiContext:a},maxSteps:10,onError:B=>{console.log("on error",B)},onToolCall:W,onFinish(B,K){G(K.usage),r(""),K.finishReason==="stop"&&r("")}});A.useEffect(()=>{g&&T&&(Q({role:"user",content:T}),k(!1))},[g,T,Q,k]);async function W({toolCall:B}){var K,re,he,je;if(["createChartFromSql","createForecast"].includes(B.toolName)){const te=B.args.sql,le=B.args.python,oe=B.args.chartType;return f(oe),u(te),h(le),p(!0),{status:"success",message:"Let the user know that chart is created"}}else if(B.toolName==="getTableColumns")r("Getting table columns...");else{if(B.toolName==="getUserSql")return i?{status:"success",sql:i,python:m||""}:{status:"error",message:"No SQL found"};if(B.toolName==="getCurrentTableScope")return r("Getting table scope..."),ee(),{status:"success",tables:x};if(B.toolName==="generateQueryConfig"){r("Generating query config...");const te=B.args.chartType,le=B.args.queryConfig;B.args.cardConfig,f(te),I(le),u(void 0),h(void 0);const oe=await w();return(K=oe==null?void 0:oe.data)!=null&&K.error?{status:"error",queryConfig:le,message:(re=oe==null?void 0:oe.data)==null?void 0:re.error.message}:(he=oe==null?void 0:oe.data)!=null&&he.records?{status:"success",queryConfig:le,message:"The data has been visualized"}:{status:"success",queryConfig:le,message:"The data has been visualized",records:(je=oe==null?void 0:oe.data)==null?void 0:je.records}}else r("Analyzing...")}}function G(B){o(K=>K?{promptTokens:K.promptTokens+B.promptTokens,completionTokens:K.completionTokens+B.completionTokens,totalTokens:K.totalTokens+B.totalTokens}:B)}function Z(B){const K=s.removeFromScopeArray(x,B);se(K)}function se(B){const K=s.sanitizeAIScope(B);R(K),P(re=>[...re,{id:crypto.randomUUID(),role:"system",content:`Current scope of tables. Always use the table reference format when generating SQL | ${JSON.stringify(K)}`}])}function ne(){const B={databaseName:C||"",schemaName:E||"",tableName:O||"",datamodelId:L||"",datamodelName:s.resolveDatamodelName(v),connectionId:S||"",connectionType:y||""},K=s.dedup([...x,B]);se(K)}function ee(){const B={databaseName:C||"",schemaName:E||"",tableName:O||"",datamodelName:s.resolveDatamodelName(v),connectionId:S||"",connectionType:y||"",datamodelId:L||""};if(!s.isInAiScopeArray([...N||[],...x],B)||x.length===0){const re=s.dedup([...N||[],...x,B]).filter(he=>he.connectionId===B.connectionId);se(re)}}const ae=!["ready","error"].includes(q),J=s.getErrorMessage(z);return{messages:F,setMessages:P,reload:_,append:Q,input:Y,setInput:V,isLoading:ae,handleSetAIScope:ee,handleAddCurrentTableToScope:ne,removeFromCurrentSelections:Z,currentAssitantScope:x,setCurrentAssitantScope:R,handleInputChange:$,handleSubmit:U,stop:H,chatStatus:q,toolStatus:n,llmUsage:l,onFinish:t,errorMessage:J}}const el=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function Ws(){const t=A.useRef(null),a=A.useRef(null),[n,r]=A.useState(!1),[l,o]=A.useState(s.isDevEnv),[c,i]=A.useState(!1),[d,m]=A.useState(!1),h=s.useCurrentAIContext(),{setCurrentAIContext:u}=s.useAIActions(),p=s.useDashboardStore(H=>H.dashboard.aiContext),f=s.useEditorStore(H=>H.card.dataSource),j=s.useEditorStore(H=>H.actions.setCardSql),x=s.useEditorStore(H=>H.actions.setRunSql),T=s.useEditorStore(H=>H.showAIDialog),g=s.useDashboardStore(H=>H.handleOpenAssistantProfile),{setShowAIDialog:b,setCardPython:C}=s.useEditorActions(),{setDataSource:O}=s.useEditorActions(),{assistantProfile:S}=s.useAssistantProfile(),{messages:E,setMessages:N,reload:D,append:w,input:M,setInput:k,isLoading:R,handleInputChange:I,handleSubmit:v,stop:y,chatStatus:L,llmUsage:F,errorMessage:P}=Zr();s.useAssistantScroll({scrollContainerRef:t,inputRef:a,messages:E}),A.useEffect(()=>{var H;T&&((H=a.current)==null||H.focus(),u({selectedEntities:(f==null?void 0:f.selectedEntities)||[]}))},[T,f,u]);function _(H,W){W==="sql"?(j(H),x(!0)):W==="python"&&(C(H),x(!0))}function z(){var H;(H=p==null?void 0:p.selectedEntities)==null||H[0],m(!0)}function Q(){h.selectedEntities.length===0&&u({selectedEntities:(p==null?void 0:p.selectedEntities)||[]})}function Y(H){Q();const W={role:"user",content:H.title+" "+H.subtitle};w(W)}function V(H){Q(),w({role:"user",content:H})}function q(){return e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.Button,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",variant:"ghost",children:e.jsx(s.EllipsisVertical,{className:"size-3.5"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",children:[e.jsx(s.DropdownMenuLabel,{children:"Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx(s.DropdownMenuCheckboxItem,{checked:n,onCheckedChange:r,children:"Token Usage"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:l,onCheckedChange:o,children:"Tool Invocations"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:c,onCheckedChange:i,children:"System Message"})]})]})}function $(){return e.jsx("div",{"data-role":"header",children:e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"})})}const U=(M==null?void 0:M.length)===0&&(E==null?void 0:E.filter(H=>H.role==="user").length)===0;return e.jsxs("div",{"data-role":"editor-assistant",className:"flex h-full w-full flex-col justify-between px-6 py-4",children:[e.jsxs("div",{"data-role":"header-section",className:"flex items-start justify-between gap-2",children:[$(),g&&e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-7 p-2",onClick:g,children:(S==null?void 0:S.name)||"Default Profile"}),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center gap-2",children:[q(),e.jsx(s.IconButton,{tooltip:"Add Context",className:"h-7 p-2 text-muted-foreground hover:text-foreground",onClick:z,children:e.jsx(s.Settings,{className:"size-4"})}),e.jsx(s.IconButton,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",tooltip:"New Chat",onClick:()=>{N([]),D()},children:e.jsx(s.SquarePen,{className:"size-4"})}),e.jsx(s.Button,{variant:"ghost",className:"h-8 text-muted-foreground hover:text-foreground",size:"sm",onClick:()=>{b(!1)},children:e.jsx(s.EyeOff,{className:"size-4"})})]})]}),e.jsx(s.CustomScrollArea,{ref:t,className:"mt-2 flex w-full grow basis-0",children:e.jsxs("div",{className:"space-y-4 py-2 pr-3","data-role":"messages",children:[e.jsx(s.PrintMessages,{messages:E,showTools:l,handleRun:_,showSystemMessage:c}),e.jsx(s.ChatStatus,{messages:E,chatStatus:L,errorMessage:P})]})}),e.jsxs("div",{className:"mb-3",children:[U&&!S&&e.jsx(s.Placeholders,{onClick:Y,placeholders:el}),e.jsx("div",{children:U&&S&&e.jsx(s.SeedQuestions,{seedQuestions:S.seedQuestions||[],handleSeedQuestionClick:V})})]}),e.jsx(s.ChatInputWithContext,{ref:a,setInput:k,stop:y,input:M,handleInputChange:I,handleSubmit:H=>{Q(),v(H)},isLoading:R,onFocus:()=>{}}),F&&n&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",F.promptTokens," prompt tokens,"," ",F.completionTokens," completion tokens,"," ",F.totalTokens," total tokens"]})}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(f==null?void 0:f.selectedEntities)||[],open:d,onOpenChange:m,dataSource:f||s.EMPTY_DATA_SOURCE,fallbackDataSource:s.EMPTY_DATA_SOURCE,dialogMode:"aiContext"})]})}function Jt(t,a){return`${t}-${a.qualifiedEntityName}-${a.name}-${Date.now()}`}function Wt(t,a){const n=s.toTitleCase(t.replace(/_/g," "));switch(a){case"SUM":return`Sum of ${n}`;case"COUNT":return`Count of ${n}`;case"DISTINCT":return`Unique ${n}`;case"AVG":return`Average of ${n}`;case"MIN":return`Min of ${n}`;case"MAX":return`Max of ${n}`;default:return n}}function sl(t,a){const n=s.getDataType(t.dataType);return n==="number"?"metrics":n==="date"||n==="string"?"groupBy":"metrics"}function cs(t,a){const n=Jt(a,t),r=s.getDataType(t.dataType),l=r==="date",o={id:n,name:t.name,dataType:t.dataType,label:s.toLabel(t.name),qualifiedEntityName:t.qualifiedEntityName,entityId:t.entityId,entityName:t.entityName,entityType:t.entityType};if(a==="metrics"){const c=r==="number"?"SUM":"COUNT",i=Wt(t.label,c);return{...o,role:"metric",label:i,aggregate:c}}return a==="groupBy"?{...o,role:"groupby",...l&&{granularity:"month",dateFormat:"YYYY-MM"}}:a==="pivotBy"?{...o,role:"pivotby"}:a==="sortBy"?{...o,role:"sortby",label:t.label,direction:"asc"}:null}function Yt(t,a,n){var r;return!n||!((r=n.baseEntity)!=null&&r.name)||!n.joins?!1:t===a||t===n.baseEntity.name||a===n.baseEntity.name?!0:n.joins.some(l=>{const o=s.getQualifiedEntityName(l.source),c=s.getQualifiedEntityName(l.target);return o===t&&c===a||o===a&&c===t})}function tl(t,a,n){if(a.length===0)return{isValid:!0};const r=a[0].qualifiedEntityName;if(!r)return{isValid:!0};if(r!==t.qualifiedEntityName&&t.qualifiedEntityName)if(n){if(!Yt(r,t.qualifiedEntityName,n))return{isValid:!1,showError:!0}}else return{isValid:!1,showError:!0};return{isValid:!0}}function ds(){s.ue.error("Join Configuration Required",{richColors:!0,description:"To combine fields from two different tables, you need to define a join between them first.",position:"top-center",duration:4e3})}function us(t,a){return a.some(n=>n.name===t.name&&n.qualifiedEntityName===t.qualifiedEntityName)}const As=t=>{var k;const{updateCardConfig:a,updateGroupByColumn:n,updateMetricColumn:r,updatePivotByColumn:l,updateSortByColumn:o,removeGroupByColumn:c,removeMetricColumn:i,removePivotByColumn:d,removeSortByColumn:m}=s.useEditorActions(),h=s.useEditorStore(R=>R.card.type),u=t==null?void 0:t.id,p=A.useCallback(R=>{if(!(t!=null&&t.config))return[];switch(R){case"groupBy":return(t.config.groupByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType,dateFormat:I.dateFormat,customFormat:I.customFormat,granularity:I.granularity}));case"metrics":return t.config.metricColumns||[];case"pivotBy":return(t.config.pivotByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType}));case"sortBy":return(t.config.sortByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType,direction:I.direction}));case"source":return[];default:return[]}},[t]),f=A.useCallback((R,I)=>{if(u)switch(R){case"groupBy":{a(u,{groupByColumns:I});break}case"metrics":{a(u,{metricColumns:I});break}case"pivotBy":{a(u,{pivotByColumns:I});break}case"sortBy":{a(u,{sortByColumns:I});break}}},[u,a]),j=A.useCallback((R,I,v)=>{if(!u)return;const L=p(R)[I];if(L)switch(R){case"groupBy":n(u,L.id,{label:v});break;case"metrics":r(u,L.id,{label:v});break;case"pivotBy":l(u,L.id,{label:v});break;case"sortBy":o(u,L.id,{label:v});break}},[u,p,n,r,l,o]),x=A.useCallback((R,I)=>{if(!u)return;const y=p("metrics")[R];y&&r(u,y.id,{aggregate:I,label:Wt(y.name,I)})},[u,p,r]),T=A.useCallback((R,I,v)=>{if(!u)return;const L=p("groupBy")[R];if(!L)return;const F={dateFormat:I};I==="custom"&&v?F.customFormat=v:F.customFormat=void 0,n(u,L.id,F)},[u,p,n]),g=A.useCallback((R,I)=>{if(!u)return;const y=p("groupBy")[R];y&&n(u,y.id,{granularity:I})},[u,p,n]),b=A.useCallback((R,I)=>{if(!u)return;const y=p("metrics")[R];y&&r(u,y.id,{aliasTemplate:I})},[u,p,r]),C=A.useCallback((R,I)=>{if(console.log("handleValueAliasesChange",R,I),!u)return;console.log("handleValueAliasesChange",R,I);const y=p("metrics")[R];y&&(console.log("handleValueAliasesChange",I),r(u,y.id,{valueAliases:I}))},[u,p,r]),O=A.useCallback(R=>{u&&a(u,{comparisonType:R})},[u,a]),S=A.useCallback(R=>{u&&a(u,{showTrendline:R})},[u,a]),E=A.useCallback(R=>{u&&a(u,{trendlineWindow:R})},[u,a]),N=A.useCallback(R=>{u&&a(u,{trendlineGranularity:R})},[u,a]),D=A.useCallback(R=>{u&&a(u,{targetValue:R})},[u,a]),w=A.useCallback(R=>{var _;if(!u)return;const I=[...p("groupBy"),...p("pivotBy"),...p("metrics"),...p("sortBy")],v=(_=t==null?void 0:t.config)==null?void 0:_.joinPlan,y=tl(R,I,v);if(!y.isValid){y.showError&&ds();return}const L=sl(R),F=p(L);if(us(R,F))return;const P=cs(R,L);P&&f(L,[...F,P])},[u,(k=t==null?void 0:t.config)==null?void 0:k.joinPlan,p,f]),M=A.useCallback((R,I)=>{if(!u)return;const y=p(R)[I];if(y){if(R!=="sortBy"){const L=p("sortBy"),F=L.filter(P=>!(P.name===y.name&&P.qualifiedEntityName===y.qualifiedEntityName));F.length!==L.length&&a(u,{sortByColumns:F})}switch(R){case"groupBy":c(u,y.id);break;case"metrics":i(u,y.id);break;case"pivotBy":d(u,y.id);break;case"sortBy":m(u,y.id);break}}},[u,p,c,i,d,m,a]);return{getFieldsForContainer:p,setFieldsForContainer:f,updateLabel:j,handleAggregationChange:x,handleDateFormatChange:T,handleGranularityChange:g,handleAliasTemplateChange:b,handleValueAliasesChange:C,removeField:M,handleFieldClick:w,handleKpiComparisonTypeChange:O,handleKpiShowTrendlineChange:S,handleKpiTrendlineWindowChange:E,handleKpiTrendlineGranularityChange:N,handleKpiTargetValueChange:D,cardType:h}};function nl({field:t}){const{attributes:a,listeners:n,setNodeRef:r,transform:l,transition:o,isDragging:c}=s.useSortable({id:t.id}),[i,d]=A.useState(!1),m=s.useEditorStore(x=>x.card),{handleFieldClick:h}=As(m),u={transform:s.CSS.Transform.toString(l),transition:o},p=s.getDataType(t.dataType),j=(x=>{const T=(x||"").toLowerCase();switch(s.getDataType(T)){case"number":return fs;case"date":return s.Calendar;case"boolean":return ot;case"json":return Sn;case"geo":return Gn;default:return it}})(t.dataType);return e.jsxs("div",{ref:r,style:u,...a,...n,className:`group/container overflow-hidden rounded-lg transition-all ${c?"scale-95 opacity-50":"border border-transparent hover:border-blue-200 hover:bg-blue-50 dark:hover:border-blue-800 dark:hover:bg-blue-950"}`,children:[e.jsxs("div",{className:"flex min-w-0 items-center gap-1 p-2",onClick:()=>h(t),children:[e.jsx("div",{className:"flex-shrink-0 hover:cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"invisible size-3.5 text-gray-400 group-hover/container:visible"})}),e.jsx(j,{className:s.cn("size-3.5 flex-shrink-0 text-gray-500",p==="string"?"text-blue-500":"",p==="number"?"text-green-500":"",p==="date"?"text-purple-500":"",p==="boolean"?"text-orange-500":"")}),e.jsx(s.TooltipProvider$1,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("div",{className:"min-w-0 flex-1 overflow-hidden",children:e.jsx("span",{className:`block text-sm text-foreground/90 hover:text-foreground dark:text-muted-foreground ${i?"whitespace-normal break-words":"truncate"}`,children:t.name})})}),e.jsx(s.TooltipContent,{side:"bottom",align:"start",className:"ml-5 max-w-xs",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"text-sm font-medium",children:t.label||t.name}),t.description&&e.jsxs("div",{className:"border-t border-border pt-2",children:[e.jsx("div",{className:"mb-1 text-xs text-muted-foreground",children:"Description:"}),e.jsx("div",{className:"text-xs leading-relaxed",children:t.description})]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"mr-2 text-muted-foreground",children:"Name:"}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.name})]}),e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"mr-2 text-muted-foreground",children:"Type:"}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.dataType})]}),t.qualifiedEntityName&&e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{className:"mr-2 text-muted-foreground",children:[t.entityType?t.entityType.charAt(0).toUpperCase()+t.entityType.slice(1):"Entity",":"]}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.qualifiedEntityName})]})]})]})})]})})]}),i&&e.jsx("div",{className:"text-muted-foreground-700 px-2 pb-3 text-sm",children:e.jsx(s.Card,{className:"",children:e.jsxs(s.CardContent,{className:"space-y-2 p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(lt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"break-all",children:t.label||t.name})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(na,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"break-all",children:t.name})]}),e.jsx(s.Badge,{variant:"outline",className:"mt-2 text-xs",children:t.dataType})]})})})]})}function al(){const[t,a]=A.useState(""),[n,r]=A.useState("all"),l=s.useEditorStore(C=>C.card.dataSource),{columns:o=[],columnGroups:c=[],isLoading:i,error:d}=s.useColumns(),m=o.filter(C=>C.name.toLowerCase().includes(t.toLowerCase())||C.dataType.toLowerCase().includes(t.toLowerCase())||C.qualifiedEntityName&&C.qualifiedEntityName.toLowerCase().includes(t.toLowerCase())),h=C=>C.dataType==="number"||C.dataType==="integer"||C.dataType==="float"||C.dataType==="decimal"||C.dataType==="currency"?"metrics":"dimensions",p=(n==="all"?m:m.filter(C=>h(C)===n)).reduce((C,O)=>{const S=O.qualifiedEntityName||"Other Fields";return C[S]||(C[S]={label:s.toLabel(O.entityName),fields:[]}),C[S].fields.push(O),C},{}),j=Object.keys(p),x=m.filter(C=>h(C)==="dimensions").length,T=m.filter(C=>h(C)==="metrics").length,g=m.filter(C=>h(C)==="calculated").length,b=m.length>0;return i?e.jsxs("div",{className:"flex h-[calc(100vh-200px)] w-[290px] flex-col items-center justify-center",children:[e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-gray-400"}),e.jsx("span",{className:"mt-2 text-sm text-gray-500",children:"Loading fields..."})]}):d?e.jsxs("div",{className:"flex h-[calc(100vh-200px)] w-[290px] flex-col items-center justify-center",children:[e.jsx(s.Database,{className:"mb-4 h-12 w-12 text-red-300"}),e.jsx("p",{className:"mb-2 text-sm text-red-500",children:"Error loading fields"}),e.jsx("p",{className:"px-4 text-center text-xs text-red-400",children:d.message||"Failed to load column data"})]}):!l||!l.selectedEntities||l.selectedEntities.length===0?e.jsx(s.ScrollArea,{className:"h-[calc(100vh-200px)] w-[290px]",children:e.jsxs("div",{className:"flex flex-col items-center justify-center py-8 text-center",children:[e.jsx(s.Database,{className:"mb-4 h-12 w-12 text-gray-300"}),e.jsx("p",{className:"mb-2 text-sm text-gray-500",children:"No data source selected"}),e.jsx("p",{className:"px-4 text-xs text-gray-400",children:"Select a table, view, or dataset from the data source panel to see available fields"})]})}):e.jsxs("div",{className:"mt-2 flex h-[calc(100vh-200px)] w-full grow flex-col gap-2 px-6 pb-6",children:[e.jsxs("div",{className:"mt-1 flex-shrink-0 space-y-2",children:[o.length>0&&e.jsx("div",{className:"mb-4",children:e.jsx(s.Input$1,{type:"text",placeholder:"Search fields...",value:t,onChange:C=>a(C.target.value)})}),b&&e.jsx(s.Tabs,{defaultValue:"all",value:n,onValueChange:C=>r(C),className:"mb-4",children:e.jsxs(s.TabsList,{className:"grid h-9 w-full grid-cols-4",children:[e.jsxs(s.TabsTrigger,{value:"all",className:"text-xs",children:["All",e.jsx("span",{className:"ml-2 text-xs font-semibold",children:m.length})]}),e.jsxs(s.TabsTrigger,{value:"dimensions",className:"text-xs",children:[e.jsx(s.Layers,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:x})]}),e.jsxs(s.TabsTrigger,{value:"metrics",className:"text-xs",children:[e.jsx(s.ChartColumn,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:T})]}),e.jsxs(s.TabsTrigger,{value:"calculated",className:"text-xs",children:[e.jsx(bn,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:g})]})]})})]}),e.jsxs(s.CustomScrollArea,{className:"py-2",children:[e.jsx(s.Accordion,{type:"multiple",defaultValue:j,className:"space-y-2",children:Object.entries(p).map(([C,O])=>{const S=c.find(E=>E.entityName===C)||{entityName:C,label:O.label};return e.jsxs(s.AccordionItem,{value:C,className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"group rounded-md px-3 py-2 hover:bg-muted hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium",children:S.label||S.entityName}),e.jsx(s.Badge,{variant:"secondary",className:"rounded-full text-xs text-muted-foreground group-hover:bg-background",children:O.fields.length})]})}),e.jsx(s.AccordionContent,{className:"pb-2",children:e.jsx("div",{className:"space-y-1",children:O.fields.map(E=>e.jsx(nl,{field:E},E.id))})})]},C)})}),Object.keys(p).length===0&&o.length===0&&e.jsxs("div",{className:"py-8 text-center text-sm text-gray-500",children:[e.jsx("p",{children:"No fields found for selected data source"}),e.jsx("p",{className:"mt-1 text-xs text-gray-400",children:"Try selecting different data sources"})]}),Object.keys(p).length===0&&o.length>0&&e.jsx("div",{className:"py-4 text-center text-sm text-gray-500",children:"No fields found for this category"}),e.jsx(s.ScrollBar,{orientation:"horizontal"}),e.jsx(s.ScrollBar,{orientation:"vertical"})]})]})}function rl(){return e.jsxs("div",{className:" h-full flex flex-col items-center",children:[" ",e.jsx(al,{})," "]})}function ll({initialJoinPlan:t,availableDataSources:a}){const[n,r]=A.useState(t),[l,o]=A.useState(new Set),c=()=>{const E=n.joins.length>0?Math.max(...n.joins.map(D=>D.sequence))+1:1,N={id:`join-${Date.now()}`,source:{type:"table",name:"",database:"",schema:""},target:{type:"table",name:"",database:"",schema:""},joinType:"INNER",sequence:E,joinKeyGroups:[{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}]};r({...n,joins:[...n.joins,N]}),o(new Set([...l,N.id]))},i=E=>{r({...n,joins:n.joins.filter(N=>N.id!==E)}),o(new Set([...l].filter(N=>N!==E)))},d=(E,N)=>{var k,R;const D=n.joins.find(I=>I.id===E);if(!D)return;const w={...D,...N},M=n.joins.map(I=>I.id===E?w:I);if((k=w.source)!=null&&k.name&&((R=w.target)!=null&&R.name)&&w.joinKeyGroups[0].keys.length===0){const I=a.find(F=>{var P;return F.name===((P=w.source)==null?void 0:P.name)}),v=a.find(F=>{var P;return F.name===((P=w.target)==null?void 0:P.name)}),y={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",entityId:(I==null?void 0:I.name)||"",entityName:(I==null?void 0:I.name)||"",entityType:(I==null?void 0:I.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",entityId:(v==null?void 0:v.name)||"",entityName:(v==null?void 0:v.name)||"",entityType:(v==null?void 0:v.type)||"table"},operator:"="},L=[...w.joinKeyGroups];L[0]={...L[0],keys:[y]},w.joinKeyGroups=L,M[M.findIndex(F=>F.id===E)]=w}r({...n,joins:M})};return{joinPlan:n,expandedJoins:l,setJoinPlan:r,addJoin:c,removeJoin:i,updateJoin:d,moveJoinUp:E=>{const N=n.joins.findIndex(D=>D.id===E);if(N>0){const D=[...n.joins],w={...D[N]},M={...D[N-1]};D[N]=M,D[N-1]=w,D.forEach((R,I)=>{R.sequence=I+1});const k={...n,joins:D};r(k)}},moveJoinDown:E=>{const N=n.joins.findIndex(D=>D.id===E);if(N<n.joins.length-1){const D=[...n.joins],w={...D[N]},M={...D[N+1]};D[N]=M,D[N+1]=w,D.forEach((R,I)=>{R.sequence=I+1});const k={...n,joins:D};r(k)}},toggleJoinExpansion:E=>{const N=new Set(l);N.has(E)?N.delete(E):N.add(E),o(N)},getAvailableSourceEntities:E=>{if(n.joins.length===0)return a;const N=n.joins.findIndex(w=>w.id===E);if(N===0)return a;const D=new Set;return n.baseEntity.name&&D.add(n.baseEntity.name),n.joins.slice(0,N).forEach(w=>{D.add(w.source.name),D.add(w.target.name)}),a.filter(w=>D.has(w.name))},getAvailableTargetEntities:E=>a.filter(N=>N.name!==E),addJoinKeyGroup:E=>{const N=n.joins.find(w=>w.id===E);if(!N)return;const D=[...N.joinKeyGroups,{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}];d(E,{joinKeyGroups:D})},updateGroupOperator:(E,N,D)=>{const w=n.joins.find(k=>k.id===E);if(!w)return;const M=[...w.joinKeyGroups];M[N]={...M[N],operator:D},d(E,{joinKeyGroups:M})},removeJoinKeyGroup:(E,N)=>{const D=n.joins.find(M=>M.id===E);if(!D)return;const w=[...D.joinKeyGroups];w.splice(N,1),d(E,{joinKeyGroups:w})},addJoinKey:(E,N)=>{const D=n.joins.find(I=>I.id===E);if(!D)return;const w=a.find(I=>I.name===D.source.name),M=a.find(I=>I.name===D.target.name),k=[...D.joinKeyGroups],R={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",entityId:(w==null?void 0:w.name)||"",entityName:(w==null?void 0:w.name)||"",entityType:(w==null?void 0:w.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",entityId:(M==null?void 0:M.name)||"",entityName:(M==null?void 0:M.name)||"",entityType:(M==null?void 0:M.type)||"table"},operator:"="};k[N]={...k[N],keys:[...k[N].keys,R]},d(E,{joinKeyGroups:k})},updateJoinKey:(E,N,D,w,M)=>{const k=n.joins.find(L=>L.id===E);if(!k)return;const R=a.find(L=>L.name===k.source.name),I=a.find(L=>L.name===k.target.name),v=[...k.joinKeyGroups],y=v[N].keys[D];w==="sourceColumnName"&&R?v[N].keys[D]={...y,source:{...y.source,name:M}}:w==="targetColumnName"&&I?v[N].keys[D]={...y,target:{...y.target,name:M}}:w==="operator"&&(v[N].keys[D]={...y,operator:M}),d(E,{joinKeyGroups:v})},removeJoinKey:(E,N,D)=>{const w=n.joins.find(k=>k.id===E);if(!w)return;const M=[...w.joinKeyGroups];M[N].keys.splice(D,1),d(E,{joinKeyGroups:M})},generateSQLPreview:()=>{const E=[...n.joins].sort((w,M)=>w.sequence-M.sequence),N=n.baseEntity;if(!N.name)return"SELECT * FROM table -- Please select a base entity";let D=`SELECT *
413
+ `;b(z)}function F(){return e.jsxs(e.Fragment,{children:[r&&e.jsx("div",{className:"relative flex w-1/3 min-w-0 grow overflow-hidden text-xs",role:"editor-filter-container",children:e.jsx("div",{className:"mt-2 w-[99%]",children:e.jsx(Ka,{jsonString:JSON.stringify(m,null,2)})})}),m&&m.length>0&&e.jsx(s.Button,{variant:"ghost",onClick:()=>l(!r),className:"absolute bottom-0 right-6 size-7 p-1",children:r?e.jsx(s.X,{className:"size-3.5 text-muted-foreground"}):e.jsx(s.Filter,{className:"size-3.5 text-muted-foreground"})})]})}function P(){return e.jsxs(s.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(s.ResizablePanel,{id:"1",order:1,defaultSize:a?20:100,className:"relative flex grow",children:[e.jsx("div",{id:"sql-container",className:"relative flex w-[99%]",children:e.jsx(Wa,{})}),a&&e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"sql"})]}),a&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(s.ResizablePanel,{id:"2",order:2,defaultSize:80,className:"relative flex grow",children:[e.jsx("div",{id:"python-container",className:"relative flex w-[99%] pt-0",children:e.jsx(Ya,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function _(){return e.jsxs("div",{role:"editor-controls",className:"item-center flex flex-wrap justify-between gap-2 px-6 py-4",children:[e.jsxs("div",{className:"flex grow items-center gap-2",children:[e.jsxs(s.Button,{title:"Shift + Enter",disabled:D||w,onClick:()=>{v()},className:"h-8 shrink-0",size:"sm",children:[D||w?e.jsx(s.LoaderCircle,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(s.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),e.jsx(At,{sql:N==null?void 0:N.sql,error:y,onDebugWithAssistant:L}),e.jsx(s.EditorClearButton,{}),e.jsx(s.Toggle,{pressed:a,onPressedChange:z=>n(z),className:"",size:"sm",children:e.jsx(Ja,{className:"size-4 text-foreground/70"})}),e.jsx("button",{disabled:c,onClick:()=>{C(!0)},className:"h-9 w-full cursor-text px-3 text-left text-sm text-muted-foreground",children:c?"":"Type your question..."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.EditorCancelButton,{}),e.jsx(s.EditorAcceptButton,{})]})]})}return e.jsxs("section",{role:"editor-section",className:"flex grow flex-col justify-end",children:[e.jsxs("div",{className:"relative flex grow justify-between overflow-y-scroll",children:[P(),F()]}),_()]})}function Xa({data:t}){var c;const[a,n]=A.useState([]),l=Object.keys(t[0]).map(i=>({accessorKey:i,header:({column:d})=>{const m=d.getIsSorted();return e.jsx("div",{className:"flex gap-2",children:e.jsxs("div",{className:"flex cursor-pointer items-center",onClick:()=>d.toggleSorting(d.getIsSorted()==="asc"),children:[i,m==="asc"?e.jsx(s.TriangleUpIcon,{className:"ml-2 h-5 w-5"}):m==="desc"?e.jsx(s.TriangleDownIcon,{className:"ml-2 h-5 w-5"}):null]})})}})),o=s.useReactTable({data:t,columns:l,getCoreRowModel:s.getCoreRowModel(),getPaginationRowModel:s.getPaginationRowModel(),onSortingChange:n,getSortedRowModel:s.getSortedRowModel(),manualPagination:!0,state:{sorting:a}});return e.jsxs(s.CustomScrollArea,{className:"grow overflow-auto font-mono text-xs text-foreground/95",children:[e.jsxs("table",{className:"w-full",children:[e.jsx(s.TableHeader,{className:"sticky top-0 z-50 h-0 bg-muted",children:o.getHeaderGroups().map(i=>e.jsx(s.TableRow,{className:"",children:i.headers.map(d=>e.jsx(s.TableHead,{className:"h-11 font-semibold text-foreground",children:d.isPlaceholder?null:s.flexRender(d.column.columnDef.header,d.getContext())},d.id))},i.id))}),e.jsx(s.TableBody,{children:(c=o.getRowModel().rows)!=null&&c.length?o.getRowModel().rows.map(i=>e.jsx(s.TableRow,{className:"h-11 whitespace-nowrap py-2","data-state":i.getIsSelected()&&"selected",children:i.getVisibleCells().map(d=>e.jsx(s.TableCell,{className:"py-2",children:s.flexRender(d.column.columnDef.cell,d.getContext())},d.id))},i.id)):e.jsx(s.TableRow,{children:e.jsx(s.TableCell,{colSpan:l.length,className:"h-24 text-center",children:"No results."})})})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function Za({card:t}){var b,C,O,S,E,N,D,w,M,k,R;const[a,n]=A.useState(!1),[r,l]=A.useState([]),{authToken:o}=s.useSemaphorContext(),c=s.getTokenParams(o==null?void 0:o.accessToken),i=s.useDashboardStore(I=>I.themeStyle),d={colors:((C=(b=i==null?void 0:i.chart)==null?void 0:b.dataset)==null?void 0:C.backgroundColor)||[]};(O=t.customCardPreferences)!=null&&O.inputData;const{getCard:m}=s.useCustomVisual(((S=t.customCardPreferences)==null?void 0:S.url)||""),h=((E=t.customCardPreferences)==null?void 0:E.visualType)||"single",u=s.useEditorStore(I=>I.frame),{setCustomCardPreferences:p}=s.useEditorActions(),{data:f}=s.useDashboardCardQuery(t),j=A.useCallback((I,v)=>{l(y=>({...y,[I]:v}))},[]);if(!((N=t.customCardPreferences)!=null&&N.componentName))return null;const x=m((D=t.customCardPreferences)==null?void 0:D.componentName);function T(I){n(I)}const g=Object.keys(((w=t.customCardPreferences)==null?void 0:w.dataInputCardIds)||{}).map(I=>{var L,F,P;const v=(F=(L=t.customCardPreferences)==null?void 0:L.dataInputCardIds)==null?void 0:F[I].cardId,y=(P=u==null?void 0:u.cards)==null?void 0:P.find(_=>_.id===v);return y||null});return e.jsx("div",{className:"semaphor-custom mb-3 flex grow basis-0 flex-col space-y-2 overflow-y-auto",children:e.jsxs(A.Suspense,{fallback:e.jsx(e.Fragment,{}),children:[x&&h==="single"&&e.jsx(x,{editing:!0,params:c,theme:d,settings:(M=t.customCardPreferences)==null?void 0:M.settings,onDataChange:j,data:(f==null?void 0:f.records)||[]}),x&&h==="multiple"&&e.jsxs(e.Fragment,{children:[Object.keys(((k=t.customCardPreferences)==null?void 0:k.dataInputCardIds)||{}).map(I=>{var L,F,P;const v=(F=(L=t.customCardPreferences)==null?void 0:L.dataInputCardIds)==null?void 0:F[I].cardId,y=(P=u==null?void 0:u.cards)==null?void 0:P.find(_=>_.id===v);if(y)return e.jsx(s.MultiInputEmptyRenderer,{onLoadingStatus:T,onDataChange:j,dataInputIndex:I,card:y},y.id+I)}),e.jsx("div",{className:"flex flex-col gap-2",children:e.jsx(x,{theme:d,onDataChange:j,settings:g.map(I=>{var v;return(v=I==null?void 0:I.customCardPreferences)==null?void 0:v.settings}),data:g.map(I=>I!=null&&I.id?r==null?void 0:r[I.id]:[])})})]})]},(R=t.customCardPreferences)==null?void 0:R.componentName)})}function Rt({className:t,...a}){var b;const n=s.useEditorStore(C=>C.card),r=s.isExplorerCard(n),l=((b=n.customCardPreferences)==null?void 0:b.showCardHeader)??!0,o=s.useEditorStore(C=>C.frame),c=s.useDashboardStore(C=>C.dashboard.filters);s.useEditorStore(C=>C.actions.setRunSql);const{updateFilter:i}=s.useDashboardActions(),d=c==null?void 0:c.find(C=>C.id===o.filterId),{data:m,isLoading:h,isFetching:u,errorMessage:p}=s.useDashboardCardQuery(n),{setCard:f}=s.useEditorActions(),{data:j}=s.useTopoJson(n),x=A.useMemo(()=>!n.id||!(m!=null&&m.records)?null:s.createChartConfig({card:n,data:m==null?void 0:m.records,cardType:n.type,queryConfig:n.queryConfig,customCfg:n.customCfg,preferences:n.preferences,topoJson:j}),[n.id,m==null?void 0:m.records,n.type,n.queryConfig,n.customCfg,n.preferences,j]);function T(C){return e.jsx(Za,{card:C})}function g(){if(m!=null&&m.records&&(m==null?void 0:m.records.length)===0&&!h&&!u)return p?e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.SearchX,{className:"size-8 text-destructive"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:r?"Error fetching data. Please check your chart configuration":"Error fetching data. Please check your SQL or Python code."})]}):e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No results to display"})]});if(!n.id)return null;if(n.type==="custom")return T(n);if(!["table","custom"].includes(n.type)&&x)return e.jsx(e.Fragment,{children:h||u?e.jsx(e.Fragment,{}):e.jsx(s.ChartJsVisual,{cfg:x})});if(n.type==="table"&&(m!=null&&m.records)&&(m==null?void 0:m.records.length)>0)return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableVisual,{card:n,data:m==null?void 0:m.records})})}return e.jsxs(s.Card,{role:"editor-visual-card",className:s.cn("relative flex h-full flex-col rounded-none",t),...a,children:[l&&e.jsxs(s.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{children:[e.jsx(s.Editable,{enabled:!0,text:n.title,onSave:C=>f({...n,title:C}),children:e.jsx("div",{className:"flex items-center gap-5",children:e.jsx(s.CardTitle,{className:"text-base",children:n.title||"Add title"})})},n.title),e.jsx(s.Editable,{enabled:!0,text:n.description||"",onSave:C=>f({...n,description:C}),children:e.jsx(s.CardDescription,{children:n.description||"Add description"})},n.description)]}),d&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Editable,{enabled:!0,className:"my-1",text:d.title||d.column,onSave:C=>i({...d,title:C}),children:e.jsx(s.Label,{children:d.title})}),e.jsx(s.FilterComponent,{filter:d})]})]}),e.jsx(s.CardContent,{className:"flex grow flex-col",children:g()})]})}const ts=({language:t,value:a,onChange:n,readonly:r})=>e.jsx(s.Ft,{className:"h-full w-full overflow-hidden text-xs",theme:"default",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,lineHeight:16,renderLineHighlight:"none",readOnly:r},defaultLanguage:t,value:a,onChange:n}),er=()=>e.jsxs(s.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(s.DialogTitle,{children:"Customize Card"})}),e.jsxs(s.DialogDescription,{children:["See the configuration options and documentation",e.jsx("a",{target:"_blank",className:"ml-1 text-blue-500 underline",href:"https://www.chartjs.org/docs/latest/samples/bar/vertical.html",children:"here."})]})]}),sr=({customCode:t,cardCustomCfgString:a,mergedConfig:n,onConfigChange:r,onCodeChange:l})=>e.jsxs(s.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(s.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(s.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(s.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(s.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(s.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{language:"json",value:n,onChange:r})}),e.jsx(s.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{readonly:!0,language:"json",value:a,onChange:()=>{}})}),e.jsx(s.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ts,{language:"javascript",value:t,onChange:l})})]}),tr=({error:t,hasCardCustomConfig:a,onReset:n,onApply:r,onClose:l})=>e.jsxs(s.DialogFooter,{className:"flex items-center",children:[e.jsx(s.Label,{className:"mr-2 text-red-500",children:t}),e.jsx(s.Button,{disabled:!a,onClick:n,variant:"outline",children:"Reset"}),e.jsx(s.Button,{onClick:r,variant:"outline",children:"Apply"}),e.jsx(s.Button,{onClick:l,variant:"outline",children:"Close"})]});function nr(){const[t,a]=A.useState(!1),[n,r]=A.useState(""),[l,o]=A.useState(""),[c,i]=A.useState(""),[d,m]=A.useState(""),h=s.useEditorStore(S=>S.card),u=s.useEditorStore(S=>S.card.customCfg),p=u?JSON.stringify(u,null,2):"",{data:f}=s.useDashboardCardQuery(h),{data:j}=s.useTopoJson(h),{setCardCustomCfg:x,setCustomVisualCode:T}=s.useEditorActions(),g=S=>{var E,N;return JSON.stringify({...S,data:{...S.data,datasets:(N=(E=S.data)==null?void 0:E.datasets)==null?void 0:N.map(D=>({...D,data:[]}))}},null,2)},b=()=>{var E,N;if(!((E=f==null?void 0:f.records)!=null&&E.length))return;const S=s.createChartConfig({card:h,data:f.records,cardType:h.type,queryConfig:h.queryConfig,preferences:h.preferences,topoJson:j});if(S){r(g(S)),i(((N=h.preferences)==null?void 0:N.customVisualCode)||"");const D=s.merge(S,u);o(g(D))}},C=()=>{try{if(c&&T(c),l){const S=JSON.parse(l),E=s.getObjectDiff(JSON.parse(n),S);x(E),m("")}}catch{m("Invalid JSON configuration")}},O=Object.keys(u||{}).length===0;return e.jsxs(s.Dialog,{open:t,onOpenChange:a,children:[e.jsx(s.DialogTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Customize Visual",onClick:b,className:O?"":"text-green-600 hover:text-green-700",children:e.jsx(s.Settings,{className:"h-4 w-4"})})}),e.jsxs(s.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(er,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackEditorCard,children:e.jsx(Rt,{className:"w-1/2 rounded shadow-none"})}),e.jsx(sr,{customCode:c,cardCustomCfgString:p,mergedConfig:l,onConfigChange:S=>o(S??""),onCodeChange:S=>i(S??"")})]}),e.jsx(tr,{error:d,hasCardCustomConfig:!!u,onReset:()=>{o(n),x(null)},onApply:C,onClose:()=>a(!1)})]})]})}function Ye(t){const[a,n]=A.useState(null);return a}function Cs(){const{authToken:t}=s.useSemaphorContext(),{data:a,isLoading:n,isFetching:r,isError:l}=s.useQuery({queryKey:["plugins",t==null?void 0:t.accessToken],queryFn:()=>s.getPlugins(t==null?void 0:t.accessToken)});return{data:a,isLoading:n,isFetching:r,isError:l}}function ar({onChartTypeChange:t}){const[a,n]=A.useState([]),{setCardCustomCfg:r,setCardPreferences:l,setCardType:o,setCustomCardPreferences:c}=s.useEditorActions(),{data:i}=Cs();function d(m,h){r(null),l({}),o(m),h&&c({type:"component",visualType:"single",url:h.url,componentName:h.name,icon:h.icon}),t==null||t()}return A.useEffect(()=>{(async()=>{var h;if(i){const p=(await Promise.all((h=i==null?void 0:i.plugins)==null?void 0:h.map(async f=>(await s.loadManifest(f.value)).visuals.map(T=>({name:T.name,icon:T.icon,url:f.value}))))).flat();n(p)}})()},[i]),a.length===0?null:e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-2"}),e.jsx("div",{className:"grid grid-cols-4 place-items-center",children:a.map((m,h)=>e.jsx(rr,{text:m.name,onClick:()=>d("custom",m),icon:m.icon},m.url+m.name+h))})]})}const rr=({text:t,icon:a,onClick:n})=>{const r=Ye(),l=t.charAt(0).toUpperCase();return e.jsx(s.IconButton,{onClick:n,tooltip:t,children:r?e.jsx(r,{className:"h-4 w-4"}):l})};function Ss(t){return Se({attr:{viewBox:"0 0 256 256",fill:"currentColor"},child:[{tag:"path",attr:{d:"M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm87.63,96H191.48A64.1,64.1,0,0,0,136,64.52V40.37A88.13,88.13,0,0,1,215.63,120ZM120,120H80.68A48.09,48.09,0,0,1,120,80.68Zm0,16v39.32A48.09,48.09,0,0,1,80.68,136Zm16,0h39.32A48.09,48.09,0,0,1,136,175.32Zm0-16V80.68A48.09,48.09,0,0,1,175.32,120ZM120,40.37V64.52A64.1,64.1,0,0,0,64.52,120H40.37A88.13,88.13,0,0,1,120,40.37ZM40.37,136H64.52A64.1,64.1,0,0,0,120,191.48v24.15A88.13,88.13,0,0,1,40.37,136ZM136,215.63V191.48A64.1,64.1,0,0,0,191.48,136h24.15A88.13,88.13,0,0,1,136,215.63Z"},child:[]}]})(t)}function lr(t){return Se({attr:{viewBox:"0 0 256 256",fill:"currentColor"},child:[{tag:"path",attr:{d:"M48,180c0,11,7.18,20,16,20a14.24,14.24,0,0,0,10.22-4.66A8,8,0,0,1,85.78,206.4,30.06,30.06,0,0,1,64,216c-17.65,0-32-16.15-32-36s14.35-36,32-36a30.06,30.06,0,0,1,21.78,9.6,8,8,0,0,1-11.56,11.06A14.24,14.24,0,0,0,64,160C55.18,160,48,169,48,180Zm79.6-8.69c-4-1.16-8.14-2.35-10.45-3.84-1.25-.81-1.23-1-1.12-1.9a4.57,4.57,0,0,1,2-3.67c4.6-3.12,15.34-1.73,19.82-.56A8,8,0,0,0,142,145.86c-2.12-.55-21-5.22-32.84,2.76a20.58,20.58,0,0,0-9,14.95c-2,15.88,13.65,20.41,23,23.11,12.06,3.49,13.12,4.92,12.78,7.59-.31,2.41-1.26,3.34-2.14,3.93-4.6,3.06-15.17,1.56-19.55.36A8,8,0,0,0,109.94,214a61.34,61.34,0,0,0,15.19,2c5.82,0,12.3-1,17.49-4.46a20.82,20.82,0,0,0,9.19-15.23C154,179,137.49,174.17,127.6,171.31Zm83.09-26.84a8,8,0,0,0-10.23,4.84L188,184.21l-12.47-34.9a8,8,0,0,0-15.07,5.38l20,56a8,8,0,0,0,15.07,0l20-56A8,8,0,0,0,210.69,144.47ZM216,88v24a8,8,0,0,1-16,0V96H152a8,8,0,0,1-8-8V40H56v72a8,8,0,0,1-16,0V40A16,16,0,0,1,56,24h96a8,8,0,0,1,5.66,2.34l56,56A8,8,0,0,1,216,88Zm-27.31-8L160,51.31V80Z"},child:[]}]})(t)}function ws(t){return Se({attr:{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"},child:[{tag:"path",attr:{d:"M6 16m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0"},child:[]},{tag:"path",attr:{d:"M16 19m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"},child:[]},{tag:"path",attr:{d:"M14.5 7.5m-4.5 0a4.5 4.5 0 1 0 9 0a4.5 4.5 0 1 0 -9 0"},child:[]}]})(t)}function Ts(t){return Se({attr:{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"},child:[{tag:"path",attr:{d:"M4.387 3h15.226a1 1 0 0 1 .948 1.316l-5.105 15.316a2 2 0 0 1 -1.898 1.368h-3.116a2 2 0 0 1 -1.898 -1.368l-5.104 -15.316a1 1 0 0 1 .947 -1.316"},child:[]},{tag:"path",attr:{d:"M5 9h14"},child:[]},{tag:"path",attr:{d:"M7 15h10"},child:[]}]})(t)}function ks(t){return Se({attr:{viewBox:"0 0 24 24"},child:[{tag:"path",attr:{fill:"none",d:"M0 0h24v24H0z"},child:[]},{tag:"path",attr:{d:"m2 19.99 7.5-7.51 4 4 7.09-7.97L22 9.92l-8.5 9.56-4-4-6 6.01-1.5-1.5zm1.5-4.5 6-6.01 4 4L22 3.92l-1.41-1.41-7.09 7.97-4-4L2 13.99l1.5 1.5z"},child:[]}]})(t)}function Ds(t){return Se({attr:{version:"1.2",baseProfile:"tiny",viewBox:"0 0 24 24"},child:[{tag:"path",attr:{d:"M4 18c-.552 0-1-.448-1-1v-6.382l-.553.276c-.495.248-1.095.046-1.342-.447-.247-.494-.046-1.094.448-1.342l2-1c.31-.155.678-.139.973.044.294.183.474.504.474.851v8c0 .552-.448 1-1 1zM13 18h-5c-.404 0-.769-.244-.924-.617-.155-.374-.069-.804.217-1.09l4-4c.254-.254.394-.591.394-.95 0-.358-.14-.695-.394-.949-.508-.508-1.39-.508-1.9.001-.253.252-.393.589-.393.948 0 .552-.448 1-1 1s-1-.448-1-1c0-.894.348-1.733.98-2.364 1.265-1.263 3.464-1.263 4.727.001.632.631.979 1.471.979 2.363 0 .893-.348 1.733-.979 2.364l-2.293 2.293h2.586c.552 0 1 .448 1 1s-.448 1-1 1zM20.955 12.377c.338-.457.545-1.016.545-1.627 0-1.517-1.234-2.75-2.75-2.75-1.031 0-1.966.569-2.44 1.484-.254.49-.063 1.094.428 1.348.49.254 1.094.062 1.348-.428.128-.249.383-.404.664-.404.414 0 .75.336.75.75s-.336.75-.75.75c-.552 0-1 .448-1 1s.448 1 1 1c.689 0 1.25.561 1.25 1.25s-.561 1.25-1.25 1.25-1.25-.561-1.25-1.25c0-.552-.448-1-1-1s-1 .448-1 1c0 1.792 1.458 3.25 3.25 3.25s3.25-1.458 3.25-3.25c0-.939-.406-1.779-1.045-2.373z"},child:[]}]})(t)}function Bt({onChartTypeChange:t,iconClassName:a}){const{setCardType:n,setCardPreferences:r,setIsDevMode:l}=s.useEditorStore(h=>h.actions),{setCardCustomCfg:o,setCustomCardPreferences:c,setQueryConfig:i}=s.useEditorActions();function d(h,u){o(null),r({}),n(h),["custom","text","map"].includes(h)&&(l(!0),i(void 0)),t==null||t()}const m=1e3;return e.jsxs("div",{className:"grid grid-cols-5 place-items-center",children:[e.jsx(s.IconButton,{openDelay:m,tooltip:"Table",onClick:()=>d("table"),className:"h-10 p-4",children:e.jsx(Je,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Bar Chart",onClick:()=>d("bar"),className:"h-10 p-4",children:e.jsx($e,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Line Chart",onClick:()=>d("line"),className:"h-10 p-4",children:e.jsx(Ge,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Stacked Bar Chart",onClick:()=>d("stackedBar"),className:"h-10 p-4",children:e.jsx(ms,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Stacked Line Chart",onClick:()=>d("stackedLine"),className:"h-10 p-4",children:e.jsx(ks,{className:s.cn("size-5",a),strokeWidth:0})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Scatter Chart",onClick:()=>d("scatter"),className:"h-10 p-4",children:e.jsx(xs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Bubble Chart",onClick:()=>d("bubble"),className:"h-10 p-4",children:e.jsx(ws,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Pie Chart",onClick:()=>d("pie"),className:"h-10 p-4",children:e.jsx(He,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Doughnut Chart",onClick:()=>d("doughnut"),className:"h-10 p-4",children:e.jsx(ps,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Funnel Chart",onClick:()=>d("funnel"),className:"h-10 p-4",children:e.jsx(Ts,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Radar Chart",onClick:()=>d("radar"),className:"h-10 p-4",children:e.jsx(js,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Polar Area Chart",onClick:()=>d("polarArea"),className:"h-10 p-4",children:e.jsx(Ss,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Tornado Chart",onClick:()=>d("tornado"),className:"h-10 p-4",children:e.jsx(ys,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{tooltip:"Range Chart",onClick:()=>d("range"),className:"h-10 p-4",children:e.jsx(hs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"KPI",onClick:()=>d("kpi"),className:"h-10 p-4",children:e.jsx(Ds,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Text",onClick:()=>d("text"),className:"h-10 p-4",children:e.jsx(Zs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{tooltip:"Map",onClick:()=>d("map"),className:"h-10 p-4",children:e.jsx(gs,{className:s.cn("size-5",a)})}),e.jsx(s.IconButton,{openDelay:m,tooltip:"Custom",onClick:()=>d("custom"),className:"h-10 p-4",children:e.jsx(Ae,{className:s.cn("size-5",a)})})]})}function or({chartType:t,className:a,customIcon:n,componentName:r}){const l=Ye();switch(t){case"bar":return e.jsx($e,{className:a});case"line":return e.jsx(Ge,{className:a});case"pie":return e.jsx(He,{className:a});case"radar":return e.jsx(js,{className:a});case"scatter":return e.jsx(xs,{className:a});case"bubble":return e.jsx(ws,{className:a});case"doughnut":return e.jsx(ps,{className:a});case"stackedBar":return e.jsx(ms,{className:a});case"stackedLine":return e.jsx(ks,{className:a});case"polarArea":return e.jsx(Ss,{className:a});case"funnel":return e.jsx(Ts,{className:a});case"tornado":case"pyramid":return e.jsx(ys,{className:a});case"range":return e.jsx(hs,{className:a});case"kpi":return e.jsx(Ds,{className:a});case"text":return e.jsx(it,{className:a});case"map":return e.jsx(gs,{className:a});case"custom":return n&&l?e.jsx(l,{className:a}):r?r==null?void 0:r.charAt(0).toUpperCase():e.jsx(Ae,{className:a});default:return e.jsx(Je,{className:a})}}function Ft({variant:t="ghost"}){var o,c;const[a,n]=A.useState(!1),r=s.useEditorStore(i=>i.card),l=r.type;return s.useDashboardStore(i=>i.isVisualEditing),e.jsxs(s.Popover,{open:a,onOpenChange:n,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{size:"sm",variant:t||"ghost",children:e.jsx(or,{className:"size-4",chartType:l,customIcon:(o=r.customCardPreferences)==null?void 0:o.icon,componentName:(c=r.customCardPreferences)==null?void 0:c.componentName})})}),e.jsxs(s.PopoverContentCustom,{onOpenAutoFocus:i=>i.preventDefault(),className:"w-72 p-1",children:[e.jsx(Bt,{onChartTypeChange:()=>n(!1)}),e.jsx(ar,{onChartTypeChange:()=>n(!1)})]})]})}const Oe="focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0";function Js(t,a){var r,l,o;const n=s.getNumberAxis(t,{});return["pie","doughnut","polarArea","funnel"].includes(a)?"category":((o=(l=(r=t==null?void 0:t.chartOptions)==null?void 0:r.scales)==null?void 0:l[n])==null?void 0:o.type)||"linear"}function ir(){var L,F,P,_,z,Q;const[t,a]=A.useState(!1),n=s.useEditorStore(Y=>{var V;return(V=Y.card)==null?void 0:V.preferences}),r=s.useEditorStore(Y=>{var V;return(V=Y.card)==null?void 0:V.type}),l=s.getNumberAxis(n,{}),[o,c]=A.useState(Js(n,r)),[i,d]=A.useState(((P=(F=(L=n==null?void 0:n.chartOptions)==null?void 0:L.scales)==null?void 0:F[l])==null?void 0:P.min)||""),[m,h]=A.useState(((Q=(z=(_=n==null?void 0:n.chartOptions)==null?void 0:_.scales)==null?void 0:z[l])==null?void 0:Q.max)||""),[u,p]=A.useState(i||m?"custom":"auto"),[f,j]=A.useState(""),[x,T]=A.useState(0),[g,b]=A.useState(""),[C,O]=A.useState(""),[S,E]=A.useState(""),N=s.useEditorStore(Y=>Y.actions.setCardPreferences),{updateCardInFrame:D}=s.useEditorActions(),w=s.useEditorStore(Y=>Y.card),M=["pie","doughnut","polarArea","funnel"].includes(r);A.useEffect(()=>{var Y,V,q,$,U,H,W,G,Z,se,ne,ee,ae,J;c(Js(n,r)),d(((q=(V=(Y=n==null?void 0:n.chartOptions)==null?void 0:Y.scales)==null?void 0:V[l])==null?void 0:q.min)===0?0:""),h(((H=(U=($=n==null?void 0:n.chartOptions)==null?void 0:$.scales)==null?void 0:U[l])==null?void 0:H.max)||""),j(((se=(Z=(G=(W=n==null?void 0:n.chartOptions)==null?void 0:W.scales)==null?void 0:G[l])==null?void 0:Z.ticks)==null?void 0:se.stepSize)||""),T(((ne=n==null?void 0:n.numberAxisFormat)==null?void 0:ne.decimalPlaces)||0),b(((ee=n==null?void 0:n.numberAxisFormat)==null?void 0:ee.suffix)||""),O(((ae=n==null?void 0:n.numberAxisFormat)==null?void 0:ae.currency)||""),E(((J=n==null?void 0:n.numberAxisFormat)==null?void 0:J.locale)||"")},[t,n,l,r]);function k(Y,V){return V===""||/^[0-9\b]+$/.test(V)?Number(V):Number(Y)}function R(){const Y=[...new Set(s.LOCALE_CURRENCY_PAIRS.map(V=>V.currency))];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-6 items-center gap-2",children:[e.jsx(s.Label,{className:"col-span-2 hover:cursor-pointer hover:underline",htmlFor:"currency",children:e.jsx("a",{target:"_blank",href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat",children:"Currency"})}),e.jsxs(s.Select,{value:C||"none",onValueChange:V=>O(V==="none"?"":V),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Currency"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),Y.map(V=>e.jsx(s.SelectItem,{value:V,children:V},V))]})})]}),e.jsxs(s.Select,{value:S,onValueChange:V=>E(V),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Locale"}),s.LOCALE_CURRENCY_PAIRS.map(V=>e.jsx(s.SelectItem,{value:V.locale,children:V.locale},V.locale))]})})]})]}),e.jsxs("div",{className:"grid grid-cols-9 items-center gap-2",children:[e.jsx(s.Label,{className:"col-span-3",htmlFor:"format",children:"Decimals"}),e.jsx(s.Input,{value:x,onChange:V=>T(Number(V.target.value)),id:"format",className:`col-span-6 h-8 w-full tracking-wide ${Oe}`})]})]})}function I(){return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mt-4 space-y-2",children:e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the scale for number axis"})}),e.jsxs("div",{className:"mt-2 grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"scale",children:"Type"}),e.jsxs(s.Select,{disabled:M,value:o,onValueChange:Y=>c(Y),children:[e.jsx(s.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{id:"type",children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Option"}),e.jsx(s.SelectItem,{value:"linear",children:"Linear"}),e.jsx(s.SelectItem,{value:"logarithmic",children:"Logarithmic"}),e.jsx(s.SelectItem,{value:"category",children:"Category"})]})})]})]}),o=="linear"&&e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2 py-1",children:[e.jsx(s.Label,{htmlFor:"scale-option",children:"Setup"}),e.jsxs(s.RadioGroup,{disabled:M,id:"scale-option",value:u,onValueChange:Y=>p(Y),className:"col-span-2 flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"auto",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Auto"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"custom",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Custom"})]})]})]}),u==="custom"&&o=="linear"&&v()]})}function v(){return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"range",children:"Range"}),e.jsx(s.Input,{id:"min",value:i,onChange:Y=>d(V=>k(V,Y.target.value)),className:`col-span-1 h-8 w-full ${Oe}`}),e.jsx(s.Input,{value:m,onChange:Y=>h(V=>k(V,Y.target.value)),id:"max",className:`col-span-1 h-8 w-full ${Oe}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(s.Label,{htmlFor:"step-size",children:"Step Size"}),e.jsx(s.Input,{value:f,onChange:Y=>j(V=>k(V,Y.target.value)),id:"step-size",className:`col-span-1 h-8 w-full ${Oe}`})]})]})}function y(){var $;let Y={};u==="custom"&&(Y={min:i,max:m,ticks:{stepSize:f}});const V={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,scales:{...($=n==null?void 0:n.chartOptions)==null?void 0:$.scales,[l]:{type:o,...o==="linear"?Y:{}}}},numberAxisFormat:{decimalPlaces:x,suffix:g,currency:C,locale:S}};console.log(V),N(V);const q={...w,preferences:V};D(q)}return e.jsxs(s.Popover,{onOpenChange:Y=>a(Y),children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Format Number",children:e.jsx(Dn,{className:"size-4"})})}),e.jsx(s.PopoverContentCustom,{align:"end",className:"max-w-65 z-[51]",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Format Options"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the format for number axis"})]}),e.jsxs("div",{className:"grid gap-2",children:[R(),I(),e.jsx(s.SelectSeparator,{}),e.jsx("div",{className:"grid grid-cols-3",children:e.jsx(s.Button,{onClick:y,variant:"secondary",className:"col-span-1 col-start-3 h-8 w-full",children:"Apply"})})]})]})})]})}function cr(){const t=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.position}),a=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.align}),n=s.useEditorStore(i=>{var d,m,h,u;return(u=(h=(m=(d=i.card.preferences)==null?void 0:d.chartOptions)==null?void 0:m.plugins)==null?void 0:h.legend)==null?void 0:u.display}),{setLegendOptions:r}=s.useEditorActions(),l=i=>{r({display:i,position:t,align:a})},o=i=>{r({display:n,position:i,align:a})},c=i=>{r({display:n,position:t,align:i})};return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Legend Settings",children:e.jsx(Pn,{className:"size-4"})})}),e.jsx(s.PopoverContentCustom,{align:"end",className:"z-[51] w-72",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Legend Options"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Set the legend options for the chart."})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"displayLegend",children:"Display"}),e.jsx("div",{className:"col-span-2 flex h-8 items-center",children:e.jsx(s.SmallSwitch,{checked:n,onCheckedChange:l,id:"displayLegend"})})]}),n&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"position",children:"Position"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(s.IconButton,{tooltip:"Top",onClick:()=>o("top"),children:e.jsx(s.ArrowUp,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Left",onClick:()=>o("left"),children:e.jsx(hn,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Bottom",onClick:()=>o("bottom"),children:e.jsx(s.ArrowDown,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Right",onClick:()=>o("right"),children:e.jsx(xn,{className:"size-4"})})]})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(s.Label,{htmlFor:"align",children:"Align"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(s.IconButton,{tooltip:"Start",onClick:()=>c("start"),children:e.jsx(on,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"Center",onClick:()=>c("center"),children:e.jsx(ln,{className:"size-4"})}),e.jsx(s.IconButton,{tooltip:"End",onClick:()=>c("end"),children:e.jsx(cn,{className:"size-4"})})]})]})]})]})]})})]})}function dr(){const t=s.useEditorStore(o=>{var c,i;return(i=(c=o.card.preferences)==null?void 0:c.chartOptions)==null?void 0:i.indexAxis}),a=s.useEditorStore(o=>o.card.type),{setChartOrientation:n}=s.useEditorStore(o=>o.actions);function r(){return e.jsx(s.IconButton,{onClick:()=>{n(t==="x"||t===void 0?"y":"x")},tooltip:"Chart Orientation",children:t==="x"||t===void 0?e.jsx(_s,{className:"size-4"}):e.jsx(Ps,{className:"size-4"})})}function l(){return e.jsx(s.IconButton,{onClick:()=>{n(t==="y"||t===void 0?"x":"y")},tooltip:"Chart Orientation",children:t==="y"||t===void 0?e.jsx(_s,{className:"size-4"}):e.jsx(Ps,{className:"size-4"})})}return e.jsx(e.Fragment,{children:a==="funnel"?l():r()})}function zt(){const t=s.useEditorStore(c=>c.card),a=s.useEditorStore(c=>c.isShowingVisual),n=s.useEditorStore(c=>{var i,d;return(d=(i=c.card)==null?void 0:i.preferences)==null?void 0:d.chartOptions}),r=s.getChartGridStyle(n),{setChartGrid:l}=s.useEditorActions();function o(){const c=["xy","none","x","y"],d=(c.indexOf(r)+1)%c.length;l(c[d])}return a?e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel"].includes(t.type)&&e.jsx(e.Fragment,{children:e.jsx(dr,{})}),!["kpi","text","custom","table"].includes(t.type)&&e.jsxs(e.Fragment,{children:[e.jsx(ir,{}),e.jsx(cr,{})]}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(t.type)&&e.jsxs(s.IconButton,{tooltip:"Grid Style",onClick:o,children:[(r==="xy"||!r)&&e.jsx(Bn,{className:"size-4"}),r==="x"&&e.jsx(wn,{className:"size-4"}),r==="y"&&e.jsx(Qn,{className:"size-4"}),r==="none"&&e.jsx(ta,{className:"size-4"})]})]}):null}function ur(){const t=s.useEditorStore(j=>j.frame),a=s.useEditorStore(j=>j.card),{setFrame:n,setActiveTabCardId:r,setCard:l,updateCardInFrame:o}=s.useEditorActions(),c=(j,x)=>{j.dataTransfer.setData("tabIndex",x.toString())},i=j=>{j.preventDefault()},d=(j,x)=>{const T=j.dataTransfer.getData("tabIndex");if(T===x.toString())return;const g=Array.from(t.cards),[b]=g.splice(parseInt(T),1);g.splice(x,0,b),n({...t,cards:g})};function m(j){const x=t.cards.filter(b=>b.id!==j),T=x[0],g={...t,cards:x,activeCardId:T.id};n(g),r(T.id),l(T)}function h(j,x){const T={...t,cards:[...t.cards,x],activeCardId:j.id};n(T),r(x.id)}function u(){const j={...a,id:s.v4(),title:`${a.title} Copy`,tabTitle:`${a.tabTitle||a.title} Copy`};h(a,j)}function p(j){const x={dataSource:j.dataSource,connectionId:j.connectionId,lastSelectedDatabase:j.lastSelectedDatabase,lastSelectedSchema:j.lastSelectedSchema,lastSelectedTable:j.lastSelectedTable,lastSelectedDatamodelId:j.lastSelectedDatamodelId,id:s.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[],mode:"explorer"};h(j,x)}function f(j,x){return j.cards.length===1&&(x.displayTab===!0||x.displayTab===void 0)||j.activeCardId!==x.id&&(x.displayTab===!0||x.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const T=x.displayTab===void 0?!1:!x.displayTab;l({...x,displayTab:T}),o({...x,displayTab:T})},children:x.displayTab===!0||x.displayTab===void 0?e.jsx(s.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(En,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}return e.jsx(s.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[t.cards.map((j,x)=>e.jsxs(s.TabsTrigger,{draggable:!0,onDragStart:T=>c(T,x),onDragOver:i,onDrop:T=>d(T,x),className:"",value:j.id,children:[f(t,j),e.jsx(s.Editable,{enabled:t.activeCardId===j.id,text:j.tabTitle||j.title,onSave:T=>{l({...a,tabTitle:T}),o({...a,tabTitle:T})},children:j.tabTitle||j.title},j.id),t.activeCardId===j.id&&t.cards.length>1&&e.jsx(s.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>m(j.id)})]},j.id)),e.jsx(s.Button,{className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",variant:"outline",onClick:()=>p(a),children:e.jsx(s.Plus,{className:"h-4 w-4"})}),e.jsx(s.Button,{className:"invisible ml-1 h-8 px-2 hover:bg-muted-foreground/50 group-hover:visible",variant:"outline",onClick:u,children:e.jsx(kn,{className:"h-4 w-4"})})]})})}function Pt(){const t=s.useEditorStore(x=>x.frame),a=s.useEditorStore(x=>x.card),n=s.useEditorStore(x=>x.isSqlRunning),r=s.useEditorStore(x=>x.isDevMode),{setFrame:l,setActiveTabCardId:o,setCard:c,setIsDevMode:i,setRunSql:d}=s.useEditorActions(),{data:m,isLoading:h,isFetching:u}=s.useDashboardCardQuery(a);function p(x){const T=t.cards.find(g=>g.id===x);if(T!=null&&T.sql){r||i(!0),o(x);return}if(T!=null&&T.queryConfig){r&&i(!1),f(x),o(x);return}r||f(x),o(x)}function f(x){const T={...t,cards:t.cards.map(b=>b.id===a.id?a:b),activeCardId:x};l(T);const g=T.cards.find(b=>b.id===x);g.sql&&d(!0),c(g)}function j(){return a.type==="kpi"?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.KPICard,{card:a,data:m==null?void 0:m.records,isPending:h})})}):a.type==="text"?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(s.TextCard,{isLoading:n,card:a,data:m==null?void 0:m.records})})})}):e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackEditorCard,children:e.jsx(Rt,{className:"grow border-none shadow-none"})})}return e.jsxs(s.Tabs,{onValueChange:p,value:t.activeCardId,className:"flex grow flex-col",children:[t.cards.map(x=>e.jsx(s.TabsContent,{className:"mt-0 grow",value:x.id,children:j()},x.id)),e.jsx(ur,{})]})}function Re({children:t}){s.useEditorStore(b=>b.frame);const a=s.useEditorStore(b=>b.card),n=s.useEditorStore(b=>b.isDevMode),r=s.useEditorStore(b=>b.isShowingVisual),l=s.useDashboardStore(b=>b.isVisualEditing),o=s.useEditorStore(b=>b.pythonStdOut);s.useEditorStore(b=>b.actions.setRunSql);const{setFrame:c,setCard:i,setActiveTabCardId:d,runAssistant:m,setIsDevMode:h}=s.useEditorActions(),{data:u,isLoading:p,isFetching:f,errorMessage:j}=s.useDashboardCardQuery(a);function x(){const b=JSON.stringify(u==null?void 0:u.records,null,2),C=new Blob([b],{type:"application/json"}),O=URL.createObjectURL(C),S=document.createElement("a");S.href=O,S.download=`${a.title}.json`,S.click()}function T(){const C=[Object.keys(u==null?void 0:u.records.reduce((N,D)=>({...N,...D}),{})).join(","),...((u==null?void 0:u.records)||[]).map(N=>Object.values(N).join(","))].join(`
414
+ `),O=new Blob([C],{type:"text/csv"}),S=URL.createObjectURL(O),E=document.createElement("a");E.href=S,E.download=`${a.title}.csv`,E.click()}function g(){const b=`Can you get the documentation for ${a.type} chart?`;m(b)}return a?e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[(p||f)&&e.jsx("div",{className:"absolute bottom-0 left-0 right-0 top-0 z-50 m-auto flex h-10 w-10 items-center justify-center rounded-md bg-muted",children:e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-muted-foreground"})}),e.jsxs("div",{className:"z-10 mb-0 flex min-h-10 items-center justify-end gap-2 px-6",children:[r&&e.jsxs("div",{className:"flex grow items-center",children:[e.jsx(Ft,{}),l&&e.jsx(s.IconButton,{tooltip:"Visual Docs",onClick:g,children:e.jsx(jn,{className:"size-4"})}),!["table","custom"].includes(a.type)&&l&&e.jsx(nr,{})]}),!r&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:T,children:e.jsx(lr,{className:"size-4"})}),e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:x,children:e.jsx(An,{strokeWidth:1.5,className:"size-4"})}),e.jsx(s.CopyButton,{text:JSON.stringify(u==null?void 0:u.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[t,e.jsx(zt,{})]}),n&&e.jsx(hr,{})]}),r?e.jsx(Pt,{}):e.jsxs(s.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[o&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:o&&e.jsx(mr,{})}),e.jsx(s.ResizableHandle,{})]}),e.jsxs(s.ResizablePanel,{id:"2",order:2,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:[(u==null?void 0:u.records)&&(u==null?void 0:u.records.length)>0&&e.jsx(Xa,{data:(u==null?void 0:u.records)||[]}),j&&e.jsx(Et,{error:j})]})]})]}):e.jsx(e.Fragment,{children:"No Active Card"})}function mr(){const t=s.useEditorStore(n=>n.pythonStdOut);if(!t)return null;const a=t.replace(/\n/g,"<br>");return e.jsxs("div",{className:"relative flex w-full flex-col border-r",children:[e.jsx("div",{className:"sticky top-0 flex max-h-11 min-h-11 grow items-center border-b bg-muted px-3 font-mono text-xs font-semibold text-foreground/70",children:"Python Output"}),e.jsxs(s.ScrollArea,{className:"overflow-y-auto px-2 pt-1 font-mono text-xs text-foreground/90",children:[e.jsx("div",{dangerouslySetInnerHTML:{__html:a}}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function hr(){const t=s.useEditorStore(n=>n.isShowingVisual),a=s.useEditorStore(n=>n.actions.setIsShowingVisual);return e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Label,{htmlFor:"visualize",children:"Chart"}),e.jsx(s.SmallSwitch,{checked:t,onCheckedChange:()=>a(!t),id:"visualize",className:"data-[state=checked]:bg-foreground/50"})]})}function xr(){var x,T;const t=s.useEditorStore(g=>g.card.customCardPreferences),{data:a}=Cs(),n=(a==null?void 0:a.plugins)||[],r=s.useEditorStore(g=>g.card),l=s.useEditorStore(g=>g.frame),o=(t==null?void 0:t.visualType)||"single",{setCustomCardPreferences:c}=s.useEditorActions(),{components:i,manifest:d}=s.useCustomVisual((t==null?void 0:t.url)||""),m=(x=d==null?void 0:d.visuals)==null?void 0:x.find(g=>g.name===(t==null?void 0:t.componentName));function h(g,b){if(!t)return;const O={...(t==null?void 0:t.dataInputCardIds)||{},[g]:{cardId:b,hookRef:null}};c({...t,dataInputCardIds:O})}function u(g){var C,O;const b=(C=l==null?void 0:l.cards)==null?void 0:C.filter(S=>{var E;return((E=S==null?void 0:S.customCardPreferences)==null?void 0:E.visualType)!=="multiple"}).map(S=>({value:S.id,label:S.tabTitle||S.title}));return e.jsx("div",{className:"space-y-2",children:(O=g==null?void 0:g.dataInputs)==null?void 0:O.map((S,E)=>{var N,D;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:S}),e.jsx(ce,{className:"col-span-6 h-8 w-full justify-between",label:"",options:b||[],value:((D=(N=t==null?void 0:t.dataInputCardIds)==null?void 0:N[E])==null?void 0:D.cardId)||"",onValueChange:w=>h(E,w)})]},E+Math.random())})})}function p(g,b,C=[],O=""){var S,E;return t?g==="input"?e.jsx(s.Input,{className:`h-8 w-full ${s.removeRing}`,value:((S=t==null?void 0:t.settings)==null?void 0:S[b])||O,onChange:N=>{c({...t,settings:{...t==null?void 0:t.settings,[b]:N.target.value}})}},b):g==="select"?e.jsx(ce,{className:"h-8 w-full",label:"",options:C,value:((E=t==null?void 0:t.settings)==null?void 0:E[b])||O,onValueChange:N=>{c({...t,settings:{...t==null?void 0:t.settings,[b]:N}})}}):null:null}function f(g){const b=Object.entries((g==null?void 0:g.settings)||{});return e.jsx("div",{className:"space-y-3",children:b.map(([C,O])=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:O.title}),p(O.ui,C,O.options,O.defaultValue)]},C))})}function j(g){t&&c({...t,visualType:g,componentName:""})}return e.jsxs(s.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"px-6 py-2",children:[e.jsxs(s.AccordionItem,{value:"plugin-settings",children:[e.jsx(s.AccordionTrigger,{children:"Plugin"}),e.jsx(s.AccordionContent,{children:e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"space-y-4",children:[e.jsx(ce,{icon:e.jsx(gn,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:n,value:(t==null?void 0:t.url)||"",onValueChange:g=>c({...t||{url:"",componentName:""},url:g})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&e.jsx(s.Tabs,{value:o,onValueChange:j,children:e.jsxs(s.TabsList,{className:"h-9 w-full",children:[e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&t&&e.jsx(e.Fragment,{children:i&&e.jsx(ce,{icon:e.jsx(Tn,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(i==null?void 0:i.filter(g=>g.type===o).map(g=>({value:g.name,label:g.name})))||[],value:t.componentName,onValueChange:g=>{console.log("changing value",g),c({...t,componentName:g})}})})]})})]}),((T=r==null?void 0:r.customCardPreferences)==null?void 0:T.componentName)&&e.jsxs(s.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{children:[m&&m.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:u(m)})]}),m&&m.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:f(m)})})]})]})]})}function we(){const[t,a]=A.useState([]),n=s.useEditorStore(m=>m.card.preferences),{setFilterOnClickColumnIndex:r,setCardPreferences:l}=s.useEditorStore(m=>m.actions),o=s.useEditorStore(m=>m.queryResultColumns);s.useEditorStore(m=>{var h;return(h=m.card.preferences)==null?void 0:h.filterOnClickColumnIndex}),A.useEffect(()=>{o&&a(new Array(o.length).fill(!1))},[o]);function c(m,h){var u,p;h?n!=null&&n.onClickFilter&&((u=n==null?void 0:n.onClickFilter)==null?void 0:u.length)>0?l({...n,onClickFilter:[...n.onClickFilter,{columnIndex:m,expression:""}]}):l({...n,onClickFilter:[{columnIndex:m,expression:""}]}):(l({...n,onClickFilter:(p=n==null?void 0:n.onClickFilter)==null?void 0:p.filter(f=>f.columnIndex!==m)}),a(f=>{const j=[...f];return j[m]=!1,j}))}function i(m,h){var p;const u=(p=n==null?void 0:n.onClickFilter)==null?void 0:p.map(f=>f.columnIndex===m?{...f,expression:h.target.value}:f);l({...n,onClickFilter:u})}function d(m){var h,u,p,f;if(t!=null&&t[m]||(u=(h=n==null?void 0:n.onClickFilter)==null?void 0:h.find(j=>j.columnIndex===m))!=null&&u.expression)return e.jsx(s.Input,{value:(f=(p=n==null?void 0:n.onClickFilter)==null?void 0:p.find(j=>j.columnIndex===m))==null?void 0:f.expression,onChange:j=>i(m,j),type:"text",className:"h-9 w-full font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"Expression"})}return e.jsxs("div",{className:"max-h-[150px] space-y-2 overflow-y-auto",children:[e.jsx(s.Label,{children:"On Click"}),o==null?void 0:o.map((m,h)=>{var u,p;return e.jsx("div",{className:"group flex items-center justify-between space-y-2",children:e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex items-center space-x-2 py-2",children:[e.jsx(s.Checkbox,{checked:!!((u=n==null?void 0:n.onClickFilter)!=null&&u.find(f=>f.columnIndex===h)),onCheckedChange:f=>c(h,f)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:m}),e.jsx("div",{className:"flex items-center gap-2",children:((p=n==null?void 0:n.onClickFilter)==null?void 0:p.find(f=>f.columnIndex===h))&&e.jsx(s.Pencil,{onClick:()=>{a(f=>{const j=[...f];return j[h]=!f[h],j})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),d(h)]})},m)})]})}function pr({idx:t}){var i;const a=s.useEditorStore(d=>{var m;return(m=d.card)==null?void 0:m.preferences}),n=s.useEditorStore(d=>{var m;return(m=d.card.preferences)==null?void 0:m.datasetOptions}),r=s.useEditorStore(d=>d.actions.setCardPreferences),l=(i=a==null?void 0:a.chartOptions)==null?void 0:i.indexAxis,o=n==null?void 0:n.find(d=>d.idx===t);function c(d,m){(d==="end"||d==="start")&&s.getContrastColor(s.getCanvasBackgroundColor());const h={display:d!=="none",align:d,anchor:d,clamp:!0};if(!o)r({...a,datasetOptions:[...n||[],{idx:m,datalabels:h}]});else{const u={...o};u.datalabels=h;const p=n==null?void 0:n.map(f=>f.idx===m?u:f);r({...a,datasetOptions:p})}}return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{children:e.jsx(lt,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>c("none",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(s.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>c("start",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(_n,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})}),e.jsx("button",{onClick:()=>c("center",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Un,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})}),e.jsx("button",{onClick:()=>c("end",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Vn,{className:s.cn("h-4 w-4",{"rotate-90":l==="y"})})})]})})]})}function fr(){const t=s.useEditorStore(o=>o.card),{data:a}=s.useDashboardCardQuery(t),{keys:n}=s.getKeys((a==null?void 0:a.records)||[]),r=n.slice(1).filter(o=>!s.METADATA_KEYS.includes(o));let l=r;if(["stackedBar","stackedLine"].includes(t.type)){const o=s.pivotData({dataArray:(a==null?void 0:a.records)||[],groupKey:n[0],pivotKey:n[1],valueKey:n[2]}),{keys:c}=s.getKeys(o);l=c.slice(1)}return{transformedMetricKeys:l,dimensionKeys:n.slice(0,1),keys:n,defaultMetricKeys:r}}function Es({children:t,className:a,delayDuration:n}){return e.jsx(s.TooltipProvider$1,{children:e.jsxs(s.Tooltip,{delayDuration:n,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:s.cn("overflow-hidden text-ellipsis whitespace-nowrap",a),children:t})}),e.jsx(s.TooltipContent,{children:t})]})})}function _t(){const t=s.useEditorStore(d=>d.card),a=s.useEditorStore(d=>{var m,h,u;return(u=(h=(m=d==null?void 0:d.card)==null?void 0:m.preferences)==null?void 0:h.chartOptions)==null?void 0:u.indexAxis}),{data:n}=s.useDashboardCardQuery(t),r=a==="y"?"Metric":"Dimension",l=a==="y"?"Dimension":"Metric",{dimensionKeys:o,transformedMetricKeys:c,keys:i}=fr();return i.length===0?null:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:r}),e.jsx("div",{className:"max-h-[125px] space-y-1 overflow-y-auto",children:o.map(d=>e.jsx("p",{className:"flex h-8 items-center rounded-md bg-muted/70 px-3 hover:bg-muted",title:d,children:Vt(d)},d))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:l}),e.jsx("div",{className:"my-1 max-h-[125px] space-y-1 overflow-y-auto",children:c.map((d,m)=>e.jsx(Ut,{className:"flex h-8 items-center bg-muted/70 px-3 hover:bg-muted",iconClassName:"invisible group-hover:visible",col:d,idx:m},d))})]})]})}function Vt(t,a=30){return t.length>a?t.slice(0,a)+"...":t}function Ut({col:t,idx:a,className:n,iconClassName:r,children:l}){const o=s.useEditorStore(h=>{var u;return(u=h.card)==null?void 0:u.preferences}),c=s.useEditorStore(h=>{var u,p;return(p=(u=h.card)==null?void 0:u.preferences)==null?void 0:p.datasetOptions}),i=c==null?void 0:c.find(h=>h.idx===a),{setCardPreferences:d}=s.useEditorStore(h=>h.actions);function m(h){if(!i)d({...o,datasetOptions:[...c||[],{idx:a,type:h==="area"?"line":h,fill:h==="area"?"origin":""}]});else{const u={...i};u.type=h==="area"?"line":h,u.fill=h==="area"?"origin":"";const p=c==null?void 0:c.map(f=>f.idx===a?u:f);d({...o,datasetOptions:p})}}return e.jsxs("div",{className:s.cn("group flex items-center justify-between gap-2 rounded-md",n),children:[l||e.jsx(Es,{className:"max-w-[100px]",title:t,children:Vt(t)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx($e,{onClick:()=>m("bar"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Ge,{onClick:()=>m("line"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(et,{onClick:()=>m("area"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(pr,{idx:a})]})]})}function qt(){const t=s.useEditorStore(f=>f.selectedConnectionId),a=s.useEditorStore(f=>f.selectedDatamodelId),n=s.useEditorStore(f=>f.selectedDatabaseName),r=s.useEditorStore(f=>f.selectedSchemaName),l=s.useEditorStore(f=>f.selectedTableName),o=s.useDashboardStore(f=>f.dashboard.filters)||[],c=s.useDashboardStore(f=>f.actions.addFilter),i=s.useDashboardStore(f=>f.actions.removeFilter),{removeFilterValues:d}=s.useDashboardActions(),{modelItems:m,tableName:h,connectionType:u}=s.useEditorAside();function p(f,j="dashboard"){var N;const x=s.fmt(f.column_name),T=s.fmt(n||""),g=s.fmt(r||""),b=s.fmt(h);let C=s.getQualifiedTableName({schemaName:g,tableName:b,connectionType:u,databaseName:T}),O=`${C}.${x}`;const S=(N=m==null?void 0:m.find(D=>D.id===a))==null?void 0:N.name;a&&a!=="none"&&(O=`${s.DATAMODEL_NAMESPCACE}.${S}.${x}`,C=`${s.DATAMODEL_NAMESPCACE}.${S}`);const E=o==null?void 0:o.find(D=>D.column===O&&D.table===b&&(D.database===T||D.database===n));if(E)d(E.id),i(E.id);else{const D=s.v4();return h==="api"&&t?(c({location:j,id:D,column:O,title:f.column_name,dataType:f.data_type,table:"api",database:"",connectionId:t,operation:"in",sql:s.getDefaultFilterSql(f.data_type,C,x)}),D):(c({id:D,location:j,column:O,title:f.column_name,dataType:f.data_type,table:l||"",database:n||"",connectionId:t||"",operation:"in",sql:s.getDefaultFilterSql(f.data_type,C,x)}),D)}}return{assignFilter:p}}function Ee(){const t=s.useDashboardStore(i=>i.dashboard.filters),a=s.useEditorStore(i=>i.frame),{setFrame:n}=s.useEditorActions(),{columns:r}=s.useEditorAside(),{assignFilter:l}=qt();function o(i){const d=l(i,"frame");n({...a,filterId:d})}const c=t==null?void 0:t.find(i=>i.id===a.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Card Filter"}),e.jsx(gr,{onSelect:o,columns:r}),c&&e.jsx(s.FilterHeader,{filter:c})]})}function gr({columns:t,onSelect:a}){var c;const[n,r]=ye.useState(!1),[l,o]=ye.useState("");return e.jsxs(s.Popover,{open:n,onOpenChange:r,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":n,className:"w-full justify-between font-normal",children:[l?(c=t.find(i=>i.column_name===l))==null?void 0:c.column_name:"Select Column...",e.jsx(s.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No framework found."}),e.jsx(s.CommandGroup,{children:t==null?void 0:t.map(i=>e.jsxs(s.CommandItem,{value:i.column_name,onSelect:d=>{const m=d===l?"":d;o(m),r(!1),m&&a(i)},children:[i.column_name,e.jsx(s.CheckIcon,{className:s.cn("ml-auto h-4 w-4",l===i.column_name?"opacity-100":"opacity-0")})]},i.column_name))})]})]})})]})}function jr({cardType:t}){return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} Chart`}),e.jsxs(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT x-axis, y-axis-1, y-axis-2,... FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"x-axis - category"}),e.jsx("li",{children:"y-axis1 - number"}),e.jsx("li",{children:"y-axis2 - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:a=>a.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/bar-line-area",children:"example"})," in the docs."]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsx(s.AccordionContent,{className:"space-y-4 px-2",children:e.jsx(_t,{})})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ke(){const t=s.useEditorStore(i=>i.card.type),a=s.useEditorStore(i=>i.isDevMode),n=s.useEditorStore(i=>i.card.preferences),{setCardPreferences:r}=s.useEditorStore(i=>i.actions);function l(i){var h;const d={datalabels:{display:i!=="none",align:i,anchor:i,clamp:!0}},m={...n,chartOptions:{...n==null?void 0:n.chartOptions,plugins:{...(h=n==null?void 0:n.chartOptions)==null?void 0:h.plugins,...d}}};r(m)}function o(){var i,d,m;return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(s.Select,{value:((m=(d=(i=n==null?void 0:n.chartOptions)==null?void 0:i.plugins)==null?void 0:d.datalabels)==null?void 0:m.anchor)||"none",onValueChange:l,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Data Labels"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"start",children:"Start"}),e.jsx(s.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"end",children:"End"})]})})]})]})}function c(){var i,d,m;return e.jsx("div",{children:e.jsxs(s.Select,{value:((m=(d=(i=n==null?void 0:n.chartOptions)==null?void 0:i.plugins)==null?void 0:d.datalabels)==null?void 0:m.anchor)||"none",onValueChange:l,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"start",children:"Start"}),e.jsx(s.SelectItem,{className:"text-xs",value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"end",children:"End"})]})})]})})}return e.jsx(e.Fragment,{children:a?o():c()})}function yr({cardType:t}){function a(n){return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:n==="bubble"?"SELECT label, [group], x-axis, y-axis, radius FROM table":"SELECT label, [group], x-axis, y-axis FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"label - category"}),e.jsx("li",{children:"group (optional) - category"}),e.jsx("li",{children:"x-axis - number"}),e.jsx("li",{children:"y-axis - number"}),n==="bubble"&&e.jsx("li",{children:"radius - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:o=>o.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/bubble",children:"example"})," in the docs."]})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} Chart`}),e.jsx(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:a(t)})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsx(s.AccordionContent,{className:"px-2",children:e.jsx(Ke,{})})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}const vr={datasets:[{type:"line",label:"Sum_sales",data:[]},{type:"bar",label:"Sum_profit",data:[]}]},br={datasets:[{fill:"origin",type:"line",label:"Sum_sales",data:[]}]};function Nr(){const t=s.useEditorStore(a=>a.card.type);return e.jsxs("div",{className:"space-y-6 p-6",children:[e.jsx(s.Accordion,{className:" ",type:"single",collapsible:!0,children:Cr(t)}),!["kpi"].includes(t)&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(Ke,{}),e.jsx(we,{}),e.jsx(Ee,{})]})]})}function Cr(t){if(["line","bar"].includes(t))return e.jsxs(e.Fragment,{children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsx("p",{children:"Chart Query"})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT x-axis, y-axis-1, y-axis-2, y-axis-3,... FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"X-axis - Category (ex. city)"}),e.jsx("li",{children:"Y-axis - Number (ex. population)"})]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:"Mixed Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-2 text-xs",children:[e.jsxs("p",{children:["Click ",e.jsx("b",{children:"Customize"})," and change the `type` property in the dataset."]}),e.jsx("pre",{children:JSON.stringify(vr,null,2)})]})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:"Area Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-2 text-xs",children:[e.jsxs("p",{children:["Click ",e.jsx("b",{children:"Customize"})," and add `fill` property to the dataset. See",e.jsx("a",{className:"mx-1 text-blue-500 hover:underline",href:"https://www.chartjs.org/docs/latest/samples/area/line-boundaries.html",target:"_blank",children:"docs"}),"for more options."]}),e.jsx("pre",{children:JSON.stringify(br,null,2)})]})]})]});if(t==="bubble")return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:t})," chart query?"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, x-axis, y-axis, radius FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Bubble Group (opt) - Category (ex. state)"}),e.jsx("li",{children:"X-axis - Number (ex. avg. age)"}),e.jsx("li",{children:"Y-axis - Number (ex. avg. weight)"}),e.jsx("li",{children:"Radius - Number (ex. population)"})]})]})]})});if(t==="scatter")return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:t})," chart query?"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, x-axis, y-axis FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Group (opt) - Category (ex. state)"}),e.jsx("li",{children:"X-axis - Number (ex. avg. age)"}),e.jsx("li",{children:"Y-axis - Number (ex. avg. weight)"})]})]})]})});if(["pie","doughnut","polarArea","funnel"].includes(t))return e.jsx(e.Fragment,{children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:[e.jsx("span",{children:t==="polarArea"?"Polar":t.charAt(0).toUpperCase()+t.slice(1)})," ","chart query"]})}),e.jsxs(s.AccordionContent,{className:" ",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs font-semibold",children:"SELECT label, measure FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs font-normal [&>li]:mt-2",children:[e.jsx("li",{children:"Label - Category (ex. city)"}),e.jsx("li",{children:"Measure - Number (ex. population)"})]})]})]})})}function Sr(){return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{type:"single",collapsible:!0,children:e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{className:"",children:"KPI Query"}),e.jsxs(s.AccordionContent,{className:"font-mono",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 font-mono text-xs",children:"SELECT current, previous FROM table"}),e.jsxs("ul",{className:"my-6 ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"current - number"}),e.jsx("li",{children:"previous - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/kpi",children:"example"})," in the docs."]})]})]})}),e.jsx("section",{className:"",children:e.jsx(s.FormatNumberSetting,{})}),e.jsx("div",{}),e.jsx("div",{})]})}function ns({children:t,summary:a,open:n,className:r}){return e.jsxs("details",{open:n,className:s.cn("space-y-4 [&_summary>svg]:open:-rotate-180",r),children:[e.jsxs("summary",{className:"flex cursor-pointer list-none items-center justify-between gap-4",children:[e.jsx(s.Label,{children:a}),e.jsx(s.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:t})]})}const wr=[{label:"Mercator",value:"mercator"},{label:"Azimuthal Equidistant",value:"azimuthalEquidistant"},{label:"Gnomonic",value:"gnomonic"},{label:"Orthographic",value:"orthographic"},{label:"Stereographic",value:"stereographic"},{label:"Equal Earth",value:"equalEarth"},{label:"Albers",value:"albers"},{label:"Albers USA",value:"albersUsa"},{label:"Conic Conformal",value:"conicConformal"},{label:"Conic Equal Area",value:"conicEqualArea"},{label:"Conic Equidistant",value:"conicEquidistant"},{label:"Equirectangular",value:"equirectangular"},{label:"Transverse Mercator",value:"transverseMercator"},{label:"Natural Earth 1",value:"naturalEarth1"}],Tr=["blues","brBG","buGn","buPu","cividis","cool","cubehelixDefault","gnBu","greens","greys","inferno","magma","orRd","oranges","pRGn","piYG","plasma","puBu","puBuGn","puOr","puRd","purples","rainbow","rdBu","rdGy","rdPu","rdYlBu","rdYlGn","reds","sinebow","spectral","turbo","viridis","warm","ylGn","ylGnBu","ylOrBr","ylOrRd"],kr=Tr.map(t=>({label:t,value:t}));function Dr(){var d,m,h,u,p,f,j,x,T,g,b,C,O,S,E,N,D,w;const t=s.useEditorStore(M=>M.card),a=(m=(d=t==null?void 0:t.preferences)==null?void 0:d.mapVisualOptions)==null?void 0:m.topoJsonUrl,[n,r]=A.useState(((u=(h=t==null?void 0:t.preferences)==null?void 0:h.mapVisualOptions)==null?void 0:u.customTopoJsonUrl)||""),{setCardPreferences:l}=s.useEditorActions(),{data:o}=s.useTopoJson(t),c=Object.keys((o==null?void 0:o.objects)||{}).map(M=>({label:M,value:M}))||[],i=[{label:"US States 10m",value:"https://unpkg.com/us-atlas/states-10m.json"},{label:"Countries 50m",value:"https://unpkg.com/world-atlas/countries-50m.json"},{label:"Custom",value:"custom"}];return e.jsxs("div",{className:"p-6",children:[e.jsx(ns,{summary:"Map Chart",children:e.jsx("p",{className:"rounded-md bg-muted p-2 font-mono text-xs",children:"SELECT country, value FROM table"})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ns,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(ce,{className:"h-9",options:i,onValueChange:M=>{console.log("value",M),l({...t.preferences,mapVisualOptions:{topoJsonUrl:M}})},value:a||"",label:"Select Map"}),a==="custom"&&e.jsx(s.Input,{onChange:M=>{r(M.target.value)},onBlur:()=>{var M;console.log("onBlur",n),l({...t.preferences,mapVisualOptions:{...(M=t.preferences)==null?void 0:M.mapVisualOptions,customTopoJsonUrl:n}})},value:n,className:`h-9 w-full ${s.removeRing}`,placeholder:"Custom URL"}),o&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Map Object"}),e.jsx(ce,{className:"h-9",options:c,value:((f=(p=t.preferences)==null?void 0:p.mapVisualOptions)==null?void 0:f.objectKey)||"",label:"Map Object",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,objectKey:M}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Projection"}),e.jsx(ce,{className:"h-9",options:wr,value:((x=(j=t.preferences)==null?void 0:j.mapVisualOptions)==null?void 0:x.projection)||"",label:"Projection",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projection:M}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Colors"}),e.jsx(ce,{className:"h-9",options:kr,value:((g=(T=t.preferences)==null?void 0:T.mapVisualOptions)==null?void 0:g.colorScale)||"",label:"Select Color Scale",onValueChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,colorScale:M}})}})]})]})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ns,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:.1,value:((C=(b=t.preferences)==null?void 0:b.mapVisualOptions)==null?void 0:C.projectionScale)||1,onChange:M=>{var k;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionScale:Number(M.target.value)}})}}),e.jsx(s.Label,{children:"X"}),e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((E=(S=(O=t.preferences)==null?void 0:O.mapVisualOptions)==null?void 0:S.projectionOffset)==null?void 0:E[0])||0,onChange:M=>{var k,R,I,v;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionOffset:[Number(M.target.value),((v=(I=(R=t.preferences)==null?void 0:R.mapVisualOptions)==null?void 0:I.projectionOffset)==null?void 0:v[1])||0]}})}}),e.jsx(s.Label,{children:"Y"}),e.jsx(s.Input,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((w=(D=(N=t.preferences)==null?void 0:N.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:w[1])||0,onChange:M=>{var k,R,I,v;l({...t.preferences,mapVisualOptions:{...(k=t.preferences)==null?void 0:k.mapVisualOptions,projectionOffset:[((v=(I=(R=t.preferences)==null?void 0:R.mapVisualOptions)==null?void 0:I.projectionOffset)==null?void 0:v[0])||0,Number(M.target.value)]}})}})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(we,{})]})}function Er(){var r;const t=s.useEditorStore(l=>l.card.preferences),{setCardPreferences:a}=s.useEditorStore(l=>l.actions);function n(l){a({...t,textVisualOptions:{...t==null?void 0:t.textVisualOptions,isDynamicText:l==="dynamic"}})}return e.jsxs(s.RadioGroup,{value:(r=t==null?void 0:t.textVisualOptions)!=null&&r.isDynamicText?"dynamic":"static",onValueChange:n,className:"flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"static",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Static"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function Be({cardType:t,docContent:a,suffix:n="Chart"}){let r="pie-donut-polar";return t==="funnel"&&(r="funnel"),e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:`${s.titleCase(t)} ${n}`}),e.jsx(s.AccordionContent,{children:a||e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT label, measure FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"label - category"}),e.jsx("li",{children:"measure - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:l=>l.stopPropagation(),className:"text-blue-700",href:`https://docs.semaphor.cloud/docs/charts/${r}`,children:"example"})," in the docs."]})]})})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[t!=="text"&&e.jsx(Ke,{}),t==="text"&&e.jsx(Er,{})]})]}),!["range","text"].includes(t)&&e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ir(){const t=s.useEditorStore(r=>{var l,o;return(o=(l=r==null?void 0:r.card)==null?void 0:l.preferences)==null?void 0:o.sortChart}),{setSortChart:a}=s.useEditorActions();function n(){return e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Sort"}),e.jsx(ce,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"ASC",value:"asc"},{label:"DESC",value:"desc"}],value:t||"none",onValueChange:r=>{a(r)}})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(s.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(s.AccordionItem,{value:"item-1",children:[e.jsx(s.AccordionTrigger,{children:"Stacked Chart"}),e.jsxs(s.AccordionContent,{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT x-axis, stack-by, y-axis FROM table"}),e.jsxs("ul",{className:"ml-6 list-disc text-xs [&>li]:mt-2",children:[e.jsx("li",{children:"x-axis - category"}),e.jsx("li",{children:"stack by - category"}),e.jsx("li",{children:"y-axis - number"})]}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:r=>r.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/stacked",children:"example"})," in the docs."]})]})]}),e.jsxs(s.AccordionItem,{value:"item-2",children:[e.jsx(s.AccordionTrigger,{children:"Settings"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[n(),e.jsx(_t,{})]})]}),e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})]})})}function Ar({cardType:t="table"}){console.log("cardType",t);const a=s.useEditorStore(l=>{var o;return(o=l.card)==null?void 0:o.preferences}),n=s.useEditorStore(l=>l.actions.setCardPreferences),r=l=>{n({...a,allowDownload:l})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(we,{}),e.jsx(Ee,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Checkbox,{onCheckedChange:r,id:"allow-download"}),e.jsx("label",{htmlFor:"allow-download",className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:"Allow Download"})]})]})}const Mr=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT dimension, cohort, metric FROM table"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/tornado",children:"example"})," in the docs."]})]}),Lr=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT y-axis, label, range-from, range-to, range-value FROM table"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/range",children:"example"})," in the docs."]})]}),Or=e.jsxs("div",{className:"space-y-4 font-mono text-xs",children:[e.jsx("div",{className:"rounded bg-muted px-2 py-1 text-xs",children:"SELECT text FROM table limit 1"}),e.jsxs("div",{children:["See ",e.jsx("a",{target:"_blank",onClick:t=>t.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/text",children:"example"})," in the docs."]})]});function Rr(){const t=s.useEditorStore(n=>n.card.type);function a(n){switch(n){case"stackedBar":case"stackedLine":return e.jsx(Ir,{});case"tornado":case"pyramid":return e.jsx(Be,{docContent:Mr,cardType:n});case"kpi":return e.jsx(Sr,{});case"line":case"bar":return e.jsx(jr,{cardType:n});case"range":return e.jsx(Be,{docContent:Lr,cardType:n});case"pie":case"doughnut":case"polarArea":case"radar":case"funnel":return e.jsx(Be,{cardType:n});case"bubble":case"scatter":return e.jsx(yr,{cardType:n});case"table":return e.jsx(Ar,{cardType:n});case"text":return e.jsx(Be,{docContent:Or,cardType:n,suffix:"Visual"});case"map":return e.jsx(Dr,{});case"custom":return e.jsx(xr,{});default:return e.jsx(Nr,{})}}return e.jsxs(s.ScrollArea,{className:"h-full w-full text-sm",children:[a(t),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function $t({label:t,column:a,functions:n,onFunctionChange:r}){let l=n[0];"aggregate"in a?l=n.find(c=>c.value===a.aggregate)||n[0]:"granularity"in a&&(l=n.find(c=>c.value===a.granularity)||n[0]);function o(c){r(c)}return e.jsx(ce,{className:"flex h-8 w-8 justify-center overflow-clip rounded-none border-none bg-muted px-0 [&>svg]:hidden",icon:l.logo,options:n,value:l.value,onValueChange:c=>o(c),label:t,showPlaceholderLabel:!1})}const Br=[{label:"Sum",value:"SUM",logo:e.jsx(rt,{className:"size-3.5"})},{label:"Count",value:"COUNT",logo:e.jsx(fs,{className:"size-3.5"})},{label:"Unique",value:"DISTINCT",logo:e.jsx(Ln,{className:"size-3.5"})},{label:"Average",value:"AVG",logo:e.jsx(mn,{className:"size-3.5"})},{label:"Min",value:"MIN",logo:e.jsx(s.ArrowDown,{className:"size-3.5"})},{label:"Max",value:"MAX",logo:e.jsx(s.ArrowUp,{className:"size-3.5"})}];function Fr({column:t}){const{updateMetricColumnOld:a}=s.useEditorActions();let n=Br;s.isTextDataType(t.type)&&(n=n.filter(l=>["COUNT","DISTINCT"].includes(l.value))),s.isNumberDataType(t.type)&&(n=n.filter(l=>!["DISTINCT"].includes(l.value)));function r(l){const o={...t,aggregate:l,label:Le(t.name,l)};a(t.id,o)}return e.jsx($t,{column:t,functions:n,onFunctionChange:l=>r(l),label:"Aggregation"})}const zr=[{label:"Day (D)",value:"day",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"D"})},{label:"Week (W)",value:"week",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"W"})},{label:"Month (M)",value:"month",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"M"})},{label:"Year (Y)",value:"year",logo:e.jsx(s.Label,{className:"cursor-pointer text-xs",children:"Y"})}];function Pr({column:t}){const{updateGroupByColumnOld:a}=s.useEditorActions();function n(r){const l={...t,granularity:r,label:De(t.name,r)};a(t.id,l)}return e.jsx($t,{column:t,functions:zr,onFunctionChange:r=>n(r),label:"Granularity"})}function _r({col:t,onChange:a,droppableId:n}){const{columns:r}=s.useDataColumns(),l=A.useMemo(()=>{let i=[];(n===be||n===Ne||n===Ce)&&(i=r);const d=i==null?void 0:i.filter(m=>m.column_name!==t.name);return d==null?void 0:d.map(m=>({id:m.column_name,value:m.column_name}))},[r,n,t.name]),o=[{id:t.name,value:t.name}];function c(i){const d=r==null?void 0:r.find(m=>{var h;return m.column_name===((h=i[0])==null?void 0:h.id)});d&&a(d)}return l!=null&&l.length?e.jsx(s.ComboBox2,{multiple:!1,options:l,selectedOptions:o,onChange:c,selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"border-none bg-muted pl-0 pr-8"}):e.jsx("input",{disabled:!0,type:"text",className:"overflow-hidden truncate text-ellipsis whitespace-nowrap bg-muted text-sm",value:t.name})}function Gt({column:t}){const a=s.useEditorStore(d=>d.card.type),n=s.useEditorStore(d=>{var m,h;return(h=(m=d.card)==null?void 0:m.config)==null?void 0:h.metricColumns}),r=(n==null?void 0:n.map(d=>d.label))||[],l=r==null?void 0:r.find(d=>d===t.label||d===t.name),o=l?r.indexOf(l):-1;function c(){return e.jsx("div",{className:"pr-1",children:l&&e.jsx(Ut,{idx:o,col:l,children:e.jsx("div",{className:"w-[130px] text-xs text-muted-foreground",children:"Display Series As"})})})}function i(){return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Data Labels"}),e.jsx(Ke,{})]})}return e.jsxs("div",{className:"space-y-1",children:[["stackedBar","stackedLine","bar","line"].includes(a)&&c(),["pie","doughnut"].includes(a)&&i()]})}function Vr({column:t,type:a,onRemoveColumn:n,droppableId:r}){const[l,o]=A.useState(!1),{updateGroupByColumnOld:c,updateMetricColumnOld:i,updatePivotByColumnOld:d}=s.useEditorActions(),{attributes:m,listeners:h,setNodeRef:u,transform:p,transition:f,isDragging:j}=s.useSortable({id:t.id,data:{type:a}}),x=p?{opacity:j?.5:void 0,transform:s.CSS.Translate.toString(p),transition:f}:void 0,T=s.isTextDataType(t.type);function g(E){const N=s.isNumberDataType(E.data_type),D=s.isTextDataType(E.data_type),w=s.isDateDataType(E.data_type),M=N?"number":D?"string":w?"date":"string";if(r===be){if(D){const k={id:t.id,name:E.column_name,type:M,role:"groupby"};c(t.id,k)}if(w){const k={id:t.id,name:E.column_name,type:M,role:"groupby",label:De(E.column_name,"month"),granularity:"month"};c(t.id,k)}}if(r===Ce){if(D){const k={id:t.id,name:E.column_name,type:M,role:"groupby"};d(t.id,k)}if(w){const k={id:t.id,name:E.column_name,type:M,label:De(E.column_name,"month"),granularity:"month",role:"groupby"};d(t.id,k)}}if(r===Ne){const k={id:t.id,name:E.column_name,type:M,role:"metric",label:Le(E.column_name,"SUM"),aggregate:N?"SUM":"COUNT"};i(t.id,k)}}function b(){if(r===Ne)return e.jsx("div",{"data-setting-container":"",className:"mt-1.5 max-h-32 overflow-y-auto rounded-md border border-border p-3 text-sm",children:e.jsx(Gt,{column:t})})}function C(){const E=s.isDateDataType(t.type);if(r===Ne)return e.jsx(Fr,{column:t});if((r===be||r===Ce)&&E)return e.jsx(Pr,{column:t})}function O(){return e.jsx("div",{className:s.cn("grow",{"pl-3":(r===be||r===Ce)&&T}),children:e.jsx(_r,{col:t,onChange:g,droppableId:r})})}function S(){return e.jsx("div",{"data-setting-actions":"",className:"flex h-full items-center gap-2",children:e.jsx("button",{className:"h-full pl-2",onClick:()=>o(!l),children:l?e.jsx(ea,{className:s.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")}):e.jsx(tt,{className:s.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")})})})}return e.jsx("div",{...h,...m,ref:u,style:x,children:e.jsxs("div",{className:"group flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex h-8 grow items-center justify-between rounded-md bg-muted",children:[S(),C(),O()]}),l&&b()]}),e.jsx("div",{className:s.cn("opacity-0 transition-opacity duration-200 group-hover:opacity-100",{hidden:l}),children:e.jsx("button",{onClick:()=>n(t.id),children:e.jsx(s.X,{className:"size-3 cursor-pointer text-muted-foreground"})})})]})})}function as({id:t,label:a,columns:n,previewColumns:r,sortableType:l,onRemoveColumn:o}){var x,T;const{setNodeRef:c,isOver:i,over:d}=s.useDroppable({id:t}),m=s.useDndStore(g=>g.activeId),h=s.useEditorStore(g=>{var b;return(b=g.card)==null?void 0:b.type}),u=i||((T=(x=d==null?void 0:d.data)==null?void 0:x.current)==null?void 0:T.type)===l,p=m?r:n,f=l===_e?"Rows":l===Ve?"Pivot Columns":"Columns";function j(){if(!p||p.length===0)return e.jsx("div",{className:"mr-5 flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground/80 bg-background px-2 py-1 text-sm text-muted-foreground",children:e.jsx("span",{className:"invisible text-muted-foreground/70 group-hover:visible",children:"Drag here"})})}return e.jsx(s.SortableContext,{items:n==null?void 0:n.map(g=>g.id),children:e.jsxs("div",{className:s.cn("group space-y-2 rounded-md p-4",u&&"border border-muted-foreground/20"),ref:c,children:[e.jsxs("div",{className:"flex items-center justify-between pr-5",children:[e.jsx(s.Label,{className:"text-sm",children:a}),h!=="table"&&e.jsx("span",{className:"text-xs text-muted-foreground/50",children:f})]}),j(),e.jsx("div",{className:"space-y-2",children:p==null?void 0:p.map(g=>g.id===kt.id?e.jsx(Ur,{},g.id):e.jsx(Vr,{droppableId:t,type:l,column:g,onRemoveColumn:o},g.id))})]})})}function Ur(){return e.jsx("div",{className:"mr-5 flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground bg-background px-2 py-1 text-sm",children:e.jsx("span",{className:"text-muted-foreground/70",children:"Drop here"})})}function Ht({children:t,id:a,type:n}){const{attributes:r,listeners:l,setNodeRef:o,transform:c,transition:i,isDragging:d}=s.useSortable({id:a,data:{type:n}}),m=c?{opacity:d?.5:void 0,transform:s.CSS.Translate.toString(c),transition:i}:void 0;return e.jsx("div",{...l,...r,ref:o,style:m,children:t})}function qr(){const t=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.orderBy})||[],a=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.groupByColumns})||[],n=s.useEditorStore(g=>{var b;return(b=g.card.queryConfig)==null?void 0:b.metricColumns})||[],{addOrderByColumn:r,removeOrderByColumn:l,updateOrderByColumn:o}=s.useEditorActions(),c=[...a,...n];if(c.length===0)return e.jsx(e.Fragment,{});const i=e.jsx(fn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),d=e.jsx(un,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),m=e.jsx(pn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),h=e.jsx(dn,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),u=c==null?void 0:c.filter(g=>!t.some(b=>b.columnId===g.id)).map(g=>({id:g.id,value:g.label||g.name}));function p(g,b){const C=f(g);return C?C.role==="metric"?b==="asc"?m:h:b==="asc"?i:d:null}function f(g){return c.find(b=>b.id===g)}function j(g){const b=t.find(C=>C.columnId===g);if(b){const C={...b,direction:b.direction==="asc"?"desc":"asc"};o(C)}}const x=g=>{r({columnId:g.id,direction:"asc"})};function T(g){const b=t.find(C=>C.columnId===g);b&&l(b)}return e.jsx(s.SortableContext,{items:t.map(g=>Ie(g.columnId)),children:e.jsxs("div",{className:"space-y-2 rounded-md p-4 text-sm",children:[e.jsx(s.Label,{className:"text-sm",children:"Sort By"}),u.length>0&&e.jsx("div",{className:"mr-5",children:e.jsx(s.ComboBox2,{multiple:!1,options:u,selectedOptions:[],onChange:g=>x(g[0]),selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"h-8 bg-muted border-none"})}),t.map(g=>{var b,C;return e.jsx(Ht,{id:Ie(g.columnId),type:Ns,children:e.jsx("div",{className:"group flex h-8 items-center gap-2 rounded-sm p-0",children:e.jsxs("div",{className:"flex h-full grow items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex h-full grow items-center justify-between gap-2 rounded-sm bg-muted px-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(tt,{className:"size-3 cursor-move text-muted-foreground hover:text-foreground"}),e.jsx(Es,{className:"w-[175px] text-sm",children:((b=f(g.columnId))==null?void 0:b.label)||((C=f(g.columnId))==null?void 0:C.name)})]}),e.jsx("button",{title:g.direction==="asc"?"Ascending":"Descending",onClick:()=>j(g.columnId),className:"rounded-sm p-0",children:p(g.columnId,g.direction)})]}),e.jsx("div",{children:e.jsx("button",{onClick:()=>T(g.columnId),className:"rounded-sm p-0",children:e.jsx(s.X,{className:"size-3 text-muted-foreground opacity-0 transition-opacity duration-200 hover:text-foreground group-hover:opacity-100"})})})]})},g.columnId)},Ie(g.columnId))})]})})}function $r(){const t=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.groupByColumns)||[]}),a=s.useEditorStore(T=>T.card.type),n=s.useDndStore(T=>T.previewGroupByColumns),r=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.metricColumns)||[]}),l=s.useDndStore(T=>T.previewMetricColumns),o=s.useEditorStore(T=>{var g;return((g=T.card.queryConfig)==null?void 0:g.pivotColumns)||[]}),c=s.useDndStore(T=>T.previewPivotColumns),{removeColumnFromGroupBy:i,removeColumnFromMetricColumns:d,removeColumnFromPivotBy:m}=s.useEditorActions();function h(T){i(T)}function u(T){d(T)}function p(T){m(T)}let f="Group By",j="Metrics",x="Stack By";return a==="table"&&(f="Rows",j="Metrics",x="Columns"),e.jsxs("div",{className:"flex h-full w-full flex-col gap-3 py-4",children:[e.jsx(Bt,{iconClassName:"size-[19px]"}),e.jsxs(s.CustomScrollArea,{className:"flex min-h-0 grow basis-0 flex-col gap-4 overflow-y-auto",children:[e.jsxs("div",{className:"flex flex-col gap-4 p-4",children:[e.jsx(as,{onRemoveColumn:h,sortableType:_e,id:be,label:f,columns:t,previewColumns:n}),e.jsx(as,{onRemoveColumn:p,sortableType:Ve,id:Ce,label:x,columns:o,previewColumns:c}),e.jsx(as,{onRemoveColumn:u,sortableType:ls,id:Ne,label:j,columns:r,previewColumns:l}),e.jsx(qr,{})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function Is({children:t,column:a,id:n,sortableType:r,className:l}){const[o,c]=A.useState(!1),{removeEditorFilter:i,clearEditorFilter:d}=s.useEditorActions(),m=s.useEditorStore(u=>{var p,f;return(f=(p=u.card.queryConfig)==null?void 0:p.filters)==null?void 0:f.some(j=>j.filterColumnName===a.name)});function h(u){i(u)}return e.jsx(Ht,{id:n,type:r,children:e.jsxs("div",{className:s.cn("group space-y-1 rounded-md bg-muted px-3 py-2",{"border-border":o}),children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-sm",children:a.name}),m&&e.jsx("button",{onClick:()=>d(a),children:e.jsx(s.FilterX,{className:"size-3 cursor-pointer items-center justify-center text-muted-foreground hover:text-foreground"})})]}),e.jsx("div",{"data-setting-actions":"",className:"invisible flex h-full items-center gap-2 group-hover:visible",children:e.jsx("button",{onClick:()=>h(a),children:e.jsx(s.X,{className:"size-3.5 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground"})})})]}),e.jsxs("div",{className:s.cn("group flex items-center rounded-t-md text-sm",!o&&"rounded-b-md",o&&"border border-border"),children:[e.jsx("div",{children:e.jsx(s.GripVertical,{className:"mr-2 size-3.5 cursor-move items-center justify-center p-0 text-muted-foreground/60 hover:text-foreground"})}),e.jsx("div",{className:s.cn("grow cursor-pointer overflow-hidden truncate text-ellipsis whitespace-nowrap",l),children:t})]})]})})}function Gr({column:t,sortableType:a}){const n=s.useDashboardStore(C=>C.themeStyle),r=s.useEditorStore(C=>{var O,S;return(S=(O=C.card)==null?void 0:O.queryConfig)==null?void 0:S.filters}),l=s.useEditorStore(C=>{var O,S;return(S=(O=C.card.queryConfig)==null?void 0:O.filterColumns)==null?void 0:S.find(E=>E.name===t.name)}),{selectedConnectionId:o,selectedDatabaseName:c,selectedTableName:i}=s.useEditorAside(),d=A.useMemo(()=>{var C;return(C=r==null?void 0:r.find(O=>O.filterColumnName===t.name))==null?void 0:C.filterValue},[r,t.name]),m=A.useMemo(()=>Array.isArray(d)?d.map(C=>({id:C,value:C})):[],[d]),{updateEditorFilterValue:h}=s.useEditorActions(),u={column:t.name,sql:t.sql,id:t.name,connectionId:o,dataType:t.type},{data:p,isLoading:f,isFetching:j,isError:x}=s.useFilterQuery(u),T=A.useMemo(()=>{var O;return u.column?(O=p==null?void 0:p.records)==null?void 0:O.map(S=>{let E=u.column.split(".").slice(-1)[0].replace(/"/g,"");return S[E]===void 0&&(E=E.toLowerCase()),{id:S[E],value:S[E]}}):[]},[p,u.column]);function g(C){const O=C.map(S=>S.id);h(t.name,O)}function b(){return f||j?e.jsx(s.Skeleton,{style:{borderRadius:n==null?void 0:n.controlInputRadius},className:"h-8 min-w-60 bg-foreground/10"}):x?e.jsx("div",{className:"flex h-8 min-w-60 items-center bg-muted px-2 text-sm",children:"Error"}):e.jsx(s.ComboBox2,{renderFilterModeButton:()=>e.jsx(Hr,{columnName:t.name}),showNegativeIndicator:(l==null?void 0:l.mode)==="exclude",options:T,onChange:g,selectedOptions:m,selectAllChecked:!1,onSelectAllCheckedChange:()=>{}})}return e.jsx(Is,{className:"w-60",id:t.id,sortableType:a,column:t,children:b()})}function Hr({columnName:t}){const{setEditorFilterMode:a}=s.useEditorActions(),n=s.useEditorStore(l=>{var o,c;return(c=(o=l.card.queryConfig)==null?void 0:o.filterColumns)==null?void 0:c.find(i=>i.name===t)});function r(){(n==null?void 0:n.mode)==="exclude"?a(t,"include"):a(t,"exclude")}return e.jsx(s.Button,{onClick:r,variant:"secondary",size:"sm",className:s.cn("h-5 text-xs hover:underline",{}),children:s.titleCase((n==null?void 0:n.mode)==="exclude"?"exclude":"include")})}function Jr({column:t,sortableType:a}){const[n,r]=A.useState(!1),{selectedConnectionId:l,selectedDatabaseName:o,selectedTableName:c}=s.useEditorAside(),i={column:t.name,sql:t.sql,id:t.name,connectionId:l,title:"",dataType:t.type},{isLoading:d,isFetching:m,isError:h,data:u}=s.useFilterQuery(i),p=u==null?void 0:u.records,{dateRange:f,setDateRange:j,initialDateRange:x}=s.useDateRangeFromRecords(p),{updateEditorFilterValue:T}=s.useEditorActions();function g(O){var E,N;const S={gte:(E=O==null?void 0:O.from)==null?void 0:E.toISOString(),lte:(N=O==null?void 0:O.to)==null?void 0:N.toISOString()};T(t.name,S)}function b(){T(t.name,null,!0),j(x)}function C(){return d||m?e.jsx("div",{children:e.jsx(s.Skeleton,{className:"h-8 w-72 bg-foreground/10"})}):h?e.jsx("div",{className:"h-8 w-72 bg-muted px-2",children:"Error"}):p?e.jsx(s.DateRangePopover,{title:i.title,open:n,inputClassName:"h-8",setOpen:r,dateRange:f,setDateRange:j,onApply:g,onClear:b}):e.jsx(s.Label,{className:"flex h-8 w-72 items-center bg-muted px-2",children:"No Data"})}return e.jsx(Is,{className:"w-72",id:t.id,sortableType:a,column:t,children:C()})}function Wr({id:t,label:a,columns:n,previewColumns:r,sortableType:l}){var j,x;const{setNodeRef:o,isOver:c,over:i}=s.useDroppable({id:t}),d=s.useDndStore(T=>T.activeId),m={},h=c||((x=(j=i==null?void 0:i.data)==null?void 0:j.current)==null?void 0:x.type)===l;function u(){if(!f||f.length===0)return e.jsx("div",{className:"flex h-8 grow items-center space-x-2 rounded-md border border-dashed border-muted-foreground/80 bg-background px-2 py-1 text-sm text-muted-foreground",children:e.jsx("span",{className:"invisible text-muted-foreground/70 group-hover:visible",children:"Drag here"})})}function p(T){const g=s.isDateDataType(T.type);return T.id===kt.id?e.jsx(Is,{id:T.id,sortableType:l,column:T,children:e.jsx(Yr,{})},T.id):g?e.jsx(Jr,{droppableId:t,sortableType:l,column:T},T.id):e.jsx(Gr,{droppableId:t,sortableType:l,column:T},T.id)}const f=d?r:n;return e.jsx(s.SortableContext,{items:n==null?void 0:n.map(T=>T.id),children:e.jsxs("div",{className:s.cn("group space-y-2 rounded-md border-muted-foreground/50 p-4",h&&"border border-muted-foreground/20"),ref:o,style:m,children:[e.jsx(s.Label,{className:"text-sm",children:a}),u(),e.jsx("div",{className:"flex flex-wrap items-center gap-4",children:f==null?void 0:f.map(T=>p(T))})]})})}function Yr(){return e.jsx("div",{className:"flex h-8 w-60 items-center space-x-2 rounded-md border border-dashed border-muted-foreground bg-background px-2 py-1 text-sm text-muted-foreground",children:"Drop here"})}function Kr(){const t=s.useEditorStore(l=>{var o;return((o=l.card.queryConfig)==null?void 0:o.filterColumns)||[]}),a=s.useDndStore(l=>l.previewFilterColumns),{removeColumnFromFilterColumns:n}=s.useEditorActions();function r(l){n(l)}return e.jsx("section",{className:"size-full px-4 pb-4",children:e.jsx(Wr,{onRemoveColumn:r,sortableType:is,id:os,label:"Filters",columns:t,previewColumns:a})})}function Qr(){const t=s.useEditorStore(o=>o.card),a=s.useEditorStore(o=>o.showAIDialog),{setShowAIDialog:n}=s.useEditorActions(),{data:r,errorMessage:l}=s.useDashboardCardQuery(t);return e.jsxs("section",{className:"flex justify-between gap-2 p-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.EditorClearButton,{}),!a&&e.jsxs(s.Button,{onClick:()=>n(!0),variant:"secondary",size:"sm",children:[e.jsx(Jn,{className:"mr-2 h-4 w-4"}),"Assistant"]}),e.jsx(At,{sql:r==null?void 0:r.sql,error:l})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"rounded-full bg-green-300 px-3 py-1 text-xs font-medium text-black",children:"Preview"}),e.jsx(s.EditorCancelButton,{}),e.jsx(s.EditorAcceptButton,{variant:"default"})]})]})}function Xr(){const t=A.useRef(null),a=A.useRef(null),n=s.useEditorStore(i=>i.isShowingVisual),r=s.useEditorStore(i=>i.showAIDialog),l=s.useEditorStore(i=>i.isDevMode);A.useEffect(()=>{var i,d,m,h;r?((i=t.current)==null||i.resize(0),(d=a.current)==null||d.resize(0)):((m=t.current)==null||m.resize(25),(h=a.current)==null||h.resize(25))},[r]);function o(){return e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(Qa,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:n&&!r?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:t,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(Rr,{})})}),!r&&e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(Re,{})})]}):e.jsx(Re,{})})]})}function c(){return e.jsx(e.Fragment,{children:e.jsx("div",{className:"grow","data-biz-mode-container":"",children:n?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:a,defaultSize:25,maxSize:30,children:e.jsx($r,{})})}),e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{defaultSize:75,maxSize:100,children:e.jsxs(s.ResizablePanelGroup,{direction:"vertical",children:[e.jsxs(s.ResizablePanel,{className:"flex min-h-fit flex-col",minSize:10,defaultSize:20,children:[e.jsx(Qr,{}),e.jsx(Kr,{})]}),e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:60,defaultSize:80,children:e.jsx(Re,{})})]})})]}):e.jsx(Re,{})})})}return e.jsx(s.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:l?o():c()})}function Zr(t){const a=s.useAIInteractionStore(B=>B.currentAIContext),[n,r]=A.useState(""),[l,o]=A.useState(null),{authToken:c}=s.useSemaphorContext(),i=s.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.sql}),d=s.useEditorStore(B=>B.isDevMode),m=s.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.python}),{setCardPython:h,setCardSql:u,setRunSql:p,switchCardType:f,setQueryConfig:j}=s.useEditorActions(),x=s.useEditorStore(B=>B.currentAssitantScope)||[],T=s.useEditorStore(B=>B.userInputForAI),g=s.useEditorStore(B=>B.triggerAIRun),b=s.useEditorStore(B=>B.card),C=s.useEditorStore(B=>B.selectedDatabaseName),O=s.useEditorStore(B=>B.selectedTableName),S=s.useEditorStore(B=>B.selectedConnectionId),E=s.useEditorStore(B=>B.selectedSchemaName),N=s.useDashboardStore(B=>B.dashboard.aiScopeTables),{errorMessage:D,refetch:w,data:M}=s.useDashboardCardQuery(b),{setTriggerAIRun:k,setCurrentAssitantScope:R,setCardConfig:I}=s.useEditorActions(),{selectedDatamodelName:v,connectionType:y,selectedDatamodelId:L}=s.useEditorAside(),{messages:F,setMessages:P,reload:_,error:z,append:Q,input:Y,setInput:V,status:q,handleInputChange:$,handleSubmit:U,stop:H}=s.useChat({api:"https://semaphor.cloud/api/v1/assistant",headers:{Authorization:`Bearer ${c==null?void 0:c.accessToken}`},body:{random:Math.random(),assistantId:"editor-assistant",isDevMode:d,aiContext:a},maxSteps:10,onError:B=>{console.log("on error",B)},onToolCall:W,onFinish(B,K){G(K.usage),r(""),K.finishReason==="stop"&&r("")}});A.useEffect(()=>{g&&T&&(Q({role:"user",content:T}),k(!1))},[g,T,Q,k]);async function W({toolCall:B}){var K,re,he,je;if(["createChartFromSql","createForecast"].includes(B.toolName)){const te=B.args.sql,le=B.args.python,oe=B.args.chartType;return f(oe),u(te),h(le),p(!0),{status:"success",message:"Let the user know that chart is created"}}else if(B.toolName==="getTableColumns")r("Getting table columns...");else{if(B.toolName==="getUserSql")return i?{status:"success",sql:i,python:m||""}:{status:"error",message:"No SQL found"};if(B.toolName==="getCurrentTableScope")return r("Getting table scope..."),ee(),{status:"success",tables:x};if(B.toolName==="generateQueryConfig"){r("Generating query config...");const te=B.args.chartType,le=B.args.queryConfig;B.args.cardConfig,f(te),I(le),u(void 0),h(void 0);const oe=await w();return(K=oe==null?void 0:oe.data)!=null&&K.error?{status:"error",queryConfig:le,message:(re=oe==null?void 0:oe.data)==null?void 0:re.error.message}:(he=oe==null?void 0:oe.data)!=null&&he.records?{status:"success",queryConfig:le,message:"The data has been visualized"}:{status:"success",queryConfig:le,message:"The data has been visualized",records:(je=oe==null?void 0:oe.data)==null?void 0:je.records}}else r("Analyzing...")}}function G(B){o(K=>K?{promptTokens:K.promptTokens+B.promptTokens,completionTokens:K.completionTokens+B.completionTokens,totalTokens:K.totalTokens+B.totalTokens}:B)}function Z(B){const K=s.removeFromScopeArray(x,B);se(K)}function se(B){const K=s.sanitizeAIScope(B);R(K),P(re=>[...re,{id:crypto.randomUUID(),role:"system",content:`Current scope of tables. Always use the table reference format when generating SQL | ${JSON.stringify(K)}`}])}function ne(){const B={databaseName:C||"",schemaName:E||"",tableName:O||"",datamodelId:L||"",datamodelName:s.resolveDatamodelName(v),connectionId:S||"",connectionType:y||""},K=s.dedup([...x,B]);se(K)}function ee(){const B={databaseName:C||"",schemaName:E||"",tableName:O||"",datamodelName:s.resolveDatamodelName(v),connectionId:S||"",connectionType:y||"",datamodelId:L||""};if(!s.isInAiScopeArray([...N||[],...x],B)||x.length===0){const re=s.dedup([...N||[],...x,B]).filter(he=>he.connectionId===B.connectionId);se(re)}}const ae=!["ready","error"].includes(q),J=s.getErrorMessage(z);return{messages:F,setMessages:P,reload:_,append:Q,input:Y,setInput:V,isLoading:ae,handleSetAIScope:ee,handleAddCurrentTableToScope:ne,removeFromCurrentSelections:Z,currentAssitantScope:x,setCurrentAssitantScope:R,handleInputChange:$,handleSubmit:U,stop:H,chatStatus:q,toolStatus:n,llmUsage:l,onFinish:t,errorMessage:J}}const el=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function Ws(){const t=A.useRef(null),a=A.useRef(null),[n,r]=A.useState(!1),[l,o]=A.useState(s.isDevEnv),[c,i]=A.useState(!1),[d,m]=A.useState(!1),h=s.useCurrentAIContext(),{setCurrentAIContext:u}=s.useAIActions(),p=s.useDashboardStore(H=>H.dashboard.aiContext),f=s.useEditorStore(H=>H.card.dataSource),j=s.useEditorStore(H=>H.actions.setCardSql),x=s.useEditorStore(H=>H.actions.setRunSql),T=s.useEditorStore(H=>H.showAIDialog),g=s.useDashboardStore(H=>H.handleOpenAssistantProfile),{setShowAIDialog:b,setCardPython:C}=s.useEditorActions(),{setDataSource:O}=s.useEditorActions(),{assistantProfile:S}=s.useAssistantProfile(),{messages:E,setMessages:N,reload:D,append:w,input:M,setInput:k,isLoading:R,handleInputChange:I,handleSubmit:v,stop:y,chatStatus:L,llmUsage:F,errorMessage:P}=Zr();s.useAssistantScroll({scrollContainerRef:t,inputRef:a,messages:E}),A.useEffect(()=>{var H;T&&((H=a.current)==null||H.focus(),u({selectedEntities:(f==null?void 0:f.selectedEntities)||[]}))},[T,f,u]);function _(H,W){W==="sql"?(j(H),x(!0)):W==="python"&&(C(H),x(!0))}function z(){var H;(H=p==null?void 0:p.selectedEntities)==null||H[0],m(!0)}function Q(){h.selectedEntities.length===0&&u({selectedEntities:(p==null?void 0:p.selectedEntities)||[]})}function Y(H){Q();const W={role:"user",content:H.title+" "+H.subtitle};w(W)}function V(H){Q(),w({role:"user",content:H})}function q(){return e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.Button,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",variant:"ghost",children:e.jsx(s.EllipsisVertical,{className:"size-3.5"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",children:[e.jsx(s.DropdownMenuLabel,{children:"Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx(s.DropdownMenuCheckboxItem,{checked:n,onCheckedChange:r,children:"Token Usage"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:l,onCheckedChange:o,children:"Tool Invocations"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:c,onCheckedChange:i,children:"System Message"})]})]})}function $(){return e.jsx("div",{"data-role":"header",children:e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"})})}const U=(M==null?void 0:M.length)===0&&(E==null?void 0:E.filter(H=>H.role==="user").length)===0;return e.jsxs("div",{"data-role":"editor-assistant",className:"flex h-full w-full flex-col justify-between px-6 py-4",children:[e.jsxs("div",{"data-role":"header-section",className:"flex items-start justify-between gap-2",children:[$(),g&&e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-7 p-2",onClick:g,children:(S==null?void 0:S.name)||"Default Profile"}),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center gap-2",children:[q(),e.jsx(s.IconButton,{tooltip:"Add Context",className:"h-7 p-2 text-muted-foreground hover:text-foreground",onClick:z,children:e.jsx(s.Settings,{className:"size-4"})}),e.jsx(s.IconButton,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",tooltip:"New Chat",onClick:()=>{N([]),D()},children:e.jsx(s.SquarePen,{className:"size-4"})}),e.jsx(s.Button,{variant:"ghost",className:"h-8 text-muted-foreground hover:text-foreground",size:"sm",onClick:()=>{b(!1)},children:e.jsx(s.EyeOff,{className:"size-4"})})]})]}),e.jsx(s.CustomScrollArea,{ref:t,className:"mt-2 flex w-full grow basis-0",children:e.jsxs("div",{className:"space-y-4 py-2 pr-3","data-role":"messages",children:[e.jsx(s.PrintMessages,{messages:E,showTools:l,handleRun:_,showSystemMessage:c}),e.jsx(s.ChatStatus,{messages:E,chatStatus:L,errorMessage:P})]})}),e.jsxs("div",{className:"mb-3",children:[U&&!S&&e.jsx(s.Placeholders,{onClick:Y,placeholders:el}),e.jsx("div",{children:U&&S&&e.jsx(s.SeedQuestions,{seedQuestions:S.seedQuestions||[],handleSeedQuestionClick:V})})]}),e.jsx(s.ChatInputWithContext,{ref:a,setInput:k,stop:y,input:M,handleInputChange:I,handleSubmit:H=>{Q(),v(H)},isLoading:R,onFocus:()=>{}}),F&&n&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",F.promptTokens," prompt tokens,"," ",F.completionTokens," completion tokens,"," ",F.totalTokens," total tokens"]})}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(f==null?void 0:f.selectedEntities)||[],open:d,onOpenChange:m,dataSource:f||s.EMPTY_DATA_SOURCE,fallbackDataSource:s.EMPTY_DATA_SOURCE,dialogMode:"aiContext"})]})}function Jt(t,a){return`${t}-${a.qualifiedEntityName}-${a.name}-${Date.now()}`}function Wt(t,a){const n=s.toTitleCase(t.replace(/_/g," "));switch(a){case"SUM":return`Sum of ${n}`;case"COUNT":return`Count of ${n}`;case"DISTINCT":return`Unique ${n}`;case"AVG":return`Average of ${n}`;case"MIN":return`Min of ${n}`;case"MAX":return`Max of ${n}`;default:return n}}function sl(t,a){const n=s.getDataType(t.dataType);return n==="number"?"metrics":n==="date"||n==="string"?"groupBy":"metrics"}function cs(t,a){const n=Jt(a,t),r=s.getDataType(t.dataType),l=r==="date",o={id:n,name:t.name,dataType:t.dataType,label:s.toLabel(t.name),qualifiedEntityName:t.qualifiedEntityName,entityId:t.entityId,entityName:t.entityName,entityType:t.entityType};if(a==="metrics"){const c=r==="number"?"SUM":"COUNT",i=Wt(t.label,c);return{...o,role:"metric",label:i,aggregate:c}}return a==="groupBy"?{...o,role:"groupby",...l&&{granularity:"month",dateFormat:"YYYY-MM"}}:a==="pivotBy"?{...o,role:"pivotby"}:a==="sortBy"?{...o,role:"sortby",label:t.label,direction:"asc"}:null}function Yt(t,a,n){var r;return!n||!((r=n.baseEntity)!=null&&r.name)||!n.joins?!1:t===a||t===n.baseEntity.name||a===n.baseEntity.name?!0:n.joins.some(l=>{const o=s.getQualifiedEntityName(l.source),c=s.getQualifiedEntityName(l.target);return o===t&&c===a||o===a&&c===t})}function tl(t,a,n){if(a.length===0)return{isValid:!0};const r=a[0].qualifiedEntityName;if(!r)return{isValid:!0};if(r!==t.qualifiedEntityName&&t.qualifiedEntityName)if(n){if(!Yt(r,t.qualifiedEntityName,n))return{isValid:!1,showError:!0}}else return{isValid:!1,showError:!0};return{isValid:!0}}function ds(){s.ue.error("Join Configuration Required",{richColors:!0,description:"To combine fields from two different tables, you need to define a join between them first.",position:"top-center",duration:4e3})}function us(t,a){return a.some(n=>n.name===t.name&&n.qualifiedEntityName===t.qualifiedEntityName)}const As=t=>{var k;const{updateCardConfig:a,updateGroupByColumn:n,updateMetricColumn:r,updatePivotByColumn:l,updateSortByColumn:o,removeGroupByColumn:c,removeMetricColumn:i,removePivotByColumn:d,removeSortByColumn:m}=s.useEditorActions(),h=s.useEditorStore(R=>R.card.type),u=t==null?void 0:t.id,p=A.useCallback(R=>{if(!(t!=null&&t.config))return[];switch(R){case"groupBy":return(t.config.groupByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType,dateFormat:I.dateFormat,customFormat:I.customFormat,granularity:I.granularity}));case"metrics":return t.config.metricColumns||[];case"pivotBy":return(t.config.pivotByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType}));case"sortBy":return(t.config.sortByColumns||[]).map(I=>({id:I.id,name:I.name,dataType:I.dataType,label:I.label!==void 0?I.label:I.name,qualifiedEntityName:I.qualifiedEntityName,entityId:I.entityId,entityName:I.entityName,entityType:I.entityType,direction:I.direction}));case"source":return[];default:return[]}},[t]),f=A.useCallback((R,I)=>{if(u)switch(R){case"groupBy":{a(u,{groupByColumns:I});break}case"metrics":{a(u,{metricColumns:I});break}case"pivotBy":{a(u,{pivotByColumns:I});break}case"sortBy":{a(u,{sortByColumns:I});break}}},[u,a]),j=A.useCallback((R,I,v)=>{if(!u)return;const L=p(R)[I];if(L)switch(R){case"groupBy":n(u,L.id,{label:v});break;case"metrics":r(u,L.id,{label:v});break;case"pivotBy":l(u,L.id,{label:v});break;case"sortBy":o(u,L.id,{label:v});break}},[u,p,n,r,l,o]),x=A.useCallback((R,I)=>{if(!u)return;const y=p("metrics")[R];y&&r(u,y.id,{aggregate:I,label:Wt(y.name,I)})},[u,p,r]),T=A.useCallback((R,I,v)=>{if(!u)return;const L=p("groupBy")[R];if(!L)return;const F={dateFormat:I};I==="custom"&&v?F.customFormat=v:F.customFormat=void 0,n(u,L.id,F)},[u,p,n]),g=A.useCallback((R,I)=>{if(!u)return;const y=p("groupBy")[R];y&&n(u,y.id,{granularity:I})},[u,p,n]),b=A.useCallback((R,I)=>{if(!u)return;const y=p("metrics")[R];y&&r(u,y.id,{aliasTemplate:I})},[u,p,r]),C=A.useCallback((R,I)=>{if(console.log("handleValueAliasesChange",R,I),!u)return;console.log("handleValueAliasesChange",R,I);const y=p("metrics")[R];y&&(console.log("handleValueAliasesChange",I),r(u,y.id,{valueAliases:I}))},[u,p,r]),O=A.useCallback(R=>{u&&a(u,{comparisonType:R})},[u,a]),S=A.useCallback(R=>{u&&a(u,{showTrendline:R})},[u,a]),E=A.useCallback(R=>{u&&a(u,{trendlineWindow:R})},[u,a]),N=A.useCallback(R=>{u&&a(u,{trendlineGranularity:R})},[u,a]),D=A.useCallback(R=>{u&&a(u,{targetValue:R})},[u,a]),w=A.useCallback(R=>{var _;if(!u)return;const I=[...p("groupBy"),...p("pivotBy"),...p("metrics"),...p("sortBy")],v=(_=t==null?void 0:t.config)==null?void 0:_.joinPlan,y=tl(R,I,v);if(!y.isValid){y.showError&&ds();return}const L=sl(R),F=p(L);if(us(R,F))return;const P=cs(R,L);P&&f(L,[...F,P])},[u,(k=t==null?void 0:t.config)==null?void 0:k.joinPlan,p,f]),M=A.useCallback((R,I)=>{if(!u)return;const y=p(R)[I];if(y){if(R!=="sortBy"){const L=p("sortBy"),F=L.filter(P=>!(P.name===y.name&&P.qualifiedEntityName===y.qualifiedEntityName));F.length!==L.length&&a(u,{sortByColumns:F})}switch(R){case"groupBy":c(u,y.id);break;case"metrics":i(u,y.id);break;case"pivotBy":d(u,y.id);break;case"sortBy":m(u,y.id);break}}},[u,p,c,i,d,m,a]);return{getFieldsForContainer:p,setFieldsForContainer:f,updateLabel:j,handleAggregationChange:x,handleDateFormatChange:T,handleGranularityChange:g,handleAliasTemplateChange:b,handleValueAliasesChange:C,removeField:M,handleFieldClick:w,handleKpiComparisonTypeChange:O,handleKpiShowTrendlineChange:S,handleKpiTrendlineWindowChange:E,handleKpiTrendlineGranularityChange:N,handleKpiTargetValueChange:D,cardType:h}};function nl({field:t}){const{attributes:a,listeners:n,setNodeRef:r,transform:l,transition:o,isDragging:c}=s.useSortable({id:t.id}),[i,d]=A.useState(!1),m=s.useEditorStore(x=>x.card),{handleFieldClick:h}=As(m),u={transform:s.CSS.Transform.toString(l),transition:o},p=s.getDataType(t.dataType),j=(x=>{const T=(x||"").toLowerCase();switch(s.getDataType(T)){case"number":return fs;case"date":return s.Calendar;case"boolean":return ot;case"json":return Sn;case"geo":return Gn;default:return it}})(t.dataType);return e.jsxs("div",{ref:r,style:u,...a,...n,className:`group/container overflow-hidden rounded-lg transition-all ${c?"scale-95 opacity-50":"border border-transparent hover:border-blue-200 hover:bg-blue-50 dark:hover:border-blue-800 dark:hover:bg-blue-950"}`,children:[e.jsxs("div",{className:"flex min-w-0 items-center gap-1 p-2",onClick:()=>h(t),children:[e.jsx("div",{className:"flex-shrink-0 hover:cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"invisible size-3.5 text-gray-400 group-hover/container:visible"})}),e.jsx(j,{className:s.cn("size-3.5 flex-shrink-0 text-gray-500",p==="string"?"text-blue-500":"",p==="number"?"text-green-500":"",p==="date"?"text-purple-500":"",p==="boolean"?"text-orange-500":"")}),e.jsx(s.TooltipProvider$1,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("div",{className:"min-w-0 flex-1 overflow-hidden",children:e.jsx("span",{className:`block text-sm text-foreground/90 hover:text-foreground dark:text-muted-foreground ${i?"whitespace-normal break-words":"truncate"}`,children:t.name})})}),e.jsx(s.TooltipContent,{side:"bottom",align:"start",className:"ml-5 max-w-xs",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"text-sm font-medium",children:t.label||t.name}),t.description&&e.jsxs("div",{className:"border-t border-border pt-2",children:[e.jsx("div",{className:"mb-1 text-xs text-muted-foreground",children:"Description:"}),e.jsx("div",{className:"text-xs leading-relaxed",children:t.description})]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"mr-2 text-muted-foreground",children:"Name:"}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.name})]}),e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"mr-2 text-muted-foreground",children:"Type:"}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.dataType})]}),t.qualifiedEntityName&&e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{className:"mr-2 text-muted-foreground",children:[t.entityType?t.entityType.charAt(0).toUpperCase()+t.entityType.slice(1):"Entity",":"]}),e.jsx("span",{className:"font-mono text-muted-foreground",children:t.qualifiedEntityName})]})]})]})})]})})]}),i&&e.jsx("div",{className:"text-muted-foreground-700 px-2 pb-3 text-sm",children:e.jsx(s.Card,{className:"",children:e.jsxs(s.CardContent,{className:"space-y-2 p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(lt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"break-all",children:t.label||t.name})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(na,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"break-all",children:t.name})]}),e.jsx(s.Badge,{variant:"outline",className:"mt-2 text-xs",children:t.dataType})]})})})]})}function al(){const[t,a]=A.useState(""),[n,r]=A.useState("all"),l=s.useEditorStore(C=>C.card.dataSource),{columns:o=[],columnGroups:c=[],isLoading:i,error:d}=s.useColumns(),m=o.filter(C=>C.name.toLowerCase().includes(t.toLowerCase())||C.dataType.toLowerCase().includes(t.toLowerCase())||C.qualifiedEntityName&&C.qualifiedEntityName.toLowerCase().includes(t.toLowerCase())),h=C=>{if(C.dataType==="number"||C.dataType==="integer"||C.dataType==="float"||C.dataType==="decimal"||C.dataType==="currency")return"metrics";const O=s.getDataType(C.dataType);return["date","string","boolean"].includes(O)?"dimensions":O==="number"?"metrics":"dimensions"},p=(n==="all"?m:m.filter(C=>h(C)===n)).reduce((C,O)=>{const S=O.qualifiedEntityName||"Other Fields";return C[S]||(C[S]={label:s.toLabel(O.entityName),fields:[]}),C[S].fields.push(O),C},{}),j=Object.keys(p),x=m.filter(C=>h(C)==="dimensions").length,T=m.filter(C=>h(C)==="metrics").length,g=m.filter(C=>h(C)==="calculated").length,b=m.length>0;return i?e.jsxs("div",{className:"flex h-[calc(100vh-200px)] w-[290px] flex-col items-center justify-center",children:[e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-gray-400"}),e.jsx("span",{className:"mt-2 text-sm text-gray-500",children:"Loading fields..."})]}):d?e.jsxs("div",{className:"flex h-[calc(100vh-200px)] w-[290px] flex-col items-center justify-center",children:[e.jsx(s.Database,{className:"mb-4 h-12 w-12 text-red-300"}),e.jsx("p",{className:"mb-2 text-sm text-red-500",children:"Error loading fields"}),e.jsx("p",{className:"px-4 text-center text-xs text-red-400",children:d.message||"Failed to load column data"})]}):!l||!l.selectedEntities||l.selectedEntities.length===0?e.jsx(s.ScrollArea,{className:"h-[calc(100vh-200px)] w-[290px]",children:e.jsxs("div",{className:"flex flex-col items-center justify-center py-8 text-center",children:[e.jsx(s.Database,{className:"mb-4 h-12 w-12 text-gray-300"}),e.jsx("p",{className:"mb-2 text-sm text-gray-500",children:"No data source selected"}),e.jsx("p",{className:"px-4 text-xs text-gray-400",children:"Select a table, view, or dataset from the data source panel to see available fields"})]})}):e.jsxs("div",{className:"mt-2 flex h-[calc(100vh-200px)] w-full grow flex-col gap-2 px-6 pb-6",children:[e.jsxs("div",{className:"mt-1 flex-shrink-0 space-y-2",children:[o.length>0&&e.jsx("div",{className:"mb-4",children:e.jsx(s.Input$1,{type:"text",placeholder:"Search fields...",value:t,onChange:C=>a(C.target.value)})}),b&&e.jsx(s.Tabs,{defaultValue:"all",value:n,onValueChange:C=>r(C),className:"mb-4",children:e.jsxs(s.TabsList,{className:"grid h-9 w-full grid-cols-4",children:[e.jsxs(s.TabsTrigger,{value:"all",className:"text-xs",children:["All",e.jsx("span",{className:"ml-2 text-xs font-semibold",children:m.length})]}),e.jsxs(s.TabsTrigger,{value:"dimensions",className:"text-xs",children:[e.jsx(s.Layers,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:x})]}),e.jsxs(s.TabsTrigger,{value:"metrics",className:"text-xs",children:[e.jsx(s.ChartColumn,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:T})]}),e.jsxs(s.TabsTrigger,{value:"calculated",className:"text-xs",children:[e.jsx(bn,{className:"size-4 shrink-0"}),e.jsx("span",{className:"ml-2 text-xs font-semibold",children:g})]})]})})]}),e.jsxs(s.CustomScrollArea,{className:"py-2",children:[e.jsx(s.Accordion,{type:"multiple",defaultValue:j,className:"space-y-2",children:Object.entries(p).map(([C,O])=>{const S=c.find(E=>E.entityName===C)||{entityName:C,label:O.label};return e.jsxs(s.AccordionItem,{value:C,className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"group rounded-md px-3 py-2 text-muted-foreground hover:bg-muted hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-primary",children:S.label||S.entityName}),e.jsx(s.Badge,{variant:"secondary",className:"rounded-full text-xs text-muted-foreground group-hover:bg-background",children:O.fields.length})]})}),e.jsx(s.AccordionContent,{className:"pb-2",children:e.jsx("div",{className:"space-y-1",children:O.fields.map(E=>e.jsx(nl,{field:E},E.id))})})]},C)})}),Object.keys(p).length===0&&o.length===0&&e.jsxs("div",{className:"py-8 text-center text-sm text-gray-500",children:[e.jsx("p",{children:"No fields found for selected data source"}),e.jsx("p",{className:"mt-1 text-xs text-gray-400",children:"Try selecting different data sources"})]}),Object.keys(p).length===0&&o.length>0&&e.jsx("div",{className:"py-4 text-center text-sm text-gray-500",children:"No fields found for this category"}),e.jsx(s.ScrollBar,{orientation:"horizontal"}),e.jsx(s.ScrollBar,{orientation:"vertical"})]})]})}function rl(){return e.jsxs("div",{className:" h-full flex flex-col items-center",children:[" ",e.jsx(al,{})," "]})}function ll({initialJoinPlan:t,availableDataSources:a}){const[n,r]=A.useState(t),[l,o]=A.useState(new Set),c=()=>{const E=n.joins.length>0?Math.max(...n.joins.map(D=>D.sequence))+1:1,N={id:`join-${Date.now()}`,source:{type:"table",name:"",database:"",schema:""},target:{type:"table",name:"",database:"",schema:""},joinType:"INNER",sequence:E,joinKeyGroups:[{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}]};r({...n,joins:[...n.joins,N]}),o(new Set([...l,N.id]))},i=E=>{r({...n,joins:n.joins.filter(N=>N.id!==E)}),o(new Set([...l].filter(N=>N!==E)))},d=(E,N)=>{var k,R;const D=n.joins.find(I=>I.id===E);if(!D)return;const w={...D,...N},M=n.joins.map(I=>I.id===E?w:I);if((k=w.source)!=null&&k.name&&((R=w.target)!=null&&R.name)&&w.joinKeyGroups[0].keys.length===0){const I=a.find(F=>{var P;return F.name===((P=w.source)==null?void 0:P.name)}),v=a.find(F=>{var P;return F.name===((P=w.target)==null?void 0:P.name)}),y={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",entityId:(I==null?void 0:I.name)||"",entityName:(I==null?void 0:I.name)||"",entityType:(I==null?void 0:I.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",entityId:(v==null?void 0:v.name)||"",entityName:(v==null?void 0:v.name)||"",entityType:(v==null?void 0:v.type)||"table"},operator:"="},L=[...w.joinKeyGroups];L[0]={...L[0],keys:[y]},w.joinKeyGroups=L,M[M.findIndex(F=>F.id===E)]=w}r({...n,joins:M})};return{joinPlan:n,expandedJoins:l,setJoinPlan:r,addJoin:c,removeJoin:i,updateJoin:d,moveJoinUp:E=>{const N=n.joins.findIndex(D=>D.id===E);if(N>0){const D=[...n.joins],w={...D[N]},M={...D[N-1]};D[N]=M,D[N-1]=w,D.forEach((R,I)=>{R.sequence=I+1});const k={...n,joins:D};r(k)}},moveJoinDown:E=>{const N=n.joins.findIndex(D=>D.id===E);if(N<n.joins.length-1){const D=[...n.joins],w={...D[N]},M={...D[N+1]};D[N]=M,D[N+1]=w,D.forEach((R,I)=>{R.sequence=I+1});const k={...n,joins:D};r(k)}},toggleJoinExpansion:E=>{const N=new Set(l);N.has(E)?N.delete(E):N.add(E),o(N)},getAvailableSourceEntities:E=>{if(n.joins.length===0)return a;const N=n.joins.findIndex(w=>w.id===E);if(N===0)return a;const D=new Set;return n.baseEntity.name&&D.add(n.baseEntity.name),n.joins.slice(0,N).forEach(w=>{D.add(w.source.name),D.add(w.target.name)}),a.filter(w=>D.has(w.name))},getAvailableTargetEntities:E=>a.filter(N=>N.name!==E),addJoinKeyGroup:E=>{const N=n.joins.find(w=>w.id===E);if(!N)return;const D=[...N.joinKeyGroups,{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}];d(E,{joinKeyGroups:D})},updateGroupOperator:(E,N,D)=>{const w=n.joins.find(k=>k.id===E);if(!w)return;const M=[...w.joinKeyGroups];M[N]={...M[N],operator:D},d(E,{joinKeyGroups:M})},removeJoinKeyGroup:(E,N)=>{const D=n.joins.find(M=>M.id===E);if(!D)return;const w=[...D.joinKeyGroups];w.splice(N,1),d(E,{joinKeyGroups:w})},addJoinKey:(E,N)=>{const D=n.joins.find(I=>I.id===E);if(!D)return;const w=a.find(I=>I.name===D.source.name),M=a.find(I=>I.name===D.target.name),k=[...D.joinKeyGroups],R={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",entityId:(w==null?void 0:w.name)||"",entityName:(w==null?void 0:w.name)||"",entityType:(w==null?void 0:w.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",entityId:(M==null?void 0:M.name)||"",entityName:(M==null?void 0:M.name)||"",entityType:(M==null?void 0:M.type)||"table"},operator:"="};k[N]={...k[N],keys:[...k[N].keys,R]},d(E,{joinKeyGroups:k})},updateJoinKey:(E,N,D,w,M)=>{const k=n.joins.find(L=>L.id===E);if(!k)return;const R=a.find(L=>L.name===k.source.name),I=a.find(L=>L.name===k.target.name),v=[...k.joinKeyGroups],y=v[N].keys[D];w==="sourceColumnName"&&R?v[N].keys[D]={...y,source:{...y.source,name:M}}:w==="targetColumnName"&&I?v[N].keys[D]={...y,target:{...y.target,name:M}}:w==="operator"&&(v[N].keys[D]={...y,operator:M}),d(E,{joinKeyGroups:v})},removeJoinKey:(E,N,D)=>{const w=n.joins.find(k=>k.id===E);if(!w)return;const M=[...w.joinKeyGroups];M[N].keys.splice(D,1),d(E,{joinKeyGroups:M})},generateSQLPreview:()=>{const E=[...n.joins].sort((w,M)=>w.sequence-M.sequence),N=n.baseEntity;if(!N.name)return"SELECT * FROM table -- Please select a base entity";let D=`SELECT *
415
415
  FROM ${N.name}`;return E.forEach(w=>{const M=a.find(I=>I.name===w.target.name);if(!M)return;const k=w.joinKeyGroups.filter(I=>I.keys&&I.keys.length>0).map(I=>{const v=I.keys.filter(y=>{var L,F,P,_;return((L=y.source)==null?void 0:L.name)&&((F=y.target)==null?void 0:F.name)&&((P=y.source)==null?void 0:P.entityName)&&((_=y.target)==null?void 0:_.entityName)}).map(y=>`${y.source.entityName}.${y.source.name} ${y.operator} ${y.target.entityName}.${y.target.name}`);return v.length>1?`(${v.join(` ${I.operator} `)})`:v[0]}).filter(Boolean),R=k.length>1?k.join(" OR "):k[0]||"condition_here";D+=`
416
416
  ${w.joinType} JOIN ${M.name}
417
417
  ON ${R}`}),D},isValidConfiguration:()=>!(!n.baseEntity.name||n.joins.some(w=>!w.source.name||!w.target.name)||n.joins.some(w=>w.joinKeyGroups.some(M=>M.keys.length===0))||n.joins.some(w=>w.joinKeyGroups.some(M=>M.keys.some(k=>!k.source.name||!k.target.name||!k.operator))))}}function ol({joinPlan:t,availableDataSources:a,onSave:n,onRemove:r,onCancel:l}){const{setCardConfig:o}=s.useEditorActions();s.useEditorStore(k=>k.card.config);const{columnGroups:c}=s.useColumns(),{joinPlan:i,expandedJoins:d,setJoinPlan:m,addJoin:h,removeJoin:u,updateJoin:p,moveJoinUp:f,moveJoinDown:j,toggleJoinExpansion:x,getAvailableSourceEntities:T,getAvailableTargetEntities:g,addJoinKeyGroup:b,updateGroupOperator:C,removeJoinKeyGroup:O,addJoinKey:S,updateJoinKey:E,removeJoinKey:N,generateSQLPreview:D,isValidConfiguration:w}=ll({initialJoinPlan:t,availableDataSources:a}),M=k=>{const R=c.find(I=>I.entityName===k);return(R==null?void 0:R.columns)||[]};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Base Entity (FROM clause)"}),e.jsxs(s.Select,{value:i.baseEntity.name,onValueChange:k=>{const R=a.find(I=>I.name===k);m({...i,baseEntity:{connectionId:(R==null?void 0:R.connectionId)||"",connectionType:(R==null?void 0:R.connectionType)||"none",type:(R==null?void 0:R.type)||"table",name:k,database:R==null?void 0:R.database,schema:R==null?void 0:R.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select base entity..."})}),e.jsx(s.SelectContent,{children:a.map(k=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:k.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:k.type}),k.name,e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]})},k.name))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This entity will be used as the base table in the FROM clause of the SQL query."})]}),e.jsxs("div",{className:"rounded-md border p-3",children:[e.jsx("div",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Join SQL Preview"}),e.jsx("code",{className:"block whitespace-pre-wrap break-all text-xs",children:D()})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(s.Label,{children:["Joins Configuration (",i.joins.length,")"]}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:h,children:[e.jsx(s.Plus,{className:"mr-1 h-4 w-4"}),"Add Join"]})})]}),i.joins.length===0?e.jsx("div",{className:"rounded-lg border border-dashed py-8 text-center text-muted-foreground",children:e.jsx("p",{children:"No joins configured. Add a join to connect multiple data sources."})}):e.jsx("div",{className:"max-h-[300px] space-y-4 overflow-y-auto",children:i.joins.map(k=>{const R=a.find(y=>y.name===k.source.name),I=a.find(y=>y.name===k.target.name),v=d.has(k.id);return e.jsxs(s.Card,{className:"border",children:[e.jsx(s.CardHeader,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{children:k.sequence}),e.jsxs(s.CardTitle,{className:"text-sm",children:["Join ",k.sequence,R&&I&&e.jsxs("span",{className:"ml-2 font-normal text-gray-600",children:["(",R.name," → ",I.name,")"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>f(k.id),disabled:k.sequence===1,children:e.jsx(s.ArrowUp,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>j(k.id),disabled:k.sequence===i.joins.length,children:e.jsx(s.ArrowDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>x(k.id),children:v?e.jsx(s.ChevronUp,{className:"h-4 w-4"}):e.jsx(s.ChevronDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>u(k.id),children:e.jsx(s.X,{className:"h-4 w-4"})})]})]})}),v&&e.jsxs(s.CardContent,{className:"space-y-4 pb-6",children:[e.jsxs("div",{className:"grid grid-cols-1 gap-4 md:grid-cols-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Source"}),e.jsxs(s.Select,{value:k.source.name,onValueChange:y=>{const L=a.find(F=>F.name===y);p(k.id,{source:{connectionId:(L==null?void 0:L.connectionId)||"",connectionType:(L==null?void 0:L.connectionType)||"none",type:(L==null?void 0:L.type)||"table",name:y,database:L==null?void 0:L.database,schema:L==null?void 0:L.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select source..."})}),e.jsx(s.SelectContent,{children:T(k.id).map(y=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:y.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:y.type}),y.name]})},y.name))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Join Type"}),e.jsxs(s.Select,{value:k.joinType,onValueChange:y=>p(k.id,{joinType:y}),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"INNER",children:"INNER JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"LEFT",children:"LEFT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"RIGHT",children:"RIGHT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"FULL",children:"FULL JOIN"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Target"}),e.jsxs(s.Select,{value:k.target.name,onValueChange:y=>{const L=a.find(F=>F.name===y);p(k.id,{target:{connectionId:(L==null?void 0:L.connectionId)||"",connectionType:(L==null?void 0:L.connectionType)||"none",type:(L==null?void 0:L.type)||"table",name:y,database:L==null?void 0:L.database,schema:L==null?void 0:L.schema}})},disabled:!k.source.name,children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select target..."})}),e.jsx(s.SelectContent,{children:k.source.name?g(k.source.name).map(y=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:y.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:y.type}),y.name,y.name===k.source.name&&e.jsx(s.Badge,{variant:"outline",className:"text-xs text-yellow-600",children:"(Self-join)"})]})},y.name)):[]})]})]})]}),k.source.name&&k.target.name&&R&&I&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{children:"Join Conditions"}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",onClick:()=>b(k.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition Group"]})]}),k.joinKeyGroups.length===0?e.jsx("div",{className:"rounded-md border p-3 text-center text-sm text-gray-500",children:"No join conditions defined. Add condition groups to specify how data sources should be joined."}):e.jsx("div",{className:"space-y-4",children:k.joinKeyGroups.map((y,L)=>e.jsxs("div",{className:"space-y-3 rounded-md border p-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Label,{children:["Group ",L+1]}),L>0&&e.jsxs("div",{className:"flex items-center gap-2 rounded-md bg-gray-100 px-2 py-1",children:[e.jsx(s.Label,{className:"text-xs font-normal",children:"Join with:"}),e.jsx("span",{className:"text-sm font-medium",children:"OR"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Select,{value:y.operator,onValueChange:F=>C(k.id,L,F),children:[e.jsx(s.SelectTrigger,{className:"w-[100px]",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"AND",children:"AND"}),e.jsx(s.SelectItem,{value:"OR",children:"OR"})]})]}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",onClick:()=>O(k.id,L),children:e.jsx(s.Trash2,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"space-y-2",children:[y.keys.length===0?e.jsx("div",{className:"rounded-md border border-dashed p-3 text-center text-sm text-gray-500",children:"No conditions in this group. Add conditions to specify join criteria."}):e.jsx("div",{className:"space-y-2",children:y.keys.map((F,P)=>e.jsxs("div",{className:"flex flex-col items-start gap-2 rounded-md border p-2 sm:flex-row sm:items-center",children:[P>0&&e.jsx("div",{className:"self-center rounded-md bg-gray-100 px-2 py-1 text-xs font-medium",children:y.operator}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:F.source.name,onValueChange:_=>E(k.id,L,P,"sourceColumnName",_),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Source column"})}),e.jsx(s.SelectContent,{children:M(k.source.name).map(_=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:_.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between",children:[e.jsx("span",{children:_.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:_.dataType})]})},_.name))})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:w-20",children:e.jsxs(s.Select,{value:F.operator,onValueChange:_=>E(k.id,L,P,"operator",_),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"=",children:"="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<",children:"<"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">",children:">"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<=",children:"<="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">=",children:">="})]})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:F.target.name,onValueChange:_=>E(k.id,L,P,"targetColumnName",_),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Target column"})}),e.jsx(s.SelectContent,{children:M(k.target.name).map(_=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:_.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between hover:bg-muted",children:[e.jsx("span",{children:_.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:_.dataType})]})},_.name))})]})}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",className:"self-center",onClick:()=>N(k.id,L,P),children:e.jsx(s.X,{className:"size-3.5"})})]},F.id||P))}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",className:"mt-2",onClick:()=>S(k.id,L),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition"]})]})]},y.id))})]})]})]},k.id)})})]}),e.jsxs("div",{className:"flex justify-end gap-2 border-t pt-4",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>{r(),l()},children:"Remove All"}),e.jsx(s.Button,{variant:"outline",onClick:l,children:"Cancel"}),e.jsx(s.Button,{onClick:()=>n(i),disabled:!w(),children:"Save"})]})]})}function Ys({children:t,open:a,onOpenChange:n,joinPlan:r,availableDataSources:l,onRemove:o,onSave:c,onCancel:i}){return e.jsxs(s.Dialog,{open:a,onOpenChange:n,children:[e.jsxs(s.DialogContent,{className:"max-w-4xl",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Join Configuration"}),e.jsx(s.DialogDescription,{children:"Configure the join between the two entities to combine the fields."})]}),e.jsx(ol,{joinPlan:r,availableDataSources:l,onSave:c,onCancel:i,onRemove:o})]}),t]})}function il({joins:t,baseEntityId:a,availableDataSources:n,generateSQLPreview:r}){return e.jsxs("div",{className:"divide-y divide-border",children:[t.length>0&&e.jsxs("div",{className:"p-4",children:[e.jsx("h3",{className:"mb-3 text-left font-semibold",children:"Join Chain"}),e.jsx("div",{className:"flex flex-wrap items-center gap-2",children:(()=>{const l=[...t].sort((c,i)=>c.sequence-i.sequence),o=n.find(c=>c.name===a);return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:o==null?void 0:o.type}),e.jsx("span",{className:"text-xs font-medium",children:o==null?void 0:o.name}),e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]}),l.map(c=>{const i=n.find(d=>d.name===c.target.name);return e.jsxs("div",{className:"flex items-center",children:[e.jsxs("div",{className:"mx-1 flex items-center gap-1",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:c.joinType}),e.jsx("span",{className:"text-muted-foreground",children:"→"})]}),e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:i==null?void 0:i.type}),e.jsx("span",{className:"text-xs font-medium",children:i==null?void 0:i.name})]})]},c.id)})]})})()})]}),e.jsxs("div",{className:"p-4",children:[e.jsxs("h3",{className:"mb-3 text-left text-sm font-medium",children:["Configured Joins (",t.length,")"]}),e.jsx("div",{className:"space-y-2",children:[...t].sort((l,o)=>l.sequence-o.sequence).map((l,o)=>{const c=n.find(d=>d.name===l.source.name),i=n.find(d=>d.name===l.target.name);return e.jsxs("div",{className:"flex items-center gap-2 rounded-md bg-muted p-2 text-left",children:[e.jsx(s.Badge,{variant:"secondary",className:"shrink-0 text-xs",children:o+1}),e.jsx(s.Badge,{variant:"outline",className:"shrink-0 text-xs",children:l.joinType}),e.jsxs("span",{className:"text-xs font-medium",children:[c==null?void 0:c.name," → ",i==null?void 0:i.name]}),e.jsxs(s.Badge,{variant:"outline",className:"ml-auto text-xs",children:[l.joinKeyGroups.reduce((d,m)=>d+m.keys.length,0)," ","condition",l.joinKeyGroups.reduce((d,m)=>d+m.keys.length,0)>1?"s":""]})]},l.id)})})]}),e.jsxs("div",{className:"p-4",children:[e.jsx("h3",{className:"mb-3 text-left text-sm font-medium",children:"SQL Query"}),e.jsx("div",{className:"overflow-x-auto rounded-md border bg-muted p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap break-all text-left font-mono text-xs text-muted-foreground",children:r()})})]})]})}function cl(){var h,u;const t=s.useEditorStore(p=>p.card),a=s.useEditorStore(p=>p.actions.updateCardConfig),n=((h=t==null?void 0:t.config)==null?void 0:h.joinPlan)||{baseEntity:{type:"table",name:"",database:"",schema:"",connectionId:"",connectionType:"none",dialect:"none"},joins:[]},[r,l]=A.useState(!1),o=((u=t==null?void 0:t.dataSource)==null?void 0:u.selectedEntities)||[],c=p=>{a(t.id,{...t==null?void 0:t.config,joinPlan:p}),l(!1)},i=()=>{a(t.id,{...t==null?void 0:t.config,joinPlan:void 0})},d=()=>{l(!1)},m=()=>{const p=[...n.joins].sort((x,T)=>x.sequence-T.sequence),f=n.baseEntity;if(!f.name)return"SELECT * FROM table -- Please select a base entity";let j=`SELECT *