react-semaphor 0.0.466 → 0.0.468
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.
- package/dist/chunks/{index-C6TRp4Lo.js → index-Biwh9UaV.js} +3728 -3677
- package/dist/chunks/{index-lNSgVyQ4.js → index-vwhwdEV6.js} +68 -63
- package/dist/dashboard/index.cjs +1 -1
- package/dist/dashboard/index.js +1 -1
- package/dist/index.cjs +7 -7
- package/dist/index.js +1718 -1688
- package/dist/style.css +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./chunks/index-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./chunks/index-vwhwdEV6.js"),Wt=require("./types/index.cjs"),e=require("react/jsx-runtime"),T=require("react");require("react-dom");function Kt(s){const a=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const n in s)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(s,n);Object.defineProperty(a,n,r.get?r:{enumerable:!0,get:()=>s[n]})}}return a.default=s,Object.freeze(a)}const le=Kt(T);/**
|
|
2
2
|
* @license lucide-react v0.453.0 - ISC
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the ISC license.
|
|
@@ -328,7 +328,7 @@
|
|
|
328
328
|
*
|
|
329
329
|
* This source code is licensed under the ISC license.
|
|
330
330
|
* See the LICENSE file in the root directory of this source tree.
|
|
331
|
-
*/const
|
|
331
|
+
*/const Ps=t.createLucideIcon("Square",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}]]);/**
|
|
332
332
|
* @license lucide-react v0.453.0 - ISC
|
|
333
333
|
*
|
|
334
334
|
* This source code is licensed under the ISC license.
|
|
@@ -338,7 +338,7 @@
|
|
|
338
338
|
*
|
|
339
339
|
* This source code is licensed under the ISC license.
|
|
340
340
|
* See the LICENSE file in the root directory of this source tree.
|
|
341
|
-
*/const
|
|
341
|
+
*/const qs=t.createLucideIcon("Tag",[["path",{d:"M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z",key:"vktsd0"}],["circle",{cx:"7.5",cy:"7.5",r:".5",fill:"currentColor",key:"kqv944"}]]);/**
|
|
342
342
|
* @license lucide-react v0.453.0 - ISC
|
|
343
343
|
*
|
|
344
344
|
* This source code is licensed under the ISC license.
|
|
@@ -358,7 +358,7 @@
|
|
|
358
358
|
*
|
|
359
359
|
* This source code is licensed under the ISC license.
|
|
360
360
|
* See the LICENSE file in the root directory of this source tree.
|
|
361
|
-
*/const Us=t.createLucideIcon("Type",[["polyline",{points:"4 7 4 4 20 4 20 7",key:"1nosan"}],["line",{x1:"9",x2:"15",y1:"20",y2:"20",key:"swin9y"}],["line",{x1:"12",x2:"12",y1:"4",y2:"20",key:"1tx1rr"}]]);function _e(s){var a;return{...s,sheets:(a=s==null?void 0:s.sheets)==null?void 0:a.map(n=>{var r;return{...n,frames:(r=n.frames)==null?void 0:r.map(o=>{var l;return{...o,cards:(l=o.cards)==null?void 0:l.map(i=>ft(i))}})}})}}function ft(s){return{...s,customCfg:$s(s.customCfg),data:[]}}function $s(s){var a,n;return s?{...s,data:{...s==null?void 0:s.data,labels:[],datasets:(n=(a=s==null?void 0:s.data)==null?void 0:a.datasets)==null?void 0:n.map(r=>({...r,data:[]}))}}:null}async function Qs(s,a,n,r){if(!r)return null;const o=JSON.stringify(a);return await t.postRequest("https://semaphor.cloud/api/v1/lens",r,{name:s,template:o,filterValues:n,operation:"create"})}async function Le(s,a){if(!a)return null;const{id:n,template:r,filterValues:o,isDefault:l,shared:i}=s;return await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{lensId:n,template:r,filterValues:o,isDefault:l,shared:i,operation:"update"})}async function Js(s,a){return a?await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{operation:"delete",lensId:s}):null}function Ws(){const[s,a]=T.useState(""),[n,r]=T.useState(!1),o=t.useDashboardStore(f=>f.lenses),{setLenses:l,setSelectedLensId:i}=t.useDashboardActions(),[m,c]=T.useState(!1),[h,u]=T.useState(!1),x=t.useDashboardStore(f=>f.dashboard),v=t.useDashboardStore(f=>f.filterValues),{authToken:d}=t.useSemaphorContext();async function g(){c(!0);const f=_e(x),y=await Qs(s,f,v,d==null?void 0:d.accessToken);y&&(l([...o||[],y]),i(y.id),u(!1)),c(!1)}return e.jsxs(t.Dialog,{open:h,onOpenChange:u,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Lens"]})}),e.jsxs(t.DialogContent,{className:"sm:max-w-[425px]",children:[e.jsxs(t.DialogHeader,{children:[e.jsx(t.DialogTitle,{children:"Create Lens"}),e.jsx(t.DialogDescription,{children:"Lenses are private by default and are only visible to you."})]}),e.jsx("div",{className:"grid gap-4 py-4",children:e.jsxs("div",{className:"grid grid-cols-8 items-center gap-4",children:[e.jsx(t.Label,{className:"text-right",children:"Name"}),e.jsx(t.Input,{value:s,onChange:f=>{a(f.target.value)},id:"name",className:"col-span-7 focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0"})]})}),n&&e.jsx("p",{className:"text-sm",children:"The lens name already exists. Use a differnet name."}),e.jsx(t.DialogFooter,{children:e.jsx(t.Button,{size:"sm",variant:"outline",type:"submit",onClick:g,disabled:m,children:m?"Creating...":"Add"})})]})]})}function Ks(){const[s,a]=T.useState(!1),n=t.useDashboardStore(u=>u.lenses),r=t.useDashboardStore(u=>u.selectedLensId),{setLenses:o,setSelectedLensId:l}=t.useDashboardActions(),{authToken:i}=t.useSemaphorContext();async function m(u){l(u)}async function c(u){if(await Js(u,i==null?void 0:i.accessToken)){const v=n==null?void 0:n.filter(d=>d.id!==u);o(v||[]),l("original")}}async function h(u){const x=n==null?void 0:n.map(g=>g.id===u.id?{...g,isDefault:!u.isDefault}:{...g,isDefault:!1});o(x||[]);const v={...u,isDefault:!u.isDefault};await Le(v,i==null?void 0:i.accessToken);const d=n==null?void 0:n.find(g=>g.isDefault);if(d&&d.id!==u.id){const g={...d,isDefault:!1};await Le(g,i==null?void 0:i.accessToken)}}return e.jsxs(t.DropdownMenu,{open:s,onOpenChange:a,children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Lenses",className:"h-7 p-2",children:e.jsx(ws,{className:"h-4 w-4"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",className:"w-60",children:[e.jsx(t.DropdownMenuLabel,{className:"px-3",children:"Lenses"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsxs(t.DropdownMenuRadioGroup,{value:r,onValueChange:m,children:[e.jsx(t.DropdownMenuRadioItem,{value:"original",children:"Original"}),n==null?void 0:n.map(u=>e.jsxs(t.DropdownMenuRadioItem,{className:"group flex justify-between gap-2",value:u.id,children:[e.jsx("p",{children:u.name}),e.jsxs("span",{className:"flex items-center gap-3",children:[e.jsx(Is,{onClick:x=>{x.preventDefault(),h(u)},className:t.cn("size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",{invisible:!u.isDefault,"text-green-500":u.isDefault}),strokeWidth:1.5}),e.jsx(t.Cross2Icon,{onClick:x=>{x.preventDefault(),c(u.id)},className:"invisible size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",strokeWidth:1.5})]})]},u.id))]})]})]})}function gt(){var l;const s=t.useDashboardStore.getState().authToken,a=t.useDashboardStore.getState().theme,n=t.useDashboardStore.getState().filterValues,r=(l=t.useDashboardStore.getState().dashboard)==null?void 0:l.title,o=t.useDashboardStore.getState().selectedSheetId;return{authToken:s,theme:a||"light",filterValues:n,dashboardTitle:r||"",selectedSheetId:o||""}}async function jt(){const s=t.useDashboardStore.getState().isDownloadingPdf,{authToken:a,theme:n,filterValues:r,dashboardTitle:o,selectedSheetId:l}=gt();if(s)return;const i=new URLSearchParams;i.set("token",(a==null?void 0:a.accessToken)||""),i.set("theme",n),i.set("filterValues",JSON.stringify(r)),i.set("selectedSheetId",l||"");const m=`https://semaphor.cloud/api/v1/generate-pdf?params=${encodeURIComponent(i.toString())}`;t.useDashboardStore.setState({isDownloadingPdf:!0});const u=(await(await fetch(m,{method:"GET",headers:{Authorization:`Bearer ${a==null?void 0:a.accessToken}`}})).json()).url,x=document.createElement("a");x.href=u;const v=`${o}-${new Date().toISOString()}.pdf`;x.download=v,x.click(),t.useDashboardStore.setState({isDownloadingPdf:!1})}function Ys({iconClassName:s}){const{authToken:a}=t.useSemaphorContext();t.useDashboardStore(o=>{var l;return(l=o.dashboard)==null?void 0:l.title}),t.useDashboardStore(o=>o.theme),t.useDashboardStore(o=>o.filterValues);const n=t.useDashboardStore(o=>o.isDownloadingPdf);async function r(){await jt()}return e.jsx(t.IconButton,{tooltip:"Download PDF",className:"h-7 p-2",onClick:r,children:n?e.jsx(ae,{className:t.cn("size-3.5 animate-spin",s)}):e.jsx(t.Download,{className:t.cn("size-4",s)})})}function Y({defaultValue:s,label:a,options:n,value:r,onValueChange:o,className:l,icon:i,showPlaceholderLabel:m=!0}){return e.jsxs(t.Select,{defaultValue:s,value:r,onValueChange:o,children:[e.jsx(t.SelectTrigger,{className:t.cn("w-full focus:ring-0 focus:ring-offset-0",l),children:e.jsxs("div",{className:"flex items-center gap-2",children:[i&&e.jsx("span",{children:i}),m&&e.jsx(t.SelectValue,{placeholder:a})]})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:a}),n.map(c=>e.jsx(t.SelectItem,{value:c.value,children:c.label},c.value))]})})]})}const Je=[{value:"daily",label:"Daily"},{value:"weekly",label:"Weekly"},{value:"biweekly",label:"Bi-Weekly"},{value:"monthly",label:"Monthly"}];function yt({triggerButtonClassName:s}){const{data:a,handleScheduleSubscription:n,handleDeleteSubscription:r,status:o,error:l,isDeleting:i}=Zs(),[m,c]=T.useState(""),[h,u]=T.useState(Je[0].value),[x,v]=T.useState("06:00"),[d,g]=T.useState("");return e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Schedule Dashboard",className:t.cn("h-7 p-2",s),children:e.jsx(xs,{className:"size-4"})})}),e.jsx(t.PopoverContent,{align:"end",className:"w-80",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:"Schedule Subscription"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Subscribe to receive dashboard via email"})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"email",children:"Email"}),e.jsx(t.Input,{id:"email",value:m,onChange:f=>c(f.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"subject",children:"Subject"}),e.jsx(t.Input,{id:"subject",placeholder:"Monthly Sales Report",value:d,onChange:f=>{g(f.target.value.replace(/[^a-zA-Z0-9\s-_]/g,""))},className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"frequency",children:"Frequency"}),e.jsx(Y,{className:"col-span-2 h-8",label:"Select Frequency",options:Je,value:h,onValueChange:f=>u(f)})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"time",children:"Time"}),e.jsx(t.Input,{id:"time",type:"time",value:x,onChange:f=>v(f.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsx("div",{className:"mt-2 flex justify-end",children:e.jsxs(t.Button,{onClick:async()=>{await n({email:m,frequency:h,time:x,subject:d}),v("00:00"),c(""),g("")},className:"h-8 p-2",disabled:o==="loading"||!m,children:[o==="loading"&&e.jsx(ae,{className:"mr-2 size-4 animate-spin"}),o==="success"&&e.jsx(t.Check,{className:"mr-2 size-4"}),o==="error"&&e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{children:e.jsx(at,{className:"mr-2 size-4 text-destructive"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{children:l})})]}),"Schedule"]})}),a&&a.length>0&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Subscriptions"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Active email subscriptions"})]}),t.isDevEnv]})}),e.jsx("div",{className:"grid gap-2",children:a==null?void 0:a.map(f=>e.jsxs("div",{className:"group grid grid-cols-5 gap-3",children:[e.jsx("div",{className:"col-span-3 text-sm",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"boder-border rounded-md border bg-muted px-2 py-1",children:[t.titleCase(f.frequency)," "]}),e.jsx(t.Label,{children:f.time})]})}),e.jsxs("div",{className:"col-span-2 flex items-center justify-end gap-4",children:[e.jsx(We,{tooltip:`Next Run: ${new Date(f.nextRun).toLocaleString()} ${f.timezone}`,children:e.jsx(hs,{className:"size-3.5"})}),e.jsx(We,{tooltip:`${f.endUserEmail}`,children:e.jsx(Os,{className:"size-3.5"})}),i===f.id?e.jsx(ae,{className:"size-3.5 animate-spin"}):e.jsx(Gs,{onClick:()=>r(f.id),className:"size-3.5 cursor-pointer"})]})]},f.id))})]})]})]})})]})})}const We=({children:s,tooltip:a})=>e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:s}),e.jsx(t.TooltipContent,{children:a})]})});function Zs(){const[s,a]=T.useState("idle"),[n,r]=T.useState(""),[o,l]=T.useState(null),i=t.useDashboardStore(N=>N.authToken),m=t.useDashboardStore(N=>N.dashboard.id),c=t.useDashboardStore(N=>N.dashboard.title),{endUserId:h}=t.getTenantAndEndUserId((i==null?void 0:i.accessToken)||""),u="https://semaphor.cloud/api/v1/subscriptions/get",x={dashboardId:m,endUserId:h},{data:v,isLoading:d,isError:g,isFetching:f,refetch:y}=t.useQuery({queryKey:["subscriptions",m,h],queryFn:()=>t.getRequest(u,(i==null?void 0:i.accessToken)||"",x),enabled:!!(i!=null&&i.accessToken)&&!!m&&!!h});async function p({email:N,subject:M,frequency:w,time:C}){const{authToken:O,...V}=gt(),U=Intl.DateTimeFormat().resolvedOptions().timeZone,k={email:N,frequency:w,time:C,timezone:U,endUserId:h,endUserEmail:N,subject:M||c||"Scheduled Dashboard",dashboardId:m,dashboardParams:V};a("loading");const z="https://semaphor.cloud/api/v1/subscriptions/create";try{const G=await t.postRequestRegular(z,(O==null?void 0:O.accessToken)||"",{...k});a("success"),r(""),y(),setTimeout(()=>{a("idle")},2e3)}catch(G){console.error(G),a("error"),r(G.message)}}async function S(N){const M="https://semaphor.cloud/api/v1/subscriptions/delete";l(N);try{await t.postRequestRegular(M,(i==null?void 0:i.accessToken)||"",{id:N}),y(),setTimeout(()=>{l(null)},2e3)}catch(w){console.error(w),l(null),a("error"),r(w.message)}}return{data:v,isLoading:d,isFetching:f,isError:g,isDeleting:o,refetch:y,handleScheduleSubscription:p,handleDeleteSubscription:S,status:s,error:n}}function Xs(){const{authToken:s}=t.useSemaphorContext();t.useDashboardStore(k=>k.theme);const a=t.useDashboardStore(k=>k.lenses),n=t.useDashboardStore(k=>k.selectedLensId),r=a==null?void 0:a.find(k=>k.id===n),o=t.useDashboardStore(k=>k.dashboard);t.useDashboardStore(k=>k.selectedSheetId);const l=t.useDashboardStore(k=>k.isDashboardEditing),i=t.useEditorStore(k=>k.selectedConnectionId),[m,c]=T.useState(!1),h=t.useDashboardStore(k=>k.filterValues),u=t.useDashboardStore(k=>k.isVisualEditing),x=t.useDashboardStore(k=>k.showFilters);t.useDashboardStore(k=>k.showDashboardJSON);const v=t.useDashboardStore(k=>k.onSaveFunction),d=t.useDashboardStore(k=>k.actions.setShowFilters),{addFrame:g,setSelectedFrameId:f}=t.useDashboardStore(k=>k.actions),{getSelectedSheet:y,setIsDashboardEditing:p,setShowDashboardJSON:S}=t.useDashboardStore(k=>k.actions),N=y(),{tenantId:M,endUserId:w}=T.useMemo(()=>t.getTenantAndEndUserId(s==null?void 0:s.accessToken),[s==null?void 0:s.accessToken]),C=T.useMemo(()=>M||w,[M,w]),O=()=>{if(!l||!N)return;const k={connectionId:i,id:t.v4(),title:"Title",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]},z={id:t.v4(),cards:[k],activeCardId:k.id};g(N.id,z),f(z.id)},V=()=>{p(!0)};async function U(){if(c(!0),!n||!r)return;const k=_e(o),z=JSON.stringify(k),G={...r,template:z,filterValues:h};await Le(G,s==null?void 0:s.accessToken),t.ue.success("Lens saved"),v==null||v(k),c(!1)}return u?null:e.jsxs("div",{className:"flex items-center justify-between gap-2 border-b py-1 pl-3 pr-2",role:"dashboard-controls",children:[!u&&e.jsx("div",{className:"flex items-center gap-3",children:e.jsx(t.Label,{children:r==null?void 0:r.name})}),e.jsxs("div",{className:"flex gap-2",children:[!1,!u&&e.jsx(Ys,{}),!u&&e.jsx(yt,{}),l&&!u&&C&&e.jsx(Ws,{}),!u&&C&&e.jsx(Ks,{}),l&&!u&&e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",disabled:!l,onClick:O,children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Frame"]}),!l&&e.jsx(t.Button,{variant:"ghost",className:"h-7 p-2",onClick:V,size:"sm",children:e.jsx(t.Pencil,{className:"h-4 w-4"})}),l&&!u&&e.jsx(t.IconButton,{className:"h-7 p-2",onClick:()=>{p(!1),S(!1)},children:e.jsx(t.Cross2Icon,{className:"h-4 w-4"})}),l&&!u&&C&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",disabled:m,onClick:U,size:"sm",children:e.jsx(Vs,{className:"h-4 w-4"})}),!x&&!u&&l&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",onClick:()=>d(!0),size:"sm",children:e.jsx(lt,{className:"h-4 w-4"})}),!u&&l&&!1,!u&&!1]})]})}function en({name:s,items:a,value:n,onChange:r,isLoading:o,isError:l,refetch:i,className:m,icon:c,showNone:h=!1}){const u=le.useRef(null),[x,v]=le.useState(!1),[d,g]=le.useState("100%"),f=h?[...a||[],{id:"none",name:"None"}]:a;le.useLayoutEffect(()=>{u.current&&g(u.current.offsetWidth)},[]);function y(){var S;return o?"Loading...":l?"Error":n?(S=a==null?void 0:a.find(N=>N.id===n))==null?void 0:S.name:`Select ${s}`}function p(S,N){r(N),v(!1)}return e.jsxs(t.Popover,{open:x,onOpenChange:v,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{ref:u,variant:"outline",role:"combobox","aria-expanded":x,className:t.cn("flex h-9 w-full justify-between",m),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[c&&e.jsx("div",{className:"mr-2",children:c}),e.jsx("span",{className:"max-w-[150px] overflow-hidden truncate text-ellipsis whitespace-nowrap",children:y()})]}),e.jsx(t.ChevronDown,{className:"size-4 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-full p-0",style:{minWidth:d},children:e.jsxs(t.Command,{children:[e.jsxs("div",{className:"flex h-9 items-center justify-between border-b pr-1",children:[e.jsx(t.CommandInput,{className:"h-9",placeholder:"Search..."}),e.jsx(t.IconButton,{className:"h-7",onClick:i,children:e.jsx(mt,{className:t.cn("size-3.5 opacity-50",o&&"animate-spin")})})]}),e.jsxs(t.CommandList,{children:[e.jsxs(t.CommandEmpty,{children:["No ",s==null?void 0:s.toLowerCase()," found."]}),e.jsx(t.CommandGroup,{children:f==null?void 0:f.map(S=>e.jsx(t.CommandItem,{className:"overflow-x-auto",value:S.name,onSelect:N=>p(N,S.id),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Check,{className:t.cn("size-4",n===S.id?"opacity-100":"opacity-0")}),e.jsx("div",{title:S.name,children:S.name})]})},S.id))})]})]})})]})}function ie({title:s,items:a,value:n,isLoading:r,isError:o,refetch:l,onChange:i,showNone:m=!1,className:c,disabled:h=!1,type:u="combobox"}){const x=r?"Loading...":o?"Error":n;function v(){if(s==="Connection")return e.jsx(Rs,{className:"size-4"});if(s==="Database")return e.jsx(js,{className:"size-4"});if(s==="Schema")return e.jsx(Fs,{className:"size-4"});if(s==="Table")return e.jsx(Ve,{className:"size-4"});if(s==="Model")return e.jsx(ds,{className:"size-4"})}function d(){return e.jsxs(t.Select,{disabled:h,value:x,onValueChange:i,children:[e.jsx(t.SelectTrigger,{className:t.cn("h-9 w-full border-muted focus:ring-1 focus:ring-offset-0",c),children:e.jsx(t.SelectValue,{placeholder:r?"Loading...":o?"Error":`Select ${s}`})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[r?e.jsx(t.SelectLabel,{children:"Loading..."}):e.jsxs(t.SelectLabel,{className:"flex items-center justify-between gap-2",children:[s,e.jsx(mt,{className:t.cn("mr-1 size-3 cursor-pointer text-muted-foreground hover:text-foreground",r&&"animate-spin"),onClick:l})]}),e.jsxs(e.Fragment,{children:[m&&e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),a&&a.map((f,y)=>e.jsx(t.SelectItem,{value:f.id,children:f.name},y))]}),(r||o)&&e.jsx(t.SelectItem,{value:x,children:x})]})})]})}function g(){return e.jsx(en,{icon:v(),items:a,value:x,onChange:i||(()=>{}),isLoading:r,isError:o,refetch:l,name:s,showNone:m})}return e.jsx(t.TooltipProvider$1,{delayDuration:0,children:e.jsx("div",{className:"flex items-center gap-2",children:u==="select"?d():g()})})}function tn({column:s}){const[a,n]=T.useState(!1),r=t.useEditorStore(f=>f.selectedTableName),o=t.useEditorStore(f=>f.selectedSchemaName),l=t.useEditorStore(f=>f.selectedDatabaseName),i=t.useEditorStore(f=>f.selectedDatamodelId),m=t.useEditorStore(f=>f.sqlGen),{modelItems:c,connectionType:h}=t.useEditorAside(),u=t.useEditorStore(f=>f.actions.setSqlGen),x=t.useEditorStore(f=>f.actions.setCardSql),v=t.useEditorStore(f=>f.actions.setRunSql);function d(f,y){var M;const p=t.createSqlGenConfig(f,m,y),S=(M=c==null?void 0:c.find(w=>w.id===i))==null?void 0:M.name;u(p);const N=t.getSql(p,t.fmt(o||""),t.fmt(r),S,h,l||"");N&&(x(N),v(!0)),n(!1)}function g(){return e.jsxs("ul",{className:"text-xs",children:[e.jsx("li",{onClick:()=>d(s,"COUNT"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Count"}),e.jsx("li",{onClick:()=>d(s,"AVG"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Avg"}),e.jsx("li",{onClick:()=>d(s,"SUM"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Sum"}),e.jsx("li",{onClick:()=>d(s,"MAX"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Max"}),e.jsx("li",{onClick:()=>d(s,"MIN"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Min"})]})}return e.jsxs(t.HoverCard,{openDelay:0,open:a,onOpenChange:n,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(ot,{className:"invisible size-3.5 cursor-pointer text-foreground/40 group-hover:visible"})}),e.jsx(t.HoverCardContent,{align:"center",className:"w-20 rounded-md p-0 shadow-none",children:g()})]})}function qe({column:s}){return e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger$1,{className:"shrink-0",asChild:!0,children:t.isTextDataType(s.data_type)?e.jsx(t.TextIcon,{className:"size-3 text-muted-foreground"}):t.isNumberDataType(s.data_type)?e.jsx(ct,{className:"size-3 text-muted-foreground"}):t.isDateDataType(s.data_type)?e.jsx(t.Calendar,{className:"size-3 text-muted-foreground"}):t.isBooleanDataType(s.data_type)?e.jsx(Hs,{className:"size-3 text-muted-foreground"}):s.data_type.startsWith("json")?e.jsx(us,{className:"size-3 text-muted-foreground"}):e.jsx(t.QuestionMarkIcon,{className:"size-3 text-muted-foreground"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{className:"text-xs",children:s.data_type})})]})}function we({column:s,className:a}){return e.jsxs("div",{className:t.cn("flex min-h-8 grow items-center space-x-2 px-2 text-sm",a),children:[e.jsx(qe,{column:s}),e.jsx("span",{children:s.column_name})]})}function sn({id:s,children:a,type:n="column"}){const{attributes:r,listeners:o,setNodeRef:l,transform:i}=t.useDraggable({id:s,data:{type:n}}),m=i?{translate:t.CSS.Translate.toString(i),transform:t.CSS.Transform.toString(i)}:void 0;return e.jsx("div",{ref:l,style:m,...o,...r,children:a})}function nn({onClick:s,column:a}){const n=t.useDndStore(u=>u.activeId),r=t.useEditorStore(u=>{var x;return(x=u.card.queryConfig)==null?void 0:x.groupByColumns}),o=t.useEditorStore(u=>{var x;return(x=u.card.queryConfig)==null?void 0:x.metricColumns}),l=t.useEditorStore(u=>{var x;return(x=u.card.queryConfig)==null?void 0:x.filterColumns});function i(u){return m(u)||c(u)||h(u)}function m(u){return r==null?void 0:r.some(x=>x.name===u.column_name)}function c(u){return o==null?void 0:o.some(x=>x.name===u.column_name)}function h(u){return l==null?void 0:l.some(x=>x.name===u.column_name)}return e.jsxs("div",{onClick:()=>s(a),className:t.cn("relative grow overflow-x-hidden overflow-y-hidden rounded-md active:border active:border-dashed active:border-muted-foreground",{"bg-muted-foreground/10":i(a)}),children:[n===a.column_name&&e.jsx(t.DragOverlay,{children:e.jsx(we,{column:a,className:"rounded-md border-2 border-dashed border-muted-foreground bg-muted shadow-md"})}),n===a.column_name&&e.jsx("div",{className:"absolute inset-0",children:n===a.column_name&&e.jsx(we,{column:a})}),e.jsx(sn,{id:a.column_name,type:"column",children:e.jsx(we,{column:a})})]})}const ke={id:"__placeholder__",role:"groupby",type:"string"},De={id:"__placeholder__",role:"groupby",type:"string"},Ie={id:"__placeholder__",role:"metric",type:"number"},Ee={name:"Filter",id:"__placeholder__",role:"filter",type:"string"},bt={id:"__placeholder__"},te="droppable-group-by",se="droppable-metrics",ne="droppable-pivot-by",be="sortable-group-by",Oe="sortable-metrics",ve="sortable-pivot-by",ze="droppable-filters",Fe="sortable-filters",Pe="sortable-order-by";function an(){const s=t.useDndStore(E=>E.insertIndex),a=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.groupByColumns)||[]}),n=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.metricColumns)||[]}),r=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.filterColumns)||[]}),o=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.orderBy)||[]}),l=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.pivotColumns)||[]}),{setInsertIndex:i,setActiveId:m,setPreviewGroupByColumns:c,setPreviewMetricColumns:h,setPreviewFilterColumns:u,setPreviewPivotColumns:x}=t.useDndActions(),{setGroupByColumns:v,setMetricColumns:d,setFilterColumns:g,setOrderByColumns:f,setPivotByColumns:y}=t.useEditorActions(),{columns:p}=t.useDataColumns(),{formattedTableName:S}=t.useEditorAside(),N=t.useSensors(t.useSensor(t.MouseSensor,{activationConstraint:{distance:1}})),M=E=>{var R;const{active:j,over:A}=E;if(((R=j.data.current)==null?void 0:R.type)==="column"&&A){const b=A.id,L=p.find(F=>F.column_name===j.id);if(!L)return;const I=t.isNumberDataType(L.data_type),$=t.isTextDataType(L.data_type),_=t.isDateDataType(L.data_type);if($||_){if(a.some(P=>P.id===b)){const P=a.findIndex(B=>B.id===b);i(P);const J=a.filter(B=>B.id!==ke.id);J.splice(P,0,ke),c(J)}else if(A.id===te){i(a.length);const P=[...a,ke];c(P),h(n),u(r)}}if($||_){if(l.some(P=>P.id===b)){const P=l.findIndex(B=>B.id===b);i(P);const J=l.filter(B=>B.id!==De.id);J.splice(P,0,De),x(J)}else if(A.id===ne){i(l.length);const P=[...l,De];x(P),c(a),h(n),u(r)}}if(I||$){if(n.some(P=>P.id===b)){console.log("isOverMetricSortable",b);const P=n.findIndex(B=>B.id===b);i(P);const J=n.filter(B=>B.id!==Ie.id);J.splice(P,0,Ie),h(J)}else if(A.id===se){i(n.length);const P=[...n,Ie];h(P),u(r),c(a),x(l)}}if(C(L)){if(r.some(P=>P.id===b)){const P=r.findIndex(B=>B.id===b);i(P);const J=r.filter(B=>B.id!==Ee.id);J.splice(P,0,Ee),u(J)}else if(A.id===ze){i(r.length);const P=[...r,Ee];u(P),h(n),c(a),x(l)}}}else c(a),h(n),u(r),x(l)};function w(E){m(E.active.id),i(null),c(a),h(n),u(r),x(l)}function C(E){return!r.some(j=>j.name===E.column_name)}function O(E,j="SUM"){const{column_name:A,data_type:R}=E,b=[...n],L=s!==null?s:n.length,I={...E,id:Z(A),name:A,label:me(A,j),type:R,role:"metric",aggregate:j};b.splice(L,0,I),d(b)}function V(E){const j=t.isTextDataType(E.data_type);t.isDateDataType(E.data_type);const A=[...l],R=s!==null?s:l.length;let b;j?b={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby"}:b={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby",label:ce(E.column_name,"month"),granularity:"month"},A.splice(R,0,b),y(A)}function U(E){const j=t.isTextDataType(E.data_type);t.isDateDataType(E.data_type);const A=[...a],R=s!==null?s:a.length;let b;j?b={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby"}:b={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby",label:ce(E.column_name,"month"),granularity:"month"},A.splice(R,0,b),v(A)}function k(E){const j=[...r],A=s!==null?s:r.length,R={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,sql:t.getDefaultFilterSql(E.data_type,S,t.fmt(E.column_name)),role:"filter"};j.splice(A,0,R),g(j)}function z(E){var b,L,I,$,_,F,P,J;const{active:j,over:A}=E,R=j.data.current;if((R==null?void 0:R.type)==="column"&&A){const B=p.find(K=>K.column_name===j.id);if(!B)return;const Q=t.isNumberDataType(B.data_type),D=t.isTextDataType(B.data_type),H=t.isDateDataType(B.data_type);(D||H)&&(A.id===te||((b=A.data.current)==null?void 0:b.type)===be)&&U(B),(D||H)&&(A.id===ne||((L=A.data.current)==null?void 0:L.type)===ve)?V(B):(Q||D)&&(A.id===se||((I=A.data.current)==null?void 0:I.type)===Oe)?(Q&&O(B),D&&O(B,"COUNT")):C(B)&&(A.id===ze||(($=A.data.current)==null?void 0:$.type)===Fe)&&k(B)}if(A&&((_=A.data.current)==null?void 0:_.type)===be&&a.some(B=>B.id===j.id)){const B=a.findIndex(D=>D.id===j.id),Q=a.findIndex(D=>D.id===A.id);if(B!==-1&&Q!==-1&&B!==Q){const D=[...a],[H]=D.splice(B,1);D.splice(Q,0,H),v(D)}}if(A&&((F=A.data.current)==null?void 0:F.type)===ve&&l.some(B=>B.id===j.id)){const B=l.findIndex(D=>D.id===j.id),Q=l.findIndex(D=>D.id===A.id);if(B!==-1&&Q!==-1&&B!==Q){const D=[...l],[H]=D.splice(B,1);D.splice(Q,0,H),y(D)}}if(A&&((P=A.data.current)==null?void 0:P.type)===Oe&&n.some(B=>B.id===j.id)){const B=n.findIndex(D=>D.id===j.id),Q=n.findIndex(D=>D.id===A.id);if(B!==-1&&Q!==-1&&B!==Q){const D=[...n],[H]=D.splice(B,1);D.splice(Q,0,H),d(D)}}if(A&&((J=A.data.current)==null?void 0:J.type)===Fe&&r.some(B=>B.id===j.id)){const B=r.findIndex(D=>D.id===j.id),Q=r.findIndex(D=>D.id===A.id);if(B!==-1&&Q!==-1&&B!==Q){const D=[...r],[H]=D.splice(B,1);D.splice(Q,0,H),g(D)}}G(E),m(null),i(null),c(a),h(n),u(r),x(l)}function G(E){const{active:j,over:A}=E,R=j.data.current;if((R==null?void 0:R.type)===Pe&&A){const b=o.findIndex(I=>ue(I.columnId)===j.id),L=o.findIndex(I=>ue(I.columnId)===A.id);if(b!==-1&&L!==-1&&b!==L){const I=[...o],[$]=I.splice(b,1);I.splice(L,0,$),f(I)}}}function q(){m(null),i(null),c(a),h(n),u(r)}return{handleDragOver:M,handleDragEnd:z,handleDragStart:w,handleDragCancel:q,sensors:N}}function Z(s){return`${s}-${t.v4()}`}function me(s,a){const n=t.toTitleCase(s.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 ue(s){return`${Pe}-${s}`}function ce(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"day":return`Date of ${n}`;case"week":return`Week of ${n}`;case"month":return`Month of ${n}`;case"year":return`Year of ${n}`;default:return n}}function rn({schemaName:s,tableName:a,columns:n,status:r}){const o=t.useEditorStore(j=>j.sqlGen),l=t.useEditorStore(j=>j.isDevMode),i=t.useEditorStore(j=>j.selectedConnectionId),m=t.useEditorStore(j=>{var A,R;return(R=(A=j.card)==null?void 0:A.queryConfig)==null?void 0:R.metricColumns});t.useEditorStore(j=>{var A,R;return(R=(A=j.card)==null?void 0:A.queryConfig)==null?void 0:R.groupByColumns});const c=t.useEditorStore(j=>j.selectedDatabaseName),h=t.useEditorStore(j=>j.selectedSchemaName),u=t.useEditorStore(j=>j.selectedTableName),x=t.useEditorStore(j=>j.selectedDatamodelId),v=t.useDashboardStore(j=>j.dashboard.aiScopeTables),{addToAIScope:d,removeFromAIScope:g}=t.useDashboardActions(),{addColumnToMetricColumns:f,addColumnToGroupBy:y,setCardSql:p,setRunSql:S,setSqlGen:N,setIsShowingVisual:M}=t.useEditorActions(),{connectionType:w,modelItems:C,selectedDatamodelName:O}=t.useEditorAside();function V(j){var A;if(l){const R=t.createSqlGenConfig(j,o);N(R);const b=(A=C==null?void 0:C.find(I=>I.id===x))==null?void 0:A.name,L=t.getSql(R,t.fmt(s),t.fmt(a),b,w,c||"");L&&(p(L),S(!0))}if(!l){if(t.isTextDataType(j.data_type))if(m&&m.length>0){const R={...j,role:"groupby",id:Z(j.column_name),name:j.column_name,type:j.data_type};y(R)}else{const R={...j,role:"metric",id:Z(j.column_name),name:j.column_name,label:me(j.column_name,"COUNT"),type:j.data_type,aggregate:"COUNT"};f(R)}if(t.isDateDataType(j.data_type)){const R={...j,role:"groupby",id:Z(j.column_name),name:j.column_name,type:j.data_type,label:ce(j.column_name,"month"),granularity:"month"};y(R)}if(t.isNumberDataType(j.data_type)){const R={...j,role:"metric",id:Z(j.column_name),name:j.column_name,label:me(j.column_name,"SUM"),type:j.data_type,aggregate:"SUM"};f(R)}}}function U(j){j===!0&&(u||O)&&i?(d({connectionId:i,connectionType:w==="S3"||w==="API"?"DuckDb":w,databaseName:c||"",schemaName:h||"",tableName:u||"",datamodelName:t.resolveDatamodelName(O),datamodelId:x||""}),t.ue.success("Added to Assistant Scope",{position:"top-right",duration:2500,richColors:!0})):(g({connectionId:i||"",connectionType:w||"",tableName:u||"",datamodelName:t.resolveDatamodelName(O),datamodelId:x||"",databaseName:c||"",schemaName:h||""}),t.ue("Removed from Assistant Scope",{position:"top-right",duration:2e3,richColors:!0}))}function k(){const j=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:O,connectionType:w,databaseName:c||""});M(!1);const A=w==="MSSQL"?`SELECT top 10 * FROM ${j}`:`SELECT * FROM ${j} LIMIT 10`;p(A),S(!0)}function z(){const j=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:O,connectionType:w,databaseName:c||""});p(`SELECT COUNT(*) as count FROM ${j}`),S(!0)}function G(){return(v==null?void 0:v.find(j=>j.connectionId===i&&(j.tableName===u||j.datamodelName===O)))!==void 0}function q(j){return l?e.jsxs("div",{className:"flex grow items-center justify-between space-x-2 rounded-md px-2 py-1",onClick:()=>V(j),children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(qe,{column:j}),e.jsx("span",{children:j.column_name})]}),e.jsx("div",{onClick:A=>A.stopPropagation(),className:"flex items-center justify-end",children:t.isNumberDataType(j.data_type)&&e.jsx(tn,{column:j})})]}):e.jsx(nn,{onClick:V,column:j})}function E(){return e.jsxs(e.Fragment,{children:[e.jsxs(t.Toggle,{title:`${G()?"Remove from":"Add to"} Assistant Scope`,pressed:G(),onPressedChange:U,variant:"outline",className:t.cn("h-9 px-2",{}),children:[e.jsx(t.Plus,{className:t.cn("mr-1 size-4",{"text-green-400":G(),"text-muted-foreground":!G()})}),"AI"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:k,size:"sm",variant:"secondary",className:"flex",children:[r!=="Success"?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(Ls,{className:"mr-2 size-4"}),"Top 10"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:z,size:"sm",variant:"secondary",className:"w-full",children:[r!=="Success"?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(xt,{className:"mr-2 size-4"}),"Count"]})]})}return r!=="Success"?e.jsx("div",{className:"flex grow flex-col justify-start gap-2 px-6",children:Array.from({length:3}).map((j,A)=>e.jsx(t.Skeleton,{className:"h-7 w-full"},A))}):e.jsxs("div",{className:"flex grow flex-col space-y-4 px-4",children:[l&&e.jsx("div",{className:"mx-2 flex items-center justify-center gap-2",children:E()}),e.jsxs(t.CustomScrollArea,{className:"flex-1 basis-0 overflow-y-auto px-2 py-0",children:[e.jsx("div",{className:"flex flex-col gap-1",children:n==null?void 0:n.map(j=>e.jsx("div",{className:t.cn("mr-2 cursor-pointer rounded-md text-sm transition-colors hover:bg-muted"),children:q(j)},j.column_name))}),e.jsx(t.ScrollBar,{orientation:"vertical"}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function vt({ColumnsComponent:s,type:a}){var Ue;const n=t.useEditorStore(W=>W.card),r=t.useEditorStore(W=>W.selectedConnectionId),o=t.useDashboardStore(W=>W.showFilters),l=t.useEditorStore(W=>W.selectedDatamodelId),i=t.useEditorStore(W=>W.selectedDatabaseName),m=t.useEditorStore(W=>W.selectedSchemaName),c=t.useEditorStore(W=>W.selectedTableName),h=t.useEditorStore(W=>W.isDevMode),{setSelectedTableName:u,setIsDevMode:x,setCardSql:v}=t.useEditorActions(),{connectionItems:d,modelItems:g,schemaItems:f,databaseItems:y,tableItems:p,columns:S,isConnectionsLoading:N,isModelsLoading:M,isConnectionsError:w,isModelsError:C,isDatabasesLoadingOrFetching:O,isDatabaseError:V,isSchemaLoadingOrFetching:U,isSchemaError:k,isColumnsError:z,isColumnsLoadingOrFetching:G,isTableLoadingOrFetching:q,isTableError:E,connectionType:j,isNonDbConnection:A,refetchDatabases:R,refetchModels:b,refetchSchemas:L,refetchTables:I,refetchColumns:$,refetchConnections:_,handleConnectionChange:F,handleDatabaseChange:P,handleSchemaChange:J,handleDatamodelChange:B,tableName:Q,schemaName:D}=t.useEditorAside(),{data:H,refetch:K}=t.useDashboardCardQuery(n);function X(){return s?s(D,Q,S):e.jsx(rn,{schemaName:D,tableName:Q,columns:S,status:G?"Loading...":z?"Error":"Success"})}function he(){window.location.href="mailto:support@semaphor.cloud?subject=Help&body=I need help with..."}function xe(W){W===!0?H&&(v(H.userSql),setTimeout(()=>{K()},100)):v(""),x(W)}function pe(){if(!r||j==="S3"||j==="API")return null;if(!l||l==="none")return e.jsx(ie,{refetch:R,showNone:!0,onChange:P,isError:V,isLoading:O,items:y,value:i||"",title:"Database",type:"combobox"})}function fe(){if(r&&!A&&(!i||i==="none")&&(!l||l==="none"))return e.jsxs("div",{className:"grid grid-cols-3",children:[e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Label,{children:"OR"})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})})]})}function $t(){if(!r||j==="S3"||j==="API")return null;if(!i||i==="none")return e.jsx(ie,{refetch:b,showNone:!0,onChange:B,isError:C,isLoading:M,items:g,value:l||"",title:"Model",type:"combobox"})}function Qt(){return!r||j==="S3"||j==="API"||!i||i==="none"?null:e.jsx(ie,{refetch:L,onChange:J,isError:k,isLoading:U,items:f,value:m||"",title:"Schema",type:"combobox"})}function Jt(){function W(){let ee=!1;return j==="API"?(ee=!1,ee):((j==="S3"||r&&i&&i!=="none"&&m)&&(ee=!0),ee)}return W()?e.jsx(ie,{refetch:I,onChange:u,isError:E,items:p,isLoading:q,value:c||"",title:"Table",type:"combobox"}):null}return e.jsxs("aside",{className:t.cn("flex h-full min-w-[310px] max-w-[310px] flex-col justify-between rounded-none",{"border-r-[1px]":!o}),children:[e.jsxs("section",{className:"space-y-3 px-6 py-4",children:[e.jsx(ie,{refetch:_,items:d,title:"Connection",value:r||"",onChange:F,isLoading:N,isError:w,type:"combobox",connectionTooltip:((Ue=d==null?void 0:d.find(W=>W.id===r))==null?void 0:Ue.type)||""}),pe(),fe(),$t(),Qt(),Jt()]}),X(),e.jsx("div",{className:"flex items-center justify-between gap-2 px-6 py-4",children:a==="main"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Switch,{className:"data-[state=checked]:bg-muted-foreground/80",checked:h,onCheckedChange:xe,id:"dev-mode"}),e.jsx(t.Label,{htmlFor:"dev-mode",children:"Dev Mode"})]}),e.jsxs(t.Button,{onClick:he,size:"sm",variant:"secondary",className:"group flex items-center gap-2 rounded-full",children:[e.jsx(zs,{className:"size-4"}),e.jsx("span",{className:"hidden group-hover:block",children:"Help"})]})]})})]})}function Ct({sql:s,error:a,onClick:n}){const r=t.useEditorStore(c=>c.isDevMode);t.useEditorStore(c=>c.card);const{setIsDevMode:o,setCardSql:l,setRunSql:i}=t.useEditorActions();if(!s&&!a)return null;function m(){o(!r),l(s),setTimeout(()=>{i(!0)},100)}return e.jsxs(t.HoverCard,{openDelay:a?0:500,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.Button,{variant:"link",className:"h-5 p-0",children:a?e.jsx(at,{className:"h-4 w-4 text-red-500"}):e.jsx(t.Info,{className:"h-4 w-4 text-muted-foreground"})})}),e.jsx(t.HoverCardContent,{align:"start",className:"w-full min-w-fit max-w-[700px]",children:e.jsx("div",{className:"flex justify-between space-x-4",children:e.jsxs("div",{className:"flex grow flex-col space-y-2",children:[e.jsxs(t.ScrollArea,{className:"max-h-[250px] min-h-0 overflow-scroll text-xs leading-relaxed text-foreground/80",children:[a&&a,s&&e.jsxs("div",{className:"flex flex-col space-y-1.5",children:[e.jsx("span",{className:"text-xs font-semibold",children:"Resolved SQL"}),e.jsx("div",{className:"rounded-md",children:e.jsx(t.CodeHighlighter,{code:s,language:"sql"})})]})]}),!r&&e.jsxs(t.Button,{onClick:m,variant:"secondary",size:"sm",children:[e.jsx(Cs,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),a&&n&&e.jsxs(t.Button,{onClick:n,size:"sm",variant:"secondary",children:[e.jsx(Ds,{className:"mr-2 h-4 w-4"}),"Debug with Assistant"]})]})})})]})}var St={color:void 0,size:void 0,className:void 0,style:void 0,attr:void 0},Ke=T.createContext&&T.createContext(St),on=["attr","size","title"];function ln(s,a){if(s==null)return{};var n=cn(s,a),r,o;if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(s);for(o=0;o<l.length;o++)r=l[o],!(a.indexOf(r)>=0)&&Object.prototype.propertyIsEnumerable.call(s,r)&&(n[r]=s[r])}return n}function cn(s,a){if(s==null)return{};var n={};for(var r in s)if(Object.prototype.hasOwnProperty.call(s,r)){if(a.indexOf(r)>=0)continue;n[r]=s[r]}return n}function Ce(){return Ce=Object.assign?Object.assign.bind():function(s){for(var a=1;a<arguments.length;a++){var n=arguments[a];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(s[r]=n[r])}return s},Ce.apply(this,arguments)}function Ye(s,a){var n=Object.keys(s);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(s);a&&(r=r.filter(function(o){return Object.getOwnPropertyDescriptor(s,o).enumerable})),n.push.apply(n,r)}return n}function Se(s){for(var a=1;a<arguments.length;a++){var n=arguments[a]!=null?arguments[a]:{};a%2?Ye(Object(n),!0).forEach(function(r){dn(s,r,n[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(s,Object.getOwnPropertyDescriptors(n)):Ye(Object(n)).forEach(function(r){Object.defineProperty(s,r,Object.getOwnPropertyDescriptor(n,r))})}return s}function dn(s,a,n){return a=un(a),a in s?Object.defineProperty(s,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):s[a]=n,s}function un(s){var a=mn(s,"string");return typeof a=="symbol"?a:a+""}function mn(s,a){if(typeof s!="object"||!s)return s;var n=s[Symbol.toPrimitive];if(n!==void 0){var r=n.call(s,a);if(typeof r!="object")return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return(a==="string"?String:Number)(s)}function Nt(s){return s&&s.map((a,n)=>T.createElement(a.tag,Se({key:n},a.attr),Nt(a.child)))}function re(s){return a=>T.createElement(hn,Ce({attr:Se({},s.attr)},a),Nt(s.child))}function hn(s){var a=n=>{var{attr:r,size:o,title:l}=s,i=ln(s,on),m=o||n.size||"1em",c;return n.className&&(c=n.className),s.className&&(c=(c?c+" ":"")+s.className),T.createElement("svg",Ce({stroke:"currentColor",fill:"currentColor",strokeWidth:"0"},n.attr,r,i,{className:c,style:Se(Se({color:s.color||n.color},n.style),s.style),height:m,width:m,xmlns:"http://www.w3.org/2000/svg"}),l&&T.createElement("title",null,l),s.children)};return Ke!==void 0?T.createElement(Ke.Consumer,null,n=>a(n)):a(St)}function xn(s){return re({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"},child:[]}]})(s)}function wt(s){const{theme:a}=t.useTheme(),n=t.useDashboardStore(f=>f.theme),r=t.useEditorStore(f=>f.frame),o=t.useEditorStore(f=>f.card),l=t.useEditorStore(f=>f.activeTabCardId),i=t.Le(),{refetch:m}=t.useDashboardCardQuery(o),{updateCardTab:c,setRunSql:h}=t.useEditorActions(),u=T.useRef(),x=T.useRef({}),v=r.cards.find(f=>f.id===l),d=T.useCallback(()=>{let f="";return s==="python"&&(f=(v==null?void 0:v.python)||t.PYTHON_DEFAULT_CODE),s==="sql"&&(f=(v==null?void 0:v.sql)||""),f},[s,v]);T.useEffect(()=>{if(l&&o.id&&o.id!==l){if(u.current&&i){const f=u.current.getModel();f&&(x.current[o.id]=f);let y=x.current[l];if(!y){const p=d();y=i.editor.createModel(p,s),x.current[l]=y}u.current.setModel(y)}c(l,o),h(!0)}},[l,o,c,i,h,s,d]),T.useEffect(()=>{if(!i)return;const f=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",y=n==="system"?f:n;i.editor.defineTheme("default",{base:y==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":y==="light"?"#ffffff":t.hslStringToHex("240 10% 10%")}}),i.editor.setTheme("default")},[a,n,i]),T.useEffect(()=>{const f=u.current;f&&i&&o&&f.addCommand(i.KeyMod.Shift|i.KeyCode.Enter,function(){return m(),!1})},[i,m,o]);function g(f,y){if(u.current=f,!x.current[o.id]){const p=f.getModel(),S=d();x.current[o.id]=p||(y==null?void 0:y.editor.createModel(S,s))||f.getModel()}f.setModel(x.current[o.id])}return{handleEditorDidMount:g}}function pn(){const s=t.useEditorStore(i=>i.card.sql),a=t.useEditorStore(i=>i.sqlGen),{setCardSql:n,setSqlGen:r}=t.useEditorActions(),{handleEditorDidMount:o}=wt("sql");function l(i){n(i),t.isObjectEmpty(a)||r({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!s&&e.jsx("div",{className:"pointer-events-none absolute left-[65px] top-3 z-10 font-mono text-sm text-muted-foreground",children:"Write your SQL here"}),e.jsx(t.Ft,{theme:"default",className:"pt-3",onChange:l,value:s||"",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:o})]})}function fn(){const s=t.useEditorStore(o=>o.card.python),{setCardPython:a}=t.useEditorActions(),{handleEditorDidMount:n}=wt("python");function r(o){a(o)}return e.jsx(t.Ft,{className:"pt-3",theme:"default",onChange:r,value:s||t.PYTHON_DEFAULT_CODE,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:n})}function gn({jsonString:s}){return e.jsx(t.Ft,{theme:"default",defaultLanguage:"json",className:"mt-1 min-h-[220px]",value:s,options:{readOnly:!0,minimap:{enabled:!1},lineNumbers:"off",glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})}function kt(s){const[a,n]=T.useState(""),[r,o]=T.useState(null),{authToken:l}=t.useSemaphorContext(),i=t.useEditorStore(D=>{var H;return(H=D==null?void 0:D.card)==null?void 0:H.sql}),m=t.useEditorStore(D=>{var H;return(H=D==null?void 0:D.card)==null?void 0:H.python}),{setCardPython:c,setCardSql:h,setRunSql:u,switchCardType:x}=t.useEditorActions(),v=t.useEditorStore(D=>D.currentAssitantScope)||[],d=t.useEditorStore(D=>D.userInputForAI),g=t.useEditorStore(D=>D.triggerAIRun),f=t.useEditorStore(D=>D.selectedDatabaseName),y=t.useEditorStore(D=>D.selectedTableName),p=t.useEditorStore(D=>D.selectedConnectionId),S=t.useEditorStore(D=>D.selectedSchemaName),N=t.useDashboardStore(D=>D.dashboard.aiScopeTables),{setTriggerAIRun:M,setCurrentAssitantScope:w}=t.useEditorActions(),{selectedDatamodelName:C,connectionType:O,selectedDatamodelId:V}=t.useEditorAside(),{messages:U,setMessages:k,reload:z,error:G,append:q,input:E,setInput:j,status:A,handleInputChange:R,handleSubmit:b,stop:L}=t.useChat({api:"https://semaphor.cloud/api/v1/assistant",headers:{Authorization:`Bearer ${l==null?void 0:l.accessToken}`},body:{random:Math.random(),assistantId:"editor-assistant"},maxSteps:10,onError:D=>{console.log("on error",D)},onToolCall:I,onFinish(D,H){$(H.usage),n(""),H.finishReason==="stop"&&n("")}});T.useEffect(()=>{g&&d&&(R({target:{value:d}}),setTimeout(()=>{b(),M(!1)},100))},[g,b,d,M,R]);async function I({toolCall:D}){if(D.toolName==="createChartFromSql"){const H=D.args.sql,K=D.args.python,X=D.args.chartType;return x(X),h(H),c(K),u(!0),{status:"success",message:"Let the user know that chart is created"}}else if(D.toolName==="getTableColumns")n("Getting table columns...");else{if(D.toolName==="getUserSql")return i?{status:"success",sql:i,python:m||""}:{status:"error",message:"No SQL found"};if(D.toolName==="getCurrentTableScope")return n("Getting table scope..."),J(),{status:"success",tables:v};n("Analyzing...")}}function $(D){o(H=>H?{promptTokens:H.promptTokens+D.promptTokens,completionTokens:H.completionTokens+D.completionTokens,totalTokens:H.totalTokens+D.totalTokens}:D)}function _(D){const H=t.removeFromScopeArray(v,D);F(H)}function F(D){const H=t.sanitizeAIScope(D);w(H),k(K=>[...K,{id:crypto.randomUUID(),role:"system",content:`Current scope of tables | ${JSON.stringify(H)}`}])}function P(){const D={databaseName:f||"",schemaName:S||"",tableName:y||"",datamodelId:V||"",datamodelName:t.resolveDatamodelName(C),connectionId:p||"",connectionType:O||""},H=t.dedup([...v,D]);F(H)}function J(){const D={databaseName:f||"",schemaName:S||"",tableName:y||"",datamodelName:t.resolveDatamodelName(C),connectionId:p||"",connectionType:O||"",datamodelId:V||""};if(!t.isInAiScopeArray([...N||[],...v],D)||v.length===0){const K=t.dedup([...N||[],...v,D]).filter(X=>X.connectionId===D.connectionId);F(K)}}const B=!["ready","error"].includes(A),Q=t.getErrorMessage(G);return{messages:U,setMessages:k,reload:z,append:q,input:E,setInput:j,isLoading:B,handleSetAIScope:J,handleAddCurrentTableToScope:P,removeFromCurrentSelections:_,currentAssitantScope:v,setCurrentAssitantScope:w,handleInputChange:R,handleSubmit:b,stop:L,chatStatus:A,toolStatus:a,llmUsage:r,onFinish:s,errorMessage:Q}}function jn(s){const a=s.toUpperCase().indexOf("WHERE"),n=s.toUpperCase().indexOf("GROUP BY"),r=s.toUpperCase().indexOf("LIMIT"),o=s.toUpperCase().indexOf("ORDER BY");let l=s.length;if(o!==-1&&(l=o),r!==-1&&r<l&&(l=r),a!==-1){const i=s.substring(0,a),m=s.substring(a);return`${i} {{ filters | where }} AND ${m.substring(6)}`}else if(n!==-1){const i=s.substring(0,n),m=s.substring(n);return`${i} {{ filters | where }} ${m}`}else{const i=s.substring(0,l),m=s.substring(l);return console.log("beforeClause",i),console.log("afterClause",m),m?`${i} {{ filters | where }} ${m}`:`${i.replace(/;$/,"")} {{ filters | where }}`}}function Dt({variant:s="secondary"}){const{setCardSql:a}=t.useEditorActions(),n=t.useEditorStore(d=>d.frame),r=t.useEditorStore(d=>d.card),o=t.useEditorStore(d=>d.selectedSchemaName),l=t.useEditorStore(d=>d.selectedDatamodelId),i=t.useEditorStore(d=>d.selectedDatabaseName),m=t.useEditorStore(d=>d.selectedTableName),c=t.useEditorStore(d=>d.selectedConnectionId),h=t.useEditorStore(d=>d.isShowingVisual),u=r.sql,x=t.useEditorStore(d=>d.onSave);function v(){let d=u;u&&!u.includes("{{")&&(d=jn(u),a(d));const g={...r,lastSelectedSchema:o,lastSelectedDatamodelId:l,lastSelectedDatabase:i,lastSelectedTable:m,connectionId:c,type:h?r.type:"table",sql:d,customCfg:h?r.customCfg:null},f=n.cards.map(p=>p.id===r.id?g:p),y={...n,cards:f,activeCardId:yn(f,n.activeCardId)};x==null||x(y)}return e.jsxs(t.Button,{onClick:v,size:"sm",variant:s,children:[e.jsx(t.Check,{className:"mr-2 size-4 font-bold"}),"Accept"]})}function It(){const{setTempQueryData:s,setSqlGen:a}=t.useEditorActions(),n=t.useQueryClient(),r=t.useEditorStore(c=>c.tempQueryData),o=t.useEditorStore(c=>c.onClose),l=t.useEditorStore(c=>c.card),{queryKey:i}=t.useDashboardCardQuery(l);function m(){if(r){const{queryKey:c,queryData:h}=r;c&&h&&n.setQueryData(c,h)}a({}),o==null||o()}return e.jsxs(t.Button,{onClick:m,size:"sm",variant:"secondary",children:[e.jsx(t.X,{className:"mr-2 size-4"}),"Close"]})}function Et(){const{setTempQueryData:s}=t.useEditorActions(),a=t.useQueryClient(),n=t.useEditorStore(d=>d.tempQueryData),r=t.useEditorStore(d=>d.card),{queryKey:o}=t.useDashboardCardQuery(r),{setCardSql:l,setSqlGen:i,setCardPython:m,setCardCustomCfg:c,setCardPreferences:h,setCardType:u,clearQueryConfig:x}=t.useEditorActions();function v(){const d=a.getQueryData(o);n!=null&&n.queryData||s({queryKey:o,queryData:d}),a.setQueryData(o,null),a.removeQueries({queryKey:o,exact:!0}),l(""),i({}),x(),m(""),c(""),h({})}return e.jsxs(t.Button,{onClick:v,className:"",size:"sm",variant:"secondary",children:[e.jsx(bs,{className:"mr-2 size-4"}),"Clear"]})}function yn(s,a){if(s.length===0)return"";if(a){const n=s.find(r=>r.id===a);if(n){const r=n.tabTitle;if(r=="All__Header")return s[0].id;if(r&&r.endsWith("__Header")){const l=r.split("__")[0],i=s.find(m=>m.tabTitle===l);return i?i.id:s[0].id}return a}}return s[0].id}function bn(){const s=t.useQueryClient(),[a,n]=T.useState(!1),[r,o]=T.useState(!1);t.useEditorStore(I=>I.frame);const l=t.useEditorStore(I=>I.card);t.useEditorStore(I=>I.isShowingVisual),t.useEditorStore(I=>I.selectedConnectionId),t.useEditorStore(I=>I.tempQueryData),t.useEditorStore(I=>I.selectedDatabaseName),t.useEditorStore(I=>I.selectedDatamodelId),t.useEditorStore(I=>I.selectedSchemaName);const i=t.useEditorStore(I=>I.showAIDialog);t.useEditorStore(I=>I.selectedTableName);const m=t.useEditorStore(I=>{var $;return($=I==null?void 0:I.card)==null?void 0:$.sql}),{sheetFilterValues:c}=t.useSheetFilterValues(),{setCardCustomCfg:h,setQueryResultColumns:u,setCardSql:x,setTempQueryData:v,setPythonStdOut:d,setCardPreferences:g,setCardType:f,setCardPython:y,runAssistant:p,setShowAIDialog:S}=t.useEditorActions();t.useEditorStore(I=>I.onSave),t.useEditorStore(I=>I.onClose),t.useEditorStore(I=>I.actions.setSqlGen);const N=t.useEditorStore(I=>I.runSql),M=t.useEditorStore(I=>I.actions.setRunSql),w=t.useEditorStore(I=>I.actions.setIsSqlRunning),{data:C,isLoading:O,isFetching:V,isSuccess:U,isError:k,error:z,queryKey:G,refetch:q,errorMessage:E}=t.useDashboardCardQuery(l),{handleSetAIScope:j}=kt();T.useEffect(()=>{if(C&&(C.rowLimitExceeded&&t.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),d((C==null?void 0:C.output)||""),C.records.length>0)){const{dimensionKeys:I}=t.getKeys(C.records);u(I)}},[C,u,d]),T.useEffect(()=>{(O||V)&&w(!0),(k||U)&&w(!1),k&&(u([]),d(""))},[O,V,C,d,k,U,w,u,z]),T.useEffect(()=>{N&&(s.getQueryData(G)||q(),M(!1))},[N,q,M,G,s]);async function A(){const I=`Can you help me debug this query in the current table scope?
|
|
361
|
+
*/const Us=t.createLucideIcon("Type",[["polyline",{points:"4 7 4 4 20 4 20 7",key:"1nosan"}],["line",{x1:"9",x2:"15",y1:"20",y2:"20",key:"swin9y"}],["line",{x1:"12",x2:"12",y1:"4",y2:"20",key:"1tx1rr"}]]);function _e(s){var a;return{...s,sheets:(a=s==null?void 0:s.sheets)==null?void 0:a.map(n=>{var r;return{...n,frames:(r=n.frames)==null?void 0:r.map(o=>{var l;return{...o,cards:(l=o.cards)==null?void 0:l.map(i=>ft(i))}})}})}}function ft(s){return{...s,customCfg:$s(s.customCfg),data:[]}}function $s(s){var a,n;return s?{...s,data:{...s==null?void 0:s.data,labels:[],datasets:(n=(a=s==null?void 0:s.data)==null?void 0:a.datasets)==null?void 0:n.map(r=>({...r,data:[]}))}}:null}async function Qs(s,a,n,r){if(!r)return null;const o=JSON.stringify(a);return await t.postRequest("https://semaphor.cloud/api/v1/lens",r,{name:s,template:o,filterValues:n,operation:"create"})}async function Le(s,a){if(!a)return null;const{id:n,template:r,filterValues:o,isDefault:l,shared:i}=s;return await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{lensId:n,template:r,filterValues:o,isDefault:l,shared:i,operation:"update"})}async function Js(s,a){return a?await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{operation:"delete",lensId:s}):null}function Ws(){const[s,a]=T.useState(""),[n,r]=T.useState(!1),o=t.useDashboardStore(p=>p.lenses),{setLenses:l,setSelectedLensId:i}=t.useDashboardActions(),[u,c]=T.useState(!1),[h,d]=T.useState(!1),x=t.useDashboardStore(p=>p.dashboard),b=t.useDashboardStore(p=>p.filterValues),{authToken:m}=t.useSemaphorContext();async function g(){c(!0);const p=_e(x),y=await Qs(s,p,b,m==null?void 0:m.accessToken);y&&(l([...o||[],y]),i(y.id),d(!1)),c(!1)}return e.jsxs(t.Dialog,{open:h,onOpenChange:d,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Lens"]})}),e.jsxs(t.DialogContent,{className:"sm:max-w-[425px]",children:[e.jsxs(t.DialogHeader,{children:[e.jsx(t.DialogTitle,{children:"Create Lens"}),e.jsx(t.DialogDescription,{children:"Lenses are private by default and are only visible to you."})]}),e.jsx("div",{className:"grid gap-4 py-4",children:e.jsxs("div",{className:"grid grid-cols-8 items-center gap-4",children:[e.jsx(t.Label,{className:"text-right",children:"Name"}),e.jsx(t.Input,{value:s,onChange:p=>{a(p.target.value)},id:"name",className:"col-span-7 focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0"})]})}),n&&e.jsx("p",{className:"text-sm",children:"The lens name already exists. Use a differnet name."}),e.jsx(t.DialogFooter,{children:e.jsx(t.Button,{size:"sm",variant:"outline",type:"submit",onClick:g,disabled:u,children:u?"Creating...":"Add"})})]})]})}function Ks(){const[s,a]=T.useState(!1),n=t.useDashboardStore(d=>d.lenses),r=t.useDashboardStore(d=>d.selectedLensId),{setLenses:o,setSelectedLensId:l}=t.useDashboardActions(),{authToken:i}=t.useSemaphorContext();async function u(d){l(d)}async function c(d){if(await Js(d,i==null?void 0:i.accessToken)){const b=n==null?void 0:n.filter(m=>m.id!==d);o(b||[]),l("original")}}async function h(d){const x=n==null?void 0:n.map(g=>g.id===d.id?{...g,isDefault:!d.isDefault}:{...g,isDefault:!1});o(x||[]);const b={...d,isDefault:!d.isDefault};await Le(b,i==null?void 0:i.accessToken);const m=n==null?void 0:n.find(g=>g.isDefault);if(m&&m.id!==d.id){const g={...m,isDefault:!1};await Le(g,i==null?void 0:i.accessToken)}}return e.jsxs(t.DropdownMenu,{open:s,onOpenChange:a,children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Lenses",className:"h-7 p-2",children:e.jsx(ws,{className:"h-4 w-4"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",className:"w-60",children:[e.jsx(t.DropdownMenuLabel,{className:"px-3",children:"Lenses"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsxs(t.DropdownMenuRadioGroup,{value:r,onValueChange:u,children:[e.jsx(t.DropdownMenuRadioItem,{value:"original",children:"Original"}),n==null?void 0:n.map(d=>e.jsxs(t.DropdownMenuRadioItem,{className:"group flex justify-between gap-2",value:d.id,children:[e.jsx("p",{children:d.name}),e.jsxs("span",{className:"flex items-center gap-3",children:[e.jsx(Is,{onClick:x=>{x.preventDefault(),h(d)},className:t.cn("size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",{invisible:!d.isDefault,"text-green-500":d.isDefault}),strokeWidth:1.5}),e.jsx(t.Cross2Icon,{onClick:x=>{x.preventDefault(),c(d.id)},className:"invisible size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",strokeWidth:1.5})]})]},d.id))]})]})]})}function gt(){var l;const s=t.useDashboardStore.getState().authToken,a=t.useDashboardStore.getState().theme,n=t.useDashboardStore.getState().filterValues,r=(l=t.useDashboardStore.getState().dashboard)==null?void 0:l.title,o=t.useDashboardStore.getState().selectedSheetId;return{authToken:s,theme:a||"light",filterValues:n,dashboardTitle:r||"",selectedSheetId:o||""}}async function jt(){const s=t.useDashboardStore.getState().isDownloadingPdf,{authToken:a,theme:n,filterValues:r,dashboardTitle:o,selectedSheetId:l}=gt();if(s)return;const i=new URLSearchParams;i.set("token",(a==null?void 0:a.accessToken)||""),i.set("theme",n),i.set("filterValues",JSON.stringify(r)),i.set("selectedSheetId",l||"");const u=`https://semaphor.cloud/api/v1/generate-pdf?params=${encodeURIComponent(i.toString())}`;t.useDashboardStore.setState({isDownloadingPdf:!0});const d=(await(await fetch(u,{method:"GET",headers:{Authorization:`Bearer ${a==null?void 0:a.accessToken}`}})).json()).url,x=document.createElement("a");x.href=d;const b=`${o}-${new Date().toISOString()}.pdf`;x.download=b,x.click(),t.useDashboardStore.setState({isDownloadingPdf:!1})}function Ys({iconClassName:s}){const{authToken:a}=t.useSemaphorContext();t.useDashboardStore(o=>{var l;return(l=o.dashboard)==null?void 0:l.title}),t.useDashboardStore(o=>o.theme),t.useDashboardStore(o=>o.filterValues);const n=t.useDashboardStore(o=>o.isDownloadingPdf);async function r(){await jt()}return e.jsx(t.IconButton,{tooltip:"Download PDF",className:"h-7 p-2",onClick:r,children:n?e.jsx(ae,{className:t.cn("size-3.5 animate-spin",s)}):e.jsx(t.Download,{className:t.cn("size-4",s)})})}function Y({defaultValue:s,label:a,options:n,value:r,onValueChange:o,className:l,icon:i,showPlaceholderLabel:u=!0}){return e.jsxs(t.Select,{defaultValue:s,value:r,onValueChange:o,children:[e.jsx(t.SelectTrigger,{className:t.cn("w-full focus:ring-0 focus:ring-offset-0",l),children:e.jsxs("div",{className:"flex items-center gap-2",children:[i&&e.jsx("span",{children:i}),u&&e.jsx(t.SelectValue,{placeholder:a})]})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:a}),n.map(c=>e.jsx(t.SelectItem,{value:c.value,children:c.label},c.value))]})})]})}const Je=[{value:"daily",label:"Daily"},{value:"weekly",label:"Weekly"},{value:"biweekly",label:"Bi-Weekly"},{value:"monthly",label:"Monthly"}];function yt({triggerButtonClassName:s}){const{data:a,handleScheduleSubscription:n,handleDeleteSubscription:r,status:o,error:l,isDeleting:i}=Zs(),[u,c]=T.useState(""),[h,d]=T.useState(Je[0].value),[x,b]=T.useState("06:00"),[m,g]=T.useState("");return e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Schedule Dashboard",className:t.cn("h-7 p-2",s),children:e.jsx(xs,{className:"size-4"})})}),e.jsx(t.PopoverContent,{align:"end",className:"w-80",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:"Schedule Subscription"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Subscribe to receive dashboard via email"})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"email",children:"Email"}),e.jsx(t.Input,{id:"email",value:u,onChange:p=>c(p.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"subject",children:"Subject"}),e.jsx(t.Input,{id:"subject",placeholder:"Monthly Sales Report",value:m,onChange:p=>{g(p.target.value.replace(/[^a-zA-Z0-9\s-_]/g,""))},className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"frequency",children:"Frequency"}),e.jsx(Y,{className:"col-span-2 h-8",label:"Select Frequency",options:Je,value:h,onValueChange:p=>d(p)})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"time",children:"Time"}),e.jsx(t.Input,{id:"time",type:"time",value:x,onChange:p=>b(p.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsx("div",{className:"mt-2 flex justify-end",children:e.jsxs(t.Button,{onClick:async()=>{await n({email:u,frequency:h,time:x,subject:m}),b("00:00"),c(""),g("")},className:"h-8 p-2",disabled:o==="loading"||!u,children:[o==="loading"&&e.jsx(ae,{className:"mr-2 size-4 animate-spin"}),o==="success"&&e.jsx(t.Check,{className:"mr-2 size-4"}),o==="error"&&e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{children:e.jsx(at,{className:"mr-2 size-4 text-destructive"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{children:l})})]}),"Schedule"]})}),a&&a.length>0&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Subscriptions"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Active email subscriptions"})]}),t.isDevEnv]})}),e.jsx("div",{className:"grid gap-2",children:a==null?void 0:a.map(p=>e.jsxs("div",{className:"group grid grid-cols-5 gap-3",children:[e.jsx("div",{className:"col-span-3 text-sm",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"boder-border rounded-md border bg-muted px-2 py-1",children:[t.titleCase(p.frequency)," "]}),e.jsx(t.Label,{children:p.time})]})}),e.jsxs("div",{className:"col-span-2 flex items-center justify-end gap-4",children:[e.jsx(We,{tooltip:`Next Run: ${new Date(p.nextRun).toLocaleString()} ${p.timezone}`,children:e.jsx(hs,{className:"size-3.5"})}),e.jsx(We,{tooltip:`${p.endUserEmail}`,children:e.jsx(Os,{className:"size-3.5"})}),i===p.id?e.jsx(ae,{className:"size-3.5 animate-spin"}):e.jsx(Gs,{onClick:()=>r(p.id),className:"size-3.5 cursor-pointer"})]})]},p.id))})]})]})]})})]})})}const We=({children:s,tooltip:a})=>e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:s}),e.jsx(t.TooltipContent,{children:a})]})});function Zs(){const[s,a]=T.useState("idle"),[n,r]=T.useState(""),[o,l]=T.useState(null),i=t.useDashboardStore(C=>C.authToken),u=t.useDashboardStore(C=>C.dashboard.id),c=t.useDashboardStore(C=>C.dashboard.title),{endUserId:h}=t.getTenantAndEndUserId((i==null?void 0:i.accessToken)||""),d="https://semaphor.cloud/api/v1/subscriptions/get",x={dashboardId:u,endUserId:h},{data:b,isLoading:m,isError:g,isFetching:p,refetch:y}=t.useQuery({queryKey:["subscriptions",u,h],queryFn:()=>t.getRequest(d,(i==null?void 0:i.accessToken)||"",x),enabled:!!(i!=null&&i.accessToken)&&!!u&&!!h});async function f({email:C,subject:A,frequency:N,time:D}){const{authToken:F,...R}=gt(),U=Intl.DateTimeFormat().resolvedOptions().timeZone,w={email:C,frequency:N,time:D,timezone:U,endUserId:h,endUserEmail:C,subject:A||c||"Scheduled Dashboard",dashboardId:u,dashboardParams:R};a("loading");const _="https://semaphor.cloud/api/v1/subscriptions/create";try{const G=await t.postRequestRegular(_,(F==null?void 0:F.accessToken)||"",{...w});a("success"),r(""),y(),setTimeout(()=>{a("idle")},2e3)}catch(G){console.error(G),a("error"),r(G.message)}}async function S(C){const A="https://semaphor.cloud/api/v1/subscriptions/delete";l(C);try{await t.postRequestRegular(A,(i==null?void 0:i.accessToken)||"",{id:C}),y(),setTimeout(()=>{l(null)},2e3)}catch(N){console.error(N),l(null),a("error"),r(N.message)}}return{data:b,isLoading:m,isFetching:p,isError:g,isDeleting:o,refetch:y,handleScheduleSubscription:f,handleDeleteSubscription:S,status:s,error:n}}function Xs(){const{authToken:s}=t.useSemaphorContext();t.useDashboardStore(w=>w.theme);const a=t.useDashboardStore(w=>w.lenses),n=t.useDashboardStore(w=>w.selectedLensId),r=a==null?void 0:a.find(w=>w.id===n),o=t.useDashboardStore(w=>w.dashboard);t.useDashboardStore(w=>w.selectedSheetId);const l=t.useDashboardStore(w=>w.isDashboardEditing),i=t.useEditorStore(w=>w.selectedConnectionId),[u,c]=T.useState(!1),h=t.useDashboardStore(w=>w.filterValues),d=t.useDashboardStore(w=>w.isVisualEditing),x=t.useDashboardStore(w=>w.showFilters);t.useDashboardStore(w=>w.showDashboardJSON);const b=t.useDashboardStore(w=>w.onSaveFunction),m=t.useDashboardStore(w=>w.actions.setShowFilters),{addFrame:g,setSelectedFrameId:p}=t.useDashboardStore(w=>w.actions),{getSelectedSheet:y,setIsDashboardEditing:f,setShowDashboardJSON:S}=t.useDashboardStore(w=>w.actions),C=y(),{tenantId:A,endUserId:N}=T.useMemo(()=>t.getTenantAndEndUserId(s==null?void 0:s.accessToken),[s==null?void 0:s.accessToken]),D=T.useMemo(()=>A||N,[A,N]),F=()=>{if(!l||!C)return;const w={connectionId:i,id:t.v4(),title:"Title",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]},_={id:t.v4(),cards:[w],activeCardId:w.id};g(C.id,_),p(_.id)},R=()=>{f(!0)};async function U(){if(c(!0),!n||!r)return;const w=_e(o),_=JSON.stringify(w),G={...r,template:_,filterValues:h};await Le(G,s==null?void 0:s.accessToken),t.ue.success("Lens saved"),b==null||b(w),c(!1)}return d?null:e.jsxs("div",{className:"flex items-center justify-between gap-2 border-b py-1 pl-3 pr-2",role:"dashboard-controls",children:[!d&&e.jsx("div",{className:"flex items-center gap-3",children:e.jsx(t.Label,{children:r==null?void 0:r.name})}),e.jsxs("div",{className:"flex gap-2",children:[!1,!d&&e.jsx(Ys,{}),!d&&e.jsx(yt,{}),l&&!d&&D&&e.jsx(Ws,{}),!d&&D&&e.jsx(Ks,{}),l&&!d&&e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",disabled:!l,onClick:F,children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Frame"]}),!l&&e.jsx(t.Button,{variant:"ghost",className:"h-7 p-2",onClick:R,size:"sm",children:e.jsx(t.Pencil,{className:"h-4 w-4"})}),l&&!d&&e.jsx(t.IconButton,{className:"h-7 p-2",onClick:()=>{f(!1),S(!1)},children:e.jsx(t.Cross2Icon,{className:"h-4 w-4"})}),l&&!d&&D&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",disabled:u,onClick:U,size:"sm",children:e.jsx(Vs,{className:"h-4 w-4"})}),!x&&!d&&l&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",onClick:()=>m(!0),size:"sm",children:e.jsx(lt,{className:"h-4 w-4"})}),!d&&l&&!1,!d&&!1]})]})}function en({name:s,items:a,value:n,onChange:r,isLoading:o,isError:l,refetch:i,className:u,icon:c,showNone:h=!1}){const d=le.useRef(null),[x,b]=le.useState(!1),[m,g]=le.useState("100%"),p=h?[...a||[],{id:"none",name:"None"}]:a;le.useLayoutEffect(()=>{d.current&&g(d.current.offsetWidth)},[]);function y(){var S;return o?"Loading...":l?"Error":n?(S=a==null?void 0:a.find(C=>C.id===n))==null?void 0:S.name:`Select ${s}`}function f(S,C){r(C),b(!1)}return e.jsxs(t.Popover,{open:x,onOpenChange:b,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{ref:d,variant:"outline",role:"combobox","aria-expanded":x,className:t.cn("flex h-9 w-full justify-between",u),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[c&&e.jsx("div",{className:"mr-2",children:c}),e.jsx("span",{className:"max-w-[150px] overflow-hidden truncate text-ellipsis whitespace-nowrap",children:y()})]}),e.jsx(t.ChevronDown,{className:"size-4 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-full p-0",style:{minWidth:m},children:e.jsxs(t.Command,{children:[e.jsxs("div",{className:"flex h-9 items-center justify-between border-b pr-1",children:[e.jsx(t.CommandInput,{className:"h-9",placeholder:"Search..."}),e.jsx(t.IconButton,{className:"h-7",onClick:i,children:e.jsx(mt,{className:t.cn("size-3.5 opacity-50",o&&"animate-spin")})})]}),e.jsxs(t.CommandList,{children:[e.jsxs(t.CommandEmpty,{children:["No ",s==null?void 0:s.toLowerCase()," found."]}),e.jsx(t.CommandGroup,{children:p==null?void 0:p.map(S=>e.jsx(t.CommandItem,{className:"overflow-x-auto",value:S.name,onSelect:C=>f(C,S.id),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Check,{className:t.cn("size-4",n===S.id?"opacity-100":"opacity-0")}),e.jsx("div",{title:S.name,children:S.name})]})},S.id))})]})]})})]})}function ie({title:s,items:a,value:n,isLoading:r,isError:o,refetch:l,onChange:i,showNone:u=!1,className:c,disabled:h=!1,type:d="combobox"}){const x=r?"Loading...":o?"Error":n;function b(){if(s==="Connection")return e.jsx(Rs,{className:"size-4"});if(s==="Database")return e.jsx(js,{className:"size-4"});if(s==="Schema")return e.jsx(Fs,{className:"size-4"});if(s==="Table")return e.jsx(Ve,{className:"size-4"});if(s==="Model")return e.jsx(ds,{className:"size-4"})}function m(){return e.jsxs(t.Select,{disabled:h,value:x,onValueChange:i,children:[e.jsx(t.SelectTrigger,{className:t.cn("h-9 w-full border-muted focus:ring-1 focus:ring-offset-0",c),children:e.jsx(t.SelectValue,{placeholder:r?"Loading...":o?"Error":`Select ${s}`})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[r?e.jsx(t.SelectLabel,{children:"Loading..."}):e.jsxs(t.SelectLabel,{className:"flex items-center justify-between gap-2",children:[s,e.jsx(mt,{className:t.cn("mr-1 size-3 cursor-pointer text-muted-foreground hover:text-foreground",r&&"animate-spin"),onClick:l})]}),e.jsxs(e.Fragment,{children:[u&&e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),a&&a.map((p,y)=>e.jsx(t.SelectItem,{value:p.id,children:p.name},y))]}),(r||o)&&e.jsx(t.SelectItem,{value:x,children:x})]})})]})}function g(){return e.jsx(en,{icon:b(),items:a,value:x,onChange:i||(()=>{}),isLoading:r,isError:o,refetch:l,name:s,showNone:u})}return e.jsx(t.TooltipProvider$1,{delayDuration:0,children:e.jsx("div",{className:"flex items-center gap-2",children:d==="select"?m():g()})})}function tn({column:s}){const[a,n]=T.useState(!1),r=t.useEditorStore(p=>p.selectedTableName),o=t.useEditorStore(p=>p.selectedSchemaName),l=t.useEditorStore(p=>p.selectedDatabaseName),i=t.useEditorStore(p=>p.selectedDatamodelId),u=t.useEditorStore(p=>p.sqlGen),{modelItems:c,connectionType:h}=t.useEditorAside(),d=t.useEditorStore(p=>p.actions.setSqlGen),x=t.useEditorStore(p=>p.actions.setCardSql),b=t.useEditorStore(p=>p.actions.setRunSql);function m(p,y){var A;const f=t.createSqlGenConfig(p,u,y),S=(A=c==null?void 0:c.find(N=>N.id===i))==null?void 0:A.name;d(f);const C=t.getSql(f,t.fmt(o||""),t.fmt(r),S,h,l||"");C&&(x(C),b(!0)),n(!1)}function g(){return e.jsxs("ul",{className:"text-xs",children:[e.jsx("li",{onClick:()=>m(s,"COUNT"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Count"}),e.jsx("li",{onClick:()=>m(s,"AVG"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Avg"}),e.jsx("li",{onClick:()=>m(s,"SUM"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Sum"}),e.jsx("li",{onClick:()=>m(s,"MAX"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Max"}),e.jsx("li",{onClick:()=>m(s,"MIN"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Min"})]})}return e.jsxs(t.HoverCard,{openDelay:0,open:a,onOpenChange:n,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(ot,{className:"invisible size-3.5 cursor-pointer text-foreground/40 group-hover:visible"})}),e.jsx(t.HoverCardContent,{align:"center",className:"w-20 rounded-md p-0 shadow-none",children:g()})]})}function Pe({column:s}){return e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger$1,{className:"shrink-0",asChild:!0,children:t.isTextDataType(s.data_type)?e.jsx(t.TextIcon,{className:"size-3 text-muted-foreground"}):t.isNumberDataType(s.data_type)?e.jsx(ct,{className:"size-3 text-muted-foreground"}):t.isDateDataType(s.data_type)?e.jsx(t.Calendar,{className:"size-3 text-muted-foreground"}):t.isBooleanDataType(s.data_type)?e.jsx(Hs,{className:"size-3 text-muted-foreground"}):s.data_type.startsWith("json")?e.jsx(us,{className:"size-3 text-muted-foreground"}):e.jsx(t.QuestionMarkIcon,{className:"size-3 text-muted-foreground"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{className:"text-xs",children:s.data_type})})]})}function we({column:s,className:a}){return e.jsxs("div",{className:t.cn("flex min-h-8 grow items-center space-x-2 px-2 text-sm",a),children:[e.jsx(Pe,{column:s}),e.jsx("span",{children:s.column_name})]})}function sn({id:s,children:a,type:n="column"}){const{attributes:r,listeners:o,setNodeRef:l,transform:i}=t.useDraggable({id:s,data:{type:n}}),u=i?{translate:t.CSS.Translate.toString(i),transform:t.CSS.Transform.toString(i)}:void 0;return e.jsx("div",{ref:l,style:u,...o,...r,children:a})}function nn({onClick:s,column:a}){const n=t.useDndStore(d=>d.activeId),r=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.groupByColumns}),o=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.metricColumns}),l=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.filterColumns});function i(d){return u(d)||c(d)||h(d)}function u(d){return r==null?void 0:r.some(x=>x.name===d.column_name)}function c(d){return o==null?void 0:o.some(x=>x.name===d.column_name)}function h(d){return l==null?void 0:l.some(x=>x.name===d.column_name)}return e.jsxs("div",{onClick:()=>s(a),className:t.cn("relative grow overflow-x-hidden overflow-y-hidden rounded-md active:border active:border-dashed active:border-muted-foreground",{"bg-muted-foreground/10":i(a)}),children:[n===a.column_name&&e.jsx(t.DragOverlay,{children:e.jsx(we,{column:a,className:"rounded-md border-2 border-dashed border-muted-foreground bg-muted shadow-md"})}),n===a.column_name&&e.jsx("div",{className:"absolute inset-0",children:n===a.column_name&&e.jsx(we,{column:a})}),e.jsx(sn,{id:a.column_name,type:"column",children:e.jsx(we,{column:a})})]})}const ke={id:"__placeholder__",role:"groupby",type:"string"},De={id:"__placeholder__",role:"groupby",type:"string"},Ie={id:"__placeholder__",role:"metric",type:"number"},Ee={name:"Filter",id:"__placeholder__",role:"filter",type:"string"},bt={id:"__placeholder__"},te="droppable-group-by",se="droppable-metrics",ne="droppable-pivot-by",be="sortable-group-by",Oe="sortable-metrics",ve="sortable-pivot-by",ze="droppable-filters",Fe="sortable-filters",qe="sortable-order-by";function an(){const s=t.useDndStore(E=>E.insertIndex),a=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.groupByColumns)||[]}),n=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.metricColumns)||[]}),r=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.filterColumns)||[]}),o=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.orderBy)||[]}),l=t.useEditorStore(E=>{var j;return((j=E.card.queryConfig)==null?void 0:j.pivotColumns)||[]}),{setInsertIndex:i,setActiveId:u,setPreviewGroupByColumns:c,setPreviewMetricColumns:h,setPreviewFilterColumns:d,setPreviewPivotColumns:x}=t.useDndActions(),{setGroupByColumns:b,setMetricColumns:m,setFilterColumns:g,setOrderByColumns:p,setPivotByColumns:y}=t.useEditorActions(),{columns:f}=t.useDataColumns(),{formattedTableName:S}=t.useEditorAside(),C=t.useSensors(t.useSensor(t.MouseSensor,{activationConstraint:{distance:1}})),A=E=>{var B;const{active:j,over:M}=E;if(((B=j.data.current)==null?void 0:B.type)==="column"&&M){const v=M.id,O=f.find(z=>z.column_name===j.id);if(!O)return;const I=t.isNumberDataType(O.data_type),$=t.isTextDataType(O.data_type),L=t.isDateDataType(O.data_type);if($||L){if(a.some(q=>q.id===v)){const q=a.findIndex(V=>V.id===v);i(q);const J=a.filter(V=>V.id!==ke.id);J.splice(q,0,ke),c(J)}else if(M.id===te){i(a.length);const q=[...a,ke];c(q),h(n),d(r)}}if($||L){if(l.some(q=>q.id===v)){const q=l.findIndex(V=>V.id===v);i(q);const J=l.filter(V=>V.id!==De.id);J.splice(q,0,De),x(J)}else if(M.id===ne){i(l.length);const q=[...l,De];x(q),c(a),h(n),d(r)}}if(I||$){if(n.some(q=>q.id===v)){console.log("isOverMetricSortable",v);const q=n.findIndex(V=>V.id===v);i(q);const J=n.filter(V=>V.id!==Ie.id);J.splice(q,0,Ie),h(J)}else if(M.id===se){i(n.length);const q=[...n,Ie];h(q),d(r),c(a),x(l)}}if(D(O)){if(r.some(q=>q.id===v)){const q=r.findIndex(V=>V.id===v);i(q);const J=r.filter(V=>V.id!==Ee.id);J.splice(q,0,Ee),d(J)}else if(M.id===ze){i(r.length);const q=[...r,Ee];d(q),h(n),c(a),x(l)}}}else c(a),h(n),d(r),x(l)};function N(E){u(E.active.id),i(null),c(a),h(n),d(r),x(l)}function D(E){return!r.some(j=>j.name===E.column_name)}function F(E,j="SUM"){const{column_name:M,data_type:B}=E,v=[...n],O=s!==null?s:n.length,I={...E,id:Z(M),name:M,label:me(M,j),type:B,role:"metric",aggregate:j};v.splice(O,0,I),m(v)}function R(E){const j=t.isTextDataType(E.data_type);t.isDateDataType(E.data_type);const M=[...l],B=s!==null?s:l.length;let v;j?v={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby"}:v={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby",label:ce(E.column_name,"month"),granularity:"month"},M.splice(B,0,v),y(M)}function U(E){const j=t.isTextDataType(E.data_type);t.isDateDataType(E.data_type);const M=[...a],B=s!==null?s:a.length;let v;j?v={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby"}:v={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,role:"groupby",label:ce(E.column_name,"month"),granularity:"month"},M.splice(B,0,v),b(M)}function w(E){const j=[...r],M=s!==null?s:r.length,B={...E,id:Z(E.column_name),name:E.column_name,type:E.data_type,sql:t.getDefaultFilterSql(E.data_type,S,t.fmt(E.column_name)),role:"filter"};j.splice(M,0,B),g(j)}function _(E){var v,O,I,$,L,z,q,J;const{active:j,over:M}=E,B=j.data.current;if((B==null?void 0:B.type)==="column"&&M){const V=f.find(K=>K.column_name===j.id);if(!V)return;const Q=t.isNumberDataType(V.data_type),k=t.isTextDataType(V.data_type),H=t.isDateDataType(V.data_type);(k||H)&&(M.id===te||((v=M.data.current)==null?void 0:v.type)===be)&&U(V),(k||H)&&(M.id===ne||((O=M.data.current)==null?void 0:O.type)===ve)?R(V):(Q||k)&&(M.id===se||((I=M.data.current)==null?void 0:I.type)===Oe)?(Q&&F(V),k&&F(V,"COUNT")):D(V)&&(M.id===ze||(($=M.data.current)==null?void 0:$.type)===Fe)&&w(V)}if(M&&((L=M.data.current)==null?void 0:L.type)===be&&a.some(V=>V.id===j.id)){const V=a.findIndex(k=>k.id===j.id),Q=a.findIndex(k=>k.id===M.id);if(V!==-1&&Q!==-1&&V!==Q){const k=[...a],[H]=k.splice(V,1);k.splice(Q,0,H),b(k)}}if(M&&((z=M.data.current)==null?void 0:z.type)===ve&&l.some(V=>V.id===j.id)){const V=l.findIndex(k=>k.id===j.id),Q=l.findIndex(k=>k.id===M.id);if(V!==-1&&Q!==-1&&V!==Q){const k=[...l],[H]=k.splice(V,1);k.splice(Q,0,H),y(k)}}if(M&&((q=M.data.current)==null?void 0:q.type)===Oe&&n.some(V=>V.id===j.id)){const V=n.findIndex(k=>k.id===j.id),Q=n.findIndex(k=>k.id===M.id);if(V!==-1&&Q!==-1&&V!==Q){const k=[...n],[H]=k.splice(V,1);k.splice(Q,0,H),m(k)}}if(M&&((J=M.data.current)==null?void 0:J.type)===Fe&&r.some(V=>V.id===j.id)){const V=r.findIndex(k=>k.id===j.id),Q=r.findIndex(k=>k.id===M.id);if(V!==-1&&Q!==-1&&V!==Q){const k=[...r],[H]=k.splice(V,1);k.splice(Q,0,H),g(k)}}G(E),u(null),i(null),c(a),h(n),d(r),x(l)}function G(E){const{active:j,over:M}=E,B=j.data.current;if((B==null?void 0:B.type)===qe&&M){const v=o.findIndex(I=>ue(I.columnId)===j.id),O=o.findIndex(I=>ue(I.columnId)===M.id);if(v!==-1&&O!==-1&&v!==O){const I=[...o],[$]=I.splice(v,1);I.splice(O,0,$),p(I)}}}function P(){u(null),i(null),c(a),h(n),d(r)}return{handleDragOver:A,handleDragEnd:_,handleDragStart:N,handleDragCancel:P,sensors:C}}function Z(s){return`${s}-${t.v4()}`}function me(s,a){const n=t.toTitleCase(s.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 ue(s){return`${qe}-${s}`}function ce(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"day":return`Date of ${n}`;case"week":return`Week of ${n}`;case"month":return`Month of ${n}`;case"year":return`Year of ${n}`;default:return n}}function rn({schemaName:s,tableName:a,columns:n,status:r}){const o=t.useEditorStore(j=>j.sqlGen),l=t.useEditorStore(j=>j.isDevMode),i=t.useEditorStore(j=>j.selectedConnectionId),u=t.useEditorStore(j=>{var M,B;return(B=(M=j.card)==null?void 0:M.queryConfig)==null?void 0:B.metricColumns});t.useEditorStore(j=>{var M,B;return(B=(M=j.card)==null?void 0:M.queryConfig)==null?void 0:B.groupByColumns});const c=t.useEditorStore(j=>j.selectedDatabaseName),h=t.useEditorStore(j=>j.selectedSchemaName),d=t.useEditorStore(j=>j.selectedTableName),x=t.useEditorStore(j=>j.selectedDatamodelId),b=t.useDashboardStore(j=>j.dashboard.aiScopeTables),{addToAIScope:m,removeFromAIScope:g}=t.useDashboardActions(),{addColumnToMetricColumns:p,addColumnToGroupBy:y,setCardSql:f,setRunSql:S,setSqlGen:C,setIsShowingVisual:A}=t.useEditorActions(),{connectionType:N,modelItems:D,selectedDatamodelName:F}=t.useEditorAside();function R(j){var M;if(l){const B=t.createSqlGenConfig(j,o);C(B);const v=(M=D==null?void 0:D.find(I=>I.id===x))==null?void 0:M.name,O=t.getSql(B,t.fmt(s),t.fmt(a),v,N,c||"");O&&(f(O),S(!0))}if(!l){if(t.isTextDataType(j.data_type))if(u&&u.length>0){const B={...j,role:"groupby",id:Z(j.column_name),name:j.column_name,type:j.data_type};y(B)}else{const B={...j,role:"metric",id:Z(j.column_name),name:j.column_name,label:me(j.column_name,"COUNT"),type:j.data_type,aggregate:"COUNT"};p(B)}if(t.isDateDataType(j.data_type)){const B={...j,role:"groupby",id:Z(j.column_name),name:j.column_name,type:j.data_type,label:ce(j.column_name,"month"),granularity:"month"};y(B)}if(t.isNumberDataType(j.data_type)){const B={...j,role:"metric",id:Z(j.column_name),name:j.column_name,label:me(j.column_name,"SUM"),type:j.data_type,aggregate:"SUM"};p(B)}}}function U(j){j===!0&&(d||F)&&i?(m({connectionId:i,connectionType:N==="S3"||N==="API"?"DuckDb":N,databaseName:c||"",schemaName:h||"",tableName:d||"",datamodelName:t.resolveDatamodelName(F),datamodelId:x||""}),t.ue.success("Added to Assistant Scope",{position:"top-right",duration:2500,richColors:!0})):(g({connectionId:i||"",connectionType:N||"",tableName:d||"",datamodelName:t.resolveDatamodelName(F),datamodelId:x||"",databaseName:c||"",schemaName:h||""}),t.ue("Removed from Assistant Scope",{position:"top-right",duration:2e3,richColors:!0}))}function w(){const j=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:F,connectionType:N,databaseName:c||""});A(!1);const M=N==="MSSQL"?`SELECT top 10 * FROM ${j}`:`SELECT * FROM ${j} LIMIT 10`;f(M),S(!0)}function _(){const j=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:F,connectionType:N,databaseName:c||""});f(`SELECT COUNT(*) as count FROM ${j}`),S(!0)}function G(){return(b==null?void 0:b.find(j=>j.connectionId===i&&(j.tableName===d||j.datamodelName===F)))!==void 0}function P(j){return l?e.jsxs("div",{className:"flex grow items-center justify-between space-x-2 rounded-md px-2 py-1",onClick:()=>R(j),children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(Pe,{column:j}),e.jsx("span",{children:j.column_name})]}),e.jsx("div",{onClick:M=>M.stopPropagation(),className:"flex items-center justify-end",children:t.isNumberDataType(j.data_type)&&e.jsx(tn,{column:j})})]}):e.jsx(nn,{onClick:R,column:j})}function E(){return e.jsxs(e.Fragment,{children:[e.jsxs(t.Toggle,{title:`${G()?"Remove from":"Add to"} Assistant Scope`,pressed:G(),onPressedChange:U,variant:"outline",className:t.cn("h-9 px-2",{}),children:[e.jsx(t.Plus,{className:t.cn("mr-1 size-4",{"text-green-400":G(),"text-muted-foreground":!G()})}),"AI"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:w,size:"sm",variant:"secondary",className:"flex",children:[r!=="Success"?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(Ls,{className:"mr-2 size-4"}),"Top 10"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:_,size:"sm",variant:"secondary",className:"w-full",children:[r!=="Success"?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(xt,{className:"mr-2 size-4"}),"Count"]})]})}return r!=="Success"?e.jsx("div",{className:"flex grow flex-col justify-start gap-2 px-6",children:Array.from({length:3}).map((j,M)=>e.jsx(t.Skeleton,{className:"h-7 w-full"},M))}):e.jsxs("div",{className:"flex grow flex-col space-y-4 px-4",children:[l&&e.jsx("div",{className:"mx-2 flex items-center justify-center gap-2",children:E()}),e.jsxs(t.CustomScrollArea,{className:"flex-1 basis-0 overflow-y-auto px-2 py-0",children:[e.jsx("div",{className:"flex flex-col gap-1",children:n==null?void 0:n.map(j=>e.jsx("div",{className:t.cn("mr-2 cursor-pointer rounded-md text-sm transition-colors hover:bg-muted"),children:P(j)},j.column_name))}),e.jsx(t.ScrollBar,{orientation:"vertical"}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function vt({ColumnsComponent:s,type:a}){var Ue;const n=t.useEditorStore(W=>W.card),r=t.useEditorStore(W=>W.selectedConnectionId),o=t.useDashboardStore(W=>W.showFilters),l=t.useEditorStore(W=>W.selectedDatamodelId),i=t.useEditorStore(W=>W.selectedDatabaseName),u=t.useEditorStore(W=>W.selectedSchemaName),c=t.useEditorStore(W=>W.selectedTableName),h=t.useEditorStore(W=>W.isDevMode),{setSelectedTableName:d,setIsDevMode:x,setCardSql:b}=t.useEditorActions(),{connectionItems:m,modelItems:g,schemaItems:p,databaseItems:y,tableItems:f,columns:S,isConnectionsLoading:C,isModelsLoading:A,isConnectionsError:N,isModelsError:D,isDatabasesLoadingOrFetching:F,isDatabaseError:R,isSchemaLoadingOrFetching:U,isSchemaError:w,isColumnsError:_,isColumnsLoadingOrFetching:G,isTableLoadingOrFetching:P,isTableError:E,connectionType:j,isNonDbConnection:M,refetchDatabases:B,refetchModels:v,refetchSchemas:O,refetchTables:I,refetchColumns:$,refetchConnections:L,handleConnectionChange:z,handleDatabaseChange:q,handleSchemaChange:J,handleDatamodelChange:V,tableName:Q,schemaName:k}=t.useEditorAside(),{data:H,refetch:K}=t.useDashboardCardQuery(n);function X(){return s?s(k,Q,S):e.jsx(rn,{schemaName:k,tableName:Q,columns:S,status:G?"Loading...":_?"Error":"Success"})}function he(){window.location.href="mailto:support@semaphor.cloud?subject=Help&body=I need help with..."}function xe(W){W===!0?H&&(b(H.userSql),setTimeout(()=>{K()},100)):b(""),x(W)}function pe(){if(!r||j==="S3"||j==="API")return null;if(!l||l==="none")return e.jsx(ie,{refetch:B,showNone:!0,onChange:q,isError:R,isLoading:F,items:y,value:i||"",title:"Database",type:"combobox"})}function fe(){if(r&&!M&&(!i||i==="none")&&(!l||l==="none"))return e.jsxs("div",{className:"grid grid-cols-3",children:[e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Label,{children:"OR"})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})})]})}function $t(){if(!r||j==="S3"||j==="API")return null;if(!i||i==="none")return e.jsx(ie,{refetch:v,showNone:!0,onChange:V,isError:D,isLoading:A,items:g,value:l||"",title:"Model",type:"combobox"})}function Qt(){return!r||j==="S3"||j==="API"||!i||i==="none"?null:e.jsx(ie,{refetch:O,onChange:J,isError:w,isLoading:U,items:p,value:u||"",title:"Schema",type:"combobox"})}function Jt(){function W(){let ee=!1;return j==="API"?(ee=!1,ee):((j==="S3"||r&&i&&i!=="none"&&u)&&(ee=!0),ee)}return W()?e.jsx(ie,{refetch:I,onChange:d,isError:E,items:f,isLoading:P,value:c||"",title:"Table",type:"combobox"}):null}return e.jsxs("aside",{className:t.cn("flex h-full min-w-[310px] max-w-[310px] flex-col justify-between rounded-none",{"border-r-[1px]":!o}),children:[e.jsxs("section",{className:"space-y-3 px-6 py-4",children:[e.jsx(ie,{refetch:L,items:m,title:"Connection",value:r||"",onChange:z,isLoading:C,isError:N,type:"combobox",connectionTooltip:((Ue=m==null?void 0:m.find(W=>W.id===r))==null?void 0:Ue.type)||""}),pe(),fe(),$t(),Qt(),Jt()]}),X(),e.jsx("div",{className:"flex items-center justify-between gap-2 px-6 py-4",children:a==="main"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Switch,{className:"data-[state=checked]:bg-muted-foreground/80",checked:h,onCheckedChange:xe,id:"dev-mode"}),e.jsx(t.Label,{htmlFor:"dev-mode",children:"Dev Mode"})]}),e.jsxs(t.Button,{onClick:he,size:"sm",variant:"secondary",className:"group flex items-center gap-2 rounded-full",children:[e.jsx(zs,{className:"size-4"}),e.jsx("span",{className:"hidden group-hover:block",children:"Help"})]})]})})]})}function Ct({sql:s,error:a,onClick:n}){const r=t.useEditorStore(c=>c.isDevMode);t.useEditorStore(c=>c.card);const{setIsDevMode:o,setCardSql:l,setRunSql:i}=t.useEditorActions();if(!s&&!a)return null;function u(){o(!r),l(s),setTimeout(()=>{i(!0)},100)}return e.jsxs(t.HoverCard,{openDelay:a?0:500,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.Button,{variant:"link",className:"h-5 p-0",children:a?e.jsx(at,{className:"h-4 w-4 text-red-500"}):e.jsx(t.Info,{className:"h-4 w-4 text-muted-foreground"})})}),e.jsx(t.HoverCardContent,{align:"start",className:"w-full min-w-fit max-w-[700px]",children:e.jsx("div",{className:"flex justify-between space-x-4",children:e.jsxs("div",{className:"flex grow flex-col space-y-2",children:[e.jsxs(t.ScrollArea,{className:"max-h-[250px] min-h-0 overflow-scroll text-xs leading-relaxed text-foreground/80",children:[a&&a,s&&e.jsxs("div",{className:"flex flex-col space-y-1.5",children:[e.jsx("span",{className:"text-xs font-semibold",children:"Resolved SQL"}),e.jsx("div",{className:"rounded-md",children:e.jsx(t.CodeHighlighter,{code:s,language:"sql"})})]})]}),!r&&e.jsxs(t.Button,{onClick:u,variant:"secondary",size:"sm",children:[e.jsx(Cs,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),a&&n&&e.jsxs(t.Button,{onClick:n,size:"sm",variant:"secondary",children:[e.jsx(Ds,{className:"mr-2 h-4 w-4"}),"Debug with Assistant"]})]})})})]})}var St={color:void 0,size:void 0,className:void 0,style:void 0,attr:void 0},Ke=T.createContext&&T.createContext(St),on=["attr","size","title"];function ln(s,a){if(s==null)return{};var n=cn(s,a),r,o;if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(s);for(o=0;o<l.length;o++)r=l[o],!(a.indexOf(r)>=0)&&Object.prototype.propertyIsEnumerable.call(s,r)&&(n[r]=s[r])}return n}function cn(s,a){if(s==null)return{};var n={};for(var r in s)if(Object.prototype.hasOwnProperty.call(s,r)){if(a.indexOf(r)>=0)continue;n[r]=s[r]}return n}function Ce(){return Ce=Object.assign?Object.assign.bind():function(s){for(var a=1;a<arguments.length;a++){var n=arguments[a];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(s[r]=n[r])}return s},Ce.apply(this,arguments)}function Ye(s,a){var n=Object.keys(s);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(s);a&&(r=r.filter(function(o){return Object.getOwnPropertyDescriptor(s,o).enumerable})),n.push.apply(n,r)}return n}function Se(s){for(var a=1;a<arguments.length;a++){var n=arguments[a]!=null?arguments[a]:{};a%2?Ye(Object(n),!0).forEach(function(r){dn(s,r,n[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(s,Object.getOwnPropertyDescriptors(n)):Ye(Object(n)).forEach(function(r){Object.defineProperty(s,r,Object.getOwnPropertyDescriptor(n,r))})}return s}function dn(s,a,n){return a=un(a),a in s?Object.defineProperty(s,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):s[a]=n,s}function un(s){var a=mn(s,"string");return typeof a=="symbol"?a:a+""}function mn(s,a){if(typeof s!="object"||!s)return s;var n=s[Symbol.toPrimitive];if(n!==void 0){var r=n.call(s,a);if(typeof r!="object")return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return(a==="string"?String:Number)(s)}function Nt(s){return s&&s.map((a,n)=>T.createElement(a.tag,Se({key:n},a.attr),Nt(a.child)))}function re(s){return a=>T.createElement(hn,Ce({attr:Se({},s.attr)},a),Nt(s.child))}function hn(s){var a=n=>{var{attr:r,size:o,title:l}=s,i=ln(s,on),u=o||n.size||"1em",c;return n.className&&(c=n.className),s.className&&(c=(c?c+" ":"")+s.className),T.createElement("svg",Ce({stroke:"currentColor",fill:"currentColor",strokeWidth:"0"},n.attr,r,i,{className:c,style:Se(Se({color:s.color||n.color},n.style),s.style),height:u,width:u,xmlns:"http://www.w3.org/2000/svg"}),l&&T.createElement("title",null,l),s.children)};return Ke!==void 0?T.createElement(Ke.Consumer,null,n=>a(n)):a(St)}function xn(s){return re({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"},child:[]}]})(s)}function wt(s){const{theme:a}=t.useTheme(),n=t.useDashboardStore(p=>p.theme),r=t.useEditorStore(p=>p.frame),o=t.useEditorStore(p=>p.card),l=t.useEditorStore(p=>p.activeTabCardId),i=t.Le(),{refetch:u}=t.useDashboardCardQuery(o),{updateCardTab:c,setRunSql:h}=t.useEditorActions(),d=T.useRef(),x=T.useRef({}),b=r.cards.find(p=>p.id===l),m=T.useCallback(()=>{let p="";return s==="python"&&(p=(b==null?void 0:b.python)||t.PYTHON_DEFAULT_CODE),s==="sql"&&(p=(b==null?void 0:b.sql)||""),p},[s,b]);T.useEffect(()=>{if(l&&o.id&&o.id!==l){if(d.current&&i){const p=d.current.getModel();p&&(x.current[o.id]=p);let y=x.current[l];if(!y){const f=m();y=i.editor.createModel(f,s),x.current[l]=y}d.current.setModel(y)}c(l,o),h(!0)}},[l,o,c,i,h,s,m]),T.useEffect(()=>{if(!i)return;const p=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",y=n==="system"?p:n;i.editor.defineTheme("default",{base:y==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":y==="light"?"#ffffff":t.hslStringToHex("240 10% 10%")}}),i.editor.setTheme("default")},[a,n,i]),T.useEffect(()=>{const p=d.current;p&&i&&o&&p.addCommand(i.KeyMod.Shift|i.KeyCode.Enter,function(){return u(),!1})},[i,u,o]);function g(p,y){if(d.current=p,!x.current[o.id]){const f=p.getModel(),S=m();x.current[o.id]=f||(y==null?void 0:y.editor.createModel(S,s))||p.getModel()}p.setModel(x.current[o.id])}return{handleEditorDidMount:g}}function pn(){const s=t.useEditorStore(i=>i.card.sql),a=t.useEditorStore(i=>i.sqlGen),{setCardSql:n,setSqlGen:r}=t.useEditorActions(),{handleEditorDidMount:o}=wt("sql");function l(i){n(i),t.isObjectEmpty(a)||r({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!s&&e.jsx("div",{className:"pointer-events-none absolute left-[65px] top-3 z-10 font-mono text-sm text-muted-foreground",children:"Write your SQL here"}),e.jsx(t.Ft,{theme:"default",className:"pt-3",onChange:l,value:s||"",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:o})]})}function fn(){const s=t.useEditorStore(o=>o.card.python),{setCardPython:a}=t.useEditorActions(),{handleEditorDidMount:n}=wt("python");function r(o){a(o)}return e.jsx(t.Ft,{className:"pt-3",theme:"default",onChange:r,value:s||t.PYTHON_DEFAULT_CODE,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:n})}function gn({jsonString:s}){return e.jsx(t.Ft,{theme:"default",defaultLanguage:"json",className:"mt-1 min-h-[220px]",value:s,options:{readOnly:!0,minimap:{enabled:!1},lineNumbers:"off",glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})}function kt(s){const[a,n]=T.useState(""),[r,o]=T.useState(null),{authToken:l}=t.useSemaphorContext(),i=t.useEditorStore(k=>{var H;return(H=k==null?void 0:k.card)==null?void 0:H.sql}),u=t.useEditorStore(k=>{var H;return(H=k==null?void 0:k.card)==null?void 0:H.python}),{setCardPython:c,setCardSql:h,setRunSql:d,switchCardType:x}=t.useEditorActions(),b=t.useEditorStore(k=>k.currentAssitantScope)||[],m=t.useEditorStore(k=>k.userInputForAI),g=t.useEditorStore(k=>k.triggerAIRun),p=t.useEditorStore(k=>k.selectedDatabaseName),y=t.useEditorStore(k=>k.selectedTableName),f=t.useEditorStore(k=>k.selectedConnectionId),S=t.useEditorStore(k=>k.selectedSchemaName),C=t.useDashboardStore(k=>k.dashboard.aiScopeTables),{setTriggerAIRun:A,setCurrentAssitantScope:N}=t.useEditorActions(),{selectedDatamodelName:D,connectionType:F,selectedDatamodelId:R}=t.useEditorAside(),{messages:U,setMessages:w,reload:_,error:G,append:P,input:E,setInput:j,status:M,handleInputChange:B,handleSubmit:v,stop:O}=t.useChat({api:"https://semaphor.cloud/api/v1/assistant",headers:{Authorization:`Bearer ${l==null?void 0:l.accessToken}`},body:{random:Math.random(),assistantId:"editor-assistant"},maxSteps:10,onError:k=>{console.log("on error",k)},onToolCall:I,onFinish(k,H){$(H.usage),n(""),H.finishReason==="stop"&&n("")}});T.useEffect(()=>{g&&m&&(B({target:{value:m}}),setTimeout(()=>{v(),A(!1)},100))},[g,v,m,A,B]);async function I({toolCall:k}){if(k.toolName==="createChartFromSql"){const H=k.args.sql,K=k.args.python,X=k.args.chartType;return x(X),h(H),c(K),d(!0),{status:"success",message:"Let the user know that chart is created"}}else if(k.toolName==="getTableColumns")n("Getting table columns...");else{if(k.toolName==="getUserSql")return i?{status:"success",sql:i,python:u||""}:{status:"error",message:"No SQL found"};if(k.toolName==="getCurrentTableScope")return n("Getting table scope..."),J(),{status:"success",tables:b};n("Analyzing...")}}function $(k){o(H=>H?{promptTokens:H.promptTokens+k.promptTokens,completionTokens:H.completionTokens+k.completionTokens,totalTokens:H.totalTokens+k.totalTokens}:k)}function L(k){const H=t.removeFromScopeArray(b,k);z(H)}function z(k){const H=t.sanitizeAIScope(k);N(H),w(K=>[...K,{id:crypto.randomUUID(),role:"system",content:`Current scope of tables | ${JSON.stringify(H)}`}])}function q(){const k={databaseName:p||"",schemaName:S||"",tableName:y||"",datamodelId:R||"",datamodelName:t.resolveDatamodelName(D),connectionId:f||"",connectionType:F||""},H=t.dedup([...b,k]);z(H)}function J(){const k={databaseName:p||"",schemaName:S||"",tableName:y||"",datamodelName:t.resolveDatamodelName(D),connectionId:f||"",connectionType:F||"",datamodelId:R||""};if(!t.isInAiScopeArray([...C||[],...b],k)||b.length===0){const K=t.dedup([...C||[],...b,k]).filter(X=>X.connectionId===k.connectionId);z(K)}}const V=!["ready","error"].includes(M),Q=t.getErrorMessage(G);return{messages:U,setMessages:w,reload:_,append:P,input:E,setInput:j,isLoading:V,handleSetAIScope:J,handleAddCurrentTableToScope:q,removeFromCurrentSelections:L,currentAssitantScope:b,setCurrentAssitantScope:N,handleInputChange:B,handleSubmit:v,stop:O,chatStatus:M,toolStatus:a,llmUsage:r,onFinish:s,errorMessage:Q}}function jn(s){const a=s.toUpperCase().indexOf("WHERE"),n=s.toUpperCase().indexOf("GROUP BY"),r=s.toUpperCase().indexOf("LIMIT"),o=s.toUpperCase().indexOf("ORDER BY");let l=s.length;if(o!==-1&&(l=o),r!==-1&&r<l&&(l=r),a!==-1){const i=s.substring(0,a),u=s.substring(a);return`${i} {{ filters | where }} AND ${u.substring(6)}`}else if(n!==-1){const i=s.substring(0,n),u=s.substring(n);return`${i} {{ filters | where }} ${u}`}else{const i=s.substring(0,l),u=s.substring(l);return console.log("beforeClause",i),console.log("afterClause",u),u?`${i} {{ filters | where }} ${u}`:`${i.replace(/;$/,"")} {{ filters | where }}`}}function Dt({variant:s="secondary"}){const{setCardSql:a}=t.useEditorActions(),n=t.useEditorStore(m=>m.frame),r=t.useEditorStore(m=>m.card),o=t.useEditorStore(m=>m.selectedSchemaName),l=t.useEditorStore(m=>m.selectedDatamodelId),i=t.useEditorStore(m=>m.selectedDatabaseName),u=t.useEditorStore(m=>m.selectedTableName),c=t.useEditorStore(m=>m.selectedConnectionId),h=t.useEditorStore(m=>m.isShowingVisual),d=r.sql,x=t.useEditorStore(m=>m.onSave);function b(){let m=d;d&&!d.includes("{{")&&(m=jn(d),a(m));const g={...r,lastSelectedSchema:o,lastSelectedDatamodelId:l,lastSelectedDatabase:i,lastSelectedTable:u,connectionId:c,type:h?r.type:"table",sql:m,customCfg:h?r.customCfg:null},p=n.cards.map(f=>f.id===r.id?g:f),y={...n,cards:p,activeCardId:yn(p,n.activeCardId)};x==null||x(y)}return e.jsxs(t.Button,{onClick:b,size:"sm",variant:s,children:[e.jsx(t.Check,{className:"mr-2 size-4 font-bold"}),"Accept"]})}function It(){const{setTempQueryData:s,setSqlGen:a}=t.useEditorActions(),n=t.useQueryClient(),r=t.useEditorStore(c=>c.tempQueryData),o=t.useEditorStore(c=>c.onClose),l=t.useEditorStore(c=>c.card),{queryKey:i}=t.useDashboardCardQuery(l);function u(){if(r){const{queryKey:c,queryData:h}=r;c&&h&&n.setQueryData(c,h)}a({}),o==null||o()}return e.jsxs(t.Button,{onClick:u,size:"sm",variant:"secondary",children:[e.jsx(t.X,{className:"mr-2 size-4"}),"Close"]})}function Et(){const{setTempQueryData:s}=t.useEditorActions(),a=t.useQueryClient(),n=t.useEditorStore(m=>m.tempQueryData),r=t.useEditorStore(m=>m.card),{queryKey:o}=t.useDashboardCardQuery(r),{setCardSql:l,setSqlGen:i,setCardPython:u,setCardCustomCfg:c,setCardPreferences:h,setCardType:d,clearQueryConfig:x}=t.useEditorActions();function b(){const m=a.getQueryData(o);n!=null&&n.queryData||s({queryKey:o,queryData:m}),a.setQueryData(o,null),a.removeQueries({queryKey:o,exact:!0}),l(""),i({}),x(),u(""),c(""),h({})}return e.jsxs(t.Button,{onClick:b,className:"",size:"sm",variant:"secondary",children:[e.jsx(bs,{className:"mr-2 size-4"}),"Clear"]})}function yn(s,a){if(s.length===0)return"";if(a){const n=s.find(r=>r.id===a);if(n){const r=n.tabTitle;if(r=="All__Header")return s[0].id;if(r&&r.endsWith("__Header")){const l=r.split("__")[0],i=s.find(u=>u.tabTitle===l);return i?i.id:s[0].id}return a}}return s[0].id}function bn(){const s=t.useQueryClient(),[a,n]=T.useState(!1),[r,o]=T.useState(!1);t.useEditorStore(I=>I.frame);const l=t.useEditorStore(I=>I.card);t.useEditorStore(I=>I.isShowingVisual),t.useEditorStore(I=>I.selectedConnectionId),t.useEditorStore(I=>I.tempQueryData),t.useEditorStore(I=>I.selectedDatabaseName),t.useEditorStore(I=>I.selectedDatamodelId),t.useEditorStore(I=>I.selectedSchemaName);const i=t.useEditorStore(I=>I.showAIDialog);t.useEditorStore(I=>I.selectedTableName);const u=t.useEditorStore(I=>{var $;return($=I==null?void 0:I.card)==null?void 0:$.sql}),{sheetFilterValues:c}=t.useSheetFilterValues(),{setCardCustomCfg:h,setQueryResultColumns:d,setCardSql:x,setTempQueryData:b,setPythonStdOut:m,setCardPreferences:g,setCardType:p,setCardPython:y,runAssistant:f,setShowAIDialog:S}=t.useEditorActions();t.useEditorStore(I=>I.onSave),t.useEditorStore(I=>I.onClose),t.useEditorStore(I=>I.actions.setSqlGen);const C=t.useEditorStore(I=>I.runSql),A=t.useEditorStore(I=>I.actions.setRunSql),N=t.useEditorStore(I=>I.actions.setIsSqlRunning),{data:D,isLoading:F,isFetching:R,isSuccess:U,isError:w,error:_,queryKey:G,refetch:P,errorMessage:E}=t.useDashboardCardQuery(l),{handleSetAIScope:j}=kt();T.useEffect(()=>{if(D&&(D.rowLimitExceeded&&t.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),m((D==null?void 0:D.output)||""),D.records.length>0)){const{dimensionKeys:I}=t.getKeys(D.records);d(I)}},[D,d,m]),T.useEffect(()=>{(F||R)&&N(!0),(w||U)&&N(!1),w&&(d([]),m(""))},[F,R,D,m,w,U,N,d,_]),T.useEffect(()=>{C&&(s.getQueryData(G)||P(),A(!1))},[C,P,A,G,s]);async function M(){const I=`Can you help me debug this query in the current table scope?
|
|
362
362
|
|
|
363
363
|
Error:
|
|
364
364
|
|
|
@@ -366,7 +366,7 @@
|
|
|
366
366
|
|
|
367
367
|
Query:
|
|
368
368
|
|
|
369
|
-
${
|
|
369
|
+
${u}
|
|
370
370
|
|
|
371
|
-
`;p(I)}function R(){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(gn,{jsonString:JSON.stringify(c,null,2)})})}),c&&c.length>0&&e.jsx(t.Button,{variant:"ghost",onClick:()=>o(!r),className:"absolute bottom-0 right-6 size-7 p-1",children:r?e.jsx(t.X,{className:"size-3.5 text-muted-foreground"}):e.jsx(lt,{className:"size-3.5 text-muted-foreground"})})]})}function b(){return e.jsxs(t.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(t.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(pn,{})}),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(t.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(t.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(fn,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function L(){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(t.Button,{title:"Shift + Enter",disabled:O||V,onClick:()=>{q()},className:"shrink-0",size:"sm",children:[O||V?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(t.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),e.jsx(Ct,{sql:C==null?void 0:C.sql,error:E,onClick:A}),e.jsx(Et,{}),e.jsx(t.Toggle,{pressed:a,onPressedChange:I=>n(I),className:"",size:"sm",children:e.jsx(xn,{className:"size-4 text-foreground/70"})}),e.jsx("button",{disabled:i,onClick:()=>{j(),S(!0)},className:"h-9 w-full cursor-text px-3 text-left text-sm text-muted-foreground",children:i?"":"Type your question..."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(It,{}),e.jsx(Dt,{})]})]})}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:[b(),R()]}),L()]})}function vn({data:s}){var i;const[a,n]=T.useState([]),o=Object.keys(s[0]).map(m=>({accessorKey:m,header:({column:c})=>{const h=c.getIsSorted();return e.jsx("div",{className:"flex gap-2",children:e.jsxs("div",{className:"flex cursor-pointer items-center",onClick:()=>c.toggleSorting(c.getIsSorted()==="asc"),children:[m,h==="asc"?e.jsx(t.TriangleUpIcon,{className:"ml-2 h-5 w-5"}):h==="desc"?e.jsx(t.TriangleDownIcon,{className:"ml-2 h-5 w-5"}):null]})})}})),l=t.useReactTable({data:s,columns:o,getCoreRowModel:t.getCoreRowModel(),getPaginationRowModel:t.getPaginationRowModel(),onSortingChange:n,getSortedRowModel:t.getSortedRowModel(),manualPagination:!0,state:{sorting:a}});return e.jsxs(t.CustomScrollArea,{className:"grow overflow-auto font-mono text-xs text-foreground/95",children:[e.jsxs("table",{className:"w-full",children:[e.jsx(t.TableHeader,{className:"sticky top-0 z-50 h-0 bg-muted",children:l.getHeaderGroups().map(m=>e.jsx(t.TableRow,{className:"",children:m.headers.map(c=>e.jsx(t.TableHead,{className:"h-11 font-semibold text-foreground",children:c.isPlaceholder?null:t.flexRender(c.column.columnDef.header,c.getContext())},c.id))},m.id))}),e.jsx(t.TableBody,{children:(i=l.getRowModel().rows)!=null&&i.length?l.getRowModel().rows.map(m=>e.jsx(t.TableRow,{className:"h-11 whitespace-nowrap py-2","data-state":m.getIsSelected()&&"selected",children:m.getVisibleCells().map(c=>e.jsx(t.TableCell,{className:"py-2",children:t.flexRender(c.column.columnDef.cell,c.getContext())},c.id))},m.id)):e.jsx(t.TableRow,{children:e.jsx(t.TableCell,{colSpan:o.length,className:"h-24 text-center",children:"No results."})})})]}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})}function Cn({card:s}){var S,N,M,w,C,O,V,U,k,z,G;const[a,n]=T.useState(!1),[r,o]=T.useState([]),{authToken:l}=t.useSemaphorContext(),i=t.getTokenParams(l==null?void 0:l.accessToken),m=t.useDashboardStore(q=>q.themeStyle),c={colors:((N=(S=m==null?void 0:m.chart)==null?void 0:S.dataset)==null?void 0:N.backgroundColor)||[]};(M=s.customCardPreferences)!=null&&M.inputData;const{getCard:h}=t.useCustomVisual(((w=s.customCardPreferences)==null?void 0:w.url)||""),u=((C=s.customCardPreferences)==null?void 0:C.visualType)||"single",x=t.useEditorStore(q=>q.frame),{setCustomCardPreferences:v}=t.useEditorActions(),{data:d}=t.useDashboardCardQuery(s),g=T.useCallback((q,E)=>{o(j=>({...j,[q]:E}))},[]);if(!((O=s.customCardPreferences)!=null&&O.componentName))return null;const f=h((V=s.customCardPreferences)==null?void 0:V.componentName);function y(q){n(q)}const p=Object.keys(((U=s.customCardPreferences)==null?void 0:U.dataInputCardIds)||{}).map(q=>{var A,R,b;const E=(R=(A=s.customCardPreferences)==null?void 0:A.dataInputCardIds)==null?void 0:R[q].cardId,j=(b=x==null?void 0:x.cards)==null?void 0:b.find(L=>L.id===E);return j||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(T.Suspense,{fallback:e.jsx(e.Fragment,{}),children:[f&&u==="single"&&e.jsx(f,{params:i,theme:c,settings:(k=s.customCardPreferences)==null?void 0:k.settings,onDataChange:g,data:(d==null?void 0:d.records)||[]}),f&&u==="multiple"&&e.jsxs(e.Fragment,{children:[Object.keys(((z=s.customCardPreferences)==null?void 0:z.dataInputCardIds)||{}).map(q=>{var A,R,b;const E=(R=(A=s.customCardPreferences)==null?void 0:A.dataInputCardIds)==null?void 0:R[q].cardId,j=(b=x==null?void 0:x.cards)==null?void 0:b.find(L=>L.id===E);if(j)return e.jsx(t.MultiInputEmptyRenderer,{onLoadingStatus:y,onDataChange:g,dataInputIndex:q,card:j},j.id+q)}),e.jsx("div",{className:"flex flex-col gap-2",children:e.jsx(f,{theme:c,onDataChange:g,settings:p.map(q=>{var E;return(E=q==null?void 0:q.customCardPreferences)==null?void 0:E.settings}),data:p.map(q=>q!=null&&q.id?r==null?void 0:r[q.id]:[])})})]})]},(G=s.customCardPreferences)==null?void 0:G.componentName)})}function Tt({className:s,...a}){var y;const n=t.useEditorStore(p=>p.card),r=((y=n.customCardPreferences)==null?void 0:y.showCardHeader)??!0,o=t.useEditorStore(p=>p.frame),l=t.useDashboardStore(p=>p.dashboard.filters);t.useEditorStore(p=>p.actions.setRunSql);const{updateFilter:i}=t.useDashboardActions(),m=l==null?void 0:l.find(p=>p.id===o.filterId),{data:c,isLoading:h,isFetching:u}=t.useDashboardCardQuery(n),{setCard:x}=t.useEditorActions(),{data:v}=t.useTopoJson(n),d=T.useMemo(()=>!n.id||!(c!=null&&c.records)?null:t.createChartConfig({data:c==null?void 0:c.records,cardType:n.type,queryConfig:n.queryConfig,customCfg:n.customCfg,preferences:n.preferences,topoJson:v}),[n.id,c==null?void 0:c.records,n.type,n.queryConfig,n.customCfg,n.preferences,v]);function g(p){return e.jsx(Cn,{card:p})}function f(){if(c!=null&&c.records&&(c==null?void 0:c.records.length)===0&&!h&&!u)return e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(t.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No records to display"})]});if(!n.id)return null;if(n.type==="custom")return g(n);if(!["table","custom"].includes(n.type)&&d)return e.jsx(e.Fragment,{children:h||u?e.jsx(e.Fragment,{}):e.jsx(t.ChartJsVisual,{cfg:d})});if(n.type==="table"&&(c!=null&&c.records)&&(c==null?void 0:c.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(t.TableVisual,{card:n,data:c==null?void 0:c.records})})}return e.jsxs(t.Card,{role:"editor-visual-card",className:t.cn("relative flex h-full flex-col rounded-none",s),...a,children:[r&&e.jsxs(t.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{children:[e.jsx(t.Editable,{enabled:!0,text:n.title,onSave:p=>x({...n,title:p}),children:e.jsx("div",{className:"flex items-center gap-5",children:e.jsx(t.CardTitle,{className:"text-base",children:n.title||"Add title"})})},n.title),e.jsx(t.Editable,{enabled:!0,text:n.description||"",onSave:p=>x({...n,description:p}),children:e.jsx(t.CardDescription,{children:n.description||"Add description"})},n.description)]}),m&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(t.Editable,{enabled:!0,className:"my-1",text:m.title||m.column,onSave:p=>i({...m,title:p}),children:e.jsx(t.Label,{children:m.title})}),e.jsx(t.FilterComponent,{filter:m})]})]}),e.jsx(t.CardContent,{className:"flex grow flex-col",children:f()})]})}const Te=({language:s,value:a,onChange:n,readonly:r})=>e.jsx(t.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:s,value:a,onChange:n}),Sn=()=>e.jsxs(t.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(t.DialogTitle,{children:"Customize Card"})}),e.jsxs(t.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."})]})]}),Nn=({customCode:s,cardCustomCfgString:a,mergedConfig:n,onConfigChange:r,onCodeChange:o})=>e.jsxs(t.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(t.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(t.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(t.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(t.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(t.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{language:"json",value:n,onChange:r})}),e.jsx(t.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{readonly:!0,language:"json",value:a,onChange:()=>{}})}),e.jsx(t.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{language:"javascript",value:s,onChange:o})})]}),wn=({error:s,hasCardCustomConfig:a,onReset:n,onApply:r,onClose:o})=>e.jsxs(t.DialogFooter,{className:"flex items-center",children:[e.jsx(t.Label,{className:"mr-2 text-red-500",children:s}),e.jsx(t.Button,{disabled:!a,onClick:n,variant:"outline",children:"Reset"}),e.jsx(t.Button,{onClick:r,variant:"outline",children:"Apply"}),e.jsx(t.Button,{onClick:o,variant:"outline",children:"Close"})]});function kn(){const[s,a]=T.useState(!1),[n,r]=T.useState(""),[o,l]=T.useState(""),[i,m]=T.useState(""),[c,h]=T.useState(""),u=t.useEditorStore(w=>w.card),x=t.useEditorStore(w=>w.card.customCfg),v=x?JSON.stringify(x,null,2):"",{data:d}=t.useDashboardCardQuery(u),{data:g}=t.useTopoJson(u),{setCardCustomCfg:f,setCustomVisualCode:y}=t.useEditorActions(),p=w=>{var C,O;return JSON.stringify({...w,data:{...w.data,datasets:(O=(C=w.data)==null?void 0:C.datasets)==null?void 0:O.map(V=>({...V,data:[]}))}},null,2)},S=()=>{var C,O;if(!((C=d==null?void 0:d.records)!=null&&C.length))return;const w=t.createChartConfig({data:d.records,cardType:u.type,queryConfig:u.queryConfig,preferences:u.preferences,topoJson:g});if(w){r(p(w)),m(((O=u.preferences)==null?void 0:O.customVisualCode)||"");const V=t.merge(w,x);l(p(V))}},N=()=>{try{if(i&&y(i),o){const w=JSON.parse(o),C=t.getObjectDiff(JSON.parse(n),w);f(C),h("")}}catch{h("Invalid JSON configuration")}},M=Object.keys(x||{}).length===0;return e.jsxs(t.Dialog,{open:s,onOpenChange:a,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Customize Visual",onClick:S,className:M?"":"text-green-600 hover:text-green-700",children:e.jsx(t.Settings,{className:"h-4 w-4"})})}),e.jsxs(t.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(Sn,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(t.ErrorBoundary,{FallbackComponent:t.FallbackEditorCard,children:e.jsx(Tt,{className:"w-1/2 rounded shadow-none"})}),e.jsx(Nn,{customCode:i,cardCustomCfgString:v,mergedConfig:o,onConfigChange:w=>l(w??""),onCodeChange:w=>m(w??"")})]}),e.jsx(wn,{error:c,hasCardCustomConfig:!!x,onReset:()=>{l(n),f(null)},onApply:N,onClose:()=>a(!1)})]})]})}function Mt(s){const[a,n]=T.useState(null);return a}function At(){const{authToken:s}=t.useSemaphorContext(),{data:a,isLoading:n,isFetching:r,isError:o}=t.useQuery({queryKey:["plugins",s==null?void 0:s.accessToken],queryFn:()=>t.getPlugins(s==null?void 0:s.accessToken)});return{data:a,isLoading:n,isFetching:r,isError:o}}function Dn({onChartTypeChange:s}){const[a,n]=T.useState([]),{setCardCustomCfg:r,setCardPreferences:o,setCardType:l,setCustomCardPreferences:i}=t.useEditorActions(),{data:m}=At();function c(h,u){r(null),o({}),l(h),u&&i({type:"component",visualType:"single",url:u.url,componentName:u.name,icon:u.icon}),s==null||s()}return T.useEffect(()=>{(async()=>{var u;if(m){const v=(await Promise.all((u=m==null?void 0:m.plugins)==null?void 0:u.map(async d=>(await t.loadManifest(d.value)).visuals.map(y=>({name:y.name,icon:y.icon,url:d.value}))))).flat();n(v)}})()},[m]),a.length===0?null:e.jsxs(e.Fragment,{children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"grid grid-cols-4 place-items-center",children:a.map((h,u)=>e.jsx(In,{text:h.name,onClick:()=>c("custom",h),icon:h.icon},h.url+h.name+u))})]})}const In=({text:s,icon:a,onClick:n})=>{const r=Mt(),o=s.charAt(0).toUpperCase();return e.jsx(t.IconButton,{onClick:n,tooltip:s,children:r?e.jsx(r,{className:"h-4 w-4"}):o})};function Lt(s){return re({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:[]}]})(s)}function En(s){return re({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:[]}]})(s)}function Ot(s){return re({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:[]}]})(s)}function zt(s){return re({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:[]}]})(s)}function Ft(s){return re({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:[]}]})(s)}function Rt(s){return re({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:[]}]})(s)}function Bt({onChartTypeChange:s,iconClassName:a}){const{setCardType:n,setCardPreferences:r,setIsDevMode:o}=t.useEditorStore(u=>u.actions),{setCardCustomCfg:l,setCustomCardPreferences:i,setQueryConfig:m}=t.useEditorActions();function c(u,x){l(null),r({}),n(u),["kpi","custom","text","map"].includes(u)&&(o(!0),m(void 0)),s==null||s()}const h=1e3;return e.jsxs("div",{className:"grid grid-cols-4 place-items-center",children:[e.jsx(t.IconButton,{openDelay:h,tooltip:"Table",onClick:()=>c("table"),className:"h-10 p-4",children:e.jsx(Ve,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Bar Chart",onClick:()=>c("bar"),className:"h-10 p-4",children:e.jsx(Re,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Line Chart",onClick:()=>c("line"),className:"h-10 p-4",children:e.jsx(Be,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Stacked Bar Chart",onClick:()=>c("stackedBar"),className:"h-10 p-4",children:e.jsx(et,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Stacked Line Chart",onClick:()=>c("stackedLine"),className:"h-10 p-4",children:e.jsx(Ft,{className:t.cn("size-5",a),strokeWidth:0})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Scatter Chart",onClick:()=>c("scatter"),className:"h-10 p-4",children:e.jsx(nt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Bubble Chart",onClick:()=>c("bubble"),className:"h-10 p-4",children:e.jsx(Ot,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Pie Chart",onClick:()=>c("pie"),className:"h-10 p-4",children:e.jsx(st,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Doughnut Chart",onClick:()=>c("doughnut"),className:"h-10 p-4",children:e.jsx(rt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Funnel Chart",onClick:()=>c("funnel"),className:"h-10 p-4",children:e.jsx(zt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Radar Chart",onClick:()=>c("radar"),className:"h-10 p-4",children:e.jsx(ut,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Polar Area Chart",onClick:()=>c("polarArea"),className:"h-10 p-4",children:e.jsx(Lt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Tornado Chart",onClick:()=>c("tornado"),className:"h-10 p-4",children:e.jsx(pt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{tooltip:"Range Chart",onClick:()=>c("range"),className:"h-10 p-4",children:e.jsx(tt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"KPI",onClick:()=>c("kpi"),className:"h-10 p-4",children:e.jsx(Rt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Text",onClick:()=>c("text"),className:"h-10 p-4",children:e.jsx(cs,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{tooltip:"Map",onClick:()=>c("map"),className:"h-10 p-4",children:e.jsx(dt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Custom",onClick:()=>c("custom"),className:"h-10 p-4",children:e.jsx(ht,{className:t.cn("size-5",a)})})]})}function Tn({chartType:s,className:a,customIcon:n,componentName:r}){const o=Mt();switch(s){case"bar":return e.jsx(Re,{className:a});case"line":return e.jsx(Be,{className:a});case"pie":return e.jsx(st,{className:a});case"radar":return e.jsx(ut,{className:a});case"scatter":return e.jsx(nt,{className:a});case"bubble":return e.jsx(Ot,{className:a});case"doughnut":return e.jsx(rt,{className:a});case"stackedBar":return e.jsx(et,{className:a});case"stackedLine":return e.jsx(Ft,{className:a});case"polarArea":return e.jsx(Lt,{className:a});case"funnel":return e.jsx(zt,{className:a});case"tornado":case"pyramid":return e.jsx(pt,{className:a});case"range":return e.jsx(tt,{className:a});case"kpi":return e.jsx(Rt,{className:a});case"text":return e.jsx(Us,{className:a});case"map":return e.jsx(dt,{className:a});case"custom":return n&&o?e.jsx(o,{className:a}):r?r==null?void 0:r.charAt(0).toUpperCase():e.jsx(ht,{className:a});default:return e.jsx(Ve,{className:a})}}function Mn(){var l,i;const[s,a]=T.useState(!1),n=t.useEditorStore(m=>m.card),r=n.type,o=t.useDashboardStore(m=>m.isVisualEditing);return e.jsxs(t.Popover,{open:s,onOpenChange:a,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.Button,{size:"sm",variant:o?"ghost":"outline",children:e.jsx(Tn,{className:"size-4",chartType:r,customIcon:(l=n.customCardPreferences)==null?void 0:l.icon,componentName:(i=n.customCardPreferences)==null?void 0:i.componentName})})}),e.jsxs(t.PopoverContentCustom,{onOpenAutoFocus:m=>m.preventDefault(),className:"w-50",children:[e.jsx(Bt,{onChartTypeChange:()=>a(!1)}),e.jsx(Dn,{onChartTypeChange:()=>a(!1)})]})]})}const ge="focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0";function Ze(s,a){var r,o,l;const n=t.getNumberAxis(s,{});return["pie","doughnut","polarArea","funnel"].includes(a)?"category":((l=(o=(r=s==null?void 0:s.chartOptions)==null?void 0:r.scales)==null?void 0:o[n])==null?void 0:l.type)||"linear"}function An(){var A,R,b,L,I,$;const[s,a]=T.useState(!1),n=t.useEditorStore(_=>{var F;return(F=_.card)==null?void 0:F.preferences}),r=t.useEditorStore(_=>{var F;return(F=_.card)==null?void 0:F.type}),o=t.getNumberAxis(n,{}),[l,i]=T.useState(Ze(n,r)),[m,c]=T.useState(((b=(R=(A=n==null?void 0:n.chartOptions)==null?void 0:A.scales)==null?void 0:R[o])==null?void 0:b.min)||""),[h,u]=T.useState((($=(I=(L=n==null?void 0:n.chartOptions)==null?void 0:L.scales)==null?void 0:I[o])==null?void 0:$.max)||""),[x,v]=T.useState(m||h?"custom":"auto"),[d,g]=T.useState(""),[f,y]=T.useState(0),[p,S]=T.useState(""),[N,M]=T.useState(""),[w,C]=T.useState(""),O=t.useEditorStore(_=>_.actions.setCardPreferences),{updateCardInFrame:V}=t.useEditorActions(),U=t.useEditorStore(_=>_.card),k=["pie","doughnut","polarArea","funnel"].includes(r);T.useEffect(()=>{var _,F,P,J,B,Q,D,H,K,X,he,xe,pe,fe;i(Ze(n,r)),c(((P=(F=(_=n==null?void 0:n.chartOptions)==null?void 0:_.scales)==null?void 0:F[o])==null?void 0:P.min)===0?0:""),u(((Q=(B=(J=n==null?void 0:n.chartOptions)==null?void 0:J.scales)==null?void 0:B[o])==null?void 0:Q.max)||""),g(((X=(K=(H=(D=n==null?void 0:n.chartOptions)==null?void 0:D.scales)==null?void 0:H[o])==null?void 0:K.ticks)==null?void 0:X.stepSize)||""),y(((he=n==null?void 0:n.numberAxisFormat)==null?void 0:he.decimalPlaces)||0),S(((xe=n==null?void 0:n.numberAxisFormat)==null?void 0:xe.suffix)||""),M(((pe=n==null?void 0:n.numberAxisFormat)==null?void 0:pe.currency)||""),C(((fe=n==null?void 0:n.numberAxisFormat)==null?void 0:fe.locale)||"")},[s,n,o,r]);function z(_,F){return F===""||/^[0-9\b]+$/.test(F)?Number(F):Number(_)}function G(){const _=[...new Set(t.LOCALE_CURRENCY_PAIRS.map(F=>F.currency))];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-6 items-center gap-2",children:[e.jsx(t.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(t.Select,{value:N||"none",onValueChange:F=>M(F==="none"?"":F),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Currency"}),e.jsx(t.SelectItem,{value:"none",children:"None"}),_.map(F=>e.jsx(t.SelectItem,{value:F,children:F},F))]})})]}),e.jsxs(t.Select,{value:w,onValueChange:F=>C(F),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Locale"}),t.LOCALE_CURRENCY_PAIRS.map(F=>e.jsx(t.SelectItem,{value:F.locale,children:F.locale},F.locale))]})})]})]}),e.jsxs("div",{className:"grid grid-cols-9 items-center gap-2",children:[e.jsx(t.Label,{className:"col-span-3",htmlFor:"format",children:"Decimals"}),e.jsx(t.Input,{value:f,onChange:F=>y(Number(F.target.value)),id:"format",className:`col-span-6 h-8 w-full tracking-wide ${ge}`})]})]})}function q(){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(t.Label,{htmlFor:"scale",children:"Type"}),e.jsxs(t.Select,{disabled:k,value:l,onValueChange:_=>i(_),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{id:"type",children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Option"}),e.jsx(t.SelectItem,{value:"linear",children:"Linear"}),e.jsx(t.SelectItem,{value:"logarithmic",children:"Logarithmic"}),e.jsx(t.SelectItem,{value:"category",children:"Category"})]})})]})]}),l=="linear"&&e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2 py-1",children:[e.jsx(t.Label,{htmlFor:"scale-option",children:"Setup"}),e.jsxs(t.RadioGroup,{disabled:k,id:"scale-option",value:x,onValueChange:_=>v(_),className:"col-span-2 flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.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(t.RadioGroupItem,{value:"custom",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Custom"})]})]})]}),x==="custom"&&l=="linear"&&E()]})}function E(){return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(t.Label,{htmlFor:"range",children:"Range"}),e.jsx(t.Input,{id:"min",value:m,onChange:_=>c(F=>z(F,_.target.value)),className:`col-span-1 h-8 w-full ${ge}`}),e.jsx(t.Input,{value:h,onChange:_=>u(F=>z(F,_.target.value)),id:"max",className:`col-span-1 h-8 w-full ${ge}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(t.Label,{htmlFor:"step-size",children:"Step Size"}),e.jsx(t.Input,{value:d,onChange:_=>g(F=>z(F,_.target.value)),id:"step-size",className:`col-span-1 h-8 w-full ${ge}`})]})]})}function j(){var J;let _={};x==="custom"&&(_={min:m,max:h,ticks:{stepSize:d}});const F={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,scales:{...(J=n==null?void 0:n.chartOptions)==null?void 0:J.scales,[o]:{type:l,...l==="linear"?_:{}}}},numberAxisFormat:{decimalPlaces:f,suffix:p,currency:N,locale:w}};console.log(F),O(F);const P={...U,preferences:F};V(P)}return e.jsxs(t.Popover,{onOpenChange:_=>a(_),children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Format Number",children:e.jsx(ys,{className:"size-4"})})}),e.jsx(t.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:[G(),q(),e.jsx(t.SelectSeparator,{}),e.jsx("div",{className:"grid grid-cols-3",children:e.jsx(t.Button,{onClick:j,variant:"secondary",className:"col-span-1 col-start-3 h-8 w-full",children:"Apply"})})]})]})})]})}function Ln(){const s=t.useEditorStore(m=>{var c,h,u,x;return(x=(u=(h=(c=m.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:u.legend)==null?void 0:x.position}),a=t.useEditorStore(m=>{var c,h,u,x;return(x=(u=(h=(c=m.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:u.legend)==null?void 0:x.align}),n=t.useEditorStore(m=>{var c,h,u,x;return(x=(u=(h=(c=m.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:u.legend)==null?void 0:x.display}),{setLegendOptions:r}=t.useEditorActions(),o=m=>{r({display:m,position:s,align:a})},l=m=>{r({display:n,position:m,align:a})},i=m=>{r({display:n,position:s,align:m})};return e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Legend Settings",children:e.jsx(Es,{className:"size-4"})})}),e.jsx(t.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(t.Label,{htmlFor:"displayLegend",children:"Display"}),e.jsx("div",{className:"col-span-2 flex h-8 items-center",children:e.jsx(t.SmallSwitch,{checked:n,onCheckedChange:o,id:"displayLegend"})})]}),n&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"position",children:"Position"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(t.IconButton,{tooltip:"Top",onClick:()=>l("top"),children:e.jsx(t.ArrowUp,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Left",onClick:()=>l("left"),children:e.jsx(ns,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Bottom",onClick:()=>l("bottom"),children:e.jsx(Xe,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Right",onClick:()=>l("right"),children:e.jsx(as,{className:"size-4"})})]})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"align",children:"Align"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(t.IconButton,{tooltip:"Start",onClick:()=>i("start"),children:e.jsx(Zt,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Center",onClick:()=>i("center"),children:e.jsx(Yt,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"End",onClick:()=>i("end"),children:e.jsx(Xt,{className:"size-4"})})]})]})]})]})]})})]})}function On(){const s=t.useEditorStore(l=>{var i,m;return(m=(i=l.card.preferences)==null?void 0:i.chartOptions)==null?void 0:m.indexAxis}),a=t.useEditorStore(l=>l.card.type),{setChartOrientation:n}=t.useEditorStore(l=>l.actions);function r(){return e.jsx(t.IconButton,{onClick:()=>{n(s==="x"||s===void 0?"y":"x")},tooltip:"Chart Orientation",children:s==="x"||s===void 0?e.jsx(Qe,{className:"size-4"}):e.jsx($e,{className:"size-4"})})}function o(){return e.jsx(t.IconButton,{onClick:()=>{n(s==="y"||s===void 0?"x":"y")},tooltip:"Chart Orientation",children:s==="y"||s===void 0?e.jsx(Qe,{className:"size-4"}):e.jsx($e,{className:"size-4"})})}return e.jsx(e.Fragment,{children:a==="funnel"?o():r()})}function zn(){const s=t.useEditorStore(i=>i.card),a=t.useEditorStore(i=>i.isShowingVisual),n=t.useEditorStore(i=>{var m,c;return(c=(m=i.card)==null?void 0:m.preferences)==null?void 0:c.chartOptions}),r=t.getChartGridStyle(n),{setChartGrid:o}=t.useEditorActions();function l(){const i=["xy","none","x","y"],c=(i.indexOf(r)+1)%i.length;o(i[c])}return a?e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel"].includes(s.type)&&e.jsx(On,{}),!["kpi","text","custom","table"].includes(s.type)&&e.jsxs(e.Fragment,{children:[e.jsx(An,{}),e.jsx(Ln,{})]}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(s.type)&&e.jsxs(t.IconButton,{tooltip:"Grid Style",onClick:l,children:[(r==="xy"||!r)&&e.jsx(ks,{className:"size-4"}),r==="x"&&e.jsx(ps,{className:"size-4"}),r==="y"&&e.jsx(Bs,{className:"size-4"}),r==="none"&&e.jsx(qs,{className:"size-4"})]})]}):null}function je(){var R;const s=t.useEditorStore(b=>b.frame),a=t.useEditorStore(b=>b.card),n=t.useEditorStore(b=>b.isDevMode),r=t.useEditorStore(b=>b.isShowingVisual),o=t.useDashboardStore(b=>b.isVisualEditing),l=t.useEditorStore(b=>b.isSqlRunning),i=t.useEditorStore(b=>b.pythonStdOut),m=t.useEditorStore(b=>{var L;return(L=b.card.preferences)==null?void 0:L.chartOptions});t.useEditorStore(b=>b.actions.setRunSql),t.getChartGridStyle(m);const{setFrame:c,setCard:h,updateCardInFrame:u,setActiveTabCardId:x,runAssistant:v,setChartGrid:d}=t.useEditorActions(),{data:g,isLoading:f,isFetching:y,error:p,isPending:S}=t.useDashboardCardQuery(a);if(!a)return e.jsx(e.Fragment,{children:"No Active Card"});function N(b){const L={connectionId:b.connectionId,lastSelectedDatabase:b.lastSelectedDatabase,lastSelectedSchema:b.lastSelectedSchema,lastSelectedTable:b.lastSelectedTable,lastSelectedDatamodelId:b.lastSelectedDatamodelId,id:t.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]};M(b,L)}function M(b,L){const I={...s,cards:[...s.cards,L],activeCardId:b.id};c(I),x(L.id)}function w(){const b={...a,id:t.v4(),title:`${a.title} Copy`,tabTitle:`${a.tabTitle||a.title} Copy`};M(a,b)}function C(b){const L=s.cards.filter(_=>_.id!==b),I=L[0],$={...s,cards:L,activeCardId:I.id};c($),x(I.id),h(I)}function O(b){x(b)}const V=(b,L)=>{console.log("handleDragStart",L),b.dataTransfer.setData("tabIndex",L.toString())},U=b=>{b.preventDefault()},k=(b,L)=>{const I=b.dataTransfer.getData("tabIndex");if(I===L.toString())return;const $=Array.from(s.cards),[_]=$.splice(parseInt(I),1);$.splice(L,0,_),c({...s,cards:$})};function z(b,L){return b.cards.length===1&&(L.displayTab===!0||L.displayTab===void 0)||b.activeCardId!==L.id&&(L.displayTab===!0||L.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const I=L.displayTab===void 0?!1:!L.displayTab;h({...L,displayTab:I}),u({...L,displayTab:I})},children:L.displayTab===!0||L.displayTab===void 0?e.jsx(t.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(vs,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}function G(){return e.jsxs(t.Tabs,{onValueChange:O,value:s.activeCardId,className:"flex grow flex-col",children:[s.cards.map(b=>e.jsx(t.TabsContent,{className:"mt-0 grow",value:b.id,children:q()},b.id)),e.jsx(t.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[s.cards.map((b,L)=>e.jsxs(t.TabsTrigger,{draggable:!0,onDragStart:I=>V(I,L),onDragOver:U,onDrop:I=>k(I,L),className:"",value:b.id,children:[z(s,b),e.jsx(t.Editable,{enabled:s.activeCardId===b.id,text:b.tabTitle||b.title,onSave:I=>{h({...a,tabTitle:I}),u({...a,tabTitle:I})},children:b.tabTitle||b.title},b.id),s.activeCardId===b.id&&s.cards.length>1&&e.jsx(t.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>C(b.id)})]},b.id)),e.jsx(t.Button,{className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",variant:"outline",onClick:()=>N(a),children:e.jsx(t.Plus,{className:"h-4 w-4"})}),e.jsx(t.Button,{className:"invisible ml-1 h-8 px-2 hover:bg-muted-foreground/50 group-hover:visible",variant:"outline",onClick:w,children:e.jsx(gs,{className:"h-4 w-4"})})]})})]})}function q(){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(t.KPICard,{card:a,data:g==null?void 0:g.records,isPending:S})})}):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(t.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(t.TextCard,{isLoading:l,card:a,data:g==null?void 0:g.records})})})}):e.jsx(t.ErrorBoundary,{FallbackComponent:t.FallbackEditorCard,children:e.jsx(Tt,{className:"grow border-none shadow-none"})})}function E(){const b=JSON.stringify(g==null?void 0:g.records,null,2),L=new Blob([b],{type:"application/json"}),I=URL.createObjectURL(L),$=document.createElement("a");$.href=I,$.download=`${a.title}.json`,$.click()}function j(){const L=[Object.keys(g==null?void 0:g.records.reduce((F,P)=>({...F,...P}),{})).join(","),...((g==null?void 0:g.records)||[]).map(F=>Object.values(F).join(","))].join(`
|
|
372
|
-
`),I=new Blob([L],{type:"text/csv"}),$=URL.createObjectURL(I),_=document.createElement("a");_.href=$,_.download=`${a.title}.csv`,_.click()}function A(){const b=`Can you get the documentation for ${a.type} chart?`;v(b)}return e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[(f||y)&&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(ae,{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(Mn,{}),o&&e.jsx(t.IconButton,{tooltip:"Visual Docs",onClick:A,children:e.jsx(ls,{className:"size-4"})}),!["table","custom"].includes(a.type)&&o&&e.jsx(kn,{})]}),!r&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(t.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:j,children:e.jsx(En,{className:"size-4"})}),e.jsx(t.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:E,children:e.jsx(Ss,{strokeWidth:1.5,className:"size-4"})}),e.jsx(t.CopyButton,{text:JSON.stringify(g==null?void 0:g.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsx(zn,{}),n&&e.jsx(Rn,{})]}),r?G():e.jsxs(t.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[i&&e.jsxs(e.Fragment,{children:[e.jsx(t.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:i&&e.jsx(Fn,{})}),e.jsx(t.ResizableHandle,{})]}),e.jsxs(t.ResizablePanel,{id:"2",order:2,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:[(g==null?void 0:g.records)&&(g==null?void 0:g.records.length)>0&&e.jsx(vn,{data:(g==null?void 0:g.records)||[]}),(p||(g==null?void 0:g.error))&&e.jsx("span",{className:"font-mono text-xs",children:(p==null?void 0:p.message)||((R=g==null?void 0:g.error)==null?void 0:R.message)})]})]})]})}function Fn(){const s=t.useEditorStore(n=>n.pythonStdOut);if(!s)return null;const a=s.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(t.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(t.ScrollBar,{orientation:"horizontal"})]})]})}function Rn(){const s=t.useEditorStore(n=>n.isShowingVisual),a=t.useEditorStore(n=>n.actions.setIsShowingVisual);return e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.Label,{htmlFor:"visualize",children:"Chart"}),e.jsx(t.SmallSwitch,{checked:s,onCheckedChange:()=>a(!s),id:"visualize",className:"data-[state=checked]:bg-foreground/50"})]})}function Bn(){var f,y;const s=t.useEditorStore(p=>p.card.customCardPreferences),{data:a}=At(),n=(a==null?void 0:a.plugins)||[],r=t.useEditorStore(p=>p.card),o=t.useEditorStore(p=>p.frame),l=(s==null?void 0:s.visualType)||"single",{setCustomCardPreferences:i}=t.useEditorActions(),{components:m,manifest:c}=t.useCustomVisual((s==null?void 0:s.url)||""),h=(f=c==null?void 0:c.visuals)==null?void 0:f.find(p=>p.name===(s==null?void 0:s.componentName));function u(p,S){if(!s)return;const M={...(s==null?void 0:s.dataInputCardIds)||{},[p]:{cardId:S,hookRef:null}};i({...s,dataInputCardIds:M})}function x(p){var N,M;const S=(N=o==null?void 0:o.cards)==null?void 0:N.filter(w=>{var C;return((C=w==null?void 0:w.customCardPreferences)==null?void 0:C.visualType)!=="multiple"}).map(w=>({value:w.id,label:w.tabTitle||w.title}));return e.jsx("div",{className:"space-y-2",children:(M=p==null?void 0:p.dataInputs)==null?void 0:M.map((w,C)=>{var O,V;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:w}),e.jsx(Y,{className:"col-span-6 h-8 w-full justify-between",label:"",options:S||[],value:((V=(O=s==null?void 0:s.dataInputCardIds)==null?void 0:O[C])==null?void 0:V.cardId)||"",onValueChange:U=>u(C,U)})]},C+Math.random())})})}function v(p,S,N=[],M=""){var w,C;return s?p==="input"?e.jsx(t.Input,{className:`h-8 w-full ${t.removeRing}`,value:((w=s==null?void 0:s.settings)==null?void 0:w[S])||M,onChange:O=>{i({...s,settings:{...s==null?void 0:s.settings,[S]:O.target.value}})}},S):p==="select"?e.jsx(Y,{className:"h-8 w-full",label:"",options:N,value:((C=s==null?void 0:s.settings)==null?void 0:C[S])||M,onValueChange:O=>{i({...s,settings:{...s==null?void 0:s.settings,[S]:O}})}}):null:null}function d(p){const S=Object.entries((p==null?void 0:p.settings)||{});return e.jsx("div",{className:"space-y-3",children:S.map(([N,M])=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:M.title}),v(M.ui,N,M.options,M.defaultValue)]},N))})}function g(p){s&&i({...s,visualType:p,componentName:""})}return e.jsxs(t.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"px-6 py-2",children:[e.jsxs(t.AccordionItem,{value:"plugin-settings",children:[e.jsx(t.AccordionTrigger,{children:"Plugin"}),e.jsx(t.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(Y,{icon:e.jsx(is,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:n,value:(s==null?void 0:s.url)||"",onValueChange:p=>i({...s||{url:"",componentName:""},url:p})}),((s==null?void 0:s.type)==="component"||(s==null?void 0:s.type)===void 0)&&e.jsx(t.Tabs,{value:l,onValueChange:g,children:e.jsxs(t.TabsList,{className:"h-9 w-full",children:[e.jsx(t.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(t.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((s==null?void 0:s.type)==="component"||(s==null?void 0:s.type)===void 0)&&s&&e.jsx(e.Fragment,{children:m&&e.jsx(Y,{icon:e.jsx(fs,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(m==null?void 0:m.filter(p=>p.type===l).map(p=>({value:p.name,label:p.name})))||[],value:s.componentName,onValueChange:p=>{console.log("changing value",p),i({...s,componentName:p})}})})]})})]}),((y=r==null?void 0:r.customCardPreferences)==null?void 0:y.componentName)&&e.jsxs(t.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{children:[h&&h.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(t.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:x(h)})]}),h&&h.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:d(h)})})]})]})]})}function oe(){const[s,a]=T.useState([]),n=t.useEditorStore(h=>h.card.preferences),{setFilterOnClickColumnIndex:r,setCardPreferences:o}=t.useEditorStore(h=>h.actions),l=t.useEditorStore(h=>h.queryResultColumns);t.useEditorStore(h=>{var u;return(u=h.card.preferences)==null?void 0:u.filterOnClickColumnIndex}),T.useEffect(()=>{l&&a(new Array(l.length).fill(!1))},[l]);function i(h,u){var x,v;u?n!=null&&n.onClickFilter&&((x=n==null?void 0:n.onClickFilter)==null?void 0:x.length)>0?o({...n,onClickFilter:[...n.onClickFilter,{columnIndex:h,expression:""}]}):o({...n,onClickFilter:[{columnIndex:h,expression:""}]}):(o({...n,onClickFilter:(v=n==null?void 0:n.onClickFilter)==null?void 0:v.filter(d=>d.columnIndex!==h)}),a(d=>{const g=[...d];return g[h]=!1,g}))}function m(h,u){var v;const x=(v=n==null?void 0:n.onClickFilter)==null?void 0:v.map(d=>d.columnIndex===h?{...d,expression:u.target.value}:d);o({...n,onClickFilter:x})}function c(h){var u,x,v,d;if(s!=null&&s[h]||(x=(u=n==null?void 0:n.onClickFilter)==null?void 0:u.find(g=>g.columnIndex===h))!=null&&x.expression)return e.jsx(t.Input,{value:(d=(v=n==null?void 0:n.onClickFilter)==null?void 0:v.find(g=>g.columnIndex===h))==null?void 0:d.expression,onChange:g=>m(h,g),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(t.Label,{children:"On Click"}),l==null?void 0:l.map((h,u)=>{var x,v;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(t.Checkbox,{checked:!!((x=n==null?void 0:n.onClickFilter)!=null&&x.find(d=>d.columnIndex===u)),onCheckedChange:d=>i(u,d)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:h}),e.jsx("div",{className:"flex items-center gap-2",children:((v=n==null?void 0:n.onClickFilter)==null?void 0:v.find(d=>d.columnIndex===u))&&e.jsx(t.Pencil,{onClick:()=>{a(d=>{const g=[...d];return g[u]=!d[u],g})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),c(u)]})},h)})]})}function Vn({idx:s}){var m;const a=t.useEditorStore(c=>{var h;return(h=c.card)==null?void 0:h.preferences}),n=t.useEditorStore(c=>{var h;return(h=c.card.preferences)==null?void 0:h.datasetOptions}),r=t.useEditorStore(c=>c.actions.setCardPreferences),o=(m=a==null?void 0:a.chartOptions)==null?void 0:m.indexAxis,l=n==null?void 0:n.find(c=>c.idx===s);function i(c,h){(c==="end"||c==="start")&&t.getContrastColor(t.getCanvasBackgroundColor());const u={display:c!=="none",align:c,anchor:c,clamp:!0};if(!l)r({...a,datasetOptions:[...n||[],{idx:h,datalabels:u}]});else{const x={...l};x.datalabels=u;const v=n==null?void 0:n.map(d=>d.idx===h?x:d);r({...a,datasetOptions:v})}}return e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{children:e.jsx(Ps,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(t.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>i("none",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(t.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>i("start",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Ts,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})}),e.jsx("button",{onClick:()=>i("center",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(As,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})}),e.jsx("button",{onClick:()=>i("end",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Ms,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})})]})})]})}function Vt(){const s=t.useEditorStore(l=>l.card),{data:a}=t.useDashboardCardQuery(s),{keys:n}=t.getKeys((a==null?void 0:a.records)||[]),r=n.slice(1).filter(l=>!t.METADATA_KEYS.includes(l));let o=r;if(["stackedBar","stackedLine"].includes(s.type)){const l=t.pivotData({dataArray:(a==null?void 0:a.records)||[],groupKey:n[0],pivotKey:n[1],valueKey:n[2]}),{keys:i}=t.getKeys(l);o=i.slice(1)}return{transformedMetricKeys:o,dimensionKeys:n.slice(0,1),keys:n,defaultMetricKeys:r}}function Ne({children:s,className:a,delayDuration:n}){return e.jsx(t.TooltipProvider$1,{children:e.jsxs(t.Tooltip,{delayDuration:n,children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:t.cn("overflow-hidden text-ellipsis whitespace-nowrap",a),children:s})}),e.jsx(t.TooltipContent,{children:s})]})})}function _t(){const s=t.useEditorStore(c=>c.card),a=t.useEditorStore(c=>{var h,u,x;return(x=(u=(h=c==null?void 0:c.card)==null?void 0:h.preferences)==null?void 0:u.chartOptions)==null?void 0:x.indexAxis}),{data:n}=t.useDashboardCardQuery(s),r=a==="y"?"Metric":"Dimension",o=a==="y"?"Dimension":"Metric",{dimensionKeys:l,transformedMetricKeys:i,keys:m}=Vt();return m.length===0?null:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:r}),e.jsx("div",{className:"max-h-[125px] space-y-1 overflow-y-auto",children:l.map(c=>e.jsx("p",{className:"flex h-8 items-center rounded-md bg-muted/70 px-3 hover:bg-muted",title:c,children:qt(c)},c))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:o}),e.jsx("div",{className:"my-1 max-h-[125px] space-y-1 overflow-y-auto",children:i.map((c,h)=>e.jsx(Pt,{className:"flex h-8 items-center bg-muted/70 px-3 hover:bg-muted",iconClassName:"invisible group-hover:visible",col:c,idx:h},c))})]})]})}function qt(s,a=30){return s.length>a?s.slice(0,a)+"...":s}function Pt({col:s,idx:a,className:n,iconClassName:r,children:o}){const l=t.useEditorStore(u=>{var x;return(x=u.card)==null?void 0:x.preferences}),i=t.useEditorStore(u=>{var x,v;return(v=(x=u.card)==null?void 0:x.preferences)==null?void 0:v.datasetOptions}),m=i==null?void 0:i.find(u=>u.idx===a),{setCardPreferences:c}=t.useEditorStore(u=>u.actions);function h(u){if(!m)c({...l,datasetOptions:[...i||[],{idx:a,type:u==="area"?"line":u,fill:u==="area"?"origin":""}]});else{const x={...m};x.type=u==="area"?"line":u,x.fill=u==="area"?"origin":"";const v=i==null?void 0:i.map(d=>d.idx===a?x:d);c({...l,datasetOptions:v})}}return e.jsxs("div",{className:t.cn("group flex items-center justify-between gap-2 rounded-md",n),children:[o||e.jsx(Ne,{className:"max-w-[100px]",title:s,children:qt(s)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(Re,{onClick:()=>h("bar"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Be,{onClick:()=>h("line"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(ms,{onClick:()=>h("area"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Vn,{idx:a})]})]})}function Ht(){const s=t.useEditorStore(d=>d.selectedConnectionId),a=t.useEditorStore(d=>d.selectedDatamodelId),n=t.useEditorStore(d=>d.selectedDatabaseName),r=t.useEditorStore(d=>d.selectedSchemaName),o=t.useEditorStore(d=>d.selectedTableName),l=t.useDashboardStore(d=>d.dashboard.filters)||[],i=t.useDashboardStore(d=>d.actions.addFilter),m=t.useDashboardStore(d=>d.actions.removeFilter),{removeFilterValues:c}=t.useDashboardActions(),{modelItems:h,tableName:u,connectionType:x}=t.useEditorAside();function v(d,g="dashboard"){var O;const f=t.fmt(d.column_name),y=t.fmt(n||""),p=t.fmt(r||""),S=t.fmt(u);let N=t.getQualifiedTableName({schemaName:p,tableName:S,connectionType:x,databaseName:y}),M=`${N}.${f}`;const w=(O=h==null?void 0:h.find(V=>V.id===a))==null?void 0:O.name;a&&a!=="none"&&(M=`${t.DATAMODEL_NAMESPCACE}.${w}.${f}`,N=`${t.DATAMODEL_NAMESPCACE}.${w}`);const C=l==null?void 0:l.find(V=>V.column===M&&V.table===S&&(V.database===y||V.database===n));if(C)c(C.id),m(C.id);else{const V=t.v4();return u==="api"&&s?(i({location:g,id:V,column:M,title:d.column_name,dataType:d.data_type,table:"api",database:"",connectionId:s,operation:"in",sql:t.getDefaultFilterSql(d.data_type,N,f)}),V):(i({id:V,location:g,column:M,title:d.column_name,dataType:d.data_type,table:o||"",database:n||"",connectionId:s||"",operation:"in",sql:t.getDefaultFilterSql(d.data_type,N,f)}),V)}}return{assignFilter:v}}function de(){const s=t.useDashboardStore(m=>m.dashboard.filters),a=t.useEditorStore(m=>m.frame),{setFrame:n}=t.useEditorActions(),{columns:r}=t.useEditorAside(),{assignFilter:o}=Ht();function l(m){const c=o(m,"frame");n({...a,filterId:c})}const i=s==null?void 0:s.find(m=>m.id===a.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(t.Label,{children:"Card Filter"}),e.jsx(_n,{onSelect:l,columns:r}),i&&e.jsx(t.FilterHeader,{filter:i})]})}function _n({columns:s,onSelect:a}){var i;const[n,r]=le.useState(!1),[o,l]=le.useState("");return e.jsxs(t.Popover,{open:n,onOpenChange:r,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":n,className:"w-full justify-between font-normal",children:[o?(i=s.find(m=>m.column_name===o))==null?void 0:i.column_name:"Select Column...",e.jsx(t.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(t.Command,{children:[e.jsx(t.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(t.CommandList,{children:[e.jsx(t.CommandEmpty,{children:"No framework found."}),e.jsx(t.CommandGroup,{children:s==null?void 0:s.map(m=>e.jsxs(t.CommandItem,{value:m.column_name,onSelect:c=>{const h=c===o?"":c;l(h),r(!1),h&&a(m)},children:[m.column_name,e.jsx(t.CheckIcon,{className:t.cn("ml-auto h-4 w-4",o===m.column_name?"opacity-100":"opacity-0")})]},m.column_name))})]})]})})]})}function qn({cardType:s}){return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} Chart`}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsx(t.AccordionContent,{className:"space-y-4 px-2",children:e.jsx(_t,{})})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function He(){var o,l,i;const s=t.useEditorStore(m=>m.card.type),a=t.useEditorStore(m=>m.card.preferences),{setCardPreferences:n}=t.useEditorStore(m=>m.actions);function r(m){var u;const c={datalabels:{display:m!=="none",align:m,anchor:m,clamp:!0}},h={...a,chartOptions:{...a==null?void 0:a.chartOptions,plugins:{...(u=a==null?void 0:a.chartOptions)==null?void 0:u.plugins,...c}}};n(h),console.log(m)}return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(t.Select,{value:((i=(l=(o=a==null?void 0:a.chartOptions)==null?void 0:o.plugins)==null?void 0:l.datalabels)==null?void 0:i.anchor)||"none",onValueChange:r,defaultValue:"none",children:[e.jsx(t.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{placeholder:"Select position"})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Data Labels"}),e.jsx(t.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(s)&&e.jsx(t.SelectItem,{value:"start",children:"Start"}),e.jsx(t.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(s)&&e.jsx(t.SelectItem,{value:"end",children:"End"})]})})]})]})}function Pn({cardType:s}){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:l=>l.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(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} Chart`}),e.jsx(t.AccordionContent,{className:"space-y-4 font-mono text-xs",children:a(s)})]}),e.jsxs(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsx(t.AccordionContent,{className:"px-2",children:e.jsx(He,{})})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}const Hn={datasets:[{type:"line",label:"Sum_sales",data:[]},{type:"bar",label:"Sum_profit",data:[]}]},Gn={datasets:[{fill:"origin",type:"line",label:"Sum_sales",data:[]}]};function Un(){const s=t.useEditorStore(a=>a.card.type);return e.jsxs("div",{className:"space-y-6 p-6",children:[e.jsx(t.Accordion,{className:" ",type:"single",collapsible:!0,children:$n(s)}),!["kpi"].includes(s)&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(He,{}),e.jsx(oe,{}),e.jsx(de,{})]})]})}function $n(s){if(["line","bar"].includes(s))return e.jsxs(e.Fragment,{children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsx("p",{children:"Chart Query"})}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:"Mixed Chart"}),e.jsxs(t.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(Hn,null,2)})]})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:"Area Chart"}),e.jsxs(t.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(Gn,null,2)})]})]})]});if(s==="bubble")return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:s})," chart query?"]})}),e.jsxs(t.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(s==="scatter")return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:s})," chart query?"]})}),e.jsxs(t.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(s))return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:[e.jsx("span",{children:s==="polarArea"?"Polar":s.charAt(0).toUpperCase()+s.slice(1)})," ","chart query"]})}),e.jsxs(t.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 Qn(){var c,h,u,x,v;const s=t.useEditorStore(d=>d.card.preferences),a=t.useEditorStore(d=>d.actions.setCardPreferences),n=t.useEditorStore(d=>d.actions.setNumberFormat);t.useEditorStore(d=>{var g,f;return(f=(g=d.card.preferences)==null?void 0:g.formatNumber)==null?void 0:f.decimalPlaces});const r=t.useEditorStore(d=>{var g,f;return(f=(g=d.card.preferences)==null?void 0:g.formatNumber)==null?void 0:f.locale}),o=t.useEditorStore(d=>{var g,f;return(f=(g=d.card.preferences)==null?void 0:g.formatNumber)==null?void 0:f.currency});t.useEditorStore(d=>d.actions.setFilterOnClickField);const l=t.useEditorStore(d=>{var g,f;return(f=(g=d.card.preferences)==null?void 0:g.formatNumber)==null?void 0:f.colorRanges})||[],i=t.useEditorStore(d=>d.actions.setColorRanges),m=[...new Set(t.LOCALE_CURRENCY_PAIRS.map(d=>d.currency))];return e.jsxs("div",{children:[e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsx(t.Checkbox,{checked:!!((c=s==null?void 0:s.formatNumber)!=null&&c.enabled),onCheckedChange:d=>{a({...s,formatNumber:{...s==null?void 0:s.formatNumber,enabled:d}})}}),e.jsx(t.Label,{children:"Format Number"})]}),((h=s==null?void 0:s.formatNumber)==null?void 0:h.enabled)===!0&&e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx("div",{children:e.jsxs("div",{className:"space-y-4 p-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Locale"}),e.jsxs(t.Select,{value:r||"none",onValueChange:d=>{var g,f,y;return n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,((f=s==null?void 0:s.formatNumber)==null?void 0:f.currency)||"",d,((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Format"}),e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),t.LOCALE_CURRENCY_PAIRS.map(d=>e.jsx(t.SelectItem,{value:d.locale,children:d.locale},d.locale))]})})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Currency"}),e.jsxs(t.Select,{value:o||"none",onValueChange:d=>{var g,f,y;console.log("currency",d),n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,d==="none"?"":d,((f=s==null?void 0:s.formatNumber)==null?void 0:f.locale)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Currency"}),e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),m.map(d=>e.jsx(t.SelectItem,{value:d,children:d},d))]})})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Decimals"}),e.jsxs(t.Select,{value:((x=(u=s==null?void 0:s.formatNumber)==null?void 0:u.decimalPlaces)==null?void 0:x.toString())||"0",onValueChange:d=>{var g,f,y;return n(Number(d),((g=s==null?void 0:s.formatNumber)==null?void 0:g.currency)||"",((f=s==null?void 0:s.formatNumber)==null?void 0:f.locale)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-3 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:["0","1","2","3","4"].map(d=>e.jsx(t.SelectItem,{value:d,children:d},d))})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Suffix"}),e.jsx(t.Input,{value:((v=s==null?void 0:s.formatNumber)==null?void 0:v.suffix)||"",className:"h-8 font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"K, M, B, %",onChange:d=>{var g,f,y;return n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,((f=s==null?void 0:s.formatNumber)==null?void 0:f.currency)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.locale)||"",d.target.value)}})]})]})]})}),e.jsx(t.Separator,{className:"my-3"}),e.jsx(t.ColorFormat,{colorRanges:l,setColorRanges:i})]})]})}function Jn(){return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(t.Accordion,{type:"single",collapsible:!0,children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"",children:"KPI Query"}),e.jsxs(t.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:s=>s.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(Qn,{})}),e.jsx("div",{}),e.jsx("div",{})]})}function Me({children:s,summary:a,open:n,className:r}){return e.jsxs("details",{open:n,className:t.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(t.Label,{children:a}),e.jsx(t.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:s})]})}const Wn=[{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"}],Kn=["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"],Yn=Kn.map(s=>({label:s,value:s}));function Zn(){var c,h,u,x,v,d,g,f,y,p,S,N,M,w,C,O,V,U;const s=t.useEditorStore(k=>k.card),a=(h=(c=s==null?void 0:s.preferences)==null?void 0:c.mapVisualOptions)==null?void 0:h.topoJsonUrl,[n,r]=T.useState(((x=(u=s==null?void 0:s.preferences)==null?void 0:u.mapVisualOptions)==null?void 0:x.customTopoJsonUrl)||""),{setCardPreferences:o}=t.useEditorActions(),{data:l}=t.useTopoJson(s),i=Object.keys((l==null?void 0:l.objects)||{}).map(k=>({label:k,value:k}))||[],m=[{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(Me,{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(t.Separator,{className:"my-4"}),e.jsx(Me,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(Y,{className:"h-9",options:m,onValueChange:k=>{console.log("value",k),o({...s.preferences,mapVisualOptions:{topoJsonUrl:k}})},value:a||"",label:"Select Map"}),a==="custom"&&e.jsx(t.Input,{onChange:k=>{r(k.target.value)},onBlur:()=>{var k;console.log("onBlur",n),o({...s.preferences,mapVisualOptions:{...(k=s.preferences)==null?void 0:k.mapVisualOptions,customTopoJsonUrl:n}})},value:n,className:`h-9 w-full ${t.removeRing}`,placeholder:"Custom URL"}),l&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Map Object"}),e.jsx(Y,{className:"h-9",options:i,value:((d=(v=s.preferences)==null?void 0:v.mapVisualOptions)==null?void 0:d.objectKey)||"",label:"Map Object",onValueChange:k=>{var z;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,objectKey:k}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Projection"}),e.jsx(Y,{className:"h-9",options:Wn,value:((f=(g=s.preferences)==null?void 0:g.mapVisualOptions)==null?void 0:f.projection)||"",label:"Projection",onValueChange:k=>{var z;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,projection:k}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Colors"}),e.jsx(Y,{className:"h-9",options:Yn,value:((p=(y=s.preferences)==null?void 0:y.mapVisualOptions)==null?void 0:p.colorScale)||"",label:"Select Color Scale",onValueChange:k=>{var z;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,colorScale:k}})}})]})]})]})}),e.jsx(t.Separator,{className:"my-4"}),e.jsx(Me,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:.1,value:((N=(S=s.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:N.projectionScale)||1,onChange:k=>{var z;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,projectionScale:Number(k.target.value)}})}}),e.jsx(t.Label,{children:"X"}),e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:10,value:((C=(w=(M=s.preferences)==null?void 0:M.mapVisualOptions)==null?void 0:w.projectionOffset)==null?void 0:C[0])||0,onChange:k=>{var z,G,q,E;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,projectionOffset:[Number(k.target.value),((E=(q=(G=s.preferences)==null?void 0:G.mapVisualOptions)==null?void 0:q.projectionOffset)==null?void 0:E[1])||0]}})}}),e.jsx(t.Label,{children:"Y"}),e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:10,value:((U=(V=(O=s.preferences)==null?void 0:O.mapVisualOptions)==null?void 0:V.projectionOffset)==null?void 0:U[1])||0,onChange:k=>{var z,G,q,E;o({...s.preferences,mapVisualOptions:{...(z=s.preferences)==null?void 0:z.mapVisualOptions,projectionOffset:[((E=(q=(G=s.preferences)==null?void 0:G.mapVisualOptions)==null?void 0:q.projectionOffset)==null?void 0:E[0])||0,Number(k.target.value)]}})}})]})}),e.jsx(t.Separator,{className:"my-4"}),e.jsx(oe,{})]})}function Xn(){var r;const s=t.useEditorStore(o=>o.card.preferences),{setCardPreferences:a}=t.useEditorStore(o=>o.actions);function n(o){a({...s,textVisualOptions:{...s==null?void 0:s.textVisualOptions,isDynamicText:o==="dynamic"}})}return e.jsxs(t.RadioGroup,{value:(r=s==null?void 0:s.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(t.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(t.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function ye({cardType:s,docContent:a,suffix:n="Chart"}){let r="pie-donut-polar";return s==="funnel"&&(r="funnel"),e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} ${n}`}),e.jsx(t.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:o=>o.stopPropagation(),className:"text-blue-700",href:`https://docs.semaphor.cloud/docs/charts/${r}`,children:"example"})," in the docs."]})]})})]}),e.jsxs(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[s!=="text"&&e.jsx(He,{}),s==="text"&&e.jsx(Xn,{})]})]}),!["range","text"].includes(s)&&e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function ea(){const s=t.useEditorStore(r=>{var o,l;return(l=(o=r==null?void 0:r.card)==null?void 0:o.preferences)==null?void 0:l.sortChart}),{setSortChart:a}=t.useEditorActions();function n(){return e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:"Sort"}),e.jsx(Y,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"ASC",value:"asc"},{label:"DESC",value:"desc"}],value:s||"none",onValueChange:r=>{a(r)}})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:"Stacked Chart"}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[n(),e.jsx(_t,{})]})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function ta({cardType:s="table"}){console.log("cardType",s);const a=t.useEditorStore(o=>{var l;return(l=o.card)==null?void 0:l.preferences}),n=t.useEditorStore(o=>o.actions.setCardPreferences),r=o=>{n({...a,allowDownload:o})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(t.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.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 sa=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/tornado",children:"example"})," in the docs."]})]}),na=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/range",children:"example"})," in the docs."]})]}),aa=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/text",children:"example"})," in the docs."]})]});function ra(){const s=t.useEditorStore(n=>n.card.type);function a(n){switch(n){case"stackedBar":case"stackedLine":return e.jsx(ea,{});case"tornado":case"pyramid":return e.jsx(ye,{docContent:sa,cardType:n});case"kpi":return e.jsx(Jn,{});case"line":case"bar":return e.jsx(qn,{cardType:n});case"range":return e.jsx(ye,{docContent:na,cardType:n});case"pie":case"doughnut":case"polarArea":case"radar":case"funnel":return e.jsx(ye,{cardType:n});case"bubble":case"scatter":return e.jsx(Pn,{cardType:n});case"table":return e.jsx(ta,{cardType:n});case"text":return e.jsx(ye,{docContent:aa,cardType:n,suffix:"Visual"});case"map":return e.jsx(Zn,{});case"custom":return e.jsx(Bn,{});default:return e.jsx(Un,{})}}return e.jsxs(t.ScrollArea,{className:"h-full w-full text-sm",children:[a(s),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})}function Gt({label:s,column:a,functions:n,onFunctionChange:r}){let o=n[0];"aggregate"in a?o=n.find(i=>i.value===a.aggregate)||n[0]:"granularity"in a&&(o=n.find(i=>i.value===a.granularity)||n[0]);function l(i){r(i)}return e.jsx(Y,{className:"flex h-8 w-8 justify-center overflow-clip rounded-none border-none bg-muted px-0 [&>svg]:hidden",icon:o.logo,options:n,value:o.value,onValueChange:i=>l(i),label:s,showPlaceholderLabel:!1})}const oa=[{label:"Sum",value:"SUM",logo:e.jsx(xt,{className:"size-3.5"})},{label:"Count",value:"COUNT",logo:e.jsx(ct,{className:"size-3.5"})},{label:"Unique",value:"DISTINCT",logo:e.jsx(Ns,{className:"size-3.5"})},{label:"Average",value:"AVG",logo:e.jsx(ss,{className:"size-3.5"})},{label:"Min",value:"MIN",logo:e.jsx(Xe,{className:"size-3.5"})},{label:"Max",value:"MAX",logo:e.jsx(t.ArrowUp,{className:"size-3.5"})}];function ia({column:s}){const{updateMetricColumn:a}=t.useEditorActions();let n=oa;t.isTextDataType(s.type)&&(n=n.filter(o=>["COUNT","DISTINCT"].includes(o.value))),t.isNumberDataType(s.type)&&(n=n.filter(o=>!["DISTINCT"].includes(o.value)));function r(o){const l={...s,aggregate:o,label:me(s.name,o)};a(s.id,l)}return e.jsx(Gt,{column:s,functions:n,onFunctionChange:o=>r(o),label:"Aggregation"})}const la=[{label:"Day (D)",value:"day",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"D"})},{label:"Week (W)",value:"week",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"W"})},{label:"Month (M)",value:"month",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"M"})},{label:"Year (Y)",value:"year",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"Y"})}];function ca({column:s}){const{updateGroupByColumn:a}=t.useEditorActions();function n(r){const o={...s,granularity:r,label:ce(s.name,r)};a(s.id,o)}return e.jsx(Gt,{column:s,functions:la,onFunctionChange:r=>n(r),label:"Granularity"})}function da({col:s,onChange:a,droppableId:n}){const{columns:r}=t.useDataColumns(),o=T.useMemo(()=>{let m=[];(n===te||n===se||n===ne)&&(m=r);const c=m==null?void 0:m.filter(h=>h.column_name!==s.name);return c==null?void 0:c.map(h=>({id:h.column_name,value:h.column_name}))},[r,n,s.name]),l=[{id:s.name,value:s.name}];function i(m){const c=r==null?void 0:r.find(h=>{var u;return h.column_name===((u=m[0])==null?void 0:u.id)});c&&a(c)}return o!=null&&o.length?e.jsx(t.ComboBox2,{multiple:!1,options:o,selectedOptions:l,onChange:i,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:s.name})}function ua({column:s}){const{updateMetricColumn:a}=t.useEditorActions(),{transformedMetricKeys:n,dimensionKeys:r}=Vt();console.log(n,r);const o=t.useEditorStore(c=>{var h;return(h=c.card.queryConfig)==null?void 0:h.pivotColumns}),l=!!(o&&o.length>0),i=n.find(c=>c===s.label||c===s.name),m=i?n.indexOf(i):-1;return e.jsxs("div",{className:"space-y-3",children:[i&&e.jsx(Pt,{idx:m,col:i,children:e.jsx(t.Editable,{enabled:!0,className:"w-[130px]",text:s.label||"",placeholder:"Enter label",onSave:c=>{a(s.id,{...s,label:c||s.name})},children:e.jsx(Ne,{className:"w-[130px]",children:s.label||s.name})})}),l&&e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(t.Label,{children:"Pivot Header"}),e.jsx(t.Editable,{enabled:!0,text:s.aliasTemplate||"",placeholder:"{label} ({pivot})",onSave:c=>{a(s.id,{...s,aliasTemplate:c||""})},children:e.jsx(Ne,{className:"font-mono text-xs text-muted-foreground",children:s.aliasTemplate||"{label} ({pivot})"})})]})]})}function ma({column:s,type:a,onRemoveColumn:n,droppableId:r}){const[o,l]=T.useState(!1),{updateGroupByColumn:i,updateMetricColumn:m,updatePivotByColumn:c}=t.useEditorActions(),{attributes:h,listeners:u,setNodeRef:x,transform:v,transition:d,isDragging:g}=t.useSortable({id:s.id,data:{type:a}}),f=v?{opacity:g?.5:void 0,transform:t.CSS.Translate.toString(v),transition:d}:void 0,y=t.isTextDataType(s.type);function p(C){const O=t.isNumberDataType(C.data_type),V=t.isTextDataType(C.data_type),U=t.isDateDataType(C.data_type),k=O?"number":V?"string":U?"date":"string";if(r===te){if(V){const z={id:s.id,name:C.column_name,type:k,role:"groupby"};i(s.id,z)}if(U){const z={id:s.id,name:C.column_name,type:k,role:"groupby",label:ce(C.column_name,"month"),granularity:"month"};i(s.id,z)}}if(r===ne){if(V){const z={id:s.id,name:C.column_name,type:k,role:"groupby"};c(s.id,z)}if(U){const z={id:s.id,name:C.column_name,type:k,label:ce(C.column_name,"month"),granularity:"month",role:"groupby"};c(s.id,z)}}if(r===se){const z={id:s.id,name:C.column_name,type:k,role:"metric",label:me(C.column_name,"SUM"),aggregate:O?"SUM":"COUNT"};m(s.id,z)}}function S(){if(r===se)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(ua,{column:s})})}function N(){const C=t.isDateDataType(s.type);if(r===se)return e.jsx(ia,{column:s});if((r===te||r===ne)&&C)return e.jsx(ca,{column:s})}function M(){return e.jsx("div",{className:t.cn("grow",{"pl-3":(r===te||r===ne)&&y}),children:e.jsx(da,{col:s,onChange:p,droppableId:r})})}function w(){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:()=>l(!o),children:o?e.jsx(_s,{className:t.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")}):e.jsx(it,{className:t.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")})})})}return e.jsx("div",{...u,...h,ref:x,style:f,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:[w(),N(),M()]}),o&&S()]}),e.jsx("div",{className:t.cn("opacity-0 transition-opacity duration-200 group-hover:opacity-100",{hidden:o}),children:e.jsx("button",{onClick:()=>n(s.id),children:e.jsx(t.X,{className:"size-3 cursor-pointer text-muted-foreground"})})})]})})}function Ae({id:s,label:a,columns:n,previewColumns:r,sortableType:o,onRemoveColumn:l}){var f,y;const{setNodeRef:i,isOver:m,over:c}=t.useDroppable({id:s}),h=t.useDndStore(p=>p.activeId),u=t.useEditorStore(p=>{var S;return(S=p.card)==null?void 0:S.type}),x=m||((y=(f=c==null?void 0:c.data)==null?void 0:f.current)==null?void 0:y.type)===o,v=h?r:n,d=o===be?"Rows":o===ve?"Pivot Columns":"Columns";function g(){if(!v||v.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(t.SortableContext,{items:n==null?void 0:n.map(p=>p.id),children:e.jsxs("div",{className:t.cn("group space-y-2 rounded-md p-4",x&&"border border-muted-foreground/20"),ref:i,children:[e.jsxs("div",{className:"flex items-center justify-between pr-5",children:[e.jsx(t.Label,{className:"text-sm",children:a}),u!=="table"&&e.jsx("span",{className:"text-xs text-muted-foreground/50",children:d})]}),g(),e.jsx("div",{className:"space-y-2",children:v==null?void 0:v.map(p=>p.id===bt.id?e.jsx(ha,{},p.id):e.jsx(ma,{droppableId:s,type:o,column:p,onRemoveColumn:l},p.id))})]})})}function ha(){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 Ut({children:s,id:a,type:n}){const{attributes:r,listeners:o,setNodeRef:l,transform:i,transition:m,isDragging:c}=t.useSortable({id:a,data:{type:n}}),h=i?{opacity:c?.5:void 0,transform:t.CSS.Translate.toString(i),transition:m}:void 0;return e.jsx("div",{...o,...r,ref:l,style:h,children:s})}function xa(){const s=t.useEditorStore(p=>{var S;return(S=p.card.queryConfig)==null?void 0:S.orderBy})||[],a=t.useEditorStore(p=>{var S;return(S=p.card.queryConfig)==null?void 0:S.groupByColumns})||[],n=t.useEditorStore(p=>{var S;return(S=p.card.queryConfig)==null?void 0:S.metricColumns})||[],{addOrderByColumn:r,removeOrderByColumn:o,updateOrderByColumn:l}=t.useEditorActions(),i=[...a,...n];if(i.length===0)return e.jsx(e.Fragment,{});const m=e.jsx(os,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),c=e.jsx(ts,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),h=e.jsx(rs,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),u=e.jsx(es,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),x=i==null?void 0:i.filter(p=>!s.some(S=>S.columnId===p.id)).map(p=>({id:p.id,value:p.label||p.name}));function v(p,S){const N=d(p);return N?N.role==="metric"?S==="asc"?h:u:S==="asc"?m:c:null}function d(p){return i.find(S=>S.id===p)}function g(p){const S=s.find(N=>N.columnId===p);if(S){const N={...S,direction:S.direction==="asc"?"desc":"asc"};l(N)}}const f=p=>{r({columnId:p.id,direction:"asc"})};function y(p){const S=s.find(N=>N.columnId===p);S&&o(S)}return e.jsx(t.SortableContext,{items:s.map(p=>ue(p.columnId)),children:e.jsxs("div",{className:"space-y-2 rounded-md p-4 text-sm",children:[e.jsx(t.Label,{className:"text-sm",children:"Sort By"}),x.length>0&&e.jsx("div",{className:"mr-5",children:e.jsx(t.ComboBox2,{multiple:!1,options:x,selectedOptions:[],onChange:p=>f(p[0]),selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"h-8 bg-muted border-none"})}),s.map(p=>{var S,N;return e.jsx(Ut,{id:ue(p.columnId),type:Pe,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(it,{className:"size-3 cursor-move text-muted-foreground hover:text-foreground"}),e.jsx(Ne,{className:"w-[175px] text-sm",children:((S=d(p.columnId))==null?void 0:S.label)||((N=d(p.columnId))==null?void 0:N.name)})]}),e.jsx("button",{title:p.direction==="asc"?"Ascending":"Descending",onClick:()=>g(p.columnId),className:"rounded-sm p-0",children:v(p.columnId,p.direction)})]}),e.jsx("div",{children:e.jsx("button",{onClick:()=>y(p.columnId),className:"rounded-sm p-0",children:e.jsx(t.X,{className:"size-3 text-muted-foreground opacity-0 transition-opacity duration-200 hover:text-foreground group-hover:opacity-100"})})})]})},p.columnId)},ue(p.columnId))})]})})}function pa(){const s=t.useEditorStore(y=>{var p;return((p=y.card.queryConfig)==null?void 0:p.groupByColumns)||[]}),a=t.useEditorStore(y=>y.card.type),n=t.useDndStore(y=>y.previewGroupByColumns),r=t.useEditorStore(y=>{var p;return((p=y.card.queryConfig)==null?void 0:p.metricColumns)||[]}),o=t.useDndStore(y=>y.previewMetricColumns),l=t.useEditorStore(y=>{var p;return((p=y.card.queryConfig)==null?void 0:p.pivotColumns)||[]}),i=t.useDndStore(y=>y.previewPivotColumns),{removeColumnFromGroupBy:m,removeColumnFromMetricColumns:c,removeColumnFromPivotBy:h}=t.useEditorActions();function u(y){m(y)}function x(y){c(y)}function v(y){h(y)}let d="Group By",g="Metrics",f="Stack By";return a==="table"&&(d="Rows",g="Metrics",f="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(t.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(Ae,{onRemoveColumn:u,sortableType:be,id:te,label:d,columns:s,previewColumns:n}),e.jsx(Ae,{onRemoveColumn:v,sortableType:ve,id:ne,label:f,columns:l,previewColumns:i}),e.jsx(Ae,{onRemoveColumn:x,sortableType:Oe,id:se,label:g,columns:r,previewColumns:o}),e.jsx(xa,{})]}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function Ge({children:s,column:a,id:n,sortableType:r,className:o}){const[l,i]=T.useState(!1),{removeEditorFilter:m,clearEditorFilter:c}=t.useEditorActions(),h=t.useEditorStore(x=>{var v,d;return(d=(v=x.card.queryConfig)==null?void 0:v.filters)==null?void 0:d.some(g=>g.filterColumnName===a.name)});function u(x){m(x)}return e.jsx(Ut,{id:n,type:r,children:e.jsxs("div",{className:t.cn("group space-y-1 rounded-md bg-muted px-3 py-2",{"border-border":l}),children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Label,{className:"text-sm",children:a.name}),h&&e.jsx("button",{onClick:()=>c(a),children:e.jsx(t.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:()=>u(a),children:e.jsx(t.X,{className:"size-3.5 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground"})})})]}),e.jsxs("div",{className:t.cn("group flex items-center rounded-t-md text-sm",!l&&"rounded-b-md",l&&"border border-border"),children:[e.jsx("div",{children:e.jsx(t.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:t.cn("grow cursor-pointer overflow-hidden truncate text-ellipsis whitespace-nowrap",o),children:s})]})]})})}function fa({column:s,sortableType:a}){const n=t.useDashboardStore(N=>N.themeStyle),r=t.useEditorStore(N=>{var M,w;return(w=(M=N.card)==null?void 0:M.queryConfig)==null?void 0:w.filters}),o=t.useEditorStore(N=>{var M,w;return(w=(M=N.card.queryConfig)==null?void 0:M.filterColumns)==null?void 0:w.find(C=>C.name===s.name)}),{selectedConnectionId:l,selectedDatabaseName:i,selectedTableName:m}=t.useEditorAside(),c=T.useMemo(()=>{var N;return(N=r==null?void 0:r.find(M=>M.filterColumnName===s.name))==null?void 0:N.filterValue},[r,s.name]),h=T.useMemo(()=>Array.isArray(c)?c.map(N=>({id:N,value:N})):[],[c]),{updateEditorFilterValue:u}=t.useEditorActions(),x={column:s.name,sql:s.sql,id:s.name,connectionId:l,dataType:s.type},{data:v,isLoading:d,isFetching:g,isError:f}=t.useFilterQuery(x),y=T.useMemo(()=>{var M;return x.column?(M=v==null?void 0:v.records)==null?void 0:M.map(w=>{let C=x.column.split(".").slice(-1)[0].replace(/"/g,"");return w[C]===void 0&&(C=C.toLowerCase()),{id:w[C],value:w[C]}}):[]},[v,x.column]);function p(N){const M=N.map(w=>w.id);u(s.name,M)}function S(){return d||g?e.jsx(t.Skeleton,{style:{borderRadius:n==null?void 0:n.controlInputRadius},className:"h-8 min-w-60 bg-foreground/10"}):f?e.jsx("div",{className:"flex h-8 min-w-60 items-center bg-muted px-2 text-sm",children:"Error"}):e.jsx(t.ComboBox2,{renderFilterModeButton:()=>e.jsx(ga,{columnName:s.name}),showNegativeIndicator:(o==null?void 0:o.mode)==="exclude",options:y,onChange:p,selectedOptions:h,selectAllChecked:!1,onSelectAllCheckedChange:()=>{}})}return e.jsx(Ge,{className:"w-60",id:s.id,sortableType:a,column:s,children:S()})}function ga({columnName:s}){const{setEditorFilterMode:a}=t.useEditorActions(),n=t.useEditorStore(o=>{var l,i;return(i=(l=o.card.queryConfig)==null?void 0:l.filterColumns)==null?void 0:i.find(m=>m.name===s)});function r(){(n==null?void 0:n.mode)==="exclude"?a(s,"include"):a(s,"exclude")}return e.jsx(t.Button,{onClick:r,variant:"secondary",size:"sm",className:t.cn("h-5 text-xs hover:underline",{}),children:t.titleCase((n==null?void 0:n.mode)==="exclude"?"exclude":"include")})}function ja({column:s,sortableType:a}){const[n,r]=T.useState(!1),{selectedConnectionId:o,selectedDatabaseName:l,selectedTableName:i}=t.useEditorAside(),m={column:s.name,sql:s.sql,id:s.name,connectionId:o,title:"",dataType:s.type},{isLoading:c,isFetching:h,isError:u,data:x}=t.useFilterQuery(m),v=x==null?void 0:x.records,{dateRange:d,setDateRange:g,initialDateRange:f}=t.useDateRangeFromRecords(v),{updateEditorFilterValue:y}=t.useEditorActions();function p(M){var C,O;const w={gte:(C=M==null?void 0:M.from)==null?void 0:C.toISOString(),lte:(O=M==null?void 0:M.to)==null?void 0:O.toISOString()};y(s.name,w)}function S(){y(s.name,null,!0),g(f)}function N(){return c||h?e.jsx("div",{children:e.jsx(t.Skeleton,{className:"h-8 w-72 bg-foreground/10"})}):u?e.jsx("div",{className:"h-8 w-72 bg-muted px-2",children:"Error"}):v?e.jsx(t.DateRangePopover,{title:m.title,open:n,inputClassName:"h-8",setOpen:r,dateRange:d,setDateRange:g,onApply:p,onClear:S}):e.jsx(t.Label,{className:"flex h-8 w-72 items-center bg-muted px-2",children:"No Data"})}return e.jsx(Ge,{className:"w-72",id:s.id,sortableType:a,column:s,children:N()})}function ya({id:s,label:a,columns:n,previewColumns:r,sortableType:o}){var g,f;const{setNodeRef:l,isOver:i,over:m}=t.useDroppable({id:s}),c=t.useDndStore(y=>y.activeId),h={},u=i||((f=(g=m==null?void 0:m.data)==null?void 0:g.current)==null?void 0:f.type)===o;function x(){if(!d||d.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 v(y){const p=t.isDateDataType(y.type);return y.id===bt.id?e.jsx(Ge,{id:y.id,sortableType:o,column:y,children:e.jsx(ba,{})},y.id):p?e.jsx(ja,{droppableId:s,sortableType:o,column:y},y.id):e.jsx(fa,{droppableId:s,sortableType:o,column:y},y.id)}const d=c?r:n;return e.jsx(t.SortableContext,{items:n==null?void 0:n.map(y=>y.id),children:e.jsxs("div",{className:t.cn("group space-y-2 rounded-md border-muted-foreground/50 p-4",u&&"border border-muted-foreground/20"),ref:l,style:h,children:[e.jsx(t.Label,{className:"text-sm",children:a}),x(),e.jsx("div",{className:"flex flex-wrap items-center gap-4",children:d==null?void 0:d.map(y=>v(y))})]})})}function ba(){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 va(){const s=t.useEditorStore(o=>{var l;return((l=o.card.queryConfig)==null?void 0:l.filterColumns)||[]}),a=t.useDndStore(o=>o.previewFilterColumns),{removeColumnFromFilterColumns:n}=t.useEditorActions();function r(o){n(o)}return e.jsx("section",{className:"size-full px-4 pb-4",children:e.jsx(ya,{onRemoveColumn:r,sortableType:Fe,id:ze,label:"Filters",columns:s,previewColumns:a})})}function Ca(){const s=t.useEditorStore(r=>r.card),{data:a,errorMessage:n}=t.useDashboardCardQuery(s);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(Et,{}),e.jsx(Ct,{sql:a==null?void 0:a.sql,error:n})]}),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(It,{}),e.jsx(Dt,{variant:"default"})]})]})}function Sa(){const s=T.useRef(null),a=t.useEditorStore(i=>i.isShowingVisual),n=t.useEditorStore(i=>i.showAIDialog),r=t.useEditorStore(i=>i.isDevMode);T.useEffect(()=>{var i,m;n?(i=s.current)==null||i.resize(0):(m=s.current)==null||m.resize(25)},[n]);function o(){return e.jsxs(e.Fragment,{children:[e.jsx(t.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(bn,{})}),e.jsx(t.ResizableHandle,{withHandle:!0}),e.jsx(t.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:a&&!n?e.jsxs(t.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(t.ResizablePanel,{ref:s,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(ra,{})})}),!n&&e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(je,{})})]}):e.jsx(je,{})})]})}function l(){return e.jsx(e.Fragment,{children:e.jsx("div",{className:"grow","data-biz-mode-container":"",children:a&&!n?e.jsxs(t.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(t.ResizablePanel,{ref:s,className:"flex min-w-[320px]",defaultSize:25,maxSize:30,children:e.jsx(pa,{})})}),!n&&e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{defaultSize:75,maxSize:100,children:e.jsxs(t.ResizablePanelGroup,{direction:"vertical",children:[e.jsxs(t.ResizablePanel,{className:"flex min-h-fit flex-col",minSize:10,defaultSize:20,children:[e.jsx(Ca,{}),e.jsx(va,{})]}),e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{minSize:60,defaultSize:80,children:e.jsx(je,{})})]})})]}):e.jsx(je,{})})})}return e.jsx(t.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:r?o():l()})}const Na=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function wa(){const s=T.useRef(null),a=T.useRef(null),[n,r]=T.useState(!1),[o,l]=T.useState(t.isDevEnv),[i,m]=T.useState(!1),c=t.useEditorStore(b=>b.actions.setCardSql),h=t.useEditorStore(b=>b.actions.setRunSql),u=t.useEditorStore(b=>b.showAIDialog),{setShowAIDialog:x,setCardPython:v}=t.useEditorActions(),{messages:d,setMessages:g,reload:f,append:y,input:p,setInput:S,isLoading:N,handleAddCurrentTableToScope:M,removeFromCurrentSelections:w,currentAssitantScope:C,handleInputChange:O,handleSubmit:V,stop:U,chatStatus:k,llmUsage:z,errorMessage:G}=kt();t.useAssistantScroll({scrollContainerRef:s,inputRef:a,messages:d}),T.useEffect(()=>{var b;u&&((b=a.current)==null||b.focus())},[u]);function q(b,L){L==="sql"?(c(b),h(!0)):L==="python"&&(v(b),h(!0))}function E(b){const L={role:"user",content:b.title+" "+b.subtitle};y(L)}function j(){return e.jsxs(t.DropdownMenu,{children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",children:e.jsx(ot,{className:"size-3.5 text-muted-foreground/70 hover:text-muted-foreground"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",children:[e.jsx(t.DropdownMenuLabel,{children:"Options"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsx(t.DropdownMenuCheckboxItem,{checked:n,onCheckedChange:r,children:"Token Usage"}),e.jsx(t.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:l,children:"Tool Invocations"}),e.jsx(t.DropdownMenuCheckboxItem,{checked:i,onCheckedChange:m,children:"System Message"})]})]})}function A(){return e.jsxs("div",{"data-role":"header",children:[e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"}),C&&C.length>0&&e.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-2",children:C==null?void 0:C.map((b,L)=>e.jsxs("div",{className:"flex items-center gap-2 rounded-md border border-border bg-muted px-2 py-1 text-xs",children:[t.getFormattedTableNameForQuery({schemaName:b.schemaName||"",tableName:b.tableName||"",modelName:b.datamodelName||"",connectionType:b.connectionType||"",databaseName:b.databaseName||""}),e.jsx("button",{onClick:()=>{w(b)},children:e.jsx(t.X,{className:"size-3 text-muted-foreground hover:text-foreground"})})]},L))}),(C==null?void 0:C.length)===0&&e.jsxs("div",{className:"mt-2 flex items-center gap-2",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Context: None"}),e.jsxs(t.Button,{className:"h-6 text-xs",size:"sm",variant:"secondary",onClick:M,children:[e.jsx(t.Plus,{className:"mr-2 size-3"}),"Current Table"]})]})]})}const R=(p==null?void 0:p.length)===0&&(d==null?void 0:d.filter(b=>b.role==="user").length)===0;return e.jsxs("div",{"data-role":"editor-assistant",className:"flex h-full w-full flex-col justify-between gap-2 px-6 py-4",children:[e.jsxs("div",{"data-role":"header-section",className:"flex items-start justify-between gap-2",children:[A(),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center",children:[j(),e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",onClick:()=>{g([]),f()},children:e.jsx(t.Plus,{className:"size-4 text-muted-foreground"})}),e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",onClick:()=>{x(!1)},children:e.jsx(t.X,{className:"size-4 text-muted-foreground"})})]})]}),e.jsx(t.CustomScrollArea,{ref:s,className:"flex w-full grow basis-0",children:e.jsxs("div",{className:"space-y-4 py-2 pr-3","data-role":"messages",children:[e.jsx(t.PrintMessages,{messages:d,showTools:o,handleRun:q,showSystemMessage:i}),e.jsx(t.ChatStatus,{messages:d,chatStatus:k,errorMessage:G})]})}),R&&e.jsx(t.Placeholders,{onClick:E,placeholders:Na}),e.jsx(t.ChatInput,{onFocus:()=>{M()},stop:U,ref:a,setInput:S,input:p,handleInputChange:O,handleSubmit:V,isLoading:N}),z&&n&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",z.promptTokens," prompt tokens,"," ",z.completionTokens," completion tokens,"," ",z.totalTokens," total tokens"]})})]})}function ka({authToken:s,onSave:a,onClose:n}){const r=T.useRef(null),o=t.useEditorStore(y=>y.showAIDialog),{setAuthToken:l}=t.useDashboardActions(),{setShowAIDialog:i,setOnSave:m,setOnClose:c}=t.useEditorActions(),{handleDragOver:h,handleDragEnd:u,handleDragStart:x,handleDragCancel:v,sensors:d}=an();T.useEffect(()=>{a&&m(a),n&&c(n)},[a,m,n,c]),T.useEffect(()=>{s&&s.accessToken&&l(s)},[s,l]),T.useEffect(()=>{var y,p;o?((y=r.current)==null||y.resize(40),i(!0)):(i(!1),(p=r.current)==null||p.resize(0))},[o,i]);function g(y){y<1?i(!1):o||i(!0)}function f(){return e.jsxs(t.ResizablePanelGroup,{"data-content":"editor-main",className:"grow",direction:"horizontal",children:[e.jsx(t.ResizablePanel,{className:"min-w-[510px] rounded-none",children:e.jsx(Sa,{})}),e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{"data-role":"editor-assistant-panel",ref:r,onResize:g,defaultSize:0,children:e.jsx(wa,{})})]})}return e.jsx(t.ErrorBoundary,{FallbackComponent:t.Fallback,children:e.jsx(t.DndContext,{sensors:d,onDragStart:x,onDragEnd:u,onDragOver:h,onDragCancel:v,children:e.jsxs("section",{className:"isolate flex h-full grow","data-content":"editor-panel",children:[e.jsx(vt,{type:"main"}),f()]})})})}function Da(){t.useDashboardStore(i=>i.selectedCardId);const s=t.useDashboardStore(i=>i.selectedSheetId),{setIsVisualEditing:a}=t.useDashboardStore(i=>i.actions),n=t.useDashboardStore(i=>i.isVisualEditing),r=t.useDashboardStore(i=>i.actions.updateFrame);function o(i){s&&(r(s,i),a(!1))}function l(){a(!1)}return e.jsx("div",{role:"editor-container",className:t.cn("flex grow",{"max-h-0 overflow-hidden":!n}),children:e.jsx(ka,{onSave:o,onClose:l})})}function Ia(){const s=t.useEditorStore(l=>l.selectedDatabaseName),a=t.useEditorStore(l=>l.selectedTableName),n=t.useDashboardStore(l=>l.dashboard.filters)||[],{columns:r}=t.useEditorAside(),{assignFilter:o}=Ht();return e.jsx(t.ScrollArea,{className:"flex-1 basis-0 flex-col px-3",children:r==null?void 0:r.map(l=>{const i=n==null?void 0:n.find(m=>m.column===t.fmt(l.column_name)&&m.table===a&&m.database===s);return e.jsx("div",{onClick:()=>o(l),className:t.cn("group flex cursor-pointer items-center justify-between border border-dashed border-background px-3 py-[6px] text-sm transition-colors hover:bg-muted/50",{"bg-muted":i}),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(qe,{column:l}),e.jsx("span",{children:l.column_name})]})},l.column_name)})})}function Ea(){t.useDashboardStore(a=>a.showFilters);const s=t.useDashboardStore(a=>a.actions.setShowFilters);return e.jsxs("section",{className:"relative flex min-w-[320px] grow flex-col border-l px-1",children:[e.jsxs("div",{className:"mt-4 flex items-center justify-between gap-2 px-3",children:[e.jsx(t.Label,{className:"px-2 text-lg",children:"Filters"}),e.jsx(t.Button,{onClick:()=>s(!1),className:"h-7 w-7 p-0",variant:"ghost",children:e.jsx(t.Cross2Icon,{})})]}),e.jsx("div",{className:"flex grow",children:e.jsx(vt,{type:"filter",ColumnsComponent:()=>e.jsx(Ia,{})})})]})}function Ta(){const s=t.useDashboardStore(r=>r.dashboard.filters),a=t.useDashboardStore(r=>r.theme),n=s==null?void 0:s.filter(r=>r.hide);return n!=null&&n.length?e.jsx("div",{className:t.cn("mb-1 flex items-center gap-2 border-y px-0.5 pb-0.5",{"bg-[repeating-linear-gradient(45deg,_#ffffff_0_3px,_#f3f3f3_3px_6px)]":a==="light","bg-[repeating-linear-gradient(45deg,_#18171d_0_3px,_#272729_3px_6px)]":a==="dark"}),children:n==null?void 0:n.map(r=>e.jsxs("div",{className:"flex min-w-[250px] max-w-fit flex-col gap-1 p-2",children:[e.jsx(t.FilterHeader,{filter:r}),e.jsx(t.FilterComponent,{filter:r})]},r.id))}):null}function Ma(){const s=t.useDashboardStore(r=>r.showFilters),a=t.useDashboardStore(r=>r.isVisualEditing),n=t.useDashboardStore(r=>r.isDashboardEditing);return e.jsxs("div",{role:"dashboard-plus-main",className:t.cn("",{"max-h-0 overflow-hidden":a,"flex grow overflow-auto":!a}),children:[e.jsxs("section",{className:"flex grow flex-col",children:[n&&e.jsx(Ta,{}),e.jsx(t.DashboardTabs,{})]}),s&&e.jsx("div",{role:"filter-aside-container",className:"flex",children:e.jsx(Ea,{})})]})}function Aa(){const s=t.useDashboardStore(o=>o.dashboard),a=t.useDashboardStore(o=>o.actions.setDashboard),n=t.useDashboardStore(o=>o.showDashboardJSON),r=t.useDashboardStore(o=>o.actions.setShowDashboardJSON);return e.jsxs("div",{className:t.cn("relative p-2",{hidden:!n}),children:[e.jsxs(t.Button,{variant:"secondary",size:"sm",className:"absolute right-2 top-2 z-10 h-8",onClick:()=>r(!1),children:[e.jsx(t.X,{className:"mr-2 h-4 w-4"}),"Close"]}),e.jsx(t.de,{onChange:o=>{if(o)try{JSON.parse(o),a(JSON.parse(o))}catch{}},defaultLanguage:"json",className:"h-[550px] overflow-y-auto",value:JSON.stringify(s,null,2),options:{minimap:{enabled:!1},glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})]})}function La({showControls:s=!1,showFooter:a=!0,...n}){return e.jsx(t.ErrorBoundary,{FallbackComponent:t.Fallback,children:e.jsxs(t.SemaphorContextProvider,{dashboardProps:n,children:[e.jsx(t.$e,{}),e.jsxs(t.UXProvider,{children:[e.jsx(Aa,{}),e.jsxs(t.DashboardPanel,{...n,children:[s&&e.jsx(Xs,{}),e.jsx(Da,{}),e.jsx(Ma,{})]})]})]})})}exports.Dashboard=t.Dashboard;exports.DashboardWC=t.DashboardWC;exports.SemaphorDataProvider=t.SemaphorQueryClient;exports.SemaphorQueryClient=t.SemaphorQueryClient;exports.createSqlGenConfig=t.createSqlGenConfig;exports.fmt=t.fmt;exports.getColumnDataType=t.getColumnDataType;exports.getFilterValueType=t.getFilterValueType;exports.getFormattedTableNameForQuery=t.getFormattedTableNameForQuery;exports.getQualifiedTableName=t.getQualifiedTableName;exports.getSql=t.getSql;exports.invalidateToken=t.invalidateToken;exports.isBooleanDataType=t.isBooleanDataType;exports.isColumnInSqlGen=t.isColumnInSqlGen;exports.isDateDataType=t.isDateDataType;exports.isNumberDataType=t.isNumberDataType;exports.isTextDataType=t.isTextDataType;exports.resolveDataType=t.resolveDataType;exports.resolveDatamodelName=t.resolveDatamodelName;exports.useCard=t.useCard;exports.useDashboardActions=t.useDashboardActions;exports.useDashboardStore=t.useDashboardStore;exports.useDateFilterHook=t.useDateFilterHook;exports.useEditorActions=t.useEditorActions;exports.useEditorAside=t.useEditorAside;exports.useEditorStore=t.useEditorStore;exports.useTextFilterHook=t.useTextFilterHook;exports.EMPTY_SELECTION=Wt.EMPTY_SELECTION;exports.ScheduleDashboard=yt;exports.SelectComponent=ie;exports.Surfboard=La;exports.cleanCard=ft;exports.downloadPdf=jt;exports.getDashbaordStateWithoutData=_e;
|
|
371
|
+
`;f(I)}function B(){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(gn,{jsonString:JSON.stringify(c,null,2)})})}),c&&c.length>0&&e.jsx(t.Button,{variant:"ghost",onClick:()=>o(!r),className:"absolute bottom-0 right-6 size-7 p-1",children:r?e.jsx(t.X,{className:"size-3.5 text-muted-foreground"}):e.jsx(lt,{className:"size-3.5 text-muted-foreground"})})]})}function v(){return e.jsxs(t.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(t.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(pn,{})}),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(t.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(t.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(fn,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function O(){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(t.Button,{title:"Shift + Enter",disabled:F||R,onClick:()=>{P()},className:"shrink-0",size:"sm",children:[F||R?e.jsx(ae,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(t.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),e.jsx(Ct,{sql:D==null?void 0:D.sql,error:E,onClick:M}),e.jsx(Et,{}),e.jsx(t.Toggle,{pressed:a,onPressedChange:I=>n(I),className:"",size:"sm",children:e.jsx(xn,{className:"size-4 text-foreground/70"})}),e.jsx("button",{disabled:i,onClick:()=>{j(),S(!0)},className:"h-9 w-full cursor-text px-3 text-left text-sm text-muted-foreground",children:i?"":"Type your question..."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(It,{}),e.jsx(Dt,{})]})]})}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:[v(),B()]}),O()]})}function vn({data:s}){var i;const[a,n]=T.useState([]),o=Object.keys(s[0]).map(u=>({accessorKey:u,header:({column:c})=>{const h=c.getIsSorted();return e.jsx("div",{className:"flex gap-2",children:e.jsxs("div",{className:"flex cursor-pointer items-center",onClick:()=>c.toggleSorting(c.getIsSorted()==="asc"),children:[u,h==="asc"?e.jsx(t.TriangleUpIcon,{className:"ml-2 h-5 w-5"}):h==="desc"?e.jsx(t.TriangleDownIcon,{className:"ml-2 h-5 w-5"}):null]})})}})),l=t.useReactTable({data:s,columns:o,getCoreRowModel:t.getCoreRowModel(),getPaginationRowModel:t.getPaginationRowModel(),onSortingChange:n,getSortedRowModel:t.getSortedRowModel(),manualPagination:!0,state:{sorting:a}});return e.jsxs(t.CustomScrollArea,{className:"grow overflow-auto font-mono text-xs text-foreground/95",children:[e.jsxs("table",{className:"w-full",children:[e.jsx(t.TableHeader,{className:"sticky top-0 z-50 h-0 bg-muted",children:l.getHeaderGroups().map(u=>e.jsx(t.TableRow,{className:"",children:u.headers.map(c=>e.jsx(t.TableHead,{className:"h-11 font-semibold text-foreground",children:c.isPlaceholder?null:t.flexRender(c.column.columnDef.header,c.getContext())},c.id))},u.id))}),e.jsx(t.TableBody,{children:(i=l.getRowModel().rows)!=null&&i.length?l.getRowModel().rows.map(u=>e.jsx(t.TableRow,{className:"h-11 whitespace-nowrap py-2","data-state":u.getIsSelected()&&"selected",children:u.getVisibleCells().map(c=>e.jsx(t.TableCell,{className:"py-2",children:t.flexRender(c.column.columnDef.cell,c.getContext())},c.id))},u.id)):e.jsx(t.TableRow,{children:e.jsx(t.TableCell,{colSpan:o.length,className:"h-24 text-center",children:"No results."})})})]}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})}function Cn({card:s}){var S,C,A,N,D,F,R,U,w,_,G;const[a,n]=T.useState(!1),[r,o]=T.useState([]),{authToken:l}=t.useSemaphorContext(),i=t.getTokenParams(l==null?void 0:l.accessToken),u=t.useDashboardStore(P=>P.themeStyle),c={colors:((C=(S=u==null?void 0:u.chart)==null?void 0:S.dataset)==null?void 0:C.backgroundColor)||[]};(A=s.customCardPreferences)!=null&&A.inputData;const{getCard:h}=t.useCustomVisual(((N=s.customCardPreferences)==null?void 0:N.url)||""),d=((D=s.customCardPreferences)==null?void 0:D.visualType)||"single",x=t.useEditorStore(P=>P.frame),{setCustomCardPreferences:b}=t.useEditorActions(),{data:m}=t.useDashboardCardQuery(s),g=T.useCallback((P,E)=>{o(j=>({...j,[P]:E}))},[]);if(!((F=s.customCardPreferences)!=null&&F.componentName))return null;const p=h((R=s.customCardPreferences)==null?void 0:R.componentName);function y(P){n(P)}const f=Object.keys(((U=s.customCardPreferences)==null?void 0:U.dataInputCardIds)||{}).map(P=>{var M,B,v;const E=(B=(M=s.customCardPreferences)==null?void 0:M.dataInputCardIds)==null?void 0:B[P].cardId,j=(v=x==null?void 0:x.cards)==null?void 0:v.find(O=>O.id===E);return j||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(T.Suspense,{fallback:e.jsx(e.Fragment,{}),children:[p&&d==="single"&&e.jsx(p,{params:i,theme:c,settings:(w=s.customCardPreferences)==null?void 0:w.settings,onDataChange:g,data:(m==null?void 0:m.records)||[]}),p&&d==="multiple"&&e.jsxs(e.Fragment,{children:[Object.keys(((_=s.customCardPreferences)==null?void 0:_.dataInputCardIds)||{}).map(P=>{var M,B,v;const E=(B=(M=s.customCardPreferences)==null?void 0:M.dataInputCardIds)==null?void 0:B[P].cardId,j=(v=x==null?void 0:x.cards)==null?void 0:v.find(O=>O.id===E);if(j)return e.jsx(t.MultiInputEmptyRenderer,{onLoadingStatus:y,onDataChange:g,dataInputIndex:P,card:j},j.id+P)}),e.jsx("div",{className:"flex flex-col gap-2",children:e.jsx(p,{theme:c,onDataChange:g,settings:f.map(P=>{var E;return(E=P==null?void 0:P.customCardPreferences)==null?void 0:E.settings}),data:f.map(P=>P!=null&&P.id?r==null?void 0:r[P.id]:[])})})]})]},(G=s.customCardPreferences)==null?void 0:G.componentName)})}function Tt({className:s,...a}){var y;const n=t.useEditorStore(f=>f.card),r=((y=n.customCardPreferences)==null?void 0:y.showCardHeader)??!0,o=t.useEditorStore(f=>f.frame),l=t.useDashboardStore(f=>f.dashboard.filters);t.useEditorStore(f=>f.actions.setRunSql);const{updateFilter:i}=t.useDashboardActions(),u=l==null?void 0:l.find(f=>f.id===o.filterId),{data:c,isLoading:h,isFetching:d}=t.useDashboardCardQuery(n),{setCard:x}=t.useEditorActions(),{data:b}=t.useTopoJson(n),m=T.useMemo(()=>!n.id||!(c!=null&&c.records)?null:t.createChartConfig({data:c==null?void 0:c.records,cardType:n.type,queryConfig:n.queryConfig,customCfg:n.customCfg,preferences:n.preferences,topoJson:b}),[n.id,c==null?void 0:c.records,n.type,n.queryConfig,n.customCfg,n.preferences,b]);function g(f){return e.jsx(Cn,{card:f})}function p(){if(c!=null&&c.records&&(c==null?void 0:c.records.length)===0&&!h&&!d)return e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(t.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No records to display"})]});if(!n.id)return null;if(n.type==="custom")return g(n);if(!["table","custom"].includes(n.type)&&m)return e.jsx(e.Fragment,{children:h||d?e.jsx(e.Fragment,{}):e.jsx(t.ChartJsVisual,{cfg:m})});if(n.type==="table"&&(c!=null&&c.records)&&(c==null?void 0:c.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(t.TableVisual,{card:n,data:c==null?void 0:c.records})})}return e.jsxs(t.Card,{role:"editor-visual-card",className:t.cn("relative flex h-full flex-col rounded-none",s),...a,children:[r&&e.jsxs(t.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{children:[e.jsx(t.Editable,{enabled:!0,text:n.title,onSave:f=>x({...n,title:f}),children:e.jsx("div",{className:"flex items-center gap-5",children:e.jsx(t.CardTitle,{className:"text-base",children:n.title||"Add title"})})},n.title),e.jsx(t.Editable,{enabled:!0,text:n.description||"",onSave:f=>x({...n,description:f}),children:e.jsx(t.CardDescription,{children:n.description||"Add description"})},n.description)]}),u&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(t.Editable,{enabled:!0,className:"my-1",text:u.title||u.column,onSave:f=>i({...u,title:f}),children:e.jsx(t.Label,{children:u.title})}),e.jsx(t.FilterComponent,{filter:u})]})]}),e.jsx(t.CardContent,{className:"flex grow flex-col",children:p()})]})}const Te=({language:s,value:a,onChange:n,readonly:r})=>e.jsx(t.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:s,value:a,onChange:n}),Sn=()=>e.jsxs(t.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(t.DialogTitle,{children:"Customize Card"})}),e.jsxs(t.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."})]})]}),Nn=({customCode:s,cardCustomCfgString:a,mergedConfig:n,onConfigChange:r,onCodeChange:o})=>e.jsxs(t.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(t.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(t.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(t.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(t.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(t.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{language:"json",value:n,onChange:r})}),e.jsx(t.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{readonly:!0,language:"json",value:a,onChange:()=>{}})}),e.jsx(t.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(Te,{language:"javascript",value:s,onChange:o})})]}),wn=({error:s,hasCardCustomConfig:a,onReset:n,onApply:r,onClose:o})=>e.jsxs(t.DialogFooter,{className:"flex items-center",children:[e.jsx(t.Label,{className:"mr-2 text-red-500",children:s}),e.jsx(t.Button,{disabled:!a,onClick:n,variant:"outline",children:"Reset"}),e.jsx(t.Button,{onClick:r,variant:"outline",children:"Apply"}),e.jsx(t.Button,{onClick:o,variant:"outline",children:"Close"})]});function kn(){const[s,a]=T.useState(!1),[n,r]=T.useState(""),[o,l]=T.useState(""),[i,u]=T.useState(""),[c,h]=T.useState(""),d=t.useEditorStore(N=>N.card),x=t.useEditorStore(N=>N.card.customCfg),b=x?JSON.stringify(x,null,2):"",{data:m}=t.useDashboardCardQuery(d),{data:g}=t.useTopoJson(d),{setCardCustomCfg:p,setCustomVisualCode:y}=t.useEditorActions(),f=N=>{var D,F;return JSON.stringify({...N,data:{...N.data,datasets:(F=(D=N.data)==null?void 0:D.datasets)==null?void 0:F.map(R=>({...R,data:[]}))}},null,2)},S=()=>{var D,F;if(!((D=m==null?void 0:m.records)!=null&&D.length))return;const N=t.createChartConfig({data:m.records,cardType:d.type,queryConfig:d.queryConfig,preferences:d.preferences,topoJson:g});if(N){r(f(N)),u(((F=d.preferences)==null?void 0:F.customVisualCode)||"");const R=t.merge(N,x);l(f(R))}},C=()=>{try{if(i&&y(i),o){const N=JSON.parse(o),D=t.getObjectDiff(JSON.parse(n),N);p(D),h("")}}catch{h("Invalid JSON configuration")}},A=Object.keys(x||{}).length===0;return e.jsxs(t.Dialog,{open:s,onOpenChange:a,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Customize Visual",onClick:S,className:A?"":"text-green-600 hover:text-green-700",children:e.jsx(t.Settings,{className:"h-4 w-4"})})}),e.jsxs(t.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(Sn,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(t.ErrorBoundary,{FallbackComponent:t.FallbackEditorCard,children:e.jsx(Tt,{className:"w-1/2 rounded shadow-none"})}),e.jsx(Nn,{customCode:i,cardCustomCfgString:b,mergedConfig:o,onConfigChange:N=>l(N??""),onCodeChange:N=>u(N??"")})]}),e.jsx(wn,{error:c,hasCardCustomConfig:!!x,onReset:()=>{l(n),p(null)},onApply:C,onClose:()=>a(!1)})]})]})}function Mt(s){const[a,n]=T.useState(null);return a}function At(){const{authToken:s}=t.useSemaphorContext(),{data:a,isLoading:n,isFetching:r,isError:o}=t.useQuery({queryKey:["plugins",s==null?void 0:s.accessToken],queryFn:()=>t.getPlugins(s==null?void 0:s.accessToken)});return{data:a,isLoading:n,isFetching:r,isError:o}}function Dn({onChartTypeChange:s}){const[a,n]=T.useState([]),{setCardCustomCfg:r,setCardPreferences:o,setCardType:l,setCustomCardPreferences:i}=t.useEditorActions(),{data:u}=At();function c(h,d){r(null),o({}),l(h),d&&i({type:"component",visualType:"single",url:d.url,componentName:d.name,icon:d.icon}),s==null||s()}return T.useEffect(()=>{(async()=>{var d;if(u){const b=(await Promise.all((d=u==null?void 0:u.plugins)==null?void 0:d.map(async m=>(await t.loadManifest(m.value)).visuals.map(y=>({name:y.name,icon:y.icon,url:m.value}))))).flat();n(b)}})()},[u]),a.length===0?null:e.jsxs(e.Fragment,{children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"grid grid-cols-4 place-items-center",children:a.map((h,d)=>e.jsx(In,{text:h.name,onClick:()=>c("custom",h),icon:h.icon},h.url+h.name+d))})]})}const In=({text:s,icon:a,onClick:n})=>{const r=Mt(),o=s.charAt(0).toUpperCase();return e.jsx(t.IconButton,{onClick:n,tooltip:s,children:r?e.jsx(r,{className:"h-4 w-4"}):o})};function Lt(s){return re({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:[]}]})(s)}function En(s){return re({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:[]}]})(s)}function Ot(s){return re({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:[]}]})(s)}function zt(s){return re({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:[]}]})(s)}function Ft(s){return re({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:[]}]})(s)}function Rt(s){return re({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:[]}]})(s)}function Bt({onChartTypeChange:s,iconClassName:a}){const{setCardType:n,setCardPreferences:r,setIsDevMode:o}=t.useEditorStore(d=>d.actions),{setCardCustomCfg:l,setCustomCardPreferences:i,setQueryConfig:u}=t.useEditorActions();function c(d,x){l(null),r({}),n(d),["kpi","custom","text","map"].includes(d)&&(o(!0),u(void 0)),s==null||s()}const h=1e3;return e.jsxs("div",{className:"grid grid-cols-4 place-items-center",children:[e.jsx(t.IconButton,{openDelay:h,tooltip:"Table",onClick:()=>c("table"),className:"h-10 p-4",children:e.jsx(Ve,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Bar Chart",onClick:()=>c("bar"),className:"h-10 p-4",children:e.jsx(Re,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Line Chart",onClick:()=>c("line"),className:"h-10 p-4",children:e.jsx(Be,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Stacked Bar Chart",onClick:()=>c("stackedBar"),className:"h-10 p-4",children:e.jsx(et,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Stacked Line Chart",onClick:()=>c("stackedLine"),className:"h-10 p-4",children:e.jsx(Ft,{className:t.cn("size-5",a),strokeWidth:0})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Scatter Chart",onClick:()=>c("scatter"),className:"h-10 p-4",children:e.jsx(nt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Bubble Chart",onClick:()=>c("bubble"),className:"h-10 p-4",children:e.jsx(Ot,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Pie Chart",onClick:()=>c("pie"),className:"h-10 p-4",children:e.jsx(st,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Doughnut Chart",onClick:()=>c("doughnut"),className:"h-10 p-4",children:e.jsx(rt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Funnel Chart",onClick:()=>c("funnel"),className:"h-10 p-4",children:e.jsx(zt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Radar Chart",onClick:()=>c("radar"),className:"h-10 p-4",children:e.jsx(ut,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Polar Area Chart",onClick:()=>c("polarArea"),className:"h-10 p-4",children:e.jsx(Lt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Tornado Chart",onClick:()=>c("tornado"),className:"h-10 p-4",children:e.jsx(pt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{tooltip:"Range Chart",onClick:()=>c("range"),className:"h-10 p-4",children:e.jsx(tt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"KPI",onClick:()=>c("kpi"),className:"h-10 p-4",children:e.jsx(Rt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Text",onClick:()=>c("text"),className:"h-10 p-4",children:e.jsx(cs,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{tooltip:"Map",onClick:()=>c("map"),className:"h-10 p-4",children:e.jsx(dt,{className:t.cn("size-5",a)})}),e.jsx(t.IconButton,{openDelay:h,tooltip:"Custom",onClick:()=>c("custom"),className:"h-10 p-4",children:e.jsx(ht,{className:t.cn("size-5",a)})})]})}function Tn({chartType:s,className:a,customIcon:n,componentName:r}){const o=Mt();switch(s){case"bar":return e.jsx(Re,{className:a});case"line":return e.jsx(Be,{className:a});case"pie":return e.jsx(st,{className:a});case"radar":return e.jsx(ut,{className:a});case"scatter":return e.jsx(nt,{className:a});case"bubble":return e.jsx(Ot,{className:a});case"doughnut":return e.jsx(rt,{className:a});case"stackedBar":return e.jsx(et,{className:a});case"stackedLine":return e.jsx(Ft,{className:a});case"polarArea":return e.jsx(Lt,{className:a});case"funnel":return e.jsx(zt,{className:a});case"tornado":case"pyramid":return e.jsx(pt,{className:a});case"range":return e.jsx(tt,{className:a});case"kpi":return e.jsx(Rt,{className:a});case"text":return e.jsx(Us,{className:a});case"map":return e.jsx(dt,{className:a});case"custom":return n&&o?e.jsx(o,{className:a}):r?r==null?void 0:r.charAt(0).toUpperCase():e.jsx(ht,{className:a});default:return e.jsx(Ve,{className:a})}}function Mn(){var l,i;const[s,a]=T.useState(!1),n=t.useEditorStore(u=>u.card),r=n.type,o=t.useDashboardStore(u=>u.isVisualEditing);return e.jsxs(t.Popover,{open:s,onOpenChange:a,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.Button,{size:"sm",variant:o?"ghost":"outline",children:e.jsx(Tn,{className:"size-4",chartType:r,customIcon:(l=n.customCardPreferences)==null?void 0:l.icon,componentName:(i=n.customCardPreferences)==null?void 0:i.componentName})})}),e.jsxs(t.PopoverContentCustom,{onOpenAutoFocus:u=>u.preventDefault(),className:"w-50",children:[e.jsx(Bt,{onChartTypeChange:()=>a(!1)}),e.jsx(Dn,{onChartTypeChange:()=>a(!1)})]})]})}const ge="focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0";function Ze(s,a){var r,o,l;const n=t.getNumberAxis(s,{});return["pie","doughnut","polarArea","funnel"].includes(a)?"category":((l=(o=(r=s==null?void 0:s.chartOptions)==null?void 0:r.scales)==null?void 0:o[n])==null?void 0:l.type)||"linear"}function An(){var M,B,v,O,I,$;const[s,a]=T.useState(!1),n=t.useEditorStore(L=>{var z;return(z=L.card)==null?void 0:z.preferences}),r=t.useEditorStore(L=>{var z;return(z=L.card)==null?void 0:z.type}),o=t.getNumberAxis(n,{}),[l,i]=T.useState(Ze(n,r)),[u,c]=T.useState(((v=(B=(M=n==null?void 0:n.chartOptions)==null?void 0:M.scales)==null?void 0:B[o])==null?void 0:v.min)||""),[h,d]=T.useState((($=(I=(O=n==null?void 0:n.chartOptions)==null?void 0:O.scales)==null?void 0:I[o])==null?void 0:$.max)||""),[x,b]=T.useState(u||h?"custom":"auto"),[m,g]=T.useState(""),[p,y]=T.useState(0),[f,S]=T.useState(""),[C,A]=T.useState(""),[N,D]=T.useState(""),F=t.useEditorStore(L=>L.actions.setCardPreferences),{updateCardInFrame:R}=t.useEditorActions(),U=t.useEditorStore(L=>L.card),w=["pie","doughnut","polarArea","funnel"].includes(r);T.useEffect(()=>{var L,z,q,J,V,Q,k,H,K,X,he,xe,pe,fe;i(Ze(n,r)),c(((q=(z=(L=n==null?void 0:n.chartOptions)==null?void 0:L.scales)==null?void 0:z[o])==null?void 0:q.min)===0?0:""),d(((Q=(V=(J=n==null?void 0:n.chartOptions)==null?void 0:J.scales)==null?void 0:V[o])==null?void 0:Q.max)||""),g(((X=(K=(H=(k=n==null?void 0:n.chartOptions)==null?void 0:k.scales)==null?void 0:H[o])==null?void 0:K.ticks)==null?void 0:X.stepSize)||""),y(((he=n==null?void 0:n.numberAxisFormat)==null?void 0:he.decimalPlaces)||0),S(((xe=n==null?void 0:n.numberAxisFormat)==null?void 0:xe.suffix)||""),A(((pe=n==null?void 0:n.numberAxisFormat)==null?void 0:pe.currency)||""),D(((fe=n==null?void 0:n.numberAxisFormat)==null?void 0:fe.locale)||"")},[s,n,o,r]);function _(L,z){return z===""||/^[0-9\b]+$/.test(z)?Number(z):Number(L)}function G(){const L=[...new Set(t.LOCALE_CURRENCY_PAIRS.map(z=>z.currency))];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-6 items-center gap-2",children:[e.jsx(t.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(t.Select,{value:C||"none",onValueChange:z=>A(z==="none"?"":z),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Currency"}),e.jsx(t.SelectItem,{value:"none",children:"None"}),L.map(z=>e.jsx(t.SelectItem,{value:z,children:z},z))]})})]}),e.jsxs(t.Select,{value:N,onValueChange:z=>D(z),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Locale"}),t.LOCALE_CURRENCY_PAIRS.map(z=>e.jsx(t.SelectItem,{value:z.locale,children:z.locale},z.locale))]})})]})]}),e.jsxs("div",{className:"grid grid-cols-9 items-center gap-2",children:[e.jsx(t.Label,{className:"col-span-3",htmlFor:"format",children:"Decimals"}),e.jsx(t.Input,{value:p,onChange:z=>y(Number(z.target.value)),id:"format",className:`col-span-6 h-8 w-full tracking-wide ${ge}`})]})]})}function P(){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(t.Label,{htmlFor:"scale",children:"Type"}),e.jsxs(t.Select,{disabled:w,value:l,onValueChange:L=>i(L),children:[e.jsx(t.SelectTrigger,{className:"col-span-2 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{id:"type",children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Option"}),e.jsx(t.SelectItem,{value:"linear",children:"Linear"}),e.jsx(t.SelectItem,{value:"logarithmic",children:"Logarithmic"}),e.jsx(t.SelectItem,{value:"category",children:"Category"})]})})]})]}),l=="linear"&&e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2 py-1",children:[e.jsx(t.Label,{htmlFor:"scale-option",children:"Setup"}),e.jsxs(t.RadioGroup,{disabled:w,id:"scale-option",value:x,onValueChange:L=>b(L),className:"col-span-2 flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.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(t.RadioGroupItem,{value:"custom",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Custom"})]})]})]}),x==="custom"&&l=="linear"&&E()]})}function E(){return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(t.Label,{htmlFor:"range",children:"Range"}),e.jsx(t.Input,{id:"min",value:u,onChange:L=>c(z=>_(z,L.target.value)),className:`col-span-1 h-8 w-full ${ge}`}),e.jsx(t.Input,{value:h,onChange:L=>d(z=>_(z,L.target.value)),id:"max",className:`col-span-1 h-8 w-full ${ge}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-2",children:[e.jsx(t.Label,{htmlFor:"step-size",children:"Step Size"}),e.jsx(t.Input,{value:m,onChange:L=>g(z=>_(z,L.target.value)),id:"step-size",className:`col-span-1 h-8 w-full ${ge}`})]})]})}function j(){var J;let L={};x==="custom"&&(L={min:u,max:h,ticks:{stepSize:m}});const z={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,scales:{...(J=n==null?void 0:n.chartOptions)==null?void 0:J.scales,[o]:{type:l,...l==="linear"?L:{}}}},numberAxisFormat:{decimalPlaces:p,suffix:f,currency:C,locale:N}};console.log(z),F(z);const q={...U,preferences:z};R(q)}return e.jsxs(t.Popover,{onOpenChange:L=>a(L),children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Format Number",children:e.jsx(ys,{className:"size-4"})})}),e.jsx(t.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:[G(),P(),e.jsx(t.SelectSeparator,{}),e.jsx("div",{className:"grid grid-cols-3",children:e.jsx(t.Button,{onClick:j,variant:"secondary",className:"col-span-1 col-start-3 h-8 w-full",children:"Apply"})})]})]})})]})}function Ln(){const s=t.useEditorStore(u=>{var c,h,d,x;return(x=(d=(h=(c=u.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:d.legend)==null?void 0:x.position}),a=t.useEditorStore(u=>{var c,h,d,x;return(x=(d=(h=(c=u.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:d.legend)==null?void 0:x.align}),n=t.useEditorStore(u=>{var c,h,d,x;return(x=(d=(h=(c=u.card.preferences)==null?void 0:c.chartOptions)==null?void 0:h.plugins)==null?void 0:d.legend)==null?void 0:x.display}),{setLegendOptions:r}=t.useEditorActions(),o=u=>{r({display:u,position:s,align:a})},l=u=>{r({display:n,position:u,align:a})},i=u=>{r({display:n,position:s,align:u})};return e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Legend Settings",children:e.jsx(Es,{className:"size-4"})})}),e.jsx(t.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(t.Label,{htmlFor:"displayLegend",children:"Display"}),e.jsx("div",{className:"col-span-2 flex h-8 items-center",children:e.jsx(t.SmallSwitch,{checked:n,onCheckedChange:o,id:"displayLegend"})})]}),n&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"position",children:"Position"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(t.IconButton,{tooltip:"Top",onClick:()=>l("top"),children:e.jsx(t.ArrowUp,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Left",onClick:()=>l("left"),children:e.jsx(ns,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Bottom",onClick:()=>l("bottom"),children:e.jsx(Xe,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Right",onClick:()=>l("right"),children:e.jsx(as,{className:"size-4"})})]})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"align",children:"Align"}),e.jsxs("div",{className:"col-span-2 flex h-8 items-center",children:[e.jsx(t.IconButton,{tooltip:"Start",onClick:()=>i("start"),children:e.jsx(Zt,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"Center",onClick:()=>i("center"),children:e.jsx(Yt,{className:"size-4"})}),e.jsx(t.IconButton,{tooltip:"End",onClick:()=>i("end"),children:e.jsx(Xt,{className:"size-4"})})]})]})]})]})]})})]})}function On(){const s=t.useEditorStore(l=>{var i,u;return(u=(i=l.card.preferences)==null?void 0:i.chartOptions)==null?void 0:u.indexAxis}),a=t.useEditorStore(l=>l.card.type),{setChartOrientation:n}=t.useEditorStore(l=>l.actions);function r(){return e.jsx(t.IconButton,{onClick:()=>{n(s==="x"||s===void 0?"y":"x")},tooltip:"Chart Orientation",children:s==="x"||s===void 0?e.jsx(Qe,{className:"size-4"}):e.jsx($e,{className:"size-4"})})}function o(){return e.jsx(t.IconButton,{onClick:()=>{n(s==="y"||s===void 0?"x":"y")},tooltip:"Chart Orientation",children:s==="y"||s===void 0?e.jsx(Qe,{className:"size-4"}):e.jsx($e,{className:"size-4"})})}return e.jsx(e.Fragment,{children:a==="funnel"?o():r()})}function zn(){const s=t.useEditorStore(i=>i.card),a=t.useEditorStore(i=>i.isShowingVisual),n=t.useEditorStore(i=>{var u,c;return(c=(u=i.card)==null?void 0:u.preferences)==null?void 0:c.chartOptions}),r=t.getChartGridStyle(n),{setChartGrid:o}=t.useEditorActions();function l(){const i=["xy","none","x","y"],c=(i.indexOf(r)+1)%i.length;o(i[c])}return a?e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel"].includes(s.type)&&e.jsx(On,{}),!["kpi","text","custom","table"].includes(s.type)&&e.jsxs(e.Fragment,{children:[e.jsx(An,{}),e.jsx(Ln,{})]}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(s.type)&&e.jsxs(t.IconButton,{tooltip:"Grid Style",onClick:l,children:[(r==="xy"||!r)&&e.jsx(ks,{className:"size-4"}),r==="x"&&e.jsx(ps,{className:"size-4"}),r==="y"&&e.jsx(Bs,{className:"size-4"}),r==="none"&&e.jsx(Ps,{className:"size-4"})]})]}):null}function je(){var B;const s=t.useEditorStore(v=>v.frame),a=t.useEditorStore(v=>v.card),n=t.useEditorStore(v=>v.isDevMode),r=t.useEditorStore(v=>v.isShowingVisual),o=t.useDashboardStore(v=>v.isVisualEditing),l=t.useEditorStore(v=>v.isSqlRunning),i=t.useEditorStore(v=>v.pythonStdOut),u=t.useEditorStore(v=>{var O;return(O=v.card.preferences)==null?void 0:O.chartOptions});t.useEditorStore(v=>v.actions.setRunSql),t.getChartGridStyle(u);const{setFrame:c,setCard:h,updateCardInFrame:d,setActiveTabCardId:x,runAssistant:b,setChartGrid:m}=t.useEditorActions(),{data:g,isLoading:p,isFetching:y,error:f,isPending:S}=t.useDashboardCardQuery(a);if(!a)return e.jsx(e.Fragment,{children:"No Active Card"});function C(v){const O={connectionId:v.connectionId,lastSelectedDatabase:v.lastSelectedDatabase,lastSelectedSchema:v.lastSelectedSchema,lastSelectedTable:v.lastSelectedTable,lastSelectedDatamodelId:v.lastSelectedDatamodelId,id:t.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]};A(v,O)}function A(v,O){const I={...s,cards:[...s.cards,O],activeCardId:v.id};c(I),x(O.id)}function N(){const v={...a,id:t.v4(),title:`${a.title} Copy`,tabTitle:`${a.tabTitle||a.title} Copy`};A(a,v)}function D(v){const O=s.cards.filter(L=>L.id!==v),I=O[0],$={...s,cards:O,activeCardId:I.id};c($),x(I.id),h(I)}function F(v){x(v)}const R=(v,O)=>{console.log("handleDragStart",O),v.dataTransfer.setData("tabIndex",O.toString())},U=v=>{v.preventDefault()},w=(v,O)=>{const I=v.dataTransfer.getData("tabIndex");if(I===O.toString())return;const $=Array.from(s.cards),[L]=$.splice(parseInt(I),1);$.splice(O,0,L),c({...s,cards:$})};function _(v,O){return v.cards.length===1&&(O.displayTab===!0||O.displayTab===void 0)||v.activeCardId!==O.id&&(O.displayTab===!0||O.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const I=O.displayTab===void 0?!1:!O.displayTab;h({...O,displayTab:I}),d({...O,displayTab:I})},children:O.displayTab===!0||O.displayTab===void 0?e.jsx(t.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(vs,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}function G(){return e.jsxs(t.Tabs,{onValueChange:F,value:s.activeCardId,className:"flex grow flex-col",children:[s.cards.map(v=>e.jsx(t.TabsContent,{className:"mt-0 grow",value:v.id,children:P()},v.id)),e.jsx(t.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[s.cards.map((v,O)=>e.jsxs(t.TabsTrigger,{draggable:!0,onDragStart:I=>R(I,O),onDragOver:U,onDrop:I=>w(I,O),className:"",value:v.id,children:[_(s,v),e.jsx(t.Editable,{enabled:s.activeCardId===v.id,text:v.tabTitle||v.title,onSave:I=>{h({...a,tabTitle:I}),d({...a,tabTitle:I})},children:v.tabTitle||v.title},v.id),s.activeCardId===v.id&&s.cards.length>1&&e.jsx(t.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>D(v.id)})]},v.id)),e.jsx(t.Button,{className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",variant:"outline",onClick:()=>C(a),children:e.jsx(t.Plus,{className:"h-4 w-4"})}),e.jsx(t.Button,{className:"invisible ml-1 h-8 px-2 hover:bg-muted-foreground/50 group-hover:visible",variant:"outline",onClick:N,children:e.jsx(gs,{className:"h-4 w-4"})})]})})]})}function P(){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(t.KPICard,{card:a,data:g==null?void 0:g.records,isPending:S})})}):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(t.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(t.TextCard,{isLoading:l,card:a,data:g==null?void 0:g.records})})})}):e.jsx(t.ErrorBoundary,{FallbackComponent:t.FallbackEditorCard,children:e.jsx(Tt,{className:"grow border-none shadow-none"})})}function E(){const v=JSON.stringify(g==null?void 0:g.records,null,2),O=new Blob([v],{type:"application/json"}),I=URL.createObjectURL(O),$=document.createElement("a");$.href=I,$.download=`${a.title}.json`,$.click()}function j(){const O=[Object.keys(g==null?void 0:g.records.reduce((z,q)=>({...z,...q}),{})).join(","),...((g==null?void 0:g.records)||[]).map(z=>Object.values(z).join(","))].join(`
|
|
372
|
+
`),I=new Blob([O],{type:"text/csv"}),$=URL.createObjectURL(I),L=document.createElement("a");L.href=$,L.download=`${a.title}.csv`,L.click()}function M(){const v=`Can you get the documentation for ${a.type} chart?`;b(v)}return e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[(p||y)&&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(ae,{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(Mn,{}),o&&e.jsx(t.IconButton,{tooltip:"Visual Docs",onClick:M,children:e.jsx(ls,{className:"size-4"})}),!["table","custom"].includes(a.type)&&o&&e.jsx(kn,{})]}),!r&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(t.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:j,children:e.jsx(En,{className:"size-4"})}),e.jsx(t.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:E,children:e.jsx(Ss,{strokeWidth:1.5,className:"size-4"})}),e.jsx(t.CopyButton,{text:JSON.stringify(g==null?void 0:g.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsx(zn,{}),n&&e.jsx(Rn,{})]}),r?G():e.jsxs(t.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[i&&e.jsxs(e.Fragment,{children:[e.jsx(t.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:i&&e.jsx(Fn,{})}),e.jsx(t.ResizableHandle,{})]}),e.jsxs(t.ResizablePanel,{id:"2",order:2,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:[(g==null?void 0:g.records)&&(g==null?void 0:g.records.length)>0&&e.jsx(vn,{data:(g==null?void 0:g.records)||[]}),(f||(g==null?void 0:g.error))&&e.jsx("span",{className:"font-mono text-xs",children:(f==null?void 0:f.message)||((B=g==null?void 0:g.error)==null?void 0:B.message)})]})]})]})}function Fn(){const s=t.useEditorStore(n=>n.pythonStdOut);if(!s)return null;const a=s.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(t.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(t.ScrollBar,{orientation:"horizontal"})]})]})}function Rn(){const s=t.useEditorStore(n=>n.isShowingVisual),a=t.useEditorStore(n=>n.actions.setIsShowingVisual);return e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.Label,{htmlFor:"visualize",children:"Chart"}),e.jsx(t.SmallSwitch,{checked:s,onCheckedChange:()=>a(!s),id:"visualize",className:"data-[state=checked]:bg-foreground/50"})]})}function Bn(){var p,y;const s=t.useEditorStore(f=>f.card.customCardPreferences),{data:a}=At(),n=(a==null?void 0:a.plugins)||[],r=t.useEditorStore(f=>f.card),o=t.useEditorStore(f=>f.frame),l=(s==null?void 0:s.visualType)||"single",{setCustomCardPreferences:i}=t.useEditorActions(),{components:u,manifest:c}=t.useCustomVisual((s==null?void 0:s.url)||""),h=(p=c==null?void 0:c.visuals)==null?void 0:p.find(f=>f.name===(s==null?void 0:s.componentName));function d(f,S){if(!s)return;const A={...(s==null?void 0:s.dataInputCardIds)||{},[f]:{cardId:S,hookRef:null}};i({...s,dataInputCardIds:A})}function x(f){var C,A;const S=(C=o==null?void 0:o.cards)==null?void 0:C.filter(N=>{var D;return((D=N==null?void 0:N.customCardPreferences)==null?void 0:D.visualType)!=="multiple"}).map(N=>({value:N.id,label:N.tabTitle||N.title}));return e.jsx("div",{className:"space-y-2",children:(A=f==null?void 0:f.dataInputs)==null?void 0:A.map((N,D)=>{var F,R;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:N}),e.jsx(Y,{className:"col-span-6 h-8 w-full justify-between",label:"",options:S||[],value:((R=(F=s==null?void 0:s.dataInputCardIds)==null?void 0:F[D])==null?void 0:R.cardId)||"",onValueChange:U=>d(D,U)})]},D+Math.random())})})}function b(f,S,C=[],A=""){var N,D;return s?f==="input"?e.jsx(t.Input,{className:`h-8 w-full ${t.removeRing}`,value:((N=s==null?void 0:s.settings)==null?void 0:N[S])||A,onChange:F=>{i({...s,settings:{...s==null?void 0:s.settings,[S]:F.target.value}})}},S):f==="select"?e.jsx(Y,{className:"h-8 w-full",label:"",options:C,value:((D=s==null?void 0:s.settings)==null?void 0:D[S])||A,onValueChange:F=>{i({...s,settings:{...s==null?void 0:s.settings,[S]:F}})}}):null:null}function m(f){const S=Object.entries((f==null?void 0:f.settings)||{});return e.jsx("div",{className:"space-y-3",children:S.map(([C,A])=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:A.title}),b(A.ui,C,A.options,A.defaultValue)]},C))})}function g(f){s&&i({...s,visualType:f,componentName:""})}return e.jsxs(t.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"px-6 py-2",children:[e.jsxs(t.AccordionItem,{value:"plugin-settings",children:[e.jsx(t.AccordionTrigger,{children:"Plugin"}),e.jsx(t.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(Y,{icon:e.jsx(is,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:n,value:(s==null?void 0:s.url)||"",onValueChange:f=>i({...s||{url:"",componentName:""},url:f})}),((s==null?void 0:s.type)==="component"||(s==null?void 0:s.type)===void 0)&&e.jsx(t.Tabs,{value:l,onValueChange:g,children:e.jsxs(t.TabsList,{className:"h-9 w-full",children:[e.jsx(t.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(t.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((s==null?void 0:s.type)==="component"||(s==null?void 0:s.type)===void 0)&&s&&e.jsx(e.Fragment,{children:u&&e.jsx(Y,{icon:e.jsx(fs,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(u==null?void 0:u.filter(f=>f.type===l).map(f=>({value:f.name,label:f.name})))||[],value:s.componentName,onValueChange:f=>{console.log("changing value",f),i({...s,componentName:f})}})})]})})]}),((y=r==null?void 0:r.customCardPreferences)==null?void 0:y.componentName)&&e.jsxs(t.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{children:[h&&h.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(t.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:x(h)})]}),h&&h.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:m(h)})})]})]})]})}function oe(){const[s,a]=T.useState([]),n=t.useEditorStore(h=>h.card.preferences),{setFilterOnClickColumnIndex:r,setCardPreferences:o}=t.useEditorStore(h=>h.actions),l=t.useEditorStore(h=>h.queryResultColumns);t.useEditorStore(h=>{var d;return(d=h.card.preferences)==null?void 0:d.filterOnClickColumnIndex}),T.useEffect(()=>{l&&a(new Array(l.length).fill(!1))},[l]);function i(h,d){var x,b;d?n!=null&&n.onClickFilter&&((x=n==null?void 0:n.onClickFilter)==null?void 0:x.length)>0?o({...n,onClickFilter:[...n.onClickFilter,{columnIndex:h,expression:""}]}):o({...n,onClickFilter:[{columnIndex:h,expression:""}]}):(o({...n,onClickFilter:(b=n==null?void 0:n.onClickFilter)==null?void 0:b.filter(m=>m.columnIndex!==h)}),a(m=>{const g=[...m];return g[h]=!1,g}))}function u(h,d){var b;const x=(b=n==null?void 0:n.onClickFilter)==null?void 0:b.map(m=>m.columnIndex===h?{...m,expression:d.target.value}:m);o({...n,onClickFilter:x})}function c(h){var d,x,b,m;if(s!=null&&s[h]||(x=(d=n==null?void 0:n.onClickFilter)==null?void 0:d.find(g=>g.columnIndex===h))!=null&&x.expression)return e.jsx(t.Input,{value:(m=(b=n==null?void 0:n.onClickFilter)==null?void 0:b.find(g=>g.columnIndex===h))==null?void 0:m.expression,onChange:g=>u(h,g),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(t.Label,{children:"On Click"}),l==null?void 0:l.map((h,d)=>{var x,b;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(t.Checkbox,{checked:!!((x=n==null?void 0:n.onClickFilter)!=null&&x.find(m=>m.columnIndex===d)),onCheckedChange:m=>i(d,m)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:h}),e.jsx("div",{className:"flex items-center gap-2",children:((b=n==null?void 0:n.onClickFilter)==null?void 0:b.find(m=>m.columnIndex===d))&&e.jsx(t.Pencil,{onClick:()=>{a(m=>{const g=[...m];return g[d]=!m[d],g})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),c(d)]})},h)})]})}function Vn({idx:s}){var u;const a=t.useEditorStore(c=>{var h;return(h=c.card)==null?void 0:h.preferences}),n=t.useEditorStore(c=>{var h;return(h=c.card.preferences)==null?void 0:h.datasetOptions}),r=t.useEditorStore(c=>c.actions.setCardPreferences),o=(u=a==null?void 0:a.chartOptions)==null?void 0:u.indexAxis,l=n==null?void 0:n.find(c=>c.idx===s);function i(c,h){(c==="end"||c==="start")&&t.getContrastColor(t.getCanvasBackgroundColor());const d={display:c!=="none",align:c,anchor:c,clamp:!0};if(!l)r({...a,datasetOptions:[...n||[],{idx:h,datalabels:d}]});else{const x={...l};x.datalabels=d;const b=n==null?void 0:n.map(m=>m.idx===h?x:m);r({...a,datasetOptions:b})}}return e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{children:e.jsx(qs,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(t.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>i("none",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(t.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>i("start",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Ts,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})}),e.jsx("button",{onClick:()=>i("center",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(As,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})}),e.jsx("button",{onClick:()=>i("end",s),className:"h-7 p-1 text-xs font-normal",children:e.jsx(Ms,{className:t.cn("h-4 w-4",{"rotate-90":o==="y"})})})]})})]})}function Vt(){const s=t.useEditorStore(l=>l.card),{data:a}=t.useDashboardCardQuery(s),{keys:n}=t.getKeys((a==null?void 0:a.records)||[]),r=n.slice(1).filter(l=>!t.METADATA_KEYS.includes(l));let o=r;if(["stackedBar","stackedLine"].includes(s.type)){const l=t.pivotData({dataArray:(a==null?void 0:a.records)||[],groupKey:n[0],pivotKey:n[1],valueKey:n[2]}),{keys:i}=t.getKeys(l);o=i.slice(1)}return{transformedMetricKeys:o,dimensionKeys:n.slice(0,1),keys:n,defaultMetricKeys:r}}function Ne({children:s,className:a,delayDuration:n}){return e.jsx(t.TooltipProvider$1,{children:e.jsxs(t.Tooltip,{delayDuration:n,children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:t.cn("overflow-hidden text-ellipsis whitespace-nowrap",a),children:s})}),e.jsx(t.TooltipContent,{children:s})]})})}function _t(){const s=t.useEditorStore(c=>c.card),a=t.useEditorStore(c=>{var h,d,x;return(x=(d=(h=c==null?void 0:c.card)==null?void 0:h.preferences)==null?void 0:d.chartOptions)==null?void 0:x.indexAxis}),{data:n}=t.useDashboardCardQuery(s),r=a==="y"?"Metric":"Dimension",o=a==="y"?"Dimension":"Metric",{dimensionKeys:l,transformedMetricKeys:i,keys:u}=Vt();return u.length===0?null:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:r}),e.jsx("div",{className:"max-h-[125px] space-y-1 overflow-y-auto",children:l.map(c=>e.jsx("p",{className:"flex h-8 items-center rounded-md bg-muted/70 px-3 hover:bg-muted",title:c,children:Pt(c)},c))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:o}),e.jsx("div",{className:"my-1 max-h-[125px] space-y-1 overflow-y-auto",children:i.map((c,h)=>e.jsx(qt,{className:"flex h-8 items-center bg-muted/70 px-3 hover:bg-muted",iconClassName:"invisible group-hover:visible",col:c,idx:h},c))})]})]})}function Pt(s,a=30){return s.length>a?s.slice(0,a)+"...":s}function qt({col:s,idx:a,className:n,iconClassName:r,children:o}){const l=t.useEditorStore(d=>{var x;return(x=d.card)==null?void 0:x.preferences}),i=t.useEditorStore(d=>{var x,b;return(b=(x=d.card)==null?void 0:x.preferences)==null?void 0:b.datasetOptions}),u=i==null?void 0:i.find(d=>d.idx===a),{setCardPreferences:c}=t.useEditorStore(d=>d.actions);function h(d){if(!u)c({...l,datasetOptions:[...i||[],{idx:a,type:d==="area"?"line":d,fill:d==="area"?"origin":""}]});else{const x={...u};x.type=d==="area"?"line":d,x.fill=d==="area"?"origin":"";const b=i==null?void 0:i.map(m=>m.idx===a?x:m);c({...l,datasetOptions:b})}}return e.jsxs("div",{className:t.cn("group flex items-center justify-between gap-2 rounded-md",n),children:[o||e.jsx(Ne,{className:"max-w-[100px]",title:s,children:Pt(s)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(Re,{onClick:()=>h("bar"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Be,{onClick:()=>h("line"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(ms,{onClick:()=>h("area"),className:t.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",r)}),e.jsx(Vn,{idx:a})]})]})}function Ht(){const s=t.useEditorStore(m=>m.selectedConnectionId),a=t.useEditorStore(m=>m.selectedDatamodelId),n=t.useEditorStore(m=>m.selectedDatabaseName),r=t.useEditorStore(m=>m.selectedSchemaName),o=t.useEditorStore(m=>m.selectedTableName),l=t.useDashboardStore(m=>m.dashboard.filters)||[],i=t.useDashboardStore(m=>m.actions.addFilter),u=t.useDashboardStore(m=>m.actions.removeFilter),{removeFilterValues:c}=t.useDashboardActions(),{modelItems:h,tableName:d,connectionType:x}=t.useEditorAside();function b(m,g="dashboard"){var F;const p=t.fmt(m.column_name),y=t.fmt(n||""),f=t.fmt(r||""),S=t.fmt(d);let C=t.getQualifiedTableName({schemaName:f,tableName:S,connectionType:x,databaseName:y}),A=`${C}.${p}`;const N=(F=h==null?void 0:h.find(R=>R.id===a))==null?void 0:F.name;a&&a!=="none"&&(A=`${t.DATAMODEL_NAMESPCACE}.${N}.${p}`,C=`${t.DATAMODEL_NAMESPCACE}.${N}`);const D=l==null?void 0:l.find(R=>R.column===A&&R.table===S&&(R.database===y||R.database===n));if(D)c(D.id),u(D.id);else{const R=t.v4();return d==="api"&&s?(i({location:g,id:R,column:A,title:m.column_name,dataType:m.data_type,table:"api",database:"",connectionId:s,operation:"in",sql:t.getDefaultFilterSql(m.data_type,C,p)}),R):(i({id:R,location:g,column:A,title:m.column_name,dataType:m.data_type,table:o||"",database:n||"",connectionId:s||"",operation:"in",sql:t.getDefaultFilterSql(m.data_type,C,p)}),R)}}return{assignFilter:b}}function de(){const s=t.useDashboardStore(u=>u.dashboard.filters),a=t.useEditorStore(u=>u.frame),{setFrame:n}=t.useEditorActions(),{columns:r}=t.useEditorAside(),{assignFilter:o}=Ht();function l(u){const c=o(u,"frame");n({...a,filterId:c})}const i=s==null?void 0:s.find(u=>u.id===a.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(t.Label,{children:"Card Filter"}),e.jsx(_n,{onSelect:l,columns:r}),i&&e.jsx(t.FilterHeader,{filter:i})]})}function _n({columns:s,onSelect:a}){var i;const[n,r]=le.useState(!1),[o,l]=le.useState("");return e.jsxs(t.Popover,{open:n,onOpenChange:r,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":n,className:"w-full justify-between font-normal",children:[o?(i=s.find(u=>u.column_name===o))==null?void 0:i.column_name:"Select Column...",e.jsx(t.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(t.Command,{children:[e.jsx(t.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(t.CommandList,{children:[e.jsx(t.CommandEmpty,{children:"No framework found."}),e.jsx(t.CommandGroup,{children:s==null?void 0:s.map(u=>e.jsxs(t.CommandItem,{value:u.column_name,onSelect:c=>{const h=c===o?"":c;l(h),r(!1),h&&a(u)},children:[u.column_name,e.jsx(t.CheckIcon,{className:t.cn("ml-auto h-4 w-4",o===u.column_name?"opacity-100":"opacity-0")})]},u.column_name))})]})]})})]})}function Pn({cardType:s}){return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} Chart`}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsx(t.AccordionContent,{className:"space-y-4 px-2",children:e.jsx(_t,{})})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function He(){var o,l,i;const s=t.useEditorStore(u=>u.card.type),a=t.useEditorStore(u=>u.card.preferences),{setCardPreferences:n}=t.useEditorStore(u=>u.actions);function r(u){var d;const c={datalabels:{display:u!=="none",align:u,anchor:u,clamp:!0}},h={...a,chartOptions:{...a==null?void 0:a.chartOptions,plugins:{...(d=a==null?void 0:a.chartOptions)==null?void 0:d.plugins,...c}}};n(h),console.log(u)}return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(t.Select,{value:((i=(l=(o=a==null?void 0:a.chartOptions)==null?void 0:o.plugins)==null?void 0:l.datalabels)==null?void 0:i.anchor)||"none",onValueChange:r,defaultValue:"none",children:[e.jsx(t.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{placeholder:"Select position"})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Data Labels"}),e.jsx(t.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(s)&&e.jsx(t.SelectItem,{value:"start",children:"Start"}),e.jsx(t.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(s)&&e.jsx(t.SelectItem,{value:"end",children:"End"})]})})]})]})}function qn({cardType:s}){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:l=>l.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(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} Chart`}),e.jsx(t.AccordionContent,{className:"space-y-4 font-mono text-xs",children:a(s)})]}),e.jsxs(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsx(t.AccordionContent,{className:"px-2",children:e.jsx(He,{})})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}const Hn={datasets:[{type:"line",label:"Sum_sales",data:[]},{type:"bar",label:"Sum_profit",data:[]}]},Gn={datasets:[{fill:"origin",type:"line",label:"Sum_sales",data:[]}]};function Un(){const s=t.useEditorStore(a=>a.card.type);return e.jsxs("div",{className:"space-y-6 p-6",children:[e.jsx(t.Accordion,{className:" ",type:"single",collapsible:!0,children:$n(s)}),!["kpi"].includes(s)&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(He,{}),e.jsx(oe,{}),e.jsx(de,{})]})]})}function $n(s){if(["line","bar"].includes(s))return e.jsxs(e.Fragment,{children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsx("p",{children:"Chart Query"})}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:"Mixed Chart"}),e.jsxs(t.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(Hn,null,2)})]})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:"Area Chart"}),e.jsxs(t.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(Gn,null,2)})]})]})]});if(s==="bubble")return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:s})," chart query?"]})}),e.jsxs(t.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(s==="scatter")return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:["How to create a ",e.jsx("span",{children:s})," chart query?"]})}),e.jsxs(t.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(s))return e.jsx(e.Fragment,{children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"font-normal",children:e.jsxs("p",{children:[e.jsx("span",{children:s==="polarArea"?"Polar":s.charAt(0).toUpperCase()+s.slice(1)})," ","chart query"]})}),e.jsxs(t.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 Qn(){var c,h,d,x,b;const s=t.useEditorStore(m=>m.card.preferences),a=t.useEditorStore(m=>m.actions.setCardPreferences),n=t.useEditorStore(m=>m.actions.setNumberFormat);t.useEditorStore(m=>{var g,p;return(p=(g=m.card.preferences)==null?void 0:g.formatNumber)==null?void 0:p.decimalPlaces});const r=t.useEditorStore(m=>{var g,p;return(p=(g=m.card.preferences)==null?void 0:g.formatNumber)==null?void 0:p.locale}),o=t.useEditorStore(m=>{var g,p;return(p=(g=m.card.preferences)==null?void 0:g.formatNumber)==null?void 0:p.currency});t.useEditorStore(m=>m.actions.setFilterOnClickField);const l=t.useEditorStore(m=>{var g,p;return(p=(g=m.card.preferences)==null?void 0:g.formatNumber)==null?void 0:p.colorRanges})||[],i=t.useEditorStore(m=>m.actions.setColorRanges),u=[...new Set(t.LOCALE_CURRENCY_PAIRS.map(m=>m.currency))];return e.jsxs("div",{children:[e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsx(t.Checkbox,{checked:!!((c=s==null?void 0:s.formatNumber)!=null&&c.enabled),onCheckedChange:m=>{a({...s,formatNumber:{...s==null?void 0:s.formatNumber,enabled:m}})}}),e.jsx(t.Label,{children:"Format Number"})]}),((h=s==null?void 0:s.formatNumber)==null?void 0:h.enabled)===!0&&e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx("div",{children:e.jsxs("div",{className:"space-y-4 p-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Locale"}),e.jsxs(t.Select,{value:r||"none",onValueChange:m=>{var g,p,y;return n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,((p=s==null?void 0:s.formatNumber)==null?void 0:p.currency)||"",m,((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Format"}),e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),t.LOCALE_CURRENCY_PAIRS.map(m=>e.jsx(t.SelectItem,{value:m.locale,children:m.locale},m.locale))]})})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Currency"}),e.jsxs(t.Select,{value:o||"none",onValueChange:m=>{var g,p,y;console.log("currency",m),n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,m==="none"?"":m,((p=s==null?void 0:s.formatNumber)==null?void 0:p.locale)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:"Currency"}),e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),u.map(m=>e.jsx(t.SelectItem,{value:m,children:m},m))]})})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Decimals"}),e.jsxs(t.Select,{value:((x=(d=s==null?void 0:s.formatNumber)==null?void 0:d.decimalPlaces)==null?void 0:x.toString())||"0",onValueChange:m=>{var g,p,y;return n(Number(m),((g=s==null?void 0:s.formatNumber)==null?void 0:g.currency)||"",((p=s==null?void 0:s.formatNumber)==null?void 0:p.locale)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.suffix)||"")},children:[e.jsx(t.SelectTrigger,{className:"col-span-3 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(t.SelectValue,{})}),e.jsx(t.SelectContent,{children:["0","1","2","3","4"].map(m=>e.jsx(t.SelectItem,{value:m,children:m},m))})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(t.Label,{children:"Suffix"}),e.jsx(t.Input,{value:((b=s==null?void 0:s.formatNumber)==null?void 0:b.suffix)||"",className:"h-8 font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"K, M, B, %",onChange:m=>{var g,p,y;return n(((g=s==null?void 0:s.formatNumber)==null?void 0:g.decimalPlaces)||0,((p=s==null?void 0:s.formatNumber)==null?void 0:p.currency)||"",((y=s==null?void 0:s.formatNumber)==null?void 0:y.locale)||"",m.target.value)}})]})]})]})}),e.jsx(t.Separator,{className:"my-3"}),e.jsx(t.ColorFormat,{colorRanges:l,setColorRanges:i})]})]})}function Jn(){return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(t.Accordion,{type:"single",collapsible:!0,children:e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{className:"",children:"KPI Query"}),e.jsxs(t.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:s=>s.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(Qn,{})}),e.jsx("div",{}),e.jsx("div",{})]})}function Me({children:s,summary:a,open:n,className:r}){return e.jsxs("details",{open:n,className:t.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(t.Label,{children:a}),e.jsx(t.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:s})]})}const Wn=[{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"}],Kn=["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"],Yn=Kn.map(s=>({label:s,value:s}));function Zn(){var c,h,d,x,b,m,g,p,y,f,S,C,A,N,D,F,R,U;const s=t.useEditorStore(w=>w.card),a=(h=(c=s==null?void 0:s.preferences)==null?void 0:c.mapVisualOptions)==null?void 0:h.topoJsonUrl,[n,r]=T.useState(((x=(d=s==null?void 0:s.preferences)==null?void 0:d.mapVisualOptions)==null?void 0:x.customTopoJsonUrl)||""),{setCardPreferences:o}=t.useEditorActions(),{data:l}=t.useTopoJson(s),i=Object.keys((l==null?void 0:l.objects)||{}).map(w=>({label:w,value:w}))||[],u=[{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(Me,{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(t.Separator,{className:"my-4"}),e.jsx(Me,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(Y,{className:"h-9",options:u,onValueChange:w=>{console.log("value",w),o({...s.preferences,mapVisualOptions:{topoJsonUrl:w}})},value:a||"",label:"Select Map"}),a==="custom"&&e.jsx(t.Input,{onChange:w=>{r(w.target.value)},onBlur:()=>{var w;console.log("onBlur",n),o({...s.preferences,mapVisualOptions:{...(w=s.preferences)==null?void 0:w.mapVisualOptions,customTopoJsonUrl:n}})},value:n,className:`h-9 w-full ${t.removeRing}`,placeholder:"Custom URL"}),l&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Map Object"}),e.jsx(Y,{className:"h-9",options:i,value:((m=(b=s.preferences)==null?void 0:b.mapVisualOptions)==null?void 0:m.objectKey)||"",label:"Map Object",onValueChange:w=>{var _;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,objectKey:w}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Projection"}),e.jsx(Y,{className:"h-9",options:Wn,value:((p=(g=s.preferences)==null?void 0:g.mapVisualOptions)==null?void 0:p.projection)||"",label:"Projection",onValueChange:w=>{var _;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,projection:w}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(t.Label,{children:"Colors"}),e.jsx(Y,{className:"h-9",options:Yn,value:((f=(y=s.preferences)==null?void 0:y.mapVisualOptions)==null?void 0:f.colorScale)||"",label:"Select Color Scale",onValueChange:w=>{var _;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,colorScale:w}})}})]})]})]})}),e.jsx(t.Separator,{className:"my-4"}),e.jsx(Me,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:.1,value:((C=(S=s.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:C.projectionScale)||1,onChange:w=>{var _;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,projectionScale:Number(w.target.value)}})}}),e.jsx(t.Label,{children:"X"}),e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:10,value:((D=(N=(A=s.preferences)==null?void 0:A.mapVisualOptions)==null?void 0:N.projectionOffset)==null?void 0:D[0])||0,onChange:w=>{var _,G,P,E;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,projectionOffset:[Number(w.target.value),((E=(P=(G=s.preferences)==null?void 0:G.mapVisualOptions)==null?void 0:P.projectionOffset)==null?void 0:E[1])||0]}})}}),e.jsx(t.Label,{children:"Y"}),e.jsx(t.Input,{className:`h-9 w-full ${t.removeRing}`,type:"number",step:10,value:((U=(R=(F=s.preferences)==null?void 0:F.mapVisualOptions)==null?void 0:R.projectionOffset)==null?void 0:U[1])||0,onChange:w=>{var _,G,P,E;o({...s.preferences,mapVisualOptions:{...(_=s.preferences)==null?void 0:_.mapVisualOptions,projectionOffset:[((E=(P=(G=s.preferences)==null?void 0:G.mapVisualOptions)==null?void 0:P.projectionOffset)==null?void 0:E[0])||0,Number(w.target.value)]}})}})]})}),e.jsx(t.Separator,{className:"my-4"}),e.jsx(oe,{})]})}function Xn(){var r;const s=t.useEditorStore(o=>o.card.preferences),{setCardPreferences:a}=t.useEditorStore(o=>o.actions);function n(o){a({...s,textVisualOptions:{...s==null?void 0:s.textVisualOptions,isDynamicText:o==="dynamic"}})}return e.jsxs(t.RadioGroup,{value:(r=s==null?void 0:s.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(t.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(t.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function ye({cardType:s,docContent:a,suffix:n="Chart"}){let r="pie-donut-polar";return s==="funnel"&&(r="funnel"),e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:`${t.titleCase(s)} ${n}`}),e.jsx(t.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:o=>o.stopPropagation(),className:"text-blue-700",href:`https://docs.semaphor.cloud/docs/charts/${r}`,children:"example"})," in the docs."]})]})})]}),e.jsxs(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[s!=="text"&&e.jsx(He,{}),s==="text"&&e.jsx(Xn,{})]})]}),!["range","text"].includes(s)&&e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function ea(){const s=t.useEditorStore(r=>{var o,l;return(l=(o=r==null?void 0:r.card)==null?void 0:o.preferences)==null?void 0:l.sortChart}),{setSortChart:a}=t.useEditorActions();function n(){return e.jsxs("div",{className:"space-y-2",children:[e.jsx(t.Label,{children:"Sort"}),e.jsx(Y,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"ASC",value:"asc"},{label:"DESC",value:"desc"}],value:s||"none",onValueChange:r=>{a(r)}})]})}return e.jsx("div",{className:"space-y-6 px-6 py-2",children:e.jsxs(t.Accordion,{defaultValue:["item-2"],type:"multiple",children:[e.jsxs(t.AccordionItem,{value:"item-1",children:[e.jsx(t.AccordionTrigger,{children:"Stacked Chart"}),e.jsxs(t.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(t.AccordionItem,{value:"item-2",children:[e.jsx(t.AccordionTrigger,{children:"Settings"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[n(),e.jsx(_t,{})]})]}),e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})]})})}function ta({cardType:s="table"}){console.log("cardType",s);const a=t.useEditorStore(o=>{var l;return(l=o.card)==null?void 0:l.preferences}),n=t.useEditorStore(o=>o.actions.setCardPreferences),r=o=>{n({...a,allowDownload:o})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(t.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(t.AccordionItem,{value:"item-3",children:[e.jsx(t.AccordionTrigger,{children:"Filters"}),e.jsxs(t.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(oe,{}),e.jsx(de,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(t.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 sa=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/tornado",children:"example"})," in the docs."]})]}),na=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/range",children:"example"})," in the docs."]})]}),aa=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:s=>s.stopPropagation(),className:"text-blue-700",href:"https://docs.semaphor.cloud/docs/charts/text",children:"example"})," in the docs."]})]});function ra(){const s=t.useEditorStore(n=>n.card.type);function a(n){switch(n){case"stackedBar":case"stackedLine":return e.jsx(ea,{});case"tornado":case"pyramid":return e.jsx(ye,{docContent:sa,cardType:n});case"kpi":return e.jsx(Jn,{});case"line":case"bar":return e.jsx(Pn,{cardType:n});case"range":return e.jsx(ye,{docContent:na,cardType:n});case"pie":case"doughnut":case"polarArea":case"radar":case"funnel":return e.jsx(ye,{cardType:n});case"bubble":case"scatter":return e.jsx(qn,{cardType:n});case"table":return e.jsx(ta,{cardType:n});case"text":return e.jsx(ye,{docContent:aa,cardType:n,suffix:"Visual"});case"map":return e.jsx(Zn,{});case"custom":return e.jsx(Bn,{});default:return e.jsx(Un,{})}}return e.jsxs(t.ScrollArea,{className:"h-full w-full text-sm",children:[a(s),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})}function Gt({label:s,column:a,functions:n,onFunctionChange:r}){let o=n[0];"aggregate"in a?o=n.find(i=>i.value===a.aggregate)||n[0]:"granularity"in a&&(o=n.find(i=>i.value===a.granularity)||n[0]);function l(i){r(i)}return e.jsx(Y,{className:"flex h-8 w-8 justify-center overflow-clip rounded-none border-none bg-muted px-0 [&>svg]:hidden",icon:o.logo,options:n,value:o.value,onValueChange:i=>l(i),label:s,showPlaceholderLabel:!1})}const oa=[{label:"Sum",value:"SUM",logo:e.jsx(xt,{className:"size-3.5"})},{label:"Count",value:"COUNT",logo:e.jsx(ct,{className:"size-3.5"})},{label:"Unique",value:"DISTINCT",logo:e.jsx(Ns,{className:"size-3.5"})},{label:"Average",value:"AVG",logo:e.jsx(ss,{className:"size-3.5"})},{label:"Min",value:"MIN",logo:e.jsx(Xe,{className:"size-3.5"})},{label:"Max",value:"MAX",logo:e.jsx(t.ArrowUp,{className:"size-3.5"})}];function ia({column:s}){const{updateMetricColumn:a}=t.useEditorActions();let n=oa;t.isTextDataType(s.type)&&(n=n.filter(o=>["COUNT","DISTINCT"].includes(o.value))),t.isNumberDataType(s.type)&&(n=n.filter(o=>!["DISTINCT"].includes(o.value)));function r(o){const l={...s,aggregate:o,label:me(s.name,o)};a(s.id,l)}return e.jsx(Gt,{column:s,functions:n,onFunctionChange:o=>r(o),label:"Aggregation"})}const la=[{label:"Day (D)",value:"day",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"D"})},{label:"Week (W)",value:"week",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"W"})},{label:"Month (M)",value:"month",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"M"})},{label:"Year (Y)",value:"year",logo:e.jsx(t.Label,{className:"cursor-pointer text-xs",children:"Y"})}];function ca({column:s}){const{updateGroupByColumn:a}=t.useEditorActions();function n(r){const o={...s,granularity:r,label:ce(s.name,r)};a(s.id,o)}return e.jsx(Gt,{column:s,functions:la,onFunctionChange:r=>n(r),label:"Granularity"})}function da({col:s,onChange:a,droppableId:n}){const{columns:r}=t.useDataColumns(),o=T.useMemo(()=>{let u=[];(n===te||n===se||n===ne)&&(u=r);const c=u==null?void 0:u.filter(h=>h.column_name!==s.name);return c==null?void 0:c.map(h=>({id:h.column_name,value:h.column_name}))},[r,n,s.name]),l=[{id:s.name,value:s.name}];function i(u){const c=r==null?void 0:r.find(h=>{var d;return h.column_name===((d=u[0])==null?void 0:d.id)});c&&a(c)}return o!=null&&o.length?e.jsx(t.ComboBox2,{multiple:!1,options:o,selectedOptions:l,onChange:i,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:s.name})}function ua({column:s}){const{updateMetricColumn:a}=t.useEditorActions(),{transformedMetricKeys:n,dimensionKeys:r}=Vt();console.log(n,r);const o=t.useEditorStore(c=>{var h;return(h=c.card.queryConfig)==null?void 0:h.pivotColumns}),l=!!(o&&o.length>0),i=n.find(c=>c===s.label||c===s.name),u=i?n.indexOf(i):-1;return e.jsxs("div",{className:"space-y-3",children:[i&&e.jsx(qt,{idx:u,col:i,children:e.jsx(t.Editable,{enabled:!0,className:"w-[130px]",text:s.label||"",placeholder:"Enter label",onSave:c=>{a(s.id,{...s,label:c||s.name})},children:e.jsx(Ne,{className:"w-[130px]",children:s.label||s.name})})}),l&&e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(t.Label,{children:"Pivot Header"}),e.jsx(t.Editable,{enabled:!0,text:s.aliasTemplate||"",placeholder:"{label} ({pivot})",onSave:c=>{a(s.id,{...s,aliasTemplate:c||""})},children:e.jsx(Ne,{className:"font-mono text-xs text-muted-foreground",children:s.aliasTemplate||"{label} ({pivot})"})})]})]})}function ma({column:s,type:a,onRemoveColumn:n,droppableId:r}){const[o,l]=T.useState(!1),{updateGroupByColumn:i,updateMetricColumn:u,updatePivotByColumn:c}=t.useEditorActions(),{attributes:h,listeners:d,setNodeRef:x,transform:b,transition:m,isDragging:g}=t.useSortable({id:s.id,data:{type:a}}),p=b?{opacity:g?.5:void 0,transform:t.CSS.Translate.toString(b),transition:m}:void 0,y=t.isTextDataType(s.type);function f(D){const F=t.isNumberDataType(D.data_type),R=t.isTextDataType(D.data_type),U=t.isDateDataType(D.data_type),w=F?"number":R?"string":U?"date":"string";if(r===te){if(R){const _={id:s.id,name:D.column_name,type:w,role:"groupby"};i(s.id,_)}if(U){const _={id:s.id,name:D.column_name,type:w,role:"groupby",label:ce(D.column_name,"month"),granularity:"month"};i(s.id,_)}}if(r===ne){if(R){const _={id:s.id,name:D.column_name,type:w,role:"groupby"};c(s.id,_)}if(U){const _={id:s.id,name:D.column_name,type:w,label:ce(D.column_name,"month"),granularity:"month",role:"groupby"};c(s.id,_)}}if(r===se){const _={id:s.id,name:D.column_name,type:w,role:"metric",label:me(D.column_name,"SUM"),aggregate:F?"SUM":"COUNT"};u(s.id,_)}}function S(){if(r===se)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(ua,{column:s})})}function C(){const D=t.isDateDataType(s.type);if(r===se)return e.jsx(ia,{column:s});if((r===te||r===ne)&&D)return e.jsx(ca,{column:s})}function A(){return e.jsx("div",{className:t.cn("grow",{"pl-3":(r===te||r===ne)&&y}),children:e.jsx(da,{col:s,onChange:f,droppableId:r})})}function N(){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:()=>l(!o),children:o?e.jsx(_s,{className:t.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")}):e.jsx(it,{className:t.cn("size-3 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground")})})})}return e.jsx("div",{...d,...h,ref:x,style:p,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:[N(),C(),A()]}),o&&S()]}),e.jsx("div",{className:t.cn("opacity-0 transition-opacity duration-200 group-hover:opacity-100",{hidden:o}),children:e.jsx("button",{onClick:()=>n(s.id),children:e.jsx(t.X,{className:"size-3 cursor-pointer text-muted-foreground"})})})]})})}function Ae({id:s,label:a,columns:n,previewColumns:r,sortableType:o,onRemoveColumn:l}){var p,y;const{setNodeRef:i,isOver:u,over:c}=t.useDroppable({id:s}),h=t.useDndStore(f=>f.activeId),d=t.useEditorStore(f=>{var S;return(S=f.card)==null?void 0:S.type}),x=u||((y=(p=c==null?void 0:c.data)==null?void 0:p.current)==null?void 0:y.type)===o,b=h?r:n,m=o===be?"Rows":o===ve?"Pivot Columns":"Columns";function g(){if(!b||b.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(t.SortableContext,{items:n==null?void 0:n.map(f=>f.id),children:e.jsxs("div",{className:t.cn("group space-y-2 rounded-md p-4",x&&"border border-muted-foreground/20"),ref:i,children:[e.jsxs("div",{className:"flex items-center justify-between pr-5",children:[e.jsx(t.Label,{className:"text-sm",children:a}),d!=="table"&&e.jsx("span",{className:"text-xs text-muted-foreground/50",children:m})]}),g(),e.jsx("div",{className:"space-y-2",children:b==null?void 0:b.map(f=>f.id===bt.id?e.jsx(ha,{},f.id):e.jsx(ma,{droppableId:s,type:o,column:f,onRemoveColumn:l},f.id))})]})})}function ha(){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 Ut({children:s,id:a,type:n}){const{attributes:r,listeners:o,setNodeRef:l,transform:i,transition:u,isDragging:c}=t.useSortable({id:a,data:{type:n}}),h=i?{opacity:c?.5:void 0,transform:t.CSS.Translate.toString(i),transition:u}:void 0;return e.jsx("div",{...o,...r,ref:l,style:h,children:s})}function xa(){const s=t.useEditorStore(f=>{var S;return(S=f.card.queryConfig)==null?void 0:S.orderBy})||[],a=t.useEditorStore(f=>{var S;return(S=f.card.queryConfig)==null?void 0:S.groupByColumns})||[],n=t.useEditorStore(f=>{var S;return(S=f.card.queryConfig)==null?void 0:S.metricColumns})||[],{addOrderByColumn:r,removeOrderByColumn:o,updateOrderByColumn:l}=t.useEditorActions(),i=[...a,...n];if(i.length===0)return e.jsx(e.Fragment,{});const u=e.jsx(os,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),c=e.jsx(ts,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),h=e.jsx(rs,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),d=e.jsx(es,{className:"size-3.5 text-muted-foreground hover:text-foreground"}),x=i==null?void 0:i.filter(f=>!s.some(S=>S.columnId===f.id)).map(f=>({id:f.id,value:f.label||f.name}));function b(f,S){const C=m(f);return C?C.role==="metric"?S==="asc"?h:d:S==="asc"?u:c:null}function m(f){return i.find(S=>S.id===f)}function g(f){const S=s.find(C=>C.columnId===f);if(S){const C={...S,direction:S.direction==="asc"?"desc":"asc"};l(C)}}const p=f=>{r({columnId:f.id,direction:"asc"})};function y(f){const S=s.find(C=>C.columnId===f);S&&o(S)}return e.jsx(t.SortableContext,{items:s.map(f=>ue(f.columnId)),children:e.jsxs("div",{className:"space-y-2 rounded-md p-4 text-sm",children:[e.jsx(t.Label,{className:"text-sm",children:"Sort By"}),x.length>0&&e.jsx("div",{className:"mr-5",children:e.jsx(t.ComboBox2,{multiple:!1,options:x,selectedOptions:[],onChange:f=>p(f[0]),selectAllChecked:!1,onSelectAllCheckedChange:()=>{},inputClassName:"h-8 bg-muted border-none"})}),s.map(f=>{var S,C;return e.jsx(Ut,{id:ue(f.columnId),type:qe,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(it,{className:"size-3 cursor-move text-muted-foreground hover:text-foreground"}),e.jsx(Ne,{className:"w-[175px] text-sm",children:((S=m(f.columnId))==null?void 0:S.label)||((C=m(f.columnId))==null?void 0:C.name)})]}),e.jsx("button",{title:f.direction==="asc"?"Ascending":"Descending",onClick:()=>g(f.columnId),className:"rounded-sm p-0",children:b(f.columnId,f.direction)})]}),e.jsx("div",{children:e.jsx("button",{onClick:()=>y(f.columnId),className:"rounded-sm p-0",children:e.jsx(t.X,{className:"size-3 text-muted-foreground opacity-0 transition-opacity duration-200 hover:text-foreground group-hover:opacity-100"})})})]})},f.columnId)},ue(f.columnId))})]})})}function pa(){const s=t.useEditorStore(y=>{var f;return((f=y.card.queryConfig)==null?void 0:f.groupByColumns)||[]}),a=t.useEditorStore(y=>y.card.type),n=t.useDndStore(y=>y.previewGroupByColumns),r=t.useEditorStore(y=>{var f;return((f=y.card.queryConfig)==null?void 0:f.metricColumns)||[]}),o=t.useDndStore(y=>y.previewMetricColumns),l=t.useEditorStore(y=>{var f;return((f=y.card.queryConfig)==null?void 0:f.pivotColumns)||[]}),i=t.useDndStore(y=>y.previewPivotColumns),{removeColumnFromGroupBy:u,removeColumnFromMetricColumns:c,removeColumnFromPivotBy:h}=t.useEditorActions();function d(y){u(y)}function x(y){c(y)}function b(y){h(y)}let m="Group By",g="Metrics",p="Stack By";return a==="table"&&(m="Rows",g="Metrics",p="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(t.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(Ae,{onRemoveColumn:d,sortableType:be,id:te,label:m,columns:s,previewColumns:n}),e.jsx(Ae,{onRemoveColumn:b,sortableType:ve,id:ne,label:p,columns:l,previewColumns:i}),e.jsx(Ae,{onRemoveColumn:x,sortableType:Oe,id:se,label:g,columns:r,previewColumns:o}),e.jsx(xa,{})]}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function Ge({children:s,column:a,id:n,sortableType:r,className:o}){const[l,i]=T.useState(!1),{removeEditorFilter:u,clearEditorFilter:c}=t.useEditorActions(),h=t.useEditorStore(x=>{var b,m;return(m=(b=x.card.queryConfig)==null?void 0:b.filters)==null?void 0:m.some(g=>g.filterColumnName===a.name)});function d(x){u(x)}return e.jsx(Ut,{id:n,type:r,children:e.jsxs("div",{className:t.cn("group space-y-1 rounded-md bg-muted px-3 py-2",{"border-border":l}),children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Label,{className:"text-sm",children:a.name}),h&&e.jsx("button",{onClick:()=>c(a),children:e.jsx(t.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:()=>d(a),children:e.jsx(t.X,{className:"size-3.5 cursor-pointer items-center justify-center rounded-r-sm text-muted-foreground"})})})]}),e.jsxs("div",{className:t.cn("group flex items-center rounded-t-md text-sm",!l&&"rounded-b-md",l&&"border border-border"),children:[e.jsx("div",{children:e.jsx(t.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:t.cn("grow cursor-pointer overflow-hidden truncate text-ellipsis whitespace-nowrap",o),children:s})]})]})})}function fa({column:s,sortableType:a}){const n=t.useDashboardStore(C=>C.themeStyle),r=t.useEditorStore(C=>{var A,N;return(N=(A=C.card)==null?void 0:A.queryConfig)==null?void 0:N.filters}),o=t.useEditorStore(C=>{var A,N;return(N=(A=C.card.queryConfig)==null?void 0:A.filterColumns)==null?void 0:N.find(D=>D.name===s.name)}),{selectedConnectionId:l,selectedDatabaseName:i,selectedTableName:u}=t.useEditorAside(),c=T.useMemo(()=>{var C;return(C=r==null?void 0:r.find(A=>A.filterColumnName===s.name))==null?void 0:C.filterValue},[r,s.name]),h=T.useMemo(()=>Array.isArray(c)?c.map(C=>({id:C,value:C})):[],[c]),{updateEditorFilterValue:d}=t.useEditorActions(),x={column:s.name,sql:s.sql,id:s.name,connectionId:l,dataType:s.type},{data:b,isLoading:m,isFetching:g,isError:p}=t.useFilterQuery(x),y=T.useMemo(()=>{var A;return x.column?(A=b==null?void 0:b.records)==null?void 0:A.map(N=>{let D=x.column.split(".").slice(-1)[0].replace(/"/g,"");return N[D]===void 0&&(D=D.toLowerCase()),{id:N[D],value:N[D]}}):[]},[b,x.column]);function f(C){const A=C.map(N=>N.id);d(s.name,A)}function S(){return m||g?e.jsx(t.Skeleton,{style:{borderRadius:n==null?void 0:n.controlInputRadius},className:"h-8 min-w-60 bg-foreground/10"}):p?e.jsx("div",{className:"flex h-8 min-w-60 items-center bg-muted px-2 text-sm",children:"Error"}):e.jsx(t.ComboBox2,{renderFilterModeButton:()=>e.jsx(ga,{columnName:s.name}),showNegativeIndicator:(o==null?void 0:o.mode)==="exclude",options:y,onChange:f,selectedOptions:h,selectAllChecked:!1,onSelectAllCheckedChange:()=>{}})}return e.jsx(Ge,{className:"w-60",id:s.id,sortableType:a,column:s,children:S()})}function ga({columnName:s}){const{setEditorFilterMode:a}=t.useEditorActions(),n=t.useEditorStore(o=>{var l,i;return(i=(l=o.card.queryConfig)==null?void 0:l.filterColumns)==null?void 0:i.find(u=>u.name===s)});function r(){(n==null?void 0:n.mode)==="exclude"?a(s,"include"):a(s,"exclude")}return e.jsx(t.Button,{onClick:r,variant:"secondary",size:"sm",className:t.cn("h-5 text-xs hover:underline",{}),children:t.titleCase((n==null?void 0:n.mode)==="exclude"?"exclude":"include")})}function ja({column:s,sortableType:a}){const[n,r]=T.useState(!1),{selectedConnectionId:o,selectedDatabaseName:l,selectedTableName:i}=t.useEditorAside(),u={column:s.name,sql:s.sql,id:s.name,connectionId:o,title:"",dataType:s.type},{isLoading:c,isFetching:h,isError:d,data:x}=t.useFilterQuery(u),b=x==null?void 0:x.records,{dateRange:m,setDateRange:g,initialDateRange:p}=t.useDateRangeFromRecords(b),{updateEditorFilterValue:y}=t.useEditorActions();function f(A){var D,F;const N={gte:(D=A==null?void 0:A.from)==null?void 0:D.toISOString(),lte:(F=A==null?void 0:A.to)==null?void 0:F.toISOString()};y(s.name,N)}function S(){y(s.name,null,!0),g(p)}function C(){return c||h?e.jsx("div",{children:e.jsx(t.Skeleton,{className:"h-8 w-72 bg-foreground/10"})}):d?e.jsx("div",{className:"h-8 w-72 bg-muted px-2",children:"Error"}):b?e.jsx(t.DateRangePopover,{title:u.title,open:n,inputClassName:"h-8",setOpen:r,dateRange:m,setDateRange:g,onApply:f,onClear:S}):e.jsx(t.Label,{className:"flex h-8 w-72 items-center bg-muted px-2",children:"No Data"})}return e.jsx(Ge,{className:"w-72",id:s.id,sortableType:a,column:s,children:C()})}function ya({id:s,label:a,columns:n,previewColumns:r,sortableType:o}){var g,p;const{setNodeRef:l,isOver:i,over:u}=t.useDroppable({id:s}),c=t.useDndStore(y=>y.activeId),h={},d=i||((p=(g=u==null?void 0:u.data)==null?void 0:g.current)==null?void 0:p.type)===o;function x(){if(!m||m.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 b(y){const f=t.isDateDataType(y.type);return y.id===bt.id?e.jsx(Ge,{id:y.id,sortableType:o,column:y,children:e.jsx(ba,{})},y.id):f?e.jsx(ja,{droppableId:s,sortableType:o,column:y},y.id):e.jsx(fa,{droppableId:s,sortableType:o,column:y},y.id)}const m=c?r:n;return e.jsx(t.SortableContext,{items:n==null?void 0:n.map(y=>y.id),children:e.jsxs("div",{className:t.cn("group space-y-2 rounded-md border-muted-foreground/50 p-4",d&&"border border-muted-foreground/20"),ref:l,style:h,children:[e.jsx(t.Label,{className:"text-sm",children:a}),x(),e.jsx("div",{className:"flex flex-wrap items-center gap-4",children:m==null?void 0:m.map(y=>b(y))})]})})}function ba(){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 va(){const s=t.useEditorStore(o=>{var l;return((l=o.card.queryConfig)==null?void 0:l.filterColumns)||[]}),a=t.useDndStore(o=>o.previewFilterColumns),{removeColumnFromFilterColumns:n}=t.useEditorActions();function r(o){n(o)}return e.jsx("section",{className:"size-full px-4 pb-4",children:e.jsx(ya,{onRemoveColumn:r,sortableType:Fe,id:ze,label:"Filters",columns:s,previewColumns:a})})}function Ca(){const s=t.useEditorStore(r=>r.card),{data:a,errorMessage:n}=t.useDashboardCardQuery(s);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(Et,{}),e.jsx(Ct,{sql:a==null?void 0:a.sql,error:n})]}),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(It,{}),e.jsx(Dt,{variant:"default"})]})]})}function Sa(){const s=T.useRef(null),a=t.useEditorStore(i=>i.isShowingVisual),n=t.useEditorStore(i=>i.showAIDialog),r=t.useEditorStore(i=>i.isDevMode);T.useEffect(()=>{var i,u;n?(i=s.current)==null||i.resize(0):(u=s.current)==null||u.resize(25)},[n]);function o(){return e.jsxs(e.Fragment,{children:[e.jsx(t.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(bn,{})}),e.jsx(t.ResizableHandle,{withHandle:!0}),e.jsx(t.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:a&&!n?e.jsxs(t.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(t.ResizablePanel,{ref:s,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(ra,{})})}),!n&&e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(je,{})})]}):e.jsx(je,{})})]})}function l(){return e.jsx(e.Fragment,{children:e.jsx("div",{className:"grow","data-biz-mode-container":"",children:a&&!n?e.jsxs(t.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(t.ResizablePanel,{ref:s,className:"flex min-w-[320px]",defaultSize:25,maxSize:30,children:e.jsx(pa,{})})}),!n&&e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{defaultSize:75,maxSize:100,children:e.jsxs(t.ResizablePanelGroup,{direction:"vertical",children:[e.jsxs(t.ResizablePanel,{className:"flex min-h-fit flex-col",minSize:10,defaultSize:20,children:[e.jsx(Ca,{}),e.jsx(va,{})]}),e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{minSize:60,defaultSize:80,children:e.jsx(je,{})})]})})]}):e.jsx(je,{})})})}return e.jsx(t.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:r?o():l()})}const Na=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function wa(){const s=T.useRef(null),a=T.useRef(null),[n,r]=T.useState(!1),[o,l]=T.useState(t.isDevEnv),[i,u]=T.useState(!1),c=t.useEditorStore(L=>L.actions.setCardSql),h=t.useEditorStore(L=>L.actions.setRunSql),d=t.useEditorStore(L=>L.showAIDialog),x=t.useDashboardStore(L=>L.handleOpenAssistantProfile),{setShowAIDialog:b,setCardPython:m}=t.useEditorActions(),{assistantProfile:g}=t.useAssistantProfile(),{messages:p,setMessages:y,reload:f,append:S,input:C,setInput:A,isLoading:N,handleAddCurrentTableToScope:D,removeFromCurrentSelections:F,currentAssitantScope:R,handleInputChange:U,handleSubmit:w,stop:_,chatStatus:G,llmUsage:P,errorMessage:E}=kt();t.useAssistantScroll({scrollContainerRef:s,inputRef:a,messages:p}),T.useEffect(()=>{var L;d&&((L=a.current)==null||L.focus())},[d]);function j(L,z){z==="sql"?(c(L),h(!0)):z==="python"&&(m(L),h(!0))}function M(L){const z={role:"user",content:L.title+" "+L.subtitle};S(z)}function B(L){S({role:"user",content:L})}function v(){return e.jsxs(t.DropdownMenu,{children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",children:e.jsx(ot,{className:"size-3.5 text-muted-foreground/70 hover:text-muted-foreground"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",children:[e.jsx(t.DropdownMenuLabel,{children:"Options"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsx(t.DropdownMenuCheckboxItem,{checked:n,onCheckedChange:r,children:"Token Usage"}),e.jsx(t.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:l,children:"Tool Invocations"}),e.jsx(t.DropdownMenuCheckboxItem,{checked:i,onCheckedChange:u,children:"System Message"})]})]})}function O(){return e.jsxs("div",{children:[R&&R.length>0&&e.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-2",children:R==null?void 0:R.map((L,z)=>e.jsxs("div",{className:"flex items-center gap-2 rounded-md border border-border bg-muted px-2 py-1 text-xs",children:[t.getFormattedTableNameForQuery({schemaName:L.schemaName||"",tableName:L.tableName||"",modelName:L.datamodelName||"",connectionType:L.connectionType||"",databaseName:L.databaseName||""}),e.jsx("button",{onClick:()=>{F(L)},children:e.jsx(t.X,{className:"size-3 text-muted-foreground hover:text-foreground"})})]},z))}),(R==null?void 0:R.length)===0&&e.jsxs("div",{className:"mt-2 flex items-center gap-2",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Context: None"}),e.jsxs(t.Button,{className:"h-6 text-xs",size:"sm",variant:"secondary",onClick:D,children:[e.jsx(t.Plus,{className:"mr-2 size-3"}),"Current Table"]})]})]})}function I(){return e.jsx("div",{"data-role":"header",children:e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"})})}const $=(C==null?void 0:C.length)===0&&(p==null?void 0:p.filter(L=>L.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:[I(),x&&g&&e.jsx(t.Button,{variant:"outline",size:"sm",className:"h-7 p-2",onClick:x,children:g==null?void 0:g.name}),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center",children:[v(),e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",onClick:()=>{y([]),f()},children:e.jsx(t.Plus,{className:"size-4 text-muted-foreground"})}),e.jsx(t.Button,{className:"h-7 p-2",size:"sm",variant:"ghost",onClick:()=>{b(!1)},children:e.jsx(t.X,{className:"size-4 text-muted-foreground"})})]})]}),O(),e.jsx(t.CustomScrollArea,{ref:s,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(t.PrintMessages,{messages:p,showTools:o,handleRun:j,showSystemMessage:i}),e.jsx(t.ChatStatus,{messages:p,chatStatus:G,errorMessage:E})]})}),e.jsxs("div",{className:"mb-3",children:[$&&!g&&e.jsx(t.Placeholders,{onClick:M,placeholders:Na}),e.jsx("div",{children:$&&g&&e.jsx(t.SeedQuestions,{seedQuestions:g.seedQuestions||[],handleSeedQuestionClick:B})})]}),e.jsx(t.ChatInput,{onFocus:()=>{D()},stop:_,ref:a,setInput:A,input:C,handleInputChange:U,handleSubmit:w,isLoading:N}),P&&n&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",P.promptTokens," prompt tokens,"," ",P.completionTokens," completion tokens,"," ",P.totalTokens," total tokens"]})})]})}function ka({authToken:s,onSave:a,onClose:n}){const r=T.useRef(null),o=t.useEditorStore(y=>y.showAIDialog),{setAuthToken:l}=t.useDashboardActions(),{setShowAIDialog:i,setOnSave:u,setOnClose:c}=t.useEditorActions(),{handleDragOver:h,handleDragEnd:d,handleDragStart:x,handleDragCancel:b,sensors:m}=an();T.useEffect(()=>{a&&u(a),n&&c(n)},[a,u,n,c]),T.useEffect(()=>{s&&s.accessToken&&l(s)},[s,l]),T.useEffect(()=>{var y,f;o?((y=r.current)==null||y.resize(40),i(!0)):(i(!1),(f=r.current)==null||f.resize(0))},[o,i]);function g(y){y<1?i(!1):o||i(!0)}function p(){return e.jsxs(t.ResizablePanelGroup,{"data-content":"editor-main",className:"grow",direction:"horizontal",children:[e.jsx(t.ResizablePanel,{className:"min-w-[510px] rounded-none",children:e.jsx(Sa,{})}),e.jsx(t.ResizableHandle,{}),e.jsx(t.ResizablePanel,{"data-role":"editor-assistant-panel",ref:r,onResize:g,defaultSize:0,children:e.jsx(wa,{})})]})}return e.jsx(t.ErrorBoundary,{FallbackComponent:t.Fallback,children:e.jsx(t.DndContext,{sensors:m,onDragStart:x,onDragEnd:d,onDragOver:h,onDragCancel:b,children:e.jsxs("section",{className:"isolate flex h-full grow","data-content":"editor-panel",children:[e.jsx(vt,{type:"main"}),p()]})})})}function Da(){t.useDashboardStore(i=>i.selectedCardId);const s=t.useDashboardStore(i=>i.selectedSheetId),{setIsVisualEditing:a}=t.useDashboardStore(i=>i.actions),n=t.useDashboardStore(i=>i.isVisualEditing),r=t.useDashboardStore(i=>i.actions.updateFrame);function o(i){s&&(r(s,i),a(!1))}function l(){a(!1)}return e.jsx("div",{role:"editor-container",className:t.cn("flex grow",{"max-h-0 overflow-hidden":!n}),children:e.jsx(ka,{onSave:o,onClose:l})})}function Ia(){const s=t.useEditorStore(l=>l.selectedDatabaseName),a=t.useEditorStore(l=>l.selectedTableName),n=t.useDashboardStore(l=>l.dashboard.filters)||[],{columns:r}=t.useEditorAside(),{assignFilter:o}=Ht();return e.jsx(t.ScrollArea,{className:"flex-1 basis-0 flex-col px-3",children:r==null?void 0:r.map(l=>{const i=n==null?void 0:n.find(u=>u.column===t.fmt(l.column_name)&&u.table===a&&u.database===s);return e.jsx("div",{onClick:()=>o(l),className:t.cn("group flex cursor-pointer items-center justify-between border border-dashed border-background px-3 py-[6px] text-sm transition-colors hover:bg-muted/50",{"bg-muted":i}),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Pe,{column:l}),e.jsx("span",{children:l.column_name})]})},l.column_name)})})}function Ea(){t.useDashboardStore(a=>a.showFilters);const s=t.useDashboardStore(a=>a.actions.setShowFilters);return e.jsxs("section",{className:"relative flex min-w-[320px] grow flex-col border-l px-1",children:[e.jsxs("div",{className:"mt-4 flex items-center justify-between gap-2 px-3",children:[e.jsx(t.Label,{className:"px-2 text-lg",children:"Filters"}),e.jsx(t.Button,{onClick:()=>s(!1),className:"h-7 w-7 p-0",variant:"ghost",children:e.jsx(t.Cross2Icon,{})})]}),e.jsx("div",{className:"flex grow",children:e.jsx(vt,{type:"filter",ColumnsComponent:()=>e.jsx(Ia,{})})})]})}function Ta(){const s=t.useDashboardStore(r=>r.dashboard.filters),a=t.useDashboardStore(r=>r.theme),n=s==null?void 0:s.filter(r=>r.hide);return n!=null&&n.length?e.jsx("div",{className:t.cn("mb-1 flex items-center gap-2 border-y px-0.5 pb-0.5",{"bg-[repeating-linear-gradient(45deg,_#ffffff_0_3px,_#f3f3f3_3px_6px)]":a==="light","bg-[repeating-linear-gradient(45deg,_#18171d_0_3px,_#272729_3px_6px)]":a==="dark"}),children:n==null?void 0:n.map(r=>e.jsxs("div",{className:"flex min-w-[250px] max-w-fit flex-col gap-1 p-2",children:[e.jsx(t.FilterHeader,{filter:r}),e.jsx(t.FilterComponent,{filter:r})]},r.id))}):null}function Ma(){const s=t.useDashboardStore(r=>r.showFilters),a=t.useDashboardStore(r=>r.isVisualEditing),n=t.useDashboardStore(r=>r.isDashboardEditing);return e.jsxs("div",{role:"dashboard-plus-main",className:t.cn("",{"max-h-0 overflow-hidden":a,"flex grow overflow-auto":!a}),children:[e.jsxs("section",{className:"flex grow flex-col",children:[n&&e.jsx(Ta,{}),e.jsx(t.DashboardTabs,{})]}),s&&e.jsx("div",{role:"filter-aside-container",className:"flex",children:e.jsx(Ea,{})})]})}function Aa(){const s=t.useDashboardStore(o=>o.dashboard),a=t.useDashboardStore(o=>o.actions.setDashboard),n=t.useDashboardStore(o=>o.showDashboardJSON),r=t.useDashboardStore(o=>o.actions.setShowDashboardJSON);return e.jsxs("div",{className:t.cn("relative p-2",{hidden:!n}),children:[e.jsxs(t.Button,{variant:"secondary",size:"sm",className:"absolute right-2 top-2 z-10 h-8",onClick:()=>r(!1),children:[e.jsx(t.X,{className:"mr-2 h-4 w-4"}),"Close"]}),e.jsx(t.de,{onChange:o=>{if(o)try{JSON.parse(o),a(JSON.parse(o))}catch{}},defaultLanguage:"json",className:"h-[550px] overflow-y-auto",value:JSON.stringify(s,null,2),options:{minimap:{enabled:!1},glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})]})}function La({showControls:s=!1,showFooter:a=!0,...n}){return e.jsx(t.ErrorBoundary,{FallbackComponent:t.Fallback,children:e.jsxs(t.SemaphorContextProvider,{dashboardProps:n,children:[e.jsx(t.$e,{}),e.jsxs(t.UXProvider,{children:[e.jsx(Aa,{}),e.jsxs(t.DashboardPanel,{...n,children:[s&&e.jsx(Xs,{}),e.jsx(Da,{}),e.jsx(Ma,{})]})]})]})})}exports.Dashboard=t.Dashboard;exports.DashboardWC=t.DashboardWC;exports.SemaphorDataProvider=t.SemaphorQueryClient;exports.SemaphorQueryClient=t.SemaphorQueryClient;exports.createSqlGenConfig=t.createSqlGenConfig;exports.fmt=t.fmt;exports.getColumnDataType=t.getColumnDataType;exports.getFilterValueType=t.getFilterValueType;exports.getFormattedTableNameForQuery=t.getFormattedTableNameForQuery;exports.getQualifiedTableName=t.getQualifiedTableName;exports.getSql=t.getSql;exports.invalidateToken=t.invalidateToken;exports.isBooleanDataType=t.isBooleanDataType;exports.isColumnInSqlGen=t.isColumnInSqlGen;exports.isDateDataType=t.isDateDataType;exports.isNumberDataType=t.isNumberDataType;exports.isTextDataType=t.isTextDataType;exports.resolveDataType=t.resolveDataType;exports.resolveDatamodelName=t.resolveDatamodelName;exports.useCard=t.useCard;exports.useDashboardActions=t.useDashboardActions;exports.useDashboardStore=t.useDashboardStore;exports.useDateFilterHook=t.useDateFilterHook;exports.useEditorActions=t.useEditorActions;exports.useEditorAside=t.useEditorAside;exports.useEditorStore=t.useEditorStore;exports.useTextFilterHook=t.useTextFilterHook;exports.EMPTY_SELECTION=Wt.EMPTY_SELECTION;exports.ScheduleDashboard=yt;exports.SelectComponent=ie;exports.Surfboard=La;exports.cleanCard=ft;exports.downloadPdf=jt;exports.getDashbaordStateWithoutData=_e;
|