react-semaphor 0.1.178 → 0.1.180
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/{dashboard-controls-00cjSeD7.js → dashboard-controls-Baw7eUyM.js} +8 -8
- package/dist/chunks/{dashboard-controls-C9IWIFfW.js → dashboard-controls-CS3pAVZi.js} +473 -470
- package/dist/chunks/{dashboard-json--ke9XK9p.js → dashboard-json-ClalWYi4.js} +1 -1
- package/dist/chunks/{dashboard-json-BDjl-3ej.js → dashboard-json-Cz4bNnCC.js} +1 -1
- package/dist/chunks/{edit-dashboard-visual-BroCdmtP.js → edit-dashboard-visual-BMElXt-n.js} +30 -35
- package/dist/chunks/{edit-dashboard-visual-BBsC9feJ.js → edit-dashboard-visual-DFJ5FJ7y.js} +11 -22
- package/dist/chunks/editor-action-buttons-DVWnnC_r.js +11 -0
- package/dist/chunks/{editor-action-buttons-CHoiNw7F.js → editor-action-buttons-MtNYw1Bb.js} +57 -45
- package/dist/chunks/index-8Crk3ROJ.js +1 -0
- package/dist/chunks/index-B4EhQdS9.js +1857 -0
- package/dist/chunks/{index-DZjcALSv.js → index-CAeKp6K8.js} +23423 -24714
- package/dist/chunks/index-CfPKTeUk.js +1082 -0
- package/dist/chunks/notification-bell-2dZI5OV0.js +21 -0
- package/dist/chunks/notification-bell-YZ8VqdwA.js +870 -0
- package/dist/chunks/{resource-management-panel-C96ElLa5.js → resource-management-panel-Buhi-kvP.js} +1 -1
- package/dist/chunks/{resource-management-panel-DE8DnPSl.js → resource-management-panel-pxL28GXq.js} +133 -133
- package/dist/chunks/{use-role-aware-display-preferences-Dovp_VmV.js → use-role-aware-display-preferences-B_i2f9tn.js} +1 -1
- package/dist/chunks/{use-role-aware-display-preferences-Rd1BhDG4.js → use-role-aware-display-preferences-JljRcjrP.js} +1 -1
- package/dist/chunks/{use-visual-utils-BkRr5LoD.js → use-visual-utils-D3HjbsKO.js} +38 -38
- package/dist/chunks/{use-visual-utils-Cnju2tnE.js → use-visual-utils-DPEHcmFT.js} +1 -1
- package/dist/dashboard/index.cjs +1 -1
- package/dist/dashboard/index.js +1 -1
- package/dist/format-utils/index.cjs +5 -0
- package/dist/format-utils/index.js +451 -0
- package/dist/index.cjs +1 -6
- package/dist/index.js +133 -126
- package/dist/style.css +1 -1
- package/dist/surfboard/index.cjs +1 -1
- package/dist/surfboard/index.js +1 -1
- package/dist/types/format-utils.d.ts +375 -0
- package/dist/types/main.d.ts +262 -0
- package/package.json +14 -1
- package/dist/chunks/editor-action-buttons-JE9t84-4.js +0 -6
- package/dist/chunks/index-CdfFCbJb.js +0 -1077
- package/dist/chunks/schedule-dashboard-BFa0Uljc.js +0 -416
- package/dist/chunks/schedule-dashboard-CzU0ziLB.js +0 -6
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),s=require("./index-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),s=require("./index-CfPKTeUk.js"),L=require("react"),Re=require("./editor-action-buttons-DVWnnC_r.js"),Xs=require("react-dom");function Zs(t){const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const a in t)if(a!=="default"){const l=Object.getOwnPropertyDescriptor(t,a);Object.defineProperty(n,a,l.get?l:{enumerable:!0,get:()=>t[a]})}}return n.default=t,Object.freeze(n)}const _e=Zs(L);/**
|
|
2
2
|
* @license lucide-react v0.453.0 - ISC
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the ISC license.
|
|
@@ -23,22 +23,17 @@
|
|
|
23
23
|
*
|
|
24
24
|
* This source code is licensed under the ISC license.
|
|
25
25
|
* See the LICENSE file in the root directory of this source tree.
|
|
26
|
-
*/const at=s.createLucideIcon("CircleX",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"m15 9-6 6",key:"1uzhvr"}],["path",{d:"m9 9 6 6",key:"z0biqf"}]]);/**
|
|
27
|
-
* @license lucide-react v0.453.0 - ISC
|
|
28
|
-
*
|
|
29
|
-
* This source code is licensed under the ISC license.
|
|
30
|
-
* See the LICENSE file in the root directory of this source tree.
|
|
31
26
|
*/const ws=s.createLucideIcon("Columns3",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M9 3v18",key:"fh3hqa"}],["path",{d:"M15 3v18",key:"14nvp0"}]]);/**
|
|
32
27
|
* @license lucide-react v0.453.0 - ISC
|
|
33
28
|
*
|
|
34
29
|
* This source code is licensed under the ISC license.
|
|
35
30
|
* See the LICENSE file in the root directory of this source tree.
|
|
36
|
-
*/const
|
|
31
|
+
*/const at=s.createLucideIcon("Component",[["path",{d:"M15.536 11.293a1 1 0 0 0 0 1.414l2.376 2.377a1 1 0 0 0 1.414 0l2.377-2.377a1 1 0 0 0 0-1.414l-2.377-2.377a1 1 0 0 0-1.414 0z",key:"1uwlt4"}],["path",{d:"M2.297 11.293a1 1 0 0 0 0 1.414l2.377 2.377a1 1 0 0 0 1.414 0l2.377-2.377a1 1 0 0 0 0-1.414L6.088 8.916a1 1 0 0 0-1.414 0z",key:"10291m"}],["path",{d:"M8.916 17.912a1 1 0 0 0 0 1.415l2.377 2.376a1 1 0 0 0 1.414 0l2.377-2.376a1 1 0 0 0 0-1.415l-2.377-2.376a1 1 0 0 0-1.414 0z",key:"1tqoq1"}],["path",{d:"M8.916 4.674a1 1 0 0 0 0 1.414l2.377 2.376a1 1 0 0 0 1.414 0l2.377-2.376a1 1 0 0 0 0-1.414l-2.377-2.377a1 1 0 0 0-1.414 0z",key:"1x6lto"}]]);/**
|
|
37
32
|
* @license lucide-react v0.453.0 - ISC
|
|
38
33
|
*
|
|
39
34
|
* This source code is licensed under the ISC license.
|
|
40
35
|
* See the LICENSE file in the root directory of this source tree.
|
|
41
|
-
*/const
|
|
36
|
+
*/const nt=s.createLucideIcon("EyeClosed",[["path",{d:"m15 18-.722-3.25",key:"1j64jw"}],["path",{d:"M2 8a10.645 10.645 0 0 0 20 0",key:"1e7gxb"}],["path",{d:"m20 15-1.726-2.05",key:"1cnuld"}],["path",{d:"m4 15 1.726-2.05",key:"1dsqqd"}],["path",{d:"m9 18 .722-3.25",key:"ypw2yx"}]]);/**
|
|
42
37
|
* @license lucide-react v0.453.0 - ISC
|
|
43
38
|
*
|
|
44
39
|
* This source code is licensed under the ISC license.
|
|
@@ -48,82 +43,82 @@
|
|
|
48
43
|
*
|
|
49
44
|
* This source code is licensed under the ISC license.
|
|
50
45
|
* See the LICENSE file in the root directory of this source tree.
|
|
51
|
-
*/const
|
|
46
|
+
*/const rt=s.createLucideIcon("FileJson2",[["path",{d:"M4 22h14a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v4",key:"1pf5j1"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}],["path",{d:"M4 12a1 1 0 0 0-1 1v1a1 1 0 0 1-1 1 1 1 0 0 1 1 1v1a1 1 0 0 0 1 1",key:"fq0c9t"}],["path",{d:"M8 18a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1 1 1 0 0 1-1-1v-1a1 1 0 0 0-1-1",key:"4gibmv"}]]);/**
|
|
52
47
|
* @license lucide-react v0.453.0 - ISC
|
|
53
48
|
*
|
|
54
49
|
* This source code is licensed under the ISC license.
|
|
55
50
|
* See the LICENSE file in the root directory of this source tree.
|
|
56
|
-
*/const
|
|
51
|
+
*/const lt=s.createLucideIcon("Fullscreen",[["path",{d:"M3 7V5a2 2 0 0 1 2-2h2",key:"aa7l1z"}],["path",{d:"M17 3h2a2 2 0 0 1 2 2v2",key:"4qcy5o"}],["path",{d:"M21 17v2a2 2 0 0 1-2 2h-2",key:"6vwrx8"}],["path",{d:"M7 21H5a2 2 0 0 1-2-2v-2",key:"ioqczr"}],["rect",{width:"10",height:"8",x:"7",y:"8",rx:"1",key:"vys8me"}]]);/**
|
|
57
52
|
* @license lucide-react v0.453.0 - ISC
|
|
58
53
|
*
|
|
59
54
|
* This source code is licensed under the ISC license.
|
|
60
55
|
* See the LICENSE file in the root directory of this source tree.
|
|
61
|
-
*/const
|
|
56
|
+
*/const ot=s.createLucideIcon("GitCompareArrows",[["circle",{cx:"5",cy:"6",r:"3",key:"1qnov2"}],["path",{d:"M12 6h5a2 2 0 0 1 2 2v7",key:"1yj91y"}],["path",{d:"m15 9-3-3 3-3",key:"1lwv8l"}],["circle",{cx:"19",cy:"18",r:"3",key:"1qljk2"}],["path",{d:"M12 18H7a2 2 0 0 1-2-2V9",key:"16sdep"}],["path",{d:"m9 15 3 3-3 3",key:"1m3kbl"}]]);/**
|
|
62
57
|
* @license lucide-react v0.453.0 - ISC
|
|
63
58
|
*
|
|
64
59
|
* This source code is licensed under the ISC license.
|
|
65
60
|
* See the LICENSE file in the root directory of this source tree.
|
|
66
|
-
*/const
|
|
61
|
+
*/const it=s.createLucideIcon("Hammer",[["path",{d:"m15 12-8.373 8.373a1 1 0 1 1-3-3L12 9",key:"eefl8a"}],["path",{d:"m18 15 4-4",key:"16gjal"}],["path",{d:"m21.5 11.5-1.914-1.914A2 2 0 0 1 19 8.172V7l-2.26-2.26a6 6 0 0 0-4.202-1.756L9 2.96l.92.82A6.18 6.18 0 0 1 12 8.4V10l2 2h1.172a2 2 0 0 1 1.414.586L18.5 14.5",key:"b7pghm"}]]);/**
|
|
67
62
|
* @license lucide-react v0.453.0 - ISC
|
|
68
63
|
*
|
|
69
64
|
* This source code is licensed under the ISC license.
|
|
70
65
|
* See the LICENSE file in the root directory of this source tree.
|
|
71
|
-
*/const
|
|
66
|
+
*/const ct=s.createLucideIcon("LayoutList",[["rect",{width:"7",height:"7",x:"3",y:"3",rx:"1",key:"1g98yp"}],["rect",{width:"7",height:"7",x:"3",y:"14",rx:"1",key:"1bb6yr"}],["path",{d:"M14 4h7",key:"3xa0d5"}],["path",{d:"M14 9h7",key:"1icrd9"}],["path",{d:"M14 15h7",key:"1mj8o2"}],["path",{d:"M14 20h7",key:"11slyb"}]]);/**
|
|
72
67
|
* @license lucide-react v0.453.0 - ISC
|
|
73
68
|
*
|
|
74
69
|
* This source code is licensed under the ISC license.
|
|
75
70
|
* See the LICENSE file in the root directory of this source tree.
|
|
76
|
-
*/const
|
|
71
|
+
*/const dt=s.createLucideIcon("Library",[["path",{d:"m16 6 4 14",key:"ji33uf"}],["path",{d:"M12 6v14",key:"1n7gus"}],["path",{d:"M8 8v12",key:"1gg7y9"}],["path",{d:"M4 4v16",key:"6qkkli"}]]);/**
|
|
77
72
|
* @license lucide-react v0.453.0 - ISC
|
|
78
73
|
*
|
|
79
74
|
* This source code is licensed under the ISC license.
|
|
80
75
|
* See the LICENSE file in the root directory of this source tree.
|
|
81
|
-
*/const
|
|
76
|
+
*/const ut=s.createLucideIcon("Link2",[["path",{d:"M9 17H7A5 5 0 0 1 7 7h2",key:"8i5ue5"}],["path",{d:"M15 7h2a5 5 0 1 1 0 10h-2",key:"1b9ql8"}],["line",{x1:"8",x2:"16",y1:"12",y2:"12",key:"1jonct"}]]);/**
|
|
82
77
|
* @license lucide-react v0.453.0 - ISC
|
|
83
78
|
*
|
|
84
79
|
* This source code is licensed under the ISC license.
|
|
85
80
|
* See the LICENSE file in the root directory of this source tree.
|
|
86
|
-
*/const
|
|
81
|
+
*/const mt=s.createLucideIcon("ListEnd",[["path",{d:"M16 12H3",key:"1a2rj7"}],["path",{d:"M16 6H3",key:"1wxfjs"}],["path",{d:"M10 18H3",key:"13769t"}],["path",{d:"M21 6v10a2 2 0 0 1-2 2h-5",key:"ilrcs8"}],["path",{d:"m16 16-2 2 2 2",key:"kkc6pm"}]]);/**
|
|
87
82
|
* @license lucide-react v0.453.0 - ISC
|
|
88
83
|
*
|
|
89
84
|
* This source code is licensed under the ISC license.
|
|
90
85
|
* See the LICENSE file in the root directory of this source tree.
|
|
91
|
-
*/const
|
|
86
|
+
*/const xt=s.createLucideIcon("ListFilter",[["path",{d:"M3 6h18",key:"d0wm0j"}],["path",{d:"M7 12h10",key:"b7w52i"}],["path",{d:"M10 18h4",key:"1ulq68"}]]);/**
|
|
92
87
|
* @license lucide-react v0.453.0 - ISC
|
|
93
88
|
*
|
|
94
89
|
* This source code is licensed under the ISC license.
|
|
95
90
|
* See the LICENSE file in the root directory of this source tree.
|
|
96
|
-
*/const
|
|
91
|
+
*/const ht=s.createLucideIcon("ListStart",[["path",{d:"M16 12H3",key:"1a2rj7"}],["path",{d:"M16 18H3",key:"12xzn7"}],["path",{d:"M10 6H3",key:"lf8lx7"}],["path",{d:"M21 18V8a2 2 0 0 0-2-2h-5",key:"1hghli"}],["path",{d:"m16 8-2-2 2-2",key:"160uvd"}]]);/**
|
|
97
92
|
* @license lucide-react v0.453.0 - ISC
|
|
98
93
|
*
|
|
99
94
|
* This source code is licensed under the ISC license.
|
|
100
95
|
* See the LICENSE file in the root directory of this source tree.
|
|
101
|
-
*/const
|
|
96
|
+
*/const pt=s.createLucideIcon("ListX",[["path",{d:"M11 12H3",key:"51ecnj"}],["path",{d:"M16 6H3",key:"1wxfjs"}],["path",{d:"M16 18H3",key:"12xzn7"}],["path",{d:"m19 10-4 4",key:"1tz659"}],["path",{d:"m15 10 4 4",key:"1n7nei"}]]);/**
|
|
102
97
|
* @license lucide-react v0.453.0 - ISC
|
|
103
98
|
*
|
|
104
99
|
* This source code is licensed under the ISC license.
|
|
105
100
|
* See the LICENSE file in the root directory of this source tree.
|
|
106
|
-
*/const
|
|
101
|
+
*/const Ve=s.createLucideIcon("MoveHorizontal",[["path",{d:"m18 8 4 4-4 4",key:"1ak13k"}],["path",{d:"M2 12h20",key:"9i4pu4"}],["path",{d:"m6 8-4 4 4 4",key:"15zrgr"}]]);/**
|
|
107
102
|
* @license lucide-react v0.453.0 - ISC
|
|
108
103
|
*
|
|
109
104
|
* This source code is licensed under the ISC license.
|
|
110
105
|
* See the LICENSE file in the root directory of this source tree.
|
|
111
|
-
*/const
|
|
106
|
+
*/const ze=s.createLucideIcon("MoveVertical",[["path",{d:"M12 2v20",key:"t6zp3m"}],["path",{d:"m8 18 4 4 4-4",key:"bh5tu3"}],["path",{d:"m8 6 4-4 4 4",key:"ybng9g"}]]);/**
|
|
112
107
|
* @license lucide-react v0.453.0 - ISC
|
|
113
108
|
*
|
|
114
109
|
* This source code is licensed under the ISC license.
|
|
115
110
|
* See the LICENSE file in the root directory of this source tree.
|
|
116
|
-
*/const
|
|
111
|
+
*/const gt=s.createLucideIcon("Palette",[["circle",{cx:"13.5",cy:"6.5",r:".5",fill:"currentColor",key:"1okk4w"}],["circle",{cx:"17.5",cy:"10.5",r:".5",fill:"currentColor",key:"f64h9f"}],["circle",{cx:"8.5",cy:"7.5",r:".5",fill:"currentColor",key:"fotxhn"}],["circle",{cx:"6.5",cy:"12.5",r:".5",fill:"currentColor",key:"qy21gx"}],["path",{d:"M12 2C6.5 2 2 6.5 2 12s4.5 10 10 10c.926 0 1.648-.746 1.648-1.688 0-.437-.18-.835-.437-1.125-.29-.289-.438-.652-.438-1.125a1.64 1.64 0 0 1 1.668-1.668h1.996c3.051 0 5.555-2.503 5.555-5.554C21.965 6.012 17.461 2 12 2z",key:"12rzf8"}]]);/**
|
|
117
112
|
* @license lucide-react v0.453.0 - ISC
|
|
118
113
|
*
|
|
119
114
|
* This source code is licensed under the ISC license.
|
|
120
115
|
* See the LICENSE file in the root directory of this source tree.
|
|
121
|
-
*/const
|
|
116
|
+
*/const ft=s.createLucideIcon("PanelRightClose",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M15 3v18",key:"14nvp0"}],["path",{d:"m8 9 3 3-3 3",key:"12hl5m"}]]);/**
|
|
122
117
|
* @license lucide-react v0.453.0 - ISC
|
|
123
118
|
*
|
|
124
119
|
* This source code is licensed under the ISC license.
|
|
125
120
|
* See the LICENSE file in the root directory of this source tree.
|
|
126
|
-
*/const
|
|
121
|
+
*/const jt=s.createLucideIcon("PanelRight",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M15 3v18",key:"14nvp0"}]]);/**
|
|
127
122
|
* @license lucide-react v0.453.0 - ISC
|
|
128
123
|
*
|
|
129
124
|
* This source code is licensed under the ISC license.
|
|
@@ -143,38 +138,38 @@
|
|
|
143
138
|
*
|
|
144
139
|
* This source code is licensed under the ISC license.
|
|
145
140
|
* See the LICENSE file in the root directory of this source tree.
|
|
146
|
-
*/const
|
|
141
|
+
*/const bt=s.createLucideIcon("Spline",[["circle",{cx:"19",cy:"5",r:"2",key:"mhkx31"}],["circle",{cx:"5",cy:"19",r:"2",key:"v8kfzx"}],["path",{d:"M5 17A12 12 0 0 1 17 5",key:"1okkup"}]]);/**
|
|
147
142
|
* @license lucide-react v0.453.0 - ISC
|
|
148
143
|
*
|
|
149
144
|
* This source code is licensed under the ISC license.
|
|
150
145
|
* See the LICENSE file in the root directory of this source tree.
|
|
151
|
-
*/const
|
|
146
|
+
*/const vt=s.createLucideIcon("SquareFunction",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",ry:"2",key:"1m3agn"}],["path",{d:"M9 17c2 0 2.8-1 2.8-2.8V10c0-2 1-3.3 3.2-3",key:"m1af9g"}],["path",{d:"M9 11.2h5.7",key:"3zgcl2"}]]);/**
|
|
152
147
|
* @license lucide-react v0.453.0 - ISC
|
|
153
148
|
*
|
|
154
149
|
* This source code is licensed under the ISC license.
|
|
155
150
|
* See the LICENSE file in the root directory of this source tree.
|
|
156
|
-
*/const
|
|
151
|
+
*/const yt=s.createLucideIcon("SquareSplitVertical",[["path",{d:"M5 8V5c0-1 1-2 2-2h10c1 0 2 1 2 2v3",key:"1pi83i"}],["path",{d:"M19 16v3c0 1-1 2-2 2H7c-1 0-2-1-2-2v-3",key:"ido5k7"}],["line",{x1:"4",x2:"20",y1:"12",y2:"12",key:"1e0a9i"}]]);/**
|
|
157
152
|
* @license lucide-react v0.453.0 - ISC
|
|
158
153
|
*
|
|
159
154
|
* This source code is licensed under the ISC license.
|
|
160
155
|
* See the LICENSE file in the root directory of this source tree.
|
|
161
|
-
*/const
|
|
156
|
+
*/const Nt=s.createLucideIcon("Square",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}]]);/**
|
|
162
157
|
* @license lucide-react v0.453.0 - ISC
|
|
163
158
|
*
|
|
164
159
|
* This source code is licensed under the ISC license.
|
|
165
160
|
* See the LICENSE file in the root directory of this source tree.
|
|
166
|
-
*/const
|
|
161
|
+
*/const Ct=s.createLucideIcon("Tags",[["path",{d:"m15 5 6.3 6.3a2.4 2.4 0 0 1 0 3.4L17 19",key:"1cbfv1"}],["path",{d:"M9.586 5.586A2 2 0 0 0 8.172 5H3a1 1 0 0 0-1 1v5.172a2 2 0 0 0 .586 1.414L8.29 18.29a2.426 2.426 0 0 0 3.42 0l3.58-3.58a2.426 2.426 0 0 0 0-3.42z",key:"135mg7"}],["circle",{cx:"6.5",cy:"9.5",r:".5",fill:"currentColor",key:"5pm5xn"}]]);/**
|
|
167
162
|
* @license lucide-react v0.453.0 - ISC
|
|
168
163
|
*
|
|
169
164
|
* This source code is licensed under the ISC license.
|
|
170
165
|
* See the LICENSE file in the root directory of this source tree.
|
|
171
|
-
*/const
|
|
166
|
+
*/const St=s.createLucideIcon("Wand",[["path",{d:"M15 4V2",key:"z1p9b7"}],["path",{d:"M15 16v-2",key:"px0unx"}],["path",{d:"M8 9h2",key:"1g203m"}],["path",{d:"M20 9h2",key:"19tzq7"}],["path",{d:"M17.8 11.8 19 13",key:"yihg8r"}],["path",{d:"M15 9h.01",key:"x1ddxp"}],["path",{d:"M17.8 6.2 19 5",key:"fd4us0"}],["path",{d:"m3 21 9-9",key:"1jfql5"}],["path",{d:"M12.2 6.2 11 5",key:"i3da3b"}]]);function ks({storeValue:t,defaultValue:n="",onStoreUpdate:a,delay:l=300}){const[c,d]=L.useState(t??n),r=s.useDebounce(c,l),i=L.useRef(t),o=L.useRef(!1);L.useEffect(()=>{t!==i.current&&(d(t??n),i.current=t)},[t,n]),L.useEffect(()=>{o.current&&r!==i.current&&(a(r),i.current=r)},[r,a]);const u=L.useCallback(m=>{o.current=!0,d(m??n)},[n]);return{value:c,onChange:u}}function wt({initialJoinPlan:t,availableDataSources:n}){const[a,l]=L.useState(t),[c,d]=L.useState(new Set),r=()=>{const T=a.joins.length>0?Math.max(...a.joins.map(C=>C.sequence))+1:1,N={id:`join-${Date.now()}`,source:{type:"table",name:"",database:"",schema:""},target:{type:"table",name:"",database:"",schema:""},joinType:"INNER",sequence:T,joinKeyGroups:[{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}]};l({...a,joins:[...a.joins,N]}),d(new Set([...c,N.id]))},i=T=>{l({...a,joins:a.joins.filter(N=>N.id!==T)}),d(new Set([...c].filter(N=>N!==T)))},o=(T,N)=>{var S,D,F,I,M;const C=a.joins.find(E=>E.id===T);if(!C)return;const k={...C,...N};if((S=k.source)!=null&&S.name&&((D=k.target)!=null&&D.name)&&k.source.name===k.target.name){s.ue.error("Self-joins are not yet supported",{description:`Joining ${k.source.name} to itself is not currently supported. Please select a different target table.`});return}const R=a.joins.filter(E=>{var B;return E.id!==T&&E.target.name===((B=k.target)==null?void 0:B.name)});if((F=k.target)!=null&&F.name&&R.length>0){s.ue.error("Duplicate join targets are not supported",{description:`The table ${k.target.name} is already used as a target in another join. Each table can only be joined once.`});return}const b=a.joins.map(E=>E.id===T?k:E);if((I=k.source)!=null&&I.name&&((M=k.target)!=null&&M.name)&&k.joinKeyGroups[0].keys.length===0){const E=n.find(q=>{var G;return q.name===((G=k.source)==null?void 0:G.name)}),B=n.find(q=>{var G;return q.name===((G=k.target)==null?void 0:G.name)}),$={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",qualifiedFieldName:"",entityId:(E==null?void 0:E.name)||"",entityName:(E==null?void 0:E.name)||"",entityType:(E==null?void 0:E.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",qualifiedFieldName:"",entityId:(B==null?void 0:B.name)||"",entityName:(B==null?void 0:B.name)||"",entityType:(B==null?void 0:B.type)||"table"},operator:"="},V=[...k.joinKeyGroups];V[0]={...V[0],keys:[$]},k.joinKeyGroups=V,b[b.findIndex(q=>q.id===T)]=k}l({...a,joins:b})};return{joinPlan:a,expandedJoins:c,setJoinPlan:l,addJoin:r,removeJoin:i,updateJoin:o,moveJoinUp:T=>{const N=a.joins.findIndex(C=>C.id===T);if(N>0){const C=[...a.joins],k={...C[N]},R={...C[N-1]};C[N]=R,C[N-1]=k,C.forEach((S,D)=>{S.sequence=D+1});const b={...a,joins:C};l(b)}},moveJoinDown:T=>{const N=a.joins.findIndex(C=>C.id===T);if(N<a.joins.length-1){const C=[...a.joins],k={...C[N]},R={...C[N+1]};C[N]=R,C[N+1]=k,C.forEach((S,D)=>{S.sequence=D+1});const b={...a,joins:C};l(b)}},toggleJoinExpansion:T=>{const N=new Set(c);N.has(T)?N.delete(T):N.add(T),d(N)},getAvailableSourceEntities:T=>{if(a.joins.length===0)return n;const N=a.joins.findIndex(k=>k.id===T);if(N===0)return n.filter(k=>k.name===a.baseEntity.name);const C=new Set;return a.baseEntity.name&&C.add(a.baseEntity.name),a.joins.slice(0,N).forEach(k=>{C.add(k.source.name),C.add(k.target.name)}),n.filter(k=>C.has(k.name))},getAvailableTargetEntities:T=>n.filter(N=>N.name!==T),addJoinKeyGroup:T=>{const N=a.joins.find(k=>k.id===T);if(!N)return;const C=[...N.joinKeyGroups,{id:"group-"+Date.now()+"-"+Math.random().toString(36).substr(2,9),operator:"AND",keys:[]}];o(T,{joinKeyGroups:C})},updateGroupOperator:(T,N,C)=>{const k=a.joins.find(b=>b.id===T);if(!k)return;const R=[...k.joinKeyGroups];R[N]={...R[N],operator:C},o(T,{joinKeyGroups:R})},removeJoinKeyGroup:(T,N)=>{const C=a.joins.find(R=>R.id===T);if(!C)return;const k=[...C.joinKeyGroups];k.splice(N,1),o(T,{joinKeyGroups:k})},addJoinKey:(T,N)=>{const C=a.joins.find(D=>D.id===T);if(!C)return;const k=n.find(D=>D.name===C.source.name),R=n.find(D=>D.name===C.target.name),b=[...C.joinKeyGroups],S={id:`key-${Date.now()}`,source:{id:`source-${Date.now()}`,name:"",dataType:"",label:"",qualifiedFieldName:"",entityId:(k==null?void 0:k.name)||"",entityName:(k==null?void 0:k.name)||"",entityType:(k==null?void 0:k.type)||"table"},target:{id:`target-${Date.now()}`,name:"",dataType:"",label:"",qualifiedFieldName:"",entityId:(R==null?void 0:R.name)||"",entityName:(R==null?void 0:R.name)||"",entityType:(R==null?void 0:R.type)||"table"},operator:"="};b[N]={...b[N],keys:[...b[N].keys,S]},o(T,{joinKeyGroups:b})},updateJoinKey:(T,N,C,k,R)=>{const b=a.joins.find(M=>M.id===T);if(!b)return;const S=n.find(M=>M.name===b.source.name),D=n.find(M=>M.name===b.target.name),F=[...b.joinKeyGroups],I=F[N].keys[C];k==="sourceColumnName"&&S?F[N].keys[C]={...I,source:{...I.source,name:R}}:k==="targetColumnName"&&D?F[N].keys[C]={...I,target:{...I.target,name:R}}:k==="operator"&&(F[N].keys[C]={...I,operator:R}),o(T,{joinKeyGroups:F})},removeJoinKey:(T,N,C)=>{const k=a.joins.find(b=>b.id===T);if(!k)return;const R=[...k.joinKeyGroups];R[N].keys.splice(C,1),o(T,{joinKeyGroups:R})},generateSQLPreview:()=>{const T=[...a.joins].sort((k,R)=>k.sequence-R.sequence),N=a.baseEntity;if(!N.name)return"SELECT * FROM table -- Please select a base entity";let C=`SELECT *
|
|
172
167
|
FROM ${N.name}`;return T.forEach(k=>{const R=n.find(D=>D.name===k.target.name);if(!R)return;const b=k.joinKeyGroups.filter(D=>D.keys&&D.keys.length>0).map(D=>{const F=D.keys.filter(I=>{var M,E,B,$;return((M=I.source)==null?void 0:M.name)&&((E=I.target)==null?void 0:E.name)&&((B=I.source)==null?void 0:B.entityName)&&(($=I.target)==null?void 0:$.entityName)}).map(I=>`${I.source.entityName}.${I.source.name} ${I.operator} ${I.target.entityName}.${I.target.name}`);return F.length>1?`(${F.join(` ${D.operator} `)})`:F[0]}).filter(Boolean),S=b.length>1?b.join(" OR "):b[0]||"condition_here";C+=`
|
|
173
168
|
${k.joinType} JOIN ${R.name}
|
|
174
|
-
ON ${S}`}),C},isValidConfiguration:()=>!(!a.baseEntity.name||a.joins.some(k=>!k.source.name||!k.target.name)||a.joins.some(k=>k.joinKeyGroups.some(R=>R.keys.length===0))||a.joins.some(k=>k.joinKeyGroups.some(R=>R.keys.some(b=>!b.source.name||!b.target.name||!b.operator))))}}function Et({joinPlan:t,availableDataSources:n,onSave:a,onRemove:l,onCancel:c}){const{setCardConfig:d}=s.useEditorActions();s.useEditorStore(b=>b.card.config);const{columnGroups:r}=s.useColumns(),{joinPlan:i,expandedJoins:o,setJoinPlan:u,addJoin:m,removeJoin:x,updateJoin:h,moveJoinUp:w,moveJoinDown:j,toggleJoinExpansion:f,getAvailableSourceEntities:p,getAvailableTargetEntities:g,addJoinKeyGroup:v,updateGroupOperator:O,removeJoinKeyGroup:A,addJoinKey:y,updateJoinKey:T,removeJoinKey:N,generateSQLPreview:C,isValidConfiguration:k}=Tt({initialJoinPlan:t,availableDataSources:n}),R=b=>{const S=r.find(D=>D.entityName===b);return(S==null?void 0:S.columns)||[]};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Base Entity (FROM clause)"}),e.jsxs(s.Select,{value:i.baseEntity.name,onValueChange:b=>{const S=n.find(D=>D.name===b);u({...i,baseEntity:{connectionId:(S==null?void 0:S.connectionId)||"",connectionType:(S==null?void 0:S.connectionType)||"none",type:(S==null?void 0:S.type)||"table",name:b,database:S==null?void 0:S.database,schema:S==null?void 0:S.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select base entity..."})}),e.jsx(s.SelectContent,{children:n.map(b=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:b.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:b.type}),b.name,e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]})},b.name))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This entity will be used as the base table in the FROM clause of the SQL query."})]}),e.jsxs("div",{className:"rounded-md border p-3",children:[e.jsx("div",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Join SQL Preview"}),e.jsx("code",{className:"block whitespace-pre-wrap break-all text-xs",children:C()})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(s.Label,{children:["Joins Configuration (",i.joins.length,")"]}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:m,children:[e.jsx(s.Plus,{className:"mr-1 h-4 w-4"}),"Add Join"]})})]}),i.joins.length===0?e.jsx("div",{className:"rounded-lg border border-dashed py-8 text-center text-muted-foreground",children:e.jsx("p",{children:"No joins configured. Add a join to connect multiple data sources."})}):e.jsx("div",{className:"max-h-[300px] space-y-4 overflow-y-auto",children:i.joins.map(b=>{const S=n.find(I=>I.name===b.source.name),D=n.find(I=>I.name===b.target.name),F=o.has(b.id);return e.jsxs(s.Card,{className:"border",children:[e.jsx(s.CardHeader,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{children:b.sequence}),e.jsxs(s.CardTitle,{className:"text-sm",children:["Join ",b.sequence,S&&D&&e.jsxs("span",{className:"ml-2 font-normal text-gray-600",children:["(",S.name," → ",D.name,")"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>w(b.id),disabled:b.sequence===1,children:e.jsx(s.ArrowUp,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>j(b.id),disabled:b.sequence===i.joins.length,children:e.jsx(s.ArrowDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>f(b.id),children:F?e.jsx(s.ChevronUp,{className:"h-4 w-4"}):e.jsx(s.ChevronDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>x(b.id),children:e.jsx(s.X,{className:"h-4 w-4"})})]})]})}),F&&e.jsxs(s.CardContent,{className:"space-y-4 pb-6",children:[e.jsxs("div",{className:"grid grid-cols-1 gap-4 md:grid-cols-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Source"}),e.jsxs(s.Select,{value:b.source.name,onValueChange:I=>{const M=n.find(E=>E.name===I);h(b.id,{source:{connectionId:(M==null?void 0:M.connectionId)||"",connectionType:(M==null?void 0:M.connectionType)||"none",type:(M==null?void 0:M.type)||"table",name:I,database:M==null?void 0:M.database,schema:M==null?void 0:M.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select source..."})}),e.jsx(s.SelectContent,{children:p(b.id).map(I=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:I.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:I.type}),I.name]})},I.name))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Join Type"}),e.jsxs(s.Select,{value:b.joinType,onValueChange:I=>h(b.id,{joinType:I}),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"INNER",children:"INNER JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"LEFT",children:"LEFT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"RIGHT",children:"RIGHT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"FULL",children:"FULL JOIN"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Target"}),e.jsxs(s.Select,{value:b.target.name,onValueChange:I=>{const M=n.find(E=>E.name===I);h(b.id,{target:{connectionId:(M==null?void 0:M.connectionId)||"",connectionType:(M==null?void 0:M.connectionType)||"none",type:(M==null?void 0:M.type)||"table",name:I,database:M==null?void 0:M.database,schema:M==null?void 0:M.schema}})},disabled:!b.source.name,children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select target..."})}),e.jsx(s.SelectContent,{children:b.source.name?g(b.source.name).map(I=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:I.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:I.type}),I.name,I.name===b.source.name&&e.jsx(s.Badge,{variant:"outline",className:"text-xs text-yellow-600",children:"(Self-join)"})]})},I.name)):[]})]})]})]}),b.source.name&&b.target.name&&S&&D&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{children:"Join Conditions"}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",onClick:()=>v(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition Group"]})]}),b.joinKeyGroups.length===0?e.jsx("div",{className:"rounded-md border p-3 text-center text-sm text-gray-500",children:"No join conditions defined. Add condition groups to specify how data sources should be joined."}):e.jsx("div",{className:"space-y-4",children:b.joinKeyGroups.map((I,M)=>e.jsxs("div",{className:"space-y-3 rounded-md border p-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Label,{children:["Group ",M+1]}),M>0&&e.jsxs("div",{className:"flex items-center gap-2 rounded-md bg-gray-100 px-2 py-1",children:[e.jsx(s.Label,{className:"text-xs font-normal",children:"Join with:"}),e.jsx("span",{className:"text-sm font-medium",children:"OR"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Select,{value:I.operator,onValueChange:E=>O(b.id,M,E),children:[e.jsx(s.SelectTrigger,{className:"w-[100px]",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"AND",children:"AND"}),e.jsx(s.SelectItem,{value:"OR",children:"OR"})]})]}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",onClick:()=>A(b.id,M),children:e.jsx(s.Trash2,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"space-y-2",children:[I.keys.length===0?e.jsx("div",{className:"rounded-md border border-dashed p-3 text-center text-sm text-gray-500",children:"No conditions in this group. Add conditions to specify join criteria."}):e.jsx("div",{className:"space-y-2",children:I.keys.map((E,B)=>e.jsxs("div",{className:"flex flex-col items-start gap-2 rounded-md border p-2 sm:flex-row sm:items-center",children:[B>0&&e.jsx("div",{className:"self-center rounded-md bg-gray-100 px-2 py-1 text-xs font-medium",children:I.operator}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:E.source.name,onValueChange:$=>T(b.id,M,B,"sourceColumnName",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Source column"})}),e.jsx(s.SelectContent,{children:R(b.source.name).map($=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:$.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between",children:[e.jsx("span",{children:$.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:$.dataType})]})},$.name))})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:w-20",children:e.jsxs(s.Select,{value:E.operator,onValueChange:$=>T(b.id,M,B,"operator",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"=",children:"="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<",children:"<"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">",children:">"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<=",children:"<="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">=",children:">="})]})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:E.target.name,onValueChange:$=>T(b.id,M,B,"targetColumnName",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Target column"})}),e.jsx(s.SelectContent,{children:R(b.target.name).map($=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:$.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between hover:bg-muted",children:[e.jsx("span",{children:$.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:$.dataType})]})},$.name))})]})}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",className:"self-center",onClick:()=>N(b.id,M,B),children:e.jsx(s.X,{className:"size-3.5"})})]},E.id||B))}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",className:"mt-2",onClick:()=>y(b.id,M),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition"]})]})]},I.id))})]})]})]},b.id)})})]}),e.jsxs("div",{className:"flex justify-end gap-2 border-t pt-4",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>{l(),c()},children:"Remove All"}),e.jsx(s.Button,{variant:"outline",onClick:c,children:"Cancel"}),e.jsx(s.Button,{onClick:()=>a(i),disabled:!k(),children:"Save"})]})]})}function Is({children:t,open:n,onOpenChange:a,joinPlan:l,availableDataSources:c,onRemove:d,onSave:r,onCancel:i}){return e.jsxs(s.Dialog,{open:n,onOpenChange:a,children:[e.jsxs(s.DialogContent,{className:"max-w-4xl",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Join Configuration"}),e.jsx(s.DialogDescription,{children:"Configure the join between the two entities to combine the fields."})]}),e.jsx(Et,{joinPlan:l,availableDataSources:c,onSave:r,onCancel:i,onRemove:d})]}),t]})}function kt({joins:t,baseEntityId:n,availableDataSources:a,generateSQLPreview:l}){return e.jsxs("div",{className:"my-2 max-h-[400px] divide-y divide-border overflow-y-auto",children:[t.length>0&&e.jsxs("div",{className:"px-4 py-2",children:[e.jsx("h3",{className:"mb-3 text-left font-semibold",children:"Join Chain"}),e.jsx("div",{className:"flex flex-wrap items-center gap-2",children:(()=>{const c=[...t].sort((r,i)=>r.sequence-i.sequence),d=a.find(r=>r.name===n);return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:d==null?void 0:d.type}),e.jsx("span",{className:"text-xs font-medium",children:d==null?void 0:d.name}),e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]}),c.map(r=>{const i=a.find(o=>o.name===r.target.name);return e.jsxs("div",{className:"flex items-center",children:[e.jsxs("div",{className:"mx-1 flex items-center gap-1",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:r.joinType}),e.jsx("span",{className:"text-muted-foreground",children:"→"})]}),e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:i==null?void 0:i.type}),e.jsx("span",{className:"text-xs font-medium",children:i==null?void 0:i.name})]})]},r.id)})]})})()})]}),e.jsxs("div",{className:"p-4",children:[e.jsxs("h3",{className:"mb-3 text-left text-sm font-medium",children:["Configured Joins (",t.length,")"]}),e.jsx("div",{className:"space-y-2",children:[...t].sort((c,d)=>c.sequence-d.sequence).map((c,d)=>{const r=a.find(u=>u.name===c.source.name),i=a.find(u=>u.name===c.target.name),o=c.joinKeyGroups.reduce((u,m)=>u+m.keys.length,0);return e.jsxs("div",{className:"rounded-md bg-muted p-2 text-left",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"secondary",className:"shrink-0 text-xs",children:d+1}),e.jsx(s.Badge,{variant:"outline",className:"shrink-0 text-xs",children:c.joinType}),e.jsxs(s.Badge,{variant:"outline",className:"ml-auto shrink-0 text-xs",children:[o," condition",o>1?"s":""]})]}),e.jsxs("div",{className:"mt-1 truncate pl-1 text-xs text-muted-foreground",children:[r==null?void 0:r.name," → ",i==null?void 0:i.name]})]},c.id)})})]}),e.jsxs("div",{className:"p-4",children:[e.jsx("h3",{className:"mb-3 text-left text-sm font-medium",children:"SQL Query"}),e.jsx("div",{className:"overflow-x-auto rounded-md border bg-muted p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap break-all text-left font-mono text-xs text-muted-foreground",children:l()})})]})]})}function os({open:t,onOpenChange:n,variant:a="button"}={}){var v,O;const l=s.useEditorStore(A=>A.card),{autoJoinMetadata:c}=s.useDashboardCardQuery(l),d=s.useEditorStore(A=>A.actions.updateCardConfig),r=((v=l==null?void 0:l.config)==null?void 0:v.joinPlan)||{baseEntity:{type:"table",name:"",database:"",schema:"",connectionId:"",connectionType:"none",dialect:"none"},joins:[]},[i,o]=L.useState(!1),u=t!==void 0,m=u?t:i,x=u?n:o,h=((O=l==null?void 0:l.dataSource)==null?void 0:O.selectedEntities)||[],w=A=>{d(l.id,{...l==null?void 0:l.config,joinPlan:A}),x(!1)},j=()=>{d(l.id,{...l==null?void 0:l.config,joinPlan:void 0})},f=()=>{x(!1)},p=()=>{const A=[...r.joins].sort((N,C)=>N.sequence-C.sequence),y=r.baseEntity;if(!y.name)return"SELECT * FROM table -- Please select a base entity";let T=`SELECT *
|
|
169
|
+
ON ${S}`}),C},isValidConfiguration:()=>!(!a.baseEntity.name||a.joins.some(k=>!k.source.name||!k.target.name)||a.joins.some(k=>k.joinKeyGroups.some(R=>R.keys.length===0))||a.joins.some(k=>k.joinKeyGroups.some(R=>R.keys.some(b=>!b.source.name||!b.target.name||!b.operator))))}}function Tt({joinPlan:t,availableDataSources:n,onSave:a,onRemove:l,onCancel:c}){const{setCardConfig:d}=s.useEditorActions();s.useEditorStore(b=>b.card.config);const{columnGroups:r}=s.useColumns(),{joinPlan:i,expandedJoins:o,setJoinPlan:u,addJoin:m,removeJoin:x,updateJoin:h,moveJoinUp:w,moveJoinDown:j,toggleJoinExpansion:f,getAvailableSourceEntities:p,getAvailableTargetEntities:g,addJoinKeyGroup:v,updateGroupOperator:O,removeJoinKeyGroup:A,addJoinKey:y,updateJoinKey:T,removeJoinKey:N,generateSQLPreview:C,isValidConfiguration:k}=wt({initialJoinPlan:t,availableDataSources:n}),R=b=>{const S=r.find(D=>D.entityName===b);return(S==null?void 0:S.columns)||[]};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Base Entity (FROM clause)"}),e.jsxs(s.Select,{value:i.baseEntity.name,onValueChange:b=>{const S=n.find(D=>D.name===b);u({...i,baseEntity:{connectionId:(S==null?void 0:S.connectionId)||"",connectionType:(S==null?void 0:S.connectionType)||"none",type:(S==null?void 0:S.type)||"table",name:b,database:S==null?void 0:S.database,schema:S==null?void 0:S.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select base entity..."})}),e.jsx(s.SelectContent,{children:n.map(b=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:b.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:b.type}),b.name,e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]})},b.name))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This entity will be used as the base table in the FROM clause of the SQL query."})]}),e.jsxs("div",{className:"rounded-md border p-3",children:[e.jsx("div",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Join SQL Preview"}),e.jsx("code",{className:"block whitespace-pre-wrap break-all text-xs",children:C()})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(s.Label,{children:["Joins Configuration (",i.joins.length,")"]}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:m,children:[e.jsx(s.Plus,{className:"mr-1 h-4 w-4"}),"Add Join"]})})]}),i.joins.length===0?e.jsx("div",{className:"rounded-lg border border-dashed py-8 text-center text-muted-foreground",children:e.jsx("p",{children:"No joins configured. Add a join to connect multiple data sources."})}):e.jsx("div",{className:"max-h-[300px] space-y-4 overflow-y-auto",children:i.joins.map(b=>{const S=n.find(I=>I.name===b.source.name),D=n.find(I=>I.name===b.target.name),F=o.has(b.id);return e.jsxs(s.Card,{className:"border",children:[e.jsx(s.CardHeader,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{children:b.sequence}),e.jsxs(s.CardTitle,{className:"text-sm",children:["Join ",b.sequence,S&&D&&e.jsxs("span",{className:"ml-2 font-normal text-gray-600",children:["(",S.name," → ",D.name,")"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>w(b.id),disabled:b.sequence===1,children:e.jsx(s.ArrowUp,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>j(b.id),disabled:b.sequence===i.joins.length,children:e.jsx(s.ArrowDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>f(b.id),children:F?e.jsx(s.ChevronUp,{className:"h-4 w-4"}):e.jsx(s.ChevronDown,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:()=>x(b.id),children:e.jsx(s.X,{className:"h-4 w-4"})})]})]})}),F&&e.jsxs(s.CardContent,{className:"space-y-4 pb-6",children:[e.jsxs("div",{className:"grid grid-cols-1 gap-4 md:grid-cols-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Source"}),e.jsxs(s.Select,{value:b.source.name,onValueChange:I=>{const M=n.find(E=>E.name===I);h(b.id,{source:{connectionId:(M==null?void 0:M.connectionId)||"",connectionType:(M==null?void 0:M.connectionType)||"none",type:(M==null?void 0:M.type)||"table",name:I,database:M==null?void 0:M.database,schema:M==null?void 0:M.schema}})},children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select source..."})}),e.jsx(s.SelectContent,{children:p(b.id).map(I=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:I.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:I.type}),I.name]})},I.name))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Join Type"}),e.jsxs(s.Select,{value:b.joinType,onValueChange:I=>h(b.id,{joinType:I}),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"INNER",children:"INNER JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"LEFT",children:"LEFT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"RIGHT",children:"RIGHT JOIN"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"FULL",children:"FULL JOIN"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Target"}),e.jsxs(s.Select,{value:b.target.name,onValueChange:I=>{const M=n.find(E=>E.name===I);h(b.id,{target:{connectionId:(M==null?void 0:M.connectionId)||"",connectionType:(M==null?void 0:M.connectionType)||"none",type:(M==null?void 0:M.type)||"table",name:I,database:M==null?void 0:M.database,schema:M==null?void 0:M.schema}})},disabled:!b.source.name,children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Select target..."})}),e.jsx(s.SelectContent,{children:b.source.name?g(b.source.name).map(I=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:I.name,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:I.type}),I.name,I.name===b.source.name&&e.jsx(s.Badge,{variant:"outline",className:"text-xs text-yellow-600",children:"(Self-join)"})]})},I.name)):[]})]})]})]}),b.source.name&&b.target.name&&S&&D&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{children:"Join Conditions"}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",onClick:()=>v(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition Group"]})]}),b.joinKeyGroups.length===0?e.jsx("div",{className:"rounded-md border p-3 text-center text-sm text-gray-500",children:"No join conditions defined. Add condition groups to specify how data sources should be joined."}):e.jsx("div",{className:"space-y-4",children:b.joinKeyGroups.map((I,M)=>e.jsxs("div",{className:"space-y-3 rounded-md border p-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Label,{children:["Group ",M+1]}),M>0&&e.jsxs("div",{className:"flex items-center gap-2 rounded-md bg-gray-100 px-2 py-1",children:[e.jsx(s.Label,{className:"text-xs font-normal",children:"Join with:"}),e.jsx("span",{className:"text-sm font-medium",children:"OR"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Select,{value:I.operator,onValueChange:E=>O(b.id,M,E),children:[e.jsx(s.SelectTrigger,{className:"w-[100px]",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"AND",children:"AND"}),e.jsx(s.SelectItem,{value:"OR",children:"OR"})]})]}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",onClick:()=>A(b.id,M),children:e.jsx(s.Trash2,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"space-y-2",children:[I.keys.length===0?e.jsx("div",{className:"rounded-md border border-dashed p-3 text-center text-sm text-gray-500",children:"No conditions in this group. Add conditions to specify join criteria."}):e.jsx("div",{className:"space-y-2",children:I.keys.map((E,B)=>e.jsxs("div",{className:"flex flex-col items-start gap-2 rounded-md border p-2 sm:flex-row sm:items-center",children:[B>0&&e.jsx("div",{className:"self-center rounded-md bg-gray-100 px-2 py-1 text-xs font-medium",children:I.operator}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:E.source.name,onValueChange:$=>T(b.id,M,B,"sourceColumnName",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Source column"})}),e.jsx(s.SelectContent,{children:R(b.source.name).map($=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:$.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between",children:[e.jsx("span",{children:$.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:$.dataType})]})},$.name))})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:w-20",children:e.jsxs(s.Select,{value:E.operator,onValueChange:$=>T(b.id,M,B,"operator",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"=",children:"="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<",children:"<"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">",children:">"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"<=",children:"<="}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:">=",children:">="})]})]})}),e.jsx("div",{className:"mb-2 w-full sm:mb-0 sm:flex-1",children:e.jsxs(s.Select,{value:E.target.name,onValueChange:$=>T(b.id,M,B,"targetColumnName",$),children:[e.jsx(s.SelectTrigger,{children:e.jsx(s.SelectValue,{placeholder:"Target column"})}),e.jsx(s.SelectContent,{children:R(b.target.name).map($=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:$.name,children:e.jsxs("div",{className:"flex w-full items-center justify-between hover:bg-muted",children:[e.jsx("span",{children:$.name}),e.jsx(s.Badge,{variant:"outline",className:"ml-2 text-xs",children:$.dataType})]})},$.name))})]})}),e.jsx(s.Button,{type:"button",variant:"ghost",size:"sm",className:"self-center",onClick:()=>N(b.id,M,B),children:e.jsx(s.X,{className:"size-3.5"})})]},E.id||B))}),e.jsxs(s.Button,{type:"button",variant:"outline",size:"sm",className:"mt-2",onClick:()=>y(b.id,M),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Condition"]})]})]},I.id))})]})]})]},b.id)})})]}),e.jsxs("div",{className:"flex justify-end gap-2 border-t pt-4",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>{l(),c()},children:"Remove All"}),e.jsx(s.Button,{variant:"outline",onClick:c,children:"Cancel"}),e.jsx(s.Button,{onClick:()=>a(i),disabled:!k(),children:"Save"})]})]})}function Is({children:t,open:n,onOpenChange:a,joinPlan:l,availableDataSources:c,onRemove:d,onSave:r,onCancel:i}){return e.jsxs(s.Dialog,{open:n,onOpenChange:a,children:[e.jsxs(s.DialogContent,{className:"max-w-4xl",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Join Configuration"}),e.jsx(s.DialogDescription,{children:"Configure the join between the two entities to combine the fields."})]}),e.jsx(Tt,{joinPlan:l,availableDataSources:c,onSave:r,onCancel:i,onRemove:d})]}),t]})}function Et({joins:t,baseEntityId:n,availableDataSources:a,generateSQLPreview:l}){return e.jsxs("div",{className:"my-2 max-h-[400px] divide-y divide-border overflow-y-auto",children:[t.length>0&&e.jsxs("div",{className:"px-4 py-2",children:[e.jsx("h3",{className:"mb-3 text-left font-semibold",children:"Join Chain"}),e.jsx("div",{className:"flex flex-wrap items-center gap-2",children:(()=>{const c=[...t].sort((r,i)=>r.sequence-i.sequence),d=a.find(r=>r.name===n);return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:d==null?void 0:d.type}),e.jsx("span",{className:"text-xs font-medium",children:d==null?void 0:d.name}),e.jsx(s.Badge,{variant:"outline",className:"text-xs text-green-600",children:"BASE"})]}),c.map(r=>{const i=a.find(o=>o.name===r.target.name);return e.jsxs("div",{className:"flex items-center",children:[e.jsxs("div",{className:"mx-1 flex items-center gap-1",children:[e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:r.joinType}),e.jsx("span",{className:"text-muted-foreground",children:"→"})]}),e.jsxs("div",{className:"flex items-center gap-1 rounded-md border bg-muted px-2 py-1",children:[e.jsx(s.Badge,{variant:"secondary",className:"text-xs",children:i==null?void 0:i.type}),e.jsx("span",{className:"text-xs font-medium",children:i==null?void 0:i.name})]})]},r.id)})]})})()})]}),e.jsxs("div",{className:"p-4",children:[e.jsxs("h3",{className:"mb-3 text-left text-sm font-medium",children:["Configured Joins (",t.length,")"]}),e.jsx("div",{className:"space-y-2",children:[...t].sort((c,d)=>c.sequence-d.sequence).map((c,d)=>{const r=a.find(u=>u.name===c.source.name),i=a.find(u=>u.name===c.target.name),o=c.joinKeyGroups.reduce((u,m)=>u+m.keys.length,0);return e.jsxs("div",{className:"rounded-md bg-muted p-2 text-left",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Badge,{variant:"secondary",className:"shrink-0 text-xs",children:d+1}),e.jsx(s.Badge,{variant:"outline",className:"shrink-0 text-xs",children:c.joinType}),e.jsxs(s.Badge,{variant:"outline",className:"ml-auto shrink-0 text-xs",children:[o," condition",o>1?"s":""]})]}),e.jsxs("div",{className:"mt-1 truncate pl-1 text-xs text-muted-foreground",children:[r==null?void 0:r.name," → ",i==null?void 0:i.name]})]},c.id)})})]}),e.jsxs("div",{className:"p-4",children:[e.jsx("h3",{className:"mb-3 text-left text-sm font-medium",children:"SQL Query"}),e.jsx("div",{className:"overflow-x-auto rounded-md border bg-muted p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap break-all text-left font-mono text-xs text-muted-foreground",children:l()})})]})]})}function os({open:t,onOpenChange:n,variant:a="button"}={}){var v,O;const l=s.useEditorStore(A=>A.card),{autoJoinMetadata:c}=s.useDashboardCardQuery(l),d=s.useEditorStore(A=>A.actions.updateCardConfig),r=((v=l==null?void 0:l.config)==null?void 0:v.joinPlan)||{baseEntity:{type:"table",name:"",database:"",schema:"",connectionId:"",connectionType:"none",dialect:"none"},joins:[]},[i,o]=L.useState(!1),u=t!==void 0,m=u?t:i,x=u?n:o,h=((O=l==null?void 0:l.dataSource)==null?void 0:O.selectedEntities)||[],w=A=>{d(l.id,{...l==null?void 0:l.config,joinPlan:A}),x(!1)},j=()=>{d(l.id,{...l==null?void 0:l.config,joinPlan:void 0})},f=()=>{x(!1)},p=()=>{const A=[...r.joins].sort((N,C)=>N.sequence-C.sequence),y=r.baseEntity;if(!y.name)return"SELECT * FROM table -- Please select a base entity";let T=`SELECT *
|
|
175
170
|
FROM ${y.name}`;return A.forEach(N=>{const C=h.find(b=>b.name===N.target.name);if(!C)return;const k=N.joinKeyGroups.filter(b=>b.keys&&b.keys.length>0).map(b=>{const S=b.keys.filter(D=>{var F,I,M,E;return((F=D.source)==null?void 0:F.name)&&((I=D.target)==null?void 0:I.name)&&((M=D.source)==null?void 0:M.entityName)&&((E=D.target)==null?void 0:E.entityName)}).map(D=>`${D.source.entityName}.${D.source.name} ${D.operator} ${D.target.entityName}.${D.target.name}`);return S.length>1?`(${S.join(` ${b.operator} `)})`:S[0]}).filter(Boolean),R=k.length>1?k.join(" OR "):k[0]||"condition_here";T+=`
|
|
176
171
|
${N.joinType} JOIN ${C.name}
|
|
177
|
-
ON ${R}`}),T};if(h.length<2)return null;const g=r.joins.length>0&&e.jsx(s.TooltipContent,{side:"bottom",className:"ml-4 max-w-md overflow-hidden p-0",children:e.jsx(kt,{joins:r.joins,baseEntityId:r.baseEntity.name,availableDataSources:h,generateSQLPreview:p})});return e.jsxs(e.Fragment,{children:[e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:a==="badge"?e.jsxs(s.Badge,{variant:"secondary",className:"h-5 cursor-pointer gap-1 px-1.5 text-xs transition-colors hover:bg-secondary/80",onClick:A=>{A.stopPropagation(),x(!0)},children:[e.jsx(s.Link,{className:"h-3 w-3"}),r.joins.length]}):e.jsxs(s.Button,{onClick:()=>x(!0),variant:"outline",className:"w-full bg-muted/50",children:[e.jsx(s.Link,{className:"mr-2 h-3.5 w-3.5"}),r.joins.length===0?"Configure Joins":r.joins.length===1?"Join":"Joins",r.joins.length>0&&e.jsx("span",{className:"ml-2 flex h-5 min-w-5 items-center justify-center rounded-full border bg-muted-foreground px-1.5 text-xs font-medium text-background",children:r.joins.length}),(c==null?void 0:c.autoResolved)&&e.jsxs(s.Badge,{variant:"secondary",className:"ml-2 h-5 gap-1 bg-blue-50 text-blue-700 dark:bg-blue-950 dark:text-blue-300",children:[e.jsx(s.Sparkles,{className:"h-3 w-3"}),"Auto"]})]})}),g]})}),e.jsx(Is,{open:m,onOpenChange:x,joinPlan:r,availableDataSources:h,onSave:w,onCancel:f,onRemove:j,children:e.jsx("div",{})})]})}function It({open:t,onOpenChange:n}){const[a,l]=L.useState([]),{updateDataSource:c}=s.useEditorActions(),d=s.useDashboardStore(j=>j.dashboard.aiContext),r=(d==null?void 0:d.selectedEntities)||[];L.useEffect(()=>{t&&l([])},[t]);const i=j=>{l(f=>f.some(g=>g.name===j.name&&g.type===j.type)?f.filter(g=>!(g.name===j.name&&g.type===j.type)):[...f,j])},o=()=>{l([...r])},u=()=>{l([])},m=()=>{if(a.length===0)return;const j=a[0],f=j.connectionId||"",p=j.connectionType||"",v=a.some(A=>A.type==="dataset")?"semanticDomain":"database";c({connectionId:f,connectionType:p,mode:v,selectedEntities:a}),n(!1)},x=j=>"label"in j&&j.label?j.label:j.name,h=j=>{if("description"in j&&j.description)return j.description;const f=[];return j.database&&f.push(j.database),j.schema&&f.push(j.schema),j.name&&f.push(j.name),f.join(".")},w=j=>a.some(f=>f.name===j.name&&f.type===j.type);return r.length===0?e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"max-w-md",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"No Data Available"}),e.jsx(s.DialogDescription,{children:"No entities are currently available in the AI context. Please add some data sources to the dashboard first."})]}),e.jsx(s.DialogFooter,{children:e.jsx(s.Button,{onClick:()=>n(!1),children:"Close"})})]})}):e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"flex max-w-2xl flex-col",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Select Data to Analyze"}),e.jsx(s.DialogDescription,{children:"Choose entities from the dashboard's context"})]}),e.jsxs("div",{className:"flex-1 space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"outline",size:"sm",onClick:o,disabled:a.length===r.length,children:"Select All"}),e.jsx(s.Button,{variant:"outline",size:"sm",onClick:u,disabled:a.length===0,children:"Clear All"})]}),e.jsxs(s.Badge,{variant:"secondary",children:[a.length," of ",r.length," selected"]})]}),e.jsx(s.Separator,{}),e.jsx(s.ScrollArea,{className:"h-64",children:e.jsx("div",{className:"space-y-2",children:r.map((j,f)=>e.jsxs("div",{className:s.cn("flex items-center gap-3 rounded-lg border p-3 transition-colors hover:bg-muted/50",w(j)&&"border-primary bg-primary/5"),children:[e.jsx(s.Checkbox,{checked:w(j),onCheckedChange:()=>i(j)}),e.jsx("div",{className:"flex items-center gap-2 text-muted-foreground",children:s.getEntityIcon(j.type)}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("div",{className:"truncate font-medium",children:x(j)}),e.jsx("div",{className:"truncate text-sm text-muted-foreground",children:h(j)})]}),e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:j.type})]},`${j.type}-${j.name}-${f}`))})})]}),e.jsx(s.DialogFooter,{children:e.jsx("div",{className:"flex w-full justify-between",children:e.jsxs("div",{className:"flex grow justify-end gap-2",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>n(!1),children:"Cancel"}),e.jsxs(s.Button,{onClick:m,disabled:a.length===0,children:["Select"," ",a.length>0?`(${a.length})`:""]})]})})})]})})}function is({entities:t,title:n}){const{isConsoleMode:a}=s.useDashboardPreferences();return e.jsx(s.TooltipContent,{side:"top",className:"ml-5 max-w-xs",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"text-sm font-medium",children:n??`${t.length} more entities`}),e.jsx("div",{className:"space-y-1",children:t.map((l,c)=>{const d=s.getEntityDetails(l),r=d.find(o=>o.label==="Description"&&o.value),i=d.filter(o=>o.label==="Description"?!1:a?!0:!["Connection","Database","Schema"].includes(o.label)).slice(0,3);return e.jsxs("div",{className:"border-b border-border pb-1 last:border-b-0",children:[e.jsx("div",{className:"text-xs font-medium",children:s.getDisplayName(l)}),r&&e.jsxs("div",{className:"mb-1 mt-1",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Description:"}),e.jsx("div",{className:"text-xs leading-relaxed",children:r.value})]}),i.length>0&&e.jsx("div",{className:"mt-1 space-y-0.5",children:i.map((o,u)=>e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{className:"mr-2 text-muted-foreground",children:[o.label,":"]}),e.jsx("span",{className:"font-mono",children:o.value})]},u))})]},`remaining-${l.type}-${l.name}-${c}`)})})]})})}function As(t,n){const[a,l]=L.useState(()=>{const c=localStorage.getItem(t);return c?JSON.parse(c):n});return L.useEffect(()=>{localStorage.setItem(t,JSON.stringify(a))},[a,t]),[a,l]}function Ds({isVisible:t}){var f,p;const n=s.useEditorStore(g=>g.isDevMode);s.useSyncLegacyEditorState();const[a,l]=L.useState(!1),[c,d]=L.useState(!1),[r,i]=As("semaphor:data-source-collapsed",!1),o=s.useEditorStore(g=>g.card.dataSource),u=s.useEditorStore(g=>{var v,O;return(O=(v=g.card)==null?void 0:v.config)==null?void 0:O.joinPlan}),{updateDataSource:m}=s.useEditorActions(),x=((f=o==null?void 0:o.selectedEntities)==null?void 0:f.length)||0,h=((p=u==null?void 0:u.joins)==null?void 0:p.length)||0,j={...{connectionId:"",connectionType:"",mode:"database",selectedEntities:[]},...o,selectedEntities:(o==null?void 0:o.selectedEntities)||[]};return e.jsxs("div",{className:s.cn("flex flex-col overflow-hidden bg-background transition-all duration-300 ease-in-out",{"w-[325px]":t,"w-0":!t}),children:[x===0&&h===0?e.jsx("div",{className:"p-4 px-6",children:e.jsxs(s.Button,{className:"w-full bg-muted/50",variant:"outline",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"mr-2 h-3.5 w-3.5"}),"Choose Dataset"]})}):e.jsxs("div",{children:[e.jsxs("div",{className:s.cn("flex cursor-pointer items-center justify-between px-6 pt-4",r?"pb-0":"pb-3"),onClick:()=>i(!r),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-foreground",children:"Data Source"}),r&&e.jsxs("div",{className:"flex items-center gap-1.5",onClick:g=>g.stopPropagation(),children:[x>0&&e.jsx(s.TooltipProvider,{delayDuration:100,children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs(s.Badge,{variant:"secondary",className:"h-5 cursor-pointer gap-1 px-1.5 text-xs transition-colors hover:bg-secondary/80",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"h-3 w-3"}),x]})}),e.jsx(is,{entities:j.selectedEntities,title:`${x} selected ${x===1?"entity":"entities"}`})]})}),h>0&&!n&&e.jsx(os,{variant:"badge"})]})]}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:g=>{g.stopPropagation(),i(!r)},children:r?e.jsx(s.ChevronDown,{className:"h-4 w-4"}):e.jsx(s.ChevronUp,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:s.cn("flex flex-col overflow-hidden transition-all duration-200 ease-in-out",r?"max-h-0 opacity-0":"max-h-96 opacity-100"),children:[e.jsx("div",{className:"px-6",children:e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs(s.Button,{className:"w-full bg-muted/50",variant:"outline",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"mr-2 h-3.5 w-3.5"}),x===1?"Dataset":"Datasets",e.jsx("span",{className:"ml-2 flex h-5 min-w-5 items-center justify-center rounded-full border bg-muted-foreground px-1.5 text-xs font-medium text-background",children:x})]})}),e.jsx(is,{entities:j.selectedEntities,title:`${x} selected ${x===1?"entity":"entities"}`})]})})}),!n&&x>1&&e.jsx("div",{className:"mt-3 px-6",children:e.jsx(os,{})})]})]}),e.jsx(s.FieldsSection,{}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(o==null?void 0:o.selectedEntities)||[],dialogMode:"dataSource",open:a,onOpenChange:l,dataSource:j}),e.jsx(It,{open:c,onOpenChange:d})]})}function At(t){return s.GenIcon({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:[]}]})(t)}function Ls(t){const{theme:n}=s.useTheme(),a=s.useDashboardStore(f=>f.theme),l=s.useEditorStore(f=>f.frame),c=s.useEditorStore(f=>f.card),d=s.useEditorStore(f=>f.activeTabCardId),r=s.Le(),{refetch:i}=s.useDashboardCardQuery(c),{updateCardTab:o,setRunSql:u}=s.useEditorActions(),m=L.useRef(void 0),x=L.useRef({}),h=l.cards.find(f=>f.id===d),w=L.useCallback(()=>{let f="";return t==="python"&&(f=(h==null?void 0:h.python)||s.PYTHON_DEFAULT_CODE),t==="sql"&&(f=(h==null?void 0:h.sql)||""),f},[t,h]);L.useEffect(()=>{if(d&&c.id&&c.id!==d){if(m.current&&r){const f=m.current.getModel();f&&(x.current[c.id]=f);let p=x.current[d];if(!p){const g=w();p=r.editor.createModel(g,t),x.current[d]=p}m.current.setModel(p)}o(d,c),u(!0)}},[d,c,o,r,u,t,w]),L.useEffect(()=>{if(!r)return;const f=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",p=a==="system"?f:a;r.editor.defineTheme("default",{base:p==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":p==="light"?"#ffffff":s.hslStringToHex("240 10% 10%")}}),r.editor.setTheme("default")},[n,a,r]),L.useEffect(()=>{const f=m.current;f&&r&&c&&f.addCommand(r.KeyMod.Shift|r.KeyCode.Enter,function(){return i(),!1})},[r,i,c]);function j(f,p){if(m.current=f,!x.current[c.id]){const g=f.getModel(),v=w();x.current[c.id]=g||(p==null?void 0:p.editor.createModel(v,t))||f.getModel()}f.setModel(x.current[c.id])}return{handleEditorDidMount:j}}const Dt=300;function Lt(){const t=s.useEditorStore(m=>m.card.sql),n=s.useEditorStore(m=>m.sqlGen),{setCardSql:a,setSqlGen:l}=s.useEditorActions(),{handleEditorDidMount:c}=Ls("sql"),d=L.useRef(n);d.current=n;const r=L.useCallback(m=>{a(m)},[a]),{value:i,onChange:o}=ks({storeValue:t,defaultValue:"",onStoreUpdate:r,delay:Dt});function u(m){o(m),s.isObjectEmpty(d.current)||l({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!i&&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(s.Ft,{theme:"default",className:"pt-3",onChange:u,value:i,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:c})]})}const Mt=300;function Ot(){const t=s.useEditorStore(d=>d.card.python),{setCardPython:n}=s.useEditorActions(),{handleEditorDidMount:a}=Ls("python"),{value:l,onChange:c}=ks({storeValue:t,defaultValue:s.PYTHON_DEFAULT_CODE,onStoreUpdate:n,delay:Mt});return e.jsx(s.Ft,{className:"pt-3",theme:"default",onChange:c,value:l,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:a})}function Ms({sql:t,showCardDetails:n=!1,className:a}){const[l,c]=L.useState(!1),d=s.useEditorStore(w=>w.card),r=s.useEditorStore(w=>w.frame),i=s.useEditorStore(w=>w.isDevMode),o=s.useDashboardStore(w=>w.selectedSheetId),u=s.useDashboardStore(w=>{var j;return(j=w.dashboard)==null?void 0:j.id}),{switchEditorMode:m,setRunSql:x}=s.useEditorActions(),h=()=>{m("dev",{sqlToLoad:t}),setTimeout(()=>{x(!0)},100)};return e.jsxs("div",{className:s.cn("flex min-h-0 flex-1 flex-col",a),children:[!i&&e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsxs(s.Button,{onClick:h,variant:"secondary",size:"sm",children:[e.jsx(Ts,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),n&&e.jsxs(s.Button,{onClick:()=>c(!l),variant:"outline",size:"sm",children:[e.jsx(s.Info,{className:"mr-2 h-4 w-4"}),"Card Details"]})]}),l&&n&&e.jsx("div",{className:"mb-3 rounded-lg border border-border bg-muted/30 p-4",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-sm font-semibold",children:"Card Information"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:()=>c(!1),children:e.jsx(at,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Card ID"}),e.jsx("code",{className:"font-mono text-xs",children:d.id||"N/A"})]}),e.jsx(s.CopyButton,{text:d.id||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Frame ID"}),e.jsx("code",{className:"font-mono text-xs",children:r.id||"N/A"})]}),e.jsx(s.CopyButton,{text:r.id||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Sheet ID"}),e.jsx("code",{className:"font-mono text-xs",children:o||"N/A"})]}),e.jsx(s.CopyButton,{text:o||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Dashboard ID"}),e.jsx("code",{className:"font-mono text-xs",children:u||"N/A"})]}),e.jsx(s.CopyButton,{text:u||"",className:"h-6 w-6"})]})]})]})}),e.jsx("div",{className:"min-h-0 flex-1 overflow-auto text-xs leading-relaxed text-foreground/80",children:e.jsx("div",{className:"flex flex-col space-y-1.5",children:e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"absolute right-2 top-2 z-10",children:e.jsx(s.CopyButton,{text:t,className:"h-6 w-6"})}),e.jsx(s.CodeHighlighter,{code:t,language:"sql"})]})})})]})}function Rt(){const t=s.useQueryClient(),{isConsoleMode:n}=s.useDashboardPreferences(),[a,l]=L.useState(!1);s.useEditorStore(V=>V.frame);const c=s.useEditorStore(V=>V.card);s.useEditorStore(V=>V.isShowingVisual),s.useEditorStore(V=>V.selectedConnectionId),s.useEditorStore(V=>V.tempQueryData),s.useEditorStore(V=>V.selectedDatabaseName),s.useEditorStore(V=>V.selectedDatamodelId),s.useEditorStore(V=>V.selectedSchemaName);const d=s.useEditorStore(V=>V.showAIDialog);s.useEditorStore(V=>V.selectedTableName),s.useEditorStore(V=>{var q;return(q=V==null?void 0:V.card)==null?void 0:q.sql}),s.useEditorStore(V=>{var q;return(q=V==null?void 0:V.card)==null?void 0:q.python});const{cardFilterValues:r}=s.useCardFilterValues((c==null?void 0:c.id)??""),i=s.useDashboardStore(V=>V.inlineFilterValuesMap[(c==null?void 0:c.id)??""]),o=L.useRef(!0),{setCardCustomCfg:u,setQueryResultColumns:m,setCardSql:x,setTempQueryData:h,setPythonStdOut:w,setCardPreferences:j,setCardType:f,setCardPython:p,setShowAIDialog:g,setCard:v,updateCardInFrame:O}=s.useEditorActions();s.useEditorStore(V=>V.onSave),s.useEditorStore(V=>V.onClose),s.useEditorStore(V=>V.actions.setSqlGen);const A=s.useEditorStore(V=>V.runSql),y=s.useEditorStore(V=>V.actions.setRunSql),T=s.useEditorStore(V=>V.actions.setIsSqlRunning),{data:N,isLoading:C,isFetching:k,isSuccess:R,isError:b,error:S,errorSql:D,queryKey:F,refetch:I,errorMessage:M}=s.useDashboardCardQuery(c);L.useEffect(()=>{if(N&&(N.rowLimitExceeded&&s.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),w((N==null?void 0:N.output)||""),N.records.length>0)){const{dimensionKeys:V}=s.getKeys(N.records);m(V)}},[N,m,w]),L.useEffect(()=>{(C||k)&&T(!0),(b||R)&&T(!1),b&&(m([]),w(""))},[C,k,N,w,b,R,T,m,S]),L.useEffect(()=>{A&&(t.getQueryData(F)||I(),y(!1))},[A,I,y,F,t]);const E=L.useRef(I);E.current=I,L.useEffect(()=>{if(o.current){o.current=!1;return}E.current()},[i]);function B(){return e.jsxs(s.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(s.ResizablePanel,{id:"1",order:1,defaultSize:a?20:100,className:"relative flex grow",children:[e.jsx("div",{id:"sql-container",className:"relative flex w-[99%]",children:e.jsx(Lt,{})}),a&&e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"sql"})]}),a&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(s.ResizablePanel,{id:"2",order:2,defaultSize:80,className:"relative flex grow",children:[e.jsx("div",{id:"python-container",className:"relative flex w-[99%] pt-0",children:e.jsx(Ot,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function $(){var V,q,G,z,H,_;return e.jsx("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-4",children:[e.jsxs(s.Button,{title:"Shift + Enter",disabled:C||k,onClick:()=>{I()},className:"h-8 shrink-0",size:"sm",children:[C||k?e.jsx(s.LoaderCircle,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(s.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),(N==null?void 0:N.sql)&&!M&&e.jsxs(s.HoverCard,{openDelay:300,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-4 w-4 shrink-0 cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.HoverCardContent,{align:"start",className:"w-[500px]",children:e.jsx(Ms,{sql:N.sql})})]}),e.jsx(He.EditorClearButton,{}),e.jsxs(s.Toggle,{pressed:a,onPressedChange:P=>l(P),className:"h-8",size:"sm",variant:"outline",children:[e.jsx(At,{className:"mr-2 size-4 text-foreground/70"}),"Python"]}),!d&&e.jsxs("button",{disabled:d,onClick:()=>{g(!0)},className:"flex h-8 w-full cursor-text items-center justify-start rounded-md bg-muted/50 px-3 text-left text-sm text-muted-foreground",children:[e.jsx(s.Sparkles,{className:"mr-3 size-4 text-foreground/70"}),"Type your question..."]}),e.jsxs("div",{className:"flex flex-1 items-center justify-end gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"pagination",checked:((q=(V=c.preferences)==null?void 0:V.tablePrefs)==null?void 0:q.enableDevModePagination)??!1,onCheckedChange:P=>{var Y;j({...c.preferences,tablePrefs:{...(Y=c.preferences)==null?void 0:Y.tablePrefs,enableDevModePagination:P}})}}),e.jsx(s.Label,{htmlFor:"pagination",className:"cursor-pointer text-sm",children:"Paginate"}),((z=(G=c.preferences)==null?void 0:G.tablePrefs)==null?void 0:z.enableDevModePagination)&&e.jsxs(s.Select,{value:String(((_=(H=c.preferences)==null?void 0:H.tablePrefs)==null?void 0:_.pageSize)||50),onValueChange:P=>{var se,W,ae;const Y=Number(P),Z={...c,preferences:{...c.preferences,tablePrefs:{...(se=c.preferences)==null?void 0:se.tablePrefs,pageSize:Y}},paginationConfig:{page:1,pageSize:Y}};v(Z),O(Z),c.sql&&((ae=(W=c.preferences)==null?void 0:W.tablePrefs)!=null&&ae.enableDevModePagination)&&y(!0)},children:[e.jsx(s.SelectTrigger,{className:"h-8 w-20",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"10",children:"10"}),e.jsx(s.SelectItem,{value:"25",children:"25"}),e.jsx(s.SelectItem,{value:"50",children:"50"}),e.jsx(s.SelectItem,{value:"100",children:"100"}),e.jsx(s.SelectItem,{value:"500",children:"500"}),e.jsx(s.SelectItem,{value:"1000",children:"1000"})]})]})]}),r&&r.length>0&&e.jsxs(s.HoverCard,{openDelay:200,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",className:"h-8 gap-2 px-2",children:[e.jsx(s.Filter,{className:"size-4"}),e.jsx(s.Badge,{className:"h-5 min-w-5 bg-purple-100 px-1.5 text-purple-700 hover:bg-purple-100 dark:bg-purple-950 dark:text-purple-300",children:r.length})]})}),e.jsx(s.HoverCardContent,{className:"mr-4 max-h-[400px] w-[500px] overflow-auto p-0",children:e.jsxs("div",{className:"p-3",children:[e.jsx("div",{className:"mb-2 text-sm font-semibold",children:"Active Dashboard Filters"}),e.jsx(s.CodeHighlighter,{code:JSON.stringify(r,null,2),language:"json"})]})})]})]})]})})}return e.jsxs("section",{role:"editor-section",className:"flex grow flex-col justify-end",children:[e.jsx("div",{className:"relative flex grow overflow-y-scroll",children:B()}),$()]})}const Ft=[{value:"table",label:"Table",icon:s.Table2},{value:"aggregateTable",label:"Aggregate",icon:s.Layers},{value:"pivotTable",label:"Pivot",icon:s.Grid3x3}];function Bt({className:t}){const n=s.useEditorStore(o=>o.card),{setCardType:a,setCard:l,updateCardInFrame:c}=s.useEditorActions(),d=["table","aggregateTable","pivotTable"].includes(n.type),r=s.hasValidCardConfig(n.config);if(!d||!r)return null;const i=o=>{var m;const u=o;if(u==="aggregateTable"&&((m=n.config)!=null&&m.pivotByColumns)&&n.config.pivotByColumns.length>0){const x={...n,type:u,config:{...n.config,pivotByColumns:[]}};l(x),c(x)}else{a(u);const x={...n,type:u};c(x)}};return e.jsx("div",{className:s.cn("flex items-center",t),children:e.jsx(s.RadioGroup,{value:n.type,onValueChange:i,className:"flex flex-row items-center justify-between gap-1 rounded-lg border p-1",children:Ft.map(o=>{const u=o.icon,m=n.type===o.value;return e.jsxs("div",{className:"flex items-center",children:[e.jsx(s.RadioGroupItem,{value:o.value,id:`table-type-${o.value}`,className:"peer sr-only"}),e.jsxs(s.Label,{htmlFor:`table-type-${o.value}`,className:s.cn("flex cursor-pointer items-center gap-1.5 rounded-md px-3 py-1 text-sm font-medium transition-all",m?"bg-muted text-foreground shadow-sm":"text-muted-foreground hover:bg-muted/50"),children:[e.jsx(u,{className:"h-3.5 w-3.5"}),o.label]})]},o.value)})})})}function Pt({type:t,isLinked:n,className:a}){const l=s.useDashboardStore(h=>h.selectedFrameId),c=s.useManagementStore(h=>h.selectedVisual),d=s.useEditorStore(h=>h.frame);let r;if(t)r=t;else if(n!==void 0)r=n?"linked":"library";else if(!!!(d!=null&&d.visualId||c!=null&&c.id)&&l)r="local";else if(c&&!l)r="library";else if(d!=null&&d.visualId&&l)r="linked";else if(c!=null&&c.id&&l)r="library";else return null;if(r==="local")return null;const i={linked:"border-purple-200/70 bg-gradient-to-r from-purple-50/50 to-purple-100/30 text-purple-900 shadow-sm hover:from-purple-100/60 hover:to-purple-100/50 hover:shadow-md dark:border-purple-800/50 dark:from-purple-950/40 dark:to-purple-900/30 dark:text-purple-100 dark:hover:from-purple-950/60 dark:hover:to-purple-900/50",library:"border-blue-200/70 bg-gradient-to-r from-blue-50/50 to-blue-100/30 text-blue-900 shadow-sm hover:from-blue-100/60 hover:to-blue-100/50 hover:shadow-md dark:border-blue-800/50 dark:from-blue-950/40 dark:to-blue-900/30 dark:text-blue-100 dark:hover:from-blue-950/60 dark:hover:to-blue-900/50"},o={linked:"Linked to library: Changes made here update the library visual and all linked instances.",library:"Library visual: Share and reuse this visual across multiple dashboards."},u={linked:"border-purple-200 bg-purple-50 text-purple-900 dark:border-purple-800 dark:bg-purple-950 dark:text-purple-100",library:"border-blue-200 bg-blue-50 text-blue-900 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-100"},m={linked:"fill-purple-50 dark:fill-purple-950",library:"fill-blue-50 dark:fill-blue-950"},x={linked:e.jsx(mt,{className:"h-3.5 w-3.5"}),library:e.jsx(ut,{className:"h-3.5 w-3.5"})};return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:300,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Badge,{variant:"outline",className:s.cn("cursor-help select-none transition-all",i[r],a),children:x[r]})}),e.jsxs(s.TooltipContent,{side:"bottom",className:s.cn("max-w-xs",u[r]),children:[e.jsx(s.TooltipArrow,{className:m[r]}),e.jsx("p",{children:o[r]})]})]})})}function Ye({error:t,errorSql:n,errorTitle:a="Unable to load data",onOpenInEditor:l,onDebugWithAssistant:c,onSendFeedback:d,showFeedbackButton:r=!0,className:i}){const[o,u]=L.useState(!1);return e.jsx("div",{className:s.cn("rounded-lg border border-border bg-muted/40 p-4",i),children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-amber-500/10 dark:bg-amber-500/20",children:e.jsx(s.TriangleAlert,{className:"h-4 w-4 text-amber-600 dark:text-amber-500"})}),e.jsxs("div",{className:"flex-1 space-y-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:a}),e.jsxs("div",{className:"relative rounded-md border border-border bg-background p-3",children:[e.jsx(s.CopyButton,{text:t,className:"absolute right-2 top-2 z-10 h-6 w-6"}),e.jsx("div",{className:"max-h-40 overflow-auto pr-10",children:e.jsx("p",{className:"break-all font-mono text-xs text-muted-foreground",children:t})})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[n&&e.jsx(s.Collapsible,{open:o,onOpenChange:u,children:e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-8",children:o?e.jsxs(e.Fragment,{children:[e.jsx(s.ChevronUp,{className:"mr-1.5 h-3.5 w-3.5"}),"Hide Details"]}):e.jsxs(e.Fragment,{children:[e.jsx(s.ChevronDown,{className:"mr-1.5 h-3.5 w-3.5"}),"Show Details"]})})})}),l&&n&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:l,children:[e.jsx(Ts,{className:"mr-1.5 h-3.5 w-3.5"}),"Open in Editor"]}),r&&d&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:d,children:[e.jsx(s.MessageSquareWarning,{className:"mr-1.5 h-3.5 w-3.5"}),"Send Feedback"]}),c&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:c,children:[e.jsx(ct,{className:"mr-1.5 h-3.5 w-3.5"}),"Debug with Assistant"]})]}),n&&e.jsx(s.Collapsible,{open:o,onOpenChange:u,children:e.jsx(s.CollapsibleContent,{className:"space-y-2",children:e.jsxs("div",{className:"mt-3 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Query"}),e.jsx(s.CopyButton,{text:n,className:"h-6 w-6"})]}),e.jsx("div",{className:"max-h-80 overflow-auto rounded-md border border-border bg-background p-2",children:e.jsx(s.CodeHighlighter,{code:n,language:"sql"})})]})})})]})]})})}function Os(t,n){const a=s.useEditorStore(u=>u.card),l=s.useEditorStore(u=>u.isDevMode),{switchEditorMode:c,setRunSql:d,runAssistant:r}=s.useEditorActions();return{isDevMode:l,handleOpenInEditor:l?void 0:()=>{const u=n||a.sql||a.python||"";c("dev",{sqlToLoad:u}),setTimeout(()=>{d(!0)},100)},handleDebugWithAssistant:l?()=>{const u=`Can you help me debug this query?
|
|
172
|
+
ON ${R}`}),T};if(h.length<2)return null;const g=r.joins.length>0&&e.jsx(s.TooltipContent,{side:"bottom",className:"ml-4 max-w-md overflow-hidden p-0",children:e.jsx(Et,{joins:r.joins,baseEntityId:r.baseEntity.name,availableDataSources:h,generateSQLPreview:p})});return e.jsxs(e.Fragment,{children:[e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:a==="badge"?e.jsxs(s.Badge,{variant:"secondary",className:"h-5 cursor-pointer gap-1 px-1.5 text-xs transition-colors hover:bg-secondary/80",onClick:A=>{A.stopPropagation(),x(!0)},children:[e.jsx(s.Link,{className:"h-3 w-3"}),r.joins.length]}):e.jsxs(s.Button,{onClick:()=>x(!0),variant:"outline",className:"w-full bg-muted/50",children:[e.jsx(s.Link,{className:"mr-2 h-3.5 w-3.5"}),r.joins.length===0?"Configure Joins":r.joins.length===1?"Join":"Joins",r.joins.length>0&&e.jsx("span",{className:"ml-2 flex h-5 min-w-5 items-center justify-center rounded-full border bg-muted-foreground px-1.5 text-xs font-medium text-background",children:r.joins.length}),(c==null?void 0:c.autoResolved)&&e.jsxs(s.Badge,{variant:"secondary",className:"ml-2 h-5 gap-1 bg-blue-50 text-blue-700 dark:bg-blue-950 dark:text-blue-300",children:[e.jsx(s.Sparkles,{className:"h-3 w-3"}),"Auto"]})]})}),g]})}),e.jsx(Is,{open:m,onOpenChange:x,joinPlan:r,availableDataSources:h,onSave:w,onCancel:f,onRemove:j,children:e.jsx("div",{})})]})}function kt({open:t,onOpenChange:n}){const[a,l]=L.useState([]),{updateDataSource:c}=s.useEditorActions(),d=s.useDashboardStore(j=>j.dashboard.aiContext),r=(d==null?void 0:d.selectedEntities)||[];L.useEffect(()=>{t&&l([])},[t]);const i=j=>{l(f=>f.some(g=>g.name===j.name&&g.type===j.type)?f.filter(g=>!(g.name===j.name&&g.type===j.type)):[...f,j])},o=()=>{l([...r])},u=()=>{l([])},m=()=>{if(a.length===0)return;const j=a[0],f=j.connectionId||"",p=j.connectionType||"",v=a.some(A=>A.type==="dataset")?"semanticDomain":"database";c({connectionId:f,connectionType:p,mode:v,selectedEntities:a}),n(!1)},x=j=>"label"in j&&j.label?j.label:j.name,h=j=>{if("description"in j&&j.description)return j.description;const f=[];return j.database&&f.push(j.database),j.schema&&f.push(j.schema),j.name&&f.push(j.name),f.join(".")},w=j=>a.some(f=>f.name===j.name&&f.type===j.type);return r.length===0?e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"max-w-md",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"No Data Available"}),e.jsx(s.DialogDescription,{children:"No entities are currently available in the AI context. Please add some data sources to the dashboard first."})]}),e.jsx(s.DialogFooter,{children:e.jsx(s.Button,{onClick:()=>n(!1),children:"Close"})})]})}):e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"flex max-w-2xl flex-col",children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Select Data to Analyze"}),e.jsx(s.DialogDescription,{children:"Choose entities from the dashboard's context"})]}),e.jsxs("div",{className:"flex-1 space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"outline",size:"sm",onClick:o,disabled:a.length===r.length,children:"Select All"}),e.jsx(s.Button,{variant:"outline",size:"sm",onClick:u,disabled:a.length===0,children:"Clear All"})]}),e.jsxs(s.Badge,{variant:"secondary",children:[a.length," of ",r.length," selected"]})]}),e.jsx(s.Separator,{}),e.jsx(s.ScrollArea,{className:"h-64",children:e.jsx("div",{className:"space-y-2",children:r.map((j,f)=>e.jsxs("div",{className:s.cn("flex items-center gap-3 rounded-lg border p-3 transition-colors hover:bg-muted/50",w(j)&&"border-primary bg-primary/5"),children:[e.jsx(s.Checkbox,{checked:w(j),onCheckedChange:()=>i(j)}),e.jsx("div",{className:"flex items-center gap-2 text-muted-foreground",children:s.getEntityIcon(j.type)}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("div",{className:"truncate font-medium",children:x(j)}),e.jsx("div",{className:"truncate text-sm text-muted-foreground",children:h(j)})]}),e.jsx(s.Badge,{variant:"outline",className:"text-xs",children:j.type})]},`${j.type}-${j.name}-${f}`))})})]}),e.jsx(s.DialogFooter,{children:e.jsx("div",{className:"flex w-full justify-between",children:e.jsxs("div",{className:"flex grow justify-end gap-2",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>n(!1),children:"Cancel"}),e.jsxs(s.Button,{onClick:m,disabled:a.length===0,children:["Select"," ",a.length>0?`(${a.length})`:""]})]})})})]})})}function is({entities:t,title:n}){const{isConsoleMode:a}=s.useDashboardPreferences();return e.jsx(s.TooltipContent,{side:"top",className:"ml-5 max-w-xs",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"text-sm font-medium",children:n??`${t.length} more entities`}),e.jsx("div",{className:"space-y-1",children:t.map((l,c)=>{const d=s.getEntityDetails(l),r=d.find(o=>o.label==="Description"&&o.value),i=d.filter(o=>o.label==="Description"?!1:a?!0:!["Connection","Database","Schema"].includes(o.label)).slice(0,3);return e.jsxs("div",{className:"border-b border-border pb-1 last:border-b-0",children:[e.jsx("div",{className:"text-xs font-medium",children:s.getDisplayName(l)}),r&&e.jsxs("div",{className:"mb-1 mt-1",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Description:"}),e.jsx("div",{className:"text-xs leading-relaxed",children:r.value})]}),i.length>0&&e.jsx("div",{className:"mt-1 space-y-0.5",children:i.map((o,u)=>e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{className:"mr-2 text-muted-foreground",children:[o.label,":"]}),e.jsx("span",{className:"font-mono",children:o.value})]},u))})]},`remaining-${l.type}-${l.name}-${c}`)})})]})})}function As(t,n){const[a,l]=L.useState(()=>{const c=localStorage.getItem(t);return c?JSON.parse(c):n});return L.useEffect(()=>{localStorage.setItem(t,JSON.stringify(a))},[a,t]),[a,l]}function Ds({isVisible:t}){var f,p;const n=s.useEditorStore(g=>g.isDevMode);s.useSyncLegacyEditorState();const[a,l]=L.useState(!1),[c,d]=L.useState(!1),[r,i]=As("semaphor:data-source-collapsed",!1),o=s.useEditorStore(g=>g.card.dataSource),u=s.useEditorStore(g=>{var v,O;return(O=(v=g.card)==null?void 0:v.config)==null?void 0:O.joinPlan}),{updateDataSource:m}=s.useEditorActions(),x=((f=o==null?void 0:o.selectedEntities)==null?void 0:f.length)||0,h=((p=u==null?void 0:u.joins)==null?void 0:p.length)||0,j={...{connectionId:"",connectionType:"",mode:"database",selectedEntities:[]},...o,selectedEntities:(o==null?void 0:o.selectedEntities)||[]};return e.jsxs("div",{className:s.cn("flex flex-col overflow-hidden bg-background transition-all duration-300 ease-in-out",{"w-[325px]":t,"w-0":!t}),children:[x===0&&h===0?e.jsx("div",{className:"p-4 px-6",children:e.jsxs(s.Button,{className:"w-full bg-muted/50",variant:"outline",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"mr-2 h-3.5 w-3.5"}),"Choose Dataset"]})}):e.jsxs("div",{children:[e.jsxs("div",{className:s.cn("flex cursor-pointer items-center justify-between px-6 pt-4",r?"pb-0":"pb-3"),onClick:()=>i(!r),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-foreground",children:"Data Source"}),r&&e.jsxs("div",{className:"flex items-center gap-1.5",onClick:g=>g.stopPropagation(),children:[x>0&&e.jsx(s.TooltipProvider,{delayDuration:100,children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs(s.Badge,{variant:"secondary",className:"h-5 cursor-pointer gap-1 px-1.5 text-xs transition-colors hover:bg-secondary/80",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"h-3 w-3"}),x]})}),e.jsx(is,{entities:j.selectedEntities,title:`${x} selected ${x===1?"entity":"entities"}`})]})}),h>0&&!n&&e.jsx(os,{variant:"badge"})]})]}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:g=>{g.stopPropagation(),i(!r)},children:r?e.jsx(s.ChevronDown,{className:"h-4 w-4"}):e.jsx(s.ChevronUp,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:s.cn("flex flex-col overflow-hidden transition-all duration-200 ease-in-out",r?"max-h-0 opacity-0":"max-h-96 opacity-100"),children:[e.jsx("div",{className:"px-6",children:e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs(s.Button,{className:"w-full bg-muted/50",variant:"outline",onClick:()=>l(!0),children:[e.jsx(s.Database,{className:"mr-2 h-3.5 w-3.5"}),x===1?"Dataset":"Datasets",e.jsx("span",{className:"ml-2 flex h-5 min-w-5 items-center justify-center rounded-full border bg-muted-foreground px-1.5 text-xs font-medium text-background",children:x})]})}),e.jsx(is,{entities:j.selectedEntities,title:`${x} selected ${x===1?"entity":"entities"}`})]})})}),!n&&x>1&&e.jsx("div",{className:"mt-3 px-6",children:e.jsx(os,{})})]})]}),e.jsx(s.FieldsSection,{}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(o==null?void 0:o.selectedEntities)||[],dialogMode:"dataSource",open:a,onOpenChange:l,dataSource:j}),e.jsx(kt,{open:c,onOpenChange:d})]})}function It(t){return s.GenIcon({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:[]}]})(t)}function Ls(t){const{theme:n}=s.useTheme(),a=s.useDashboardStore(f=>f.theme),l=s.useEditorStore(f=>f.frame),c=s.useEditorStore(f=>f.card),d=s.useEditorStore(f=>f.activeTabCardId),r=s.Le(),{refetch:i}=s.useDashboardCardQuery(c),{updateCardTab:o,setRunSql:u}=s.useEditorActions(),m=L.useRef(void 0),x=L.useRef({}),h=l.cards.find(f=>f.id===d),w=L.useCallback(()=>{let f="";return t==="python"&&(f=(h==null?void 0:h.python)||s.PYTHON_DEFAULT_CODE),t==="sql"&&(f=(h==null?void 0:h.sql)||""),f},[t,h]);L.useEffect(()=>{if(d&&c.id&&c.id!==d){if(m.current&&r){const f=m.current.getModel();f&&(x.current[c.id]=f);let p=x.current[d];if(!p){const g=w();p=r.editor.createModel(g,t),x.current[d]=p}m.current.setModel(p)}o(d,c),u(!0)}},[d,c,o,r,u,t,w]),L.useEffect(()=>{if(!r)return;const f=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",p=a==="system"?f:a;r.editor.defineTheme("default",{base:p==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":p==="light"?"#ffffff":s.hslStringToHex("240 10% 10%")}}),r.editor.setTheme("default")},[n,a,r]),L.useEffect(()=>{const f=m.current;f&&r&&c&&f.addCommand(r.KeyMod.Shift|r.KeyCode.Enter,function(){return i(),!1})},[r,i,c]);function j(f,p){if(m.current=f,!x.current[c.id]){const g=f.getModel(),v=w();x.current[c.id]=g||(p==null?void 0:p.editor.createModel(v,t))||f.getModel()}f.setModel(x.current[c.id])}return{handleEditorDidMount:j}}const At=300;function Dt(){const t=s.useEditorStore(m=>m.card.sql),n=s.useEditorStore(m=>m.sqlGen),{setCardSql:a,setSqlGen:l}=s.useEditorActions(),{handleEditorDidMount:c}=Ls("sql"),d=L.useRef(n);d.current=n;const r=L.useCallback(m=>{a(m)},[a]),{value:i,onChange:o}=ks({storeValue:t,defaultValue:"",onStoreUpdate:r,delay:At});function u(m){o(m),s.isObjectEmpty(d.current)||l({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!i&&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(s.Ft,{theme:"default",className:"pt-3",onChange:u,value:i,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:c})]})}const Lt=300;function Mt(){const t=s.useEditorStore(d=>d.card.python),{setCardPython:n}=s.useEditorActions(),{handleEditorDidMount:a}=Ls("python"),{value:l,onChange:c}=ks({storeValue:t,defaultValue:s.PYTHON_DEFAULT_CODE,onStoreUpdate:n,delay:Lt});return e.jsx(s.Ft,{className:"pt-3",theme:"default",onChange:c,value:l,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:a})}function Ms({sql:t,showCardDetails:n=!1,className:a}){const[l,c]=L.useState(!1),d=s.useEditorStore(w=>w.card),r=s.useEditorStore(w=>w.frame),i=s.useEditorStore(w=>w.isDevMode),o=s.useDashboardStore(w=>w.selectedSheetId),u=s.useDashboardStore(w=>{var j;return(j=w.dashboard)==null?void 0:j.id}),{switchEditorMode:m,setRunSql:x}=s.useEditorActions(),h=()=>{m("dev",{sqlToLoad:t}),setTimeout(()=>{x(!0)},100)};return e.jsxs("div",{className:s.cn("flex min-h-0 flex-1 flex-col",a),children:[!i&&e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsxs(s.Button,{onClick:h,variant:"secondary",size:"sm",children:[e.jsx(Ts,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),n&&e.jsxs(s.Button,{onClick:()=>c(!l),variant:"outline",size:"sm",children:[e.jsx(s.Info,{className:"mr-2 h-4 w-4"}),"Card Details"]})]}),l&&n&&e.jsx("div",{className:"mb-3 rounded-lg border border-border bg-muted/30 p-4",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-sm font-semibold",children:"Card Information"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:()=>c(!1),children:e.jsx(Re.CircleX,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Card ID"}),e.jsx("code",{className:"font-mono text-xs",children:d.id||"N/A"})]}),e.jsx(s.CopyButton,{text:d.id||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Frame ID"}),e.jsx("code",{className:"font-mono text-xs",children:r.id||"N/A"})]}),e.jsx(s.CopyButton,{text:r.id||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Sheet ID"}),e.jsx("code",{className:"font-mono text-xs",children:o||"N/A"})]}),e.jsx(s.CopyButton,{text:o||"",className:"h-6 w-6"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-background/60 px-3 py-2",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Dashboard ID"}),e.jsx("code",{className:"font-mono text-xs",children:u||"N/A"})]}),e.jsx(s.CopyButton,{text:u||"",className:"h-6 w-6"})]})]})]})}),e.jsx("div",{className:"min-h-0 flex-1 overflow-auto text-xs leading-relaxed text-foreground/80",children:e.jsx("div",{className:"flex flex-col space-y-1.5",children:e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"absolute right-2 top-2 z-10",children:e.jsx(s.CopyButton,{text:t,className:"h-6 w-6"})}),e.jsx(s.CodeHighlighter,{code:t,language:"sql"})]})})})]})}function Ot(){const t=s.useQueryClient(),{isConsoleMode:n}=s.useDashboardPreferences(),[a,l]=L.useState(!1);s.useEditorStore(V=>V.frame);const c=s.useEditorStore(V=>V.card);s.useEditorStore(V=>V.isShowingVisual),s.useEditorStore(V=>V.selectedConnectionId),s.useEditorStore(V=>V.tempQueryData),s.useEditorStore(V=>V.selectedDatabaseName),s.useEditorStore(V=>V.selectedDatamodelId),s.useEditorStore(V=>V.selectedSchemaName);const d=s.useEditorStore(V=>V.showAIDialog);s.useEditorStore(V=>V.selectedTableName),s.useEditorStore(V=>{var q;return(q=V==null?void 0:V.card)==null?void 0:q.sql}),s.useEditorStore(V=>{var q;return(q=V==null?void 0:V.card)==null?void 0:q.python});const{cardFilterValues:r}=s.useCardFilterValues((c==null?void 0:c.id)??""),i=s.useDashboardStore(V=>V.inlineFilterValuesMap[(c==null?void 0:c.id)??""]),o=L.useRef(!0),{setCardCustomCfg:u,setQueryResultColumns:m,setCardSql:x,setTempQueryData:h,setPythonStdOut:w,setCardPreferences:j,setCardType:f,setCardPython:p,setShowAIDialog:g,setCard:v,updateCardInFrame:O}=s.useEditorActions();s.useEditorStore(V=>V.onSave),s.useEditorStore(V=>V.onClose),s.useEditorStore(V=>V.actions.setSqlGen);const A=s.useEditorStore(V=>V.runSql),y=s.useEditorStore(V=>V.actions.setRunSql),T=s.useEditorStore(V=>V.actions.setIsSqlRunning),{data:N,isLoading:C,isFetching:k,isSuccess:R,isError:b,error:S,errorSql:D,queryKey:F,refetch:I,errorMessage:M}=s.useDashboardCardQuery(c);L.useEffect(()=>{if(N&&(N.rowLimitExceeded&&s.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),w((N==null?void 0:N.output)||""),N.records.length>0)){const{dimensionKeys:V}=s.getKeys(N.records);m(V)}},[N,m,w]),L.useEffect(()=>{(C||k)&&T(!0),(b||R)&&T(!1),b&&(m([]),w(""))},[C,k,N,w,b,R,T,m,S]),L.useEffect(()=>{A&&(t.getQueryData(F)||I(),y(!1))},[A,I,y,F,t]);const E=L.useRef(I);E.current=I,L.useEffect(()=>{if(o.current){o.current=!1;return}E.current()},[i]);function B(){return e.jsxs(s.ResizablePanelGroup,{className:"flex grow pb-1",direction:"vertical",role:"editor-resizable-group",children:[e.jsxs(s.ResizablePanel,{id:"1",order:1,defaultSize:a?20:100,className:"relative flex grow",children:[e.jsx("div",{id:"sql-container",className:"relative flex w-[99%]",children:e.jsx(Dt,{})}),a&&e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"sql"})]}),a&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizableHandle,{withHandle:!0,className:"border"}),e.jsxs(s.ResizablePanel,{id:"2",order:2,defaultSize:80,className:"relative flex grow",children:[e.jsx("div",{id:"python-container",className:"relative flex w-[99%] pt-0",children:e.jsx(Mt,{})}),e.jsx("code",{className:"absolute right-6 top-3 text-xs text-muted-foreground/70",children:"python"})]})]})]})}function $(){var V,q,G,z,H,_;return e.jsx("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-4",children:[e.jsxs(s.Button,{title:"Shift + Enter",disabled:C||k,onClick:()=>{I()},className:"h-8 shrink-0",size:"sm",children:[C||k?e.jsx(s.LoaderCircle,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(s.Play,{className:"mr-2 h-4 w-4"}),"Run"]}),(N==null?void 0:N.sql)&&!M&&e.jsxs(s.HoverCard,{openDelay:300,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-4 w-4 shrink-0 cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.HoverCardContent,{align:"start",className:"w-[500px]",children:e.jsx(Ms,{sql:N.sql})})]}),e.jsx(Re.EditorClearButton,{}),e.jsxs(s.Toggle,{pressed:a,onPressedChange:P=>l(P),className:"h-8",size:"sm",variant:"outline",children:[e.jsx(It,{className:"mr-2 size-4 text-foreground/70"}),"Python"]}),!d&&e.jsxs("button",{disabled:d,onClick:()=>{g(!0)},className:"flex h-8 w-full cursor-text items-center justify-start rounded-md bg-muted/50 px-3 text-left text-sm text-muted-foreground",children:[e.jsx(s.Sparkles,{className:"mr-3 size-4 text-foreground/70"}),"Type your question..."]}),e.jsxs("div",{className:"flex flex-1 items-center justify-end gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"pagination",checked:((q=(V=c.preferences)==null?void 0:V.tablePrefs)==null?void 0:q.enableDevModePagination)??!1,onCheckedChange:P=>{var Y;j({...c.preferences,tablePrefs:{...(Y=c.preferences)==null?void 0:Y.tablePrefs,enableDevModePagination:P}})}}),e.jsx(s.Label,{htmlFor:"pagination",className:"cursor-pointer text-sm",children:"Paginate"}),((z=(G=c.preferences)==null?void 0:G.tablePrefs)==null?void 0:z.enableDevModePagination)&&e.jsxs(s.Select,{value:String(((_=(H=c.preferences)==null?void 0:H.tablePrefs)==null?void 0:_.pageSize)||50),onValueChange:P=>{var se,W,ae;const Y=Number(P),Z={...c,preferences:{...c.preferences,tablePrefs:{...(se=c.preferences)==null?void 0:se.tablePrefs,pageSize:Y}},paginationConfig:{page:1,pageSize:Y}};v(Z),O(Z),c.sql&&((ae=(W=c.preferences)==null?void 0:W.tablePrefs)!=null&&ae.enableDevModePagination)&&y(!0)},children:[e.jsx(s.SelectTrigger,{className:"h-8 w-20",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"10",children:"10"}),e.jsx(s.SelectItem,{value:"25",children:"25"}),e.jsx(s.SelectItem,{value:"50",children:"50"}),e.jsx(s.SelectItem,{value:"100",children:"100"}),e.jsx(s.SelectItem,{value:"500",children:"500"}),e.jsx(s.SelectItem,{value:"1000",children:"1000"})]})]})]}),r&&r.length>0&&e.jsxs(s.HoverCard,{openDelay:200,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",className:"h-8 gap-2 px-2",children:[e.jsx(s.Filter,{className:"size-4"}),e.jsx(s.Badge,{className:"h-5 min-w-5 bg-purple-100 px-1.5 text-purple-700 hover:bg-purple-100 dark:bg-purple-950 dark:text-purple-300",children:r.length})]})}),e.jsx(s.HoverCardContent,{className:"mr-4 max-h-[400px] w-[500px] overflow-auto p-0",children:e.jsxs("div",{className:"p-3",children:[e.jsx("div",{className:"mb-2 text-sm font-semibold",children:"Active Dashboard Filters"}),e.jsx(s.CodeHighlighter,{code:JSON.stringify(r,null,2),language:"json"})]})})]})]})]})})}return e.jsxs("section",{role:"editor-section",className:"flex grow flex-col justify-end",children:[e.jsx("div",{className:"relative flex grow overflow-y-scroll",children:B()}),$()]})}const Rt=[{value:"table",label:"Table",icon:s.Table2},{value:"aggregateTable",label:"Aggregate",icon:s.Layers},{value:"pivotTable",label:"Pivot",icon:s.Grid3x3}];function Ft({className:t}){const n=s.useEditorStore(o=>o.card),{setCardType:a,setCard:l,updateCardInFrame:c}=s.useEditorActions(),d=["table","aggregateTable","pivotTable"].includes(n.type),r=s.hasValidCardConfig(n.config);if(!d||!r)return null;const i=o=>{var m;const u=o;if(u==="aggregateTable"&&((m=n.config)!=null&&m.pivotByColumns)&&n.config.pivotByColumns.length>0){const x={...n,type:u,config:{...n.config,pivotByColumns:[]}};l(x),c(x)}else{a(u);const x={...n,type:u};c(x)}};return e.jsx("div",{className:s.cn("flex items-center",t),children:e.jsx(s.RadioGroup,{value:n.type,onValueChange:i,className:"flex flex-row items-center justify-between gap-1 rounded-lg border p-1",children:Rt.map(o=>{const u=o.icon,m=n.type===o.value;return e.jsxs("div",{className:"flex items-center",children:[e.jsx(s.RadioGroupItem,{value:o.value,id:`table-type-${o.value}`,className:"peer sr-only"}),e.jsxs(s.Label,{htmlFor:`table-type-${o.value}`,className:s.cn("flex cursor-pointer items-center gap-1.5 rounded-md px-3 py-1 text-sm font-medium transition-all",m?"bg-muted text-foreground shadow-sm":"text-muted-foreground hover:bg-muted/50"),children:[e.jsx(u,{className:"h-3.5 w-3.5"}),o.label]})]},o.value)})})})}function Bt({type:t,isLinked:n,className:a}){const l=s.useDashboardStore(h=>h.selectedFrameId),c=s.useManagementStore(h=>h.selectedVisual),d=s.useEditorStore(h=>h.frame);let r;if(t)r=t;else if(n!==void 0)r=n?"linked":"library";else if(!!!(d!=null&&d.visualId||c!=null&&c.id)&&l)r="local";else if(c&&!l)r="library";else if(d!=null&&d.visualId&&l)r="linked";else if(c!=null&&c.id&&l)r="library";else return null;if(r==="local")return null;const i={linked:"border-purple-200/70 bg-gradient-to-r from-purple-50/50 to-purple-100/30 text-purple-900 shadow-sm hover:from-purple-100/60 hover:to-purple-100/50 hover:shadow-md dark:border-purple-800/50 dark:from-purple-950/40 dark:to-purple-900/30 dark:text-purple-100 dark:hover:from-purple-950/60 dark:hover:to-purple-900/50",library:"border-blue-200/70 bg-gradient-to-r from-blue-50/50 to-blue-100/30 text-blue-900 shadow-sm hover:from-blue-100/60 hover:to-blue-100/50 hover:shadow-md dark:border-blue-800/50 dark:from-blue-950/40 dark:to-blue-900/30 dark:text-blue-100 dark:hover:from-blue-950/60 dark:hover:to-blue-900/50"},o={linked:"Linked to library: Changes made here update the library visual and all linked instances.",library:"Library visual: Share and reuse this visual across multiple dashboards."},u={linked:"border-purple-200 bg-purple-50 text-purple-900 dark:border-purple-800 dark:bg-purple-950 dark:text-purple-100",library:"border-blue-200 bg-blue-50 text-blue-900 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-100"},m={linked:"fill-purple-50 dark:fill-purple-950",library:"fill-blue-50 dark:fill-blue-950"},x={linked:e.jsx(ut,{className:"h-3.5 w-3.5"}),library:e.jsx(dt,{className:"h-3.5 w-3.5"})};return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:300,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Badge,{variant:"outline",className:s.cn("cursor-help select-none transition-all",i[r],a),children:x[r]})}),e.jsxs(s.TooltipContent,{side:"bottom",className:s.cn("max-w-xs",u[r]),children:[e.jsx(s.TooltipArrow,{className:m[r]}),e.jsx("p",{children:o[r]})]})]})})}function Ye({error:t,errorSql:n,errorTitle:a="Unable to load data",onOpenInEditor:l,onDebugWithAssistant:c,onSendFeedback:d,showFeedbackButton:r=!0,className:i}){const[o,u]=L.useState(!1);return e.jsx("div",{className:s.cn("rounded-lg border border-border bg-muted/40 p-4",i),children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-amber-500/10 dark:bg-amber-500/20",children:e.jsx(s.TriangleAlert,{className:"h-4 w-4 text-amber-600 dark:text-amber-500"})}),e.jsxs("div",{className:"flex-1 space-y-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:a}),e.jsxs("div",{className:"relative rounded-md border border-border bg-background p-3",children:[e.jsx(s.CopyButton,{text:t,className:"absolute right-2 top-2 z-10 h-6 w-6"}),e.jsx("div",{className:"max-h-40 overflow-auto pr-10",children:e.jsx("p",{className:"break-all font-mono text-xs text-muted-foreground",children:t})})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[n&&e.jsx(s.Collapsible,{open:o,onOpenChange:u,children:e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-8",children:o?e.jsxs(e.Fragment,{children:[e.jsx(s.ChevronUp,{className:"mr-1.5 h-3.5 w-3.5"}),"Hide Details"]}):e.jsxs(e.Fragment,{children:[e.jsx(s.ChevronDown,{className:"mr-1.5 h-3.5 w-3.5"}),"Show Details"]})})})}),l&&n&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:l,children:[e.jsx(Ts,{className:"mr-1.5 h-3.5 w-3.5"}),"Open in Editor"]}),r&&d&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:d,children:[e.jsx(s.MessageSquareWarning,{className:"mr-1.5 h-3.5 w-3.5"}),"Send Feedback"]}),c&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8",onClick:c,children:[e.jsx(it,{className:"mr-1.5 h-3.5 w-3.5"}),"Debug with Assistant"]})]}),n&&e.jsx(s.Collapsible,{open:o,onOpenChange:u,children:e.jsx(s.CollapsibleContent,{className:"space-y-2",children:e.jsxs("div",{className:"mt-3 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Query"}),e.jsx(s.CopyButton,{text:n,className:"h-6 w-6"})]}),e.jsx("div",{className:"max-h-80 overflow-auto rounded-md border border-border bg-background p-2",children:e.jsx(s.CodeHighlighter,{code:n,language:"sql"})})]})})})]})]})})}function Os(t,n){const a=s.useEditorStore(u=>u.card),l=s.useEditorStore(u=>u.isDevMode),{switchEditorMode:c,setRunSql:d,runAssistant:r}=s.useEditorActions();return{isDevMode:l,handleOpenInEditor:l?void 0:()=>{const u=n||a.sql||a.python||"";c("dev",{sqlToLoad:u}),setTimeout(()=>{d(!0)},100)},handleDebugWithAssistant:l?()=>{const u=`Can you help me debug this query?
|
|
178
173
|
|
|
179
174
|
Error:
|
|
180
175
|
|
|
@@ -184,6 +179,6 @@ ${N.joinType} JOIN ${C.name}
|
|
|
184
179
|
|
|
185
180
|
${n||a.sql||a.python}
|
|
186
181
|
|
|
187
|
-
`;r(u)}:void 0}}function Rs({className:t,...n}){var V,q,G;const a=s.useEditorStore(z=>z.card),l=((V=a.customCardPreferences)==null?void 0:V.showCardHeader)??!0,c=((G=(q=a.preferences)==null?void 0:q.displayOptions)==null?void 0:G.showInlineFilterBar)??!0,d=s.useEditorStore(z=>z.frame),r=s.useDashboardStore(z=>z.dashboard.filters),i=s.useDashboardStore(z=>z.selectedSheetId),[o,u]=L.useState(!1),{currentUser:m}=s.useCurrentUserInfo(),{tokenProps:x}=L.useContext(s.SemaphorContext),h=s.useEditorStore(z=>z.actions.setRunSql),{updateFilter:w}=s.useDashboardActions(),j=r==null?void 0:r.find(z=>z.id===d.filterId),{data:f,isLoading:p,isFetching:g,errorMessage:v,errorSql:O,pagination:A}=s.useDashboardCardQuery(a),{handleOpenInEditor:y,handleDebugWithAssistant:T}=Os(v,O||a.sql||a.python),N=L.useMemo(()=>(f==null?void 0:f.records)||[],[f==null?void 0:f.records]),{setCard:C,updateCardInFrame:k}=s.useEditorActions(),{data:R}=s.useTopoJson(a),b=L.useMemo(()=>{var z;return a.type!=="pivotTable"||!((z=f==null?void 0:f.records)!=null&&z.length)?null:{records:f.records,columnSubtotalMeta:f.columnSubtotalMeta,subtotalColumns:f.subtotalColumns||[],pivotSchema:f.pivotSchema||[],groupByColumns:f.groupByColumns}},[a.type,f==null?void 0:f.records,f==null?void 0:f.columnSubtotalMeta,f==null?void 0:f.subtotalColumns,f==null?void 0:f.pivotSchema,f==null?void 0:f.groupByColumns]),S=s.usePivotTableConfig(a,b),D=()=>{u(!0)},F=L.useMemo(()=>{const z=typeof window<"u"?window.location.href:"",H=typeof navigator<"u"?navigator.userAgent:"",_=typeof window<"u"?`${window.innerWidth}x${window.innerHeight}`:"";return{category:s.FeedbackCategory.QUERY_GENERATION,user:m,tokenContext:x,systemState:{cardConfig:a.config,dataSource:a.dataSource,dashboardFilters:r,errorMessage:v||void 0,errorSql:O||a.sql||a.python,generatedSql:O||a.sql},metadata:{url:z,userAgent:H,timestamp:new Date().toISOString(),viewport:_}}},[m,x,a,r,v,O]),I=(z,H)=>{var P,Y;const _={...a,paginationConfig:{page:z+1,pageSize:H}};C(_),k(_),a.sql&&((Y=(P=a.preferences)==null?void 0:P.tablePrefs)!=null&&Y.enableDevModePagination)&&h(!0)},M=z=>{var P,Y,Z,se;const H=s.sortStateToSortByColumns(z,(P=a.config)==null?void 0:P.metricColumns,(Y=a.config)==null?void 0:Y.groupByColumns),_={...a,config:{...a.config||{},sortByColumns:H}};C(_),k(_),a.sql&&((se=(Z=a.preferences)==null?void 0:Z.tablePrefs)!=null&&se.enableDevModePagination)&&h(!0)},E=L.useMemo(()=>!a.id||!(f!=null&&f.records)?null:s.createChartConfig({card:a,data:f==null?void 0:f.records,cardType:a.type,queryConfig:a.queryConfig,customCfg:a.customCfg,preferences:a.preferences,topoJson:R}),[a.id,f==null?void 0:f.records,a.type,a.customCfg,a.preferences,R]);function B(z){return e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(s.EditorCustomVisual,{card:z})})}function $(){if(f!=null&&f.records&&(f==null?void 0:f.records.length)===0&&!p&&!g)return v?e.jsx("div",{className:"flex h-full flex-col items-start justify-start overflow-auto p-4",children:e.jsx(Ye,{error:v,errorSql:O||a.sql||a.python,onOpenInEditor:y,onDebugWithAssistant:T,onSendFeedback:D,showFeedbackButton:!0})}):e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No results to display"})]});if((p||g)&&(!(f!=null&&f.records)||f.records.length===0))return a.type==="table"?e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableSkeleton,{})}):e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})});if(!a.id)return null;if(a.type==="custom")return B(a);if(!["table","pivotTable","aggregateTable","custom"].includes(a.type)&&E)return e.jsx("div",{className:"relative h-full min-h-0 w-full flex-1 grow basis-0",children:p||g?e.jsx(e.Fragment,{}):e.jsx(s.ChartJsVisual,{cfg:E})});if(a.type==="table")return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableVisual,{card:a,data:N,paginationMetadata:A,onPaginationChange:I,onSortChange:M,isLoading:p||g})});if(a.type==="pivotTable"&&S)return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.PivotTableTanstack,{card:a,data:S.data,options:S.options,onAggregationChange:z=>{const H=s.handleAggregationChange(z,a.config),_={...a,config:H};C(_),k(_)},onAggregationLabelChange:z=>{const H=s.handleAggregationLabelChange(z,a.config),_={...a,config:H};C(_),k(_)}})});if(a.type==="aggregateTable")return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.AggregateTable,{card:a,data:(f==null?void 0:f.records)||[],groupByColumns:f==null?void 0:f.groupByColumns,pivotSchema:f==null?void 0:f.pivotSchema,paginationMetadata:A,onPaginationChange:I,onAggregationChange:z=>{const H=s.handleAggregationChange(z,a.config),_={...a,config:H};C(_),k(_)},isLoading:p||g})})}return e.jsxs(s.Card,{role:"editor-visual-card",className:s.cn("relative flex h-full flex-col rounded-none",t),...n,children:[(p||g)&&(f==null?void 0:f.records)&&f.records.length>0&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),l&&e.jsxs(s.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.CardTitle,{className:"text-base",children:e.jsx(s.EditableText,{value:a.title,onSave:z=>C({...a,title:z}),isEditable:!0,placeholder:"Add title",displayClassName:"text-base font-semibold",inputClassName:"text-base font-semibold"})}),e.jsx(Pt,{}),e.jsx("div",{className:"ml-auto",children:e.jsx(Bt,{})})]}),e.jsx("div",{className:"w-1/2",children:e.jsx(s.EditableText,{value:(a.description||"").trim(),onSave:z=>C({...a,description:z.trim()}),isEditable:!0,placeholder:"Add description",displayClassName:"text-sm text-muted-foreground",inputClassName:"text-sm"})})]}),j&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Label,{className:"my-1",children:e.jsx(s.EditableText,{value:j.title||j.column,onSave:z=>w({...j,title:z}),isEditable:!0,placeholder:"Filter",displayClassName:"text-sm",inputClassName:"text-sm"})}),e.jsx(s.FilterComponent,{filter:j})]})]}),c&&a.inlineFilters&&a.inlineFilters.length>0&&e.jsx("div",{className:"px-6 pb-3",children:e.jsx(s.InlineFilterBar,{card:a,sheetId:i||""})}),e.jsx(s.CardContent,{className:"flex grow flex-col",children:$()}),e.jsx(s.FeedbackDialog,{open:o,onOpenChange:u,feedbackData:F,showDebugInfo:!0})]})}const ze=({language:t,value:n,onChange:a,readonly:l})=>e.jsx(s.Ft,{className:"h-full w-full overflow-hidden text-xs",theme:"default",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,lineHeight:16,renderLineHighlight:"none",readOnly:l},defaultLanguage:t,value:n,onChange:a}),Vt=()=>e.jsxs(s.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(s.DialogTitle,{children:"Customize Card"})}),e.jsxs(s.DialogDescription,{children:["See the configuration options and documentation",e.jsx("a",{target:"_blank",className:"ml-1 text-blue-500 underline",href:"https://www.chartjs.org/docs/latest/samples/bar/vertical.html",children:"here."})]})]}),zt=({customCode:t,cardCustomCfgString:n,mergedConfig:a,onConfigChange:l,onCodeChange:c})=>e.jsxs(s.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(s.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(s.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(s.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(s.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(s.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ze,{language:"json",value:a,onChange:l})}),e.jsx(s.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ze,{readonly:!0,language:"json",value:n,onChange:()=>{}})}),e.jsx(s.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx(ze,{language:"javascript",value:t,onChange:c})})]}),$t=({error:t,hasCardCustomConfig:n,onReset:a,onApply:l,onClose:c})=>e.jsxs(s.DialogFooter,{className:"flex items-center",children:[e.jsx(s.Label,{className:"mr-2 text-red-500",children:t}),e.jsx(s.Button,{disabled:!n,onClick:a,variant:"outline",children:"Reset"}),e.jsx(s.Button,{onClick:l,variant:"outline",children:"Apply"}),e.jsx(s.Button,{onClick:c,variant:"outline",children:"Close"})]});function qt(){const[t,n]=L.useState(!1),[a,l]=L.useState(""),[c,d]=L.useState(""),[r,i]=L.useState(""),[o,u]=L.useState(""),m=s.useEditorStore(y=>y.card),x=s.useEditorStore(y=>y.card.customCfg),h=x?JSON.stringify(x,null,2):"",{data:w}=s.useDashboardCardQuery(m),{data:j}=s.useTopoJson(m),{setCardCustomCfg:f,setCustomVisualCode:p}=s.useEditorActions(),g=y=>{var T,N;return JSON.stringify({...y,data:{...y.data,datasets:(N=(T=y.data)==null?void 0:T.datasets)==null?void 0:N.map(C=>({...C,data:[]}))}},null,2)},v=()=>{var T,N;if(!((T=w==null?void 0:w.records)!=null&&T.length))return;const y=s.createChartConfig({card:m,data:w.records,cardType:m.type,queryConfig:m.queryConfig,preferences:m.preferences,topoJson:j});if(y){l(g(y)),i(((N=m.preferences)==null?void 0:N.customVisualCode)||"");const C=s.merge(y,x);d(g(C))}},O=()=>{try{if(r&&p(r),c){const y=JSON.parse(c),T=s.getObjectDiff(JSON.parse(a),y);f(T),u("")}}catch{u("Invalid JSON configuration")}},A=Object.keys(x||{}).length===0;return e.jsxs(s.Dialog,{open:t,onOpenChange:n,children:[e.jsx(s.DialogTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Customize Visual",onClick:v,className:A?"":"text-green-600 hover:text-green-700",children:e.jsx(s.Settings,{className:"h-4 w-4"})})}),e.jsxs(s.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(Vt,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(Rs,{className:"w-1/2 rounded shadow-none"})}),e.jsx(zt,{customCode:r,cardCustomCfgString:h,mergedConfig:c,onConfigChange:y=>d(y??""),onCodeChange:y=>i(y??"")})]}),e.jsx($t,{error:o,hasCardCustomConfig:!!x,onReset:()=>{d(a),f(null)},onApply:O,onClose:()=>n(!1)})]})]})}function Kt({isStarred:t,onClick:n,className:a}){const l=c=>{c.stopPropagation(),c.preventDefault(),n(c)};return e.jsx("button",{type:"button",onClick:l,className:s.cn("flex items-center justify-center rounded-sm p-0.5 transition-all","hover:bg-muted/80",a),"aria-label":t?"Remove from favorites":"Add to favorites",children:e.jsx(s.Star,{className:s.cn("h-3.5 w-3.5 transition-colors",t?"fill-yellow-400 text-yellow-400":"text-muted-foreground hover:text-foreground")})})}function cs({chartType:t,chartName:n,isCustom:a=!1,pluginName:l,isStarred:c,showStar:d=!0,onSelect:r,onToggleStar:i}){return e.jsxs("button",{onClick:r,className:s.cn("group relative flex flex-col items-center gap-2 rounded-lg border border-border","bg-background p-3 transition-all","hover:border-primary/50 hover:bg-muted/50 hover:shadow-sm"),children:[d&&e.jsx("div",{className:s.cn("absolute left-1 top-1 transition-opacity",c?"opacity-100":"opacity-0 group-hover:opacity-100"),children:e.jsx(Kt,{isStarred:c,onClick:()=>i()})}),a&&l&&e.jsx("div",{className:"absolute right-1 top-1",children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:"flex items-center justify-center",children:e.jsx(s.Info,{className:"h-3 w-3 text-muted-foreground"})})}),e.jsx(s.TooltipContent,{side:"top",className:"text-xs",children:l})]})}),e.jsx(s.ChartIcon,{chartType:t,chartName:n,isCustom:a,className:"h-6 w-6"}),e.jsx("span",{title:n,className:"line-clamp-2 text-center text-xs font-medium text-foreground",children:n})]})}const ds=[{id:"table",name:"Table"},{id:"bar",name:"Bar Chart"},{id:"line",name:"Line Chart"},{id:"area",name:"Area Chart"},{id:"combo",name:"Combo Chart"},{id:"stackedBar",name:"Stacked Bar"},{id:"stackedArea",name:"Stacked Area"},{id:"stackedLine",name:"Stacked Line"},{id:"scatter",name:"Scatter"},{id:"bubble",name:"Bubble"},{id:"pie",name:"Pie Chart"},{id:"doughnut",name:"Doughnut"},{id:"funnel",name:"Funnel"},{id:"radar",name:"Radar"},{id:"polarArea",name:"Polar Area"},{id:"tornado",name:"Tornado"},{id:"range",name:"Range"},{id:"bullet",name:"Bullet Chart"},{id:"treemap",name:"Treemap"},{id:"kpi",name:"KPI"},{id:"text",name:"Text"},{id:"map",name:"Map"}];function Fs({onChartSelect:t,onClose:n}){const[a,l]=L.useState(""),{starredCharts:c,isStarred:d,toggleStar:r}=s.useStarredChartsStore(),{data:i=[],isLoading:o}=s.useCustomVisualsQuery(),{setCardType:u,setCardPreferences:m,setIsDevMode:x}=s.useEditorStore(y=>y.actions),{setCardCustomCfg:h,setCustomCardPreferences:w,setQueryConfig:j}=s.useEditorActions(),f=L.useMemo(()=>{if(!a)return ds;const y=a.toLowerCase();return ds.filter(T=>T.name.toLowerCase().includes(y))},[a]),p=L.useMemo(()=>{const y=i.filter(N=>N.componentType==="chart");if(!a)return y;const T=a.toLowerCase();return y.filter(N=>N.name.toLowerCase().includes(T))},[i,a]),g=y=>{h(null),m({}),u(y),["custom","text","map"].includes(y)&&(x(!0),j(void 0)),t==null||t(y),n==null||n()},v=y=>{h(null),m({}),u("custom"),w({type:"component",visualType:"single",url:y.url,componentName:y.name,icon:y.icon,pluginChartType:y.chartType}),j(void 0),t==null||t("custom",y),n==null||n()},O=y=>{const T={type:"default",id:y.id,name:y.name};r(T)},A=y=>{const T={type:"custom",id:y.name,url:y.url,icon:y.icon,name:y.name};r(T)};return e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"mb-3 flex items-center gap-2 rounded-sm border border-border bg-background px-3 py-2",children:[e.jsx(s.Search,{className:"h-4 w-4 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search charts...",value:a,onChange:y=>l(y.target.value),className:"h-auto border-0 p-0 text-sm focus-visible:ring-0 focus-visible:ring-offset-0"})]}),e.jsxs("div",{className:"max-h-[400px] overflow-y-auto",children:[f.length>0&&e.jsxs("div",{className:"mb-3",children:[e.jsx("span",{className:"mb-2 block text-xs font-medium text-muted-foreground",children:"Default Charts"}),e.jsx("div",{className:"grid grid-cols-3 gap-2",children:f.map(y=>e.jsx(cs,{chartType:y.id,chartName:y.name,isCustom:!1,isStarred:d("default",y.id),onSelect:()=>g(y.id),onToggleStar:()=>O(y)},y.id))})]}),p.length>0&&e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-3"}),e.jsxs("div",{children:[e.jsx("span",{className:"mb-2 block text-xs font-medium text-muted-foreground",children:"Custom Charts"}),e.jsx("div",{className:"grid grid-cols-3 gap-2",children:p.map((y,T)=>e.jsx(cs,{chartName:y.name,isCustom:!0,pluginName:y.pluginName,isStarred:d("custom",y.name),onSelect:()=>v(y),onToggleStar:()=>A(y)},`${y.url}-${y.name}-${T}`))})]})]}),o&&e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-3"}),e.jsx("div",{className:"py-4 text-center text-sm text-muted-foreground",children:"Loading custom charts..."})]}),f.length===0&&p.length===0&&!o&&e.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"No charts found"})]})]})}function Bs({variant:t="ghost"}){var d,r;const[n,a]=L.useState(!1),l=s.useEditorStore(i=>i.card),c=l.type;return e.jsxs(s.Popover,{open:n,onOpenChange:a,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{size:"sm",variant:t||"ghost",children:e.jsx(s.SelectedChartIcon,{className:"size-4",chartType:c,customIcon:(d=l.customCardPreferences)==null?void 0:d.icon,componentName:(r=l.customCardPreferences)==null?void 0:r.componentName})})}),e.jsx(s.PopoverContent,{onOpenAutoFocus:i=>i.preventDefault(),className:"z-[70] w-[360px] p-3",children:e.jsx(Fs,{onClose:()=>a(!1)})})]})}function Gt(){const t=s.useEditorStore(o=>{var u,m;return(m=(u=o.card.preferences)==null?void 0:u.chartOptions)==null?void 0:m.indexAxis}),n=s.useEditorStore(o=>o.card.type),{setChartOrientation:a,setCardPreferences:l}=s.useEditorStore(o=>o.actions),c=o=>{if(a(o),n!=="bullet")return;const u=o==="y"?"horizontal":"vertical",m=s.useEditorStore.getState().card.preferences,x={...m||{},bulletConfig:{...(m==null?void 0:m.bulletConfig)||{},orientation:u}};l(x)};function d(){const o=t??"x",u=()=>{c(o==="x"?"y":"x")};return e.jsx(s.IconButton,{onClick:u,tooltip:"Chart Orientation",children:o==="x"?e.jsx(Ve,{className:"size-4"}):e.jsx(Pe,{className:"size-4"})})}function r(){const o=()=>{c(t==="y"||t===void 0?"x":"y")};return e.jsx(s.IconButton,{onClick:o,tooltip:"Chart Orientation",children:t==="y"||t===void 0?e.jsx(Ve,{className:"size-4"}):e.jsx(Pe,{className:"size-4"})})}function i(){const o=t??"y",u=()=>{c(o==="y"?"x":"y")};return e.jsx(s.IconButton,{onClick:u,tooltip:"Chart Orientation",children:o==="y"?e.jsx(Pe,{className:"size-4"}):e.jsx(Ve,{className:"size-4"})})}return e.jsx(e.Fragment,{children:n==="funnel"?r():n==="bullet"?i():d()})}function Ut({className:t}){var w,j,f,p,g,v,O,A,y,T;const n=s.useEditorStore(N=>N.card),{setCardPreferences:a,updateCardInFrame:l,setCard:c}=s.useEditorActions(),[d,r]=L.useState(!1),i=((j=(w=n.preferences)==null?void 0:w.displayOptions)==null?void 0:j.showFilterInfo)??!0,o=((p=(f=n.preferences)==null?void 0:f.displayOptions)==null?void 0:p.showCardToolbar)??!0,u=((v=(g=n.preferences)==null?void 0:g.displayOptions)==null?void 0:v.showChrome)??!0,m=((A=(O=n.preferences)==null?void 0:O.displayOptions)==null?void 0:A.allowScroll)??!0,x=((T=(y=n.preferences)==null?void 0:y.displayOptions)==null?void 0:T.showInlineFilterBar)??!0,h=(N,C)=>{var b;const k={...n.preferences,displayOptions:{...(b=n.preferences)==null?void 0:b.displayOptions,[N]:C}};a(k);const R={...n,preferences:k};c(R),l(R)};return e.jsx("div",{className:s.cn("flex items-center",t),children:e.jsxs(s.DropdownMenu,{modal:!1,open:d,onOpenChange:r,children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.IconButton,{className:"h-8 w-8 p-0",tooltip:"Display Options",children:e.jsx(Es,{className:"h-4 w-4"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",className:"w-64",children:[e.jsx(s.DropdownMenuLabel,{children:"Display Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx("div",{className:"px-2 py-1.5",children:e.jsx("p",{className:"mb-2 text-xs text-muted-foreground",children:"Changes apply to dashboard view only"})}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:i,onCheckedChange:N=>{h("showFilterInfo",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Filter Info"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Badge"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:N=>{h("showCardToolbar",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Card Toolbar"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Hover actions"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:u,onCheckedChange:N=>{h("showChrome",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Card Chrome"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Frame & padding"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:m,onCheckedChange:N=>{h("allowScroll",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Allow Scroll"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Card content scroll"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:x,onCheckedChange:N=>{h("showInlineFilterBar",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Inline Filter Bar"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Card filters"})]})]})]})})}function _t(){const[t,n]=L.useState(!1),a=s.useEditorStore(x=>x.card),l=s.useEditorStore(x=>x.selectedConnectionId),{setCard:c}=s.useEditorActions(),d=s.useDashboardStore(x=>x.actions.removeCardInlineFilterValue),r=(a==null?void 0:a.inlineFilters)||[];function i(x){var O,A,y;const h=((y=(A=(O=a==null?void 0:a.dataSource)==null?void 0:O.selectedEntities)==null?void 0:A[0])==null?void 0:y.database)||"",w=x.qualifiedFieldName||x.name;if(r.find(T=>T.column===w))return;const f=x.qualifiedFieldName?x.qualifiedFieldName.split(".").slice(0,-1).join("."):x.qualifiedEntityName||x.entityName,p=s.fmt(x.name),g=x.qualifiedFieldName||p,v={id:s.v4(),column:w,title:x.label||x.name,dataType:x.dataType,table:x.entityName||"",database:h,connectionId:l||"",operation:"in",sql:s.getDefaultFilterSql(x.dataType,f,g),width:250};c({...a,inlineFilters:[...r,v]})}function o(x){const h=r.filter(w=>w.id!==x);c({...a,inlineFilters:h}),d(a.id,x)}function u(x,h){const w=r.map(j=>j.id===x?{...j,...h}:j);c({...a,inlineFilters:w})}return["bar","line","pie","doughnut","table","stackedBar","stackedLine","horizontalBar","radar","polarArea","scatter","bubble","funnel","aggregateTable","custom"].includes(a==null?void 0:a.type)?e.jsx("div",{className:"flex items-center gap-1",children:e.jsxs(s.Popover,{open:t,onOpenChange:n,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.IconButton,{tooltip:"Add Inline Filter",className:s.cn("relative",{"text-primary":r.length>0}),children:[e.jsx(ht,{className:"h-4 w-4"}),r.length>0&&e.jsx("span",{className:"absolute -right-1 -top-1 flex h-4 w-4 items-center justify-center rounded-full bg-primary text-[10px] font-medium text-primary-foreground",children:r.length})]})}),e.jsx(s.PopoverContent,{align:"end",className:"w-80 p-0",children:e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"border-b px-4 py-3",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Inline Filters"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"On-card filters for fast, focused exploration."})]}),e.jsx("div",{className:"flex max-h-[300px] flex-col overflow-hidden",children:e.jsx(s.DndContext,{collisionDetection:s.closestCenter,children:e.jsx(s.SortableContext,{items:[],strategy:s.verticalListSortingStrategy,children:e.jsx(s.FieldsList,{context:"inline-filter",onFieldSelect:i,inlineFilters:r})})})}),r.length>0&&e.jsxs("div",{className:"border-t px-4 py-3",children:[e.jsx("p",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Active Filters"}),e.jsx("div",{className:"space-y-1",children:r.map(x=>e.jsx(Ht,{filter:x,onUpdate:h=>u(x.id,h),onRemove:()=>o(x.id)},x.id))})]})]})})]})}):null}function Ht({filter:t,onUpdate:n,onRemove:a}){return e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-muted px-2 py-1.5 text-sm",children:[e.jsx("span",{className:"truncate",title:t.column,children:t.title}),e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx(s.CustomFilterPopover,{filter:t,variant:"inline",onAccept:l=>n(l),trigger:e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-5 w-5 shrink-0",title:"Configure filter",children:e.jsx(s.Settings2,{className:"h-3 w-3"})})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-5 w-5 shrink-0 hover:bg-destructive/10 hover:text-destructive",onClick:a,children:e.jsx(s.X,{className:"h-3 w-3"})})]})]})}function Ps(){const t=s.useEditorStore(d=>d.card),n=s.useEditorStore(d=>{var r,i;return(i=(r=d.card)==null?void 0:r.preferences)==null?void 0:i.chartOptions}),a=s.getChartGridStyle(n),{setChartGrid:l}=s.useEditorActions();function c(){const d=["xy","none","x","y"],i=(d.indexOf(a)+1)%d.length;l(d[i])}return e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center gap-2",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel","bullet"].includes(t.type)&&e.jsx(e.Fragment,{children:e.jsx(Gt,{})}),!["kpi","text","custom","table"].includes(t.type)&&e.jsx(e.Fragment,{}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(t.type)&&e.jsxs(s.IconButton,{tooltip:"Grid Style",onClick:c,children:[(a==="xy"||!a)&&e.jsx(s.Grid3x3,{className:"size-4"}),a==="x"&&e.jsx(ws,{className:"size-4"}),a==="y"&&e.jsx(Je,{className:"size-4"}),a==="none"&&e.jsx(Ct,{className:"size-4"})]}),e.jsx(_t,{}),e.jsx(Ut,{})]})}function Jt(){const t=s.useEditorStore(p=>p.frame),n=s.useEditorStore(p=>p.card),{setFrame:a,setActiveTabCardId:l,setCard:c,updateCardInFrame:d,clearModeDrafts:r,initializeModeDrafts:i}=s.useEditorActions(),o=(p,g)=>{p.dataTransfer.setData("tabIndex",g.toString())},u=p=>{p.preventDefault()},m=(p,g)=>{const v=p.dataTransfer.getData("tabIndex");if(v===g.toString())return;const O=Array.from(t.cards),[A]=O.splice(parseInt(v),1);O.splice(g,0,A),a({...t,cards:O})};function x(p){const g=t.cards.filter(y=>y.id!==p),v=g[0],O={...t,cards:g,activeCardId:v.id},A=p===n.id;A&&r(),a(O),l(v.id),c(v),A&&i(v)}function h(p,g){const v={...t,cards:[...t.cards,g],activeCardId:p.id};a(v),l(g.id)}function w(){const p={...n,id:s.v4(),title:`${n.title} Copy`,tabTitle:`${n.tabTitle||n.title} Copy`};h(n,p)}function j(p){const g={dataSource:p.dataSource,connectionId:p.connectionId,lastSelectedDatabase:p.lastSelectedDatabase,lastSelectedSchema:p.lastSelectedSchema,lastSelectedTable:p.lastSelectedTable,lastSelectedDatamodelId:p.lastSelectedDatamodelId,id:s.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[],mode:"explorer"};h(p,g)}function f(p,g){return p.cards.length===1&&(g.displayTab===!0||g.displayTab===void 0)||p.activeCardId!==g.id&&(g.displayTab===!0||g.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const v=g.displayTab===void 0?!1:!g.displayTab;c({...g,displayTab:v}),d({...g,displayTab:v})},children:g.displayTab===!0||g.displayTab===void 0?e.jsx(s.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(rt,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}return e.jsx(s.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[t.cards.map((p,g)=>e.jsxs(s.TabsTrigger,{draggable:!0,onDragStart:v=>o(v,g),onDragOver:u,onDrop:v=>m(v,g),className:"",value:p.id,children:[f(t,p),e.jsx(s.Editable,{enabled:t.activeCardId===p.id,text:p.tabTitle||p.title,onSave:v=>{c({...n,tabTitle:v}),d({...n,tabTitle:v})},children:p.tabTitle||p.title},p.id),t.activeCardId===p.id&&e.jsx(s.CopyPlus,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:v=>{v.stopPropagation(),w()}}),t.activeCardId===p.id&&t.cards.length>1&&e.jsx(s.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>x(p.id)})]},p.id)),e.jsx(s.IconButton,{tooltip:"Add Card",className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",onClick:()=>j(n),children:e.jsx(s.Plus,{className:"h-4 w-4"})})]})})}function _e(){const t=s.useEditorStore(g=>g.frame),n=s.useEditorStore(g=>g.card),a=s.useEditorStore(g=>g.isSqlRunning);s.useEditorStore(g=>g.isDevMode);const{setFrame:l,setActiveTabCardId:c,setCard:d,setRunSql:r,mergeDraftsIntoCard:i,clearModeDrafts:o,initializeModeDrafts:u}=s.useEditorActions(),{data:m,isLoading:x,isFetching:h,isError:w}=s.useDashboardCardQuery(n);function j(g){const v=t.cards.find(O=>O.id===g);v&&(i(),f(g),c(g),o(),u(v))}function f(g){const v=s.useEditorStore.getState().card,O=s.useEditorStore.getState().frame,A={...O,cards:O.cards.map(T=>T.id===v.id?v:T),activeCardId:g};l(A);const y=A.cards.find(T=>T.id===g);y.sql&&r(!0),d(y)}function p(){return n.type==="kpi"?x||h?e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}):e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border border-border",children:e.jsx(s.KPICard,{card:n,data:m==null?void 0:m.records,isPending:x||h,isError:w})})}):n.type==="text"?x||h?e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}):e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(s.TextCard,{isLoading:a,card:n,data:m==null?void 0:m.records})})})}):e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(Rs,{className:"grow border-none shadow-none"})})}return e.jsxs(s.Tabs,{onValueChange:j,value:t.activeCardId,className:"flex grow flex-col",children:[t.cards.map(g=>e.jsx(s.TabsContent,{className:"mt-0 grow",value:g.id,children:p()},g.id)),e.jsx(Jt,{})]})}function us({children:t}){const n=s.useEditorStore(M=>M.frame),a=s.useEditorStore(M=>M.card),l=s.useEditorStore(M=>M.isDevMode),c=s.useEditorStore(M=>M.isShowingVisual),d=s.useDashboardStore(M=>M.isVisualEditing),r=s.useEditorStore(M=>M.pythonStdOut),i=s.useEditorStore(M=>M.actions.setRunSql),{setFrame:o,setCard:u,setActiveTabCardId:m,runAssistant:x,setCardPreferences:h,mergeDraftsIntoCard:w,clearModeDrafts:j,initializeModeDrafts:f}=s.useEditorActions(),{data:p,isLoading:g,isFetching:v,errorMessage:O,errorSql:A,refetch:y,pagination:T}=s.useDashboardCardQuery(a),{handleDebugWithAssistant:N}=Os(O,A),C=L.useMemo(()=>(p==null?void 0:p.records)||[],[p==null?void 0:p.records]),[k,R]=L.useState(!1);L.useEffect(()=>{v||R(!1)},[v]);function b(M,E){var V,q;const B={...a,paginationConfig:{page:M+1,pageSize:E}};u(B);const $={...n,cards:n.cards.map(G=>G.id===a.id?B:G)};o($),a.sql&&((q=(V=a.preferences)==null?void 0:V.tablePrefs)!=null&&q.enableDevModePagination)&&i(!0)}function S(M){var V,q,G,z;const E=s.sortStateToSortByColumns(M,(V=a.config)==null?void 0:V.metricColumns,(q=a.config)==null?void 0:q.groupByColumns),B={...a,config:{...a.config||{},sortByColumns:E}};u(B);const $={...n,cards:n.cards.map(H=>H.id===a.id?B:H)};o($),a.sql&&((z=(G=a.preferences)==null?void 0:G.tablePrefs)!=null&&z.enableDevModePagination)&&i(!0)}function D(){const M=JSON.stringify(p==null?void 0:p.records,null,2),E=new Blob([M],{type:"application/json"}),B=URL.createObjectURL(E),$=document.createElement("a");$.href=B,$.download=`${a.title}.json`,$.click()}function F(){const E=[Object.keys(p==null?void 0:p.records.reduce((q,G)=>({...q,...G}),{})).join(","),...((p==null?void 0:p.records)||[]).map(q=>Object.values(q).join(","))].join(`
|
|
188
|
-
`),B=new Blob([E],{type:"text/csv"}),$=URL.createObjectURL(B),V=document.createElement("a");V.href=$,V.download=`${a.title}.csv`,V.click()}function I(){const M=`Can you get the documentation for ${a.type} chart?`;x(M)}return a?e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[e.jsxs("div",{className:"z-10 mb-0 flex min-h-10 items-center justify-end gap-2 px-6",children:[c&&e.jsxs("div",{className:"flex grow items-center",children:[e.jsx(Bs,{}),d&&e.jsx(s.IconButton,{tooltip:"Visual Docs",onClick:I,children:e.jsx(tt,{className:"size-4"})}),!["table","custom"].includes(a.type)&&d&&e.jsx(qt,{})]}),!c&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:F,children:e.jsx(s.PiFileCsv,{className:"size-4"})}),e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:D,children:e.jsx(lt,{strokeWidth:1.5,className:"size-4"})}),e.jsx(s.CopyButton,{text:JSON.stringify(p==null?void 0:p.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[t,e.jsx(s.IconButton,{tooltip:"Refresh",disabled:g||v,onClick:()=>{R(!0),y()},children:e.jsx(s.RefreshCcw,{className:s.cn("h-4 w-4",{"animate-spin":k})})}),e.jsx(Ps,{})]}),l&&e.jsx(Qt,{})]}),c?e.jsx(_e,{}):e.jsxs(s.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[r&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:r&&e.jsx(Yt,{})}),e.jsx(s.ResizableHandle,{})]}),e.jsxs(s.ResizablePanel,{id:"2",order:2,minSize:10,className:s.cn("relative flex h-full min-h-0 w-full flex-1 basis-0",{"px-6":r}),children:[(p==null?void 0:p.records)&&(p==null?void 0:p.records.length)>0&&e.jsx(s.TableVisual,{card:a,data:C,paginationMetadata:T,onPaginationChange:b,onSortChange:S,isLoading:g||v}),O&&e.jsx("div",{className:"flex h-full flex-col items-start justify-start overflow-auto p-4",children:e.jsx(Ye,{error:O,errorSql:A||a.sql||a.python,onDebugWithAssistant:N,showFeedbackButton:!1})})]})]})]}):e.jsx(e.Fragment,{children:"No Active Card"})}function Yt(){const t=s.useEditorStore(a=>a.pythonStdOut);if(!t)return null;const n=t.replace(/\n/g,"<br>");return e.jsxs("div",{className:"relative flex w-full flex-col",children:[e.jsx("div",{className:"sticky top-0 flex max-h-11 min-h-11 grow items-center font-mono text-xs font-semibold text-foreground/70",children:"Python Output"}),e.jsxs(s.ScrollArea,{className:"overflow-y-auto pt-1 font-mono text-xs text-foreground/90",children:[e.jsx("div",{dangerouslySetInnerHTML:{__html:n}}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function Qt(){const t=s.useEditorStore(a=>a.isShowingVisual),n=s.useEditorStore(a=>a.actions.setIsShowingVisual);return e.jsxs(s.Toggle,{pressed:!t,onPressedChange:()=>n(!t),variant:"outline",size:"sm",className:"h-8 gap-2",children:[e.jsx(s.Table2,{className:"h-4 w-4"}),e.jsx("span",{className:"text-sm",children:"Results"})]})}function oe({value:t,onCommit:n,type:a="text",placeholder:l,className:c}){const[d,r]=L.useState(t),i=L.useRef(!1);L.useEffect(()=>{r(t)},[t]);const o=()=>{if(i.current){i.current=!1;return}d!==t&&n(d)},u=m=>{if(m.key==="Enter"){m.currentTarget.blur();return}m.key==="Escape"&&(i.current=!0,r(t),m.currentTarget.blur())};return e.jsx(s.Input$1,{type:a,placeholder:l,value:d,onChange:m=>r(m.target.value),onBlur:o,onKeyDown:u,className:c})}function pe({defaultValue:t,label:n,options:a,value:l,onValueChange:c,className:d,icon:r,showPlaceholderLabel:i=!0}){return e.jsxs(s.Select,{defaultValue:t,value:l,onValueChange:c,children:[e.jsx(s.SelectTrigger,{className:s.cn("w-full focus:ring-0 focus:ring-offset-0",d),children:e.jsxs("div",{className:"flex items-center gap-2",children:[r&&e.jsx("span",{children:r}),i&&e.jsx(s.SelectValue,{placeholder:n})]})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:n}),a.map(o=>e.jsx(s.SelectItem,{value:o.value,children:o.label},o.value))]})})]})}const Wt={table:({children:t})=>e.jsx("table",{className:"my-3 w-full border-collapse overflow-hidden rounded-md border border-border text-sm",children:t}),thead:({children:t})=>e.jsx("thead",{className:"bg-muted",children:t}),th:({children:t})=>e.jsx("th",{className:"border border-border px-3 py-2 text-left font-medium",children:t}),td:({children:t})=>e.jsx("td",{className:"border border-border px-3 py-2",children:t}),code:({children:t,className:n})=>(n==null?void 0:n.includes("language-"))?e.jsx("pre",{className:"my-3 overflow-x-auto rounded-md bg-muted p-3 text-xs",children:e.jsx("code",{children:t})}):e.jsx("code",{className:"rounded bg-muted px-1 py-0.5 text-xs",children:t}),pre:({children:t})=>e.jsx(e.Fragment,{children:t})};function Vs(){var f,p;const t=s.useEditorStore(g=>g.card.customCardPreferences),{data:n}=s.usePluginQuery(),a=(n==null?void 0:n.plugins)||[],l=s.useEditorStore(g=>g.card),c=s.useEditorStore(g=>g.frame),d=(t==null?void 0:t.visualType)||"single",{setCustomCardPreferences:r}=s.useEditorActions(),{components:i,manifest:o}=s.useCustomVisual((t==null?void 0:t.url)||""),u=(f=o==null?void 0:o.visuals)==null?void 0:f.find(g=>g.name===(t==null?void 0:t.componentName));function m(g,v){if(!t)return;const A={...(t==null?void 0:t.dataInputCardIds)||{},[g]:{cardId:v,hookRef:null}};r({...t,dataInputCardIds:A})}function x(g){var O,A;const v=(O=c==null?void 0:c.cards)==null?void 0:O.filter(y=>{var T;return((T=y==null?void 0:y.customCardPreferences)==null?void 0:T.visualType)!=="multiple"}).map(y=>({value:y.id,label:y.tabTitle||y.title}));return e.jsx("div",{className:"space-y-2",children:(A=g==null?void 0:g.dataInputs)==null?void 0:A.map((y,T)=>{var N,C;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:y}),e.jsx(pe,{className:"col-span-6 h-8 w-full justify-between",label:"",options:v||[],value:((C=(N=t==null?void 0:t.dataInputCardIds)==null?void 0:N[T])==null?void 0:C.cardId)||"",onValueChange:k=>m(T,k)})]},T+Math.random())})})}function h(g,v,O=[],A=""){var y,T;return t?g==="input"?e.jsx(oe,{className:`h-8 w-full ${s.removeRing}`,value:String(((y=t==null?void 0:t.settings)==null?void 0:y[v])??A??""),onCommit:N=>{r({...t,settings:{...t==null?void 0:t.settings,[v]:N}})}},v):g==="select"?e.jsx(pe,{className:"h-8 w-full",label:"",options:O,value:((T=t==null?void 0:t.settings)==null?void 0:T[v])||A,onValueChange:N=>{r({...t,settings:{...t==null?void 0:t.settings,[v]:N}})}}):null:null}function w(g){const v=Object.entries((g==null?void 0:g.settings)||{});return e.jsx("div",{className:"space-y-3",children:v.map(([O,A])=>{var y;return e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{children:A.title}),((y=A.docs)==null?void 0:y.description)&&e.jsx(s.TooltipProvider,{delayDuration:0,children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-3.5 w-3.5 cursor-help text-muted-foreground"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:e.jsx("p",{className:"text-xs",children:A.docs.description})})]})})]}),h(A.ui,O,A.options,A.defaultValue)]},O)})})}function j(g){t&&r({...t,visualType:g,componentName:""})}return e.jsxs(s.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"",children:[e.jsxs(s.AccordionItem,{value:"plugin-settings",children:[e.jsx(s.AccordionTrigger,{children:"Plugin"}),e.jsx(s.AccordionContent,{children:e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"space-y-4",children:[e.jsx(pe,{icon:e.jsx(Ss,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:a,value:(t==null?void 0:t.url)||"",onValueChange:g=>r({...t||{url:"",componentName:""},url:g})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&e.jsx(s.Tabs,{value:d,onValueChange:j,children:e.jsxs(s.TabsList,{className:"h-9 w-full",children:[e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&t&&e.jsx(e.Fragment,{children:i&&e.jsx(pe,{icon:e.jsx(nt,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(i==null?void 0:i.filter(g=>g.type===d).map(g=>({value:g.name,label:g.name})))||[],value:t.componentName,onValueChange:g=>{console.log("changing value",g),r({...t,componentName:g})}})})]})})]}),((p=l==null?void 0:l.customCardPreferences)==null?void 0:p.componentName)&&e.jsxs(s.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(s.AccordionTrigger,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{children:(u==null?void 0:u.name)||"Settings"}),(u==null?void 0:u.docs)&&e.jsxs(s.HoverCard,{openDelay:0,closeDelay:100,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,onClick:g=>g.stopPropagation(),children:e.jsx(s.Info,{className:"h-3.5 w-3.5 cursor-help text-muted-foreground transition-colors hover:text-foreground"})}),e.jsx(s.HoverCardContent,{side:"right",align:"start",className:"max-h-[40vh] w-[500px] overflow-y-auto text-left",children:e.jsxs("div",{className:"space-y-4 text-left",children:[u.docs.description&&e.jsx("p",{className:"text-sm text-muted-foreground",children:u.docs.description}),u.docs.dataSchema&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-semibold uppercase tracking-wide text-muted-foreground",children:"Data Schema"}),e.jsx("div",{className:"prose prose-sm dark:prose-invert prose-pre:bg-muted prose-pre:text-xs max-w-none",children:e.jsx(s.Markdown,{remarkPlugins:[s.remarkGfm],components:Wt,children:u.docs.dataSchema})})]}),u.docs.useCases&&u.docs.useCases.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-semibold uppercase tracking-wide text-muted-foreground",children:"Use Cases"}),e.jsx("ul",{className:"space-y-1 text-sm",children:u.docs.useCases.map((g,v)=>e.jsxs("li",{className:"flex items-start gap-2",children:[e.jsx("span",{className:"mt-1 text-primary",children:"•"}),e.jsx("span",{children:g})]},v))})]})]})})]})]})}),e.jsxs(s.AccordionContent,{children:[u&&u.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:x(u)})]}),u&&u.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:w(u)})})]})]})]})}function Xt({colorRanges:t,setColorRanges:n}){const a={start:-1/0,end:100,color:""};function l(r,i,o){const u=[...t];u[r]={...u[r],[i]:o},n(u)}function c(r){const i=[...t];i.splice(r,1),n(i)}function d(){n([...t,a])}return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{children:"Color Range"}),e.jsx(s.Button,{onClick:d,variant:"secondary",className:"h-5 p-1 text-muted-foreground",children:e.jsx(s.Plus,{className:"size-3"})})]}),t.map((r,i)=>e.jsxs("div",{className:"flex items-center gap-2 px-2",children:[e.jsx(s.Input$1,{onChange:o=>l(i,"start",o.target.value),value:r.start,className:s.cn(`h-7 ${s.removeRing}`)}),e.jsx("span",{children:"-"}),e.jsx(s.Input$1,{value:r.end,onChange:o=>l(i,"end",o.target.value),className:s.cn(`h-7 ${s.removeRing}`)}),e.jsx(s.Input$1,{onChange:o=>l(i,"color",o.target.value),value:r.color,className:s.cn(`h-7 min-w-14 p-0 ${s.removeRing}`),type:"color",list:"presetColors"}),e.jsxs("datalist",{id:"presetColors",children:[e.jsx("option",{children:s.colors.green[600]}),e.jsx("option",{children:s.colors.red[500]}),e.jsx("option",{children:s.colors.yellow[400]}),e.jsx("option",{children:s.colors.orange[500]})]}),e.jsx(s.Button,{onClick:()=>c(i),variant:"secondary",className:"h-5 p-1 text-muted-foreground",children:e.jsx(s.X,{className:"size-3"})})]},i))]})}function Zt(){var m,x,h,w,j;const t=s.useEditorStore(f=>f.card.preferences),n=s.useEditorStore(f=>f.actions.setCardPreferences),a=s.useEditorStore(f=>f.card),l=s.hasValidCardConfig(a.config),c=s.useEditorStore(f=>f.actions.setNumberFormat);s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.decimalPlaces});const d=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.locale}),r=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.currency});s.useEditorStore(f=>f.actions.setFilterOnClickField);const i=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.colorRanges})||[],o=s.useEditorStore(f=>f.actions.setColorRanges),u=[...new Set(s.LOCALE_CURRENCY_PAIRS.map(f=>f.currency))];return e.jsxs("div",{children:[!l&&e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsx(s.Checkbox,{checked:!!((m=t==null?void 0:t.formatNumber)!=null&&m.enabled),onCheckedChange:f=>{n({...t,formatNumber:{...t==null?void 0:t.formatNumber,enabled:f}})}}),e.jsx(s.Label,{children:"Format Number"})]}),(((x=t==null?void 0:t.formatNumber)==null?void 0:x.enabled)===!0||l)&&e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx("div",{children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Locale"}),e.jsxs(s.Select,{value:d||"none",onValueChange:f=>{var p,g,v;return c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,((g=t==null?void 0:t.formatNumber)==null?void 0:g.currency)||"",f,((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Format"}),e.jsx(s.SelectItem,{value:"none",children:"Auto"},"none"),s.LOCALE_CURRENCY_PAIRS.map(f=>e.jsx(s.SelectItem,{value:f.locale,children:f.locale},f.locale))]})})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Currency"}),e.jsxs(s.Select,{value:r||"none",onValueChange:f=>{var p,g,v;console.log("currency",f),c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,f==="none"?"":f,((g=t==null?void 0:t.formatNumber)==null?void 0:g.locale)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Currency"}),e.jsx(s.SelectItem,{value:"none",children:"None"},"none"),u.map(f=>e.jsx(s.SelectItem,{value:f,children:f},f))]})})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Decimals"}),e.jsxs(s.Select,{value:((w=(h=t==null?void 0:t.formatNumber)==null?void 0:h.decimalPlaces)==null?void 0:w.toString())||"0",onValueChange:f=>{var p,g,v;return c(Number(f),((p=t==null?void 0:t.formatNumber)==null?void 0:p.currency)||"",((g=t==null?void 0:t.formatNumber)==null?void 0:g.locale)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-3 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:["0","1","2","3","4"].map(f=>e.jsx(s.SelectItem,{value:f,children:f},f))})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Suffix"}),e.jsx(s.Input$1,{value:((j=t==null?void 0:t.formatNumber)==null?void 0:j.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:f=>{var p,g,v;return c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,((g=t==null?void 0:t.formatNumber)==null?void 0:g.currency)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.locale)||"",f.target.value)}})]})]})]})}),e.jsx(s.Separator,{className:"my-3"}),e.jsx(Xt,{colorRanges:i,setColorRanges:o})]})]})}const ea={combo:{label:"Combo Chart",query:"SELECT x-axis, metric-1, metric-2, ... FROM table",fields:[{name:"x-axis",type:"category",description:"dimension"},{name:"metric-1, metric-2",type:"number",description:"can be bar, line, or area"}],description:"Combo charts allow mixing bar and line series with optional dual Y-axis for metrics with different scales.",docsUrl:"https://docs.semaphor.cloud/docs/charts/combo"},area:{label:"Area Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, city"},{name:"Y-axis",type:"number",description:"e.g., sales, population"}],description:"Area charts display quantitative data with filled areas under the line.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},stackedArea:{label:"Stacked Area Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, city"},{name:"Y-axis",type:"number",description:"e.g., sales, population"}],description:"Stacked area charts show how multiple series contribute to a total over time.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},bar:{label:"Bar Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., city, product"},{name:"Y-axis",type:"number",description:"e.g., sales, count"}],description:"Bar charts compare values across categories using horizontal or vertical bars.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},line:{label:"Line Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, month"},{name:"Y-axis",type:"number",description:"e.g., revenue, count"}],description:"Line charts show trends over time or continuous data.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},stackedBar:{label:"Stacked Bar Chart",query:"SELECT x-axis, stack-by, y-axis FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., month"},{name:"Stack-by",type:"category",description:"e.g., product type"},{name:"Y-axis",type:"number",description:"e.g., sales"}],description:"Stacked bar charts show how parts contribute to a whole across categories.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},stackedLine:{label:"Stacked Line Chart",query:"SELECT x-axis, stack-by, y-axis FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date"},{name:"Stack-by",type:"category",description:"e.g., region"},{name:"Y-axis",type:"number",description:"e.g., revenue"}],description:"Stacked line charts show cumulative trends over time.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},pie:{label:"Pie Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., product, region"},{name:"Measure",type:"number",description:"e.g., sales, count"}],description:"Pie charts show proportions of a whole.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},doughnut:{label:"Doughnut Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., product, region"},{name:"Measure",type:"number",description:"e.g., sales, count"}],description:"Doughnut charts show proportions with a hollow center.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},polarArea:{label:"Polar Area Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., skill, metric"},{name:"Measure",type:"number",description:"e.g., score, value"}],description:"Polar area charts compare values using sectors of equal angle but varying radius.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},radar:{label:"Radar Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., attribute, skill"},{name:"Measure",type:"number",description:"e.g., score, rating"}],description:"Radar charts display multivariate data on axes starting from the same point.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},funnel:{label:"Funnel Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., stage name"},{name:"Measure",type:"number",description:"e.g., count, value"}],description:"Funnel charts visualize progressive reduction of data through stages.",docsUrl:"https://docs.semaphor.cloud/docs/charts/funnel"},bubble:{label:"Bubble Chart",query:"SELECT label, [group], x-axis, y-axis, radius FROM table",fields:[{name:"Label",type:"category",description:"e.g., city"},{name:"Group",type:"category",description:"e.g., region",optional:!0},{name:"X-axis",type:"number",description:"e.g., population"},{name:"Y-axis",type:"number",description:"e.g., income"},{name:"Radius",type:"number",description:"e.g., area"}],description:"Bubble charts display three dimensions of data using position and size.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bubble"},scatter:{label:"Scatter Chart",query:"SELECT label, [group], x-axis, y-axis FROM table",fields:[{name:"Label",type:"category",description:"e.g., item name"},{name:"Group",type:"category",description:"e.g., type",optional:!0},{name:"X-axis",type:"number",description:"e.g., price"},{name:"Y-axis",type:"number",description:"e.g., quantity"}],description:"Scatter charts show relationships between two numeric variables.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bubble"},tornado:{label:"Tornado Chart",query:"SELECT dimension, cohort, metric FROM table",fields:[{name:"Dimension",type:"category",description:"e.g., age group"},{name:"Cohort",type:"category",description:"e.g., gender"},{name:"Metric",type:"number",description:"e.g., population"}],description:"Tornado charts compare two groups across categories using back-to-back bars.",docsUrl:"https://docs.semaphor.cloud/docs/charts/tornado"},pyramid:{label:"Pyramid Chart",query:"SELECT dimension, cohort, metric FROM table",fields:[{name:"Dimension",type:"category",description:"e.g., age group"},{name:"Cohort",type:"category",description:"e.g., gender"},{name:"Metric",type:"number",description:"e.g., population"}],description:"Population pyramids show age and gender distribution.",docsUrl:"https://docs.semaphor.cloud/docs/charts/tornado"},range:{label:"Range Chart",query:"SELECT y-axis, label, range-from, range-to, range-value FROM table",fields:[{name:"Y-axis",type:"category",description:"e.g., category"},{name:"Label",type:"category",description:"e.g., range name"},{name:"Range-from",type:"number",description:"start value"},{name:"Range-to",type:"number",description:"end value"},{name:"Range-value",type:"number",description:"e.g., midpoint"}],description:"Range charts display data ranges with start and end values.",docsUrl:"https://docs.semaphor.cloud/docs/charts/range"},heatmap:{label:"Heatmap",query:"SELECT x-axis, y-axis, value FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., day"},{name:"Y-axis",type:"category",description:"e.g., hour"},{name:"Value",type:"number",description:"e.g., count, intensity"}],description:"Heatmaps visualize data density using color intensity.",docsUrl:"https://docs.semaphor.cloud/docs/charts/heatmap"},bullet:{label:"Bullet Chart",query:"SELECT label, actual, target, [ranges] FROM table",fields:[{name:"Label",type:"category",description:"e.g., metric name"},{name:"Actual",type:"number",description:"current value"},{name:"Target",type:"number",description:"goal value"},{name:"Ranges",type:"number",description:"threshold values",optional:!0}],description:"Bullet charts compare actual performance against targets.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bullet"},text:{label:"Text Visual",query:"SELECT text FROM table LIMIT 1",fields:[{name:"Text",type:"string",description:"content to display"}],description:"Text visuals display dynamic text content from your data.",docsUrl:"https://docs.semaphor.cloud/docs/charts/text"},kpi:{label:"KPI",query:"SELECT current, previous FROM table",fields:[{name:"Current",type:"number",description:"current value"},{name:"Previous",type:"number",description:"comparison value"}],description:"KPI visuals display key performance indicators with comparison values.",docsUrl:"https://docs.semaphor.cloud/docs/charts/kpi"},map:{label:"Map Chart",query:"SELECT country, value FROM table",fields:[{name:"Country/Region",type:"category",description:"geographic identifier"},{name:"Value",type:"number",description:"e.g., population, sales"}],description:"Map charts visualize geographic data using color intensity.",docsUrl:"https://docs.semaphor.cloud/docs/charts/map"},table:{label:"Table",query:"SELECT column1, column2, ... FROM table",fields:[{name:"Columns",type:"category",description:"any data columns"}],description:"Tables display data in rows and columns.",docsUrl:"https://docs.semaphor.cloud/docs/charts/table"}};function Qe(t){return ea[t]}function sa({chartType:t,showDocsLink:n=!0}){const a=Qe(t);return a?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-medium uppercase tracking-wide text-muted-foreground",children:"Query Format"}),e.jsx("code",{className:"block rounded-md bg-muted/50 px-3 py-2 font-mono text-xs text-foreground",children:a.query})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-medium uppercase tracking-wide text-muted-foreground",children:"Fields"}),e.jsx("div",{className:"rounded-md border",children:e.jsxs("table",{className:"w-full text-xs",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/50",children:[e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Field"}),e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Type"}),e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Description"})]})}),e.jsx("tbody",{children:a.fields.map((l,c)=>e.jsxs("tr",{className:c<a.fields.length-1?"border-b":"",children:[e.jsxs("td",{className:"px-2.5 py-1.5 font-medium text-foreground",children:[l.name,l.optional&&e.jsx("span",{className:"ml-1 text-muted-foreground",children:"?"})]}),e.jsx("td",{className:"px-2.5 py-1.5 font-mono text-muted-foreground",children:l.type}),e.jsx("td",{className:"px-2.5 py-1.5 text-muted-foreground",children:l.description||"—"})]},l.name))})]})})]}),e.jsx("p",{className:"text-xs leading-relaxed text-muted-foreground",children:a.description}),n&&a.docsUrl&&e.jsxs("a",{href:a.docsUrl,target:"_blank",rel:"noopener noreferrer",onClick:l=>l.stopPropagation(),className:"inline-flex items-center gap-1.5 text-xs font-medium text-primary hover:underline",children:["View documentation",e.jsx(s.ExternalLink,{className:"h-3 w-3"})]})]}):null}function ta({chartType:t}){return Qe(t)?e.jsxs(s.HoverCard,{openDelay:200,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsx("button",{type:"button",className:"rounded bg-muted px-2 py-0.5 font-mono text-[10px] font-semibold uppercase tracking-wider text-muted-foreground transition-colors hover:bg-muted/80 hover:text-foreground",children:"SQL"})}),e.jsx(s.HoverCardContent,{className:"w-80 p-4",side:"left",align:"start",sideOffset:8,children:e.jsx(sa,{chartType:t})})]}):null}function Re({chartType:t,suffix:n="Configuration"}){const a=Qe(t),l=(a==null?void 0:a.label)||t;return e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h3",{className:"text-sm font-medium",children:[l," ",n]}),e.jsx(ta,{chartType:t})]})}function aa(){return e.jsxs("div",{className:"space-y-6 px-6 py-4",children:[e.jsx(Re,{chartType:"kpi"}),e.jsx("section",{children:e.jsx(Zt,{})})]})}function ms({children:t,summary:n,open:a,className:l}){return e.jsxs("details",{open:a,className:s.cn("space-y-4 [&_summary>svg]:open:-rotate-180",l),children:[e.jsxs("summary",{className:"flex cursor-pointer list-none items-center justify-between gap-4",children:[e.jsx(s.Label,{children:n}),e.jsx(s.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:t})]})}function zs(){const[t,n]=L.useState([]),a=s.useEditorStore(u=>u.card.preferences),{setFilterOnClickColumnIndex:l,setCardPreferences:c}=s.useEditorStore(u=>u.actions),d=s.useEditorStore(u=>u.queryResultColumns);s.useEditorStore(u=>{var m;return(m=u.card.preferences)==null?void 0:m.filterOnClickColumnIndex}),L.useEffect(()=>{d&&n(new Array(d.length).fill(!1))},[d]);function r(u,m){var x,h;m?a!=null&&a.onClickFilter&&((x=a==null?void 0:a.onClickFilter)==null?void 0:x.length)>0?c({...a,onClickFilter:[...a.onClickFilter,{columnIndex:u,expression:""}]}):c({...a,onClickFilter:[{columnIndex:u,expression:""}]}):(c({...a,onClickFilter:(h=a==null?void 0:a.onClickFilter)==null?void 0:h.filter(w=>w.columnIndex!==u)}),n(w=>{const j=[...w];return j[u]=!1,j}))}function i(u,m){var h;const x=(h=a==null?void 0:a.onClickFilter)==null?void 0:h.map(w=>w.columnIndex===u?{...w,expression:m.target.value}:w);c({...a,onClickFilter:x})}function o(u){var m,x,h,w;if(t!=null&&t[u]||(x=(m=a==null?void 0:a.onClickFilter)==null?void 0:m.find(j=>j.columnIndex===u))!=null&&x.expression)return e.jsx(s.Input$1,{value:(w=(h=a==null?void 0:a.onClickFilter)==null?void 0:h.find(j=>j.columnIndex===u))==null?void 0:w.expression,onChange:j=>i(u,j),type:"text",className:"h-9 w-full font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"Expression"})}return e.jsx("div",{className:"max-h-[150px] space-y-2 overflow-y-auto",children:d==null?void 0:d.map((u,m)=>{var x,h;return e.jsx("div",{className:"group flex items-center justify-between space-y-2",children:e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex items-center space-x-2 py-2",children:[e.jsx(s.Checkbox,{checked:!!((x=a==null?void 0:a.onClickFilter)!=null&&x.find(w=>w.columnIndex===m)),onCheckedChange:w=>r(m,w)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:u}),e.jsx("div",{className:"flex items-center gap-2",children:((h=a==null?void 0:a.onClickFilter)==null?void 0:h.find(w=>w.columnIndex===m))&&e.jsx(s.Pencil,{onClick:()=>{n(w=>{const j=[...w];return j[m]=!w[m],j})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),o(m)]})},u)})})}const na=[{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"}],ra=["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"],la=ra.map(t=>({label:t,value:t}));function oa(){var o,u,m,x,h,w,j,f,p,g,v,O,A,y,T,N,C,k;const t=s.useEditorStore(R=>R.card),n=(u=(o=t==null?void 0:t.preferences)==null?void 0:o.mapVisualOptions)==null?void 0:u.topoJsonUrl,[a,l]=L.useState(((x=(m=t==null?void 0:t.preferences)==null?void 0:m.mapVisualOptions)==null?void 0:x.customTopoJsonUrl)||""),{setCardPreferences:c}=s.useEditorActions(),{data:d}=s.useTopoJson(t),r=Object.keys((d==null?void 0:d.objects)||{}).map(R=>({label:R,value:R}))||[],i=[{label:"US States 10m",value:"https://unpkg.com/us-atlas/states-10m.json"},{label:"Countries 50m",value:"https://unpkg.com/world-atlas/countries-50m.json"},{label:"Custom",value:"custom"}];return e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Re,{chartType:"map"}),e.jsx(ms,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(pe,{className:"h-9",options:i,onValueChange:R=>{console.log("value",R),c({...t.preferences,mapVisualOptions:{topoJsonUrl:R}})},value:n||"",label:"Select Map"}),n==="custom"&&e.jsx(s.Input$1,{onChange:R=>{l(R.target.value)},onBlur:()=>{var R;console.log("onBlur",a),c({...t.preferences,mapVisualOptions:{...(R=t.preferences)==null?void 0:R.mapVisualOptions,customTopoJsonUrl:a}})},value:a,className:`h-9 w-full ${s.removeRing}`,placeholder:"Custom URL"}),d&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Map Object"}),e.jsx(pe,{className:"h-9",options:r,value:((w=(h=t.preferences)==null?void 0:h.mapVisualOptions)==null?void 0:w.objectKey)||"",label:"Map Object",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,objectKey:R}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Projection"}),e.jsx(pe,{className:"h-9",options:na,value:((f=(j=t.preferences)==null?void 0:j.mapVisualOptions)==null?void 0:f.projection)||"",label:"Projection",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projection:R}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Colors"}),e.jsx(pe,{className:"h-9",options:la,value:((g=(p=t.preferences)==null?void 0:p.mapVisualOptions)==null?void 0:g.colorScale)||"",label:"Select Color Scale",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,colorScale:R}})}})]})]})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ms,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:.1,value:((O=(v=t.preferences)==null?void 0:v.mapVisualOptions)==null?void 0:O.projectionScale)||1,onChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionScale:Number(R.target.value)}})}}),e.jsx(s.Label,{children:"X"}),e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((T=(y=(A=t.preferences)==null?void 0:A.mapVisualOptions)==null?void 0:y.projectionOffset)==null?void 0:T[0])||0,onChange:R=>{var b,S,D,F;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionOffset:[Number(R.target.value),((F=(D=(S=t.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:F[1])||0]}})}}),e.jsx(s.Label,{children:"Y"}),e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((k=(C=(N=t.preferences)==null?void 0:N.mapVisualOptions)==null?void 0:C.projectionOffset)==null?void 0:k[1])||0,onChange:R=>{var b,S,D,F;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionOffset:[((F=(D=(S=t.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:F[0])||0,Number(R.target.value)]}})}})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(zs,{})]})}function ia(){const t=s.useDashboardStore(i=>i.dashboard.filters),n=s.useEditorStore(i=>i.frame),{setFrame:a}=s.useEditorActions(),{columns:l}=s.useEditorAside(),{assignFilter:c}=s.useAssignFilter();function d(i){const o=c(i,"frame");a({...n,filterId:o})}const r=t==null?void 0:t.find(i=>i.id===n.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Card Filter"}),e.jsx(ca,{onSelect:d,columns:l}),r&&e.jsx(s.FilterHeader,{filter:r})]})}function ca({columns:t,onSelect:n}){var r;const[a,l]=Ue.useState(!1),[c,d]=Ue.useState("");return e.jsxs(s.Popover,{open:a,onOpenChange:l,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":a,className:"w-full justify-between font-normal",children:[c?(r=t.find(i=>i.column_name===c))==null?void 0:r.column_name:"Select Column...",e.jsx(s.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No framework found."}),e.jsx(s.CommandGroup,{children:t==null?void 0:t.map(i=>e.jsxs(s.CommandItem,{value:i.column_name,onSelect:o=>{const u=o===c?"":o;d(u),l(!1),u&&n(i)},children:[i.column_name,e.jsx(s.CheckIcon,{className:s.cn("ml-auto h-4 w-4",c===i.column_name?"opacity-100":"opacity-0")})]},i.column_name))})]})]})})]})}function da({cardType:t="table"}){console.log("cardType",t);const n=s.useEditorStore(c=>{var d;return(d=c.card)==null?void 0:d.preferences}),a=s.useEditorStore(c=>c.actions.setCardPreferences),l=c=>{a({...n,allowDownload:c})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Click Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(zs,{}),e.jsx(ia,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Checkbox,{onCheckedChange:l,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"})]})]})}function ua(){var l;const t=s.useEditorStore(c=>c.card.preferences),{setCardPreferences:n}=s.useEditorStore(c=>c.actions);function a(c){n({...t,textVisualOptions:{...t==null?void 0:t.textVisualOptions,isDynamicText:c==="dynamic"}})}return e.jsxs(s.RadioGroup,{value:(l=t==null?void 0:t.textVisualOptions)!=null&&l.isDynamicText?"dynamic":"static",onValueChange:a,className:"flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"static",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Static"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function ma(){return e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Re,{chartType:"text"}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Text Type"}),e.jsx(ua,{})]})]})}function $s({color:t,onColorChange:n,onClose:a}){const[l,c]=L.useState(0),[d,r]=L.useState(100),[i,o]=L.useState(50),[u,m]=L.useState(t);L.useEffect(()=>{const p=xs(t);if(p){const g=hs(p.r,p.g,p.b);c(g.h),r(g.s),o(g.l),m(t)}},[t]);const x=p=>{c(p),w(p,d,i)},h=p=>{const g=p.currentTarget.getBoundingClientRect(),v=Math.max(0,Math.min(1,(p.clientX-g.left)/g.width)),O=Math.max(0,Math.min(1,(p.clientY-g.top)/g.height)),A=v*100,y=(1-O)*100;r(A),o(y),w(l,A,y)},w=(p,g,v)=>{const O=ha(p,g,v),A=xa(O.r,O.g,O.b);m(A)},j=p=>{if(m(p),/^#[0-9A-Fa-f]{6}$/.test(p)){const g=xs(p);if(g){const v=hs(g.r,g.g,g.b);c(v.h),r(v.s),o(v.l)}}},f=()=>{/^#[0-9A-Fa-f]{6}$/.test(u)&&(n(u),a())};return e.jsxs("div",{className:"p-4 space-y-4 w-[320px]",children:[e.jsxs("div",{className:"relative h-[200px] rounded cursor-crosshair",style:{background:`hsl(${l}, 100%, 50%)`},onClick:h,onMouseMove:p=>{p.buttons===1&&h(p)},children:[e.jsx("div",{className:"absolute inset-0 rounded",style:{background:"linear-gradient(to right, white, transparent)"}}),e.jsx("div",{className:"absolute inset-0 rounded",style:{background:"linear-gradient(to bottom, transparent, black)"}}),e.jsx("div",{className:"absolute w-4 h-4 -ml-2 -mt-2 rounded-full border-2 border-white shadow-md",style:{left:`${d}%`,top:`${100-i}%`,backgroundColor:u}})]}),e.jsx("div",{className:"space-y-2",children:e.jsx("input",{type:"range",min:"0",max:"360",value:l,onChange:p=>x(Number(p.target.value)),className:"w-full h-4 rounded-lg appearance-none cursor-pointer",style:{background:"linear-gradient(to right, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000)"}})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"HEX"}),e.jsx(s.Input$1,{value:u,onChange:p=>j(p.target.value),placeholder:"#000000",className:"font-mono"})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"w-12 h-12 rounded-full border shadow-sm",style:{backgroundColor:t}}),e.jsx("span",{className:"text-xs text-muted-foreground mt-1",children:"CURRENT"})]}),e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"w-12 h-12 rounded-full border shadow-sm",style:{backgroundColor:u}}),e.jsx("span",{className:"text-xs text-muted-foreground mt-1",children:"NEW"})]})]}),e.jsxs("div",{className:"flex gap-2 justify-end",children:[e.jsx(s.Button,{variant:"outline",onClick:a,children:"Cancel"}),e.jsx(s.Button,{onClick:f,children:"OK"})]})]})}function xs(t){const n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16)}:null}function xa(t,n,a){return"#"+((1<<24)+(t<<16)+(n<<8)+a).toString(16).slice(1)}function hs(t,n,a){t/=255,n/=255,a/=255;const l=Math.max(t,n,a),c=Math.min(t,n,a);let d=0,r=0;const i=(l+c)/2;if(l!==c){const o=l-c;switch(r=i>.5?o/(2-l-c):o/(l+c),l){case t:d=((n-a)/o+(n<a?6:0))/6;break;case n:d=((a-t)/o+2)/6;break;case a:d=((t-n)/o+4)/6;break}}return{h:Math.round(d*360),s:Math.round(r*100),l:Math.round(i*100)}}function ha(t,n,a){t/=360,n/=100,a/=100;let l,c,d;if(n===0)l=c=d=a;else{const r=(u,m,x)=>(x<0&&(x+=1),x>1&&(x-=1),x<.16666666666666666?u+(m-u)*6*x:x<.5?m:x<.6666666666666666?u+(m-u)*(.6666666666666666-x)*6:u),i=a<.5?a*(1+n):a+n-a*n,o=2*a-i;l=r(o,i,t+1/3),c=r(o,i,t),d=r(o,i,t-1/3)}return{r:Math.round(l*255),g:Math.round(c*255),b:Math.round(d*255)}}const pa=["#000000","#ffffff","#6b7280","#dc2626","#ea580c","#ca8a04","#16a34a","#0891b2","#2563eb","#7c3aed","#c026d3","#e11d48","#0f172a","#f3f4f6","#fbbf24"];function ga(){var O,A,y,T,N,C,k,R,b,S;const t=s.useEditorStore(D=>D.card),n=s.useEditorStore(D=>{var F;return(F=D.card)==null?void 0:F.preferences}),{setCardPreferences:a,updateCardInFrame:l}=s.useEditorActions(),{data:c}=s.useDashboardCardQuery(t),[d,r]=L.useState({}),[i,o]=L.useState(null),[u,m]=L.useState(null),x=L.useMemo(()=>{var M,E,B,$,V,q,G;if(!(c!=null&&c.records)||c.records.length===0)return[];const D=(t==null?void 0:t.type)==="stackedBar"||(t==null?void 0:t.type)==="stackedLine",F=(t==null?void 0:t.type)==="bar"||(t==null?void 0:t.type)==="line"||(t==null?void 0:t.type)==="horizontalBar"||(t==null?void 0:t.type)==="bullet"||(t==null?void 0:t.type)==="combo"||(t==null?void 0:t.type)==="area"||(t==null?void 0:t.type)==="stackedArea",I=((M=t==null?void 0:t.config)==null?void 0:M.pivotByColumns)&&t.config.pivotByColumns.length>0;if((D||F)&&I){if(c.pivotSchema&&Array.isArray(c.pivotSchema)){const z=new Set;return c.pivotSchema.forEach(H=>{H.alias&&z.add(H.alias)}),Array.from(z)}if(c.records.length>0){const z=c.records[0],H=Object.keys(z),_=new Set;(E=t==null?void 0:t.config)!=null&&E.groupByColumns&&t.config.groupByColumns.forEach(Z=>{_.add(Z.label||Z.name),_.add(Z.alias||Z.name)});const P=new Set;return(B=t==null?void 0:t.config)!=null&&B.metricColumns&&t.config.metricColumns.forEach(Z=>{const se=Z.alias||Z.name;P.add(se),P.add(`${se}_count`),P.add(`${se}_sum`),P.add(`${se}_avg`),P.add(`${se}_min`),P.add(`${se}_max`);const W=Z.entityName||Z.qualifiedEntityName;W&&(P.add(`${W}_${se}`),P.add(`${W}_${se}_count`))}),H.filter(Z=>!_.has(Z)&&!P.has(Z))}}if((F||D)&&!I&&((V=($=t==null?void 0:t.config)==null?void 0:$.metricColumns)!=null&&V.length))return t.config.metricColumns.map(z=>z.alias||z.label||z.name);if((G=(q=t==null?void 0:t.config)==null?void 0:q.groupByColumns)!=null&&G.length){const z=t.config.groupByColumns[0],H=z.label||z.name,_=new Set;return c.records.forEach(P=>{const Y=P[H];Y!=null&&_.add(String(Y))}),Array.from(_)}return[]},[c,(O=t==null?void 0:t.config)==null?void 0:O.groupByColumns,(A=t==null?void 0:t.config)==null?void 0:A.pivotByColumns,(y=t==null?void 0:t.config)==null?void 0:y.metricColumns,t==null?void 0:t.type]),h=L.useMemo(()=>s.getDefaultChartColors(),[]);L.useEffect(()=>{const D={};x.forEach((F,I)=>{D[F]=s.getColorForValue(F,I,n==null?void 0:n.colorConfig)}),r(D)},[x,n==null?void 0:n.colorConfig,h]);const w=(D,F)=>{const I={...d,[D]:F};r(I);const M={...n||{},colorConfig:{segments:I}};a(M);const E={...t,preferences:M};l(E)},j=(D,F)=>{var V;const I=h[F%h.length],{[D]:M,...E}=((V=n==null?void 0:n.colorConfig)==null?void 0:V.segments)||{},B={...n||{},colorConfig:{segments:E}};a(B);const $={...t,preferences:B};l($),r(q=>({...q,[D]:I}))};if(!x.length||!((t==null?void 0:t.type)==="stackedBar"||(t==null?void 0:t.type)==="stackedLine"?((N=(T=t==null?void 0:t.config)==null?void 0:T.groupByColumns)==null?void 0:N.length)||((k=(C=t==null?void 0:t.config)==null?void 0:C.pivotByColumns)==null?void 0:k.length):(b=(R=t==null?void 0:t.config)==null?void 0:R.groupByColumns)==null?void 0:b.length)||["table","kpi","text","custom","heatmap"].includes(t.type))return null;const g=()=>{const D={...n||{},colorConfig:{segments:{}}};a(D);const F={...t,preferences:D};l(F);const I={};x.forEach((M,E)=>{I[M]=s.getColorForValue(M,E,void 0)}),r(I)},v=Object.keys(((S=n==null?void 0:n.colorConfig)==null?void 0:S.segments)||{}).length>0;return e.jsxs(s.AccordionItem,{value:"colors",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ft,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Colors"})]})}),e.jsx(s.AccordionContent,{className:"",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"space-y-2",children:e.jsx("div",{className:"flex items-center justify-between",children:v&&e.jsxs("button",{className:"ml-auto flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground",onClick:g,children:[e.jsx(ls,{className:"h-3 w-3"}),"Reset All"]})})}),e.jsx("div",{className:"max-h-[200px] space-y-2 overflow-y-auto pr-1",children:x.map((D,F)=>{const I=s.isDefaultColor(d[D],F),M=!I;return e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Popover,{open:i===D,onOpenChange:E=>o(E?D:null),children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx("button",{className:"h-5 w-5 shrink-0 rounded border border-input shadow-sm transition-colors hover:border-gray-400 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",style:{backgroundColor:d[D]}})}),e.jsx(s.PopoverContent,{align:"start",className:"w-auto p-3",children:e.jsxs("div",{className:"space-y-2.5",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Theme Colors"}),e.jsx("div",{className:"grid grid-cols-5 gap-1.5",children:h.slice(0,10).map((E,B)=>e.jsxs("button",{className:s.cn("relative h-5 w-5 rounded border-2 transition-all hover:scale-110",d[D]===E?"border-gray-900":"border-transparent"),style:{backgroundColor:E},onClick:()=>{w(D,E),o(null)},children:[B===F%h.length&&e.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:e.jsx("div",{className:"rounded-full bg-white/90 px-0.5 text-[8px] font-bold text-gray-900 shadow-sm",children:"D"})}),e.jsx("span",{className:"sr-only",children:E})]},`default-${B}`))})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Popular Colors"}),e.jsx("div",{className:"grid grid-cols-5 gap-1.5",children:pa.map((E,B)=>e.jsx("button",{className:s.cn("h-5 w-5 rounded border transition-all hover:scale-110",d[D]===E?"border-2 border-gray-900":E==="#ffffff"?"border border-gray-300":"border border-transparent"),style:{backgroundColor:E},onClick:()=>{w(D,E),o(null)},children:e.jsx("span",{className:"sr-only",children:E})},`preset-${B}`))})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 pt-1",children:[e.jsx("button",{className:"text-sm font-medium text-blue-600 hover:text-blue-700",onClick:()=>{m({value:D,color:d[D]}),o(null)},children:"Custom color"}),!I&&e.jsxs("button",{className:"flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground",onClick:()=>{j(D,F),o(null)},children:[e.jsx(ls,{className:"h-3 w-3"}),"Reset"]})]})]})})]}),e.jsx("span",{className:"flex-1 truncate text-sm",children:D}),M&&e.jsx("span",{className:"text-xs text-muted-foreground",children:"(custom)"})]},D)})}),u&&e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[e.jsx("div",{className:"absolute inset-0 bg-black/50",onClick:()=>m(null)}),e.jsx("div",{className:"relative rounded-lg bg-white shadow-lg",children:e.jsx($s,{color:u.color,onColorChange:D=>{w(u.value,D),m(null)},onClose:()=>m(null)})})]})]})})]})}function ie({value:t,onSave:n,placeholder:a="",type:l="text",className:c=""}){const[d,r]=L.useState(t??""),[i,o]=L.useState(!1);L.useEffect(()=>{r(t??"")},[t]);const u=()=>{n(d),o(!1)},m=()=>{r(t??""),o(!1)};return e.jsxs("div",{className:`flex items-center gap-2 ${c}`,children:[e.jsx(s.Input,{placeholder:a,type:l,value:i?d:t??"",onChange:x=>{r(x.target.value),o(!0)},onFocus:()=>o(!0),onClick:x=>x.stopPropagation(),className:"h-8 text-xs",onPointerDown:x=>x.stopPropagation(),onKeyDown:x=>{x.stopPropagation(),x.key==="Enter"&&i&&d!==t?u():x.key==="Escape"&&m()},"aria-label":a||"Edit field"}),i&&d!==t&&e.jsxs(e.Fragment,{children:[e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm edit",onClick:u,children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel edit",onClick:m,children:e.jsx(s.X,{className:"h-4 w-4 text-muted-foreground"})})]})]})}const fa=["auto","number","currency","percent","scientific","date"],ja=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"GBP",label:"GBP - British Pound"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"CHF",label:"CHF - Swiss Franc"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"},{value:"HKD",label:"HKD - Hong Kong Dollar"},{value:"SGD",label:"SGD - Singapore Dollar"},{value:"NZD",label:"NZD - New Zealand Dollar"},{value:"SEK",label:"SEK - Swedish Krona"},{value:"NOK",label:"NOK - Norwegian Krone"},{value:"DKK",label:"DKK - Danish Krone"},{value:"KRW",label:"KRW - South Korean Won"},{value:"INR",label:"INR - Indian Rupee"},{value:"RUB",label:"RUB - Russian Ruble"},{value:"BRL",label:"BRL - Brazilian Real"},{value:"MXN",label:"MXN - Mexican Peso"},{value:"TRY",label:"TRY - Turkish Lira"},{value:"ZAR",label:"ZAR - South African Rand"},{value:"SAR",label:"SAR - Saudi Riyal"},{value:"AED",label:"AED - UAE Dirham"},{value:"ARS",label:"ARS - Argentine Peso"},{value:"CLP",label:"CLP - Chilean Peso"},{value:"COP",label:"COP - Colombian Peso"},{value:"THB",label:"THB - Thai Baht"},{value:"IDR",label:"IDR - Indonesian Rupiah"},{value:"MYR",label:"MYR - Malaysian Ringgit"},{value:"PHP",label:"PHP - Philippine Peso"},{value:"PLN",label:"PLN - Polish Zloty"},{value:"NGN",label:"NGN - Nigerian Naira"},{value:"KES",label:"KES - Kenyan Shilling"},{value:"GHS",label:"GHS - Ghanaian Cedi"},{value:"ZMW",label:"ZMW - Zambian Kwacha"},{value:"ZWL",label:"ZWL - Zimbabwean Dollar"},{value:"EGP",label:"EGP - Egyptian Pound"},{value:"MAD",label:"MAD - Moroccan Dirham"},{value:"UAH",label:"UAH - Ukrainian Hryvnia"},{value:"HUF",label:"HUF - Hungarian Forint"},{value:"CZK",label:"CZK - Czech Koruna"},{value:"RON",label:"RON - Romanian Leu"},{value:"PKR",label:"PKR - Pakistani Rupee"},{value:"BDT",label:"BDT - Bangladeshi Taka"},{value:"VND",label:"VND - Vietnamese Dong"},{value:"TWD",label:"TWD - New Taiwan Dollar"},{value:"ILS",label:"ILS - Israeli New Shekel"}],ba=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"en-IN",label:"English (India)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"},{value:"ja-JP",label:"Japanese"},{value:"ko-KR",label:"Korean"},{value:"ru-RU",label:"Russian"},{value:"ar-SA",label:"Arabic"},{value:"hi-IN",label:"Hindi"},{value:"nl-NL",label:"Dutch"},{value:"sv-SE",label:"Swedish"}],va=t=>t.charAt(0).toUpperCase()+t.slice(1);function We({formatOptions:t={},onFormatOptionsChange:n,showTitle:a=!0,title:l="NUMBER FORMAT"}){const c=t.type||"auto",d=(i,o)=>{n({...t,[i]:o})},r=()=>c==="currency"?2:c==="percent"?1:0;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[a&&e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:l}),e.jsxs(s.Select,{value:c,onValueChange:i=>d("type",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:fa.map(i=>e.jsx(s.SelectItem,{value:i,className:"text-xs",children:i==="auto"?"Auto (Default)":va(i)},i))})]})]}),c!=="auto"&&e.jsxs("div",{className:"space-y-2 border-l-2 border-muted pl-4",children:[["number","currency","percent"].includes(c)&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Decimals"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"number",min:"0",max:"10",placeholder:"0",value:t.decimalPlaces??r(),onChange:i=>d("decimalPlaces",parseInt(i.target.value)||0),className:"h-8 text-xs"})})]}),c==="currency"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Currency"}),e.jsxs(s.Select,{value:t.currency||"USD",onValueChange:i=>d("currency",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 flex-1 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:ja.map(i=>e.jsx(s.SelectItem,{value:i.value,className:"text-xs",children:i.label},i.value))})]})]}),["number","currency"].includes(c)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:t.useSuffix??!1,onCheckedChange:i=>d("useSuffix",i)})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Locale"}),e.jsxs(s.Select,{value:t.locale||"en-US",onValueChange:i=>d("locale",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 flex-1 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:ba.map(i=>e.jsx(s.SelectItem,{value:i.value,className:"text-xs",children:i.label},i.value))})]})]}),c==="number"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Prefix"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{placeholder:"e.g., $",value:t.prefix||"",onChange:i=>d("prefix",i.target.value),className:"h-8 text-xs"})})]}),c==="number"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Suffix"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{placeholder:"e.g., units",value:t.suffix||"",onChange:i=>d("suffix",i.target.value),className:"h-8 text-xs"})})]}),["number","currency"].includes(c)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Negative in ()"}),e.jsx(s.SmallSwitch,{checked:t.negativeInParentheses??!1,onCheckedChange:i=>d("negativeInParentheses",i)})]}),c==="percent"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Multiply By"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"number",placeholder:"100",value:t.multiplyBy??"",onChange:i=>d("multiplyBy",i.target.value?parseFloat(i.target.value):void 0),className:"h-8 text-xs"})})]}),c==="date"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Format"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"text",placeholder:"MM/DD/YYYY",value:t.dateFormat||"",onChange:i=>d("dateFormat",i.target.value),className:"h-8 text-xs"})})]})]})]})}function $e({axis:t,label:n,type:a,config:l,onChange:c,defaultTitle:d,hidePosition:r}){var w,j,f,p,g,v,O,A,y,T,N,C,k,R;const i=(b,S)=>{c({...l??{},[b]:S})},o=(b,S)=>{var M,E,B,$;const D=b==="enabled"&&S===!0,F=(M=l==null?void 0:l.name)==null?void 0:M.text,I=D&&!F&&d;c({...l??{},name:{...l==null?void 0:l.name,enabled:b==="enabled"?S:((E=l==null?void 0:l.name)==null?void 0:E.enabled)??!1,text:b==="text"?S:I?d:F,fontSize:b==="fontSize"?S:(B=l==null?void 0:l.name)==null?void 0:B.fontSize,fontWeight:b==="fontWeight"?S:($=l==null?void 0:l.name)==null?void 0:$.fontWeight}})},u=(b,S)=>{var D,F;c({...l??{},labels:{enabled:b==="enabled"?S:((D=l==null?void 0:l.labels)==null?void 0:D.enabled)??!0,rotation:b==="rotation"?S:(F=l==null?void 0:l.labels)==null?void 0:F.rotation}})},m=(b,S)=>{c({...l??{},scale:{...l==null?void 0:l.scale,[b]:S===""?"auto":Number(S)}})},x=t==="x"?["auto","top","bottom"]:["auto","left","right"],h=["auto","0","45","90","-45","-90"];return e.jsxs(s.AccordionItem,{value:`${t}-axis`,className:"border-b-0",children:[e.jsxs("div",{className:"flex w-full items-center justify-between gap-2",children:[e.jsx(s.AccordionTrigger,{className:"flex grow items-center justify-between gap-3 py-2 text-sm font-medium hover:no-underline",children:e.jsx("div",{className:"flex grow items-center justify-between gap-2",children:e.jsx("span",{children:n})})}),e.jsx(s.SmallSwitch,{checked:(l==null?void 0:l.enabled)!==!1,onCheckedChange:b=>i("enabled",b),onClick:b=>b.stopPropagation()})]}),e.jsxs(s.AccordionContent,{className:"space-y-4 pt-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:`${t}-title-enabled`,checked:((w=l==null?void 0:l.name)==null?void 0:w.enabled)??!1,onCheckedChange:b=>o("enabled",b===!0)}),e.jsx(s.Label,{htmlFor:`${t}-title-enabled`,className:"text-xs font-medium cursor-pointer",children:"TITLE"})]}),((j=l==null?void 0:l.name)==null?void 0:j.enabled)&&e.jsxs("div",{className:"space-y-2 pl-6",children:[e.jsx(ie,{placeholder:`${n} title`,value:((f=l==null?void 0:l.name)==null?void 0:f.text)||"",onSave:b=>o("text",b),className:""}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Size"}),e.jsx("div",{className:"w-14",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((p=l==null?void 0:l.name)==null?void 0:p.fontSize)||12),onSave:b=>o("fontSize",Number(b)||12),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Weight"}),e.jsxs(s.Select,{value:((g=l==null?void 0:l.name)==null?void 0:g.fontWeight)||"normal",onValueChange:b=>o("fontWeight",b),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]})]})]})]}),!r&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"POSITION"}),e.jsxs(s.Select,{value:(l==null?void 0:l.position)||"auto",onValueChange:b=>i("position",b),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:x.map(b=>e.jsx(s.SelectItem,{value:b,className:"text-xs",children:b==="auto"?"Auto (Default)":s.titleCase(b)},b))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"LABELS"}),e.jsx(s.SmallSwitch,{checked:((v=l==null?void 0:l.labels)==null?void 0:v.enabled)!==!1,onCheckedChange:b=>u("enabled",b)})]}),((O=l==null?void 0:l.labels)==null?void 0:O.enabled)!==!1&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Rotation"}),e.jsxs(s.Select,{value:String(((A=l==null?void 0:l.labels)==null?void 0:A.rotation)||"auto"),onValueChange:b=>u("rotation",b==="auto"?"auto":Number(b)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:h.map(b=>e.jsx(s.SelectItem,{value:b,className:"text-xs",children:b==="auto"?"Auto (Default)":`${b}°`},b))})]})]})]}),a==="value"&&e.jsx(We,{formatOptions:(l==null?void 0:l.formatOptions)||{},onFormatOptionsChange:b=>c({...l,formatOptions:b}),showTitle:!0,title:"FORMAT"}),a==="value"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"SCALE"}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Min"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((y=l==null?void 0:l.scale)==null?void 0:y.min)==="auto"||((T=l==null?void 0:l.scale)==null?void 0:T.min)===void 0?"":String(l.scale.min),onSave:b=>m("min",b),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Max"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((N=l==null?void 0:l.scale)==null?void 0:N.max)==="auto"||((C=l==null?void 0:l.scale)==null?void 0:C.max)===void 0?"":String(l.scale.max),onSave:b=>m("max",b),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Step"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((k=l==null?void 0:l.scale)==null?void 0:k.stepSize)==="auto"||((R=l==null?void 0:l.scale)==null?void 0:R.stepSize)===void 0?"":String(l.scale.stepSize),onSave:b=>m("stepSize",b),className:""})})]})]})]})]})]})}function qe(t,n,a){var d,r,i;if(t==="x"){const o=(r=(d=n==null?void 0:n.config)==null?void 0:d.groupByColumns)==null?void 0:r[0];return(o==null?void 0:o.label)||(o==null?void 0:o.name)||""}const l=((i=n==null?void 0:n.config)==null?void 0:i.metricColumns)||[],c=(a==null?void 0:a.datasetOptions)||[];if(t==="y"){const o=l.filter((u,m)=>{const x=c.find(h=>h.idx===m);return!x||x.yAxisId!=="y2"});return o.length===1&&(o[0].label||o[0].name)||""}if(t==="y2"){const o=l.filter((u,m)=>{const x=c.find(h=>h.idx===m);return(x==null?void 0:x.yAxisId)==="y2"});return o.length===1&&(o[0].label||o[0].name)||""}return""}function ya(){const t=s.useEditorStore(i=>i.card),n=s.useEditorStore(i=>{var o;return(o=i.card)==null?void 0:o.preferences}),{setCardPreferences:a,updateCardInFrame:l}=s.useEditorActions();if(!t||["tornado","pyramid","kpi","treemap"].includes(t.type))return null;const c=s.AxisConfigurator.getAxisInfo(t.type,n);if(!c.hasXAxis&&!c.hasYAxis)return null;const d=(i,o)=>{let u;i==="x"?u="xAxisConfig":i==="y"?u="yAxisConfig":u="secondaryYAxisConfig";const m={...n||{},[u]:o};a(m);const x={...t,preferences:m};l(x)},r=c.hasY2Axis&&t.type==="combo";return e.jsxs(s.AccordionItem,{value:"axes",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Es,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Axes"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs(s.Accordion,{type:"single",collapsible:!0,className:"w-full",children:[c.hasXAxis&&e.jsx($e,{axis:"x",label:"X-Axis",type:c.xAxisType,config:s.mergeAxisConfig(s.getDefaultAxisConfig(t.type,"x"),n==null?void 0:n.xAxisConfig),onChange:i=>d("x",i),defaultTitle:qe("x",t,n)}),c.hasYAxis&&e.jsx($e,{axis:"y",label:r?"Y-Axis (Left)":"Y-Axis",type:c.yAxisType,config:s.mergeAxisConfig(s.getDefaultAxisConfig(t.type,"y"),n==null?void 0:n.yAxisConfig),onChange:i=>d("y",i),defaultTitle:qe("y",t,n)}),r&&e.jsx($e,{axis:"y2",label:"Y2-Axis (Right)",type:"value",config:s.mergeAxisConfig({enabled:!0,position:"right"},n==null?void 0:n.secondaryYAxisConfig),onChange:i=>d("y2",i),defaultTitle:qe("y2",t,n),hidePosition:!0})]})})]})}function Na(){const t=s.useEditorStore(m=>m.card),n=s.useEditorStore(m=>{var x,h,w,j;return(j=(w=(h=(x=m.card)==null?void 0:x.preferences)==null?void 0:h.chartOptions)==null?void 0:w.plugins)==null?void 0:j.legend}),{setLegendOptions:a}=s.useEditorActions();if(!t||!s.chartSupportsLegend(t.type))return null;const l=s.getDefaultLegendConfig(t.type),c=(l==null?void 0:l.display)??!0,d=(l==null?void 0:l.position)??"top",r=(l==null?void 0:l.align)??"center",i=(m,x)=>{const h={...n,[m]:x};a(h)},o=["top","bottom","left","right"],u=["start","center","end"];return e.jsxs(s.AccordionItem,{value:"legend",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(dt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Legend"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Display"}),e.jsx(s.SmallSwitch,{checked:(n==null?void 0:n.display)??c,onCheckedChange:m=>i("display",m)})]}),((n==null?void 0:n.display)??c)&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Position"}),e.jsxs(s.Select,{value:(n==null?void 0:n.position)||d,onValueChange:m=>i("position",m),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:o.map(m=>e.jsx(s.SelectItem,{value:m,className:"text-xs",children:s.titleCase(m)},m))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Alignment"}),e.jsxs(s.Select,{value:(n==null?void 0:n.align)||r,onValueChange:m=>i("align",m),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:u.map(m=>e.jsx(s.SelectItem,{value:m,className:"text-xs",children:s.titleCase(m)},m))})]})]})]})]})})]})}function Ca(){var I,M,E,B,$,V,q,G,z,H,_;const t=s.useEditorStore(P=>P.card),n=s.useEditorStore(P=>{var Y,Z;return(Z=(Y=P.card)==null?void 0:Y.preferences)==null?void 0:Z.dataLabelsConfig}),a=s.useEditorStore(P=>{var Y;return(Y=P.card)==null?void 0:Y.preferences}),{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!s.chartSupportsDataLabels(t.type))return null;const d=t.type==="bullet"?((I=a==null?void 0:a.bulletConfig)==null?void 0:I.orientation)??(((M=a==null?void 0:a.chartOptions)==null?void 0:M.indexAxis)==="x"?"vertical":"horizontal"):void 0,r=d!=null?d==="horizontal":((E=a==null?void 0:a.chartOptions)==null?void 0:E.indexAxis)==="y",i=t.type==="stackedBar"||t.type==="stackedLine",o=t.type==="pie"||t.type==="doughnut"||t.type==="polarArea"||t.type==="funnel",u=!o,m=t.type==="bubble"||t.type==="scatter",x=($=(B=a==null?void 0:a.chartOptions)==null?void 0:B.plugins)==null?void 0:$.datalabels,h=(x==null?void 0:x.display)!==!1&&((x==null?void 0:x.anchor)||(x==null?void 0:x.align)),w=s.getDefaultDataLabelsConfig(t.type,t.preferences),j=n??w,f=(w==null?void 0:w.enabled)??h??!1,p=(n==null?void 0:n.enabled)!==void 0?n.enabled:f,g=(P,Y)=>{const W={...w??{},...n??{},[P]:Y};P==="enabled"&&(W.display=Y,Y&&m&&!W.position&&(W.position="bottom")),P==="position"&&m&&(W.position="bottom");const ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},v=(P,Y)=>{const Z=n??{},W={...w??{},...Z,font:{...Z.font||{},[P]:Y}},ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},O=(P,Y)=>{const Z=n??{},W={...w??{},...Z,formatOptions:{...Z.formatOptions||{},[P]:Y}},ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},A=()=>{const P=t.type;return P==="stackedBar"||P==="stackedLine"||P==="pie"||P==="doughnut"?"center":r?"right":"top"},y=t.type!=="treemap"&&t.type!=="heatmap",T=t.type!=="treemap",N=A();let C=[];if(y)if(m)C=["bottom"];else if(i||o)C=["center"];else{const P=r?["center","left","right"]:["center","top","bottom"];C=["auto",...t.type==="bullet"?P:P.filter(Z=>Z!==N),"custom"]}const k=()=>s.titleCase(N),R=["center","start","end"],b=["center","start","end","top","bottom"],S=u?["auto","number","currency","percent","none"]:["number","currency","percent","none"],D=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"GBP",label:"GBP - British Pound"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"INR",label:"INR - Indian Rupee"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"}],F=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"}];return e.jsxs(s.AccordionItem,{value:"data-labels",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(St,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Data Labels"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Display"}),e.jsx(s.SmallSwitch,{checked:p,onCheckedChange:P=>g("enabled",P)})]}),p&&e.jsxs(e.Fragment,{children:[y&&!i&&!o&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Position"}),e.jsxs(s.Select,{value:m?(j==null?void 0:j.position)||"bottom":(j==null?void 0:j.position)||"auto",onValueChange:P=>g("position",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:C.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:P==="auto"?k():P==="custom"?"Custom (Advanced)":s.titleCase(P)},P))})]})]}),i&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Show Total"}),e.jsx(s.SmallSwitch,{checked:(n==null?void 0:n.showTotal)??!1,onCheckedChange:P=>g("showTotal",P)})]}),(j==null?void 0:j.position)==="custom"&&e.jsxs("div",{className:"space-y-2 pl-4 border-l-2 border-muted",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-16",children:"Anchor"}),e.jsxs(s.Select,{value:(j==null?void 0:j.anchor)||"center",onValueChange:P=>g("anchor",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:R.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:s.titleCase(P)},P))})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-16",children:"Align"}),e.jsxs(s.Select,{value:String((j==null?void 0:j.align)||"center"),onValueChange:P=>g("align",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:b.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:s.titleCase(P)},P))})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Format"}),e.jsxs(s.Select,{value:(j==null?void 0:j.format)||(u?"auto":"none"),onValueChange:P=>g("format",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:S.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:P==="auto"&&u?"Auto (Use Axis)":P==="none"?"None (Raw Value)":s.titleCase(P)},P))})]})]}),(j==null?void 0:j.format)&&!["auto","none"].includes(j.format)&&e.jsxs("div",{className:"space-y-2 pl-4 border-l-2 border-muted",children:[["number","currency","percent"].includes(j.format)&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Decimals"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(((V=j==null?void 0:j.formatOptions)==null?void 0:V.decimalPlaces)??(j.format==="currency"?2:j.format==="percent"?1:0)),onSave:P=>O("decimalPlaces",Number(P)),className:""})})]}),j.format==="currency"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Currency"}),e.jsxs(s.Select,{value:((q=j==null?void 0:j.formatOptions)==null?void 0:q.currency)||"USD",onValueChange:P=>O("currency",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:D.map(P=>e.jsx(s.SelectItem,{value:P.value,className:"text-xs",children:P.label},P.value))})]})]}),["number","currency"].includes(j.format)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:((G=j==null?void 0:j.formatOptions)==null?void 0:G.useSuffix)??!1,onCheckedChange:P=>O("useSuffix",P)})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Locale"}),e.jsxs(s.Select,{value:((z=j==null?void 0:j.formatOptions)==null?void 0:z.locale)||"en-US",onValueChange:P=>O("locale",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:F.map(P=>e.jsx(s.SelectItem,{value:P.value,className:"text-xs",children:P.label},P.value))})]})]})]}),T&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Appearance"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Size"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((H=j==null?void 0:j.font)==null?void 0:H.size)||12),onSave:P=>v("size",Number(P)),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Weight"}),e.jsxs(s.Select,{value:((_=j==null?void 0:j.font)==null?void 0:_.weight)||"normal",onValueChange:P=>v("weight",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Color"}),e.jsxs(s.Select,{value:(j==null?void 0:j.color)==="auto"||!(j!=null&&j.color)?"auto":"custom",onValueChange:P=>{P==="auto"&&g("color","auto")},children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"auto",className:"text-xs",children:"Auto (Smart Contrast)"}),e.jsx(s.SelectItem,{value:"custom",disabled:!0,className:"text-xs",children:"Custom (Coming Soon)"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Rotation"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"0",value:String((j==null?void 0:j.rotation)||0),onSave:P=>g("rotation",Number(P)),className:""})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"degrees"})]})]})]})]})})]})}const ps=[{value:"green",color:"#16a34a",label:"Green"},{value:"red",color:"#dc2626",label:"Red"},{value:"neutral",color:"#6b7280",label:"Neutral"}],Sa=["#16a34a","#dc2626","#6b7280","#2563eb","#ca8a04","#7c3aed","#0891b2","#e11d48","#ea580c"];function De({value:t,onChange:n,label:a}){const[l,c]=L.useState(!1),[d,r]=L.useState(!1),i=()=>{const o=ps.find(u=>u.value===t);return o?o.color:t.startsWith("#")?t:ps[2].color};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Popover,{open:l,onOpenChange:c,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx("button",{className:"size-5 rounded border-2 border-input shadow-sm transition-all hover:scale-110 hover:border-gray-400",style:{backgroundColor:i()},"aria-label":`Change color for ${a}`})}),e.jsx(s.PopoverContent,{align:"start",className:"w-auto p-2",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"grid grid-cols-3 items-center justify-items-center gap-1",children:Sa.map(o=>e.jsx("button",{className:s.cn("h-5 w-5 rounded border-2 transition-all",i()===o?"border-gray-900":"border-transparent"),style:{backgroundColor:o},onClick:()=>{n(o),c(!1)},children:e.jsx("span",{className:"sr-only",children:o})},o))}),e.jsx("button",{className:"w-full rounded border border-input px-2 py-1 text-xs hover:bg-muted",onClick:()=>{r(!0),c(!1)},children:"Custom color..."})]})})]}),e.jsx("span",{className:"text-xs text-muted-foreground",children:a})]}),d&&e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[e.jsx("div",{className:"absolute inset-0 bg-black/50",onClick:()=>r(!1)}),e.jsx("div",{className:"relative rounded-lg bg-white shadow-lg",children:e.jsx($s,{color:i(),onColorChange:o=>{n(o),r(!1)},onClose:()=>r(!1)})})]})]})}const gs={difference:{label:"Difference",description:"Shows the exact difference between two values.",formula:"Primary - Secondary",example:"Sales increased by $5,000 compared to last year."},change:{label:"Change",description:"Shows the percentage increase or decrease between two values.",formula:"(Primary - Secondary) / Secondary × 100%",example:"Year-over-year sales change of 25%."},change_difference:{label:"Change (difference)",description:"Shows both the percentage change and the exact difference between two values.",formula:`% Change: (Primary - Secondary) / Secondary × 100%
|
|
189
|
-
Difference: Primary - Secondary`,example:"Sales grew by 25% ($5,000) compared to last year."},ratio:{label:"Ratio",description:"Shows how much of one value is covered by another — useful for quotas and benchmarks.",formula:"Primary / Secondary × 100%",example:"You've achieved 80% of your sales quota."}};function wa(){var h,w,j,f,p,g,v,O,A,y,T;const t=s.useEditorStore(N=>N.card),{setCardPreferences:n}=s.useEditorActions(),a=((h=t.config)==null?void 0:h.metricColumns)||[],l=((w=t.config)==null?void 0:w.groupByColumns)||[],c=a.length===2&&l.length===0,d=a.length>=1&&l.length>0;if(!(t.type==="kpi"&&(c||d)))return null;const i=((f=(j=t.preferences)==null?void 0:j.kpiVisualOptions)==null?void 0:f.metricComparison)||{},o=N=>{var k;const C={...t.preferences,kpiVisualOptions:{...(k=t.preferences)==null?void 0:k.kpiVisualOptions,metricComparison:{...i,...N}}};n(C)},u=d||i.enabled,m=((p=t.config)==null?void 0:p.comparisonType)||"none",x=m==="previous_period"||m==="same_period_last_year"||m==="start_vs_end";return e.jsxs(s.AccordionItem,{value:"comparison",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex items-center gap-2 py-2 text-sm hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(it,{className:"h-4 w-4"}),e.jsx("span",{children:"Comparison"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 pb-4 pt-2",children:[c&&a.length===2&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{htmlFor:"enable-comparison",className:"text-xs",children:"Enable Metric Comparison"}),e.jsx(s.SmallSwitch,{id:"enable-comparison",checked:i.enabled||!1,onCheckedChange:N=>o({enabled:N,...N&&!i.calculationType?{calculationType:"difference"}:{}})})]}),u&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(s.Label,{htmlFor:"calculation-type",className:"text-xs font-medium uppercase text-muted-foreground",children:"Calculated as"}),e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-3 w-3 cursor-help text-muted-foreground"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:(()=>{const N=i.calculationType||"difference",C=gs[N];return e.jsxs("div",{className:"space-y-2 text-xs",children:[e.jsx("div",{className:"text-sm font-semibold",children:C==null?void 0:C.label}),e.jsx("p",{children:C==null?void 0:C.description}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:"Formula:"})," ",e.jsx("code",{className:"rounded bg-muted px-1 py-0.5 text-[10px]",children:C==null?void 0:C.formula})]}),e.jsxs("div",{className:"border-t pt-1.5 italic",children:[e.jsx("span",{className:"font-medium",children:"Example:"})," ",C==null?void 0:C.example]})]})})()})]})]}),e.jsxs(s.Select,{value:i.calculationType||"difference",onValueChange:N=>o({calculationType:N}),children:[e.jsx(s.SelectTrigger,{id:"calculation-type",className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Object.entries(gs).map(([N,C])=>e.jsx(s.SelectItem,{value:N,className:"text-xs",children:C.label},N))})]})]}),e.jsx(We,{formatOptions:i.formatOptions||{},onFormatOptionsChange:N=>o({formatOptions:N}),showTitle:!0,title:"COMPARISON FORMAT"}),(x||c)&&e.jsxs("div",{className:"space-y-3 border-t pt-3",children:[e.jsx(s.Label,{className:"text-xs font-medium uppercase text-muted-foreground",children:"Indicator"}),c&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Position"}),e.jsxs(s.Select,{value:i.position||"right",onValueChange:N=>o({position:N}),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"right",className:"text-xs",children:"Right"}),e.jsx(s.SelectItem,{value:"left",className:"text-xs",children:"Left"}),e.jsx(s.SelectItem,{value:"top",className:"text-xs",children:"Top"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"show-arrow",checked:i.showArrow!==!1,onCheckedChange:N=>o({showArrow:N})}),e.jsx(s.Label,{htmlFor:"show-arrow",className:"cursor-pointer text-xs text-muted-foreground",children:"Arrow"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"show-color",checked:i.showColor!==!1,onCheckedChange:N=>o({showColor:N})}),e.jsx(s.Label,{htmlFor:"show-color",className:"cursor-pointer text-xs text-muted-foreground",children:"Color"})]}),i.showColor!==!1&&e.jsxs("div",{className:"space-y-2 border-l-2 border-muted pl-3",children:[e.jsx(De,{value:((g=i.colorConfig)==null?void 0:g.increase)||"green",onChange:N=>o({colorConfig:{...i.colorConfig,increase:N}}),label:"Primary > Secondary"}),e.jsx(De,{value:((v=i.colorConfig)==null?void 0:v.decrease)||"red",onChange:N=>o({colorConfig:{...i.colorConfig,decrease:N}}),label:"Primary < Secondary"}),e.jsx(De,{value:((O=i.colorConfig)==null?void 0:O.noChange)||"neutral",onChange:N=>o({colorConfig:{...i.colorConfig,noChange:N}}),label:"Primary = Secondary"})]})]}),e.jsxs("div",{className:"space-y-3 border-t pt-3",children:[e.jsx(s.Label,{className:"text-xs font-medium uppercase text-muted-foreground",children:"Label"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"conditional-label",checked:i.conditionalLabel||!1,onCheckedChange:N=>o({conditionalLabel:N})}),e.jsx(s.Label,{htmlFor:"conditional-label",className:"cursor-pointer text-xs text-muted-foreground",children:"Conditional"})]}),i.conditionalLabel?e.jsxs("div",{className:"space-y-3 border-l-2 border-muted pl-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(s.Label,{htmlFor:"label-increase",className:"text-xs text-muted-foreground",children:["Primary ",">"," Secondary"]}),e.jsx(s.Input$1,{id:"label-increase",type:"text",value:((A=i.conditionalLabels)==null?void 0:A.increase)??"Increase",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,increase:N.target.value}}),placeholder:"Increase",className:"h-7 text-xs"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(s.Label,{htmlFor:"label-decrease",className:"text-xs text-muted-foreground",children:["Primary ","<"," Secondary"]}),e.jsx(s.Input$1,{id:"label-decrease",type:"text",value:((y=i.conditionalLabels)==null?void 0:y.decrease)??"Decrease",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,decrease:N.target.value}}),placeholder:"Decrease",className:"h-7 text-xs"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"label-no-change",className:"text-xs text-muted-foreground",children:"Primary = Secondary"}),e.jsx(s.Input$1,{id:"label-no-change",type:"text",value:((T=i.conditionalLabels)==null?void 0:T.noChange)??"No change",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,noChange:N.target.value}}),placeholder:"No change",className:"h-7 text-xs"})]})]}):e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"label-name",className:"text-xs text-muted-foreground",children:"Previous Label"}),e.jsx(s.Input$1,{id:"label-name",type:"text",value:i.labelName??"",onChange:N=>o({labelName:N.target.value}),placeholder:"e.g. Previous, Last Period",className:"h-7 text-xs"})]})]})]})]})]})}function Ta(){var c,d;const t=s.useEditorStore(r=>r.card),{setCardPreferences:n}=s.useEditorActions();if(t.type!=="kpi")return null;const a=((d=(c=t.preferences)==null?void 0:c.kpiVisualOptions)==null?void 0:d.formatOptions)||{},l=r=>{var o;const i={...t.preferences,kpiVisualOptions:{...(o=t.preferences)==null?void 0:o.kpiVisualOptions,formatOptions:r}};n(i)};return e.jsxs(s.AccordionItem,{value:"kpi-format",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex items-center gap-2 py-2 text-sm hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Hash,{className:"h-4 w-4"}),e.jsx("span",{children:"Number Format"})]})}),e.jsx(s.AccordionContent,{className:"space-y-4 pb-4 pt-2",children:e.jsx(We,{formatOptions:a,onFormatOptionsChange:l,showTitle:!1})})]})}function Ea(){var d;const t=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.type}),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),{setCardPreferences:a}=s.useEditorActions();if(t!=="treemap")return null;const l=((d=n==null?void 0:n.chartOptions)==null?void 0:d.treemapColorMode)??"branch",c=r=>{const i={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,treemapColorMode:r}};a(i)};return e.jsxs(s.AccordionItem,{value:"treemap-color-mode",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.TbChartTreemap,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Treemap Colors"})]})}),e.jsx(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:e.jsxs(s.RadioGroup,{value:l,onValueChange:r=>c(r??"branch"),className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:"treemap-color-branch",value:"branch"}),e.jsxs(s.Label,{htmlFor:"treemap-color-branch",className:"flex flex-col text-sm font-normal leading-tight",children:[e.jsx("span",{className:"font-medium",children:"Shade by value (default)"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Keeps parent hue and adjusts lightness based on the node's share within the branch."})]})]}),e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:"treemap-color-category",value:"category"}),e.jsxs(s.Label,{htmlFor:"treemap-color-category",className:"flex flex-col text-sm font-normal leading-tight",children:[e.jsx("span",{className:"font-medium",children:"Single color per category"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Applies the top-level color to all descendants without lightening/darkening."})]})]})]})})]})}const ka=Object.keys(s.HEATMAP_PRESET_PALETTES);function Ia(){const t=s.useEditorStore(h=>h.card),n=s.useEditorStore(h=>{var w;return(w=h.card)==null?void 0:w.preferences}),{setCardPreferences:a}=s.useEditorActions(),l=(t==null?void 0:t.type)==="heatmap",c=(n==null?void 0:n.heatmapConfig)||{},d=c.colorPalette??"green",r=c.legendPosition??"bottom",i=L.useMemo(()=>c.colorRange&&c.colorRange[0]&&c.colorRange[1]?[c.colorRange[0],c.colorRange[1]]:[...s.DEFAULT_HEATMAP_CUSTOM_RANGE],[c.colorRange]),o=h=>{const w={...c,...h};w.colorPalette!=="custom"?delete w.colorRange:(!w.colorRange||!w.colorRange[0]||!w.colorRange[1])&&(w.colorRange=[...s.DEFAULT_HEATMAP_CUSTOM_RANGE]),a({...n||{},heatmapConfig:w})},u=h=>{o({colorPalette:h,colorRange:h==="custom"?c.colorRange??[...s.DEFAULT_HEATMAP_CUSTOM_RANGE]:void 0})},m=(h,w)=>{const j=[...i];j[h]=w,o({colorPalette:"custom",colorRange:j})},x=h=>{o({legendPosition:h})};return l?e.jsxs(s.AccordionItem,{value:"heatmap-color-config",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Grid3x3,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Heatmap Settings"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:[e.jsxs(s.RadioGroup,{value:d,onValueChange:h=>u(h??"blue"),className:"space-y-3",children:[ka.map(h=>{const w=s.HEATMAP_PRESET_PALETTES[h];return e.jsxs("div",{className:"flex items-center gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:`heatmap-${h}`,value:h}),e.jsxs(s.Label,{htmlFor:`heatmap-${h}`,className:"flex w-full flex-col gap-1 text-sm font-normal",children:[e.jsx("span",{className:"font-medium",children:w.label}),e.jsx("span",{className:"h-2 rounded-full",style:{background:`linear-gradient(90deg, ${w.start}, ${w.end})`}})]})]},h)}),e.jsxs("div",{className:"flex flex-col gap-2 rounded-md border border-border px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.RadioGroupItem,{id:"heatmap-custom",value:"custom"}),e.jsx(s.Label,{htmlFor:"heatmap-custom",className:"text-sm font-medium",children:"Custom colors"})]}),d==="custom"&&e.jsxs("div",{className:"flex flex-col gap-3 pl-6",children:[e.jsx(De,{value:i[0],onChange:h=>m(0,h),label:"Start color"}),e.jsx(De,{value:i[1],onChange:h=>m(1,h),label:"End color"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Color scale position"}),e.jsxs(s.Select,{value:r,onValueChange:h=>x(h??"bottom"),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:["top","bottom","left","right"].map(h=>e.jsx(s.SelectItem,{value:h,className:"text-xs",children:h[0].toUpperCase()+h.slice(1)},h))})]})]})]})]}):null}const fs=["poor","satisfactory","good"];function js(t){if(!t)return;const n={...t};return n.targetMetricKey||delete n.targetMetricKey,n.comparativeMetricKey||delete n.comparativeMetricKey,!n.targetValue&&n.targetValue!==0&&delete n.targetValue,(!n.rangeKeys||Object.keys(n.rangeKeys).length===0)&&delete n.rangeKeys,(!n.ranges||Object.keys(n.ranges).length===0)&&delete n.ranges,(!n.rangeLabels||Object.keys(n.rangeLabels).length===0)&&delete n.rangeLabels,n}function Aa(){var D,F;const t=s.useEditorStore(I=>I.card.preferences),n=s.useEditorStore(I=>I.card),a=s.useEditorStore(I=>I.isDevMode),{setCardPreferences:l,setChartOrientation:c}=s.useEditorActions(),d=L.useMemo(()=>(t==null?void 0:t.bulletConfig)||{},[t==null?void 0:t.bulletConfig]),i=s.hasValidCardConfig(n==null?void 0:n.config)&&!a,[o,u]=L.useState(d.showRanges??!1),[m,x]=L.useState(d.rangesArePercentages??!1),h=d.disableAutoComparative?!1:d.comparativeMetricKey!==void 0||!i,[w,j]=L.useState(h);L.useEffect(()=>{u(d.showRanges??!1)},[d.showRanges]),L.useEffect(()=>{x(d.rangesArePercentages??!1)},[d.rangesArePercentages]);const f=d.targetValue===void 0?"":String(d.targetValue);L.useEffect(()=>{const I=d.disableAutoComparative?!1:d.comparativeMetricKey!==void 0||!i;j(I)},[d.disableAutoComparative,d.comparativeMetricKey,i]);const p=L.useMemo(()=>{var I,M;return((M=(I=n==null?void 0:n.config)==null?void 0:I.metricColumns)==null?void 0:M.map(E=>{const B=E.label||E.name||E.alias||"";return{value:B,label:B}}))||[]},[(D=n==null?void 0:n.config)==null?void 0:D.metricColumns]);L.useEffect(()=>{var G,z;if(!t||p.length===0)return;const I={};let M=!1;const E=(G=p[1])==null?void 0:G.value;!d.targetMetricKey&&E&&(I.targetMetricKey=E,M=!0);const B=(z=p[2])==null?void 0:z.value;!d.comparativeMetricKey&&!d.disableAutoComparative&&B&&(I.comparativeMetricKey=B,M=!0);const $=p.slice(3),V={...d.rangeKeys||{}};let q=!1;if(fs.forEach((H,_)=>{!V[H]&&$[_]&&(V[H]=$[_].value,q=!0)}),q&&(I.rangeKeys=V,M=!0,d.showRanges===void 0&&(I.showRanges=!0)),M){const H=js({...d,...I});l({...t,bulletConfig:H})}},[p,d,t,l]);const g=I=>{const M=js({...d,...I});l({...t,bulletConfig:M})},v=(I,M)=>{const E={...d.rangeKeys||{},[I]:M.trim()};M.trim()||delete E[I],g({rangeKeys:Object.keys(E).length>0?E:void 0})},O=(I,M)=>{const E=M===""?void 0:Number(M),B={...d.ranges||{}};E===void 0||Number.isNaN(E)?delete B[I]:B[I]=E,g({ranges:Object.keys(B).length?B:void 0})},A=I=>{if(I===""){g({targetValue:void 0});return}const M=Number(I);g({targetValue:Number.isNaN(M)?void 0:M})},y=(I,M)=>{const E=M.trim(),B={...d.rangeLabels||{}};E?B[I]=E:delete B[I],g({rangeLabels:Object.keys(B).length>0?B:void 0})},T=I=>{u(I),g({showRanges:I})},N=I=>{x(I),g({rangesArePercentages:I})},C=d.orientation||(((F=t==null?void 0:t.chartOptions)==null?void 0:F.indexAxis)==="y"?"horizontal":"vertical"),k=I=>{g({orientation:I}),c(I==="horizontal"?"y":"x")},R=[{value:"__none",label:"None"},...p.map(I=>({value:I.value,label:I.label}))],b=R,S=I=>{if(j(I),!I){g({comparativeMetricKey:void 0,disableAutoComparative:!0});return}if(!i){g({disableAutoComparative:!1});return}if(d.comparativeMetricKey)g({disableAutoComparative:!1});else{const M=p.find(E=>E.value!==d.targetMetricKey);g({comparativeMetricKey:M==null?void 0:M.value,disableAutoComparative:!1})}};return e.jsxs(s.AccordionItem,{value:"bullet-config",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.BulletChartIcon,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Bullet Chart"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-6 border-t border-border/50 py-4",children:[e.jsxs("section",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"ORIENTATION"}),e.jsxs(s.Select,{value:C,onValueChange:I=>k(I),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"horizontal",className:"text-xs",children:"Horizontal"}),e.jsx(s.SelectItem,{value:"vertical",className:"text-xs",children:"Vertical"})]})]})]}),i&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"TARGET METRIC"}),e.jsxs(s.Select,{value:d.targetMetricKey||"__none",onValueChange:I=>g({targetMetricKey:I==="__none"?void 0:I}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select metric"})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"__none",className:"text-xs",children:"None"}),p.map(I=>e.jsx(s.SelectItem,{value:I.value,className:"text-xs",children:I.label},I.value))]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"COMPARATIVE METRIC"}),i?e.jsxs(s.Select,{value:d.comparativeMetricKey||"__none",onValueChange:I=>g({comparativeMetricKey:I==="__none"?void 0:I,disableAutoComparative:I==="__none"}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select metric"})}),e.jsx(s.SelectContent,{children:R.map(I=>e.jsx(s.SelectItem,{value:I.value,className:"text-xs",children:I.label},I.value))})]}):e.jsxs("div",{className:"space-y-2 rounded-md border border-border/50 p-3 text-xs",children:[e.jsxs("div",{className:"flex items-center justify-between font-medium",children:[e.jsx("span",{children:"Show comparative"}),e.jsx(s.SmallSwitch,{checked:w,onCheckedChange:I=>S(I)})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Comparative values are auto-detected from the third metric in your results."})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"TARGET VALUE (FALLBACK)"}),e.jsx(oe,{type:"number",className:"h-8 text-xs",value:f,onCommit:A,placeholder:"Optional numeric fallback"})]})]}),e.jsxs("section",{className:"space-y-4 rounded-md border border-border/60 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"QUALITATIVE RANGES"}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:200,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("button",{type:"button",className:"text-muted-foreground transition-colors hover:text-foreground",children:e.jsx(s.Info,{className:"h-3.5 w-3.5"})})}),e.jsx(s.TooltipContent,{side:"top",children:"Use columns or static values for Poor / Average / Good."})]})})]}),e.jsx("div",{className:"flex items-center gap-2 text-xs font-medium",children:e.jsx(s.SmallSwitch,{checked:o,onCheckedChange:T})})]}),e.jsxs("div",{className:"flex items-center gap-2 text-xs font-medium",children:[e.jsx("input",{type:"checkbox",checked:m,disabled:!o,onChange:I=>N(I.target.checked),className:"h-3.5 w-3.5 accent-primary disabled:opacity-50"}),e.jsx("span",{children:"Use % of value"})]})]}),o&&e.jsx("div",{className:"space-y-3",children:fs.map(I=>{var M,E,B,$,V;return e.jsxs("div",{className:"space-y-2 rounded-md border border-border/60 bg-muted/40 p-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," LABEL"]}),e.jsx(oe,{placeholder:`Display label (${I})`,value:((M=d.rangeLabels)==null?void 0:M[I])||"",onCommit:q=>y(I,q),className:"h-8 text-xs"})]}),i?e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," COLUMN"]}),e.jsxs(s.Select,{value:((E=d.rangeKeys)==null?void 0:E[I])||"__none",onValueChange:q=>{q==="__none"?v(I,""):v(I,q)},children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select column"})}),e.jsx(s.SelectContent,{children:b.map(q=>e.jsx(s.SelectItem,{value:q.value,className:"text-xs",children:q.label},q.value))})]})]}):e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," COLUMN KEY"]}),e.jsx(oe,{placeholder:"Column key",value:((B=d.rangeKeys)==null?void 0:B[I])||"",onCommit:q=>v(I,q),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," VALUE"]}),e.jsx(oe,{type:"number",placeholder:m?"0.80":"Static value",value:(($=d.ranges)==null?void 0:$[I])===void 0?"":String((V=d.ranges)==null?void 0:V[I]),onCommit:q=>O(I,q),className:"h-8 text-xs"})]})]},I)})})]})]})]})}function bs(t){if(!t)return;const n={...t};if(n.xMetricKey||delete n.xMetricKey,n.yMetricKey||delete n.yMetricKey,n.radiusMetricKey||delete n.radiusMetricKey,n.labelKey||delete n.labelKey,n.groupKey||delete n.groupKey,n.enableGrouping===void 0&&delete n.enableGrouping,n.radiusScale||delete n.radiusScale,n.fixedRadius===void 0&&delete n.fixedRadius,n.minRadius===void 0&&delete n.minRadius,n.maxRadius===void 0&&delete n.maxRadius,Object.keys(n).length!==0)return n}function Da(){var y,T,N,C,k,R,b,S,D,F,I,M;const t=s.useEditorStore(E=>E.card.preferences),n=s.useEditorStore(E=>E.card),a=s.useEditorStore(E=>E.card.type),l=s.useEditorStore(E=>E.isDevMode),{setCardPreferences:c,updateCardInFrame:d}=s.useEditorActions(),r=a==="bubble",i=a==="scatter",o=L.useMemo(()=>(t==null?void 0:t.bubbleScatterConfig)||{},[t==null?void 0:t.bubbleScatterConfig]),m=s.hasValidCardConfig(n==null?void 0:n.config)&&!l,x=L.useMemo(()=>{var E,B;return((B=(E=n==null?void 0:n.config)==null?void 0:E.metricColumns)==null?void 0:B.map($=>{const V=$.label||$.name||$.alias||"";return{value:V,label:V}}))||[]},[(y=n==null?void 0:n.config)==null?void 0:y.metricColumns]),h=L.useMemo(()=>{var E,B;return((B=(E=n==null?void 0:n.config)==null?void 0:E.groupByColumns)==null?void 0:B.map($=>{const V=$.label||$.name||$.alias||"";return{value:V,label:V}}))||[]},[(T=n==null?void 0:n.config)==null?void 0:T.groupByColumns]),w=L.useMemo(()=>o.enableGrouping!==void 0?o.enableGrouping:!l&&h.length>=2,[o.enableGrouping,l,h.length]),[j,f]=L.useState(w);L.useEffect(()=>{f(w)},[w]);const p=E=>{const B={...t||{},bubbleScatterConfig:E};c(B),n&&d({...n,preferences:B})},g=E=>{c(E),n&&d({...n,preferences:E})};L.useEffect(()=>{if(!t||x.length===0)return;const E={};let B=!1;if(!o.xMetricKey&&x[0]&&(E.xMetricKey=x[0].value,B=!0),!o.yMetricKey&&x[1]&&(E.yMetricKey=x[1].value,B=!0),r&&!o.radiusMetricKey&&x[2]&&(E.radiusMetricKey=x[2].value,B=!0),!o.labelKey&&h[0]&&(E.labelKey=h[0].value,B=!0),w&&!o.groupKey&&h[1]&&(E.groupKey=h[1].value,E.enableGrouping=!0,B=!0),B){const $=bs({...o,...E});p($)}},[x,h,o,t,c,r,w]);const v=E=>{const B=bs({...o,...E});p(B)},O=E=>{f(E),v({enableGrouping:E,groupKey:E?o.groupKey:void 0})};if(!r&&!i)return null;const A=r?s.Circle:s.ChartScatter;return e.jsxs(s.AccordionItem,{value:"bubble-scatter-config",children:[e.jsx(s.AccordionTrigger,{className:"py-2 text-sm",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{className:"h-4 w-4"}),e.jsxs("span",{children:[r?"Bubble":"Scatter"," Configuration"]})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-1 pb-4",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-1 text-xs font-medium text-muted-foreground",children:["Dimensions",e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{children:e.jsx(s.Info,{className:"h-3 w-3"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:e.jsx("p",{children:"Label identifies each point. Group creates separate series in the legend."})})]})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Label Column"}),m?e.jsxs(s.Select,{value:o.labelKey||"",onValueChange:E=>v({labelKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select label column"})}),e.jsx(s.SelectContent,{children:h.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for labels",value:o.labelKey||"",onCommit:E=>v({labelKey:E})})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Group points into series"}),e.jsx(s.SmallSwitch,{checked:j,onCheckedChange:O})]}),j&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Group By"}),m?e.jsxs(s.Select,{value:o.groupKey||"",onValueChange:E=>v({groupKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select group column"})}),e.jsx(s.SelectContent,{children:h.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for grouping",value:o.groupKey||"",onCommit:E=>v({groupKey:E})})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Metrics"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"X Axis"}),m?e.jsxs(s.Select,{value:o.xMetricKey||"",onValueChange:E=>v({xMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select X metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for X axis",value:o.xMetricKey||"",onCommit:E=>v({xMetricKey:E})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Y Axis"}),m?e.jsxs(s.Select,{value:o.yMetricKey||"",onValueChange:E=>v({yMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select Y metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for Y axis",value:o.yMetricKey||"",onCommit:E=>v({yMetricKey:E})})]}),r&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Radius"}),m?e.jsxs(s.Select,{value:o.radiusMetricKey||"",onValueChange:E=>v({radiusMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select radius metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for radius",value:o.radiusMetricKey||"",onCommit:E=>v({radiusMetricKey:E})})]})]}),r&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Radius Settings"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Scale Mode"}),e.jsxs(s.Select,{value:o.radiusScale||"sqrt",onValueChange:E=>v({radiusScale:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"sqrt",children:"Square Root (area-proportional)"}),e.jsx(s.SelectItem,{value:"linear",children:"Linear"}),e.jsx(s.SelectItem,{value:"fixed",children:"Fixed Size"})]})]})]}),o.radiusScale!=="fixed"&&e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Min Radius"}),e.jsx(oe,{className:"h-8 text-xs",type:"number",placeholder:"5",value:((N=o.minRadius)==null?void 0:N.toString())||"",onCommit:E=>v({minRadius:E?Number(E):void 0})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Max Radius"}),e.jsx(oe,{className:"h-8 text-xs",type:"number",placeholder:"30",value:((C=o.maxRadius)==null?void 0:C.toString())||"",onCommit:E=>v({maxRadius:E?Number(E):void 0})})]})]}),o.radiusScale==="fixed"&&e.jsxs("div",{className:"space-y-1",children:[e.jsxs(s.Label,{className:"text-xs",children:["Fixed Radius: ",o.fixedRadius||10]}),e.jsx(s.Slider,{value:[o.fixedRadius||10],onValueChange:([E])=>v({fixedRadius:E}),min:3,max:30,step:1,className:"py-2"})]})]}),i&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Point Settings"}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs(s.Label,{className:"text-xs",children:["Point Size: ",o.fixedRadius||5]}),e.jsx(s.Slider,{value:[o.fixedRadius||5],onValueChange:([E])=>v({fixedRadius:E}),min:2,max:15,step:1,className:"py-2"})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-medium text-muted-foreground",children:"Axis Titles"}),e.jsx(s.SmallSwitch,{checked:((R=(k=t==null?void 0:t.xAxisConfig)==null?void 0:k.name)==null?void 0:R.enabled)??!1,onCheckedChange:E=>{var V,q,G,z;const B=o.xMetricKey||"X Axis",$=o.yMetricKey||"Y Axis";g({...t??{},xAxisConfig:{...t==null?void 0:t.xAxisConfig,name:{enabled:E,text:((q=(V=t==null?void 0:t.xAxisConfig)==null?void 0:V.name)==null?void 0:q.text)||B}},yAxisConfig:{...t==null?void 0:t.yAxisConfig,name:{enabled:E,text:((z=(G=t==null?void 0:t.yAxisConfig)==null?void 0:G.name)==null?void 0:z.text)||$}}})}})]}),((S=(b=t==null?void 0:t.xAxisConfig)==null?void 0:b.name)==null?void 0:S.enabled)&&e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"X Title"}),e.jsx(oe,{className:"h-8 text-xs",placeholder:o.xMetricKey||"X Axis",value:((F=(D=t==null?void 0:t.xAxisConfig)==null?void 0:D.name)==null?void 0:F.text)||"",onCommit:E=>{var B;g({...t??{},xAxisConfig:{...t==null?void 0:t.xAxisConfig,name:{...(B=t==null?void 0:t.xAxisConfig)==null?void 0:B.name,enabled:!0,text:E||o.xMetricKey||"X Axis"}}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Y Title"}),e.jsx(oe,{className:"h-8 text-xs",placeholder:o.yMetricKey||"Y Axis",value:((M=(I=t==null?void 0:t.yAxisConfig)==null?void 0:I.name)==null?void 0:M.text)||"",onCommit:E=>{var B;g({...t??{},yAxisConfig:{...t==null?void 0:t.yAxisConfig,name:{...(B=t==null?void 0:t.yAxisConfig)==null?void 0:B.name,enabled:!0,text:E||o.yMetricKey||"Y Axis"}}})}})]})]})]})]})]})}const La=[{value:"percentOfTotal",label:"% of Total",description:"Each stage shows percentage of the sum of all values"},{value:"percentOfFirst",label:"% of Stage 1",description:"First stage = 100%, others relative to first stage"}];function Ma(){const t=s.useEditorStore(h=>h.card),n=s.useEditorStore(h=>{var w;return(w=h.card)==null?void 0:w.preferences}),{setCardPreferences:a}=s.useEditorActions(),l=(t==null?void 0:t.type)==="funnel",c=(n==null?void 0:n.funnelConfig)||{},d=c.percentMode??"percentOfTotal",r=c.shrinkFraction??1,i=c.labelThreshold??0,o=h=>{const w={...c,...h};a({...n||{},funnelConfig:w})},u=h=>{o({percentMode:h})},m=h=>{o({shrinkFraction:h[0]})},x=h=>{o({labelThreshold:h[0]})};return l?e.jsxs(s.AccordionItem,{value:"funnel-config",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Filter,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Funnel Settings"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"PERCENTAGE MODE"}),e.jsx(s.RadioGroup,{value:d,onValueChange:h=>u(h),className:"space-y-2",children:La.map(h=>e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:`funnel-${h.value}`,value:h.value,className:"mt-0.5"}),e.jsxs(s.Label,{htmlFor:`funnel-${h.value}`,className:"flex flex-col gap-0.5 text-sm font-normal",children:[e.jsx("span",{className:"font-medium",children:h.label}),e.jsx("span",{className:"text-xs text-muted-foreground",children:h.description})]})]},h.value))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"SEGMENT SHRINK"}),e.jsxs("div",{className:"flex items-center gap-3 pt-1",children:[e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Same Width"}),e.jsx(s.Slider,{value:[r],onValueChange:m,min:0,max:1,step:.1,className:"flex-1"}),e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Full Shrink"})]}),e.jsxs("p",{className:"text-center text-xs text-muted-foreground",children:["Current: ",Math.round(r*100),"%"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"LABEL VISIBILITY THRESHOLD"}),e.jsxs("div",{className:"flex items-center gap-3 pt-1",children:[e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Show All"}),e.jsx(s.Slider,{value:[i],onValueChange:x,min:0,max:25,step:1,className:"flex-1"}),e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Hide Small"})]}),e.jsx("p",{className:"text-center text-xs text-muted-foreground",children:i===0?"Showing all labels":`Hide labels below ${i}%`})]})]})]}):null}function Fe(){const t=s.useEditorStore(d=>d.card),{data:n}=s.useDashboardCardQuery(t),{keys:a}=s.getKeys((n==null?void 0:n.records)||[]),l=a.slice(1).filter(d=>!s.METADATA_KEYS.includes(d));let c=l;if(["stackedBar","stackedLine"].includes(t.type)){const d=s.pivotData({dataArray:(n==null?void 0:n.records)||[],groupKey:a[0],pivotKey:a[1],valueKey:a[2]}),{keys:r}=s.getKeys(d);c=r.slice(1)}return{transformedMetricKeys:c,dimensionKeys:a.slice(0,1),keys:a,defaultMetricKeys:l}}const Oa=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"GBP",label:"GBP - British Pound"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"INR",label:"INR - Indian Rupee"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"}],Ra=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"}],Fa=["center","start","end"],Ba=["center","start","end","top","bottom"];function Be({config:t,onChange:n,isHorizontal:a=!1,showAutoFormat:l=!0,positionOptions:c,showAppearance:d=!0}){var j,f,p,g,v,O;const r=c??(a?["auto","center","left","right","custom"]:["auto","center","top","bottom","custom"]),i=l?["auto","number","currency","percent","none"]:["number","currency","percent","none"],o=(A,y)=>{const T={...t,[A]:y};n(T)},u=(A,y)=>{const T={...t,font:{...(t==null?void 0:t.font)||{},[A]:y}};n(T)},m=(A,y)=>{const T={...t,formatOptions:{...(t==null?void 0:t.formatOptions)||{},[A]:y}};n(T)},x=()=>a?"Right":"Top",h=t||{},w=h.format||(l?"auto":"number");return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Position"}),e.jsxs(s.Select,{value:h.position||"auto",onValueChange:A=>o("position",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:r.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:A==="auto"?x():A==="custom"?"Custom (Advanced)":s.titleCase(A)},A))})]})]}),h.position==="custom"&&e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Anchor"}),e.jsxs(s.Select,{value:h.anchor||"center",onValueChange:A=>o("anchor",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Fa.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:s.titleCase(A)},A))})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Align"}),e.jsxs(s.Select,{value:String(h.align||"center"),onValueChange:A=>o("align",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ba.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:s.titleCase(A)},A))})]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Format"}),e.jsxs(s.Select,{value:w,onValueChange:A=>o("format",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:i.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:A==="auto"&&l?"Auto (Use Axis)":A==="none"?"None (Raw Value)":s.titleCase(A)},A))})]})]}),!["auto","none"].includes(w)&&e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[["number","currency","percent"].includes(w)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Decimals"}),e.jsx("div",{className:"w-16",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(((j=h.formatOptions)==null?void 0:j.decimalPlaces)??(w==="currency"?2:w==="percent"?1:0)),onSave:A=>m("decimalPlaces",Number(A)),className:""})})]}),w==="currency"&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Currency"}),e.jsxs(s.Select,{value:((f=h.formatOptions)==null?void 0:f.currency)||"USD",onValueChange:A=>m("currency",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Oa.map(A=>e.jsx(s.SelectItem,{value:A.value,className:"text-xs",children:A.label},A.value))})]})]}),["number","currency"].includes(w)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:((p=h.formatOptions)==null?void 0:p.useSuffix)??!1,onCheckedChange:A=>m("useSuffix",A)})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Locale"}),e.jsxs(s.Select,{value:((g=h.formatOptions)==null?void 0:g.locale)||"en-US",onValueChange:A=>m("locale",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ra.map(A=>e.jsx(s.SelectItem,{value:A.value,className:"text-xs",children:A.label},A.value))})]})]})]}),d&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Appearance"}),e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Size"}),e.jsx("div",{className:"w-16",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((v=h.font)==null?void 0:v.size)||12),onSave:A=>u("size",Number(A)),className:""})})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Weight"}),e.jsxs(s.Select,{value:((O=h.font)==null?void 0:O.weight)||"normal",onValueChange:A=>u("weight",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Color"}),e.jsxs(s.Select,{value:h.color==="auto"||!h.color?"auto":"custom",onValueChange:A=>{A==="auto"&&o("color","auto")},children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"auto",className:"text-xs",children:"Auto (Contrast)"}),e.jsx(s.SelectItem,{value:"custom",disabled:!0,className:"text-xs",children:"Custom"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Rotation"}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("div",{className:"w-12",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(h.rotation||0),onSave:A=>o("rotation",Number(A)),className:""})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"°"})]})]})]})]})]})}const Pa=[{type:"bar",icon:s.ChartColumnIncreasing,label:"Bar"},{type:"line",icon:s.ChartLine,label:"Line"}],Va=[0,1,2,3,4,5,6,8,10],za=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],$a=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function qa(){return s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type})!=="combo"?null:e.jsxs(s.AccordionItem,{value:"combo-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(Ka,{})})]})}function Ka(){const{transformedMetricKeys:t}=Fe();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(Ga,{metricKey:n,idx:a},n))})}function Ga({metricKey:t,idx:n}){var g;const[a,l]=L.useState(!1),c=s.useEditorStore(v=>{var O;return(O=v.card)==null?void 0:O.preferences}),d=s.useEditorStore(v=>{var O,A;return(A=(O=v.card)==null?void 0:O.preferences)==null?void 0:A.datasetOptions}),r=d==null?void 0:d.find(v=>v.idx===n),{setCardPreferences:i}=s.useEditorStore(v=>v.actions),o=(r==null?void 0:r.type)||"bar",u=(r==null?void 0:r.yAxisId)||"y",m=((g=r==null?void 0:r.dataLabels)==null?void 0:g.enabled)??!1,x=o==="line"?s.ChartLine:s.ChartColumnIncreasing;function h(v){const O={idx:n,type:o,yAxisId:u,dataLabels:r==null?void 0:r.dataLabels,...v};if(!r)i({...c||{},datasetOptions:[...d||[],O]});else{const A=d==null?void 0:d.map(y=>y.idx===n?{...y,...O}:y);i({...c||{},datasetOptions:A})}}function w(v){h({type:v})}function j(v){h({yAxisId:v})}function f(v){h({dataLabels:{...r==null?void 0:r.dataLabels,enabled:v}})}function p(v){h({dataLabels:v})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsxs("div",{className:"flex shrink-0 items-center gap-1.5",children:[e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(x,{className:"h-3 w-3"}),o==="line"?"Line":"Bar"]}),e.jsx("span",{className:s.cn("rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:u==="y2"?"Y2":"Y1"})]})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"flex items-end gap-4",children:[e.jsxs("div",{className:"flex-1 space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Chart Type"}),e.jsx("div",{className:"flex gap-2",children:Pa.map(({type:v,icon:O,label:A})=>{const y=o===v;return e.jsxs("button",{onClick:()=>w(v),className:s.cn("flex items-center gap-2 rounded-md border px-3 py-1.5 text-xs transition-colors",y?"border-primary bg-primary/10 text-primary":"border-border bg-background text-muted-foreground hover:bg-muted"),children:[e.jsx(O,{className:"h-4 w-4"}),e.jsx("span",{children:A})]},v)})})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Axis"}),e.jsxs(s.Select,{value:u,onValueChange:v=>j(v),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"y",className:"text-xs",children:"Y1 (Left)"}),e.jsx(s.SelectItem,{value:"y2",className:"text-xs",children:"Y2 (Right)"})]})]})]})]}),o==="line"&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:(r==null?void 0:r.lineStyle)||"solid",onValueChange:v=>h({lineStyle:v}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:$a.map(({value:v,label:O})=>e.jsx(s.SelectItem,{value:v,className:"text-xs",children:O},v))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String((r==null?void 0:r.pointRadius)??3),onValueChange:v=>h({pointRadius:Number(v)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Va.map(v=>e.jsx(s.SelectItem,{value:String(v),className:"text-xs",children:v===0?"None":v},v))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:(r==null?void 0:r.pointStyle)||"circle",onValueChange:v=>h({pointStyle:v}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:za.map(({value:v,label:O})=>e.jsx(s.SelectItem,{value:v,className:"text-xs",children:O},v))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:m,onCheckedChange:f})]}),m&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Be,{config:r==null?void 0:r.dataLabels,onChange:p,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const Ke=[{value:"origin",label:"Origin (X-axis)",description:"Fill to the X-axis (y=0)"},{value:"start",label:"Start (Top)",description:"Fill to the top of chart area"},{value:"end",label:"End (Bottom)",description:"Fill to the bottom of chart area"},{value:"-1",label:"Previous Series",description:"Fill to the series below"},{value:"+1",label:"Next Series",description:"Fill to the series above"},{value:"false",label:"No Fill (Line)",description:"Show as line without fill"}],Ua=[0,1,2,3,4,5,6,8,10],_a=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],Ha=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function Ja(t){return t==null?"origin":t===!1?"false":t}function Ya(){const t=s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type});return t!=="area"&&t!=="stackedArea"?null:e.jsxs(s.AccordionItem,{value:"area-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(Qa,{})})]})}function Qa(){const{transformedMetricKeys:t}=Fe();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(Wa,{metricKey:n,idx:a},n))})}function Wa({metricKey:t,idx:n}){var f;const[a,l]=L.useState(!1),c=s.useEditorStore(p=>{var g;return(g=p.card)==null?void 0:g.preferences}),d=s.useEditorStore(p=>{var g,v;return(v=(g=p.card)==null?void 0:g.preferences)==null?void 0:v.datasetOptions}),r=d==null?void 0:d.find(p=>p.idx===n),{setCardPreferences:i}=s.useEditorStore(p=>p.actions),o=Ja(r==null?void 0:r.fill),u=((f=r==null?void 0:r.dataLabels)==null?void 0:f.enabled)??!1,m=Ke.find(p=>p.value===o)||Ke[0];function x(p){const g={idx:n,fill:o,dataLabels:r==null?void 0:r.dataLabels,...p};if(!r)i({...c||{},datasetOptions:[...d||[],g]});else{const v=d==null?void 0:d.map(O=>O.idx===n?{...O,...g}:O);i({...c||{},datasetOptions:v})}}function h(p){x({fill:p==="false"?!1:p})}function w(p){x({dataLabels:{...r==null?void 0:r.dataLabels,enabled:p}})}function j(p){x({dataLabels:p})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.ChartArea,{className:"h-3 w-3"}),m.label.split(" ")[0]]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Fill Target"}),e.jsxs(s.Select,{value:o,onValueChange:p=>h(p),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ke.map(p=>e.jsx(s.SelectItem,{value:p.value,className:"text-xs",children:e.jsx("div",{className:"flex flex-col",children:e.jsx("span",{children:p.label})})},p.value))})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:(r==null?void 0:r.lineStyle)||"solid",onValueChange:p=>x({lineStyle:p}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ha.map(({value:p,label:g})=>e.jsx(s.SelectItem,{value:p,className:"text-xs",children:g},p))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String((r==null?void 0:r.pointRadius)??3),onValueChange:p=>x({pointRadius:Number(p)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ua.map(p=>e.jsx(s.SelectItem,{value:String(p),className:"text-xs",children:p===0?"None":p},p))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:(r==null?void 0:r.pointStyle)||"circle",onValueChange:p=>x({pointStyle:p}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:_a.map(({value:p,label:g})=>e.jsx(s.SelectItem,{value:p,className:"text-xs",children:g},p))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:u,onCheckedChange:w})]}),u&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Be,{config:r==null?void 0:r.dataLabels,onChange:j,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const Xa=[0,1,2,3,4,5,6,8,10],Za=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],Ge=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function en(){return s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type})!=="line"?null:e.jsxs(s.AccordionItem,{value:"line-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(sn,{})})]})}function sn(){const{transformedMetricKeys:t}=Fe();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(tn,{metricKey:n,idx:a},n))})}function tn({metricKey:t,idx:n}){var p;const[a,l]=L.useState(!1),c=s.useEditorStore(g=>{var v;return(v=g.card)==null?void 0:v.preferences}),d=s.useEditorStore(g=>{var v,O;return(O=(v=g.card)==null?void 0:v.preferences)==null?void 0:O.datasetOptions}),r=d==null?void 0:d.find(g=>g.idx===n),{setCardPreferences:i}=s.useEditorStore(g=>g.actions),o=(r==null?void 0:r.lineStyle)||"solid",u=(r==null?void 0:r.pointRadius)??3,m=(r==null?void 0:r.pointStyle)||"circle",x=((p=r==null?void 0:r.dataLabels)==null?void 0:p.enabled)??!1,h=Ge.find(g=>g.value===o)||Ge[0];function w(g){const v={idx:n,lineStyle:o,pointRadius:u,pointStyle:m,dataLabels:r==null?void 0:r.dataLabels,...g};if(!r)i({...c||{},datasetOptions:[...d||[],v]});else{const O=d==null?void 0:d.map(A=>A.idx===n?{...A,...v}:A);i({...c||{},datasetOptions:O})}}function j(g){w({dataLabels:{...r==null?void 0:r.dataLabels,enabled:g}})}function f(g){w({dataLabels:g})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.TrendingUp,{className:"h-3 w-3"}),h.label.split(" ")[0]]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:o,onValueChange:g=>w({lineStyle:g}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ge.map(({value:g,label:v})=>e.jsx(s.SelectItem,{value:g,className:"text-xs",children:v},g))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String(u),onValueChange:g=>w({pointRadius:Number(g)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Xa.map(g=>e.jsx(s.SelectItem,{value:String(g),className:"text-xs",children:g===0?"None":g},g))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:m,onValueChange:g=>w({pointStyle:g}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Za.map(({value:g,label:v})=>e.jsx(s.SelectItem,{value:g,className:"text-xs",children:v},g))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:x,onCheckedChange:j})]}),x&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Be,{config:r==null?void 0:r.dataLabels,onChange:f,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const an=[0,2,4,6,8,10,12,16,20];function nn(){const t=s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type});return!t||!["bar","horizontalBar","stackedBar"].includes(t)?null:e.jsxs(s.AccordionItem,{value:"bar-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(rn,{})})]})}function rn(){const{transformedMetricKeys:t}=Fe();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(ln,{metricKey:n,idx:a},n))})}function ln({metricKey:t,idx:n}){var j;const[a,l]=L.useState(!1),c=s.useEditorStore(f=>{var p;return(p=f.card)==null?void 0:p.type}),d=s.useEditorStore(f=>{var p;return(p=f.card)==null?void 0:p.preferences}),r=s.useEditorStore(f=>{var p,g;return(g=(p=f.card)==null?void 0:p.preferences)==null?void 0:g.datasetOptions}),i=r==null?void 0:r.find(f=>f.idx===n),{setCardPreferences:o}=s.useEditorStore(f=>f.actions),u=(i==null?void 0:i.borderRadius)??0,m=((j=i==null?void 0:i.dataLabels)==null?void 0:j.enabled)??!1;function x(f){const p={idx:n,borderRadius:u,dataLabels:i==null?void 0:i.dataLabels,...f};if(!i)o({...d||{},datasetOptions:[...r||[],p]});else{const g=r==null?void 0:r.map(v=>v.idx===n?{...v,...p}:v);o({...d||{},datasetOptions:g})}}function h(f){x({dataLabels:{...i==null?void 0:i.dataLabels,enabled:f}})}function w(f){x({dataLabels:f})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:u>0&&e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.ChartColumn,{className:"h-3 w-3"}),"r:",u]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Bar Style"}),e.jsx("div",{className:"space-y-3",children:e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Corner Radius"}),e.jsxs(s.Select,{value:String(u),onValueChange:f=>x({borderRadius:Number(f)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:an.map(f=>e.jsx(s.SelectItem,{value:String(f),className:"text-xs",children:f===0?"None":`${f}px`},f))})]})]})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:m,onCheckedChange:h})]}),m&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Be,{config:i==null?void 0:i.dataLabels,onChange:w,isHorizontal:c==="horizontalBar",showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const on=[0,1,2,3,4,5,6,8,10],cn=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],dn=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function un(){const t=s.useEditorStore(r=>r.card),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),a=n==null?void 0:n.lineStyleConfig,{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!["line","area","stackedArea"].includes(t.type))return null;const d=(r,i)=>{const o={...n||{},lineStyleConfig:{...a,[r]:i}};l(o),c({...t,preferences:o})};return e.jsxs(s.AccordionItem,{value:"line-style",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(vt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Line Style"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Default styles for all series. Override per-series in the Series section."}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Pattern"}),e.jsxs(s.Select,{value:(a==null?void 0:a.lineStyle)||"solid",onValueChange:r=>d("lineStyle",r),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:dn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Point Size"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.pointRadius)??3),onValueChange:r=>d("pointRadius",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:on.map(r=>e.jsx(s.SelectItem,{value:String(r),className:"text-xs",children:r===0?"None":r},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Point Shape"}),e.jsxs(s.Select,{value:(a==null?void 0:a.pointStyle)||"circle",onValueChange:r=>d("pointStyle",r),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:cn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]})]})})]})}const mn=[0,2,4,6,8,10,12,16,20],xn=[{value:"0.5",label:"50%"},{value:"0.6",label:"60%"},{value:"0.7",label:"70%"},{value:"0.8",label:"80%"},{value:"0.9",label:"90%"},{value:"1",label:"100%"}],hn=[{value:"0.5",label:"50%"},{value:"0.6",label:"60%"},{value:"0.7",label:"70%"},{value:"0.8",label:"80%"},{value:"0.9",label:"90%"},{value:"1",label:"100%"}];function pn(){const t=s.useEditorStore(r=>r.card),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),a=n==null?void 0:n.barStyleConfig,{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!["bar","horizontalBar","stackedBar"].includes(t.type))return null;const d=(r,i)=>{const o={...n||{},barStyleConfig:{...a,[r]:i}};l(o),c({...t,preferences:o})};return e.jsxs(s.AccordionItem,{value:"bar-style",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.ChartColumn,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Bar Style"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Default styles for all bars. Override per-series in the Series section."}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Corner Radius"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.borderRadius)??0),onValueChange:r=>d("borderRadius",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:mn.map(r=>e.jsx(s.SelectItem,{value:String(r),className:"text-xs",children:r===0?"None":`${r}px`},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Bar Width"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.barPercentage)??.9),onValueChange:r=>d("barPercentage",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:xn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Category Width"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.categoryPercentage)??.8),onValueChange:r=>d("categoryPercentage",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:hn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]})]})})]})}function gn(){const t=s.useEditorStore(a=>{var l,c;return(c=(l=a==null?void 0:a.card)==null?void 0:l.preferences)==null?void 0:c.sortChart}),{setSortChart:n}=s.useEditorActions();return e.jsxs(s.AccordionItem,{value:"stacked-sort",children:[e.jsx(s.AccordionTrigger,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.ArrowUpDown,{className:"h-4 w-4"}),"Sort"]})}),e.jsx(s.AccordionContent,{className:"space-y-4 px-1",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Sort Order"}),e.jsx(pe,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"Ascending",value:"asc"},{label:"Descending",value:"desc"}],value:t||"none",onValueChange:a=>{n(a)}})]})})]})}function qs({mode:t}){const n=s.useEditorStore(a=>{var l;return(l=a.card)==null?void 0:l.type});return e.jsxs(e.Fragment,{children:[n==="combo"&&e.jsx(qa,{}),(n==="area"||n==="stackedArea")&&e.jsx(Ya,{}),n==="line"&&e.jsx(en,{}),(n==="bar"||n==="horizontalBar"||n==="stackedBar")&&e.jsx(nn,{}),e.jsx(un,{}),e.jsx(pn,{}),e.jsx(Ea,{}),t==="config"&&e.jsx(Ta,{}),t==="config"&&e.jsx(wa,{}),e.jsx(Ia,{}),n==="bullet"&&e.jsx(Aa,{}),n==="funnel"&&e.jsx(Ma,{}),(n==="bubble"||n==="scatter")&&e.jsx(Da,{}),(n==="stackedBar"||n==="stackedLine")&&e.jsx(gn,{}),e.jsx(ga,{}),e.jsx(ya,{}),e.jsx(Na,{}),e.jsx(Ca,{})]})}function fn(){const t=s.useEditorStore(a=>a.card),n=t==null?void 0:t.type;return n?e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Re,{chartType:n}),e.jsx(s.Accordion,{type:"single",collapsible:!0,className:"w-full px-2",children:e.jsx(qs,{mode:"dev"})})]}):null}function jn(){const t=s.useEditorStore(a=>a.card.type);function n(){switch(t){case"kpi":return e.jsx(aa,{});case"table":return e.jsx(da,{});case"map":return e.jsx(oa,{});case"custom":return e.jsx("div",{className:"px-6 py-2",children:e.jsx(Vs,{})});case"text":return e.jsx(ma,{});default:return e.jsx(fn,{})}}return e.jsxs(s.ScrollArea,{className:"h-full w-full text-sm",children:[n(),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function bn(){const t=L.useRef(null),n=L.useRef(null),a=s.useEditorStore(c=>c.isShowingVisual),l=s.useEditorStore(c=>c.showAIDialog);return L.useEffect(()=>{var c,d,r,i;l?((c=t.current)==null||c.resize(0),(d=n.current)==null||d.resize(0)):((r=t.current)==null||r.resize(25),(i=n.current)==null||i.resize(25))},[l]),e.jsxs(s.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:[e.jsx(s.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(Rt,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:a&&!l?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:t,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(jn,{})})}),!l&&e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(us,{})})]}):e.jsx(us,{})})]})}function vn(t){var ue;s.useAIInteractionStore(K=>K.currentAIContext);const[n,a]=L.useState(""),[l,c]=L.useState(null),{authToken:d,tokenProps:r}=s.useSemaphorContext(),i=s.useEditorStore(K=>{var ee;return(ee=K==null?void 0:K.card)==null?void 0:ee.sql}),o=s.useEditorStore(K=>K.isDevMode),u=s.useEditorStore(K=>{var ee;return(ee=K==null?void 0:K.card)==null?void 0:ee.python}),{setCardPython:m,setCardSql:x,setRunSql:h,switchCardType:w,setQueryConfig:j}=s.useEditorActions(),f=s.useEditorStore(K=>K.currentAssitantScope)||[],p=s.useEditorStore(K=>K.userInputForAI),g=s.useEditorStore(K=>K.triggerAIRun),v=s.useEditorStore(K=>K.card),O=s.useEditorStore(K=>K.selectedDatabaseName),A=s.useEditorStore(K=>K.selectedTableName),y=s.useEditorStore(K=>K.selectedConnectionId),T=s.useEditorStore(K=>K.selectedSchemaName),N=s.useDashboardStore(K=>K.dashboard.aiScopeTables),{errorMessage:C,refetch:k,data:R}=s.useDashboardCardQuery(v),{setTriggerAIRun:b,setCurrentAssitantScope:S,setCardConfig:D}=s.useEditorActions(),{selectedDatamodelName:F,connectionType:I,selectedDatamodelId:M}=s.useEditorAside(),E=(ue=s.useDashboardQuery().data)==null?void 0:ue.assistantProfileId,{messages:B,setMessages:$,regenerate:V,error:q,sendMessage:G,status:z,addToolResult:H,stop:_,clearError:P}=s.useChat({transport:new s.DefaultChatTransport({api:`${r.apiServiceUrl}/v1/assistant`,headers:()=>({Authorization:`Bearer ${d==null?void 0:d.accessToken}`}),body:()=>{const K=s.useAIInteractionStore.getState();return{random:Math.random(),assistantId:"editor-assistant",assistantProfileId:E,isDevMode:o,aiContext:K.currentAIContext,reasoningEffort:K.reasoningEffort,showReasoning:K.showReasoning}},credentials:"omit",prepareSendMessagesRequest:({messages:K,body:ee})=>({body:{...ee,messages:s.cleanupIncompleteMessages(K)}})}),onError:K=>{var ee;if(K.name==="AbortError"||(ee=K.message)!=null&&ee.includes("aborted")){console.log("[Editor Assistant] Stream aborted (expected)");return}console.error("[Editor Assistant] Stream error:",K)},onToolCall:Y,onFinish({isAbort:K,isDisconnect:ee,isError:de}){a(""),console.log(K?"[Editor Assistant] Stream aborted by user":ee?"[Editor Assistant] Stream disconnected (network error)":de?"[Editor Assistant] Stream error occurred":"[Editor Assistant] Stream completed successfully")}});L.useEffect(()=>{g&&p&&(G({role:"user",parts:[{type:"text",text:p}]}),b(!1))},[g,p,G,b]);async function Y({toolCall:K}){var ee,de,me,Se,xe,je,he,be,we,ge,Te,Ee,ke,Ie,ve,ye;if(["createChartFromSql","createForecast"].includes(K.toolName)){const J=((ee=K.input)==null?void 0:ee.sql)??((de=K.args)==null?void 0:de.sql),ne=((me=K.input)==null?void 0:me.python)??((Se=K.args)==null?void 0:Se.python),re=((xe=K.input)==null?void 0:xe.chartType)??((je=K.args)==null?void 0:je.chartType);w(re),x(J),m(ne),h(!0),H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",message:"Let the user know that chart is created"}})}else if(K.toolName==="getUserSql"){if(!i){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"error",message:"No SQL found"}});return}H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",sql:i,python:u||""}})}else if(K.toolName==="generateQueryConfig"){a("Generating query config...");const J=((he=K.input)==null?void 0:he.chartType)??((be=K.args)==null?void 0:be.chartType),ne=((we=K.input)==null?void 0:we.queryConfig)??((ge=K.args)==null?void 0:ge.queryConfig);((Te=K.input)==null?void 0:Te.cardConfig)??((Ee=K.args)==null||Ee.cardConfig),w(J),D(ne),x(void 0),m(void 0);const re=await k();if((ke=re==null?void 0:re.data)!=null&&ke.error){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"error",queryConfig:ne,message:(Ie=re==null?void 0:re.data)==null?void 0:Ie.error.message}});return}if((ve=re==null?void 0:re.data)!=null&&ve.records){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",queryConfig:ne,message:"The data has been visualized"}});return}H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",queryConfig:ne,message:"The data has been visualized",records:(ye=re==null?void 0:re.data)==null?void 0:ye.records}})}else a("Analyzing...")}function Z(K){const ee=s.removeFromScopeArray(f,K);se(ee)}function se(K){const ee=s.sanitizeAIScope(K);S(ee),$(de=>[...de,{id:crypto.randomUUID(),role:"system",parts:[{type:"text",text:`Current scope of tables. Always use the table reference format when generating SQL | ${JSON.stringify(ee)}`}]}])}function W(){const K={databaseName:O||"",schemaName:T||"",tableName:A||"",datamodelId:M||"",datamodelName:s.resolveDatamodelName(F),connectionId:y||"",connectionType:I||""},ee=s.dedup([...f,K]);se(ee)}function ae(){const K={databaseName:O||"",schemaName:T||"",tableName:A||"",datamodelName:s.resolveDatamodelName(F),connectionId:y||"",connectionType:I||"",datamodelId:M||""};if(!s.isInAiScopeArray([...N||[],...f],K)||f.length===0){const de=s.dedup([...N||[],...f,K]).filter(me=>me.connectionId===K.connectionId);se(de)}}const Q=!["ready","error"].includes(z),ce=s.getErrorMessage(q);return{messages:B,setMessages:$,sendMessage:G,regenerate:V,isLoading:Q,handleSetAIScope:ae,handleAddCurrentTableToScope:W,removeFromCurrentSelections:Z,currentAssitantScope:f,setCurrentAssitantScope:S,stop:_,status:z,toolStatus:n,llmUsage:l,onFinish:t,errorMessage:ce,clearError:P}}const yn=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function vs({isFullscreen:t,onFullscreenChange:n,fullscreenContainer:a}){const l=L.useRef(null),[c,d]=L.useState(!1),[r,i]=L.useState(s.isDevEnv),[o,u]=L.useState(!1),[m,x]=L.useState(!1),[h,w]=L.useState(!1),j=t??h,f=s.useCurrentAIContext(),{setCurrentAIContext:p}=s.useAIActions(),{clearQueue:g}=s.useQueueActions(),{clearFileAttachments:v}=s.useFileActions(),O=s.useFileAttachments("editor"),A=s.useDashboardStore(J=>J.dashboard.aiContext),y=s.useEditorStore(J=>J.card.dataSource),T=s.useEditorStore(J=>J.actions.setCardSql),N=s.useEditorStore(J=>J.actions.setRunSql),C=s.useEditorStore(J=>J.showAIDialog),k=s.useDashboardStore(J=>J.handleOpenAssistantProfile),{setShowAIDialog:R,setCardPython:b}=s.useEditorActions(),{setDataSource:S}=s.useEditorActions(),{assistantProfile:D}=s.useAssistantProfile(),{messages:F,setMessages:I,sendMessage:M,isLoading:E,stop:B,status:$,llmUsage:V,errorMessage:q,clearError:G}=vn(),[z,H]=L.useState(""),_=J=>{H(J.target.value)},P=J=>{J.preventDefault();const ne=z.trim();if(!ne&&O.length===0)return;const re=[];ne&&re.push({type:"text",text:ne}),O.forEach(Ne=>{Ne.data&&re.push({type:"file",mediaType:Ne.type,filename:Ne.name,url:`data:${Ne.type};base64,${Ne.data}`})}),M({role:"user",parts:re}),H(""),v("editor"),ee({force:!0})},{visibleMessages:Y,hasMoreMessages:Z,totalMessageCount:se,loadMoreMessages:W,resetMessageWindow:ae}=s.useMessageWindow(F,{maxVisibleMessages:15,initialLoadCount:10,loadMoreCount:5}),{scrollRef:Q,isAtBottom:ce,hasNewMessages:ue,newMessageCount:K,scrollToBottom:ee,handleScroll:de,resetNewMessageCount:me}=s.useAutoScroll(Y,{threshold:50,behavior:"smooth",isStreaming:E});L.useEffect(()=>{var J;C&&((J=l.current)==null||J.focus(),p({selectedEntities:(y==null?void 0:y.selectedEntities)||[]}))},[C,y,p]),s.useAutoSubmitQueue({assistantType:"editor",status:$,isLoading:E,sendMessage:M});const{hasActiveTools:Se}=s.useActiveToolStatus(F),xe=L.useCallback(J=>{n==null||n(J),t===void 0&&w(J)},[t,n]);L.useEffect(()=>{if(!j)return;function J(ne){ne.key==="Escape"&&xe(!1)}return window.addEventListener("keydown",J),()=>{window.removeEventListener("keydown",J)}},[j,xe]);const je=L.useCallback(J=>{M({role:"user",parts:[{type:"text",text:J}]}),ee({force:!0})},[M,ee]),he=L.useCallback(async()=>{const J=JSON.stringify(F??[],null,2);try{if(typeof navigator>"u"||!navigator.clipboard)throw new Error("Clipboard API unavailable");await navigator.clipboard.writeText(J),s.ue.success("Conversation copied")}catch(ne){console.error("Failed to copy conversation",ne),s.ue.error("Unable to copy conversation")}},[F]),be=L.useCallback(()=>{xe(!j)},[j,xe]);function we(J,ne){ne==="sql"?(T(J),N(!0)):ne==="python"&&(b(J),N(!0))}function ge(){f.selectedEntities.length===0&&p({selectedEntities:(A==null?void 0:A.selectedEntities)||[]})}function Te(J){ge();const ne={role:"user",parts:[{type:"text",text:J.title+" "+J.subtitle}]};M(ne)}function Ee(J){ge(),M({role:"user",parts:[{type:"text",text:J}]})}function ke(){return e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.Button,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",variant:"ghost",children:e.jsx(s.EllipsisVertical,{className:"size-3.5"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",children:[e.jsx(s.DropdownMenuLabel,{children:"Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx(s.DropdownMenuCheckboxItem,{checked:c,onCheckedChange:d,children:"Token Usage"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:r,onCheckedChange:i,children:"Tool Invocations"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:u,children:"System Message"})]})]})}function Ie(){return e.jsx("div",{"data-role":"header",children:e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"})})}const ve=(z==null?void 0:z.length)===0&&(F==null?void 0:F.filter(J=>J.role==="user").length)===0,ye=e.jsxs("div",{"data-role":"editor-assistant",className:s.cn("flex h-full w-full flex-col justify-between px-6 py-4",j&&"rounded-2xl border border-border/60 bg-background px-8 pb-6 pt-6 shadow-xl"),children:[e.jsxs("div",{"data-role":"header-section",className:"flex items-start justify-between gap-2",children:[Ie(),k&&e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-7 p-2",onClick:k,children:(D==null?void 0:D.name)||"Default Profile"}),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center",children:[ke(),r&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"ml-2 h-7 px-2 text-muted-foreground hover:text-foreground",onClick:he,children:"Copy Conversation"}),e.jsx(s.IconButton,{tooltip:j?"Exit Fullscreen":"Expand",className:"h-7 p-2 text-muted-foreground hover:text-foreground",onClick:be,children:j?e.jsx(s.Minimize2,{className:"size-4"}):e.jsx(s.Maximize2,{className:"size-4"})}),e.jsx(s.IconButton,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",tooltip:"New Chat",onClick:()=>{I([]),g("editor"),G(),ae()},children:e.jsx(s.SquarePen,{className:"size-4"})}),e.jsx(s.Button,{variant:"ghost",className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",onClick:()=>{R(!1)},children:e.jsx(s.X,{className:"size-4"})})]})]}),e.jsx(s.CustomScrollArea,{ref:Q,className:"mt-2 flex w-full grow basis-0",onScroll:de,children:e.jsxs("div",{className:"space-y-4 py-2 pr-3","data-role":"messages",children:[Z&&e.jsx("div",{className:"flex justify-center py-2",children:e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:W,className:"text-xs",children:["Load ",Math.min(5,se-Y.length)," ","more messages (",se-Y.length," ","hidden)"]})}),e.jsx(s.PrintMessages,{messages:Y,showTools:r,handleRun:we,showSystemMessage:o,onQuickReply:je,disableQuickReplies:E}),!Se&&e.jsx(s.AssistantStatus,{status:$,errorMessage:q})]})}),C&&!ce&&e.jsx("div",{className:"absolute bottom-24 right-8 z-10",children:e.jsxs(s.Button,{onClick:()=>{ee({force:!0}),me()},size:"sm",variant:"secondary",className:"rounded-full shadow-lg","aria-label":"Scroll to bottom",children:[e.jsx(s.ChevronDown,{className:"h-4 w-4"}),ue&&K>0&&e.jsx("span",{className:"ml-1.5 flex h-5 w-5 items-center justify-center rounded-full bg-primary text-xs text-primary-foreground",children:K>9?"9+":K})]})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[ve&&!D&&e.jsx(s.Placeholders,{onClick:Te,placeholders:yn}),e.jsx("div",{children:ve&&D&&e.jsx(s.SeedQuestions,{seedQuestions:D.seedQuestions||[],handleSeedQuestionClick:Ee})})]}),e.jsx(s.PendingMessagesDisplay,{assistantType:"editor",className:"mb-3"}),e.jsx(s.ChatInputWithContext,{ref:l,setInput:H,stop:B,input:z,handleInputChange:_,handleSubmit:J=>{ge(),P(J),ee({force:!0})},isLoading:E,status:$,assistantType:"editor",onFocus:()=>{}})]}),V&&c&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",V.inputTokens," prompt tokens, ",V.outputTokens," ","completion tokens, ",V.totalTokens," total tokens"]})}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(y==null?void 0:y.selectedEntities)||[],open:m,onOpenChange:x,dataSource:y||s.EMPTY_DATA_SOURCE,dialogMode:"aiContext"})]});return j&&a?Xs.createPortal(e.jsx("div",{className:"pointer-events-auto absolute inset-0 z-50 flex bg-background px-6 pb-6 pt-[calc(var(--app-header-height,15px)+1rem)]",children:e.jsx("div",{className:"flex h-full w-full",children:ye})}),a):ye}function Nn({selectedChart:t,onChartChange:n}){const[a,l]=L.useState(!1),c=s.useEditorStore(i=>i.card),d=()=>{var o;if(t==="custom")return((o=c==null?void 0:c.customCardPreferences)==null?void 0:o.componentName)||"Custom Visual";const i=s.chartTypes.find(u=>u.id===t);return(i==null?void 0:i.label)||"Select chart type"},r=()=>{var o;if(t==="custom"){const u=(o=c==null?void 0:c.customCardPreferences)==null?void 0:o.componentName;if(u){const m=s.getAutoDetectedChartIcon(u);return m||e.jsx(s.ChartIcon,{chartName:u,isCustom:!0,className:"h-4 w-4"})}return e.jsx(s.Shapes,{className:"h-4 w-4"})}const i=s.chartTypes.find(u=>u.id===t);if(i){const u=i.icon;return e.jsx(u,{className:"h-4 w-4"})}return e.jsx(s.Shapes,{className:"h-4 w-4"})};return e.jsx("div",{children:e.jsxs(s.Popover,{open:a,onOpenChange:l,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":a,className:"w-full justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[r(),e.jsx("span",{children:d()})]}),e.jsx(s.ChevronDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[360px] p-3",align:"start",onOpenAutoFocus:i=>i.preventDefault(),children:e.jsx(Fs,{onChartSelect:i=>n(i),onClose:()=>l(!1)})})]})})}let Cn=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce((n,a)=>(a&=63,a<36?n+=a.toString(36):a<62?n+=(a-26).toString(36).toUpperCase():a>62?n+="-":n+="_",n),"");const Oe=[{id:"sum",label:"Sum",symbol:"+",description:"Add two metrics together",template:()=>"({field_0} + {field_1})",requiresTwoInputs:!0},{id:"difference",label:"Difference",symbol:"−",description:"Subtract one metric from another",template:()=>"({field_0} - {field_1})",requiresTwoInputs:!0},{id:"product",label:"Product (Multiplication)",symbol:"×",description:"Multiply two metrics",template:()=>"({field_0} * {field_1})",requiresTwoInputs:!0},{id:"ratio",label:"Ratio",symbol:"÷",description:"Divide one metric by another",template:()=>"({field_0} / NULLIF({field_1}, 0))",requiresTwoInputs:!0},{id:"change",label:"Change",symbol:"Δ",description:"Calculate percentage change from A to B",template:()=>"(({field_1} - {field_0}) / NULLIF({field_0}, 0))",requiresTwoInputs:!0},{id:"percentage",label:"Percentage",symbol:"%",description:"Calculate (A / B) × 100",template:()=>"(({field_0} / NULLIF({field_1}, 0)) * 100)",requiresTwoInputs:!0},{id:"scale",label:"Scale (Multiply by Factor)",symbol:"×n",description:"Multiply a metric by a constant factor",template:()=>"({field_0} * {constant})",requiresTwoInputs:!1},{id:"custom",label:"Custom Expression",symbol:"{}",description:"Drag fields to create custom expression with tokens",requiresTwoInputs:!1,allowsCustomExpression:!0}];function Sn(t,n,a,l,c,d,r,i,o){const u=Oe.find(f=>f.id===n);if(!u)throw new Error(`Unknown operation: ${n}`);let m,x=[],h;if(n==="custom"){if(!(i!=null&&i.trim()))throw new Error("Custom expression is required");m=i.trim()}else if(n==="scale"){if(!a)throw new Error("Metric A is required for scale operation");if(o===void 0||o===0)throw new Error("Constant factor is required for scale operation");m=u.template().replace("{constant}",String(o)),x=[a],h=o}else{if(!a||!l)throw new Error("Metrics A and B are required for standard operations");m=u.template(),x=[a,l]}const w={operation:n,inputFields:x,...h!==void 0&&{constant:h}};return{id:r||`calc_${Cn(10)}`,name:t.toLowerCase().replace(/\s+/g,"_"),label:t,alias:t.toLowerCase().replace(/\s+/g,"_"),qualifiedFieldName:t.toLowerCase().replace(/\s+/g,"_"),dataType:"number",role:"metric",aggregate:"SUM",expression:m,calculatedFormula:w,entityId:d,entityName:s.CALCULATED_FIELD_ENTITY_MARKER,entityType:"table",description:`Calculated field: ${t}`}}function ys(t){var l,c;const n=Oe.find(d=>d.id===t.operation);if(!n)return"Unknown operation";if(t.operation==="custom")return"Custom expression";if(t.operation==="scale"){const d=((l=t.inputFields[0])==null?void 0:l.label)||((c=t.inputFields[0])==null?void 0:c.name)||"A",r=t.constant??1;return`${d} × ${r}`}return t.inputFields.map(d=>d.label||d.name).join(` ${n.symbol} `)}const Xe=({dataType:t})=>s.getFieldIconComponent(t);function wn({aliasTemplate:t,onAliasTemplateChange:n}){var o;const[a,l]=L.useState(t),[c,d]=L.useState(!1),r=(o=s.useEditorStore(u=>{var m;return(m=u.card.config)==null?void 0:m.pivotByColumns}))==null?void 0:o[0],i=r?`e.g. Min Sales in {{${r.name}}}`:"e.g. Min Sales in {{region}}";return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-gray-600",children:"Pivot Alias Template"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Input,{placeholder:i,value:c?a:t,onChange:u=>{l(u.target.value),d(!0)},onFocus:()=>d(!0),onPointerDown:u=>u.stopPropagation(),className:"h-8 text-xs",onKeyDown:u=>{u.key==="Enter"&&c&&a!==t?(n(a),d(!1)):u.key==="Escape"&&(l(t),d(!1))},"aria-label":"Edit alias template"}),c&&a!==t&&e.jsxs(e.Fragment,{children:[e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm alias template",onClick:()=>{n(a),d(!1)},children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel alias template edit",onClick:()=>{l(t),d(!1)},children:e.jsx(s.X,{className:"h-4 w-4 text-gray-400"})})]})]})]})}function Tn({valueAliases:t,onValueAliasesChange:n}){const[a,l]=L.useState(null),[c,d]=L.useState(""),[r,i]=L.useState(""),o=()=>{n({...t,"":""})},u=x=>{const h={...t};delete h[x],n(h)},m=x=>{const h={...t};delete h[x],h[c]=r,n(h),l(null)};return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Pivot Value Aliases"}),e.jsxs("div",{className:"space-y-2",children:[Object.entries(t).map(([x,h],w)=>e.jsx("div",{className:"flex items-center gap-2",children:a===x?e.jsxs(e.Fragment,{children:[e.jsx(s.Input,{placeholder:"Value key",value:c,onChange:j=>d(j.target.value),className:"h-8 w-24 text-xs",onPointerDown:j=>j.stopPropagation(),onKeyDown:j=>{j.key==="Enter"?m(x):j.key==="Escape"&&l(null)},"aria-label":"Edit value alias key"}),e.jsx(s.Input,{placeholder:"Alias label",value:r,onChange:j=>i(j.target.value),className:"h-8 w-24 text-xs",onPointerDown:j=>j.stopPropagation(),onKeyDown:j=>{j.key==="Enter"?m(x):j.key==="Escape"&&l(null)},"aria-label":"Edit value alias label"}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm value alias edit",onClick:()=>m(x),children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel value alias edit",onClick:()=>l(null),children:e.jsx(s.X,{className:"h-4 w-4 text-muted-foreground"})})]}):e.jsxs(e.Fragment,{children:[e.jsx(s.Input,{placeholder:"Value key",value:x,className:"h-8 w-24 text-xs",readOnly:!0,onClick:()=>{l(x),d(x),i(h)},"aria-label":"Edit value alias key"}),e.jsx(s.Input,{placeholder:"Alias label",value:h,className:"h-8 w-24 text-xs",readOnly:!0,onClick:()=>{l(x),d(x),i(h)},"aria-label":"Edit value alias label"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-8 w-8 p-0",onClick:()=>u(x),"aria-label":"Remove value alias",children:e.jsx(s.X,{className:"h-3 w-3"})})]})},x+w)),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"mt-1 h-8 text-xs",onClick:o,children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Value Alias"]})]})]})}function En({idx:t}){var i;const n=s.useEditorStore(o=>{var u;return(u=o.card)==null?void 0:u.preferences}),a=s.useEditorStore(o=>{var u;return(u=o.card.preferences)==null?void 0:u.datasetOptions}),l=s.useEditorStore(o=>o.actions.setCardPreferences),c=(i=n==null?void 0:n.chartOptions)==null?void 0:i.indexAxis,d=a==null?void 0:a.find(o=>o.idx===t);function r(o,u){(o==="end"||o==="start")&&s.getContrastColor(s.getCanvasBackgroundColor());const m={display:o!=="none",align:o,anchor:o,clamp:!0};if(!d)l({...n,datasetOptions:[...a||[],{idx:u,datalabels:m}]});else{const x={...d};x.datalabels=m;const h=a==null?void 0:a.map(w=>w.idx===u?x:w);l({...n,datasetOptions:h})}}return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{children:e.jsx(s.Tag,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>r("none",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(s.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>r("start",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(xt,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})}),e.jsx("button",{onClick:()=>r("center",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(gt,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})}),e.jsx("button",{onClick:()=>r("end",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(pt,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})})]})})]})}function Ze({children:t,className:n,delayDuration:a}){return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:a,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:s.cn("overflow-hidden text-ellipsis whitespace-nowrap",n),children:t})}),e.jsx(s.TooltipContent,{children:t})]})})}function kn(t,n=30){return t.length>n?t.slice(0,n)+"...":t}function In({col:t,idx:n,className:a,iconClassName:l,children:c}){const d=s.useEditorStore(m=>{var x;return(x=m.card)==null?void 0:x.preferences}),r=s.useEditorStore(m=>{var x,h;return(h=(x=m.card)==null?void 0:x.preferences)==null?void 0:h.datasetOptions}),i=r==null?void 0:r.find(m=>m.idx===n),{setCardPreferences:o}=s.useEditorStore(m=>m.actions);function u(m){if(!i)o({...d,datasetOptions:[...r||[],{idx:n,type:m==="area"?"line":m,fill:m==="area"?"origin":""}]});else{const x={...i};x.type=m==="area"?"line":m,x.fill=m==="area"?"origin":"";const h=r==null?void 0:r.map(w=>w.idx===n?x:w);o({...d,datasetOptions:h})}}return e.jsxs("div",{className:s.cn("group flex items-center justify-between gap-2 rounded-md",a),children:[c||e.jsx(Ze,{className:"max-w-[100px]",title:t,children:kn(t)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(s.ChartColumnIncreasing,{onClick:()=>u("bar"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(s.ChartLine,{onClick:()=>u("line"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(s.ChartArea,{onClick:()=>u("area"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(En,{idx:n})]})]})}function An(){const t=s.useEditorStore(i=>i.card.type),n=s.useEditorStore(i=>i.isDevMode),a=s.useEditorStore(i=>i.card.preferences),{setCardPreferences:l}=s.useEditorStore(i=>i.actions);function c(i){var m;const o={datalabels:{display:i!=="none",align:i,anchor:i,clamp:!0}},u={...a,chartOptions:{...a==null?void 0:a.chartOptions,plugins:{...(m=a==null?void 0:a.chartOptions)==null?void 0:m.plugins,...o}}};l(u)}function d(){var i,o,u;return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(s.Select,{value:((u=(o=(i=a==null?void 0:a.chartOptions)==null?void 0:i.plugins)==null?void 0:o.datalabels)==null?void 0:u.anchor)||"none",onValueChange:c,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Data Labels"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"start",children:"Start"}),e.jsx(s.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"end",children:"End"})]})})]})]})}function r(){var i,o,u;return e.jsx("div",{children:e.jsxs(s.Select,{value:((u=(o=(i=a==null?void 0:a.chartOptions)==null?void 0:i.plugins)==null?void 0:o.datalabels)==null?void 0:u.anchor)||"none",onValueChange:c,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"start",children:"Start"}),e.jsx(s.SelectItem,{className:"text-xs",value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"end",children:"End"})]})})]})})}return e.jsx(e.Fragment,{children:n?d():r()})}function Dn({column:t}){const n=s.useEditorStore(o=>o.card.type),a=s.useEditorStore(o=>{var u,m;return(m=(u=o.card)==null?void 0:u.config)==null?void 0:m.metricColumns}),l=(a==null?void 0:a.map(o=>o.label))||[],c=l==null?void 0:l.find(o=>o===t.label||o===t.name),d=c?l.indexOf(c):-1;function r(){return e.jsx("div",{className:"pr-1",children:c&&e.jsx(In,{idx:d,col:c,children:e.jsx("div",{className:"w-[130px] text-xs text-muted-foreground",children:"Display Series As"})})})}function i(){return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Data Labels"}),e.jsx(An,{})]})}return e.jsxs("div",{className:"space-y-1",children:[["stackedBar","stackedLine","bar","line"].includes(n)&&r(),["pie","doughnut"].includes(n)&&i()]})}function Ln({field:t,searchQuery:n="",limit:a=500}){const{authToken:l,tokenProps:c}=s.useSemaphorContext(),d=s.useEditorStore(u=>u.card),r=d==null?void 0:d.dataSource,i=["attribute-values",t.entityName,t.name,n,a],o=s.useQuery({queryKey:i,queryFn:async()=>{if(!(r!=null&&r.connectionId))return console.warn("No connection ID available for attribute values query"),[];try{const u={groupByColumns:[{...t,role:"groupby"}],metricColumns:[],filters:n?{id:"search-filter",logicalOperator:"AND",negate:!1,conditions:[{id:"search-condition",field:t,operator:"contains",value:n}],groups:[]}:void 0,rowLimit:a},m={connection_id:r.connectionId,cardConfig:u,cardDataSource:r,cache_config:s.getCacheConfig({card:d})};console.log("requestBody",m);const x=await s.postRequestRegular(`${c.apiServiceUrl}/v1/query`,(l==null?void 0:l.accessToken)||"",m);if(x!=null&&x.error)return console.error("Error fetching attribute values:",x.error),[];const h=t.name;return((x==null?void 0:x.records)||[]).map(j=>{const f=j[h]||j[t.alias||h]||j[t.label];return{id:f||"",value:f||"",label:String(f||"(empty)")}})}catch(u){return console.error("Error fetching attribute values:",u),[]}},enabled:!!t&&!!(r!=null&&r.connectionId),staleTime:5*60*1e3,gcTime:10*60*1e3});return{values:o.data||[],isLoading:o.isLoading,isFetching:o.isFetching,isError:o.isError,error:o.error,refetch:o.refetch}}function Ks({open:t,onOpenChange:n,availableFields:a,existingFilters:l,editingCondition:c,onSave:d}){var O,A;const r=!!c,[i,o]=L.useState(r?"values":"fields"),[u,m]=L.useState(""),[x,h]=L.useState({id:(c==null?void 0:c.id)||s.v4(),field:(c==null?void 0:c.field)||null,selectedValues:c?Array.isArray(c.value)?c.value:[c.value]:[]}),w=a.filter(y=>{var N;return r?!1:!((N=l==null?void 0:l.conditions)==null?void 0:N.some(C=>C.field.id===y.id))&&y.label.toLowerCase().includes(u.toLowerCase())}),j=y=>{h({...x,field:y}),o("values")},f=()=>{if(x.field&&x.selectedValues.length>0){const y={id:x.id,field:x.field,operator:"in",value:x.selectedValues};let T;if(r){const N=((l==null?void 0:l.conditions)||[]).map(C=>C.id===c.id?y:C);T={...l,conditions:N}}else{const N=(l==null?void 0:l.conditions)||[];T={id:(l==null?void 0:l.id)||s.v4(),logicalOperator:"AND",negate:!1,conditions:[...N,y],groups:[]}}d(T),n(!1)}},p=()=>{if(!r||!c)return;const y=((l==null?void 0:l.conditions)||[]).filter(T=>T.id!==c.id);if(y.length>0){const T={...l,conditions:y};d(T)}else d(void 0);n(!1)},g=y=>{h({...x,selectedValues:y})},v=()=>{r&&c?(h({id:c.id,field:c.field,selectedValues:Array.isArray(c.value)?c.value:[c.value]}),o("values")):(h({id:s.v4(),field:null,selectedValues:[]}),o("fields")),m("")};return L.useEffect(()=>{t&&v()},[t,r,c]),e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"max-w-2xl",children:[e.jsx(s.DialogHeader,{children:e.jsx(s.DialogTitle,{children:r?`Edit Filter: ${(O=x.field)==null?void 0:O.label}`:i==="fields"?"Select Field to Filter":`Select Values for ${(A=x.field)==null?void 0:A.label}`})}),e.jsxs("div",{className:"space-y-4",children:[!r&&(l==null?void 0:l.conditions)&&l.conditions.length>0&&i==="fields"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Current Filters:"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:l.conditions.map(y=>e.jsxs("div",{className:"flex items-center gap-1 px-2 py-1 bg-muted rounded-md text-xs",children:[e.jsxs("span",{className:"font-medium",children:[y.field.label,":"]}),e.jsx("span",{className:"text-muted-foreground",children:Array.isArray(y.value)?`${y.value.length} values`:String(y.value)})]},y.id))})]}),i==="fields"&&!r?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search fields...",value:u,onChange:y=>m(y.target.value),className:"pl-10"})]}),e.jsx("div",{className:"grid grid-cols-2 gap-2 max-h-[400px] overflow-y-auto",children:w.map(y=>e.jsxs(s.Button,{variant:"outline",className:"justify-start h-auto py-2 px-3",onClick:()=>j(y),children:[e.jsx(Xe,{dataType:s.getDataType(y.dataType)}),e.jsxs("div",{className:"ml-2 text-left",children:[e.jsx("div",{className:"text-sm font-medium",children:y.label}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[y.entityName,".",y.name]})]})]},y.id))}),w.length===0&&u&&e.jsxs("div",{className:"text-center py-4 text-sm text-muted-foreground",children:['No fields found matching "',u,'"']}),w.length===0&&!u&&a.length>0&&e.jsx("div",{className:"text-center py-4 text-sm text-muted-foreground",children:"All available fields are already filtered"})]}):e.jsx(Mn,{field:x.field,selectedValues:x.selectedValues,onValuesChange:g,onBack:r?void 0:()=>o("fields")}),e.jsxs("div",{className:"flex justify-between pt-4 border-t",children:[e.jsx("div",{children:r?e.jsx(s.Button,{variant:"destructive",onClick:p,children:"Delete Filter"}):e.jsx(s.Button,{variant:"outline",onClick:v,children:"Reset"})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>n(!1),children:"Cancel"}),e.jsx(s.Button,{onClick:f,disabled:!x.field||x.selectedValues.length===0,children:"Apply"})]})]})]})]})})}function Mn({field:t,selectedValues:n,onValuesChange:a,onBack:l}){const[c,d]=L.useState(""),{values:r,isLoading:i,isFetching:o}=Ln({field:t,searchQuery:c,limit:500}),u=c?r.filter(h=>String(h.value).toLowerCase().includes(c.toLowerCase())):r,m=h=>{const w=n.includes(h);a(w?n.filter(j=>j!==h):[...n,h])},x=()=>{n.length===u.length&&u.length>0?a([]):a(u.map(h=>h.value))};return e.jsxs("div",{className:"space-y-4",children:[l&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:l,children:"← Back"}),e.jsxs(s.Label,{className:"text-sm font-medium",children:["Select values for ",t.label]})]}),e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search values...",value:c,onChange:h=>d(h.target.value),className:"pl-10"})]}),i||o?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(s.LoaderCircle,{className:"h-6 w-6 animate-spin"})}):e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 pb-2 border-b",children:[e.jsx(s.Checkbox,{checked:n.length===u.length&&u.length>0,onCheckedChange:x}),e.jsxs(s.Label,{className:"text-sm",children:["Select All (",u.length,")"]})]}),e.jsx("div",{className:"max-h-[300px] overflow-y-auto space-y-1",children:u.map(h=>e.jsxs("div",{className:"flex items-center gap-2 p-2 hover:bg-muted/50 rounded cursor-pointer",onClick:()=>m(h.value),children:[e.jsx(s.Checkbox,{checked:n.includes(h.value),onCheckedChange:()=>m(h.value)}),e.jsx(Ze,{className:"text-sm",children:h.label})]},h.id))})]})]})}function Gs({open:t,onOpenChange:n,availableMetrics:a,existingField:l,onSave:c,cardId:d}){const[r,i]=L.useState(""),[o,u]=L.useState("ratio"),[m,x]=L.useState(null),[h,w]=L.useState(null),[j,f]=L.useState(1),[p,g]=L.useState(""),[v,O]=L.useState({format:"number",decimalPlaces:2}),[A,y]=L.useState({}),T=Oe.find(S=>S.id===o),N=a.find(S=>S.id===m),C=a.find(S=>S.id===h);L.useEffect(()=>{if(l&&t)if(i(l.label),l.calculatedFormula)if(u(l.calculatedFormula.operation),l.calculatedFormula.operation==="custom")g(l.expression||"");else if(l.calculatedFormula.operation==="scale"){const S=l.calculatedFormula.inputFields;if(S.length>=1){const D=a.find(F=>F.qualifiedFieldName===S[0].qualifiedFieldName);x((D==null?void 0:D.id)||null)}l.calculatedFormula.constant!==void 0&&f(l.calculatedFormula.constant)}else{const S=l.calculatedFormula.inputFields;if(S.length>=2){const D=a.find(I=>I.qualifiedFieldName===S[0].qualifiedFieldName),F=a.find(I=>I.qualifiedFieldName===S[1].qualifiedFieldName);x((D==null?void 0:D.id)||null),w((F==null?void 0:F.id)||null)}}else u("custom"),g(l.expression||"")},[l,t,a]);const k=()=>{const S={};return r.trim()||(S.name="Name is required"),o==="custom"?p.trim()||(S.customExpression="Custom expression is required"):o==="scale"?(m||(S.inputA="Please select a metric for input A"),(isNaN(j)||j===0)&&(S.constantFactor="Please enter a valid non-zero number"),l&&m===l.id&&(S.circular="Cannot reference the field being edited")):(m||(S.inputA="Please select a metric for input A"),h||(S.inputB="Please select a metric for input B"),m===h&&(S.inputs="Inputs A and B must be different metrics"),l&&(m===l.id||h===l.id)&&(S.circular="Cannot reference the field being edited")),y(S),Object.keys(S).length===0},R=()=>{if(!k())return;const S=Sn(r,o,N||null,C||null,v,d,l==null?void 0:l.id,o==="custom"?p:void 0,o==="scale"?j:void 0);c(S),b()},b=()=>{i(""),u("ratio"),x(null),w(null),f(1),g(""),y({}),n(!1)};return e.jsxs(s.Dialog,{modal:!1,open:t,onOpenChange:n,children:[t&&e.jsx("div",{className:"pointer-events-none fixed inset-0 z-40 bg-background/80 backdrop-blur-sm"}),e.jsxs(s.DialogContent,{className:"max-w-md max-h-[90vh] overflow-y-auto",onInteractOutside:S=>{S.preventDefault()},children:[e.jsxs(s.DialogHeader,{children:[e.jsxs(s.DialogTitle,{children:[l?"Edit":"Create"," Calculated Metric"]}),e.jsx(s.DialogDescription,{children:"Combine existing metrics to create a new calculated field"})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"name",children:"Name"}),e.jsx(s.Input$1,{id:"name",value:r,onChange:S=>i(S.target.value),placeholder:"e.g., Profit Margin",className:A.name?"border-red-500":""}),A.name&&e.jsx("p",{className:"text-xs text-red-500",children:A.name})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"operation",children:"Outcome is"}),e.jsxs(s.Select,{value:o,onValueChange:S=>{u(S),S==="custom"?(x(null),w(null)):o==="custom"&&g("")},children:[e.jsx(s.SelectTrigger,{id:"operation",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"CALCULATED"}),Oe.map(S=>e.jsx(s.SelectItem,{value:S.id,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-mono text-sm",children:S.symbol}),e.jsx("span",{children:S.label})]})},S.id))]})})]}),T&&e.jsx("p",{className:"text-xs text-muted-foreground",children:T.description})]}),o==="custom"?e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"customExpression",children:"SQL Expression"}),e.jsx(s.Textarea,{id:"customExpression",value:p,onChange:S=>g(S.target.value),placeholder:"e.g., CASE WHEN revenue > 1000 THEN revenue - cost ELSE 0 END",className:s.cn("font-mono text-sm",A.customExpression&&"border-red-500"),rows:4}),A.customExpression&&e.jsx("p",{className:"text-xs text-red-500",children:A.customExpression}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Write a SQL expression using column names from your data source"})]}):e.jsxs("div",{className:"rounded-md bg-muted p-3 font-mono text-sm",children:["= ",m&&N?N.label:"A"," ",T==null?void 0:T.symbol," ",h&&C?C.label:"B"]}),o!=="custom"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"inputA",children:"Where"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex h-8 w-8 items-center justify-center rounded bg-muted font-mono text-sm font-medium",children:"A"}),e.jsxs(s.Select,{value:m||"",onValueChange:x,children:[e.jsx(s.SelectTrigger,{id:"inputA",className:A.inputA?"border-red-500":"",children:e.jsx(s.SelectValue,{placeholder:"+ Choose metric"})}),e.jsx(s.SelectContent,{children:a.filter(S=>!s.isCalculatedMetric(S)||S.id!==(l==null?void 0:l.id)).map(S=>e.jsxs(s.SelectItem,{value:S.id,children:[S.label||S.name,S.aggregate&&` (${S.aggregate})`]},S.id))})]})]}),A.inputA&&e.jsx("p",{className:"text-xs text-red-500",children:A.inputA})]}),o!=="custom"&&o!=="scale"&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex h-8 w-8 items-center justify-center rounded bg-muted font-mono text-sm font-medium",children:"B"}),e.jsxs(s.Select,{value:h||"",onValueChange:w,children:[e.jsx(s.SelectTrigger,{id:"inputB",className:A.inputB?"border-red-500":"",children:e.jsx(s.SelectValue,{placeholder:"+ Choose metric"})}),e.jsx(s.SelectContent,{children:a.filter(S=>!s.isCalculatedMetric(S)||S.id!==(l==null?void 0:l.id)).map(S=>e.jsxs(s.SelectItem,{value:S.id,children:[S.label||S.name,S.aggregate&&` (${S.aggregate})`]},S.id))})]})]}),A.inputB&&e.jsx("p",{className:"text-xs text-red-500",children:A.inputB})]}),o==="scale"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"constantFactor",children:"Multiplication Factor"}),e.jsx(s.Input$1,{id:"constantFactor",type:"number",step:"any",value:j,onChange:S=>f(parseFloat(S.target.value)||0),placeholder:"e.g., 0.15",className:A.constantFactor?"border-red-500":""}),A.constantFactor&&e.jsx("p",{className:"text-xs text-red-500",children:A.constantFactor}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Enter the constant value to multiply the metric by (e.g., 0.15 for 15%)"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"format",children:"Format"}),e.jsxs(s.Select,{value:v.format,onValueChange:S=>O({...v,format:S}),children:[e.jsx(s.SelectTrigger,{id:"format",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsxs(s.SelectItem,{value:"number",children:["Decimal (",v.decimalPlaces,")"]}),e.jsx(s.SelectItem,{value:"currency",children:"Currency"}),e.jsx(s.SelectItem,{value:"percent",children:"Percentage"})]})]})]}),(A.inputs||A.circular)&&e.jsxs(s.Alert,{variant:"destructive",children:[e.jsx(s.CircleAlert,{className:"h-4 w-4"}),e.jsx(s.AlertDescription,{children:A.inputs||A.circular})]})]}),e.jsxs(s.DialogFooter,{children:[e.jsx(s.Button,{variant:"outline",onClick:b,children:"Cancel"}),e.jsxs(s.Button,{onClick:R,children:[l?"Update":"Create"," Metric"]})]})]})]})}function On({field:t,zone:n,index:a,sortDirection:l,onSortDirectionChange:c,hasPivotColumns:d=!1,cardType:r}){var ss,ts,as,ns,rs;const{attributes:i,listeners:o,setNodeRef:u,transform:m,transition:x,isDragging:h}=s.useSortable({id:t.id}),w=s.useEditorStore(U=>U.card),{updateLabel:j,handleAggregationChange:f,handleDateFormatChange:p,handleGranularityChange:g,handleAliasTemplateChange:v,handleValueAliasesChange:O,removeField:A,handleKpiComparisonTypeChange:y,handleKpiShowTrendlineChange:T,handleKpiTrendlineWindowChange:N,handleKpiTrendlineGranularityChange:C,handleKpiTargetValueChange:k}=s.useFieldManagement(w),[R,b]=L.useState(!1),[S,D]=L.useState(!1),[F,I]=L.useState(!1),[M,E]=L.useState(null),[B,$]=L.useState(t.dateFormat||"YYYY-MM-DD"),[V,q]=L.useState(t.customFormat||""),[G,z]=L.useState(!!t.customFormat),[H,_]=L.useState(t.granularity||"day"),[P,Y]=L.useState(t.aliasTemplate||""),[Z,se]=L.useState(t.valueAliases||{}),[W,ae]=L.useState((ss=t.parameters)==null?void 0:ss.filters),Q=U=>{U==="custom"?z(!0):(z(!1),$(U),n==="groupBy"&&p(a,U,""))},ce=U=>{q(U),n==="groupBy"&&p(a,"custom",U)},ue=U=>{var le;_(U);const X=(le=s.getDateFormatOptionsForGranularity(U)[0])==null?void 0:le.value;X&&!G&&($(X),n==="groupBy"&&p(a,X,"")),n==="groupBy"&&g(a,U)},K=U=>{Y(U),n==="metrics"&&v(a,U)},ee=U=>{se(U),n==="metrics"&&O(a,U)},de=U=>{j(n,a,U)},me=U=>{var te;if(ae(U),n==="metrics"){const X={...t,parameters:{...t.parameters,filters:U}},le=[...((te=w.config)==null?void 0:te.metricColumns)||[]];le[a]=X,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:le})}D(!1)},Se=()=>{A(n,a)},xe={transform:s.CSS.Transform.toString(m),transition:x},je=s.getDataType(t.dataType),he=je==="date",be=r==="kpi",we=s.getDateFormatOptionsForGranularity(H),ge=n!=="pivotBy",Te=he&&n==="groupBy"&&(!be||((ts=w.config)==null?void 0:ts.comparisonType)==="start_vs_end"),Ee=!be&&he&&n==="groupBy",ke=r&&s.COMPARISON_SUPPORTED_CHART_TYPES.includes(r)&&he&&n==="groupBy",Ie=r==="kpi"&&he&&n==="groupBy",ve=r&&r!=="pivotTable"&&n==="metrics"&&((as=w.config)==null?void 0:as.comparisonType)==="target",ye=n==="metrics",J=()=>{const U=t,te=n==="metrics"&&s.isCalculatedMetric(U);return e.jsxs("div",{className:"space-y-1",children:[!te&&e.jsx("div",{title:`${t.entityName}.${t.name}`,onPointerDown:X=>{X.stopPropagation()},className:"max-w-[270px] cursor-default overflow-hidden text-ellipsis whitespace-nowrap px-0.5 text-xs text-gray-500",children:`${t.entityName}.${t.name}`}),te&&U.calculatedFormula&&e.jsxs("div",{title:ys(U.calculatedFormula),onPointerDown:X=>{X.stopPropagation()},className:"max-w-[270px] cursor-default overflow-hidden text-ellipsis whitespace-nowrap rounded bg-muted/50 px-1.5 py-0.5 font-mono text-xs text-primary",children:["= ",ys(U.calculatedFormula)]})]})},ne=()=>ge?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Label"}),e.jsx(ie,{value:t.label??"",onSave:de,placeholder:"Display label...",type:"text"})]}):null,re=()=>Te?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Granularity"}),e.jsxs(s.Select,{value:H,onValueChange:ue,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select granularity..."})}),e.jsx(s.SelectContent,{children:["day","week","month","quarter","year"].map(U=>e.jsx(s.SelectItem,{className:"text-xs",value:U,children:s.toTitleCase(U)},U))})]})]}):null,Ne=()=>Ee?e.jsxs(e.Fragment,{children:[re(),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Date Format"}),e.jsxs(s.Select,{value:G?"custom":B,onValueChange:Q,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select date format..."})}),e.jsx(s.SelectContent,{children:we.map(U=>e.jsx(s.SelectItem,{className:"text-xs",value:U.value,children:U.label},U.value))})]}),G&&e.jsxs("div",{className:"mt-2 space-y-1",children:[e.jsx(s.Input,{placeholder:"Enter custom format (e.g., YYYY-MM-DD HH:mm)",value:V,onChange:U=>{U.stopPropagation(),ce(U.target.value)},onClick:U=>U.stopPropagation(),className:"h-8 text-xs"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Examples: YYYY-MM-DD, MM/DD/YY, DD-MMM-YYYY HH:mm"})]})]})]}):null,_s=()=>{var X,le,Ce,Ae,fe;const U=ke,te=Ie;return!U&&!te?null:e.jsxs(e.Fragment,{children:[U&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Comparison Type"}),e.jsxs(s.Select,{value:((X=w.config)==null?void 0:X.comparisonType)||"none",onValueChange:y,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select comparison type..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"none",children:"No Comparison"}),e.jsx(s.SelectItem,{value:"previous_period",children:"Previous Period"}),e.jsx(s.SelectItem,{value:"same_period_last_year",children:"Same Period Last Year"}),e.jsx(s.SelectItem,{value:"start_vs_end",children:"Start vs End"})]})]})]}),te&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"space-y-1",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Show Trendline"}),e.jsx(s.SmallSwitch,{checked:((le=w.config)==null?void 0:le.showTrendline)||!1,onCheckedChange:T})]})}),((Ce=w.config)==null?void 0:Ce.showTrendline)&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Trendline Window"}),e.jsx(s.Input,{type:"number",min:"1",max:"365",value:((Ae=w.config)==null?void 0:Ae.trendlineWindow)||30,onChange:Le=>{const Ws=parseInt(Le.target.value)||30;N(Ws)},className:"h-8 text-xs",placeholder:"30"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Trendline Granularity"}),e.jsxs(s.Select,{value:((fe=w.config)==null?void 0:fe.trendlineGranularity)||"day",onValueChange:C,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select granularity..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"day",children:"Day"}),e.jsx(s.SelectItem,{value:"week",children:"Week"}),e.jsx(s.SelectItem,{value:"month",children:"Month"})]})]})]})]})]})]})},Hs=()=>{var U,te;return ve?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Target Value"}),e.jsx(ie,{value:((U=w.config)==null?void 0:U.targetValue)!==void 0&&((te=w.config)==null?void 0:te.targetValue)!==null?String(w.config.targetValue):"",onSave:X=>{!isNaN(Number(X))&&X!==null?k(Number(X)):k(0)},placeholder:"Enter target value...",type:"text"})]}):null},Js=()=>{if(!ye)return null;const U=t,te=s.isCalculatedMetric(U);return e.jsxs(e.Fragment,{children:[te&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Formula"}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8 w-full justify-start text-xs",onClick:()=>I(!0),children:[e.jsx(s.Pencil,{className:"mr-2 h-3 w-3"}),"Edit Formula"]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Aggregation"}),e.jsxs(s.Select,{value:t.aggregate,onValueChange:X=>{f(a,X)},disabled:te,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select function..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"COUNT",children:"Count"}),e.jsx(s.SelectItem,{className:"text-xs",value:"DISTINCT",children:"Count Distinct"}),s.getDataType(t.dataType)==="number"&&e.jsxs(e.Fragment,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"SUM",children:"Sum"}),e.jsx(s.SelectItem,{className:"text-xs",value:"AVG",children:"Average"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MIN",children:"Min"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MAX",children:"Max"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MEDIAN",children:"Median"})]})]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Attribute Filters"}),(W==null?void 0:W.conditions)&&W.conditions.length>0&&e.jsx("div",{className:"mb-2 space-y-1",children:W.conditions.map(X=>e.jsxs("div",{className:"flex cursor-pointer items-center gap-2 rounded bg-muted/50 p-2 text-xs transition-colors hover:bg-muted/70",onClick:()=>{E(X),D(!0)},children:[e.jsxs("span",{className:"font-medium",children:[X.field.label,":"]}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:"cursor-help text-muted-foreground",children:Array.isArray(X.value)?X.value.length===1?String(X.value[0]):X.value.length<=2?X.value.join(", "):`${X.value.slice(0,2).join(", ")} +${X.value.length-2}`:String(X.value)})}),Array.isArray(X.value)&&X.value.length>1&&e.jsx(s.TooltipContent,{side:"top",className:"max-w-[300px]",children:e.jsxs("div",{className:"text-xs",children:[e.jsxs("div",{className:"mb-1 font-medium",children:["Selected values (",X.value.length,"):"]}),e.jsx("div",{className:"max-h-[200px] overflow-y-auto",children:X.value.map((le,Ce)=>e.jsx("div",{children:String(le)},Ce))})]})})]})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"ml-auto h-4 w-4 p-0",onClick:le=>{var Ae;le.stopPropagation();const Ce=W.conditions.filter(fe=>fe.id!==X.id);if(Ce.length>0){const fe={...W,conditions:Ce};me(fe)}else if(ae(void 0),n==="metrics"){const fe={...t,parameters:{...t.parameters,filters:void 0}},Le=[...((Ae=w.config)==null?void 0:Ae.metricColumns)||[]];Le[a]=fe,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:Le})}},children:e.jsx(s.X,{className:"h-3 w-3"})})]},X.id))}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8 w-full justify-start text-xs",onClick:()=>{E(null),D(!0)},children:[e.jsx(s.Filter,{className:"mr-2 h-3 w-3"}),"Add attribute filter"]})]}),e.jsx(Dn,{column:t}),d&&e.jsxs(e.Fragment,{children:[e.jsx(wn,{aliasTemplate:P,onAliasTemplateChange:K}),e.jsx(Tn,{valueAliases:Z,onValueAliasesChange:ee})]})]})},{columns:Ys=[]}=s.useColumns(),Qs=(W==null?void 0:W.conditions)&&W.conditions.length>0;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{ref:u,style:xe,...i,...o,className:`group mx-auto flex w-full flex-col gap-2 rounded border border-border bg-background p-2 transition-all hover:shadow-sm dark:border-gray-800 ${h?"z-50 scale-95 opacity-50":"border-slate-200"} ${n==="filters"?"cursor-default":"cursor-move"}`,children:[e.jsxs("div",{className:"flex grow items-center gap-2",children:[e.jsx(Xe,{dataType:je}),e.jsx(Ze,{className:s.cn("w-[140px] flex-1 text-sm",Qs&&"w-[120px]"),children:s.getMetricDisplayLabel(t)}),n==="metrics"&&((ns=W==null?void 0:W.conditions)==null?void 0:ns.length)&&!R&&e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs("div",{className:"flex items-center gap-1 rounded bg-primary/10 px-1.5 py-0.5 text-xs",children:[e.jsx(s.Filter,{className:"h-3 w-3"}),e.jsx("span",{children:W.conditions.length})]})}),e.jsx(s.TooltipContent,{side:"top",children:e.jsx("div",{className:"text-xs",children:W.conditions.map(U=>e.jsxs("div",{children:[U.field.label,":"," ",Array.isArray(U.value)?`${U.value.length} values`:String(U.value)]},U.id))})})]})}),n!=="sortBy"&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),b(!R)},children:e.jsx(s.ChevronDown,{className:`h-3 w-3 transition-transform ${R?"rotate-180":""}`})}),n==="sortBy"&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),console.log("DraggableField - Current sortDirection:",l);const te=l==="asc"?"desc":"asc";console.log("DraggableField - New direction to set:",te),c(te)},children:l==="asc"?e.jsx(s.ArrowUp,{className:"h-3 w-3"}):e.jsx(s.ArrowDown,{className:"h-3 w-3"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),Se()},children:e.jsx(s.X,{className:"h-2.5 w-2.5"})})]}),R&&e.jsxs("div",{className:"space-y-3",children:[J(),ne(),r==="kpi"&&re(),Ne(),_s(),Hs(),Js()]})]}),S&&e.jsx(Ks,{open:S,onOpenChange:U=>{D(U),U||E(null)},availableFields:Ys,existingFilters:W,editingCondition:M,onSave:U=>{var te;if(U)me(U);else if(ae(void 0),n==="metrics"){const X={...t,parameters:{...t.parameters,filters:void 0}},le=[...((te=w.config)==null?void 0:te.metricColumns)||[]];le[a]=X,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:le})}E(null)}}),F&&n==="metrics"&&e.jsx(Gs,{open:F,onOpenChange:I,availableMetrics:((rs=w.config)==null?void 0:rs.metricColumns)||[],existingField:t,onSave:U=>{var X;const te=[...((X=w.config)==null?void 0:X.metricColumns)||[]];te[a]=U,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:te})},cardId:w.id})]})}function Rn({concept:t}){const n=()=>{switch(t){case"groupBy":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Rows = Group By"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that define how your data is grouped into rows."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"flex border-b pb-1 dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-100 text-xs font-medium dark:bg-blue-200/20",children:"Product"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"Sales"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"Profit"})]}),e.jsxs("div",{className:"flex border-b dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/50 text-xs font-medium dark:bg-blue-200/20",children:"Electronics"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$5,200"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$1,300"})]}),e.jsxs("div",{className:"flex border-b dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/30 text-xs font-medium dark:bg-blue-300/20",children:"Clothing"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$3,100"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$620"})]}),e.jsxs("div",{className:"flex",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/20 text-xs font-medium dark:bg-blue-200/20",children:"Food"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$2,400"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$480"})]})]})]});case"pivotBy":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Pivot Columns = Pivot By"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that split your data into multiple columns or series."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"flex border-b pb-1 dark:border-border",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Region"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"North"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"South"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"East"})]}),e.jsxs("div",{className:"flex border-b py-1 dark:border-border",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Sales"}),e.jsx("div",{className:"w-1/4 bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$2,300"}),e.jsx("div",{className:"w-1/4 bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"$1,800"}),e.jsx("div",{className:"w-1/4 bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$3,400"})]}),e.jsxs("div",{className:"flex py-1",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Profit"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$460"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"$360"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$680"})]})]})]});case"metrics":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Metric Values (Cells)"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that represent the values shown in your visualization."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"col-span-1"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q1"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q2"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q3"})]}),e.jsxs("div",{className:"mt-1 grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"text-xs font-medium",children:"Sales"}),e.jsx("div",{className:"rounded bg-blue-100 p-1 text-center text-xs dark:bg-blue-300/20",children:"$2,300"}),e.jsx("div",{className:"rounded bg-blue-200 p-1 text-center text-xs dark:bg-blue-400/20",children:"$3,100"}),e.jsx("div",{className:"rounded bg-blue-300 p-1 text-center text-xs dark:bg-blue-500/20",children:"$4,200"})]}),e.jsxs("div",{className:"mt-1 grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"text-xs font-medium",children:"Profit"}),e.jsx("div",{className:"rounded bg-green-100 p-1 text-center text-xs dark:bg-green-300/20",children:"$460"}),e.jsx("div",{className:"rounded bg-green-200 p-1 text-center text-xs dark:bg-green-400/20",children:"$620"}),e.jsx("div",{className:"rounded bg-green-300 p-1 text-center text-xs dark:bg-green-500/20",children:"$840"})]})]})]});default:return null}};return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:300,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.CircleHelp,{className:"size-4 text-muted-foreground hover:text-foreground"})}),e.jsx(s.TooltipContent,{side:"right",align:"start",className:"p-3",children:n()})]})})}function Me({zone:t,fields:n,label:a,placeholder:l,shouldShow:c,hasPivotColumns:d=!1,activeField:r=null}){const{isOver:i,setNodeRef:o}=s.useDroppable({id:t}),{updateSortByColumn:u}=s.useEditorActions(),m=s.useEditorStore(p=>p.card),x=s.useEditorStore(p=>p.card.type),h=n.length>0,w=L.useMemo(()=>!r||!i?!0:s.validateFieldForChartType(r,t,x).isValid,[r,i,t,x]),j=(p,g)=>{var O;!((O=m.config)!=null&&O.sortByColumns)||m.config.sortByColumns.findIndex(A=>A.id===p)===-1||u(m.id,p,{direction:g})};if(t==="sortBy"&&c===!1)return null;const f=t!=="sortBy"&&x!=="kpi";return e.jsxs("div",{className:"mb-4 w-full",children:[e.jsxs("div",{className:"mb-2 flex items-center gap-2",children:[e.jsx(s.Label,{className:s.cn("block text-sm font-medium",{}),children:a}),f&&e.jsx(Rn,{concept:t})]}),e.jsx("div",{ref:o,className:s.cn("rounded-lg border-2 border-dotted p-0.5 transition-all",{"border-green-500 bg-green-50 dark:bg-green-950":i&&w,"border-red-500 bg-red-50 dark:bg-red-950":i&&!w,"border-muted-foreground/30 bg-muted/40":!i&&h,"min-h-[60px] border-muted-foreground/20 bg-background":!i&&!h}),children:n.length===0?e.jsx("div",{className:"flex min-h-[60px] items-center justify-center text-sm text-muted-foreground/60",children:l}):e.jsx(s.SortableContext,{items:n.map(p=>p.id),strategy:s.verticalListSortingStrategy,children:e.jsx("div",{className:"flex flex-wrap gap-1",children:n.map((p,g)=>{const O=p.direction;return e.jsx("div",{className:"flex w-full items-center gap-2",children:e.jsx("div",{className:"flex-1",children:e.jsx(On,{field:p,zone:t,index:g,sortDirection:O,onSortDirectionChange:A=>j(p.id,A),hasPivotColumns:d,cardType:x})})},p.id)})})})})]})}function Fn({groupByFields:t,pivotByFields:n,metricsFields:a,sortByFields:l,activeField:c}){const[d,r]=L.useState(!1),i=s.useEditorStore(O=>{var A;return((A=O.card.customCardPreferences)==null?void 0:A.url)&&O.card.type==="custom"}),o=s.useEditorStore(O=>O.card),u=s.useEditorStore(O=>O.actions.updateCardConfig),m=o==null?void 0:o.type,x=t.length>0||a.length>0,h=m!=="kpi"||a.length<=2,w=m!=="kpi",j=m!=="kpi"&&x,f=["pie","doughnut","polar","funnel","aggregateTable","bubble","scatter"].includes(m),p=w&&!f,g=()=>m==="kpi"?{label:"Trend By",placeholder:"Drop fields to trend data"}:{label:"Rows",placeholder:"Drop fields to group data"},v=()=>m==="kpi"?{label:"Metric",placeholder:"Drop field for KPI"}:{label:"Columns",placeholder:"Drop fields to measure"};return e.jsxs(e.Fragment,{children:[e.jsxs(s.Accordion,{type:"single",collapsible:!0,defaultValue:"fields",className:"w-full",children:[e.jsxs(s.AccordionItem,{value:"fields",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.List,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"FIELDS"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 pt-2",children:[h&&e.jsx(Me,{zone:"groupBy",fields:t,label:g().label,placeholder:g().placeholder,activeField:c}),p&&e.jsx(Me,{zone:"pivotBy",fields:n,label:"Pivot Columns",placeholder:"Drop fields to pivot data",activeField:c}),e.jsx(Me,{zone:"metrics",fields:a,label:v().label,placeholder:v().placeholder,hasPivotColumns:n.length>0,activeField:c}),a.length>=1&&e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:()=>r(!0),className:"mt-2 w-full justify-start text-xs text-muted-foreground hover:text-foreground",children:[e.jsx(s.Calculator,{className:"mr-2 h-4 w-4"}),"Create Metric"]}),j&&e.jsx(Me,{zone:"sortBy",fields:l,label:"Sort By",placeholder:"Drop fields from Rows or Metrics",activeField:c})]})]}),!i&&e.jsxs(s.AccordionItem,{value:"configuration",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Settings2,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"CONFIGURATION"})]})}),e.jsx(s.AccordionContent,{className:"rounded-md border border-border bg-muted/50 px-3 py-0",children:e.jsx(s.Accordion,{type:"single",collapsible:!0,className:"w-full",children:e.jsx(qs,{mode:"config"})})})]}),i&&e.jsxs(s.AccordionItem,{value:"plugin",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ss,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"CONFIGURATION"})]})}),e.jsx(s.AccordionContent,{className:"rounded-md border border-border bg-muted/50 px-3 py-0",children:e.jsx(Vs,{})})]})]}),e.jsx(Gs,{open:d,onOpenChange:r,availableMetrics:a,onSave:O=>{u(o.id,{metricColumns:[...a,O]})},cardId:o.id})]})}function Bn({onEditJoins:t}){const n=s.useEditorStore(i=>i.card),{autoJoinMetadata:a}=s.useDashboardCardQuery(n);if(!(a!=null&&a.autoResolved))return null;const{resolvedJoinPlan:l}=a,c=(l==null?void 0:l.joins)||[];if(c.length===0)return null;const d=i=>!i||i.length===0?"No conditions":i.map(o=>{var h,w,j,f;const u=`${(h=o.source)==null?void 0:h.entityName}.${(w=o.source)==null?void 0:w.name}`,m=`${(j=o.target)==null?void 0:j.entityName}.${(f=o.target)==null?void 0:f.name}`,x=o.operator||"=";return`${u} ${x} ${m}`}).join(" AND "),r=i=>!i.joinKeyGroups||i.joinKeyGroups.length===0?"No conditions defined":i.joinKeyGroups.length===1?d(i.joinKeyGroups[0].keys):i.joinKeyGroups.map((o,u)=>({id:u,operator:o.operator,condition:d(o.keys)}));return e.jsx(s.Card,{className:"border-blue-500/30 bg-blue-50/30 dark:bg-blue-950/20",children:e.jsxs("div",{className:"flex items-center justify-between p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(wt,{className:"size-4 text-blue-600 dark:text-blue-400"}),e.jsx("h3",{className:"text-sm font-medium text-blue-900 dark:text-blue-100",children:"Auto-Resolved Joins"}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Badge,{variant:"secondary",className:"cursor-help bg-blue-100 text-xs text-blue-700 dark:bg-blue-900 dark:text-blue-300",children:c.length})}),e.jsx(s.TooltipContent,{side:"bottom",align:"start",className:"max-w-md p-3",children:e.jsx("div",{className:"space-y-2",children:c.map((i,o)=>{const u=r(i),m=Array.isArray(u);return e.jsxs("div",{className:"rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"mb-1 flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs",children:[e.jsx("span",{className:"font-medium text-foreground",children:i.source.name}),e.jsx(s.ArrowRight,{className:"size-3 text-muted-foreground"}),e.jsx("span",{className:"font-medium text-foreground",children:i.target.name})]}),e.jsx(s.Badge,{variant:"outline",className:"border-green-500/40 bg-green-50/30 text-xs text-green-700 dark:bg-green-950/30 dark:text-green-300",children:i.joinType})]}),e.jsx("div",{className:"mt-1 space-y-1",children:m?u.map(x=>e.jsxs("div",{className:"rounded bg-muted/30 px-2 py-1",children:[e.jsx("code",{className:"block break-all text-xs text-muted-foreground",children:x.condition}),x.operator&&e.jsxs("span",{className:"ml-2 text-xs text-muted-foreground",children:["(",x.operator,")"]})]},x.id)):e.jsx("div",{className:"rounded bg-muted/30 px-2 py-1",children:e.jsx("code",{className:"block break-all text-xs text-muted-foreground",children:u})})})]},i.id||o)})})})]})})]}),t&&e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:t,className:"h-7 gap-1 p-2 text-xs text-blue-700 hover:text-blue-900 dark:text-blue-300 dark:hover:text-blue-100",children:e.jsx(s.SquarePen,{className:"size-3.5"})})]})})}const Ns={getItem(t){if(typeof window>"u")return null;try{return localStorage.getItem(t)}catch(n){return console.warn(`Failed to read from localStorage (${t}):`,n),null}},setItem(t,n){if(!(typeof window>"u"))try{localStorage.setItem(t,n)}catch(a){console.warn(`Failed to write to localStorage (${t}):`,a)}}};function Pn({selectedChart:t,onChartChange:n,groupByFields:a,pivotByFields:l,metricsFields:c,sortByFields:d,activeField:r}){var N,C;const[i,o]=L.useState(()=>Ns.getItem("chart-selector-expanded")==="true"),[u,m]=L.useState(!1),x=s.useEditorStore(k=>k.card),h=s.useEditorStore(k=>k.actions.updateCardConfig),w=a.length>0||l.length>0||c.length>0,{data:j,autoJoinMetadata:f}=s.useDashboardCardQuery(x),p=((N=x==null?void 0:x.config)==null?void 0:N.joinPlan)||(f!=null&&f.autoResolved?f.resolvedJoinPlan:{baseEntity:{type:"table",name:"",database:"",schema:"",connectionId:"",connectionType:"none",dialect:"none"},joins:[]}),g=((C=x==null?void 0:x.dataSource)==null?void 0:C.selectedEntities)||[],v=k=>{h(x.id,{...x==null?void 0:x.config,joinPlan:k}),m(!1)},O=()=>{h(x.id,{...x==null?void 0:x.config,joinPlan:void 0}),m(!1)},A=()=>{m(!1)},y=()=>{m(!0)};L.useEffect(()=>{Ns.setItem("chart-selector-expanded",String(i))},[i]);const T=()=>{o(k=>!k)};return e.jsxs("div",{"data-content":"Chart Configuration Panel",className:"flex w-[350px] flex-col border-x border-border",children:[e.jsxs("div",{className:s.cn("px-6 pt-4",{"space-y-4":i}),children:[e.jsx("div",{className:s.cn("overflow-hidden transition-all duration-300 ease-in-out",i?"max-h-[200px] opacity-100":"max-h-0 opacity-0"),children:i&&e.jsx(Nn,{selectedChart:t,onChartChange:n})}),e.jsx("div",{"data-content":"Chart Quick Selector",className:"flex items-center justify-center",children:e.jsx(s.ChartQuickSelector,{selectedChart:t,onChartChange:n,isDropdownExpanded:i,onToggleDropdown:T})})]}),e.jsx("div",{className:s.cn("flex justify-end gap-2 px-6 py-6"),children:e.jsx("div",{className:s.cn("invisible flex gap-2",{visible:w||j}),children:e.jsx(He.EditorClearButton,{})})}),e.jsxs(s.ScrollArea,{className:"w-full flex-1 basis-0 px-6 pb-6",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx(Bn,{onEditJoins:y}),e.jsx(Fn,{groupByFields:a,pivotByFields:l,metricsFields:c,sortByFields:d,activeField:r})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]}),e.jsx(Is,{open:u,onOpenChange:m,joinPlan:p,availableDataSources:g,onSave:v,onCancel:A,onRemove:O,children:e.jsx("div",{})})]})}function Vn({initialFilters:t,availableFields:n}={}){const[a,l]=L.useState(t||{id:`group-${Date.now()}`,logicalOperator:"AND",negate:!1,conditions:[],groups:[]});s.useEditorStore(y=>y.card.dataSource);const{columns:c,isLoading:d}=s.useColumns(),i=n||c||[],o={id:"default",qualifiedFieldName:"default",name:"default",dataType:"string",label:"Default",entityId:"default",entityName:"Default",entityType:"table"},u=y=>{const T=N=>{if(N.id===y){const C=i[0]||o,k=s.getDataType(C.dataType)==="date";return{...N,conditions:[...N.conditions,{id:`condition-${Date.now()}`,field:C,operator:k?"between":"=",value:k?[]:""}]}}return{...N,groups:N.groups.map(T)}};l(T(a))},m=y=>{const T=N=>{if(N.id===y){const C=i[0]||o,k=s.getDataType(C.dataType)==="date";return{...N,groups:[...N.groups,{id:`group-${Date.now()}`,logicalOperator:"AND",negate:!1,conditions:[{id:`condition-${Date.now()}`,field:C,operator:k?"between":"=",value:k?[]:""}],groups:[]}]}}return{...N,groups:N.groups.map(T)}};l(T(a))},x=(y,T)=>{const N=C=>C.id===y?{...C,conditions:C.conditions.filter(k=>k.id!==T)}:{...C,groups:C.groups.map(N)};l(N(a))},h=(y,T)=>{const N=C=>C.id===y?{...C,groups:C.groups.filter(k=>k.id!==T)}:{...C,groups:C.groups.map(N)};l(N(a))},w=(y,T,N,C)=>{if(N==="field")throw new Error("Use a dedicated method to update the field property with a Field object");const k=R=>R.id===y?{...R,conditions:R.conditions.map(b=>b.id===T?{...b,[N]:C}:b)}:{...R,groups:R.groups.map(k)};l(k(a))},j=(y,T,N)=>{const C=k=>k.id===y?{...k,conditions:k.conditions.map(R=>R.id===T?{...R,...N}:R)}:{...k,groups:k.groups.map(C)};l(C(a))},f=(y,T)=>{const N=C=>C.id===y?{...C,logicalOperator:T}:{...C,groups:C.groups.map(N)};l(N(a))},p=(y,T)=>{const N=C=>C.id===y?{...C,negate:T}:{...C,groups:C.groups.map(N)};l(N(a))},g=(y,T,N)=>{const C=k=>k.id===y?{...k,conditions:s.arrayMove(k.conditions,T,N)}:{...k,groups:k.groups.map(C)};l(C(a))},v=(y,T,N)=>{const C=k=>k.id===y?{...k,groups:s.arrayMove(k.groups,T,N)}:{...k,groups:k.groups.map(C)};l(C(a))},O=y=>{if(!y||y.conditions.length===0&&y.groups.length===0)return"-- No filters defined";const T=C=>{let k="";typeof C.field=="object"?k=C.field.entityName?`${C.field.entityName}.${C.field.name}`:C.field.name:k=String(C.field);const R=C.operator;let b=C.value;if(C.field.dataType==="string"&&!["in","not in"].includes(C.operator))typeof b=="string"?b=`'${b}'`:Array.isArray(b)&&(b=`'${b.join(", ")}'`);else if(C.field.dataType==="date"||C.field.dataType==="datetime"||C.field.dataType==="timestamp")if(C.operator==="between"&&Array.isArray(b)&&b.length===2){const S=b[0]?`'${b[0]}'`:"NULL",D=b[1]?`'${b[1]}'`:"NULL";return`${k} BETWEEN ${S} AND ${D}`}else if((C.operator==="in"||C.operator==="not in")&&Array.isArray(b)){const S=b.map(D=>D?`'${D}'`:"NULL").join(", ");return`${k} ${R} (${S})`}else typeof b=="string"||b instanceof Date?b=`'${b}'`:Array.isArray(b)&&b.length>0&&(b=`'${b[0]}'`);else if(["in","not in"].includes(C.operator)){let S=[];Array.isArray(b)?S=b.map(D=>typeof D=="string"?`'${D}'`:String(D)):typeof b=="string"?S=b.split(",").map(D=>{const F=D.trim();return C.field.dataType==="string"?`'${F}'`:F}):typeof b=="number"&&(S=[String(b)]),b=`(${S.join(", ")})`}switch(R){case"contains":return typeof b=="string"?`${k} LIKE '%${b.replace(/'/g,"")}'`:`${k} LIKE '%${String(b)}'`;case"startsWith":return typeof b=="string"?`${k} LIKE '${b.replace(/'/g,"")}%'`:`${k} LIKE '${String(b)}%'`;case"endsWith":return typeof b=="string"?`${k} LIKE '%${b.replace(/'/g,"")}'`:`${k} LIKE '%${String(b)}'`;case"isNull":return`${k} IS NULL`;case"isNotNull":return`${k} IS NOT NULL`;default:return`${k} ${R} ${b}`}},N=C=>{const k=C.conditions.map(T),R=C.groups.map(F=>N(F)),b=[...k,...R].filter(Boolean);if(b.length===0)return"";const S=b.join(` ${C.logicalOperator} `),D=b.length>1?`(${S})`:S;return C.negate?`NOT ${D}`:D};return N(y)},A=a.conditions.length>0||a.groups.length>0;return{currentFilters:a,setCurrentFilters:l,availableFields:i,isLoading:d,addCondition:u,addGroup:m,removeCondition:x,removeGroup:h,updateCondition:w,updateConditionFields:j,updateGroupOperator:f,updateGroupNegate:p,reorderConditions:g,reorderGroups:v,generateSQLPreview:O,hasAnyFilters:A}}function zn({children:t,className:n,delayDuration:a}){return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:a,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("div",{className:s.cn("w-[200px] overflow-hidden text-ellipsis whitespace-nowrap text-sm",n),children:t})}),e.jsx(s.PortalTooltipContent,{children:t})]})})}const $n=t=>{if(t.qualifiedEntityName){const n=t.qualifiedEntityName.split(".");return n.length>1?n[0]:"default"}return"default"};function qn({value:t,onValueChange:n,availableFields:a,placeholder:l="Select field",className:c="w-64"}){const[d,r]=L.useState(!1),i=L.useMemo(()=>{const o={};return a.forEach(u=>{const m=$n(u);o[m]||(o[m]={}),o[m][u.entityId]||(o[m][u.entityId]=[]),o[m][u.entityId].push(u)}),o},[a]);return e.jsxs(s.Popover,{modal:!0,open:d,onOpenChange:r,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":d,className:s.cn("justify-between",!t&&"text-muted-foreground",c),children:[t?e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs(zn,{children:[t.entityName,".",t.name]})}):l,e.jsx(s.ChevronsUpDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"z-50 w-80 p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search fields...",className:"h-9"}),e.jsx(s.CommandEmpty,{children:"No fields found."}),e.jsx(s.CommandList,{className:"max-h-80",children:Object.entries(i).map(([o,u])=>e.jsx(s.CommandGroup,{heading:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Database,{className:"h-3 w-3"}),o]}),children:Object.entries(u).map(([m,x])=>e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 bg-gray-50/50 px-2 py-1 text-xs font-medium text-gray-600",children:[s.getEntityIcon(x[0].entityType,{size:"h-3 w-3"}),e.jsx("span",{children:x[0].entityName})]}),x.map((h,w)=>e.jsxs(s.CommandItem,{value:h.id,onSelect:()=>{n(h),r(!1)},className:"overflow-x-auto",children:[e.jsx(s.Check,{className:s.cn("mr-2 h-4 w-4",(t==null?void 0:t.id)===h.id?"opacity-100":"opacity-0")}),e.jsxs("div",{className:"flex min-w-0 flex-1 flex-col",children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"",children:h.name})}),e.jsx("div",{className:"flex items-center gap-2 text-xs text-gray-500",children:h.description&&e.jsx("span",{children:h.description})})]})]},h.id+w))]},`${o}-${m}`))},o))})]})})]})}function es(t){var f;const n=s.useEditorStore(p=>p.card),{authToken:a,tokenProps:l}=s.useSemaphorContext(),c=s.useEditorStore(p=>p.card.config),d=c==null?void 0:c.joinPlan,r={...t,alias:t.name,label:"value",role:"groupby"},i={...t,role:"metric",label:"count",alias:"count",aggregate:"COUNT"},o={...r,role:"sortby",direction:"asc"},u={joinPlan:d,groupByColumns:[r],metricColumns:[i],sortByColumns:[o]},m=((f=n.dataSource)==null?void 0:f.connectionId)||n.connectionId,{data:x,isPending:h,error:w}=s.useQuery({queryKey:["filterValues",t.entityId,t.name,t.qualifiedEntityName],queryFn:async()=>{try{return await s.postRequestRegular(`${l.apiServiceUrl}/v1/query`,(a==null?void 0:a.accessToken)||"",{connection_id:m,cardConfig:u,cardDataSource:n.dataSource})}catch(p){console.log(t),console.error("error",p)}},enabled:!!m,refetchOnMount:!0,refetchOnWindowFocus:!1});return{filterValues:((x==null?void 0:x.records)||[]).map(p=>({label:p.value||"BLANK",value:p.value||"BLANK",count:(p==null?void 0:p.count)||0})),isPending:h,error:w}}function Kn({field:t,selected:n,onChange:a,placeholder:l="Select items...",className:c}){const[d,r]=Ue.useState(!1),{filterValues:i}=es(t),o=m=>{a(n.filter(x=>x!==m))},u=m=>{n.includes(m)?a(n.filter(x=>x!==m)):a([...n,m])};return e.jsx("div",{className:s.cn("w-full",c),children:e.jsxs(s.Popover,{open:d,onOpenChange:r,modal:!0,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":d,className:"h-auto min-h-10 w-full justify-between p-2",children:[e.jsx("div",{className:"flex flex-1 flex-wrap gap-1",children:n.length>0?n.map(m=>{const x=i.find(h=>h.value===m);return e.jsxs(s.Badge,{variant:"secondary",className:"text-xs",children:[x==null?void 0:x.label,e.jsx("div",{className:"ml-1 rounded-full outline-none ring-offset-background focus:ring-2 focus:ring-ring focus:ring-offset-2",onKeyDown:h=>{h.key==="Enter"&&o(m)},onMouseDown:h=>{h.preventDefault(),h.stopPropagation()},onClick:h=>{h.stopPropagation(),o(m)},children:e.jsx(s.X,{className:"h-3 w-3 text-muted-foreground hover:text-foreground"})})]},m)}):e.jsx("span",{className:"text-muted-foreground",children:l})}),e.jsx(s.ChevronDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"z-50 w-fit overflow-auto p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No results found."}),e.jsx(s.CommandGroup,{children:i.map((m,x)=>e.jsxs(s.CommandItem,{onSelect:()=>u(m.value),children:[e.jsx(s.Check,{className:s.cn("mr-2 h-4 w-4",n.includes(m.value)?"opacity-100":"opacity-0")}),e.jsxs("div",{className:"flex grow items-center justify-between gap-4",children:[e.jsx("span",{children:m.label}),e.jsx("span",{className:"text-xs text-muted-foreground",children:m.count})]})]},m.value+x))})]})]})})]})})}function Gn({field:t,value:n,onChange:a,placeholder:l="Select value...",className:c}){const{filterValues:d,isPending:r,error:i}=es(t);return r?e.jsxs("div",{className:"flex flex-1 items-center justify-center p-2 text-xs text-gray-500",children:[e.jsx(s.LoaderCircle,{className:"mr-2 size-3.5 animate-spin"})," Loading values..."]}):i?e.jsx("div",{className:"flex-1 p-2 text-xs text-red-500",children:"Error loading values"}):e.jsxs(s.Select,{value:n,onValueChange:a,children:[e.jsx(s.SelectTrigger,{className:c||"flex-1",children:e.jsx(s.SelectValue,{placeholder:l,children:n})}),e.jsx(s.SelectContent,{children:d.map((o,u)=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:o.value,children:o.label},o.value+u))})]})}function Un({value:t,operator:n,onChange:a,className:l}){var o;const{tokenProps:c}=s.useSemaphorContext(),d=(o=c==null?void 0:c.params)==null?void 0:o.timezone,r=L.useMemo(()=>{if(t){if(n==="between"||n==="not between"){if(Array.isArray(t)&&t.length===2){const u=t[0]?new Date(t[0]):void 0,m=t[1]?new Date(t[1]):void 0;if(u&&m)return{from:u,to:m}}}else if(n==="="||n==="!="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:u,to:u}}}else if(n===">"||n===">="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:u,to:void 0}}}else if(n==="<"||n==="<="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:void 0,to:u}}}else if((n==="in"||n==="not in")&&Array.isArray(t)&&t.length>0){const u=new Date(t[0]),m=t.length>1?new Date(t[t.length-1]):u;if(!isNaN(u.getTime()))return{from:u,to:m}}}},[t,n]),i=u=>{if(!u){a(void 0);return}if(u.from&&u.to)a([u.from.toISOString(),u.to.toISOString()]);else if(u.from){const m=new Date(u.from);m.setHours(23,59,59,999),a([u.from.toISOString(),m.toISOString()])}else if(u.to){const m=new Date(u.to);m.setHours(0,0,0,0),a([m.toISOString(),u.to.toISOString()])}};return e.jsx(s.DateRangePicker,{value:r,onChange:i,timezone:d,showRelativeTab:!0,className:l,placeholder:"Select date..."})}const _n=["=","!=",">",">=","<","<=","contains","startsWith","endsWith","in","not in","isNull","isNotNull"],Hn=["=","!=","contains","startsWith","endsWith","in","not in","isNull","isNotNull"],Jn=["=","!=",">",">=","<","<=","in","not in","isNull","isNotNull"],Yn=["=","!=","isNull","isNotNull"],Qn=["between","isNull","isNotNull"],Wn={"=":"Equals","!=":"Not Equals",">":"Greater Than",">=":"Greater Than or Equal","<":"Less Than","<=":"Less Than or Equal",contains:"Contains",startsWith:"Starts With",endsWith:"Ends With",in:"In","not in":"Not In",between:"Between",isNull:"Is Null",isNotNull:"Is Not Null","not between":"Not Between"};function Cs(t){return Wn[t]||s.titleCase(t)}function Xn(t){switch(t){case"string":return Hn;case"number":return Jn;case"boolean":return Yn;case"date":return Qn;default:return _n}}function Us({condition:t,index:n,groupId:a,groupLogicalOperator:l,onUpdateCondition:c,onRemoveCondition:d,onUpdateGroupOperator:r,availableFields:i,updateConditionFields:o}){const{attributes:u,listeners:m,setNodeRef:x,transform:h,transition:w,isDragging:j}=s.useSortable({id:t.id}),{isPending:f,error:p}=es(t.field),g={transform:s.CSS.Transform.toString(h),transition:w};return e.jsxs("div",{ref:x,style:g,className:`mb-2 flex items-center gap-2 rounded p-2 hover:bg-muted/50 ${j?"z-50 opacity-50":""}`,children:[e.jsx("div",{...u,...m,className:"cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-4 w-4 flex-shrink-0 text-gray-400"})}),n>0&&e.jsxs(s.Select,{value:l,onValueChange:v=>r(a,v),children:[e.jsx(s.SelectTrigger,{className:"w-24",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"AND",children:"AND"}),e.jsx(s.SelectItem,{value:"OR",children:"OR"})]})]}),n===0&&e.jsx("div",{className:"w-24"}),e.jsx(qn,{value:t.field,onValueChange:v=>{const O=v,A=s.getDataType(O.dataType)==="date";o(a,t.id,{field:O,value:"",operator:A?"between":"="})},availableFields:i,placeholder:"Select field",className:"w-64"}),e.jsxs(s.Select,{value:t.operator,onValueChange:v=>{v==="in"||v==="not in"?o(a,t.id,{operator:v,value:[]}):v==="isNull"||v==="isNotNull"?o(a,t.id,{operator:v,value:null}):o(a,t.id,{operator:v,value:""})},children:[e.jsx(s.SelectTrigger,{className:"w-32",children:e.jsx(s.SelectValue,{children:t.operator?Cs(t.operator):""})}),e.jsx(s.SelectContent,{children:Xn(s.getDataType(t.field.dataType)).map(v=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:v,children:Cs(v)},v))})]}),t.operator==="isNull"||t.operator==="isNotNull"?e.jsx("div",{className:"flex-1"}):s.getDataType(t.field.dataType)==="string"?t.operator==="in"||t.operator==="not in"?f?e.jsxs("div",{className:"flex flex-1 items-center justify-center p-2 text-xs text-gray-500",children:[e.jsx(s.LoaderCircle,{className:"mr-2 size-3.5 animate-spin"})," Loading values..."]}):p?e.jsx("div",{className:"flex-1 p-2 text-xs text-red-500",children:"Error loading values"}):e.jsx(Kn,{field:t.field,selected:Array.isArray(t.value)&&t.value.every(v=>typeof v=="string")?t.value:[],onChange:v=>o(a,t.id,{value:v}),placeholder:`Select values for ${t.operator}...`,className:"flex-1"}):["contains","startsWith","endsWith"].includes(t.operator)?e.jsx(s.Input,{value:typeof t.value=="string"?t.value:"",onChange:v=>o(a,t.id,{value:v.target.value}),className:"flex-1",placeholder:t.operator.charAt(0).toUpperCase()+t.operator.slice(1)+"..."}):e.jsx(Gn,{field:t.field,value:typeof t.value=="string"?t.value:"",onChange:v=>o(a,t.id,{value:v}),placeholder:"Select value...",className:"flex-1"}):s.getDataType(t.field.dataType)==="boolean"?e.jsxs(s.Select,{value:typeof t.value=="boolean"?String(t.value):"",onValueChange:v=>o(a,t.id,{value:v==="true"}),children:[e.jsx(s.SelectTrigger,{className:"flex-1",children:e.jsx(s.SelectValue,{placeholder:"Select value..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"true",children:"True"}),e.jsx(s.SelectItem,{value:"false",children:"False"})]})]}):s.getDataType(t.field.dataType)==="date"?e.jsx(Un,{value:t.value,operator:t.operator,onChange:v=>o(a,t.id,{value:v}),className:"flex-1"}):e.jsx(s.Input,{value:typeof t.value=="string"||typeof t.value=="number"?t.value:"",onChange:v=>c(a,t.id,"value",v.target.value),className:"flex-1",placeholder:t.operator==="in"||t.operator==="not in"?"value1, value2, value3":"Value"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 flex-shrink-0 p-0",onClick:()=>d(a,t.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]})}function Zn({group:t,depth:n,parentGroupId:a,onUpdateGroupOperator:l,onUpdateGroupNegate:c,onRemoveGroup:d,onUpdateCondition:r,onUpdateField:i,onRemoveCondition:o,onReorderConditions:u,onReorderGroups:m,onAddCondition:x,onAddGroup:h,availableFields:w,renderFilterGroup:j,updateConditionFields:f}){const{attributes:p,listeners:g,setNodeRef:v,transform:O,transition:A,isDragging:y}=s.useSortable({id:t.id}),T={transform:s.CSS.Transform.toString(O),transition:A},N=k=>{const{active:R,over:b}=k;if(b&&R.id!==b.id){const S=t.conditions.findIndex(F=>F.id===R.id),D=t.conditions.findIndex(F=>F.id===b.id);S!==-1&&D!==-1&&u(t.id,S,D)}},C=k=>{const{active:R,over:b}=k;if(b&&R.id!==b.id&&a){const S=t.groups.findIndex(F=>F.id===R.id),D=t.groups.findIndex(F=>F.id===b.id);S!==-1&&D!==-1&&m(a,S,D)}};return e.jsxs("div",{ref:v,style:T,className:`rounded-lg ${n>0?"mt-2 border border-border":""} ${y?"z-50 opacity-50":""}`,children:[n>0&&e.jsxs("div",{className:"flex items-center justify-between rounded-t-lg bg-muted p-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{...p,...g,className:"cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-4 w-4 text-muted-foreground"})}),e.jsx(s.Button,{variant:t.negate?"default":"outline",size:"sm",className:"h-7 px-2 text-xs",onClick:()=>c(t.id,!t.negate),children:"NOT"}),e.jsxs(s.Select,{value:t.logicalOperator,onValueChange:k=>l(t.id,k),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"AND",children:"AND"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"OR",children:"OR"})]})]}),e.jsx("span",{className:"text-sm font-medium",children:"Group"})]}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>a&&d(a,t.id),children:e.jsx(s.Trash2,{className:"h-3 w-3"})})]}),e.jsxs("div",{className:`p-3 ${n>0?"":"px-0"}`,children:[e.jsx(s.DndContext,{sensors:s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),collisionDetection:s.closestCenter,onDragEnd:N,children:e.jsx(s.SortableContext,{items:t.conditions.map(k=>k.id),strategy:s.verticalListSortingStrategy,children:t.conditions.map((k,R)=>e.jsx(Us,{condition:k,index:R,groupId:t.id,groupLogicalOperator:t.logicalOperator,onUpdateCondition:r,onUpdateField:i,onRemoveCondition:o,onUpdateGroupOperator:l,availableFields:w,updateConditionFields:f},k.id))})}),e.jsx(s.DndContext,{sensors:s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),collisionDetection:s.closestCenter,onDragEnd:C,children:e.jsx(s.SortableContext,{items:t.groups.map(k=>k.id),strategy:s.verticalListSortingStrategy,children:t.groups.map(k=>j(k,n+1,t.id))})}),e.jsxs("div",{className:"mt-3 flex items-center gap-2",children:[e.jsxs(s.Button,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:()=>x(t.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Condition"]}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>h(t.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Group"]})]})]})]})}function er({filters:t,availableFields:n,onSave:a,onClearAll:l,onCancel:c}){const{currentFilters:d,availableFields:r,isLoading:i,addCondition:o,addGroup:u,removeCondition:m,removeGroup:x,updateCondition:h,updateConditionFields:w,updateGroupOperator:j,updateGroupNegate:f,reorderConditions:p,reorderGroups:g,generateSQLPreview:v,hasAnyFilters:O,setCurrentFilters:A}=Vn({initialFilters:t,availableFields:n}),y=s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),[T,N]=L.useState(!1),C=(b,S,D)=>{const F=I=>I.id===b?{...I,conditions:I.conditions.map(M=>M.id===S?{...M,field:D,dataType:D.dataType}:M)}:{...I,groups:I.groups.map(F)};A(F(d))},k=(b,S=0,D)=>S===0?e.jsx("div",{className:"rounded-lg",children:e.jsxs("div",{className:"p-3 px-0",children:[e.jsx(s.DndContext,{sensors:y,collisionDetection:s.closestCenter,onDragEnd:F=>{const{active:I,over:M}=F;if(M&&I.id!==M.id){const E=b.conditions.findIndex($=>$.id===I.id),B=b.conditions.findIndex($=>$.id===M.id);E!==-1&&B!==-1&&p(b.id,E,B)}},children:e.jsx(s.SortableContext,{items:b.conditions.map(F=>F.id),strategy:s.verticalListSortingStrategy,children:b.conditions.map((F,I)=>e.jsx(Us,{condition:F,index:I,groupId:b.id,groupLogicalOperator:b.logicalOperator,onUpdateCondition:h,onUpdateField:C,onRemoveCondition:m,onUpdateGroupOperator:j,availableFields:r,updateConditionFields:w},F.id))})}),e.jsx(s.DndContext,{sensors:y,collisionDetection:s.closestCenter,onDragEnd:F=>{const{active:I,over:M}=F;if(M&&I.id!==M.id){const E=b.groups.findIndex($=>$.id===I.id),B=b.groups.findIndex($=>$.id===M.id);E!==-1&&B!==-1&&g(b.id,E,B)}},children:e.jsx(s.SortableContext,{items:b.groups.map(F=>F.id),strategy:s.verticalListSortingStrategy,children:b.groups.map(F=>k(F,S+1,b.id))})}),e.jsxs("div",{className:"mt-3 flex items-center gap-2",children:[e.jsxs(s.Button,{size:"sm",className:"h-7 text-xs",variant:"outline",onClick:()=>o(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Condition"]}),T&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>u(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Group"]})]})]})},b.id):e.jsx(Zn,{group:b,depth:S,parentGroupId:D,onUpdateGroupOperator:j,onUpdateGroupNegate:f,onRemoveGroup:x,onUpdateCondition:h,onUpdateField:C,onRemoveCondition:m,onReorderConditions:p,onReorderGroups:g,onAddCondition:o,onAddGroup:u,availableFields:r,renderFilterGroup:k,updateConditionFields:w},b.id),R=s.getFilterCount(t);return e.jsxs("div",{className:"space-y-4",children:[e.jsx(s.Card,{children:e.jsx(s.CardContent,{className:"p-4",children:e.jsxs("div",{className:"space-y-4",children:[i?e.jsxs("div",{className:"flex items-center justify-center p-4",children:[e.jsx("div",{className:"h-5 w-5 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600"}),e.jsx("span",{className:"ml-2 text-sm text-gray-600",children:"Loading columns..."})]}):k(d),e.jsx("div",{className:"mt-4 border-t border-border pt-4",children:e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.SmallSwitch,{id:"advanced-mode",checked:T,onCheckedChange:b=>N(b)}),e.jsx(s.Label,{htmlFor:"advanced-mode",className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:"Advanced mode (enable grouping)"})]})}),O&&e.jsxs("div",{className:"mt-4 border-t border-border pt-4",children:[e.jsx("div",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"SQL WHERE Clause Preview"}),e.jsx("div",{className:"overflow-x-auto rounded-md border bg-muted p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap font-mono text-xs text-muted-foreground",children:v(d)})})]})]})})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(s.Button,{size:"sm",variant:"outline",onClick:c,children:"Cancel"}),R>0&&e.jsx(s.Button,{variant:"outline",size:"sm",onClick:l,children:"Clear All"}),e.jsx(s.Button,{size:"sm",onClick:()=>a(O?d:null),children:"Apply Filters"})]})]})}function sr({cardId:t,filters:n,onFiltersChange:a,onClearFilters:l,availableFields:c,isOpen:d,onOpenChange:r}){const i=u=>{a(t,u),r(!1)},o=()=>{l(t),r(!1)};return e.jsxs(s.Dialog,{modal:!1,open:d,onOpenChange:r,children:[d&&e.jsx("div",{className:"pointer-events-none fixed inset-0 z-40 bg-background/80 backdrop-blur-sm"}),e.jsxs(s.DialogContent,{className:"max-h-[80vh] max-w-4xl overflow-y-auto",onInteractOutside:u=>{u.preventDefault()},children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Card Filters"}),e.jsx(s.DialogDescription,{children:"Add filters to the card to limit the data that is displayed."})]}),e.jsx(er,{filters:n,availableFields:c,onSave:i,onCancel:()=>r(!1),onClearAll:o})]})]})}function tr({activeCardId:t,sourceFields:n,getFieldsForContainer:a,setFieldsForContainer:l}){var p,g;const[c,d]=L.useState(null),r=s.useEditorStore(v=>v.card),i=s.useEditorStore(v=>v.actions.updateCardConfig),o=(p=r==null?void 0:r.config)==null?void 0:p.joinPlan,u=(g=r==null?void 0:r.dataSource)==null?void 0:g.semanticDomainId,{data:m}=s.useJoinabilityMap(u),x=[...n,...a("groupBy"),...a("pivotBy"),...a("metrics"),...a("sortBy")],h=L.useCallback(v=>v.startsWith("card-")?null:v.startsWith("source-")||n.find(O=>O.id===v)?"source":a("groupBy").find(O=>O.id===v)?"groupBy":a("metrics").find(O=>O.id===v)?"metrics":a("pivotBy").find(O=>O.id===v)?"pivotBy":a("sortBy").find(O=>O.id===v)?"sortBy":null,[n,a]),w=L.useCallback(v=>{d(v.active.id)},[]),j=L.useCallback(v=>{var k;const{active:O,over:A}=v;if(!A||!t){d(null);return}const y=O.id,T=A.id,N=h(y),C=h(T)||T;if(C==="sortBy"){if(N==="source"){const R=n.find(S=>S.id===y);if(!R){d(null);return}if(!(a("groupBy").some(S=>S.name===R.name&&S.qualifiedEntityName===R.qualifiedEntityName)||a("metrics").some(S=>S.name===R.name&&S.qualifiedEntityName===R.qualifiedEntityName))){d(null);return}}else if(N==="pivotBy"){d(null);return}else if(N!=="groupBy"&&N!=="metrics"&&N!=="sortBy"){d(null);return}}if(N==="source"&&C==="sortBy"){const R=[...n].find(F=>F.id===y);if(!R){d(null);return}const b=a("sortBy");if(s.fieldAlreadyExists(R,b)){d(null);return}const S=s.validateFieldForChartType(R,"sortBy",r.type);if(!S.isValid){s.ue.error("Invalid Field Type",{description:S.reason,position:"top-center",duration:3e3}),d(null);return}const D=s.getNewField(R,"sortBy",b);d(null),l("sortBy",[...b,D]);return}if(N==="source"&&C!=="source"&&C!=="sortBy"){const R=[...n].find(M=>M.id===y);if(!R){d(null);return}const b=[...a("groupBy"),...a("pivotBy"),...a("metrics"),...a("sortBy")];if(b.length>0){const M=b[0].qualifiedEntityName;if(!M){d(null);return}if(M!==R.qualifiedEntityName&&R.qualifiedEntityName)if(u&&(m!=null&&m.joinabilityMap)){if(s.calculateFieldState(R,{joinabilityMap:m.joinabilityMap,selectedFields:b,baseEntityName:(k=o==null?void 0:o.baseEntity)==null?void 0:k.name}).state==="unreachable"){s.showJoinRequiredNotification(),d(null);return}}else{if(!o){s.showJoinRequiredNotification(),d(null);return}if(!s.areEntitiesJoined(M,R.qualifiedEntityName,o)){s.showJoinRequiredNotification(),d(null);return}}}const S=a(C);if(s.fieldAlreadyExists(R,S)){d(null);return}const D=s.validateFieldForChartType(R,C,r.type);if(!D.isValid){s.ue.error("Invalid Field Type",{description:D.reason,position:"top-center",duration:3e3}),d(null);return}const F=s.getNewField(R,C,S),I=[...S,F];d(null),l(C,I),C==="metrics"&&t&&s.cleanupKpiMetricComparisonMode(r.type,r.config,i,t,I);return}if(N&&C&&N!=="source"&&C!=="source"&&N!==C){const R=a(N),b=a(C),S=R.find(E=>E.id===y);if(!S){d(null);return}const D=s.validateFieldForChartType(S,C,r.type);if(!D.isValid){s.ue.error("Invalid Field Type",{description:D.reason,position:"top-center",duration:3e3}),d(null);return}const F=s.getNewField(S,C,b);if(d(null),C==="sortBy"){s.fieldAlreadyExists(S,b)||l(C,[...b,F]);return}const I=R.filter(E=>E.id!==y),M=[...b,F];l(N,I),l(C,M),C==="metrics"&&t&&s.cleanupKpiMetricComparisonMode(r.type,r.config,i,t,M);return}if(N&&C&&N===C&&N!=="source"){const R=a(N),b=R.findIndex(D=>D.id===y),S=R.findIndex(D=>D.id===T);if(b!==S){const D=s.arrayMove(R,b,S);l(N,D)}}d(null)},[t,n,a,l,h,o,u,m,r.type,i,r.config]),f=c?x.find(v=>v.id===c):null;return{activeId:c,activeField:f,handleDragStart:w,handleDragEnd:j}}function ar({activeView:t,onViewChange:n,splitViewEnabled:a}){const l=s.useEditorStore(i=>i.card),{config:c}=L.useContext(s.SemaphorContext).tokenProps,d=(c==null?void 0:c.showInfoTab)!==!1,r=()=>a?!1:!["table","pivotTable","aggregateTable"].includes(l.type);return e.jsx(s.Tabs,{value:t,onValueChange:i=>n(i),children:e.jsxs(s.TabsList,{className:"h-9 w-fit gap-1 bg-background p-0",children:[e.jsx(s.TabsTrigger,{value:"chart",className:"rounded-md transition data-[state=active]:bg-muted",children:"Chart"}),r()&&e.jsx(s.TabsTrigger,{value:"table",className:"rounded-md transition data-[state=active]:bg-muted",children:"Results"}),l.type!=="kpi"&&d&&e.jsx(s.TabsTrigger,{value:"info",className:"rounded-md transition data-[state=active]:bg-muted",children:"Info"})]})})}const nr=[{value:"10",label:"Top 10"},{value:"20",label:"Top 20"},{value:"50",label:"Top 50"},{value:"100",label:"Top 100"},{value:"200",label:"Top 200"},{value:"500",label:"Top 500"},{value:"1000",label:"Top 1000"}];function rr(){var o,u;const t=s.useEditorStore(m=>m.card),{updateRowLimit:n}=s.useEditorActions(),[a,l]=L.useState(!1),[c,d]=L.useState(""),r=m=>{if(m==="custom"){l(!0);return}l(!1),n(t.id,parseInt(m))},i=m=>{const x=m.target.value;d(x),x&&!isNaN(parseInt(x))&&n(t.id,parseInt(x))};return e.jsx("div",{className:"flex items-center gap-2",children:a?e.jsx(s.Input$1,{type:"number",value:c,onChange:i,className:"h-7 w-24",placeholder:"Enter limit",min:1}):e.jsxs(s.Select,{value:((u=(o=t.config)==null?void 0:o.rowLimit)==null?void 0:u.toString())||"100",onValueChange:r,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-24 border-none px-1 [&>svg]:hidden",children:e.jsx(s.SelectValue,{placeholder:"Select limit"})}),e.jsx(s.SelectContent,{children:nr.map(m=>e.jsx(s.SelectItem,{value:m.value,children:m.label},m.value))})]})})}function lr({card:t,queryData:n,isLoading:a,isFetching:l,pagination:c,onPaginationChange:d,onSortChange:r,sortState:i,splitViewEnabled:o}){const u=L.useMemo(()=>(n==null?void 0:n.records)||[],[n==null?void 0:n.records]);return!o||["table","pivotTable","aggregateTable"].includes(t.type)?e.jsx(_e,{}):e.jsxs(s.ResizablePanelGroup,{direction:"vertical",className:"h-full min-h-0 w-full",children:[e.jsx(s.ResizablePanel,{defaultSize:60,minSize:30,role:"chart-section",className:"flex flex-col pb-3",children:e.jsx(_e,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{defaultSize:40,minSize:20,role:"results-section",className:"flex flex-col",children:e.jsxs("div",{className:"relative flex h-full min-h-0 max-w-full flex-1 grow basis-0 p-6",children:[(a||l)&&(n==null?void 0:n.records)&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),(n==null?void 0:n.records)&&e.jsx(s.TableVisual,{card:t,data:u,paginationMetadata:c,onPaginationChange:d,isLoading:a||l,onSortChange:r,sortState:i})]})})]})}const or="semaphor-split-view-enabled";function ir(){return As(or,!0)}function cr({activeView:t,onViewChange:n,showDebug:a,onDebugToggle:l,onFiltersClick:c,activeCard:d,columns:r,columnsLoading:i,columnsError:o}){var se,W,ae;const u=s.useEditorStore(Q=>Q.card),m=s.useDashboardStore(Q=>Q.filterValues),x=s.useDashboardStore(Q=>Q.selectedSheetId),h=s.useDashboardStore(Q=>Q.inlineFilterValuesMap[(d==null?void 0:d.id)||""]),{updateCardConfig:w,setCard:j,updateCardInFrame:f,setShowAIDialog:p}=s.useEditorActions(),g=s.useEditorStore(Q=>Q.showAIDialog),v=s.useDashboardStore(Q=>Q.isVisualEditing),[O,A]=L.useState(!1),[y,T]=L.useState(!1),[N,C]=L.useState(null),{config:k}=L.useContext(s.SemaphorContext).tokenProps,R=(k==null?void 0:k.showInfoTab)!==!1,[b,S]=ir();s.useEditorStore(Q=>{var ce;return((ce=Q.card.customCardPreferences)==null?void 0:ce.url)&&Q.card.type==="custom"});const{data:D,errorMessage:F,isLoading:I,isFetching:M,errorSql:E,pagination:B,refetch:$,comparisonMetadata:V}=s.useDashboardCardQuery(d),q=L.useMemo(()=>(D==null?void 0:D.records)||[],[D==null?void 0:D.records]),[G,z]=L.useState(!1);L.useEffect(()=>{M||z(!1)},[M]);const H=()=>{const Q=!b;Q&&t==="table"&&n("chart"),S(Q)},_=(Q,ce)=>{const ue={...d,paginationConfig:{page:Q+1,pageSize:ce}};j(ue),f(ue)};function P(){return e.jsxs("div",{className:"relative border-b border-border bg-background p-4",children:[e.jsx("button",{onClick:l,className:"absolute right-4 top-4 rounded-md p-1 hover:bg-muted","aria-label":"Close Debug Panel",children:e.jsx(s.X,{className:"h-4 w-4"})}),e.jsxs("div",{className:"space-y-4 pr-8",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Active Card Config"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(u.config||{},null,2)})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Dashboard Filters"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(m||{},null,2)})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Card Data Source State"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(d.dataSource||{},null,2)})]}),e.jsxs("div",{children:[e.jsxs("h3",{className:"mb-2 text-sm font-medium",children:["Columns (",r.length,")"]}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:i?"Loading...":o?"Error loading columns":JSON.stringify(r.slice(0,5),null,2)})]})]})]})}const Y=Q=>{if(!u.config)return;const ce=s.sortStateToSortByColumns(Q,u.config.metricColumns,u.config.groupByColumns);w(u.id,{...u.config,sortByColumns:ce})},Z=L.useMemo(()=>{var Q;return s.sortByColumnsToSortState((Q=u.config)==null?void 0:Q.sortByColumns)},[(se=u.config)==null?void 0:se.sortByColumns]);return e.jsxs("div",{className:s.cn("relative flex w-0 grow flex-col",{"border-l":g&&v}),children:[a&&P(),e.jsxs("div",{className:"group/visualization relative flex h-full max-w-full grow flex-col pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 px-6",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[t==="chart"&&g&&e.jsx(Bs,{variant:"outline"}),e.jsx(ar,{activeView:t,onViewChange:n,splitViewEnabled:b}),e.jsx(s.FilterInfo,{cardId:d.id,cardFilters:(W=d.config)==null?void 0:W.filters,cardFilterCount:s.getFilterCount(((ae=d.config)==null?void 0:ae.filters)||null),onFiltersClick:c,inlineFilters:d.inlineFilters,inlineFilterValues:h,sheetId:x||""}),V&&e.jsx(s.ComparisonMetadataBadge,{metadata:V,variant:"compact"}),e.jsx("div",{className:"ml-2",children:e.jsx(rr,{})}),g&&e.jsx(He.EditorClearButton,{})]}),e.jsxs("div",{className:s.cn("flex grow items-center justify-end gap-2"),children:[e.jsx(s.IconButton,{tooltip:"Refresh",disabled:I||M,onClick:()=>{z(!0),$()},children:e.jsx(s.RefreshCcw,{className:s.cn("h-4 w-4",{"animate-spin":G})})}),t==="chart"&&e.jsx(s.IconButton,{tooltip:b?"Hide Results Table":"Show Results Table",onClick:H,children:b?e.jsx(ot,{className:"size-4"}):e.jsx(Nt,{className:"size-4"})}),t==="chart"&&e.jsx(e.Fragment,{children:e.jsx(Ps,{})}),!g&&e.jsx(s.IconButton,{onClick:()=>p(!g),tooltip:"AI",className:"h-8","aria-label":"AI",children:e.jsx(s.Sparkles,{className:"size-4"})}),l&&s.isDevEnv]})]}),e.jsxs("div",{className:"mt-3 flex min-h-0 grow flex-col",children:[t==="chart"&&e.jsx("div",{className:s.cn("flex grow flex-col pb-6",{"pb-0":b&&!["table","pivotTable","aggregateTable"].includes(d.type)}),children:e.jsx(lr,{card:d,queryData:D,isLoading:I,isFetching:M,pagination:B,onPaginationChange:_,onSortChange:Y,sortState:Z,splitViewEnabled:b})}),t==="table"&&(D==null?void 0:D.records)&&e.jsxs("div",{className:"relative flex h-full min-h-0 max-w-full flex-1 grow basis-0 p-6",children:[(I||M)&&(D==null?void 0:D.records)&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),e.jsx(s.TableVisual,{card:d,data:q,paginationMetadata:B,onPaginationChange:_,isLoading:I||M,onSortChange:Y,sortState:Z})]}),t==="info"&&R&&e.jsx("div",{className:"flex min-h-0 flex-1 flex-col pb-6",children:e.jsx("div",{className:"flex min-h-0 flex-1 flex-col rounded-md bg-background px-6",children:F?e.jsx(Ye,{error:F,errorSql:E,onSendFeedback:()=>{const Q=s.prepareFeedbackData(s.FeedbackCategory.QUERY_GENERATION,{cardConfig:u.config,dataSource:d.dataSource,dashboardFilters:m,errorMessage:F,errorSql:E,generatedSql:D==null?void 0:D.sql});C(Q),T(!0)},onOpenInEditor:()=>{p(!0),setTimeout(()=>{const Q=s.useEditorStore.getState().actions;Q.setIsDevMode(!0),Q.setCardSql(E||""),setTimeout(()=>{Q.setRunSql(!0)},100)},100)}}):(D==null?void 0:D.sql)&&e.jsx(Ms,{sql:D.sql,showCardDetails:!0})})})]}),N&&e.jsx(s.FeedbackDialog,{open:y,onOpenChange:T,feedbackData:N})]})]})}function dr({activeField:t}){const[n,a]=L.useState(!0);return L.useEffect(()=>{t&&a(!0)},[t]),t?e.jsxs("div",{className:`flex max-w-[300px] items-center gap-2 rounded-lg border border-blue-500 bg-background p-2 shadow-lg transition-all duration-200 ${n?"scale-100 opacity-100":"scale-95 opacity-0"}`,children:[e.jsx(s.GripVertical,{className:"h-3 w-3 shrink-0 text-muted-foreground"}),e.jsx(Xe,{dataType:t.dataType}),e.jsx("span",{className:"overflow-hidden text-ellipsis whitespace-nowrap text-sm dark:text-muted-foreground",children:t.label||t.name})]}):null}const ur=()=>{const t=console.error;console.error=(...l)=>{l.length>0&&typeof l[0]=="string"&&l[0].includes("ResizeObserver loop completed with undelivered notifications")||t.apply(console,l)};const n=l=>{if(l.message&&(l.message.includes("ResizeObserver loop completed with undelivered notifications")||l.message.includes("ResizeObserver loop limit exceeded")))return l.preventDefault(),l.stopPropagation(),!1},a=l=>{var c;if((c=l.reason)!=null&&c.message&&(l.reason.message.includes("ResizeObserver loop completed with undelivered notifications")||l.reason.message.includes("ResizeObserver loop limit exceeded")))return l.preventDefault(),!1};return window.addEventListener("error",n,!0),window.addEventListener("unhandledrejection",a,!0),()=>{console.error=t,window.removeEventListener("error",n,!0),window.removeEventListener("unhandledrejection",a,!0)}};function mr({column:t,isSelected:n,onSelect:a,availableFields:l,width:c,onWidthChange:d}){var $,V,q;const r=s.useEditorStore(G=>G.card),{updateCardConfig:i,removeGroupByColumn:o,removeMetricColumn:u,updateFilters:m}=s.useEditorActions(),[x,h]=L.useState(!1),[w,j]=L.useState(!1),f=L.useRef(null),p=L.useCallback(G=>{G.preventDefault(),G.stopPropagation(),j(!0),f.current={startX:G.clientX,startWidth:c||150};const z=_=>{if(!f.current)return;const P=_.clientX-f.current.startX,Y=Math.max(80,f.current.startWidth+P);d==null||d(t.id,Y)},H=()=>{j(!1),f.current=null,document.removeEventListener("mousemove",z),document.removeEventListener("mouseup",H)};document.addEventListener("mousemove",z),document.addEventListener("mouseup",H)},[c,t.id,d]),g=($=r==null?void 0:r.config)==null?void 0:$.filters,v=(V=g==null?void 0:g.conditions)==null?void 0:V.some(G=>G.field.name===t.name&&G.field.qualifiedEntityName===t.qualifiedEntityName),O=G=>{G?m(r.id,G):i(r.id,{filters:void 0})},{attributes:A,listeners:y,setNodeRef:T,transform:N,transition:C,isDragging:k}=s.useSortable({id:t.id}),R={transform:s.CSS.Transform.toString(N),transition:C},b=s.getDataType(t.dataType),S=xr(b),D=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.sortByColumns)||[],z=G.findIndex(_=>_.name===t.name);if(z!==-1){const _=[...G];_[z]={..._[z],direction:"asc"},i(r.id,{sortByColumns:_})}else i(r.id,{sortByColumns:[...G,{...t,id:`sort-${t.id}`,role:"sortby",direction:"asc"}]})},F=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.sortByColumns)||[],z=G.findIndex(_=>_.name===t.name);if(z!==-1){const _=[...G];_[z]={..._[z],direction:"desc"},i(r.id,{sortByColumns:_})}else i(r.id,{sortByColumns:[...G,{...t,id:`sort-${t.id}`,role:"sortby",direction:"desc"}]})},I=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.groupByColumns)||[];if(!G.some(_=>_.name===t.name)){const _={...t,id:`groupby-${t.id}-${Date.now()}`,role:"groupby"};i(r.id,{groupByColumns:[...G,_]})}},M=G=>{var _;const z=((_=r==null?void 0:r.config)==null?void 0:_.metricColumns)||[];if(!z.some(P=>P.name===t.name&&P.aggregate===G)){const P={...t,id:`metric-${t.id}-${G}-${Date.now()}`,label:`${G} of ${t.label||t.name}`,role:"metric",aggregate:G};i(r.id,{metricColumns:[...z,P]})}},E=()=>{t.role==="groupby"?o(r.id,t.id):t.role==="metric"&&u(r.id,t.id)},B={...R,width:c?`${c}px`:void 0,minWidth:c?`${c}px`:"120px"};return e.jsxs("th",{ref:T,style:B,className:s.cn("group relative border-r border-border bg-background px-1 py-0",k&&"opacity-50",n&&"bg-primary/10",w&&"select-none"),children:[e.jsxs("div",{className:"flex items-center",children:[e.jsx("button",{...A,...y,className:"cursor-grab p-1 text-muted-foreground opacity-0 transition-opacity hover:text-foreground group-hover:opacity-100 active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-3 w-3"})}),e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsxs("button",{onClick:a,className:s.cn("flex flex-1 items-center justify-between gap-1 rounded px-2 py-2 text-left text-sm font-medium transition-colors hover:bg-muted/50",n&&"bg-primary/10"),children:[e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(S,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"truncate",children:t.label||t.name}),t.role==="metric"&&t.aggregate&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",t.aggregate,")"]}),v&&e.jsx(s.Filter,{className:"h-3 w-3 text-primary"})]}),e.jsx(s.ChevronDown,{className:"h-3 w-3 text-muted-foreground"})]})}),e.jsxs(s.DropdownMenuContent,{align:"start",className:"w-48",children:[e.jsxs(s.DropdownMenuItem,{onClick:()=>h(!0),children:[e.jsx(s.Filter,{className:s.cn("mr-2 h-4 w-4",v&&"text-primary")}),v?"Edit Filter":"Filter"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{onClick:D,children:[e.jsx(st,{className:"mr-2 h-4 w-4"}),"Sort A → Z"]}),e.jsxs(s.DropdownMenuItem,{onClick:F,children:[e.jsx(et,{className:"mr-2 h-4 w-4"}),"Sort Z → A"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuSub,{children:[e.jsxs(s.DropdownMenuSubTrigger,{children:[e.jsx(s.Calculator,{className:"mr-2 h-4 w-4"}),"Summarize"]}),e.jsxs(s.DropdownMenuSubContent,{children:[e.jsx(s.DropdownMenuItem,{onClick:()=>M("SUM"),children:"Sum"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("AVG"),children:"Average"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("COUNT"),children:"Count"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("DISTINCT"),children:"Count Distinct"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("MIN"),children:"Min"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("MAX"),children:"Max"})]})]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{onClick:I,children:[e.jsx(Je,{className:"mr-2 h-4 w-4"}),"Group by this column"]}),e.jsxs(s.DropdownMenuItem,{disabled:!0,children:[e.jsx(ws,{className:"mr-2 h-4 w-4"}),"Pivot by this column"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{disabled:!0,children:[e.jsx(s.EyeOff,{className:"mr-2 h-4 w-4"}),"Hide column"]}),e.jsxs(s.DropdownMenuItem,{onClick:E,className:"text-destructive focus:text-destructive",children:[e.jsx(s.Trash2,{className:"mr-2 h-4 w-4"}),"Remove column"]})]})]})]}),e.jsx("div",{onMouseDown:p,className:s.cn("absolute right-0 top-0 h-full w-1 cursor-col-resize bg-transparent hover:bg-primary/50",w&&"bg-primary")}),e.jsx(Ks,{open:x,onOpenChange:h,availableFields:l.length>0?l:[t],existingFilters:g,editingCondition:v?(q=g==null?void 0:g.conditions)==null?void 0:q.find(G=>G.field.name===t.name&&G.field.qualifiedEntityName===t.qualifiedEntityName):void 0,onSave:O})]})}function xr(t){switch(t){case"number":return s.Hash;case"date":return s.Calendar;case"string":default:return s.Type}}function hr({selectedColumn:t,onColumnSelect:n,isLoading:a=!1,sourceFields:l=[]}){var C,k,R,b;const c=s.useEditorStore(S=>S.card),{updateCardConfig:d}=s.useEditorActions(),[r,i]=L.useState(null),[o,u]=L.useState({}),[m,x]=L.useState(!1),h=L.useMemo(()=>{var F,I;const S=((F=c==null?void 0:c.config)==null?void 0:F.groupByColumns)||[],D=((I=c==null?void 0:c.config)==null?void 0:I.metricColumns)||[];return[...S,...D]},[(C=c==null?void 0:c.config)==null?void 0:C.groupByColumns,(k=c==null?void 0:c.config)==null?void 0:k.metricColumns]),w=L.useMemo(()=>{const S=new Set(h.map(D=>`${D.qualifiedEntityName}.${D.name}`));return l.filter(D=>!S.has(`${D.qualifiedEntityName}.${D.name}`))},[l,h]),{data:j,isLoading:f}=s.useDashboardCardQuery(c),p=(j==null?void 0:j.records)||[],g=L.useCallback((S,D)=>{u(F=>({...F,[S]:D}))},[]),v=L.useCallback(S=>{var M,E;const D=s.getDataType(S.dataType),F=((M=c==null?void 0:c.config)==null?void 0:M.groupByColumns)||[],I=((E=c==null?void 0:c.config)==null?void 0:E.metricColumns)||[];if(D==="number"){const B={...S,id:`metric-${S.id}-${Date.now()}`,role:"metric",aggregate:"SUM"};d(c.id,{metricColumns:[...I,B]})}else{const B={...S,id:`groupby-${S.id}-${Date.now()}`,role:"groupby"};d(c.id,{groupByColumns:[...F,B]})}x(!1)},[(R=c==null?void 0:c.config)==null?void 0:R.groupByColumns,(b=c==null?void 0:c.config)==null?void 0:b.metricColumns,c.id,d]),O=s.useSensors(s.useSensor(s.PointerSensor,{activationConstraint:{distance:8}})),A=S=>{i(S.active.id)},y=S=>{const{active:D,over:F}=S;if(i(null),!F||D.id===F.id)return;const I=h.findIndex(E=>E.id===D.id),M=h.findIndex(E=>E.id===F.id);if(I!==-1&&M!==-1){const E=s.arrayMove(h,I,M),B=E.filter(V=>V.role==="groupby"),$=E.filter(V=>V.role==="metric");d(c.id,{groupByColumns:B,metricColumns:$})}},T=r?h.find(S=>S.id===r):null,N=S=>{let D="";for(;S>=0;)D=String.fromCharCode(S%26+65)+D,S=Math.floor(S/26)-1;return D};return a?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):h.length===0?e.jsx("div",{className:"flex h-full flex-col items-center justify-center gap-4 p-8 text-center",children:e.jsxs("div",{className:"rounded-lg border-2 border-dashed border-muted-foreground/25 p-12",children:[e.jsx("h3",{className:"text-lg font-medium text-muted-foreground",children:"No columns selected"}),e.jsx("p",{className:"mt-2 text-sm text-muted-foreground/75",children:"Select columns from the panel on the right to build your table"})]})}):e.jsxs("div",{className:"flex h-full flex-col",children:[e.jsx("div",{className:"flex-1 overflow-auto",children:e.jsxs(s.DndContext,{sensors:O,collisionDetection:s.closestCenter,onDragStart:A,onDragEnd:y,children:[e.jsxs("table",{className:"w-full border-collapse",children:[e.jsxs("thead",{children:[e.jsxs("tr",{className:"border-b border-border bg-muted/30",children:[e.jsx("th",{className:"sticky left-0 z-20 w-12 border-r border-border bg-muted/50 px-2 py-1 text-center text-xs font-medium text-muted-foreground"}),e.jsx(s.SortableContext,{items:h.map(S=>S.id),strategy:s.horizontalListSortingStrategy,children:h.map((S,D)=>e.jsx("th",{style:{width:o[S.id]?`${o[S.id]}px`:void 0,minWidth:o[S.id]?`${o[S.id]}px`:"120px"},className:"border-r border-border bg-muted/30 px-2 py-1 text-center text-xs font-medium text-muted-foreground",children:N(D)},`letter-${S.id}`))}),e.jsx("th",{className:"w-10 border-r border-border bg-muted/30 px-2 py-1"})]}),e.jsxs("tr",{className:"border-b-2 border-border bg-background",children:[e.jsx("th",{className:"sticky left-0 z-20 w-12 border-r border-border bg-muted/50 px-2 py-2 text-center text-xs font-medium text-muted-foreground",children:"#"}),e.jsx(s.SortableContext,{items:h.map(S=>S.id),strategy:s.horizontalListSortingStrategy,children:h.map(S=>e.jsx(mr,{column:S,isSelected:(t==null?void 0:t.id)===S.id,onSelect:()=>n(S),availableFields:l,width:o[S.id],onWidthChange:g},S.id))}),e.jsx("th",{className:"w-10 border-r border-border bg-background px-1 py-2",children:e.jsxs(s.Popover,{open:m,onOpenChange:x,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",title:"Add column",children:e.jsx(s.Plus,{className:"h-4 w-4"})})}),e.jsx(s.PopoverContent,{className:"w-[280px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search columns..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No columns available."}),e.jsx(s.CommandGroup,{heading:"Available Columns",children:w.map(S=>{const D=s.getDataType(S.dataType),F=D==="number"?s.Hash:D==="date"?s.Calendar:s.Type;return e.jsxs(s.CommandItem,{value:S.label||S.name,onSelect:()=>v(S),children:[e.jsx(F,{className:"mr-2 h-3.5 w-3.5 text-muted-foreground"}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm",children:S.label||S.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:S.entityName})]})]},S.id)})})]})]})})]})})]})]}),e.jsx("tbody",{children:f?e.jsx("tr",{children:e.jsx("td",{colSpan:h.length+2,className:"h-32 text-center",children:e.jsx(s.LoaderCircle,{className:"mx-auto h-6 w-6 animate-spin text-muted-foreground"})})}):p.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:h.length+2,className:"h-32 text-center text-muted-foreground",children:"No data available"})}):p.slice(0,100).map((S,D)=>e.jsxs("tr",{className:s.cn("border-b border-border transition-colors hover:bg-muted/50",D%2===0?"bg-background":"bg-muted/20"),children:[e.jsx("td",{className:"sticky left-0 z-10 w-12 border-r border-border bg-muted/50 px-2 py-2 text-center text-xs font-medium text-muted-foreground",children:D+1}),h.map(F=>{const I=S[F.name]??S[F.label??""];return e.jsx("td",{style:{width:o[F.id]?`${o[F.id]}px`:void 0,minWidth:o[F.id]?`${o[F.id]}px`:"120px"},className:s.cn("border-r border-border px-3 py-2 text-sm",(t==null?void 0:t.id)===F.id&&"bg-primary/5"),children:e.jsx("span",{className:"block truncate",children:pr(I,F)})},`${D}-${F.id}`)}),e.jsx("td",{className:"w-10 border-r border-border"})]},D))})]}),e.jsx(s.DragOverlay,{children:T&&e.jsx("div",{className:"rounded border border-primary bg-background px-3 py-2 text-sm font-medium shadow-lg",children:T.label||T.name})})]})}),e.jsxs("div",{className:"flex items-center justify-between border-t border-border bg-muted/30 px-4 py-2 text-xs text-muted-foreground",children:[e.jsxs("span",{children:["Showing ",Math.min(p.length,100)," of ",p.length," rows"]}),e.jsxs("span",{children:[h.length," columns"]})]})]})}function pr(t,n){if(t==null)return"—";if(typeof t=="number")return n.role==="metric"?new Intl.NumberFormat("en-US",{minimumFractionDigits:0,maximumFractionDigits:2}).format(t):t.toString();if(t instanceof Date||typeof t=="string"&&!isNaN(Date.parse(t))){const a=new Date(t);if(!isNaN(a.getTime()))return a.toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}return String(t)}function gr({entityGroups:t,isLoading:n=!1}){var O,A;const a=s.useEditorStore(y=>y.card),{updateCardConfig:l,removeGroupByColumn:c,removeMetricColumn:d}=s.useEditorActions(),[r,i]=L.useState(!0),[o,u]=L.useState(""),[m,x]=L.useState(new Set),h=((O=a==null?void 0:a.config)==null?void 0:O.groupByColumns)||[],w=((A=a==null?void 0:a.config)==null?void 0:A.metricColumns)||[],j=y=>{const T=new Set(m);T.has(y)?T.delete(y):T.add(y),x(T)},f=y=>h.some(T=>T.name===y.name&&T.qualifiedEntityName===y.qualifiedEntityName)||w.some(T=>T.name===y.name&&T.qualifiedEntityName===y.qualifiedEntityName),p=(y,T)=>{if(T)if(s.getDataType(y.dataType)==="number"){const C={...y,id:`metric-${y.id}-${Date.now()}`,role:"metric",aggregate:"SUM"};l(a.id,{metricColumns:[...w,C]})}else{const C={...y,id:`groupby-${y.id}-${Date.now()}`,role:"groupby"};l(a.id,{groupByColumns:[...h,C]})}else{const N=h.findIndex(k=>k.name===y.name&&k.qualifiedEntityName===y.qualifiedEntityName),C=w.findIndex(k=>k.name===y.name&&k.qualifiedEntityName===y.qualifiedEntityName);N!==-1&&c(a.id,h[N].id),C!==-1&&d(a.id,w[C].id)}},g=y=>{if(!o)return y;const T=o.toLowerCase();return y.filter(N=>N.name.toLowerCase().includes(T)||N.label&&N.label.toLowerCase().includes(T))},v=y=>{switch(s.getDataType(y)){case"number":return s.Hash;case"date":return s.Calendar;default:return s.Type}};return r?e.jsxs("div",{className:"flex h-full w-72 flex-col border-l border-border bg-background",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-border px-3 py-2",children:[e.jsx("span",{className:"text-sm font-medium",children:"Columns"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",onClick:()=>i(!1),title:"Close panel",children:e.jsx(jt,{className:"h-4 w-4"})})]}),e.jsx("div",{className:"border-b border-border p-2",children:e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input$1,{placeholder:"Search columns...",value:o,onChange:y=>u(y.target.value),className:"h-8 pl-8 text-sm"})]})}),(h.length>0||w.length>0)&&e.jsxs("div",{className:"border-b border-border",children:[h.length>0&&e.jsxs(s.Collapsible,{defaultOpen:!0,children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 px-3 py-2 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:"h-3.5 w-3.5 transition-transform duration-200 [[data-state=open]>&]:rotate-90"}),e.jsx(Je,{className:"h-3.5 w-3.5 text-blue-500"}),e.jsx("span",{children:"Groupings"}),e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:h.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"space-y-0.5 px-3 pb-2",children:h.map(y=>{const T=v(y.dataType);return e.jsxs("div",{className:"group flex items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-muted/50",children:[e.jsx(s.GripVertical,{className:"h-3 w-3 cursor-grab text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100"}),e.jsx(T,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:y.label||y.name}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-5 w-5 p-0 opacity-0 transition-opacity group-hover:opacity-100",onClick:()=>c(a.id,y.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]},y.id)})})})]}),w.length>0&&e.jsxs(s.Collapsible,{defaultOpen:!0,children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 px-3 py-2 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:"h-3.5 w-3.5 transition-transform duration-200 [[data-state=open]>&]:rotate-90"}),e.jsx(s.Calculator,{className:"h-3.5 w-3.5 text-green-500"}),e.jsx("span",{children:"Metrics"}),e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:w.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"space-y-0.5 px-3 pb-2",children:w.map(y=>{const T=v(y.dataType);return e.jsxs("div",{className:"group flex items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-muted/50",children:[e.jsx(s.GripVertical,{className:"h-3 w-3 cursor-grab text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100"}),e.jsx(T,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:y.label||y.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:y.aggregate}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-5 w-5 p-0 opacity-0 transition-opacity group-hover:opacity-100",onClick:()=>d(a.id,y.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]},y.id)})})})]})]}),e.jsx(s.ScrollArea,{className:"flex-1",children:e.jsx("div",{className:"p-2",children:n?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx("div",{className:"h-6 w-6 animate-spin rounded-full border-2 border-muted-foreground border-t-transparent"})}):t.length===0?e.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"No columns available"}):t.map(y=>{const T=g(y.columns);if(T.length===0)return null;const N=m.has(y.entityId)||o.length>0;return e.jsxs(s.Collapsible,{open:N,onOpenChange:()=>j(y.entityId),children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:s.cn("h-3.5 w-3.5 transition-transform duration-200",N&&"rotate-90")}),e.jsx(s.Table2,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate text-left",children:y.label||y.entityName}),e.jsx("span",{className:"text-xs text-muted-foreground",children:T.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"ml-4 space-y-0.5 border-l border-border pl-2",children:T.map(C=>{const k=v(C.dataType),R=f(C);return e.jsxs("label",{className:s.cn("flex cursor-pointer items-center gap-2 rounded px-2 py-1.5 text-sm transition-colors hover:bg-muted/50",R&&"bg-primary/5"),children:[e.jsx(s.Checkbox,{checked:R,onCheckedChange:b=>p(C,b),className:"h-3.5 w-3.5"}),e.jsx(k,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:C.label||C.name})]},C.id)})})})]},y.entityId)})})})]}):e.jsx("div",{className:"flex h-full items-start border-l border-border bg-muted/30 p-2",children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-8 w-8 p-0",onClick:()=>i(!0),title:"Open panel",children:e.jsx(bt,{className:"h-4 w-4"})})})}const fr=[{name:"SUM",description:"Sum of values",syntax:"SUM([column])"},{name:"AVG",description:"Average of values",syntax:"AVG([column])"},{name:"COUNT",description:"Count of rows",syntax:"COUNT([column])"},{name:"COUNTD",description:"Count distinct values",syntax:"COUNTD([column])"},{name:"MIN",description:"Minimum value",syntax:"MIN([column])"},{name:"MAX",description:"Maximum value",syntax:"MAX([column])"},{name:"IF",description:"Conditional logic",syntax:"IF(condition, then, else)"},{name:"CASE",description:"Multiple conditions",syntax:"CASE WHEN ... THEN ... END"},{name:"CONCAT",description:"Concatenate text",syntax:"CONCAT([col1], [col2])"},{name:"ROUND",description:"Round number",syntax:"ROUND([column], decimals)"},{name:"FLOOR",description:"Round down",syntax:"FLOOR([column])"},{name:"CEIL",description:"Round up",syntax:"CEIL([column])"},{name:"ABS",description:"Absolute value",syntax:"ABS([column])"},{name:"DATEPART",description:"Extract date part",syntax:"DATEPART(year, [date])"},{name:"DATEDIFF",description:"Difference between dates",syntax:"DATEDIFF(day, [date1], [date2])"}];function jr({value:t,onChange:n,onSubmit:a,onCancel:l,selectedColumn:c,availableColumns:d}){const[r,i]=L.useState(!1),[o,u]=L.useState("column"),m=L.useRef(null);L.useEffect(()=>{const j=f=>{f.key==="Escape"&&t?l():f.key==="Enter"&&t&&!r&&a(t)};return window.addEventListener("keydown",j),()=>window.removeEventListener("keydown",j)},[t,l,a,r]);const x=j=>{const f=`[${j.label||j.name}]`,p=m.current;if(p){const g=p.selectionStart||0,v=p.selectionEnd||0,O=t.slice(0,g)+f+t.slice(v);n(O),setTimeout(()=>{p.setSelectionRange(g+f.length,g+f.length),p.focus()},0)}else n(t+f);i(!1)},h=j=>{const f=m.current;if(f){const p=f.selectionStart||0,g=f.selectionEnd||0,v=t.slice(0,p)+j.syntax+t.slice(g);n(v),setTimeout(()=>{f.setSelectionRange(p+j.name.length+1,p+j.syntax.length-1),f.focus()},0)}else n(t+j.syntax);i(!1)},w=j=>{switch(s.getDataType(j)){case"number":return s.Hash;case"date":return s.Calendar;default:return s.Type}};return e.jsxs("div",{className:"flex items-center gap-2 border-b border-border bg-muted/30 px-3 py-1.5",children:[e.jsx("div",{className:"flex min-w-[100px] items-center justify-center rounded border border-border bg-background px-2 py-1 text-xs font-medium text-muted-foreground",children:c?e.jsx("span",{className:"truncate",children:c.label||c.name}):e.jsx("span",{children:"—"})}),e.jsxs(s.Popover,{open:r&&o==="function",onOpenChange:j=>{i(j),j&&u("function")},children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",title:"Insert function",children:e.jsx(yt,{className:"h-4 w-4"})})}),e.jsx(s.PopoverContent,{className:"w-[300px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search functions..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No functions found."}),e.jsx(s.CommandGroup,{heading:"Functions",children:fr.map(j=>e.jsx(s.CommandItem,{value:j.name,onSelect:()=>h(j),children:e.jsxs("div",{className:"flex flex-col gap-0.5",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-mono text-sm font-medium",children:j.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:j.description})]}),e.jsx("span",{className:"font-mono text-xs text-muted-foreground/75",children:j.syntax})]})},j.name))})]})]})})]}),e.jsxs(s.Popover,{open:r&&o==="column",onOpenChange:j=>{i(j),j&&u("column")},children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"ghost",size:"sm",className:"h-7 gap-1 px-2 text-xs text-muted-foreground hover:text-foreground",title:"Insert column reference",children:[e.jsx("span",{children:"Columns"}),e.jsx(s.ChevronDown,{className:"h-3 w-3"})]})}),e.jsx(s.PopoverContent,{className:"w-[250px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search columns..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No columns found."}),e.jsx(s.CommandGroup,{heading:"Available Columns",children:d.map(j=>{const f=w(j.dataType);return e.jsxs(s.CommandItem,{value:j.label||j.name,onSelect:()=>x(j),children:[e.jsx(f,{className:"mr-2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"truncate",children:j.label||j.name})]},j.id)})})]})]})})]}),e.jsx("div",{className:"relative flex-1",children:e.jsx(s.Input$1,{ref:m,value:t,onChange:j=>n(j.target.value),placeholder:"Enter a formula or select a column...",className:"h-7 border-border bg-background font-mono text-sm"})}),t&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-destructive hover:bg-destructive/10 hover:text-destructive",onClick:l,title:"Cancel (Esc)",children:e.jsx(s.X,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-green-600 hover:bg-green-600/10 hover:text-green-600",onClick:()=>a(t),title:"Apply (Enter)",children:e.jsx(s.Check,{className:"h-4 w-4"})})]})]})}function br({className:t}){var h,w,j,f;const n=s.useEditorStore(p=>p.card),[a,l]=L.useState(null),[c,d]=L.useState(""),{columns:r=[],columnGroups:i=[],isLoading:o}=s.useColumns();(h=n==null?void 0:n.config)!=null&&h.groupByColumns,(w=n==null?void 0:n.config)!=null&&w.metricColumns,(j=n==null?void 0:n.config)!=null&&j.groupByColumns,(f=n==null?void 0:n.config)!=null&&f.pivotByColumns;const u=p=>{l(p),d(p?`[${p.label||p.name}]`:"")},m=p=>{console.log("Formula submitted:",p),d(""),l(null)},x=()=>{d(""),l(null)};return e.jsxs("div",{className:s.cn("flex h-full w-full flex-col",t),children:[e.jsx(jr,{value:c,onChange:d,onSubmit:m,onCancel:x,selectedColumn:a,availableColumns:r}),e.jsxs("div",{className:"flex flex-1 overflow-hidden",children:[e.jsx("div",{className:"flex-1 overflow-auto",children:e.jsx(hr,{selectedColumn:a,onColumnSelect:u,isLoading:o,sourceFields:r})}),e.jsx(gr,{entityGroups:i,isLoading:o})]})]})}function vr(){var $,V;const[t,n]=L.useState("chart"),[a,l]=L.useState(!1),[c,d]=L.useState(!1),[r,i]=L.useState(!0),o=s.useEditorStore(q=>q.showAIDialog),u=s.useDashboardStore(q=>q.isVisualEditing),{setCardType:m}=s.useEditorActions(),x=s.useEditorStore(q=>q.card.type);L.useEffect(()=>ur(),[]);const h=q=>{m(q)},w=s.useEditorStore(q=>q.frame),j=s.useEditorStore(q=>q.card);s.useEditorStore(q=>q.activeTabCardId),s.useEditorStore(q=>q.actions.getAvailableFields);const{addCard:f,removeCard:p,updateCard:g,setActiveCard:v,updateFilters:O}=s.useEditorActions(),{columns:A=[],isLoading:y,error:T}=s.useColumns(),N=s.useSensors(s.useSensor(s.PointerSensor,{activationConstraint:{distance:8}})),C=j,{getFieldsForContainer:k,setFieldsForContainer:R}=s.useFieldManagement(C),{activeField:b,handleDragStart:S,handleDragEnd:D}=tr({activeCardId:w.activeCardId,sourceFields:A,getFieldsForContainer:k,setFieldsForContainer:R}),F=(q,G)=>{G&&O(q,G)},I=q=>{var G;g(q,{config:{...(G=w.cards.find(z=>z.id===q))==null?void 0:G.config,filters:void 0}})},M=A,E=q=>{if(!q)return 0;let G=q.conditions.length;return q.groups.forEach(z=>{G+=E(z)}),G},B=E((($=C==null?void 0:C.config)==null?void 0:$.filters)||null);return e.jsxs(s.DndContext,{sensors:N,collisionDetection:s.closestCenter,onDragStart:S,onDragEnd:D,children:[e.jsxs("div",{className:"flex h-full grow",children:[e.jsx(Ds,{isVisible:r,onToggleVisibility:()=>i(!r)}),e.jsx("div",{className:"flex flex-1 transition-all duration-300 ease-in-out",children:x==="tableBuilder"?e.jsx(br,{className:"flex-1"}):e.jsxs(e.Fragment,{children:[!o&&e.jsx(Pn,{selectedChart:x,onChartChange:h,groupByFields:k("groupBy"),pivotByFields:k("pivotBy"),metricsFields:k("metrics"),sortByFields:k("sortBy"),activeField:b}),e.jsx(cr,{selectedChart:x,activeView:t,onViewChange:n,showDebug:a,onDebugToggle:()=>l(!a),onFiltersClick:()=>d(!0),filterCount:B,activeCard:C,dataSourceState:j.dataSource||s.EMPTY_DATA_SOURCE,columns:A,columnsLoading:y,columnsError:T,getFieldsForContainer:k,cards:w.cards,activeCardId:w.activeCardId,addCard:f,setActiveCard:v})]})}),!r&&u&&e.jsx("div",{className:"fixed left-6 top-[105px] z-50",children:e.jsx(s.Button,{size:"sm",variant:"outline",className:"h-10 w-10 rounded-full border-border bg-background p-0 shadow-lg hover:bg-muted",onClick:()=>i(!0),title:"Show Data Source Panel",children:e.jsx(s.Database,{className:"h-4 w-4"})})})]}),C&&e.jsx(sr,{cardId:C.id,filters:((V=C.config)==null?void 0:V.filters)||null,onFiltersChange:F,onClearFilters:I,availableFields:M,isOpen:c,onOpenChange:d}),e.jsx(s.DragOverlay,{dropAnimation:null,children:e.jsx(dr,{activeField:b||null})})]})}function yr({authToken:t,onSave:n,onClose:a}){const l=L.useRef(null),c=s.useEditorStore(T=>T.showAIDialog),d=s.useEditorStore(T=>T.isDevMode),r=s.useAssistantLayoutStore(T=>T.setFullscreen),i=s.useAssistantLayoutStore(T=>T.setPanelOpen),o=s.useAssistantLayoutStore(T=>T.setContainer),u=s.useAssistantPanel("editor"),m=s.useFullscreenAssistant(),x=s.useAssistantContainer("editor"),h=m==="editor",w=L.useCallback(T=>{o("editor",T)},[o]),{setAuthToken:j}=s.useDashboardActions(),{setShowAIDialog:f,setOnSave:p,setOnClose:g}=s.useEditorActions(),{setActiveAssistantType:v}=s.useAIActions();L.useEffect(()=>{n&&p(n),a&&g(a)},[n,p,a,g]),L.useEffect(()=>{t&&t.accessToken&&j(t)},[t,j]),L.useEffect(()=>{var T,N;c?((T=l.current)==null||T.resize(40),i("editor",!0),v("editor")):((N=l.current)==null||N.resize(0),i("editor",!1),m==="editor"&&r(null),v(null))},[c,i,v,r,m]);function O(T){var N;T<1?(f(!1),i("editor",!1),m==="editor"&&r(null),v(null),(N=l.current)==null||N.resize(0)):(c||(f(!0),v("editor")),u.isOpen||i("editor",!0))}function A(){return e.jsxs(e.Fragment,{children:[e.jsx(Ds,{isVisible:!0,onToggleVisibility:()=>{}}),e.jsxs("div",{className:"relative flex grow border-l",children:[e.jsxs(s.ResizablePanelGroup,{"data-content":"editor-main",className:s.cn("grow transition-opacity",h&&"pointer-events-none opacity-0"),direction:"horizontal",children:[e.jsx(s.ResizablePanel,{className:"min-w-[510px] rounded-none",children:e.jsx(bn,{})}),e.jsx(s.ResizableHandle,{disabled:!c}),e.jsx(s.ResizablePanel,{"data-role":"editor-assistant-panel",ref:l,onResize:O,defaultSize:u.isOpen?40:0,className:"flex",children:e.jsx("div",{className:"grow overflow-y-auto",children:e.jsx(vs,{isFullscreen:h,onFullscreenChange:T=>r(T?"editor":null),fullscreenContainer:x})})})]}),e.jsx("div",{ref:w,className:s.cn("absolute inset-0",!h&&"pointer-events-none")})]})]})}function y(){return e.jsxs("div",{className:"relative flex h-full grow",children:[e.jsxs(s.ResizablePanelGroup,{"data-content":"explorer",className:s.cn("flex grow transition-opacity",h&&"pointer-events-none opacity-0"),direction:"horizontal",children:[e.jsx(s.ResizablePanel,{className:"min-w-[510px] grow rounded-none",children:e.jsx(vr,{})}),e.jsx(s.ResizableHandle,{disabled:!c}),e.jsx(s.ResizablePanel,{"data-role":"editor-assistant-panel",ref:l,onResize:O,defaultSize:u.isOpen?40:0,className:"flex",children:e.jsx("div",{className:"grow overflow-y-auto",children:e.jsx(vs,{isFullscreen:h,onFullscreenChange:T=>r(T?"editor":null),fullscreenContainer:x})})})]}),e.jsx("div",{ref:w,className:s.cn("absolute inset-0",!h&&"pointer-events-none")})]})}return e.jsx(s.ErrorBoundary,{FallbackComponent:s.Fallback,children:d?e.jsx("section",{className:"isolate flex h-full grow","data-content":"editor-panel",children:A()}):y()})}function Nr(){s.useDashboardStore(r=>r.selectedCardId);const t=s.useDashboardStore(r=>r.selectedSheetId),{setIsVisualEditing:n}=s.useDashboardStore(r=>r.actions),a=s.useDashboardStore(r=>r.isVisualEditing),l=s.useDashboardStore(r=>r.actions.updateFrame);function c(r){t&&(l(t,r),n(!1))}function d(){n(!1)}return e.jsx("div",{role:"editor-container",className:s.cn("flex-1 grow basis-0 overflow-auto",{"max-h-0 overflow-hidden":!a}),children:e.jsx(yr,{onSave:c,onClose:d})})}exports.default=Nr;
|
|
182
|
+
`;r(u)}:void 0}}function Rs({className:t,...n}){var V,q,G;const a=s.useEditorStore(z=>z.card),l=((V=a.customCardPreferences)==null?void 0:V.showCardHeader)??!0,c=((G=(q=a.preferences)==null?void 0:q.displayOptions)==null?void 0:G.showInlineFilterBar)??!0,d=s.useEditorStore(z=>z.frame),r=s.useDashboardStore(z=>z.dashboard.filters),i=s.useDashboardStore(z=>z.selectedSheetId),[o,u]=L.useState(!1),{currentUser:m}=s.useCurrentUserInfo(),{tokenProps:x}=L.useContext(s.SemaphorContext),h=s.useEditorStore(z=>z.actions.setRunSql),{updateFilter:w}=s.useDashboardActions(),j=r==null?void 0:r.find(z=>z.id===d.filterId),{data:f,isLoading:p,isFetching:g,errorMessage:v,errorSql:O,pagination:A}=s.useDashboardCardQuery(a),{handleOpenInEditor:y,handleDebugWithAssistant:T}=Os(v,O||a.sql||a.python),N=L.useMemo(()=>(f==null?void 0:f.records)||[],[f==null?void 0:f.records]),{setCard:C,updateCardInFrame:k}=s.useEditorActions(),{data:R}=s.useTopoJson(a),b=L.useMemo(()=>{var z;return a.type!=="pivotTable"||!((z=f==null?void 0:f.records)!=null&&z.length)?null:{records:f.records,columnSubtotalMeta:f.columnSubtotalMeta,subtotalColumns:f.subtotalColumns||[],pivotSchema:f.pivotSchema||[],groupByColumns:f.groupByColumns}},[a.type,f==null?void 0:f.records,f==null?void 0:f.columnSubtotalMeta,f==null?void 0:f.subtotalColumns,f==null?void 0:f.pivotSchema,f==null?void 0:f.groupByColumns]),S=s.usePivotTableConfig(a,b),D=()=>{u(!0)},F=L.useMemo(()=>{const z=typeof window<"u"?window.location.href:"",H=typeof navigator<"u"?navigator.userAgent:"",_=typeof window<"u"?`${window.innerWidth}x${window.innerHeight}`:"";return{category:s.FeedbackCategory.QUERY_GENERATION,user:m,tokenContext:x,systemState:{cardConfig:a.config,dataSource:a.dataSource,dashboardFilters:r,errorMessage:v||void 0,errorSql:O||a.sql||a.python,generatedSql:O||a.sql},metadata:{url:z,userAgent:H,timestamp:new Date().toISOString(),viewport:_}}},[m,x,a,r,v,O]),I=(z,H)=>{var P,Y;const _={...a,paginationConfig:{page:z+1,pageSize:H}};C(_),k(_),a.sql&&((Y=(P=a.preferences)==null?void 0:P.tablePrefs)!=null&&Y.enableDevModePagination)&&h(!0)},M=z=>{var P,Y,Z,se;const H=s.sortStateToSortByColumns(z,(P=a.config)==null?void 0:P.metricColumns,(Y=a.config)==null?void 0:Y.groupByColumns),_={...a,config:{...a.config||{},sortByColumns:H}};C(_),k(_),a.sql&&((se=(Z=a.preferences)==null?void 0:Z.tablePrefs)!=null&&se.enableDevModePagination)&&h(!0)},E=L.useMemo(()=>!a.id||!(f!=null&&f.records)?null:s.createChartConfig({card:a,data:f==null?void 0:f.records,cardType:a.type,queryConfig:a.queryConfig,customCfg:a.customCfg,preferences:a.preferences,topoJson:R}),[a.id,f==null?void 0:f.records,a.type,a.customCfg,a.preferences,R]);function B(z){return e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(s.EditorCustomVisual,{card:z})})}function $(){if(f!=null&&f.records&&(f==null?void 0:f.records.length)===0&&!p&&!g)return v?e.jsx("div",{className:"flex h-full flex-col items-start justify-start overflow-auto p-4",children:e.jsx(Ye,{error:v,errorSql:O||a.sql||a.python,onOpenInEditor:y,onDebugWithAssistant:T,onSendFeedback:D,showFeedbackButton:!0})}):e.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2",children:[e.jsx(s.TreePalm,{className:"size-8 text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No results to display"})]});if((p||g)&&(!(f!=null&&f.records)||f.records.length===0))return a.type==="table"?e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableSkeleton,{})}):e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})});if(!a.id)return null;if(a.type==="custom")return B(a);if(!["table","pivotTable","aggregateTable","custom"].includes(a.type)&&E)return e.jsx("div",{className:"relative h-full min-h-0 w-full flex-1 grow basis-0",children:p||g?e.jsx(e.Fragment,{}):e.jsx(s.ChartJsVisual,{cfg:E})});if(a.type==="table")return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.TableVisual,{card:a,data:N,paginationMetadata:A,onPaginationChange:I,onSortChange:M,isLoading:p||g})});if(a.type==="pivotTable"&&S)return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.PivotTableTanstack,{card:a,data:S.data,options:S.options,onAggregationChange:z=>{const H=s.handleAggregationChange(z,a.config),_={...a,config:H};C(_),k(_)},onAggregationLabelChange:z=>{const H=s.handleAggregationLabelChange(z,a.config),_={...a,config:H};C(_),k(_)}})});if(a.type==="aggregateTable")return e.jsx("div",{className:"relative mb-2 flex h-full min-h-0 w-full flex-1 grow basis-0",children:e.jsx(s.AggregateTable,{card:a,data:(f==null?void 0:f.records)||[],groupByColumns:f==null?void 0:f.groupByColumns,pivotSchema:f==null?void 0:f.pivotSchema,paginationMetadata:A,onPaginationChange:I,onAggregationChange:z=>{const H=s.handleAggregationChange(z,a.config),_={...a,config:H};C(_),k(_)},isLoading:p||g})})}return e.jsxs(s.Card,{role:"editor-visual-card",className:s.cn("relative flex h-full flex-col rounded-none",t),...n,children:[(p||g)&&(f==null?void 0:f.records)&&f.records.length>0&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),l&&e.jsxs(s.CardHeader,{className:"flex flex-row items-start justify-between py-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.CardTitle,{className:"text-base",children:e.jsx(s.EditableText,{value:a.title,onSave:z=>C({...a,title:z}),isEditable:!0,placeholder:"Add title",displayClassName:"text-base font-semibold",inputClassName:"text-base font-semibold"})}),e.jsx(Bt,{}),e.jsx("div",{className:"ml-auto",children:e.jsx(Ft,{})})]}),e.jsx("div",{className:"w-1/2",children:e.jsx(s.EditableText,{value:(a.description||"").trim(),onSave:z=>C({...a,description:z.trim()}),isEditable:!0,placeholder:"Add description",displayClassName:"text-sm text-muted-foreground",inputClassName:"text-sm"})})]}),j&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Label,{className:"my-1",children:e.jsx(s.EditableText,{value:j.title||j.column,onSave:z=>w({...j,title:z}),isEditable:!0,placeholder:"Filter",displayClassName:"text-sm",inputClassName:"text-sm"})}),e.jsx(s.FilterComponent,{filter:j})]})]}),c&&a.inlineFilters&&a.inlineFilters.length>0&&e.jsx("div",{className:"px-6 pb-3",children:e.jsx(s.InlineFilterBar,{card:a,sheetId:i||""})}),e.jsx(s.CardContent,{className:"flex grow flex-col",children:$()}),e.jsx(s.FeedbackDialog,{open:o,onOpenChange:u,feedbackData:F,showDebugInfo:!0})]})}const $e=({language:t,value:n,onChange:a,readonly:l})=>e.jsx(s.Ft,{className:"h-full w-full overflow-hidden text-xs",theme:"default",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,lineHeight:16,renderLineHighlight:"none",readOnly:l},defaultLanguage:t,value:n,onChange:a}),Pt=()=>e.jsxs(s.DialogHeader,{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(s.DialogTitle,{children:"Customize Card"})}),e.jsxs(s.DialogDescription,{children:["See the configuration options and documentation",e.jsx("a",{target:"_blank",className:"ml-1 text-blue-500 underline",href:"https://www.chartjs.org/docs/latest/samples/bar/vertical.html",children:"here."})]})]}),Vt=({customCode:t,cardCustomCfgString:n,mergedConfig:a,onConfigChange:l,onCodeChange:c})=>e.jsxs(s.Tabs,{defaultValue:"final-config",className:"flex w-1/2 flex-col",children:[e.jsxs(s.TabsList,{className:"justify-start rounded-sm rounded-b-none border border-b-0",children:[e.jsx(s.TabsTrigger,{value:"final-config",children:"Config"}),e.jsx(s.TabsTrigger,{value:"custom-config",children:"Changes"}),e.jsx(s.TabsTrigger,{value:"custom-code",children:"Custom Code"})]}),e.jsx(s.TabsContent,{value:"final-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx($e,{language:"json",value:a,onChange:l})}),e.jsx(s.TabsContent,{value:"custom-config",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx($e,{readonly:!0,language:"json",value:n,onChange:()=>{}})}),e.jsx(s.TabsContent,{value:"custom-code",className:"mt-0 grow rounded-b border border-t-0",children:e.jsx($e,{language:"javascript",value:t,onChange:c})})]}),zt=({error:t,hasCardCustomConfig:n,onReset:a,onApply:l,onClose:c})=>e.jsxs(s.DialogFooter,{className:"flex items-center",children:[e.jsx(s.Label,{className:"mr-2 text-red-500",children:t}),e.jsx(s.Button,{disabled:!n,onClick:a,variant:"outline",children:"Reset"}),e.jsx(s.Button,{onClick:l,variant:"outline",children:"Apply"}),e.jsx(s.Button,{onClick:c,variant:"outline",children:"Close"})]});function $t(){const[t,n]=L.useState(!1),[a,l]=L.useState(""),[c,d]=L.useState(""),[r,i]=L.useState(""),[o,u]=L.useState(""),m=s.useEditorStore(y=>y.card),x=s.useEditorStore(y=>y.card.customCfg),h=x?JSON.stringify(x,null,2):"",{data:w}=s.useDashboardCardQuery(m),{data:j}=s.useTopoJson(m),{setCardCustomCfg:f,setCustomVisualCode:p}=s.useEditorActions(),g=y=>{var T,N;return JSON.stringify({...y,data:{...y.data,datasets:(N=(T=y.data)==null?void 0:T.datasets)==null?void 0:N.map(C=>({...C,data:[]}))}},null,2)},v=()=>{var T,N;if(!((T=w==null?void 0:w.records)!=null&&T.length))return;const y=s.createChartConfig({card:m,data:w.records,cardType:m.type,queryConfig:m.queryConfig,preferences:m.preferences,topoJson:j});if(y){l(g(y)),i(((N=m.preferences)==null?void 0:N.customVisualCode)||"");const C=s.merge(y,x);d(g(C))}},O=()=>{try{if(r&&p(r),c){const y=JSON.parse(c),T=s.getObjectDiff(JSON.parse(a),y);f(T),u("")}}catch{u("Invalid JSON configuration")}},A=Object.keys(x||{}).length===0;return e.jsxs(s.Dialog,{open:t,onOpenChange:n,children:[e.jsx(s.DialogTrigger,{asChild:!0,children:e.jsx(s.IconButton,{tooltip:"Customize Visual",onClick:v,className:A?"":"text-green-600 hover:text-green-700",children:e.jsx(s.Settings,{className:"h-4 w-4"})})}),e.jsxs(s.DialogContent,{className:"flex h-[600px] grow flex-col overflow-auto sm:min-w-[900px] lg:min-w-[1200px]",children:[e.jsx(Pt,{}),e.jsxs("div",{className:"flex grow gap-2",children:[e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(Rs,{className:"w-1/2 rounded shadow-none"})}),e.jsx(Vt,{customCode:r,cardCustomCfgString:h,mergedConfig:c,onConfigChange:y=>d(y??""),onCodeChange:y=>i(y??"")})]}),e.jsx(zt,{error:o,hasCardCustomConfig:!!x,onReset:()=>{d(a),f(null)},onApply:O,onClose:()=>n(!1)})]})]})}function qt({isStarred:t,onClick:n,className:a}){const l=c=>{c.stopPropagation(),c.preventDefault(),n(c)};return e.jsx("button",{type:"button",onClick:l,className:s.cn("flex items-center justify-center rounded-sm p-0.5 transition-all","hover:bg-muted/80",a),"aria-label":t?"Remove from favorites":"Add to favorites",children:e.jsx(s.Star,{className:s.cn("h-3.5 w-3.5 transition-colors",t?"fill-yellow-400 text-yellow-400":"text-muted-foreground hover:text-foreground")})})}function cs({chartType:t,chartName:n,isCustom:a=!1,pluginName:l,isStarred:c,showStar:d=!0,onSelect:r,onToggleStar:i}){return e.jsxs("button",{onClick:r,className:s.cn("group relative flex flex-col items-center gap-2 rounded-lg border border-border","bg-background p-3 transition-all","hover:border-primary/50 hover:bg-muted/50 hover:shadow-sm"),children:[d&&e.jsx("div",{className:s.cn("absolute left-1 top-1 transition-opacity",c?"opacity-100":"opacity-0 group-hover:opacity-100"),children:e.jsx(qt,{isStarred:c,onClick:()=>i()})}),a&&l&&e.jsx("div",{className:"absolute right-1 top-1",children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:"flex items-center justify-center",children:e.jsx(s.Info,{className:"h-3 w-3 text-muted-foreground"})})}),e.jsx(s.TooltipContent,{side:"top",className:"text-xs",children:l})]})}),e.jsx(s.ChartIcon,{chartType:t,chartName:n,isCustom:a,className:"h-6 w-6"}),e.jsx("span",{title:n,className:"line-clamp-2 text-center text-xs font-medium text-foreground",children:n})]})}const ds=[{id:"table",name:"Table"},{id:"bar",name:"Bar Chart"},{id:"line",name:"Line Chart"},{id:"area",name:"Area Chart"},{id:"combo",name:"Combo Chart"},{id:"stackedBar",name:"Stacked Bar"},{id:"stackedArea",name:"Stacked Area"},{id:"stackedLine",name:"Stacked Line"},{id:"scatter",name:"Scatter"},{id:"bubble",name:"Bubble"},{id:"pie",name:"Pie Chart"},{id:"doughnut",name:"Doughnut"},{id:"funnel",name:"Funnel"},{id:"radar",name:"Radar"},{id:"polarArea",name:"Polar Area"},{id:"tornado",name:"Tornado"},{id:"range",name:"Range"},{id:"bullet",name:"Bullet Chart"},{id:"treemap",name:"Treemap"},{id:"kpi",name:"KPI"},{id:"text",name:"Text"},{id:"map",name:"Map"}];function Fs({onChartSelect:t,onClose:n}){const[a,l]=L.useState(""),{starredCharts:c,isStarred:d,toggleStar:r}=s.useStarredChartsStore(),{data:i=[],isLoading:o}=s.useCustomVisualsQuery(),{setCardType:u,setCardPreferences:m,setIsDevMode:x}=s.useEditorStore(y=>y.actions),{setCardCustomCfg:h,setCustomCardPreferences:w,setQueryConfig:j}=s.useEditorActions(),f=L.useMemo(()=>{if(!a)return ds;const y=a.toLowerCase();return ds.filter(T=>T.name.toLowerCase().includes(y))},[a]),p=L.useMemo(()=>{const y=i.filter(N=>N.componentType==="chart");if(!a)return y;const T=a.toLowerCase();return y.filter(N=>N.name.toLowerCase().includes(T))},[i,a]),g=y=>{h(null),m({}),u(y),["custom","text","map"].includes(y)&&(x(!0),j(void 0)),t==null||t(y),n==null||n()},v=y=>{h(null),m({}),u("custom"),w({type:"component",visualType:"single",url:y.url,componentName:y.name,icon:y.icon,pluginChartType:y.chartType}),j(void 0),t==null||t("custom",y),n==null||n()},O=y=>{const T={type:"default",id:y.id,name:y.name};r(T)},A=y=>{const T={type:"custom",id:y.name,url:y.url,icon:y.icon,name:y.name};r(T)};return e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"mb-3 flex items-center gap-2 rounded-sm border border-border bg-background px-3 py-2",children:[e.jsx(s.Search,{className:"h-4 w-4 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search charts...",value:a,onChange:y=>l(y.target.value),className:"h-auto border-0 p-0 text-sm focus-visible:ring-0 focus-visible:ring-offset-0"})]}),e.jsxs("div",{className:"max-h-[400px] overflow-y-auto",children:[f.length>0&&e.jsxs("div",{className:"mb-3",children:[e.jsx("span",{className:"mb-2 block text-xs font-medium text-muted-foreground",children:"Default Charts"}),e.jsx("div",{className:"grid grid-cols-3 gap-2",children:f.map(y=>e.jsx(cs,{chartType:y.id,chartName:y.name,isCustom:!1,isStarred:d("default",y.id),onSelect:()=>g(y.id),onToggleStar:()=>O(y)},y.id))})]}),p.length>0&&e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-3"}),e.jsxs("div",{children:[e.jsx("span",{className:"mb-2 block text-xs font-medium text-muted-foreground",children:"Custom Charts"}),e.jsx("div",{className:"grid grid-cols-3 gap-2",children:p.map((y,T)=>e.jsx(cs,{chartName:y.name,isCustom:!0,pluginName:y.pluginName,isStarred:d("custom",y.name),onSelect:()=>v(y),onToggleStar:()=>A(y)},`${y.url}-${y.name}-${T}`))})]})]}),o&&e.jsxs(e.Fragment,{children:[e.jsx(s.Separator,{className:"my-3"}),e.jsx("div",{className:"py-4 text-center text-sm text-muted-foreground",children:"Loading custom charts..."})]}),f.length===0&&p.length===0&&!o&&e.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"No charts found"})]})]})}function Bs({variant:t="ghost"}){var d,r;const[n,a]=L.useState(!1),l=s.useEditorStore(i=>i.card),c=l.type;return e.jsxs(s.Popover,{open:n,onOpenChange:a,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{size:"sm",variant:t||"ghost",children:e.jsx(s.SelectedChartIcon,{className:"size-4",chartType:c,customIcon:(d=l.customCardPreferences)==null?void 0:d.icon,componentName:(r=l.customCardPreferences)==null?void 0:r.componentName})})}),e.jsx(s.PopoverContent,{onOpenAutoFocus:i=>i.preventDefault(),className:"z-[70] w-[360px] p-3",children:e.jsx(Fs,{onClose:()=>a(!1)})})]})}function Kt(){const t=s.useEditorStore(o=>{var u,m;return(m=(u=o.card.preferences)==null?void 0:u.chartOptions)==null?void 0:m.indexAxis}),n=s.useEditorStore(o=>o.card.type),{setChartOrientation:a,setCardPreferences:l}=s.useEditorStore(o=>o.actions),c=o=>{if(a(o),n!=="bullet")return;const u=o==="y"?"horizontal":"vertical",m=s.useEditorStore.getState().card.preferences,x={...m||{},bulletConfig:{...(m==null?void 0:m.bulletConfig)||{},orientation:u}};l(x)};function d(){const o=t??"x",u=()=>{c(o==="x"?"y":"x")};return e.jsx(s.IconButton,{onClick:u,tooltip:"Chart Orientation",children:o==="x"?e.jsx(ze,{className:"size-4"}):e.jsx(Ve,{className:"size-4"})})}function r(){const o=()=>{c(t==="y"||t===void 0?"x":"y")};return e.jsx(s.IconButton,{onClick:o,tooltip:"Chart Orientation",children:t==="y"||t===void 0?e.jsx(ze,{className:"size-4"}):e.jsx(Ve,{className:"size-4"})})}function i(){const o=t??"y",u=()=>{c(o==="y"?"x":"y")};return e.jsx(s.IconButton,{onClick:u,tooltip:"Chart Orientation",children:o==="y"?e.jsx(Ve,{className:"size-4"}):e.jsx(ze,{className:"size-4"})})}return e.jsx(e.Fragment,{children:n==="funnel"?r():n==="bullet"?i():d()})}function Gt({className:t}){var w,j,f,p,g,v,O,A,y,T;const n=s.useEditorStore(N=>N.card),{setCardPreferences:a,updateCardInFrame:l,setCard:c}=s.useEditorActions(),[d,r]=L.useState(!1),i=((j=(w=n.preferences)==null?void 0:w.displayOptions)==null?void 0:j.showFilterInfo)??!0,o=((p=(f=n.preferences)==null?void 0:f.displayOptions)==null?void 0:p.showCardToolbar)??!0,u=((v=(g=n.preferences)==null?void 0:g.displayOptions)==null?void 0:v.showChrome)??!0,m=((A=(O=n.preferences)==null?void 0:O.displayOptions)==null?void 0:A.allowScroll)??!0,x=((T=(y=n.preferences)==null?void 0:y.displayOptions)==null?void 0:T.showInlineFilterBar)??!0,h=(N,C)=>{var b;const k={...n.preferences,displayOptions:{...(b=n.preferences)==null?void 0:b.displayOptions,[N]:C}};a(k);const R={...n,preferences:k};c(R),l(R)};return e.jsx("div",{className:s.cn("flex items-center",t),children:e.jsxs(s.DropdownMenu,{modal:!1,open:d,onOpenChange:r,children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.IconButton,{className:"h-8 w-8 p-0",tooltip:"Display Options",children:e.jsx(Es,{className:"h-4 w-4"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",className:"w-64",children:[e.jsx(s.DropdownMenuLabel,{children:"Display Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx("div",{className:"px-2 py-1.5",children:e.jsx("p",{className:"mb-2 text-xs text-muted-foreground",children:"Changes apply to dashboard view only"})}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:i,onCheckedChange:N=>{h("showFilterInfo",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Filter Info"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Badge"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:N=>{h("showCardToolbar",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Card Toolbar"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Hover actions"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:u,onCheckedChange:N=>{h("showChrome",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Card Chrome"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Frame & padding"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:m,onCheckedChange:N=>{h("allowScroll",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Allow Scroll"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Card content scroll"})]}),e.jsxs(s.DropdownMenuCheckboxItem,{checked:x,onCheckedChange:N=>{h("showInlineFilterBar",N)},onSelect:N=>N.preventDefault(),className:"pr-2",children:[e.jsx("span",{className:"flex-1",children:"Inline Filter Bar"}),e.jsx("span",{className:"ml-4 text-xs text-muted-foreground",children:"Card filters"})]})]})]})})}function Ut(){const[t,n]=L.useState(!1),a=s.useEditorStore(x=>x.card),l=s.useEditorStore(x=>x.selectedConnectionId),{setCard:c}=s.useEditorActions(),d=s.useDashboardStore(x=>x.actions.removeCardInlineFilterValue),r=(a==null?void 0:a.inlineFilters)||[];function i(x){var O,A,y;const h=((y=(A=(O=a==null?void 0:a.dataSource)==null?void 0:O.selectedEntities)==null?void 0:A[0])==null?void 0:y.database)||"",w=x.qualifiedFieldName||x.name;if(r.find(T=>T.column===w))return;const f=x.qualifiedFieldName?x.qualifiedFieldName.split(".").slice(0,-1).join("."):x.qualifiedEntityName||x.entityName,p=s.fmt(x.name),g=x.qualifiedFieldName||p,v={id:s.v4(),column:w,title:x.label||x.name,dataType:x.dataType,table:x.entityName||"",database:h,connectionId:l||"",operation:"in",sql:s.getDefaultFilterSql(x.dataType,f,g),width:250};c({...a,inlineFilters:[...r,v]})}function o(x){const h=r.filter(w=>w.id!==x);c({...a,inlineFilters:h}),d(a.id,x)}function u(x,h){const w=r.map(j=>j.id===x?{...j,...h}:j);c({...a,inlineFilters:w})}return["bar","line","pie","doughnut","table","stackedBar","stackedLine","horizontalBar","radar","polarArea","scatter","bubble","funnel","aggregateTable","custom"].includes(a==null?void 0:a.type)?e.jsx("div",{className:"flex items-center gap-1",children:e.jsxs(s.Popover,{open:t,onOpenChange:n,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.IconButton,{tooltip:"Add Inline Filter",className:s.cn("relative",{"text-primary":r.length>0}),children:[e.jsx(xt,{className:"h-4 w-4"}),r.length>0&&e.jsx("span",{className:"absolute -right-1 -top-1 flex h-4 w-4 items-center justify-center rounded-full bg-primary text-[10px] font-medium text-primary-foreground",children:r.length})]})}),e.jsx(s.PopoverContent,{align:"end",className:"w-80 p-0",children:e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"border-b px-4 py-3",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Inline Filters"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"On-card filters for fast, focused exploration."})]}),e.jsx("div",{className:"flex max-h-[300px] flex-col overflow-hidden",children:e.jsx(s.DndContext,{collisionDetection:s.closestCenter,children:e.jsx(s.SortableContext,{items:[],strategy:s.verticalListSortingStrategy,children:e.jsx(s.FieldsList,{context:"inline-filter",onFieldSelect:i,inlineFilters:r})})})}),r.length>0&&e.jsxs("div",{className:"border-t px-4 py-3",children:[e.jsx("p",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Active Filters"}),e.jsx("div",{className:"space-y-1",children:r.map(x=>e.jsx(_t,{filter:x,onUpdate:h=>u(x.id,h),onRemove:()=>o(x.id)},x.id))})]})]})})]})}):null}function _t({filter:t,onUpdate:n,onRemove:a}){return e.jsxs("div",{className:"flex items-center justify-between rounded-md bg-muted px-2 py-1.5 text-sm",children:[e.jsx("span",{className:"truncate",title:t.column,children:t.title}),e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx(s.CustomFilterPopover,{filter:t,variant:"inline",onAccept:l=>n(l),trigger:e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-5 w-5 shrink-0",title:"Configure filter",children:e.jsx(s.Settings2,{className:"h-3 w-3"})})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-5 w-5 shrink-0 hover:bg-destructive/10 hover:text-destructive",onClick:a,children:e.jsx(s.X,{className:"h-3 w-3"})})]})]})}function Ps(){const t=s.useEditorStore(d=>d.card),n=s.useEditorStore(d=>{var r,i;return(i=(r=d.card)==null?void 0:r.preferences)==null?void 0:i.chartOptions}),a=s.getChartGridStyle(n),{setChartGrid:l}=s.useEditorActions();function c(){const d=["xy","none","x","y"],i=(d.indexOf(a)+1)%d.length;l(d[i])}return e.jsxs("div",{"data-info":"visual-settings",className:"flex items-center gap-2",children:[["line","bar","range","stackedBar","stackedLine","horizontalBar","funnel","bullet"].includes(t.type)&&e.jsx(e.Fragment,{children:e.jsx(Kt,{})}),!["kpi","text","custom","table"].includes(t.type)&&e.jsx(e.Fragment,{}),!["pie","doughnut","funnel","radar","polarArea","table","custom","kpi","text","map"].includes(t.type)&&e.jsxs(s.IconButton,{tooltip:"Grid Style",onClick:c,children:[(a==="xy"||!a)&&e.jsx(s.Grid3x3,{className:"size-4"}),a==="x"&&e.jsx(ws,{className:"size-4"}),a==="y"&&e.jsx(Je,{className:"size-4"}),a==="none"&&e.jsx(Nt,{className:"size-4"})]}),e.jsx(Ut,{}),e.jsx(Gt,{})]})}function Ht(){const t=s.useEditorStore(p=>p.frame),n=s.useEditorStore(p=>p.card),{setFrame:a,setActiveTabCardId:l,setCard:c,updateCardInFrame:d,clearModeDrafts:r,initializeModeDrafts:i}=s.useEditorActions(),o=(p,g)=>{p.dataTransfer.setData("tabIndex",g.toString())},u=p=>{p.preventDefault()},m=(p,g)=>{const v=p.dataTransfer.getData("tabIndex");if(v===g.toString())return;const O=Array.from(t.cards),[A]=O.splice(parseInt(v),1);O.splice(g,0,A),a({...t,cards:O})};function x(p){const g=t.cards.filter(y=>y.id!==p),v=g[0],O={...t,cards:g,activeCardId:v.id},A=p===n.id;A&&r(),a(O),l(v.id),c(v),A&&i(v)}function h(p,g){const v={...t,cards:[...t.cards,g],activeCardId:p.id};a(v),l(g.id)}function w(){const p={...n,id:s.v4(),title:`${n.title} Copy`,tabTitle:`${n.tabTitle||n.title} Copy`};h(n,p)}function j(p){const g={dataSource:p.dataSource,connectionId:p.connectionId,lastSelectedDatabase:p.lastSelectedDatabase,lastSelectedSchema:p.lastSelectedSchema,lastSelectedTable:p.lastSelectedTable,lastSelectedDatamodelId:p.lastSelectedDatamodelId,id:s.v4(),title:"Title",description:"",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[],mode:"explorer"};h(p,g)}function f(p,g){return p.cards.length===1&&(g.displayTab===!0||g.displayTab===void 0)||p.activeCardId!==g.id&&(g.displayTab===!0||g.displayTab===void 0)?null:e.jsx("div",{onClick:()=>{const v=g.displayTab===void 0?!1:!g.displayTab;c({...g,displayTab:v}),d({...g,displayTab:v})},children:g.displayTab===!0||g.displayTab===void 0?e.jsx(s.Eye,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"}):e.jsx(nt,{className:"mr-2 size-3.5 text-muted-foreground hover:text-foreground"})})}return e.jsx(s.TabsList,{className:"group mx-4 justify-between overflow-x-auto",children:e.jsxs("div",{className:"flex items-center",children:[t.cards.map((p,g)=>e.jsxs(s.TabsTrigger,{draggable:!0,onDragStart:v=>o(v,g),onDragOver:u,onDrop:v=>m(v,g),className:"",value:p.id,children:[f(t,p),e.jsx(s.Editable,{enabled:t.activeCardId===p.id,text:p.tabTitle||p.title,onSave:v=>{c({...n,tabTitle:v}),d({...n,tabTitle:v})},children:p.tabTitle||p.title},p.id),t.activeCardId===p.id&&e.jsx(s.CopyPlus,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:v=>{v.stopPropagation(),w()}}),t.activeCardId===p.id&&t.cards.length>1&&e.jsx(s.X,{className:"ml-2 size-3.5 text-muted-foreground hover:text-foreground",onClick:()=>x(p.id)})]},p.id)),e.jsx(s.IconButton,{tooltip:"Add Card",className:"ml-1 h-8 px-2 hover:bg-muted-foreground/50",onClick:()=>j(n),children:e.jsx(s.Plus,{className:"h-4 w-4"})})]})})}function He(){const t=s.useEditorStore(g=>g.frame),n=s.useEditorStore(g=>g.card),a=s.useEditorStore(g=>g.isSqlRunning);s.useEditorStore(g=>g.isDevMode);const{setFrame:l,setActiveTabCardId:c,setCard:d,setRunSql:r,mergeDraftsIntoCard:i,clearModeDrafts:o,initializeModeDrafts:u}=s.useEditorActions(),{data:m,isLoading:x,isFetching:h,isError:w}=s.useDashboardCardQuery(n);function j(g){const v=t.cards.find(O=>O.id===g);v&&(i(),f(g),c(g),o(),u(v))}function f(g){const v=s.useEditorStore.getState().card,O=s.useEditorStore.getState().frame,A={...O,cards:O.cards.map(T=>T.id===v.id?v:T),activeCardId:g};l(A);const y=A.cards.find(T=>T.id===g);y.sql&&r(!0),d(y)}function p(){return n.type==="kpi"?x||h?e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}):e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border border-border",children:e.jsx(s.KPICard,{card:n,data:m==null?void 0:m.records,isPending:x||h,isError:w})})}):n.type==="text"?x||h?e.jsx("div",{className:"z-50 flex h-full items-center justify-center",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}):e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx("div",{className:"w-1/2 rounded-md border",children:e.jsx(s.ScrollArea,{className:"mx-3 my-6 h-[300px] overflow-y-auto break-words px-3",children:e.jsx(s.TextCard,{isLoading:a,card:n,data:m==null?void 0:m.records})})})}):e.jsx(s.ErrorBoundary,{FallbackComponent:s.FallbackVisual,children:e.jsx(Rs,{className:"grow border-none shadow-none"})})}return e.jsxs(s.Tabs,{onValueChange:j,value:t.activeCardId,className:"flex grow flex-col",children:[t.cards.map(g=>e.jsx(s.TabsContent,{className:"mt-0 grow",value:g.id,children:p()},g.id)),e.jsx(Ht,{})]})}function us({children:t}){const n=s.useEditorStore(M=>M.frame),a=s.useEditorStore(M=>M.card),l=s.useEditorStore(M=>M.isDevMode),c=s.useEditorStore(M=>M.isShowingVisual),d=s.useDashboardStore(M=>M.isVisualEditing),r=s.useEditorStore(M=>M.pythonStdOut),i=s.useEditorStore(M=>M.actions.setRunSql),{setFrame:o,setCard:u,setActiveTabCardId:m,runAssistant:x,setCardPreferences:h,mergeDraftsIntoCard:w,clearModeDrafts:j,initializeModeDrafts:f}=s.useEditorActions(),{data:p,isLoading:g,isFetching:v,errorMessage:O,errorSql:A,refetch:y,pagination:T}=s.useDashboardCardQuery(a),{handleDebugWithAssistant:N}=Os(O,A),C=L.useMemo(()=>(p==null?void 0:p.records)||[],[p==null?void 0:p.records]),[k,R]=L.useState(!1);L.useEffect(()=>{v||R(!1)},[v]);function b(M,E){var V,q;const B={...a,paginationConfig:{page:M+1,pageSize:E}};u(B);const $={...n,cards:n.cards.map(G=>G.id===a.id?B:G)};o($),a.sql&&((q=(V=a.preferences)==null?void 0:V.tablePrefs)!=null&&q.enableDevModePagination)&&i(!0)}function S(M){var V,q,G,z;const E=s.sortStateToSortByColumns(M,(V=a.config)==null?void 0:V.metricColumns,(q=a.config)==null?void 0:q.groupByColumns),B={...a,config:{...a.config||{},sortByColumns:E}};u(B);const $={...n,cards:n.cards.map(H=>H.id===a.id?B:H)};o($),a.sql&&((z=(G=a.preferences)==null?void 0:G.tablePrefs)!=null&&z.enableDevModePagination)&&i(!0)}function D(){const M=JSON.stringify(p==null?void 0:p.records,null,2),E=new Blob([M],{type:"application/json"}),B=URL.createObjectURL(E),$=document.createElement("a");$.href=B,$.download=`${a.title}.json`,$.click()}function F(){const E=[Object.keys(p==null?void 0:p.records.reduce((q,G)=>({...q,...G}),{})).join(","),...((p==null?void 0:p.records)||[]).map(q=>Object.values(q).join(","))].join(`
|
|
183
|
+
`),B=new Blob([E],{type:"text/csv"}),$=URL.createObjectURL(B),V=document.createElement("a");V.href=$,V.download=`${a.title}.csv`,V.click()}function I(){const M=`Can you get the documentation for ${a.type} chart?`;x(M)}return a?e.jsxs("div",{className:"relative isolate flex h-full flex-col py-2 pb-3",children:[e.jsxs("div",{className:"z-10 mb-0 flex min-h-10 items-center justify-end gap-2 px-6",children:[c&&e.jsxs("div",{className:"flex grow items-center",children:[e.jsx(Bs,{}),d&&e.jsx(s.IconButton,{tooltip:"Visual Docs",onClick:I,children:e.jsx(tt,{className:"size-4"})}),!["table","custom"].includes(a.type)&&d&&e.jsx($t,{})]}),!c&&e.jsxs("div",{className:"flex grow items-center gap-1",children:[e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:F,children:e.jsx(s.PiFileCsv,{className:"size-4"})}),e.jsx(s.Button,{className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70",variant:"ghost",size:"icon",onClick:D,children:e.jsx(rt,{strokeWidth:1.5,className:"size-4"})}),e.jsx(s.CopyButton,{text:JSON.stringify(p==null?void 0:p.records,null,2),className:"h-7 w-fit px-1 font-mono text-xs text-muted-foreground/70"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[t,e.jsx(s.IconButton,{tooltip:"Refresh",disabled:g||v,onClick:()=>{R(!0),y()},children:e.jsx(s.RefreshCcw,{className:s.cn("h-4 w-4",{"animate-spin":k})})}),e.jsx(Ps,{})]}),l&&e.jsx(Yt,{})]}),c?e.jsx(He,{}):e.jsxs(s.ResizablePanelGroup,{className:"px-6",direction:"horizontal",children:[r&&e.jsxs(e.Fragment,{children:[e.jsx(s.ResizablePanel,{id:"1",order:1,defaultSize:30,minSize:10,className:"relative flex h-full min-h-0 w-full flex-1 basis-0",children:r&&e.jsx(Jt,{})}),e.jsx(s.ResizableHandle,{})]}),e.jsxs(s.ResizablePanel,{id:"2",order:2,minSize:10,className:s.cn("relative flex h-full min-h-0 w-full flex-1 basis-0",{"px-6":r}),children:[(p==null?void 0:p.records)&&(p==null?void 0:p.records.length)>0&&e.jsx(s.TableVisual,{card:a,data:C,paginationMetadata:T,onPaginationChange:b,onSortChange:S,isLoading:g||v}),O&&e.jsx("div",{className:"flex h-full flex-col items-start justify-start overflow-auto p-4",children:e.jsx(Ye,{error:O,errorSql:A||a.sql||a.python,onDebugWithAssistant:N,showFeedbackButton:!1})})]})]})]}):e.jsx(e.Fragment,{children:"No Active Card"})}function Jt(){const t=s.useEditorStore(a=>a.pythonStdOut);if(!t)return null;const n=t.replace(/\n/g,"<br>");return e.jsxs("div",{className:"relative flex w-full flex-col",children:[e.jsx("div",{className:"sticky top-0 flex max-h-11 min-h-11 grow items-center font-mono text-xs font-semibold text-foreground/70",children:"Python Output"}),e.jsxs(s.ScrollArea,{className:"overflow-y-auto pt-1 font-mono text-xs text-foreground/90",children:[e.jsx("div",{dangerouslySetInnerHTML:{__html:n}}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})]})}function Yt(){const t=s.useEditorStore(a=>a.isShowingVisual),n=s.useEditorStore(a=>a.actions.setIsShowingVisual);return e.jsxs(s.Toggle,{pressed:!t,onPressedChange:()=>n(!t),variant:"outline",size:"sm",className:"h-8 gap-2",children:[e.jsx(s.Table2,{className:"h-4 w-4"}),e.jsx("span",{className:"text-sm",children:"Results"})]})}function oe({value:t,onCommit:n,type:a="text",placeholder:l,className:c}){const[d,r]=L.useState(t),i=L.useRef(!1);L.useEffect(()=>{r(t)},[t]);const o=()=>{if(i.current){i.current=!1;return}d!==t&&n(d)},u=m=>{if(m.key==="Enter"){m.currentTarget.blur();return}m.key==="Escape"&&(i.current=!0,r(t),m.currentTarget.blur())};return e.jsx(s.Input$1,{type:a,placeholder:l,value:d,onChange:m=>r(m.target.value),onBlur:o,onKeyDown:u,className:c})}function pe({defaultValue:t,label:n,options:a,value:l,onValueChange:c,className:d,icon:r,showPlaceholderLabel:i=!0}){return e.jsxs(s.Select,{defaultValue:t,value:l,onValueChange:c,children:[e.jsx(s.SelectTrigger,{className:s.cn("w-full focus:ring-0 focus:ring-offset-0",d),children:e.jsxs("div",{className:"flex items-center gap-2",children:[r&&e.jsx("span",{children:r}),i&&e.jsx(s.SelectValue,{placeholder:n})]})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:n}),a.map(o=>e.jsx(s.SelectItem,{value:o.value,children:o.label},o.value))]})})]})}const Qt={table:({children:t})=>e.jsx("table",{className:"my-3 w-full border-collapse overflow-hidden rounded-md border border-border text-sm",children:t}),thead:({children:t})=>e.jsx("thead",{className:"bg-muted",children:t}),th:({children:t})=>e.jsx("th",{className:"border border-border px-3 py-2 text-left font-medium",children:t}),td:({children:t})=>e.jsx("td",{className:"border border-border px-3 py-2",children:t}),code:({children:t,className:n})=>(n==null?void 0:n.includes("language-"))?e.jsx("pre",{className:"my-3 overflow-x-auto rounded-md bg-muted p-3 text-xs",children:e.jsx("code",{children:t})}):e.jsx("code",{className:"rounded bg-muted px-1 py-0.5 text-xs",children:t}),pre:({children:t})=>e.jsx(e.Fragment,{children:t})};function Vs(){var f,p;const t=s.useEditorStore(g=>g.card.customCardPreferences),{data:n}=s.usePluginQuery(),a=(n==null?void 0:n.plugins)||[],l=s.useEditorStore(g=>g.card),c=s.useEditorStore(g=>g.frame),d=(t==null?void 0:t.visualType)||"single",{setCustomCardPreferences:r}=s.useEditorActions(),{components:i,manifest:o}=s.useCustomVisual((t==null?void 0:t.url)||""),u=(f=o==null?void 0:o.visuals)==null?void 0:f.find(g=>g.name===(t==null?void 0:t.componentName));function m(g,v){if(!t)return;const A={...(t==null?void 0:t.dataInputCardIds)||{},[g]:{cardId:v,hookRef:null}};r({...t,dataInputCardIds:A})}function x(g){var O,A;const v=(O=c==null?void 0:c.cards)==null?void 0:O.filter(y=>{var T;return((T=y==null?void 0:y.customCardPreferences)==null?void 0:T.visualType)!=="multiple"}).map(y=>({value:y.id,label:y.tabTitle||y.title}));return e.jsx("div",{className:"space-y-2",children:(A=g==null?void 0:g.dataInputs)==null?void 0:A.map((y,T)=>{var N,C;return e.jsxs("div",{className:"grid grid-cols-10 gap-2",children:[e.jsx("div",{className:"col-span-4 flex items-center",children:y}),e.jsx(pe,{className:"col-span-6 h-8 w-full justify-between",label:"",options:v||[],value:((C=(N=t==null?void 0:t.dataInputCardIds)==null?void 0:N[T])==null?void 0:C.cardId)||"",onValueChange:k=>m(T,k)})]},T+Math.random())})})}function h(g,v,O=[],A=""){var y,T;return t?g==="input"?e.jsx(oe,{className:`h-8 w-full ${s.removeRing}`,value:String(((y=t==null?void 0:t.settings)==null?void 0:y[v])??A??""),onCommit:N=>{r({...t,settings:{...t==null?void 0:t.settings,[v]:N}})}},v):g==="select"?e.jsx(pe,{className:"h-8 w-full",label:"",options:O,value:((T=t==null?void 0:t.settings)==null?void 0:T[v])||A,onValueChange:N=>{r({...t,settings:{...t==null?void 0:t.settings,[v]:N}})}}):null:null}function w(g){const v=Object.entries((g==null?void 0:g.settings)||{});return e.jsx("div",{className:"space-y-3",children:v.map(([O,A])=>{var y;return e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{children:A.title}),((y=A.docs)==null?void 0:y.description)&&e.jsx(s.TooltipProvider,{delayDuration:0,children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-3.5 w-3.5 cursor-help text-muted-foreground"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:e.jsx("p",{className:"text-xs",children:A.docs.description})})]})})]}),h(A.ui,O,A.options,A.defaultValue)]},O)})})}function j(g){t&&r({...t,visualType:g,componentName:""})}return e.jsxs(s.Accordion,{type:"multiple",defaultValue:["component-settings"],className:"",children:[e.jsxs(s.AccordionItem,{value:"plugin-settings",children:[e.jsx(s.AccordionTrigger,{children:"Plugin"}),e.jsx(s.AccordionContent,{children:e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"space-y-4",children:[e.jsx(pe,{icon:e.jsx(Ss,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Plugin",options:a,value:(t==null?void 0:t.url)||"",onValueChange:g=>r({...t||{url:"",componentName:""},url:g})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&e.jsx(s.Tabs,{value:d,onValueChange:j,children:e.jsxs(s.TabsList,{className:"h-9 w-full",children:[e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"single",children:"Single Visual"}),e.jsx(s.TabsTrigger,{className:"h-7 w-full",value:"multiple",children:"Multi-Visual"})]})})]})}),((t==null?void 0:t.type)==="component"||(t==null?void 0:t.type)===void 0)&&t&&e.jsx(e.Fragment,{children:i&&e.jsx(pe,{icon:e.jsx(at,{className:"h-3.5 w-3.5"}),className:"h-9 w-full",label:"Select Component",options:(i==null?void 0:i.filter(g=>g.type===d).map(g=>({value:g.name,label:g.name})))||[],value:t.componentName,onValueChange:g=>{console.log("changing value",g),r({...t,componentName:g})}})})]})})]}),((p=l==null?void 0:l.customCardPreferences)==null?void 0:p.componentName)&&e.jsxs(s.AccordionItem,{className:"border-none",value:"component-settings",children:[e.jsx(s.AccordionTrigger,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{children:(u==null?void 0:u.name)||"Settings"}),(u==null?void 0:u.docs)&&e.jsxs(s.HoverCard,{openDelay:0,closeDelay:100,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,onClick:g=>g.stopPropagation(),children:e.jsx(s.Info,{className:"h-3.5 w-3.5 cursor-help text-muted-foreground transition-colors hover:text-foreground"})}),e.jsx(s.HoverCardContent,{side:"right",align:"start",className:"max-h-[40vh] w-[500px] overflow-y-auto text-left",children:e.jsxs("div",{className:"space-y-4 text-left",children:[u.docs.description&&e.jsx("p",{className:"text-sm text-muted-foreground",children:u.docs.description}),u.docs.dataSchema&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-semibold uppercase tracking-wide text-muted-foreground",children:"Data Schema"}),e.jsx("div",{className:"prose prose-sm dark:prose-invert prose-pre:bg-muted prose-pre:text-xs max-w-none",children:e.jsx(s.Markdown,{remarkPlugins:[s.remarkGfm],components:Qt,children:u.docs.dataSchema})})]}),u.docs.useCases&&u.docs.useCases.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-semibold uppercase tracking-wide text-muted-foreground",children:"Use Cases"}),e.jsx("ul",{className:"space-y-1 text-sm",children:u.docs.useCases.map((g,v)=>e.jsxs("li",{className:"flex items-start gap-2",children:[e.jsx("span",{className:"mt-1 text-primary",children:"•"}),e.jsx("span",{children:g})]},v))})]})]})})]})]})}),e.jsxs(s.AccordionContent,{children:[u&&u.dataInputs&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Input Mapping"}),e.jsx("div",{className:"px-2",children:x(u)})]}),u&&u.settings&&e.jsx("div",{className:"space-y-3",children:e.jsx("div",{className:"px-2",children:w(u)})})]})]})]})}function Wt({colorRanges:t,setColorRanges:n}){const a={start:-1/0,end:100,color:""};function l(r,i,o){const u=[...t];u[r]={...u[r],[i]:o},n(u)}function c(r){const i=[...t];i.splice(r,1),n(i)}function d(){n([...t,a])}return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{children:"Color Range"}),e.jsx(s.Button,{onClick:d,variant:"secondary",className:"h-5 p-1 text-muted-foreground",children:e.jsx(s.Plus,{className:"size-3"})})]}),t.map((r,i)=>e.jsxs("div",{className:"flex items-center gap-2 px-2",children:[e.jsx(s.Input$1,{onChange:o=>l(i,"start",o.target.value),value:r.start,className:s.cn(`h-7 ${s.removeRing}`)}),e.jsx("span",{children:"-"}),e.jsx(s.Input$1,{value:r.end,onChange:o=>l(i,"end",o.target.value),className:s.cn(`h-7 ${s.removeRing}`)}),e.jsx(s.Input$1,{onChange:o=>l(i,"color",o.target.value),value:r.color,className:s.cn(`h-7 min-w-14 p-0 ${s.removeRing}`),type:"color",list:"presetColors"}),e.jsxs("datalist",{id:"presetColors",children:[e.jsx("option",{children:s.colors.green[600]}),e.jsx("option",{children:s.colors.red[500]}),e.jsx("option",{children:s.colors.yellow[400]}),e.jsx("option",{children:s.colors.orange[500]})]}),e.jsx(s.Button,{onClick:()=>c(i),variant:"secondary",className:"h-5 p-1 text-muted-foreground",children:e.jsx(s.X,{className:"size-3"})})]},i))]})}function Xt(){var m,x,h,w,j;const t=s.useEditorStore(f=>f.card.preferences),n=s.useEditorStore(f=>f.actions.setCardPreferences),a=s.useEditorStore(f=>f.card),l=s.hasValidCardConfig(a.config),c=s.useEditorStore(f=>f.actions.setNumberFormat);s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.decimalPlaces});const d=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.locale}),r=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.currency});s.useEditorStore(f=>f.actions.setFilterOnClickField);const i=s.useEditorStore(f=>{var p,g;return(g=(p=f.card.preferences)==null?void 0:p.formatNumber)==null?void 0:g.colorRanges})||[],o=s.useEditorStore(f=>f.actions.setColorRanges),u=[...new Set(s.LOCALE_CURRENCY_PAIRS.map(f=>f.currency))];return e.jsxs("div",{children:[!l&&e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsx(s.Checkbox,{checked:!!((m=t==null?void 0:t.formatNumber)!=null&&m.enabled),onCheckedChange:f=>{n({...t,formatNumber:{...t==null?void 0:t.formatNumber,enabled:f}})}}),e.jsx(s.Label,{children:"Format Number"})]}),(((x=t==null?void 0:t.formatNumber)==null?void 0:x.enabled)===!0||l)&&e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx("div",{children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Locale"}),e.jsxs(s.Select,{value:d||"none",onValueChange:f=>{var p,g,v;return c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,((g=t==null?void 0:t.formatNumber)==null?void 0:g.currency)||"",f,((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Format"}),e.jsx(s.SelectItem,{value:"none",children:"Auto"},"none"),s.LOCALE_CURRENCY_PAIRS.map(f=>e.jsx(s.SelectItem,{value:f.locale,children:f.locale},f.locale))]})})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Currency"}),e.jsxs(s.Select,{value:r||"none",onValueChange:f=>{var p,g,v;console.log("currency",f),c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,f==="none"?"":f,((g=t==null?void 0:t.formatNumber)==null?void 0:g.locale)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-6 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Currency"}),e.jsx(s.SelectItem,{value:"none",children:"None"},"none"),u.map(f=>e.jsx(s.SelectItem,{value:f,children:f},f))]})})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Decimals"}),e.jsxs(s.Select,{value:((w=(h=t==null?void 0:t.formatNumber)==null?void 0:h.decimalPlaces)==null?void 0:w.toString())||"0",onValueChange:f=>{var p,g,v;return c(Number(f),((p=t==null?void 0:t.formatNumber)==null?void 0:p.currency)||"",((g=t==null?void 0:t.formatNumber)==null?void 0:g.locale)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.suffix)||"")},children:[e.jsx(s.SelectTrigger,{className:"col-span-3 h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:["0","1","2","3","4"].map(f=>e.jsx(s.SelectItem,{value:f,children:f},f))})]})]}),e.jsxs("div",{className:"w-full space-y-1",children:[e.jsx(s.Label,{children:"Suffix"}),e.jsx(s.Input$1,{value:((j=t==null?void 0:t.formatNumber)==null?void 0:j.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:f=>{var p,g,v;return c(((p=t==null?void 0:t.formatNumber)==null?void 0:p.decimalPlaces)||0,((g=t==null?void 0:t.formatNumber)==null?void 0:g.currency)||"",((v=t==null?void 0:t.formatNumber)==null?void 0:v.locale)||"",f.target.value)}})]})]})]})}),e.jsx(s.Separator,{className:"my-3"}),e.jsx(Wt,{colorRanges:i,setColorRanges:o})]})]})}const Zt={combo:{label:"Combo Chart",query:"SELECT x-axis, metric-1, metric-2, ... FROM table",fields:[{name:"x-axis",type:"category",description:"dimension"},{name:"metric-1, metric-2",type:"number",description:"can be bar, line, or area"}],description:"Combo charts allow mixing bar and line series with optional dual Y-axis for metrics with different scales.",docsUrl:"https://docs.semaphor.cloud/docs/charts/combo"},area:{label:"Area Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, city"},{name:"Y-axis",type:"number",description:"e.g., sales, population"}],description:"Area charts display quantitative data with filled areas under the line.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},stackedArea:{label:"Stacked Area Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, city"},{name:"Y-axis",type:"number",description:"e.g., sales, population"}],description:"Stacked area charts show how multiple series contribute to a total over time.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},bar:{label:"Bar Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., city, product"},{name:"Y-axis",type:"number",description:"e.g., sales, count"}],description:"Bar charts compare values across categories using horizontal or vertical bars.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},line:{label:"Line Chart",query:"SELECT x-axis, y-axis-1, y-axis-2, ... FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date, month"},{name:"Y-axis",type:"number",description:"e.g., revenue, count"}],description:"Line charts show trends over time or continuous data.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bar-line-area"},stackedBar:{label:"Stacked Bar Chart",query:"SELECT x-axis, stack-by, y-axis FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., month"},{name:"Stack-by",type:"category",description:"e.g., product type"},{name:"Y-axis",type:"number",description:"e.g., sales"}],description:"Stacked bar charts show how parts contribute to a whole across categories.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},stackedLine:{label:"Stacked Line Chart",query:"SELECT x-axis, stack-by, y-axis FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., date"},{name:"Stack-by",type:"category",description:"e.g., region"},{name:"Y-axis",type:"number",description:"e.g., revenue"}],description:"Stacked line charts show cumulative trends over time.",docsUrl:"https://docs.semaphor.cloud/docs/charts/stacked"},pie:{label:"Pie Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., product, region"},{name:"Measure",type:"number",description:"e.g., sales, count"}],description:"Pie charts show proportions of a whole.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},doughnut:{label:"Doughnut Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., product, region"},{name:"Measure",type:"number",description:"e.g., sales, count"}],description:"Doughnut charts show proportions with a hollow center.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},polarArea:{label:"Polar Area Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., skill, metric"},{name:"Measure",type:"number",description:"e.g., score, value"}],description:"Polar area charts compare values using sectors of equal angle but varying radius.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},radar:{label:"Radar Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., attribute, skill"},{name:"Measure",type:"number",description:"e.g., score, rating"}],description:"Radar charts display multivariate data on axes starting from the same point.",docsUrl:"https://docs.semaphor.cloud/docs/charts/pie-donut-polar"},funnel:{label:"Funnel Chart",query:"SELECT label, measure FROM table",fields:[{name:"Label",type:"category",description:"e.g., stage name"},{name:"Measure",type:"number",description:"e.g., count, value"}],description:"Funnel charts visualize progressive reduction of data through stages.",docsUrl:"https://docs.semaphor.cloud/docs/charts/funnel"},bubble:{label:"Bubble Chart",query:"SELECT label, [group], x-axis, y-axis, radius FROM table",fields:[{name:"Label",type:"category",description:"e.g., city"},{name:"Group",type:"category",description:"e.g., region",optional:!0},{name:"X-axis",type:"number",description:"e.g., population"},{name:"Y-axis",type:"number",description:"e.g., income"},{name:"Radius",type:"number",description:"e.g., area"}],description:"Bubble charts display three dimensions of data using position and size.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bubble"},scatter:{label:"Scatter Chart",query:"SELECT label, [group], x-axis, y-axis FROM table",fields:[{name:"Label",type:"category",description:"e.g., item name"},{name:"Group",type:"category",description:"e.g., type",optional:!0},{name:"X-axis",type:"number",description:"e.g., price"},{name:"Y-axis",type:"number",description:"e.g., quantity"}],description:"Scatter charts show relationships between two numeric variables.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bubble"},tornado:{label:"Tornado Chart",query:"SELECT dimension, cohort, metric FROM table",fields:[{name:"Dimension",type:"category",description:"e.g., age group"},{name:"Cohort",type:"category",description:"e.g., gender"},{name:"Metric",type:"number",description:"e.g., population"}],description:"Tornado charts compare two groups across categories using back-to-back bars.",docsUrl:"https://docs.semaphor.cloud/docs/charts/tornado"},pyramid:{label:"Pyramid Chart",query:"SELECT dimension, cohort, metric FROM table",fields:[{name:"Dimension",type:"category",description:"e.g., age group"},{name:"Cohort",type:"category",description:"e.g., gender"},{name:"Metric",type:"number",description:"e.g., population"}],description:"Population pyramids show age and gender distribution.",docsUrl:"https://docs.semaphor.cloud/docs/charts/tornado"},range:{label:"Range Chart",query:"SELECT y-axis, label, range-from, range-to, range-value FROM table",fields:[{name:"Y-axis",type:"category",description:"e.g., category"},{name:"Label",type:"category",description:"e.g., range name"},{name:"Range-from",type:"number",description:"start value"},{name:"Range-to",type:"number",description:"end value"},{name:"Range-value",type:"number",description:"e.g., midpoint"}],description:"Range charts display data ranges with start and end values.",docsUrl:"https://docs.semaphor.cloud/docs/charts/range"},heatmap:{label:"Heatmap",query:"SELECT x-axis, y-axis, value FROM table",fields:[{name:"X-axis",type:"category",description:"e.g., day"},{name:"Y-axis",type:"category",description:"e.g., hour"},{name:"Value",type:"number",description:"e.g., count, intensity"}],description:"Heatmaps visualize data density using color intensity.",docsUrl:"https://docs.semaphor.cloud/docs/charts/heatmap"},bullet:{label:"Bullet Chart",query:"SELECT label, actual, target, [ranges] FROM table",fields:[{name:"Label",type:"category",description:"e.g., metric name"},{name:"Actual",type:"number",description:"current value"},{name:"Target",type:"number",description:"goal value"},{name:"Ranges",type:"number",description:"threshold values",optional:!0}],description:"Bullet charts compare actual performance against targets.",docsUrl:"https://docs.semaphor.cloud/docs/charts/bullet"},text:{label:"Text Visual",query:"SELECT text FROM table LIMIT 1",fields:[{name:"Text",type:"string",description:"content to display"}],description:"Text visuals display dynamic text content from your data.",docsUrl:"https://docs.semaphor.cloud/docs/charts/text"},kpi:{label:"KPI",query:"SELECT current, previous FROM table",fields:[{name:"Current",type:"number",description:"current value"},{name:"Previous",type:"number",description:"comparison value"}],description:"KPI visuals display key performance indicators with comparison values.",docsUrl:"https://docs.semaphor.cloud/docs/charts/kpi"},map:{label:"Map Chart",query:"SELECT country, value FROM table",fields:[{name:"Country/Region",type:"category",description:"geographic identifier"},{name:"Value",type:"number",description:"e.g., population, sales"}],description:"Map charts visualize geographic data using color intensity.",docsUrl:"https://docs.semaphor.cloud/docs/charts/map"},table:{label:"Table",query:"SELECT column1, column2, ... FROM table",fields:[{name:"Columns",type:"category",description:"any data columns"}],description:"Tables display data in rows and columns.",docsUrl:"https://docs.semaphor.cloud/docs/charts/table"}};function Qe(t){return Zt[t]}function ea({chartType:t,showDocsLink:n=!0}){const a=Qe(t);return a?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-medium uppercase tracking-wide text-muted-foreground",children:"Query Format"}),e.jsx("code",{className:"block rounded-md bg-muted/50 px-3 py-2 font-mono text-xs text-foreground",children:a.query})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-xs font-medium uppercase tracking-wide text-muted-foreground",children:"Fields"}),e.jsx("div",{className:"rounded-md border",children:e.jsxs("table",{className:"w-full text-xs",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/50",children:[e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Field"}),e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Type"}),e.jsx("th",{className:"px-2.5 py-1.5 text-left font-medium text-muted-foreground",children:"Description"})]})}),e.jsx("tbody",{children:a.fields.map((l,c)=>e.jsxs("tr",{className:c<a.fields.length-1?"border-b":"",children:[e.jsxs("td",{className:"px-2.5 py-1.5 font-medium text-foreground",children:[l.name,l.optional&&e.jsx("span",{className:"ml-1 text-muted-foreground",children:"?"})]}),e.jsx("td",{className:"px-2.5 py-1.5 font-mono text-muted-foreground",children:l.type}),e.jsx("td",{className:"px-2.5 py-1.5 text-muted-foreground",children:l.description||"—"})]},l.name))})]})})]}),e.jsx("p",{className:"text-xs leading-relaxed text-muted-foreground",children:a.description}),n&&a.docsUrl&&e.jsxs("a",{href:a.docsUrl,target:"_blank",rel:"noopener noreferrer",onClick:l=>l.stopPropagation(),className:"inline-flex items-center gap-1.5 text-xs font-medium text-primary hover:underline",children:["View documentation",e.jsx(s.ExternalLink,{className:"h-3 w-3"})]})]}):null}function sa({chartType:t}){return Qe(t)?e.jsxs(s.HoverCard,{openDelay:200,children:[e.jsx(s.HoverCardTrigger,{asChild:!0,children:e.jsx("button",{type:"button",className:"rounded bg-muted px-2 py-0.5 font-mono text-[10px] font-semibold uppercase tracking-wider text-muted-foreground transition-colors hover:bg-muted/80 hover:text-foreground",children:"SQL"})}),e.jsx(s.HoverCardContent,{className:"w-80 p-4",side:"left",align:"start",sideOffset:8,children:e.jsx(ea,{chartType:t})})]}):null}function Fe({chartType:t,suffix:n="Configuration"}){const a=Qe(t),l=(a==null?void 0:a.label)||t;return e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h3",{className:"text-sm font-medium",children:[l," ",n]}),e.jsx(sa,{chartType:t})]})}function ta(){return e.jsxs("div",{className:"space-y-6 px-6 py-4",children:[e.jsx(Fe,{chartType:"kpi"}),e.jsx("section",{children:e.jsx(Xt,{})})]})}function ms({children:t,summary:n,open:a,className:l}){return e.jsxs("details",{open:a,className:s.cn("space-y-4 [&_summary>svg]:open:-rotate-180",l),children:[e.jsxs("summary",{className:"flex cursor-pointer list-none items-center justify-between gap-4",children:[e.jsx(s.Label,{children:n}),e.jsx(s.ChevronUp,{className:"h-4 w-4"})]}),e.jsx("div",{className:"transition-all ease-in-out",children:t})]})}function zs(){const[t,n]=L.useState([]),a=s.useEditorStore(u=>u.card.preferences),{setFilterOnClickColumnIndex:l,setCardPreferences:c}=s.useEditorStore(u=>u.actions),d=s.useEditorStore(u=>u.queryResultColumns);s.useEditorStore(u=>{var m;return(m=u.card.preferences)==null?void 0:m.filterOnClickColumnIndex}),L.useEffect(()=>{d&&n(new Array(d.length).fill(!1))},[d]);function r(u,m){var x,h;m?a!=null&&a.onClickFilter&&((x=a==null?void 0:a.onClickFilter)==null?void 0:x.length)>0?c({...a,onClickFilter:[...a.onClickFilter,{columnIndex:u,expression:""}]}):c({...a,onClickFilter:[{columnIndex:u,expression:""}]}):(c({...a,onClickFilter:(h=a==null?void 0:a.onClickFilter)==null?void 0:h.filter(w=>w.columnIndex!==u)}),n(w=>{const j=[...w];return j[u]=!1,j}))}function i(u,m){var h;const x=(h=a==null?void 0:a.onClickFilter)==null?void 0:h.map(w=>w.columnIndex===u?{...w,expression:m.target.value}:w);c({...a,onClickFilter:x})}function o(u){var m,x,h,w;if(t!=null&&t[u]||(x=(m=a==null?void 0:a.onClickFilter)==null?void 0:m.find(j=>j.columnIndex===u))!=null&&x.expression)return e.jsx(s.Input$1,{value:(w=(h=a==null?void 0:a.onClickFilter)==null?void 0:h.find(j=>j.columnIndex===u))==null?void 0:w.expression,onChange:j=>i(u,j),type:"text",className:"h-9 w-full font-mono text-xs focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0",placeholder:"Expression"})}return e.jsx("div",{className:"max-h-[150px] space-y-2 overflow-y-auto",children:d==null?void 0:d.map((u,m)=>{var x,h;return e.jsx("div",{className:"group flex items-center justify-between space-y-2",children:e.jsxs("div",{className:"grow",children:[e.jsxs("div",{className:"flex items-center space-x-2 py-2",children:[e.jsx(s.Checkbox,{checked:!!((x=a==null?void 0:a.onClickFilter)!=null&&x.find(w=>w.columnIndex===m)),onCheckedChange:w=>r(m,w)}),e.jsx("label",{className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:u}),e.jsx("div",{className:"flex items-center gap-2",children:((h=a==null?void 0:a.onClickFilter)==null?void 0:h.find(w=>w.columnIndex===m))&&e.jsx(s.Pencil,{onClick:()=>{n(w=>{const j=[...w];return j[m]=!w[m],j})},className:"invisible size-3 cursor-pointer text-gray-400 group-hover:visible"})})]}),o(m)]})},u)})})}const aa=[{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"}],na=["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"],ra=na.map(t=>({label:t,value:t}));function la(){var o,u,m,x,h,w,j,f,p,g,v,O,A,y,T,N,C,k;const t=s.useEditorStore(R=>R.card),n=(u=(o=t==null?void 0:t.preferences)==null?void 0:o.mapVisualOptions)==null?void 0:u.topoJsonUrl,[a,l]=L.useState(((x=(m=t==null?void 0:t.preferences)==null?void 0:m.mapVisualOptions)==null?void 0:x.customTopoJsonUrl)||""),{setCardPreferences:c}=s.useEditorActions(),{data:d}=s.useTopoJson(t),r=Object.keys((d==null?void 0:d.objects)||{}).map(R=>({label:R,value:R}))||[],i=[{label:"US States 10m",value:"https://unpkg.com/us-atlas/states-10m.json"},{label:"Countries 50m",value:"https://unpkg.com/world-atlas/countries-50m.json"},{label:"Custom",value:"custom"}];return e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Fe,{chartType:"map"}),e.jsx(ms,{open:!0,summary:"Settings",children:e.jsxs("div",{className:"space-y-3 px-2",children:[e.jsx(pe,{className:"h-9",options:i,onValueChange:R=>{console.log("value",R),c({...t.preferences,mapVisualOptions:{topoJsonUrl:R}})},value:n||"",label:"Select Map"}),n==="custom"&&e.jsx(s.Input$1,{onChange:R=>{l(R.target.value)},onBlur:()=>{var R;console.log("onBlur",a),c({...t.preferences,mapVisualOptions:{...(R=t.preferences)==null?void 0:R.mapVisualOptions,customTopoJsonUrl:a}})},value:a,className:`h-9 w-full ${s.removeRing}`,placeholder:"Custom URL"}),d&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Map Object"}),e.jsx(pe,{className:"h-9",options:r,value:((w=(h=t.preferences)==null?void 0:h.mapVisualOptions)==null?void 0:w.objectKey)||"",label:"Map Object",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,objectKey:R}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Projection"}),e.jsx(pe,{className:"h-9",options:aa,value:((f=(j=t.preferences)==null?void 0:j.mapVisualOptions)==null?void 0:f.projection)||"",label:"Projection",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projection:R}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{children:"Colors"}),e.jsx(pe,{className:"h-9",options:ra,value:((g=(p=t.preferences)==null?void 0:p.mapVisualOptions)==null?void 0:g.colorScale)||"",label:"Select Color Scale",onValueChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,colorScale:R}})}})]})]})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(ms,{className:"",summary:"Scale Options",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:.1,value:((O=(v=t.preferences)==null?void 0:v.mapVisualOptions)==null?void 0:O.projectionScale)||1,onChange:R=>{var b;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionScale:Number(R.target.value)}})}}),e.jsx(s.Label,{children:"X"}),e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((T=(y=(A=t.preferences)==null?void 0:A.mapVisualOptions)==null?void 0:y.projectionOffset)==null?void 0:T[0])||0,onChange:R=>{var b,S,D,F;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionOffset:[Number(R.target.value),((F=(D=(S=t.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:F[1])||0]}})}}),e.jsx(s.Label,{children:"Y"}),e.jsx(s.Input$1,{className:`h-9 w-full ${s.removeRing}`,type:"number",step:10,value:((k=(C=(N=t.preferences)==null?void 0:N.mapVisualOptions)==null?void 0:C.projectionOffset)==null?void 0:k[1])||0,onChange:R=>{var b,S,D,F;c({...t.preferences,mapVisualOptions:{...(b=t.preferences)==null?void 0:b.mapVisualOptions,projectionOffset:[((F=(D=(S=t.preferences)==null?void 0:S.mapVisualOptions)==null?void 0:D.projectionOffset)==null?void 0:F[0])||0,Number(R.target.value)]}})}})]})}),e.jsx(s.Separator,{className:"my-4"}),e.jsx(zs,{})]})}function oa(){const t=s.useDashboardStore(i=>i.dashboard.filters),n=s.useEditorStore(i=>i.frame),{setFrame:a}=s.useEditorActions(),{columns:l}=s.useEditorAside(),{assignFilter:c}=s.useAssignFilter();function d(i){const o=c(i,"frame");a({...n,filterId:o})}const r=t==null?void 0:t.find(i=>i.id===n.filterId);return e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{children:"Card Filter"}),e.jsx(ia,{onSelect:d,columns:l}),r&&e.jsx(s.FilterHeader,{filter:r})]})}function ia({columns:t,onSelect:n}){var r;const[a,l]=_e.useState(!1),[c,d]=_e.useState("");return e.jsxs(s.Popover,{open:a,onOpenChange:l,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{size:"sm",variant:"outline",role:"combobox","aria-expanded":a,className:"w-full justify-between font-normal",children:[c?(r=t.find(i=>i.column_name===c))==null?void 0:r.column_name:"Select Column...",e.jsx(s.CaretSortIcon,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[200px] p-0",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search column",className:"h-9"}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No framework found."}),e.jsx(s.CommandGroup,{children:t==null?void 0:t.map(i=>e.jsxs(s.CommandItem,{value:i.column_name,onSelect:o=>{const u=o===c?"":o;d(u),l(!1),u&&n(i)},children:[i.column_name,e.jsx(s.CheckIcon,{className:s.cn("ml-auto h-4 w-4",c===i.column_name?"opacity-100":"opacity-0")})]},i.column_name))})]})]})})]})}function ca({cardType:t="table"}){console.log("cardType",t);const n=s.useEditorStore(c=>{var d;return(d=c.card)==null?void 0:d.preferences}),a=s.useEditorStore(c=>c.actions.setCardPreferences),l=c=>{a({...n,allowDownload:c})};return e.jsxs("div",{className:"space-y-6 px-6 py-2",children:[e.jsx(s.Accordion,{defaultValue:["item-3"],type:"multiple",children:e.jsxs(s.AccordionItem,{value:"item-3",children:[e.jsx(s.AccordionTrigger,{children:"Click Filters"}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-2",children:[e.jsx(zs,{}),e.jsx(oa,{})]})]})}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.Checkbox,{onCheckedChange:l,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"})]})]})}function da(){var l;const t=s.useEditorStore(c=>c.card.preferences),{setCardPreferences:n}=s.useEditorStore(c=>c.actions);function a(c){n({...t,textVisualOptions:{...t==null?void 0:t.textVisualOptions,isDynamicText:c==="dynamic"}})}return e.jsxs(s.RadioGroup,{value:(l=t==null?void 0:t.textVisualOptions)!=null&&l.isDynamicText?"dynamic":"static",onValueChange:a,className:"flex gap-3",defaultValue:"x",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"static",id:"r1"}),e.jsx("label",{className:"text-sm",htmlFor:"r1",children:"Static"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.RadioGroupItem,{value:"dynamic",id:"r2"}),e.jsx("label",{className:"text-sm",htmlFor:"r2",children:"Dynamic"})]})]})}function ua(){return e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Fe,{chartType:"text"}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Text Type"}),e.jsx(da,{})]})]})}function $s({color:t,onColorChange:n,onClose:a}){const[l,c]=L.useState(0),[d,r]=L.useState(100),[i,o]=L.useState(50),[u,m]=L.useState(t);L.useEffect(()=>{const p=xs(t);if(p){const g=hs(p.r,p.g,p.b);c(g.h),r(g.s),o(g.l),m(t)}},[t]);const x=p=>{c(p),w(p,d,i)},h=p=>{const g=p.currentTarget.getBoundingClientRect(),v=Math.max(0,Math.min(1,(p.clientX-g.left)/g.width)),O=Math.max(0,Math.min(1,(p.clientY-g.top)/g.height)),A=v*100,y=(1-O)*100;r(A),o(y),w(l,A,y)},w=(p,g,v)=>{const O=xa(p,g,v),A=ma(O.r,O.g,O.b);m(A)},j=p=>{if(m(p),/^#[0-9A-Fa-f]{6}$/.test(p)){const g=xs(p);if(g){const v=hs(g.r,g.g,g.b);c(v.h),r(v.s),o(v.l)}}},f=()=>{/^#[0-9A-Fa-f]{6}$/.test(u)&&(n(u),a())};return e.jsxs("div",{className:"p-4 space-y-4 w-[320px]",children:[e.jsxs("div",{className:"relative h-[200px] rounded cursor-crosshair",style:{background:`hsl(${l}, 100%, 50%)`},onClick:h,onMouseMove:p=>{p.buttons===1&&h(p)},children:[e.jsx("div",{className:"absolute inset-0 rounded",style:{background:"linear-gradient(to right, white, transparent)"}}),e.jsx("div",{className:"absolute inset-0 rounded",style:{background:"linear-gradient(to bottom, transparent, black)"}}),e.jsx("div",{className:"absolute w-4 h-4 -ml-2 -mt-2 rounded-full border-2 border-white shadow-md",style:{left:`${d}%`,top:`${100-i}%`,backgroundColor:u}})]}),e.jsx("div",{className:"space-y-2",children:e.jsx("input",{type:"range",min:"0",max:"360",value:l,onChange:p=>x(Number(p.target.value)),className:"w-full h-4 rounded-lg appearance-none cursor-pointer",style:{background:"linear-gradient(to right, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000)"}})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"HEX"}),e.jsx(s.Input$1,{value:u,onChange:p=>j(p.target.value),placeholder:"#000000",className:"font-mono"})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"w-12 h-12 rounded-full border shadow-sm",style:{backgroundColor:t}}),e.jsx("span",{className:"text-xs text-muted-foreground mt-1",children:"CURRENT"})]}),e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"w-12 h-12 rounded-full border shadow-sm",style:{backgroundColor:u}}),e.jsx("span",{className:"text-xs text-muted-foreground mt-1",children:"NEW"})]})]}),e.jsxs("div",{className:"flex gap-2 justify-end",children:[e.jsx(s.Button,{variant:"outline",onClick:a,children:"Cancel"}),e.jsx(s.Button,{onClick:f,children:"OK"})]})]})}function xs(t){const n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16)}:null}function ma(t,n,a){return"#"+((1<<24)+(t<<16)+(n<<8)+a).toString(16).slice(1)}function hs(t,n,a){t/=255,n/=255,a/=255;const l=Math.max(t,n,a),c=Math.min(t,n,a);let d=0,r=0;const i=(l+c)/2;if(l!==c){const o=l-c;switch(r=i>.5?o/(2-l-c):o/(l+c),l){case t:d=((n-a)/o+(n<a?6:0))/6;break;case n:d=((a-t)/o+2)/6;break;case a:d=((t-n)/o+4)/6;break}}return{h:Math.round(d*360),s:Math.round(r*100),l:Math.round(i*100)}}function xa(t,n,a){t/=360,n/=100,a/=100;let l,c,d;if(n===0)l=c=d=a;else{const r=(u,m,x)=>(x<0&&(x+=1),x>1&&(x-=1),x<.16666666666666666?u+(m-u)*6*x:x<.5?m:x<.6666666666666666?u+(m-u)*(.6666666666666666-x)*6:u),i=a<.5?a*(1+n):a+n-a*n,o=2*a-i;l=r(o,i,t+1/3),c=r(o,i,t),d=r(o,i,t-1/3)}return{r:Math.round(l*255),g:Math.round(c*255),b:Math.round(d*255)}}const ha=["#000000","#ffffff","#6b7280","#dc2626","#ea580c","#ca8a04","#16a34a","#0891b2","#2563eb","#7c3aed","#c026d3","#e11d48","#0f172a","#f3f4f6","#fbbf24"];function pa(){var O,A,y,T,N,C,k,R,b,S;const t=s.useEditorStore(D=>D.card),n=s.useEditorStore(D=>{var F;return(F=D.card)==null?void 0:F.preferences}),{setCardPreferences:a,updateCardInFrame:l}=s.useEditorActions(),{data:c}=s.useDashboardCardQuery(t),[d,r]=L.useState({}),[i,o]=L.useState(null),[u,m]=L.useState(null),x=L.useMemo(()=>{var M,E,B,$,V,q,G;if(!(c!=null&&c.records)||c.records.length===0)return[];const D=(t==null?void 0:t.type)==="stackedBar"||(t==null?void 0:t.type)==="stackedLine",F=(t==null?void 0:t.type)==="bar"||(t==null?void 0:t.type)==="line"||(t==null?void 0:t.type)==="horizontalBar"||(t==null?void 0:t.type)==="bullet"||(t==null?void 0:t.type)==="combo"||(t==null?void 0:t.type)==="area"||(t==null?void 0:t.type)==="stackedArea",I=((M=t==null?void 0:t.config)==null?void 0:M.pivotByColumns)&&t.config.pivotByColumns.length>0;if((D||F)&&I){if(c.pivotSchema&&Array.isArray(c.pivotSchema)){const z=new Set;return c.pivotSchema.forEach(H=>{H.alias&&z.add(H.alias)}),Array.from(z)}if(c.records.length>0){const z=c.records[0],H=Object.keys(z),_=new Set;(E=t==null?void 0:t.config)!=null&&E.groupByColumns&&t.config.groupByColumns.forEach(Z=>{_.add(Z.label||Z.name),_.add(Z.alias||Z.name)});const P=new Set;return(B=t==null?void 0:t.config)!=null&&B.metricColumns&&t.config.metricColumns.forEach(Z=>{const se=Z.alias||Z.name;P.add(se),P.add(`${se}_count`),P.add(`${se}_sum`),P.add(`${se}_avg`),P.add(`${se}_min`),P.add(`${se}_max`);const W=Z.entityName||Z.qualifiedEntityName;W&&(P.add(`${W}_${se}`),P.add(`${W}_${se}_count`))}),H.filter(Z=>!_.has(Z)&&!P.has(Z))}}if((F||D)&&!I&&((V=($=t==null?void 0:t.config)==null?void 0:$.metricColumns)!=null&&V.length))return t.config.metricColumns.map(z=>z.alias||z.label||z.name);if((G=(q=t==null?void 0:t.config)==null?void 0:q.groupByColumns)!=null&&G.length){const z=t.config.groupByColumns[0],H=z.label||z.name,_=new Set;return c.records.forEach(P=>{const Y=P[H];Y!=null&&_.add(String(Y))}),Array.from(_)}return[]},[c,(O=t==null?void 0:t.config)==null?void 0:O.groupByColumns,(A=t==null?void 0:t.config)==null?void 0:A.pivotByColumns,(y=t==null?void 0:t.config)==null?void 0:y.metricColumns,t==null?void 0:t.type]),h=L.useMemo(()=>s.getDefaultChartColors(),[]);L.useEffect(()=>{const D={};x.forEach((F,I)=>{D[F]=s.getColorForValue(F,I,n==null?void 0:n.colorConfig)}),r(D)},[x,n==null?void 0:n.colorConfig,h]);const w=(D,F)=>{const I={...d,[D]:F};r(I);const M={...n||{},colorConfig:{segments:I}};a(M);const E={...t,preferences:M};l(E)},j=(D,F)=>{var V;const I=h[F%h.length],{[D]:M,...E}=((V=n==null?void 0:n.colorConfig)==null?void 0:V.segments)||{},B={...n||{},colorConfig:{segments:E}};a(B);const $={...t,preferences:B};l($),r(q=>({...q,[D]:I}))};if(!x.length||!((t==null?void 0:t.type)==="stackedBar"||(t==null?void 0:t.type)==="stackedLine"?((N=(T=t==null?void 0:t.config)==null?void 0:T.groupByColumns)==null?void 0:N.length)||((k=(C=t==null?void 0:t.config)==null?void 0:C.pivotByColumns)==null?void 0:k.length):(b=(R=t==null?void 0:t.config)==null?void 0:R.groupByColumns)==null?void 0:b.length)||["table","kpi","text","custom","heatmap"].includes(t.type))return null;const g=()=>{const D={...n||{},colorConfig:{segments:{}}};a(D);const F={...t,preferences:D};l(F);const I={};x.forEach((M,E)=>{I[M]=s.getColorForValue(M,E,void 0)}),r(I)},v=Object.keys(((S=n==null?void 0:n.colorConfig)==null?void 0:S.segments)||{}).length>0;return e.jsxs(s.AccordionItem,{value:"colors",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(gt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Colors"})]})}),e.jsx(s.AccordionContent,{className:"",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"space-y-2",children:e.jsx("div",{className:"flex items-center justify-between",children:v&&e.jsxs("button",{className:"ml-auto flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground",onClick:g,children:[e.jsx(ls,{className:"h-3 w-3"}),"Reset All"]})})}),e.jsx("div",{className:"max-h-[200px] space-y-2 overflow-y-auto pr-1",children:x.map((D,F)=>{const I=s.isDefaultColor(d[D],F),M=!I;return e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Popover,{open:i===D,onOpenChange:E=>o(E?D:null),children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx("button",{className:"h-5 w-5 shrink-0 rounded border border-input shadow-sm transition-colors hover:border-gray-400 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",style:{backgroundColor:d[D]}})}),e.jsx(s.PopoverContent,{align:"start",className:"w-auto p-3",children:e.jsxs("div",{className:"space-y-2.5",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Theme Colors"}),e.jsx("div",{className:"grid grid-cols-5 gap-1.5",children:h.slice(0,10).map((E,B)=>e.jsxs("button",{className:s.cn("relative h-5 w-5 rounded border-2 transition-all hover:scale-110",d[D]===E?"border-gray-900":"border-transparent"),style:{backgroundColor:E},onClick:()=>{w(D,E),o(null)},children:[B===F%h.length&&e.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:e.jsx("div",{className:"rounded-full bg-white/90 px-0.5 text-[8px] font-bold text-gray-900 shadow-sm",children:"D"})}),e.jsx("span",{className:"sr-only",children:E})]},`default-${B}`))})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Popular Colors"}),e.jsx("div",{className:"grid grid-cols-5 gap-1.5",children:ha.map((E,B)=>e.jsx("button",{className:s.cn("h-5 w-5 rounded border transition-all hover:scale-110",d[D]===E?"border-2 border-gray-900":E==="#ffffff"?"border border-gray-300":"border border-transparent"),style:{backgroundColor:E},onClick:()=>{w(D,E),o(null)},children:e.jsx("span",{className:"sr-only",children:E})},`preset-${B}`))})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 pt-1",children:[e.jsx("button",{className:"text-sm font-medium text-blue-600 hover:text-blue-700",onClick:()=>{m({value:D,color:d[D]}),o(null)},children:"Custom color"}),!I&&e.jsxs("button",{className:"flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground",onClick:()=>{j(D,F),o(null)},children:[e.jsx(ls,{className:"h-3 w-3"}),"Reset"]})]})]})})]}),e.jsx("span",{className:"flex-1 truncate text-sm",children:D}),M&&e.jsx("span",{className:"text-xs text-muted-foreground",children:"(custom)"})]},D)})}),u&&e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[e.jsx("div",{className:"absolute inset-0 bg-black/50",onClick:()=>m(null)}),e.jsx("div",{className:"relative rounded-lg bg-white shadow-lg",children:e.jsx($s,{color:u.color,onColorChange:D=>{w(u.value,D),m(null)},onClose:()=>m(null)})})]})]})})]})}function ie({value:t,onSave:n,placeholder:a="",type:l="text",className:c=""}){const[d,r]=L.useState(t??""),[i,o]=L.useState(!1);L.useEffect(()=>{r(t??"")},[t]);const u=()=>{n(d),o(!1)},m=()=>{r(t??""),o(!1)};return e.jsxs("div",{className:`flex items-center gap-2 ${c}`,children:[e.jsx(s.Input,{placeholder:a,type:l,value:i?d:t??"",onChange:x=>{r(x.target.value),o(!0)},onFocus:()=>o(!0),onClick:x=>x.stopPropagation(),className:"h-8 text-xs",onPointerDown:x=>x.stopPropagation(),onKeyDown:x=>{x.stopPropagation(),x.key==="Enter"&&i&&d!==t?u():x.key==="Escape"&&m()},"aria-label":a||"Edit field"}),i&&d!==t&&e.jsxs(e.Fragment,{children:[e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm edit",onClick:u,children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel edit",onClick:m,children:e.jsx(s.X,{className:"h-4 w-4 text-muted-foreground"})})]})]})}const ga=["auto","number","currency","percent","scientific","date"],fa=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"GBP",label:"GBP - British Pound"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"CHF",label:"CHF - Swiss Franc"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"},{value:"HKD",label:"HKD - Hong Kong Dollar"},{value:"SGD",label:"SGD - Singapore Dollar"},{value:"NZD",label:"NZD - New Zealand Dollar"},{value:"SEK",label:"SEK - Swedish Krona"},{value:"NOK",label:"NOK - Norwegian Krone"},{value:"DKK",label:"DKK - Danish Krone"},{value:"KRW",label:"KRW - South Korean Won"},{value:"INR",label:"INR - Indian Rupee"},{value:"RUB",label:"RUB - Russian Ruble"},{value:"BRL",label:"BRL - Brazilian Real"},{value:"MXN",label:"MXN - Mexican Peso"},{value:"TRY",label:"TRY - Turkish Lira"},{value:"ZAR",label:"ZAR - South African Rand"},{value:"SAR",label:"SAR - Saudi Riyal"},{value:"AED",label:"AED - UAE Dirham"},{value:"ARS",label:"ARS - Argentine Peso"},{value:"CLP",label:"CLP - Chilean Peso"},{value:"COP",label:"COP - Colombian Peso"},{value:"THB",label:"THB - Thai Baht"},{value:"IDR",label:"IDR - Indonesian Rupiah"},{value:"MYR",label:"MYR - Malaysian Ringgit"},{value:"PHP",label:"PHP - Philippine Peso"},{value:"PLN",label:"PLN - Polish Zloty"},{value:"NGN",label:"NGN - Nigerian Naira"},{value:"KES",label:"KES - Kenyan Shilling"},{value:"GHS",label:"GHS - Ghanaian Cedi"},{value:"ZMW",label:"ZMW - Zambian Kwacha"},{value:"ZWL",label:"ZWL - Zimbabwean Dollar"},{value:"EGP",label:"EGP - Egyptian Pound"},{value:"MAD",label:"MAD - Moroccan Dirham"},{value:"UAH",label:"UAH - Ukrainian Hryvnia"},{value:"HUF",label:"HUF - Hungarian Forint"},{value:"CZK",label:"CZK - Czech Koruna"},{value:"RON",label:"RON - Romanian Leu"},{value:"PKR",label:"PKR - Pakistani Rupee"},{value:"BDT",label:"BDT - Bangladeshi Taka"},{value:"VND",label:"VND - Vietnamese Dong"},{value:"TWD",label:"TWD - New Taiwan Dollar"},{value:"ILS",label:"ILS - Israeli New Shekel"}],ja=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"en-IN",label:"English (India)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"},{value:"ja-JP",label:"Japanese"},{value:"ko-KR",label:"Korean"},{value:"ru-RU",label:"Russian"},{value:"ar-SA",label:"Arabic"},{value:"hi-IN",label:"Hindi"},{value:"nl-NL",label:"Dutch"},{value:"sv-SE",label:"Swedish"}],ba=t=>t.charAt(0).toUpperCase()+t.slice(1);function We({formatOptions:t={},onFormatOptionsChange:n,showTitle:a=!0,title:l="NUMBER FORMAT"}){const c=t.type||"auto",d=(i,o)=>{n({...t,[i]:o})},r=()=>c==="currency"?2:c==="percent"?1:0;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[a&&e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:l}),e.jsxs(s.Select,{value:c,onValueChange:i=>d("type",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:ga.map(i=>e.jsx(s.SelectItem,{value:i,className:"text-xs",children:i==="auto"?"Auto (Default)":ba(i)},i))})]})]}),c!=="auto"&&e.jsxs("div",{className:"space-y-2 border-l-2 border-muted pl-4",children:[["number","currency","percent"].includes(c)&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Decimals"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"number",min:"0",max:"10",placeholder:"0",value:t.decimalPlaces??r(),onChange:i=>d("decimalPlaces",parseInt(i.target.value)||0),className:"h-8 text-xs"})})]}),c==="currency"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Currency"}),e.jsxs(s.Select,{value:t.currency||"USD",onValueChange:i=>d("currency",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 flex-1 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:fa.map(i=>e.jsx(s.SelectItem,{value:i.value,className:"text-xs",children:i.label},i.value))})]})]}),["number","currency"].includes(c)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:t.useSuffix??!1,onCheckedChange:i=>d("useSuffix",i)})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Locale"}),e.jsxs(s.Select,{value:t.locale||"en-US",onValueChange:i=>d("locale",i),children:[e.jsx(s.SelectTrigger,{className:"h-8 flex-1 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:ja.map(i=>e.jsx(s.SelectItem,{value:i.value,className:"text-xs",children:i.label},i.value))})]})]}),c==="number"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Prefix"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{placeholder:"e.g., $",value:t.prefix||"",onChange:i=>d("prefix",i.target.value),className:"h-8 text-xs"})})]}),c==="number"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Suffix"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{placeholder:"e.g., units",value:t.suffix||"",onChange:i=>d("suffix",i.target.value),className:"h-8 text-xs"})})]}),["number","currency"].includes(c)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Negative in ()"}),e.jsx(s.SmallSwitch,{checked:t.negativeInParentheses??!1,onCheckedChange:i=>d("negativeInParentheses",i)})]}),c==="percent"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Multiply By"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"number",placeholder:"100",value:t.multiplyBy??"",onChange:i=>d("multiplyBy",i.target.value?parseFloat(i.target.value):void 0),className:"h-8 text-xs"})})]}),c==="date"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-20 text-xs text-muted-foreground",children:"Format"}),e.jsx("div",{className:"flex-1",children:e.jsx(s.Input$1,{type:"text",placeholder:"MM/DD/YYYY",value:t.dateFormat||"",onChange:i=>d("dateFormat",i.target.value),className:"h-8 text-xs"})})]})]})]})}function qe({axis:t,label:n,type:a,config:l,onChange:c,defaultTitle:d,hidePosition:r}){var w,j,f,p,g,v,O,A,y,T,N,C,k,R;const i=(b,S)=>{c({...l??{},[b]:S})},o=(b,S)=>{var M,E,B,$;const D=b==="enabled"&&S===!0,F=(M=l==null?void 0:l.name)==null?void 0:M.text,I=D&&!F&&d;c({...l??{},name:{...l==null?void 0:l.name,enabled:b==="enabled"?S:((E=l==null?void 0:l.name)==null?void 0:E.enabled)??!1,text:b==="text"?S:I?d:F,fontSize:b==="fontSize"?S:(B=l==null?void 0:l.name)==null?void 0:B.fontSize,fontWeight:b==="fontWeight"?S:($=l==null?void 0:l.name)==null?void 0:$.fontWeight}})},u=(b,S)=>{var D,F;c({...l??{},labels:{enabled:b==="enabled"?S:((D=l==null?void 0:l.labels)==null?void 0:D.enabled)??!0,rotation:b==="rotation"?S:(F=l==null?void 0:l.labels)==null?void 0:F.rotation}})},m=(b,S)=>{c({...l??{},scale:{...l==null?void 0:l.scale,[b]:S===""?"auto":Number(S)}})},x=t==="x"?["auto","top","bottom"]:["auto","left","right"],h=["auto","0","45","90","-45","-90"];return e.jsxs(s.AccordionItem,{value:`${t}-axis`,className:"border-b-0",children:[e.jsxs("div",{className:"flex w-full items-center justify-between gap-2",children:[e.jsx(s.AccordionTrigger,{className:"flex grow items-center justify-between gap-3 py-2 text-sm font-medium hover:no-underline",children:e.jsx("div",{className:"flex grow items-center justify-between gap-2",children:e.jsx("span",{children:n})})}),e.jsx(s.SmallSwitch,{checked:(l==null?void 0:l.enabled)!==!1,onCheckedChange:b=>i("enabled",b),onClick:b=>b.stopPropagation()})]}),e.jsxs(s.AccordionContent,{className:"space-y-4 pt-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:`${t}-title-enabled`,checked:((w=l==null?void 0:l.name)==null?void 0:w.enabled)??!1,onCheckedChange:b=>o("enabled",b===!0)}),e.jsx(s.Label,{htmlFor:`${t}-title-enabled`,className:"text-xs font-medium cursor-pointer",children:"TITLE"})]}),((j=l==null?void 0:l.name)==null?void 0:j.enabled)&&e.jsxs("div",{className:"space-y-2 pl-6",children:[e.jsx(ie,{placeholder:`${n} title`,value:((f=l==null?void 0:l.name)==null?void 0:f.text)||"",onSave:b=>o("text",b),className:""}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Size"}),e.jsx("div",{className:"w-14",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((p=l==null?void 0:l.name)==null?void 0:p.fontSize)||12),onSave:b=>o("fontSize",Number(b)||12),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Weight"}),e.jsxs(s.Select,{value:((g=l==null?void 0:l.name)==null?void 0:g.fontWeight)||"normal",onValueChange:b=>o("fontWeight",b),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]})]})]})]}),!r&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"POSITION"}),e.jsxs(s.Select,{value:(l==null?void 0:l.position)||"auto",onValueChange:b=>i("position",b),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:x.map(b=>e.jsx(s.SelectItem,{value:b,className:"text-xs",children:b==="auto"?"Auto (Default)":s.titleCase(b)},b))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"LABELS"}),e.jsx(s.SmallSwitch,{checked:((v=l==null?void 0:l.labels)==null?void 0:v.enabled)!==!1,onCheckedChange:b=>u("enabled",b)})]}),((O=l==null?void 0:l.labels)==null?void 0:O.enabled)!==!1&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Rotation"}),e.jsxs(s.Select,{value:String(((A=l==null?void 0:l.labels)==null?void 0:A.rotation)||"auto"),onValueChange:b=>u("rotation",b==="auto"?"auto":Number(b)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:h.map(b=>e.jsx(s.SelectItem,{value:b,className:"text-xs",children:b==="auto"?"Auto (Default)":`${b}°`},b))})]})]})]}),a==="value"&&e.jsx(We,{formatOptions:(l==null?void 0:l.formatOptions)||{},onFormatOptionsChange:b=>c({...l,formatOptions:b}),showTitle:!0,title:"FORMAT"}),a==="value"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"SCALE"}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Min"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((y=l==null?void 0:l.scale)==null?void 0:y.min)==="auto"||((T=l==null?void 0:l.scale)==null?void 0:T.min)===void 0?"":String(l.scale.min),onSave:b=>m("min",b),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Max"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((N=l==null?void 0:l.scale)==null?void 0:N.max)==="auto"||((C=l==null?void 0:l.scale)==null?void 0:C.max)===void 0?"":String(l.scale.max),onSave:b=>m("max",b),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"w-8 text-xs text-muted-foreground",children:"Step"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"auto",value:((k=l==null?void 0:l.scale)==null?void 0:k.stepSize)==="auto"||((R=l==null?void 0:l.scale)==null?void 0:R.stepSize)===void 0?"":String(l.scale.stepSize),onSave:b=>m("stepSize",b),className:""})})]})]})]})]})]})}function Ke(t,n,a){var d,r,i;if(t==="x"){const o=(r=(d=n==null?void 0:n.config)==null?void 0:d.groupByColumns)==null?void 0:r[0];return(o==null?void 0:o.label)||(o==null?void 0:o.name)||""}const l=((i=n==null?void 0:n.config)==null?void 0:i.metricColumns)||[],c=(a==null?void 0:a.datasetOptions)||[];if(t==="y"){const o=l.filter((u,m)=>{const x=c.find(h=>h.idx===m);return!x||x.yAxisId!=="y2"});return o.length===1&&(o[0].label||o[0].name)||""}if(t==="y2"){const o=l.filter((u,m)=>{const x=c.find(h=>h.idx===m);return(x==null?void 0:x.yAxisId)==="y2"});return o.length===1&&(o[0].label||o[0].name)||""}return""}function va(){const t=s.useEditorStore(i=>i.card),n=s.useEditorStore(i=>{var o;return(o=i.card)==null?void 0:o.preferences}),{setCardPreferences:a,updateCardInFrame:l}=s.useEditorActions();if(!t||["tornado","pyramid","kpi","treemap"].includes(t.type))return null;const c=s.AxisConfigurator.getAxisInfo(t.type,n);if(!c.hasXAxis&&!c.hasYAxis)return null;const d=(i,o)=>{let u;i==="x"?u="xAxisConfig":i==="y"?u="yAxisConfig":u="secondaryYAxisConfig";const m={...n||{},[u]:o};a(m);const x={...t,preferences:m};l(x)},r=c.hasY2Axis&&t.type==="combo";return e.jsxs(s.AccordionItem,{value:"axes",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Es,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Axes"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs(s.Accordion,{type:"single",collapsible:!0,className:"w-full",children:[c.hasXAxis&&e.jsx(qe,{axis:"x",label:"X-Axis",type:c.xAxisType,config:s.mergeAxisConfig(s.getDefaultAxisConfig(t.type,"x"),n==null?void 0:n.xAxisConfig),onChange:i=>d("x",i),defaultTitle:Ke("x",t,n)}),c.hasYAxis&&e.jsx(qe,{axis:"y",label:r?"Y-Axis (Left)":"Y-Axis",type:c.yAxisType,config:s.mergeAxisConfig(s.getDefaultAxisConfig(t.type,"y"),n==null?void 0:n.yAxisConfig),onChange:i=>d("y",i),defaultTitle:Ke("y",t,n)}),r&&e.jsx(qe,{axis:"y2",label:"Y2-Axis (Right)",type:"value",config:s.mergeAxisConfig({enabled:!0,position:"right"},n==null?void 0:n.secondaryYAxisConfig),onChange:i=>d("y2",i),defaultTitle:Ke("y2",t,n),hidePosition:!0})]})})]})}function ya(){const t=s.useEditorStore(m=>m.card),n=s.useEditorStore(m=>{var x,h,w,j;return(j=(w=(h=(x=m.card)==null?void 0:x.preferences)==null?void 0:h.chartOptions)==null?void 0:w.plugins)==null?void 0:j.legend}),{setLegendOptions:a}=s.useEditorActions();if(!t||!s.chartSupportsLegend(t.type))return null;const l=s.getDefaultLegendConfig(t.type),c=(l==null?void 0:l.display)??!0,d=(l==null?void 0:l.position)??"top",r=(l==null?void 0:l.align)??"center",i=(m,x)=>{const h={...n,[m]:x};a(h)},o=["top","bottom","left","right"],u=["start","center","end"];return e.jsxs(s.AccordionItem,{value:"legend",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ct,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Legend"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Display"}),e.jsx(s.SmallSwitch,{checked:(n==null?void 0:n.display)??c,onCheckedChange:m=>i("display",m)})]}),((n==null?void 0:n.display)??c)&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Position"}),e.jsxs(s.Select,{value:(n==null?void 0:n.position)||d,onValueChange:m=>i("position",m),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:o.map(m=>e.jsx(s.SelectItem,{value:m,className:"text-xs",children:s.titleCase(m)},m))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Alignment"}),e.jsxs(s.Select,{value:(n==null?void 0:n.align)||r,onValueChange:m=>i("align",m),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:u.map(m=>e.jsx(s.SelectItem,{value:m,className:"text-xs",children:s.titleCase(m)},m))})]})]})]})]})})]})}function Na(){var I,M,E,B,$,V,q,G,z,H,_;const t=s.useEditorStore(P=>P.card),n=s.useEditorStore(P=>{var Y,Z;return(Z=(Y=P.card)==null?void 0:Y.preferences)==null?void 0:Z.dataLabelsConfig}),a=s.useEditorStore(P=>{var Y;return(Y=P.card)==null?void 0:Y.preferences}),{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!s.chartSupportsDataLabels(t.type))return null;const d=t.type==="bullet"?((I=a==null?void 0:a.bulletConfig)==null?void 0:I.orientation)??(((M=a==null?void 0:a.chartOptions)==null?void 0:M.indexAxis)==="x"?"vertical":"horizontal"):void 0,r=d!=null?d==="horizontal":((E=a==null?void 0:a.chartOptions)==null?void 0:E.indexAxis)==="y",i=t.type==="stackedBar"||t.type==="stackedLine",o=t.type==="pie"||t.type==="doughnut"||t.type==="polarArea"||t.type==="funnel",u=!o,m=t.type==="bubble"||t.type==="scatter",x=($=(B=a==null?void 0:a.chartOptions)==null?void 0:B.plugins)==null?void 0:$.datalabels,h=(x==null?void 0:x.display)!==!1&&((x==null?void 0:x.anchor)||(x==null?void 0:x.align)),w=s.getDefaultDataLabelsConfig(t.type,t.preferences),j=n??w,f=(w==null?void 0:w.enabled)??h??!1,p=(n==null?void 0:n.enabled)!==void 0?n.enabled:f,g=(P,Y)=>{const W={...w??{},...n??{},[P]:Y};P==="enabled"&&(W.display=Y,Y&&m&&!W.position&&(W.position="bottom")),P==="position"&&m&&(W.position="bottom");const ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},v=(P,Y)=>{const Z=n??{},W={...w??{},...Z,font:{...Z.font||{},[P]:Y}},ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},O=(P,Y)=>{const Z=n??{},W={...w??{},...Z,formatOptions:{...Z.formatOptions||{},[P]:Y}},ae={...t.preferences||{},dataLabelsConfig:W};l(ae);const Q={...t,preferences:ae};c(Q)},A=()=>{const P=t.type;return P==="stackedBar"||P==="stackedLine"||P==="pie"||P==="doughnut"?"center":r?"right":"top"},y=t.type!=="treemap"&&t.type!=="heatmap",T=t.type!=="treemap",N=A();let C=[];if(y)if(m)C=["bottom"];else if(i||o)C=["center"];else{const P=r?["center","left","right"]:["center","top","bottom"];C=["auto",...t.type==="bullet"?P:P.filter(Z=>Z!==N),"custom"]}const k=()=>s.titleCase(N),R=["center","start","end"],b=["center","start","end","top","bottom"],S=u?["auto","number","currency","percent","none"]:["number","currency","percent","none"],D=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"GBP",label:"GBP - British Pound"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"INR",label:"INR - Indian Rupee"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"}],F=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"}];return e.jsxs(s.AccordionItem,{value:"data-labels",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ct,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Data Labels"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Display"}),e.jsx(s.SmallSwitch,{checked:p,onCheckedChange:P=>g("enabled",P)})]}),p&&e.jsxs(e.Fragment,{children:[y&&!i&&!o&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Position"}),e.jsxs(s.Select,{value:m?(j==null?void 0:j.position)||"bottom":(j==null?void 0:j.position)||"auto",onValueChange:P=>g("position",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:C.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:P==="auto"?k():P==="custom"?"Custom (Advanced)":s.titleCase(P)},P))})]})]}),i&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Show Total"}),e.jsx(s.SmallSwitch,{checked:(n==null?void 0:n.showTotal)??!1,onCheckedChange:P=>g("showTotal",P)})]}),(j==null?void 0:j.position)==="custom"&&e.jsxs("div",{className:"space-y-2 pl-4 border-l-2 border-muted",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-16",children:"Anchor"}),e.jsxs(s.Select,{value:(j==null?void 0:j.anchor)||"center",onValueChange:P=>g("anchor",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:R.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:s.titleCase(P)},P))})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-16",children:"Align"}),e.jsxs(s.Select,{value:String((j==null?void 0:j.align)||"center"),onValueChange:P=>g("align",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:b.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:s.titleCase(P)},P))})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Format"}),e.jsxs(s.Select,{value:(j==null?void 0:j.format)||(u?"auto":"none"),onValueChange:P=>g("format",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:S.map(P=>e.jsx(s.SelectItem,{value:P,className:"text-xs",children:P==="auto"&&u?"Auto (Use Axis)":P==="none"?"None (Raw Value)":s.titleCase(P)},P))})]})]}),(j==null?void 0:j.format)&&!["auto","none"].includes(j.format)&&e.jsxs("div",{className:"space-y-2 pl-4 border-l-2 border-muted",children:[["number","currency","percent"].includes(j.format)&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Decimals"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(((V=j==null?void 0:j.formatOptions)==null?void 0:V.decimalPlaces)??(j.format==="currency"?2:j.format==="percent"?1:0)),onSave:P=>O("decimalPlaces",Number(P)),className:""})})]}),j.format==="currency"&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Currency"}),e.jsxs(s.Select,{value:((q=j==null?void 0:j.formatOptions)==null?void 0:q.currency)||"USD",onValueChange:P=>O("currency",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:D.map(P=>e.jsx(s.SelectItem,{value:P.value,className:"text-xs",children:P.label},P.value))})]})]}),["number","currency"].includes(j.format)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:((G=j==null?void 0:j.formatOptions)==null?void 0:G.useSuffix)??!1,onCheckedChange:P=>O("useSuffix",P)})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Locale"}),e.jsxs(s.Select,{value:((z=j==null?void 0:j.formatOptions)==null?void 0:z.locale)||"en-US",onValueChange:P=>O("locale",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:F.map(P=>e.jsx(s.SelectItem,{value:P.value,className:"text-xs",children:P.label},P.value))})]})]})]}),T&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Appearance"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Size"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((H=j==null?void 0:j.font)==null?void 0:H.size)||12),onSave:P=>v("size",Number(P)),className:""})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Weight"}),e.jsxs(s.Select,{value:((_=j==null?void 0:j.font)==null?void 0:_.weight)||"normal",onValueChange:P=>v("weight",P),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Color"}),e.jsxs(s.Select,{value:(j==null?void 0:j.color)==="auto"||!(j!=null&&j.color)?"auto":"custom",onValueChange:P=>{P==="auto"&&g("color","auto")},children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs flex-1",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"auto",className:"text-xs",children:"Auto (Smart Contrast)"}),e.jsx(s.SelectItem,{value:"custom",disabled:!0,className:"text-xs",children:"Custom (Coming Soon)"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground w-20",children:"Rotation"}),e.jsx("div",{className:"flex-1",children:e.jsx(ie,{type:"number",placeholder:"0",value:String((j==null?void 0:j.rotation)||0),onSave:P=>g("rotation",Number(P)),className:""})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"degrees"})]})]})]})]})})]})}const ps=[{value:"green",color:"#16a34a",label:"Green"},{value:"red",color:"#dc2626",label:"Red"},{value:"neutral",color:"#6b7280",label:"Neutral"}],Ca=["#16a34a","#dc2626","#6b7280","#2563eb","#ca8a04","#7c3aed","#0891b2","#e11d48","#ea580c"];function De({value:t,onChange:n,label:a}){const[l,c]=L.useState(!1),[d,r]=L.useState(!1),i=()=>{const o=ps.find(u=>u.value===t);return o?o.color:t.startsWith("#")?t:ps[2].color};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(s.Popover,{open:l,onOpenChange:c,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx("button",{className:"size-5 rounded border-2 border-input shadow-sm transition-all hover:scale-110 hover:border-gray-400",style:{backgroundColor:i()},"aria-label":`Change color for ${a}`})}),e.jsx(s.PopoverContent,{align:"start",className:"w-auto p-2",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"grid grid-cols-3 items-center justify-items-center gap-1",children:Ca.map(o=>e.jsx("button",{className:s.cn("h-5 w-5 rounded border-2 transition-all",i()===o?"border-gray-900":"border-transparent"),style:{backgroundColor:o},onClick:()=>{n(o),c(!1)},children:e.jsx("span",{className:"sr-only",children:o})},o))}),e.jsx("button",{className:"w-full rounded border border-input px-2 py-1 text-xs hover:bg-muted",onClick:()=>{r(!0),c(!1)},children:"Custom color..."})]})})]}),e.jsx("span",{className:"text-xs text-muted-foreground",children:a})]}),d&&e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[e.jsx("div",{className:"absolute inset-0 bg-black/50",onClick:()=>r(!1)}),e.jsx("div",{className:"relative rounded-lg bg-white shadow-lg",children:e.jsx($s,{color:i(),onColorChange:o=>{n(o),r(!1)},onClose:()=>r(!1)})})]})]})}const gs={difference:{label:"Difference",description:"Shows the exact difference between two values.",formula:"Primary - Secondary",example:"Sales increased by $5,000 compared to last year."},change:{label:"Change",description:"Shows the percentage increase or decrease between two values.",formula:"(Primary - Secondary) / Secondary × 100%",example:"Year-over-year sales change of 25%."},change_difference:{label:"Change (difference)",description:"Shows both the percentage change and the exact difference between two values.",formula:`% Change: (Primary - Secondary) / Secondary × 100%
|
|
184
|
+
Difference: Primary - Secondary`,example:"Sales grew by 25% ($5,000) compared to last year."},ratio:{label:"Ratio",description:"Shows how much of one value is covered by another — useful for quotas and benchmarks.",formula:"Primary / Secondary × 100%",example:"You've achieved 80% of your sales quota."}};function Sa(){var h,w,j,f,p,g,v,O,A,y,T;const t=s.useEditorStore(N=>N.card),{setCardPreferences:n}=s.useEditorActions(),a=((h=t.config)==null?void 0:h.metricColumns)||[],l=((w=t.config)==null?void 0:w.groupByColumns)||[],c=a.length===2&&l.length===0,d=a.length>=1&&l.length>0;if(!(t.type==="kpi"&&(c||d)))return null;const i=((f=(j=t.preferences)==null?void 0:j.kpiVisualOptions)==null?void 0:f.metricComparison)||{},o=N=>{var k;const C={...t.preferences,kpiVisualOptions:{...(k=t.preferences)==null?void 0:k.kpiVisualOptions,metricComparison:{...i,...N}}};n(C)},u=d||i.enabled,m=((p=t.config)==null?void 0:p.comparisonType)||"none",x=m==="previous_period"||m==="same_period_last_year"||m==="start_vs_end";return e.jsxs(s.AccordionItem,{value:"comparison",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex items-center gap-2 py-2 text-sm hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ot,{className:"h-4 w-4"}),e.jsx("span",{children:"Comparison"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 pb-4 pt-2",children:[c&&a.length===2&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{htmlFor:"enable-comparison",className:"text-xs",children:"Enable Metric Comparison"}),e.jsx(s.SmallSwitch,{id:"enable-comparison",checked:i.enabled||!1,onCheckedChange:N=>o({enabled:N,...N&&!i.calculationType?{calculationType:"difference"}:{}})})]}),u&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(s.Label,{htmlFor:"calculation-type",className:"text-xs font-medium uppercase text-muted-foreground",children:"Calculated as"}),e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Info,{className:"h-3 w-3 cursor-help text-muted-foreground"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:(()=>{const N=i.calculationType||"difference",C=gs[N];return e.jsxs("div",{className:"space-y-2 text-xs",children:[e.jsx("div",{className:"text-sm font-semibold",children:C==null?void 0:C.label}),e.jsx("p",{children:C==null?void 0:C.description}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:"Formula:"})," ",e.jsx("code",{className:"rounded bg-muted px-1 py-0.5 text-[10px]",children:C==null?void 0:C.formula})]}),e.jsxs("div",{className:"border-t pt-1.5 italic",children:[e.jsx("span",{className:"font-medium",children:"Example:"})," ",C==null?void 0:C.example]})]})})()})]})]}),e.jsxs(s.Select,{value:i.calculationType||"difference",onValueChange:N=>o({calculationType:N}),children:[e.jsx(s.SelectTrigger,{id:"calculation-type",className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Object.entries(gs).map(([N,C])=>e.jsx(s.SelectItem,{value:N,className:"text-xs",children:C.label},N))})]})]}),e.jsx(We,{formatOptions:i.formatOptions||{},onFormatOptionsChange:N=>o({formatOptions:N}),showTitle:!0,title:"COMPARISON FORMAT"}),(x||c)&&e.jsxs("div",{className:"space-y-3 border-t pt-3",children:[e.jsx(s.Label,{className:"text-xs font-medium uppercase text-muted-foreground",children:"Indicator"}),c&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Position"}),e.jsxs(s.Select,{value:i.position||"right",onValueChange:N=>o({position:N}),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"right",className:"text-xs",children:"Right"}),e.jsx(s.SelectItem,{value:"left",className:"text-xs",children:"Left"}),e.jsx(s.SelectItem,{value:"top",className:"text-xs",children:"Top"})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"show-arrow",checked:i.showArrow!==!1,onCheckedChange:N=>o({showArrow:N})}),e.jsx(s.Label,{htmlFor:"show-arrow",className:"cursor-pointer text-xs text-muted-foreground",children:"Arrow"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"show-color",checked:i.showColor!==!1,onCheckedChange:N=>o({showColor:N})}),e.jsx(s.Label,{htmlFor:"show-color",className:"cursor-pointer text-xs text-muted-foreground",children:"Color"})]}),i.showColor!==!1&&e.jsxs("div",{className:"space-y-2 border-l-2 border-muted pl-3",children:[e.jsx(De,{value:((g=i.colorConfig)==null?void 0:g.increase)||"green",onChange:N=>o({colorConfig:{...i.colorConfig,increase:N}}),label:"Primary > Secondary"}),e.jsx(De,{value:((v=i.colorConfig)==null?void 0:v.decrease)||"red",onChange:N=>o({colorConfig:{...i.colorConfig,decrease:N}}),label:"Primary < Secondary"}),e.jsx(De,{value:((O=i.colorConfig)==null?void 0:O.noChange)||"neutral",onChange:N=>o({colorConfig:{...i.colorConfig,noChange:N}}),label:"Primary = Secondary"})]})]}),e.jsxs("div",{className:"space-y-3 border-t pt-3",children:[e.jsx(s.Label,{className:"text-xs font-medium uppercase text-muted-foreground",children:"Label"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Checkbox,{id:"conditional-label",checked:i.conditionalLabel||!1,onCheckedChange:N=>o({conditionalLabel:N})}),e.jsx(s.Label,{htmlFor:"conditional-label",className:"cursor-pointer text-xs text-muted-foreground",children:"Conditional"})]}),i.conditionalLabel?e.jsxs("div",{className:"space-y-3 border-l-2 border-muted pl-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(s.Label,{htmlFor:"label-increase",className:"text-xs text-muted-foreground",children:["Primary ",">"," Secondary"]}),e.jsx(s.Input$1,{id:"label-increase",type:"text",value:((A=i.conditionalLabels)==null?void 0:A.increase)??"Increase",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,increase:N.target.value}}),placeholder:"Increase",className:"h-7 text-xs"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(s.Label,{htmlFor:"label-decrease",className:"text-xs text-muted-foreground",children:["Primary ","<"," Secondary"]}),e.jsx(s.Input$1,{id:"label-decrease",type:"text",value:((y=i.conditionalLabels)==null?void 0:y.decrease)??"Decrease",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,decrease:N.target.value}}),placeholder:"Decrease",className:"h-7 text-xs"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"label-no-change",className:"text-xs text-muted-foreground",children:"Primary = Secondary"}),e.jsx(s.Input$1,{id:"label-no-change",type:"text",value:((T=i.conditionalLabels)==null?void 0:T.noChange)??"No change",onChange:N=>o({conditionalLabels:{...i.conditionalLabels,noChange:N.target.value}}),placeholder:"No change",className:"h-7 text-xs"})]})]}):e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"label-name",className:"text-xs text-muted-foreground",children:"Previous Label"}),e.jsx(s.Input$1,{id:"label-name",type:"text",value:i.labelName??"",onChange:N=>o({labelName:N.target.value}),placeholder:"e.g. Previous, Last Period",className:"h-7 text-xs"})]})]})]})]})]})}function wa(){var c,d;const t=s.useEditorStore(r=>r.card),{setCardPreferences:n}=s.useEditorActions();if(t.type!=="kpi")return null;const a=((d=(c=t.preferences)==null?void 0:c.kpiVisualOptions)==null?void 0:d.formatOptions)||{},l=r=>{var o;const i={...t.preferences,kpiVisualOptions:{...(o=t.preferences)==null?void 0:o.kpiVisualOptions,formatOptions:r}};n(i)};return e.jsxs(s.AccordionItem,{value:"kpi-format",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex items-center gap-2 py-2 text-sm hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Hash,{className:"h-4 w-4"}),e.jsx("span",{children:"Number Format"})]})}),e.jsx(s.AccordionContent,{className:"space-y-4 pb-4 pt-2",children:e.jsx(We,{formatOptions:a,onFormatOptionsChange:l,showTitle:!1})})]})}function Ta(){var d;const t=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.type}),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),{setCardPreferences:a}=s.useEditorActions();if(t!=="treemap")return null;const l=((d=n==null?void 0:n.chartOptions)==null?void 0:d.treemapColorMode)??"branch",c=r=>{const i={...n||{},chartOptions:{...n==null?void 0:n.chartOptions,treemapColorMode:r}};a(i)};return e.jsxs(s.AccordionItem,{value:"treemap-color-mode",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.TbChartTreemap,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Treemap Colors"})]})}),e.jsx(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:e.jsxs(s.RadioGroup,{value:l,onValueChange:r=>c(r??"branch"),className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:"treemap-color-branch",value:"branch"}),e.jsxs(s.Label,{htmlFor:"treemap-color-branch",className:"flex flex-col text-sm font-normal leading-tight",children:[e.jsx("span",{className:"font-medium",children:"Shade by value (default)"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Keeps parent hue and adjusts lightness based on the node's share within the branch."})]})]}),e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:"treemap-color-category",value:"category"}),e.jsxs(s.Label,{htmlFor:"treemap-color-category",className:"flex flex-col text-sm font-normal leading-tight",children:[e.jsx("span",{className:"font-medium",children:"Single color per category"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Applies the top-level color to all descendants without lightening/darkening."})]})]})]})})]})}const Ea=Object.keys(s.HEATMAP_PRESET_PALETTES);function ka(){const t=s.useEditorStore(h=>h.card),n=s.useEditorStore(h=>{var w;return(w=h.card)==null?void 0:w.preferences}),{setCardPreferences:a}=s.useEditorActions(),l=(t==null?void 0:t.type)==="heatmap",c=(n==null?void 0:n.heatmapConfig)||{},d=c.colorPalette??"green",r=c.legendPosition??"bottom",i=L.useMemo(()=>c.colorRange&&c.colorRange[0]&&c.colorRange[1]?[c.colorRange[0],c.colorRange[1]]:[...s.DEFAULT_HEATMAP_CUSTOM_RANGE],[c.colorRange]),o=h=>{const w={...c,...h};w.colorPalette!=="custom"?delete w.colorRange:(!w.colorRange||!w.colorRange[0]||!w.colorRange[1])&&(w.colorRange=[...s.DEFAULT_HEATMAP_CUSTOM_RANGE]),a({...n||{},heatmapConfig:w})},u=h=>{o({colorPalette:h,colorRange:h==="custom"?c.colorRange??[...s.DEFAULT_HEATMAP_CUSTOM_RANGE]:void 0})},m=(h,w)=>{const j=[...i];j[h]=w,o({colorPalette:"custom",colorRange:j})},x=h=>{o({legendPosition:h})};return l?e.jsxs(s.AccordionItem,{value:"heatmap-color-config",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Grid3x3,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Heatmap Settings"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:[e.jsxs(s.RadioGroup,{value:d,onValueChange:h=>u(h??"blue"),className:"space-y-3",children:[Ea.map(h=>{const w=s.HEATMAP_PRESET_PALETTES[h];return e.jsxs("div",{className:"flex items-center gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:`heatmap-${h}`,value:h}),e.jsxs(s.Label,{htmlFor:`heatmap-${h}`,className:"flex w-full flex-col gap-1 text-sm font-normal",children:[e.jsx("span",{className:"font-medium",children:w.label}),e.jsx("span",{className:"h-2 rounded-full",style:{background:`linear-gradient(90deg, ${w.start}, ${w.end})`}})]})]},h)}),e.jsxs("div",{className:"flex flex-col gap-2 rounded-md border border-border px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.RadioGroupItem,{id:"heatmap-custom",value:"custom"}),e.jsx(s.Label,{htmlFor:"heatmap-custom",className:"text-sm font-medium",children:"Custom colors"})]}),d==="custom"&&e.jsxs("div",{className:"flex flex-col gap-3 pl-6",children:[e.jsx(De,{value:i[0],onChange:h=>m(0,h),label:"Start color"}),e.jsx(De,{value:i[1],onChange:h=>m(1,h),label:"End color"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Color scale position"}),e.jsxs(s.Select,{value:r,onValueChange:h=>x(h??"bottom"),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:["top","bottom","left","right"].map(h=>e.jsx(s.SelectItem,{value:h,className:"text-xs",children:h[0].toUpperCase()+h.slice(1)},h))})]})]})]})]}):null}const fs=["poor","satisfactory","good"];function js(t){if(!t)return;const n={...t};return n.targetMetricKey||delete n.targetMetricKey,n.comparativeMetricKey||delete n.comparativeMetricKey,!n.targetValue&&n.targetValue!==0&&delete n.targetValue,(!n.rangeKeys||Object.keys(n.rangeKeys).length===0)&&delete n.rangeKeys,(!n.ranges||Object.keys(n.ranges).length===0)&&delete n.ranges,(!n.rangeLabels||Object.keys(n.rangeLabels).length===0)&&delete n.rangeLabels,n}function Ia(){var D,F;const t=s.useEditorStore(I=>I.card.preferences),n=s.useEditorStore(I=>I.card),a=s.useEditorStore(I=>I.isDevMode),{setCardPreferences:l,setChartOrientation:c}=s.useEditorActions(),d=L.useMemo(()=>(t==null?void 0:t.bulletConfig)||{},[t==null?void 0:t.bulletConfig]),i=s.hasValidCardConfig(n==null?void 0:n.config)&&!a,[o,u]=L.useState(d.showRanges??!1),[m,x]=L.useState(d.rangesArePercentages??!1),h=d.disableAutoComparative?!1:d.comparativeMetricKey!==void 0||!i,[w,j]=L.useState(h);L.useEffect(()=>{u(d.showRanges??!1)},[d.showRanges]),L.useEffect(()=>{x(d.rangesArePercentages??!1)},[d.rangesArePercentages]);const f=d.targetValue===void 0?"":String(d.targetValue);L.useEffect(()=>{const I=d.disableAutoComparative?!1:d.comparativeMetricKey!==void 0||!i;j(I)},[d.disableAutoComparative,d.comparativeMetricKey,i]);const p=L.useMemo(()=>{var I,M;return((M=(I=n==null?void 0:n.config)==null?void 0:I.metricColumns)==null?void 0:M.map(E=>{const B=E.label||E.name||E.alias||"";return{value:B,label:B}}))||[]},[(D=n==null?void 0:n.config)==null?void 0:D.metricColumns]);L.useEffect(()=>{var G,z;if(!t||p.length===0)return;const I={};let M=!1;const E=(G=p[1])==null?void 0:G.value;!d.targetMetricKey&&E&&(I.targetMetricKey=E,M=!0);const B=(z=p[2])==null?void 0:z.value;!d.comparativeMetricKey&&!d.disableAutoComparative&&B&&(I.comparativeMetricKey=B,M=!0);const $=p.slice(3),V={...d.rangeKeys||{}};let q=!1;if(fs.forEach((H,_)=>{!V[H]&&$[_]&&(V[H]=$[_].value,q=!0)}),q&&(I.rangeKeys=V,M=!0,d.showRanges===void 0&&(I.showRanges=!0)),M){const H=js({...d,...I});l({...t,bulletConfig:H})}},[p,d,t,l]);const g=I=>{const M=js({...d,...I});l({...t,bulletConfig:M})},v=(I,M)=>{const E={...d.rangeKeys||{},[I]:M.trim()};M.trim()||delete E[I],g({rangeKeys:Object.keys(E).length>0?E:void 0})},O=(I,M)=>{const E=M===""?void 0:Number(M),B={...d.ranges||{}};E===void 0||Number.isNaN(E)?delete B[I]:B[I]=E,g({ranges:Object.keys(B).length?B:void 0})},A=I=>{if(I===""){g({targetValue:void 0});return}const M=Number(I);g({targetValue:Number.isNaN(M)?void 0:M})},y=(I,M)=>{const E=M.trim(),B={...d.rangeLabels||{}};E?B[I]=E:delete B[I],g({rangeLabels:Object.keys(B).length>0?B:void 0})},T=I=>{u(I),g({showRanges:I})},N=I=>{x(I),g({rangesArePercentages:I})},C=d.orientation||(((F=t==null?void 0:t.chartOptions)==null?void 0:F.indexAxis)==="y"?"horizontal":"vertical"),k=I=>{g({orientation:I}),c(I==="horizontal"?"y":"x")},R=[{value:"__none",label:"None"},...p.map(I=>({value:I.value,label:I.label}))],b=R,S=I=>{if(j(I),!I){g({comparativeMetricKey:void 0,disableAutoComparative:!0});return}if(!i){g({disableAutoComparative:!1});return}if(d.comparativeMetricKey)g({disableAutoComparative:!1});else{const M=p.find(E=>E.value!==d.targetMetricKey);g({comparativeMetricKey:M==null?void 0:M.value,disableAutoComparative:!1})}};return e.jsxs(s.AccordionItem,{value:"bullet-config",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.BulletChartIcon,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Bullet Chart"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-6 border-t border-border/50 py-4",children:[e.jsxs("section",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"ORIENTATION"}),e.jsxs(s.Select,{value:C,onValueChange:I=>k(I),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"horizontal",className:"text-xs",children:"Horizontal"}),e.jsx(s.SelectItem,{value:"vertical",className:"text-xs",children:"Vertical"})]})]})]}),i&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"TARGET METRIC"}),e.jsxs(s.Select,{value:d.targetMetricKey||"__none",onValueChange:I=>g({targetMetricKey:I==="__none"?void 0:I}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select metric"})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"__none",className:"text-xs",children:"None"}),p.map(I=>e.jsx(s.SelectItem,{value:I.value,className:"text-xs",children:I.label},I.value))]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"COMPARATIVE METRIC"}),i?e.jsxs(s.Select,{value:d.comparativeMetricKey||"__none",onValueChange:I=>g({comparativeMetricKey:I==="__none"?void 0:I,disableAutoComparative:I==="__none"}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select metric"})}),e.jsx(s.SelectContent,{children:R.map(I=>e.jsx(s.SelectItem,{value:I.value,className:"text-xs",children:I.label},I.value))})]}):e.jsxs("div",{className:"space-y-2 rounded-md border border-border/50 p-3 text-xs",children:[e.jsxs("div",{className:"flex items-center justify-between font-medium",children:[e.jsx("span",{children:"Show comparative"}),e.jsx(s.SmallSwitch,{checked:w,onCheckedChange:I=>S(I)})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Comparative values are auto-detected from the third metric in your results."})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"TARGET VALUE (FALLBACK)"}),e.jsx(oe,{type:"number",className:"h-8 text-xs",value:f,onCommit:A,placeholder:"Optional numeric fallback"})]})]}),e.jsxs("section",{className:"space-y-4 rounded-md border border-border/60 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"QUALITATIVE RANGES"}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:200,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("button",{type:"button",className:"text-muted-foreground transition-colors hover:text-foreground",children:e.jsx(s.Info,{className:"h-3.5 w-3.5"})})}),e.jsx(s.TooltipContent,{side:"top",children:"Use columns or static values for Poor / Average / Good."})]})})]}),e.jsx("div",{className:"flex items-center gap-2 text-xs font-medium",children:e.jsx(s.SmallSwitch,{checked:o,onCheckedChange:T})})]}),e.jsxs("div",{className:"flex items-center gap-2 text-xs font-medium",children:[e.jsx("input",{type:"checkbox",checked:m,disabled:!o,onChange:I=>N(I.target.checked),className:"h-3.5 w-3.5 accent-primary disabled:opacity-50"}),e.jsx("span",{children:"Use % of value"})]})]}),o&&e.jsx("div",{className:"space-y-3",children:fs.map(I=>{var M,E,B,$,V;return e.jsxs("div",{className:"space-y-2 rounded-md border border-border/60 bg-muted/40 p-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," LABEL"]}),e.jsx(oe,{placeholder:`Display label (${I})`,value:((M=d.rangeLabels)==null?void 0:M[I])||"",onCommit:q=>y(I,q),className:"h-8 text-xs"})]}),i?e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," COLUMN"]}),e.jsxs(s.Select,{value:((E=d.rangeKeys)==null?void 0:E[I])||"__none",onValueChange:q=>{q==="__none"?v(I,""):v(I,q)},children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select column"})}),e.jsx(s.SelectContent,{children:b.map(q=>e.jsx(s.SelectItem,{value:q.value,className:"text-xs",children:q.label},q.value))})]})]}):e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," COLUMN KEY"]}),e.jsx(oe,{placeholder:"Column key",value:((B=d.rangeKeys)==null?void 0:B[I])||"",onCommit:q=>v(I,q),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(s.Label,{className:"text-[11px] font-medium uppercase tracking-wide",children:[I," VALUE"]}),e.jsx(oe,{type:"number",placeholder:m?"0.80":"Static value",value:(($=d.ranges)==null?void 0:$[I])===void 0?"":String((V=d.ranges)==null?void 0:V[I]),onCommit:q=>O(I,q),className:"h-8 text-xs"})]})]},I)})})]})]})]})}function bs(t){if(!t)return;const n={...t};if(n.xMetricKey||delete n.xMetricKey,n.yMetricKey||delete n.yMetricKey,n.radiusMetricKey||delete n.radiusMetricKey,n.labelKey||delete n.labelKey,n.groupKey||delete n.groupKey,n.enableGrouping===void 0&&delete n.enableGrouping,n.radiusScale||delete n.radiusScale,n.fixedRadius===void 0&&delete n.fixedRadius,n.minRadius===void 0&&delete n.minRadius,n.maxRadius===void 0&&delete n.maxRadius,Object.keys(n).length!==0)return n}function Aa(){var y,T,N,C,k,R,b,S,D,F,I,M;const t=s.useEditorStore(E=>E.card.preferences),n=s.useEditorStore(E=>E.card),a=s.useEditorStore(E=>E.card.type),l=s.useEditorStore(E=>E.isDevMode),{setCardPreferences:c,updateCardInFrame:d}=s.useEditorActions(),r=a==="bubble",i=a==="scatter",o=L.useMemo(()=>(t==null?void 0:t.bubbleScatterConfig)||{},[t==null?void 0:t.bubbleScatterConfig]),m=s.hasValidCardConfig(n==null?void 0:n.config)&&!l,x=L.useMemo(()=>{var E,B;return((B=(E=n==null?void 0:n.config)==null?void 0:E.metricColumns)==null?void 0:B.map($=>{const V=$.label||$.name||$.alias||"";return{value:V,label:V}}))||[]},[(y=n==null?void 0:n.config)==null?void 0:y.metricColumns]),h=L.useMemo(()=>{var E,B;return((B=(E=n==null?void 0:n.config)==null?void 0:E.groupByColumns)==null?void 0:B.map($=>{const V=$.label||$.name||$.alias||"";return{value:V,label:V}}))||[]},[(T=n==null?void 0:n.config)==null?void 0:T.groupByColumns]),w=L.useMemo(()=>o.enableGrouping!==void 0?o.enableGrouping:!l&&h.length>=2,[o.enableGrouping,l,h.length]),[j,f]=L.useState(w);L.useEffect(()=>{f(w)},[w]);const p=E=>{const B={...t||{},bubbleScatterConfig:E};c(B),n&&d({...n,preferences:B})},g=E=>{c(E),n&&d({...n,preferences:E})};L.useEffect(()=>{if(!t||x.length===0)return;const E={};let B=!1;if(!o.xMetricKey&&x[0]&&(E.xMetricKey=x[0].value,B=!0),!o.yMetricKey&&x[1]&&(E.yMetricKey=x[1].value,B=!0),r&&!o.radiusMetricKey&&x[2]&&(E.radiusMetricKey=x[2].value,B=!0),!o.labelKey&&h[0]&&(E.labelKey=h[0].value,B=!0),w&&!o.groupKey&&h[1]&&(E.groupKey=h[1].value,E.enableGrouping=!0,B=!0),B){const $=bs({...o,...E});p($)}},[x,h,o,t,c,r,w]);const v=E=>{const B=bs({...o,...E});p(B)},O=E=>{f(E),v({enableGrouping:E,groupKey:E?o.groupKey:void 0})};if(!r&&!i)return null;const A=r?s.Circle:s.ChartScatter;return e.jsxs(s.AccordionItem,{value:"bubble-scatter-config",children:[e.jsx(s.AccordionTrigger,{className:"py-2 text-sm",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{className:"h-4 w-4"}),e.jsxs("span",{children:[r?"Bubble":"Scatter"," Configuration"]})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 px-1 pb-4",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-1 text-xs font-medium text-muted-foreground",children:["Dimensions",e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{children:e.jsx(s.Info,{className:"h-3 w-3"})}),e.jsx(s.TooltipContent,{side:"right",className:"max-w-xs",children:e.jsx("p",{children:"Label identifies each point. Group creates separate series in the legend."})})]})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Label Column"}),m?e.jsxs(s.Select,{value:o.labelKey||"",onValueChange:E=>v({labelKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select label column"})}),e.jsx(s.SelectContent,{children:h.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for labels",value:o.labelKey||"",onCommit:E=>v({labelKey:E})})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Group points into series"}),e.jsx(s.SmallSwitch,{checked:j,onCheckedChange:O})]}),j&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Group By"}),m?e.jsxs(s.Select,{value:o.groupKey||"",onValueChange:E=>v({groupKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select group column"})}),e.jsx(s.SelectContent,{children:h.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for grouping",value:o.groupKey||"",onCommit:E=>v({groupKey:E})})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Metrics"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"X Axis"}),m?e.jsxs(s.Select,{value:o.xMetricKey||"",onValueChange:E=>v({xMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select X metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for X axis",value:o.xMetricKey||"",onCommit:E=>v({xMetricKey:E})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Y Axis"}),m?e.jsxs(s.Select,{value:o.yMetricKey||"",onValueChange:E=>v({yMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select Y metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for Y axis",value:o.yMetricKey||"",onCommit:E=>v({yMetricKey:E})})]}),r&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Radius"}),m?e.jsxs(s.Select,{value:o.radiusMetricKey||"",onValueChange:E=>v({radiusMetricKey:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select radius metric"})}),e.jsx(s.SelectContent,{children:x.map(E=>e.jsx(s.SelectItem,{value:E.value,children:E.label},E.value))})]}):e.jsx(oe,{className:"h-8 text-xs",placeholder:"Column name for radius",value:o.radiusMetricKey||"",onCommit:E=>v({radiusMetricKey:E})})]})]}),r&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Radius Settings"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Scale Mode"}),e.jsxs(s.Select,{value:o.radiusScale||"sqrt",onValueChange:E=>v({radiusScale:E}),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"sqrt",children:"Square Root (area-proportional)"}),e.jsx(s.SelectItem,{value:"linear",children:"Linear"}),e.jsx(s.SelectItem,{value:"fixed",children:"Fixed Size"})]})]})]}),o.radiusScale!=="fixed"&&e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Min Radius"}),e.jsx(oe,{className:"h-8 text-xs",type:"number",placeholder:"5",value:((N=o.minRadius)==null?void 0:N.toString())||"",onCommit:E=>v({minRadius:E?Number(E):void 0})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Max Radius"}),e.jsx(oe,{className:"h-8 text-xs",type:"number",placeholder:"30",value:((C=o.maxRadius)==null?void 0:C.toString())||"",onCommit:E=>v({maxRadius:E?Number(E):void 0})})]})]}),o.radiusScale==="fixed"&&e.jsxs("div",{className:"space-y-1",children:[e.jsxs(s.Label,{className:"text-xs",children:["Fixed Radius: ",o.fixedRadius||10]}),e.jsx(s.Slider,{value:[o.fixedRadius||10],onValueChange:([E])=>v({fixedRadius:E}),min:3,max:30,step:1,className:"py-2"})]})]}),i&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:"Point Settings"}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs(s.Label,{className:"text-xs",children:["Point Size: ",o.fixedRadius||5]}),e.jsx(s.Slider,{value:[o.fixedRadius||5],onValueChange:([E])=>v({fixedRadius:E}),min:2,max:15,step:1,className:"py-2"})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-medium text-muted-foreground",children:"Axis Titles"}),e.jsx(s.SmallSwitch,{checked:((R=(k=t==null?void 0:t.xAxisConfig)==null?void 0:k.name)==null?void 0:R.enabled)??!1,onCheckedChange:E=>{var V,q,G,z;const B=o.xMetricKey||"X Axis",$=o.yMetricKey||"Y Axis";g({...t??{},xAxisConfig:{...t==null?void 0:t.xAxisConfig,name:{enabled:E,text:((q=(V=t==null?void 0:t.xAxisConfig)==null?void 0:V.name)==null?void 0:q.text)||B}},yAxisConfig:{...t==null?void 0:t.yAxisConfig,name:{enabled:E,text:((z=(G=t==null?void 0:t.yAxisConfig)==null?void 0:G.name)==null?void 0:z.text)||$}}})}})]}),((S=(b=t==null?void 0:t.xAxisConfig)==null?void 0:b.name)==null?void 0:S.enabled)&&e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"X Title"}),e.jsx(oe,{className:"h-8 text-xs",placeholder:o.xMetricKey||"X Axis",value:((F=(D=t==null?void 0:t.xAxisConfig)==null?void 0:D.name)==null?void 0:F.text)||"",onCommit:E=>{var B;g({...t??{},xAxisConfig:{...t==null?void 0:t.xAxisConfig,name:{...(B=t==null?void 0:t.xAxisConfig)==null?void 0:B.name,enabled:!0,text:E||o.xMetricKey||"X Axis"}}})}})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs",children:"Y Title"}),e.jsx(oe,{className:"h-8 text-xs",placeholder:o.yMetricKey||"Y Axis",value:((M=(I=t==null?void 0:t.yAxisConfig)==null?void 0:I.name)==null?void 0:M.text)||"",onCommit:E=>{var B;g({...t??{},yAxisConfig:{...t==null?void 0:t.yAxisConfig,name:{...(B=t==null?void 0:t.yAxisConfig)==null?void 0:B.name,enabled:!0,text:E||o.yMetricKey||"Y Axis"}}})}})]})]})]})]})]})}const Da=[{value:"percentOfTotal",label:"% of Total",description:"Each stage shows percentage of the sum of all values"},{value:"percentOfFirst",label:"% of Stage 1",description:"First stage = 100%, others relative to first stage"}];function La(){const t=s.useEditorStore(h=>h.card),n=s.useEditorStore(h=>{var w;return(w=h.card)==null?void 0:w.preferences}),{setCardPreferences:a}=s.useEditorActions(),l=(t==null?void 0:t.type)==="funnel",c=(n==null?void 0:n.funnelConfig)||{},d=c.percentMode??"percentOfTotal",r=c.shrinkFraction??1,i=c.labelThreshold??0,o=h=>{const w={...c,...h};a({...n||{},funnelConfig:w})},u=h=>{o({percentMode:h})},m=h=>{o({shrinkFraction:h[0]})},x=h=>{o({labelThreshold:h[0]})};return l?e.jsxs(s.AccordionItem,{value:"funnel-config",className:"border-none",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-3 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Filter,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Funnel Settings"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 border-t border-border bg-background/50 px-3 py-4 text-sm",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"PERCENTAGE MODE"}),e.jsx(s.RadioGroup,{value:d,onValueChange:h=>u(h),className:"space-y-2",children:Da.map(h=>e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border px-3 py-2",children:[e.jsx(s.RadioGroupItem,{id:`funnel-${h.value}`,value:h.value,className:"mt-0.5"}),e.jsxs(s.Label,{htmlFor:`funnel-${h.value}`,className:"flex flex-col gap-0.5 text-sm font-normal",children:[e.jsx("span",{className:"font-medium",children:h.label}),e.jsx("span",{className:"text-xs text-muted-foreground",children:h.description})]})]},h.value))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"SEGMENT SHRINK"}),e.jsxs("div",{className:"flex items-center gap-3 pt-1",children:[e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Same Width"}),e.jsx(s.Slider,{value:[r],onValueChange:m,min:0,max:1,step:.1,className:"flex-1"}),e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Full Shrink"})]}),e.jsxs("p",{className:"text-center text-xs text-muted-foreground",children:["Current: ",Math.round(r*100),"%"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"LABEL VISIBILITY THRESHOLD"}),e.jsxs("div",{className:"flex items-center gap-3 pt-1",children:[e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Show All"}),e.jsx(s.Slider,{value:[i],onValueChange:x,min:0,max:25,step:1,className:"flex-1"}),e.jsx("span",{className:"whitespace-nowrap text-xs text-muted-foreground",children:"Hide Small"})]}),e.jsx("p",{className:"text-center text-xs text-muted-foreground",children:i===0?"Showing all labels":`Hide labels below ${i}%`})]})]})]}):null}function Be(){const t=s.useEditorStore(d=>d.card),{data:n}=s.useDashboardCardQuery(t),{keys:a}=s.getKeys((n==null?void 0:n.records)||[]),l=a.slice(1).filter(d=>!s.METADATA_KEYS.includes(d));let c=l;if(["stackedBar","stackedLine"].includes(t.type)){const d=s.pivotData({dataArray:(n==null?void 0:n.records)||[],groupKey:a[0],pivotKey:a[1],valueKey:a[2]}),{keys:r}=s.getKeys(d);c=r.slice(1)}return{transformedMetricKeys:c,dimensionKeys:a.slice(0,1),keys:a,defaultMetricKeys:l}}const Ma=[{value:"USD",label:"USD - US Dollar"},{value:"EUR",label:"EUR - Euro"},{value:"GBP",label:"GBP - British Pound"},{value:"JPY",label:"JPY - Japanese Yen"},{value:"CNY",label:"CNY - Chinese Yuan"},{value:"INR",label:"INR - Indian Rupee"},{value:"CAD",label:"CAD - Canadian Dollar"},{value:"AUD",label:"AUD - Australian Dollar"}],Oa=[{value:"en-US",label:"English (US)"},{value:"en-GB",label:"English (UK)"},{value:"de-DE",label:"German"},{value:"fr-FR",label:"French"},{value:"es-ES",label:"Spanish"},{value:"it-IT",label:"Italian"},{value:"pt-BR",label:"Portuguese (Brazil)"},{value:"zh-CN",label:"Chinese (Simplified)"}],Ra=["center","start","end"],Fa=["center","start","end","top","bottom"];function Pe({config:t,onChange:n,isHorizontal:a=!1,showAutoFormat:l=!0,positionOptions:c,showAppearance:d=!0}){var j,f,p,g,v,O;const r=c??(a?["auto","center","left","right","custom"]:["auto","center","top","bottom","custom"]),i=l?["auto","number","currency","percent","none"]:["number","currency","percent","none"],o=(A,y)=>{const T={...t,[A]:y};n(T)},u=(A,y)=>{const T={...t,font:{...(t==null?void 0:t.font)||{},[A]:y}};n(T)},m=(A,y)=>{const T={...t,formatOptions:{...(t==null?void 0:t.formatOptions)||{},[A]:y}};n(T)},x=()=>a?"Right":"Top",h=t||{},w=h.format||(l?"auto":"number");return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Position"}),e.jsxs(s.Select,{value:h.position||"auto",onValueChange:A=>o("position",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:r.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:A==="auto"?x():A==="custom"?"Custom (Advanced)":s.titleCase(A)},A))})]})]}),h.position==="custom"&&e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Anchor"}),e.jsxs(s.Select,{value:h.anchor||"center",onValueChange:A=>o("anchor",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ra.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:s.titleCase(A)},A))})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Align"}),e.jsxs(s.Select,{value:String(h.align||"center"),onValueChange:A=>o("align",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Fa.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:s.titleCase(A)},A))})]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Format"}),e.jsxs(s.Select,{value:w,onValueChange:A=>o("format",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:i.map(A=>e.jsx(s.SelectItem,{value:A,className:"text-xs",children:A==="auto"&&l?"Auto (Use Axis)":A==="none"?"None (Raw Value)":s.titleCase(A)},A))})]})]}),!["auto","none"].includes(w)&&e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[["number","currency","percent"].includes(w)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Decimals"}),e.jsx("div",{className:"w-16",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(((j=h.formatOptions)==null?void 0:j.decimalPlaces)??(w==="currency"?2:w==="percent"?1:0)),onSave:A=>m("decimalPlaces",Number(A)),className:""})})]}),w==="currency"&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Currency"}),e.jsxs(s.Select,{value:((f=h.formatOptions)==null?void 0:f.currency)||"USD",onValueChange:A=>m("currency",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ma.map(A=>e.jsx(s.SelectItem,{value:A.value,className:"text-xs",children:A.label},A.value))})]})]}),["number","currency"].includes(w)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Abbreviate (K, M, B)"}),e.jsx(s.SmallSwitch,{checked:((p=h.formatOptions)==null?void 0:p.useSuffix)??!1,onCheckedChange:A=>m("useSuffix",A)})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Locale"}),e.jsxs(s.Select,{value:((g=h.formatOptions)==null?void 0:g.locale)||"en-US",onValueChange:A=>m("locale",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Oa.map(A=>e.jsx(s.SelectItem,{value:A.value,className:"text-xs",children:A.label},A.value))})]})]})]}),d&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Appearance"}),e.jsxs("div",{className:"space-y-2 rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Size"}),e.jsx("div",{className:"w-16",children:e.jsx(ie,{type:"number",placeholder:"12",value:String(((v=h.font)==null?void 0:v.size)||12),onSave:A=>u("size",Number(A)),className:""})})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Weight"}),e.jsxs(s.Select,{value:((O=h.font)==null?void 0:O.weight)||"normal",onValueChange:A=>u("weight",A),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-24 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"normal",className:"text-xs",children:"Normal"}),e.jsx(s.SelectItem,{value:"bold",className:"text-xs",children:"Bold"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Color"}),e.jsxs(s.Select,{value:h.color==="auto"||!h.color?"auto":"custom",onValueChange:A=>{A==="auto"&&o("color","auto")},children:[e.jsx(s.SelectTrigger,{className:"h-7 w-28 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"auto",className:"text-xs",children:"Auto (Contrast)"}),e.jsx(s.SelectItem,{value:"custom",disabled:!0,className:"text-xs",children:"Custom"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs",children:"Rotation"}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("div",{className:"w-12",children:e.jsx(ie,{type:"number",placeholder:"0",value:String(h.rotation||0),onSave:A=>o("rotation",Number(A)),className:""})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"°"})]})]})]})]})]})}const Ba=[{type:"bar",icon:s.ChartColumnIncreasing,label:"Bar"},{type:"line",icon:s.ChartLine,label:"Line"}],Pa=[0,1,2,3,4,5,6,8,10],Va=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],za=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function $a(){return s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type})!=="combo"?null:e.jsxs(s.AccordionItem,{value:"combo-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(qa,{})})]})}function qa(){const{transformedMetricKeys:t}=Be();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(Ka,{metricKey:n,idx:a},n))})}function Ka({metricKey:t,idx:n}){var g;const[a,l]=L.useState(!1),c=s.useEditorStore(v=>{var O;return(O=v.card)==null?void 0:O.preferences}),d=s.useEditorStore(v=>{var O,A;return(A=(O=v.card)==null?void 0:O.preferences)==null?void 0:A.datasetOptions}),r=d==null?void 0:d.find(v=>v.idx===n),{setCardPreferences:i}=s.useEditorStore(v=>v.actions),o=(r==null?void 0:r.type)||"bar",u=(r==null?void 0:r.yAxisId)||"y",m=((g=r==null?void 0:r.dataLabels)==null?void 0:g.enabled)??!1,x=o==="line"?s.ChartLine:s.ChartColumnIncreasing;function h(v){const O={idx:n,type:o,yAxisId:u,dataLabels:r==null?void 0:r.dataLabels,...v};if(!r)i({...c||{},datasetOptions:[...d||[],O]});else{const A=d==null?void 0:d.map(y=>y.idx===n?{...y,...O}:y);i({...c||{},datasetOptions:A})}}function w(v){h({type:v})}function j(v){h({yAxisId:v})}function f(v){h({dataLabels:{...r==null?void 0:r.dataLabels,enabled:v}})}function p(v){h({dataLabels:v})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsxs("div",{className:"flex shrink-0 items-center gap-1.5",children:[e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(x,{className:"h-3 w-3"}),o==="line"?"Line":"Bar"]}),e.jsx("span",{className:s.cn("rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:u==="y2"?"Y2":"Y1"})]})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"flex items-end gap-4",children:[e.jsxs("div",{className:"flex-1 space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Chart Type"}),e.jsx("div",{className:"flex gap-2",children:Ba.map(({type:v,icon:O,label:A})=>{const y=o===v;return e.jsxs("button",{onClick:()=>w(v),className:s.cn("flex items-center gap-2 rounded-md border px-3 py-1.5 text-xs transition-colors",y?"border-primary bg-primary/10 text-primary":"border-border bg-background text-muted-foreground hover:bg-muted"),children:[e.jsx(O,{className:"h-4 w-4"}),e.jsx("span",{children:A})]},v)})})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Axis"}),e.jsxs(s.Select,{value:u,onValueChange:v=>j(v),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-20 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"y",className:"text-xs",children:"Y1 (Left)"}),e.jsx(s.SelectItem,{value:"y2",className:"text-xs",children:"Y2 (Right)"})]})]})]})]}),o==="line"&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:(r==null?void 0:r.lineStyle)||"solid",onValueChange:v=>h({lineStyle:v}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:za.map(({value:v,label:O})=>e.jsx(s.SelectItem,{value:v,className:"text-xs",children:O},v))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String((r==null?void 0:r.pointRadius)??3),onValueChange:v=>h({pointRadius:Number(v)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Pa.map(v=>e.jsx(s.SelectItem,{value:String(v),className:"text-xs",children:v===0?"None":v},v))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:(r==null?void 0:r.pointStyle)||"circle",onValueChange:v=>h({pointStyle:v}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Va.map(({value:v,label:O})=>e.jsx(s.SelectItem,{value:v,className:"text-xs",children:O},v))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:m,onCheckedChange:f})]}),m&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Pe,{config:r==null?void 0:r.dataLabels,onChange:p,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const Ge=[{value:"origin",label:"Origin (X-axis)",description:"Fill to the X-axis (y=0)"},{value:"start",label:"Start (Top)",description:"Fill to the top of chart area"},{value:"end",label:"End (Bottom)",description:"Fill to the bottom of chart area"},{value:"-1",label:"Previous Series",description:"Fill to the series below"},{value:"+1",label:"Next Series",description:"Fill to the series above"},{value:"false",label:"No Fill (Line)",description:"Show as line without fill"}],Ga=[0,1,2,3,4,5,6,8,10],Ua=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],_a=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function Ha(t){return t==null?"origin":t===!1?"false":t}function Ja(){const t=s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type});return t!=="area"&&t!=="stackedArea"?null:e.jsxs(s.AccordionItem,{value:"area-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(Ya,{})})]})}function Ya(){const{transformedMetricKeys:t}=Be();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(Qa,{metricKey:n,idx:a},n))})}function Qa({metricKey:t,idx:n}){var f;const[a,l]=L.useState(!1),c=s.useEditorStore(p=>{var g;return(g=p.card)==null?void 0:g.preferences}),d=s.useEditorStore(p=>{var g,v;return(v=(g=p.card)==null?void 0:g.preferences)==null?void 0:v.datasetOptions}),r=d==null?void 0:d.find(p=>p.idx===n),{setCardPreferences:i}=s.useEditorStore(p=>p.actions),o=Ha(r==null?void 0:r.fill),u=((f=r==null?void 0:r.dataLabels)==null?void 0:f.enabled)??!1,m=Ge.find(p=>p.value===o)||Ge[0];function x(p){const g={idx:n,fill:o,dataLabels:r==null?void 0:r.dataLabels,...p};if(!r)i({...c||{},datasetOptions:[...d||[],g]});else{const v=d==null?void 0:d.map(O=>O.idx===n?{...O,...g}:O);i({...c||{},datasetOptions:v})}}function h(p){x({fill:p==="false"?!1:p})}function w(p){x({dataLabels:{...r==null?void 0:r.dataLabels,enabled:p}})}function j(p){x({dataLabels:p})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.ChartArea,{className:"h-3 w-3"}),m.label.split(" ")[0]]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Fill Target"}),e.jsxs(s.Select,{value:o,onValueChange:p=>h(p),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ge.map(p=>e.jsx(s.SelectItem,{value:p.value,className:"text-xs",children:e.jsx("div",{className:"flex flex-col",children:e.jsx("span",{children:p.label})})},p.value))})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:(r==null?void 0:r.lineStyle)||"solid",onValueChange:p=>x({lineStyle:p}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:_a.map(({value:p,label:g})=>e.jsx(s.SelectItem,{value:p,className:"text-xs",children:g},p))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String((r==null?void 0:r.pointRadius)??3),onValueChange:p=>x({pointRadius:Number(p)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ga.map(p=>e.jsx(s.SelectItem,{value:String(p),className:"text-xs",children:p===0?"None":p},p))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:(r==null?void 0:r.pointStyle)||"circle",onValueChange:p=>x({pointStyle:p}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ua.map(({value:p,label:g})=>e.jsx(s.SelectItem,{value:p,className:"text-xs",children:g},p))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:u,onCheckedChange:w})]}),u&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Pe,{config:r==null?void 0:r.dataLabels,onChange:j,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const Wa=[0,1,2,3,4,5,6,8,10],Xa=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],Ue=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function Za(){return s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type})!=="line"?null:e.jsxs(s.AccordionItem,{value:"line-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(en,{})})]})}function en(){const{transformedMetricKeys:t}=Be();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(sn,{metricKey:n,idx:a},n))})}function sn({metricKey:t,idx:n}){var p;const[a,l]=L.useState(!1),c=s.useEditorStore(g=>{var v;return(v=g.card)==null?void 0:v.preferences}),d=s.useEditorStore(g=>{var v,O;return(O=(v=g.card)==null?void 0:v.preferences)==null?void 0:O.datasetOptions}),r=d==null?void 0:d.find(g=>g.idx===n),{setCardPreferences:i}=s.useEditorStore(g=>g.actions),o=(r==null?void 0:r.lineStyle)||"solid",u=(r==null?void 0:r.pointRadius)??3,m=(r==null?void 0:r.pointStyle)||"circle",x=((p=r==null?void 0:r.dataLabels)==null?void 0:p.enabled)??!1,h=Ue.find(g=>g.value===o)||Ue[0];function w(g){const v={idx:n,lineStyle:o,pointRadius:u,pointStyle:m,dataLabels:r==null?void 0:r.dataLabels,...g};if(!r)i({...c||{},datasetOptions:[...d||[],v]});else{const O=d==null?void 0:d.map(A=>A.idx===n?{...A,...v}:A);i({...c||{},datasetOptions:O})}}function j(g){w({dataLabels:{...r==null?void 0:r.dataLabels,enabled:g}})}function f(g){w({dataLabels:g})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.TrendingUp,{className:"h-3 w-3"}),h.label.split(" ")[0]]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Style"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Pattern"}),e.jsxs(s.Select,{value:o,onValueChange:g=>w({lineStyle:g}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Ue.map(({value:g,label:v})=>e.jsx(s.SelectItem,{value:g,className:"text-xs",children:v},g))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Size"}),e.jsxs(s.Select,{value:String(u),onValueChange:g=>w({pointRadius:Number(g)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Wa.map(g=>e.jsx(s.SelectItem,{value:String(g),className:"text-xs",children:g===0?"None":g},g))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Point Shape"}),e.jsxs(s.Select,{value:m,onValueChange:g=>w({pointStyle:g}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Xa.map(({value:g,label:v})=>e.jsx(s.SelectItem,{value:g,className:"text-xs",children:v},g))})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:x,onCheckedChange:j})]}),x&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Pe,{config:r==null?void 0:r.dataLabels,onChange:f,isHorizontal:!1,showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const tn=[0,2,4,6,8,10,12,16,20];function an(){const t=s.useEditorStore(n=>{var a;return(a=n.card)==null?void 0:a.type});return!t||!["bar","horizontalBar","stackedBar"].includes(t)?null:e.jsxs(s.AccordionItem,{value:"bar-series",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Layers,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Series"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsx(nn,{})})]})}function nn(){const{transformedMetricKeys:t}=Be();return t.length===0?e.jsx("div",{className:"rounded-lg border border-dashed border-border p-4 text-center text-xs text-muted-foreground",children:"Add metrics to configure series"}):e.jsx("div",{className:"space-y-3",children:t.map((n,a)=>e.jsx(rn,{metricKey:n,idx:a},n))})}function rn({metricKey:t,idx:n}){var j;const[a,l]=L.useState(!1),c=s.useEditorStore(f=>{var p;return(p=f.card)==null?void 0:p.type}),d=s.useEditorStore(f=>{var p;return(p=f.card)==null?void 0:p.preferences}),r=s.useEditorStore(f=>{var p,g;return(g=(p=f.card)==null?void 0:p.preferences)==null?void 0:g.datasetOptions}),i=r==null?void 0:r.find(f=>f.idx===n),{setCardPreferences:o}=s.useEditorStore(f=>f.actions),u=(i==null?void 0:i.borderRadius)??0,m=((j=i==null?void 0:i.dataLabels)==null?void 0:j.enabled)??!1;function x(f){const p={idx:n,borderRadius:u,dataLabels:i==null?void 0:i.dataLabels,...f};if(!i)o({...d||{},datasetOptions:[...r||[],p]});else{const g=r==null?void 0:r.map(v=>v.idx===n?{...v,...p}:v);o({...d||{},datasetOptions:g})}}function h(f){x({dataLabels:{...i==null?void 0:i.dataLabels,enabled:f}})}function w(f){x({dataLabels:f})}return e.jsx(s.Collapsible,{open:a,onOpenChange:l,children:e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30",children:[e.jsx(s.CollapsibleTrigger,{asChild:!0,children:e.jsxs("button",{className:"flex w-full items-center justify-between gap-3 overflow-hidden px-3 py-2 text-left hover:bg-muted/50",children:[e.jsxs("div",{className:"flex min-w-0 flex-1 items-center gap-2",children:[e.jsx(s.ChevronRight,{className:s.cn("h-4 w-4 shrink-0 text-muted-foreground transition-transform",a&&"rotate-90")}),e.jsx("span",{className:"max-w-[100px] truncate text-xs font-medium",title:t,children:t})]}),e.jsx("div",{className:"flex shrink-0 items-center gap-1.5",children:u>0&&e.jsxs("span",{className:s.cn("flex items-center gap-1 rounded px-1.5 py-0.5 text-xs","border border-border bg-background text-muted-foreground"),children:[e.jsx(s.ChartColumn,{className:"h-3 w-3"}),"r:",u]})})]})}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-4 p-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Bar Style"}),e.jsx("div",{className:"space-y-3",children:e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Corner Radius"}),e.jsxs(s.Select,{value:String(u),onValueChange:f=>x({borderRadius:Number(f)}),children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:tn.map(f=>e.jsx(s.SelectItem,{value:String(f),className:"text-xs",children:f===0?"None":`${f}px`},f))})]})]})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs text-muted-foreground",children:"Data Labels"}),e.jsx(s.SmallSwitch,{checked:m,onCheckedChange:h})]}),m&&e.jsx("div",{className:"rounded-md border border-border bg-background p-2",children:e.jsx(Pe,{config:i==null?void 0:i.dataLabels,onChange:w,isHorizontal:c==="horizontalBar",showAutoFormat:!0,showAppearance:!0})})]})]})})})]})})}const ln=[0,1,2,3,4,5,6,8,10],on=[{value:"circle",label:"Circle ●"},{value:"rect",label:"Square ■"},{value:"rectRounded",label:"Rounded ▢"},{value:"rectRot",label:"Diamond ◆"},{value:"triangle",label:"Triangle ▲"},{value:"cross",label:"Cross +"},{value:"crossRot",label:"X ×"},{value:"star",label:"Star ★"},{value:"dash",label:"Dash —"},{value:"line",label:"Line │"}],cn=[{value:"solid",label:"Solid ───"},{value:"dashed",label:"Dashed - - -"},{value:"dotted",label:"Dotted ···"},{value:"dash-dot",label:"Dash-Dot -·-·"}];function dn(){const t=s.useEditorStore(r=>r.card),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),a=n==null?void 0:n.lineStyleConfig,{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!["line","area","stackedArea"].includes(t.type))return null;const d=(r,i)=>{const o={...n||{},lineStyleConfig:{...a,[r]:i}};l(o),c({...t,preferences:o})};return e.jsxs(s.AccordionItem,{value:"line-style",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(bt,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Line Style"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Default styles for all series. Override per-series in the Series section."}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Line Pattern"}),e.jsxs(s.Select,{value:(a==null?void 0:a.lineStyle)||"solid",onValueChange:r=>d("lineStyle",r),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:cn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Point Size"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.pointRadius)??3),onValueChange:r=>d("pointRadius",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:ln.map(r=>e.jsx(s.SelectItem,{value:String(r),className:"text-xs",children:r===0?"None":r},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Point Shape"}),e.jsxs(s.Select,{value:(a==null?void 0:a.pointStyle)||"circle",onValueChange:r=>d("pointStyle",r),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:on.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]})]})})]})}const un=[0,2,4,6,8,10,12,16,20],mn=[{value:"0.5",label:"50%"},{value:"0.6",label:"60%"},{value:"0.7",label:"70%"},{value:"0.8",label:"80%"},{value:"0.9",label:"90%"},{value:"1",label:"100%"}],xn=[{value:"0.5",label:"50%"},{value:"0.6",label:"60%"},{value:"0.7",label:"70%"},{value:"0.8",label:"80%"},{value:"0.9",label:"90%"},{value:"1",label:"100%"}];function hn(){const t=s.useEditorStore(r=>r.card),n=s.useEditorStore(r=>{var i;return(i=r.card)==null?void 0:i.preferences}),a=n==null?void 0:n.barStyleConfig,{setCardPreferences:l,updateCardInFrame:c}=s.useEditorActions();if(!t||!["bar","horizontalBar","stackedBar"].includes(t.type))return null;const d=(r,i)=>{const o={...n||{},barStyleConfig:{...a,[r]:i}};l(o),c({...t,preferences:o})};return e.jsxs(s.AccordionItem,{value:"bar-style",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.ChartColumn,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"Bar Style"})]})}),e.jsx(s.AccordionContent,{className:"pt-2",children:e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Default styles for all bars. Override per-series in the Series section."}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Corner Radius"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.borderRadius)??0),onValueChange:r=>d("borderRadius",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:un.map(r=>e.jsx(s.SelectItem,{value:String(r),className:"text-xs",children:r===0?"None":`${r}px`},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Bar Width"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.barPercentage)??.9),onValueChange:r=>d("barPercentage",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:mn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(s.Label,{className:"text-xs font-medium",children:"Category Width"}),e.jsxs(s.Select,{value:String((a==null?void 0:a.categoryPercentage)??.8),onValueChange:r=>d("categoryPercentage",Number(r)),children:[e.jsx(s.SelectTrigger,{className:"h-8 text-xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:xn.map(({value:r,label:i})=>e.jsx(s.SelectItem,{value:r,className:"text-xs",children:i},r))})]})]})]})})]})}function pn(){const t=s.useEditorStore(a=>{var l,c;return(c=(l=a==null?void 0:a.card)==null?void 0:l.preferences)==null?void 0:c.sortChart}),{setSortChart:n}=s.useEditorActions();return e.jsxs(s.AccordionItem,{value:"stacked-sort",children:[e.jsx(s.AccordionTrigger,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.ArrowUpDown,{className:"h-4 w-4"}),"Sort"]})}),e.jsx(s.AccordionContent,{className:"space-y-4 px-1",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{children:"Sort Order"}),e.jsx(pe,{className:"h-8",label:"Sort Chart",options:[{label:"None",value:"none"},{label:"Ascending",value:"asc"},{label:"Descending",value:"desc"}],value:t||"none",onValueChange:a=>{n(a)}})]})})]})}function qs({mode:t}){const n=s.useEditorStore(a=>{var l;return(l=a.card)==null?void 0:l.type});return e.jsxs(e.Fragment,{children:[n==="combo"&&e.jsx($a,{}),(n==="area"||n==="stackedArea")&&e.jsx(Ja,{}),n==="line"&&e.jsx(Za,{}),(n==="bar"||n==="horizontalBar"||n==="stackedBar")&&e.jsx(an,{}),e.jsx(dn,{}),e.jsx(hn,{}),e.jsx(Ta,{}),t==="config"&&e.jsx(wa,{}),t==="config"&&e.jsx(Sa,{}),e.jsx(ka,{}),n==="bullet"&&e.jsx(Ia,{}),n==="funnel"&&e.jsx(La,{}),(n==="bubble"||n==="scatter")&&e.jsx(Aa,{}),(n==="stackedBar"||n==="stackedLine")&&e.jsx(pn,{}),e.jsx(pa,{}),e.jsx(va,{}),e.jsx(ya,{}),e.jsx(Na,{})]})}function gn(){const t=s.useEditorStore(a=>a.card),n=t==null?void 0:t.type;return n?e.jsxs("div",{className:"space-y-4 px-6 py-4",children:[e.jsx(Fe,{chartType:n}),e.jsx(s.Accordion,{type:"single",collapsible:!0,className:"w-full px-2",children:e.jsx(qs,{mode:"dev"})})]}):null}function fn(){const t=s.useEditorStore(a=>a.card.type);function n(){switch(t){case"kpi":return e.jsx(ta,{});case"table":return e.jsx(ca,{});case"map":return e.jsx(la,{});case"custom":return e.jsx("div",{className:"px-6 py-2",children:e.jsx(Vs,{})});case"text":return e.jsx(ua,{});default:return e.jsx(gn,{})}}return e.jsxs(s.ScrollArea,{className:"h-full w-full text-sm",children:[n(),e.jsx(s.ScrollBar,{orientation:"horizontal"})]})}function jn(){const t=L.useRef(null),n=L.useRef(null),a=s.useEditorStore(c=>c.isShowingVisual),l=s.useEditorStore(c=>c.showAIDialog);return L.useEffect(()=>{var c,d,r,i;l?((c=t.current)==null||c.resize(0),(d=n.current)==null||d.resize(0)):((r=t.current)==null||r.resize(25),(i=n.current)==null||i.resize(25))},[l]),e.jsxs(s.ResizablePanelGroup,{direction:"vertical",className:"min-h-[250px] min-w-full max-w-lg",children:[e.jsx(s.ResizablePanel,{role:"query-section",className:"flex",minSize:20,defaultSize:40,children:e.jsx(Ot,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{className:"pb-1",role:"visual-section",minSize:0,defaultSize:60,children:a&&!l?e.jsxs(s.ResizablePanelGroup,{direction:"horizontal",children:[e.jsx(e.Fragment,{children:e.jsx(s.ResizablePanel,{ref:t,className:"flex",defaultSize:25,maxSize:30,children:e.jsx(fn,{})})}),!l&&e.jsx(s.ResizableHandle,{}),e.jsx(s.ResizablePanel,{minSize:40,defaultSize:75,maxSize:100,children:e.jsx(us,{})})]}):e.jsx(us,{})})]})}function bn(t){var ue;s.useAIInteractionStore(K=>K.currentAIContext);const[n,a]=L.useState(""),[l,c]=L.useState(null),{authToken:d,tokenProps:r}=s.useSemaphorContext(),i=s.useEditorStore(K=>{var ee;return(ee=K==null?void 0:K.card)==null?void 0:ee.sql}),o=s.useEditorStore(K=>K.isDevMode),u=s.useEditorStore(K=>{var ee;return(ee=K==null?void 0:K.card)==null?void 0:ee.python}),{setCardPython:m,setCardSql:x,setRunSql:h,switchCardType:w,setQueryConfig:j}=s.useEditorActions(),f=s.useEditorStore(K=>K.currentAssitantScope)||[],p=s.useEditorStore(K=>K.userInputForAI),g=s.useEditorStore(K=>K.triggerAIRun),v=s.useEditorStore(K=>K.card),O=s.useEditorStore(K=>K.selectedDatabaseName),A=s.useEditorStore(K=>K.selectedTableName),y=s.useEditorStore(K=>K.selectedConnectionId),T=s.useEditorStore(K=>K.selectedSchemaName),N=s.useDashboardStore(K=>K.dashboard.aiScopeTables),{errorMessage:C,refetch:k,data:R}=s.useDashboardCardQuery(v),{setTriggerAIRun:b,setCurrentAssitantScope:S,setCardConfig:D}=s.useEditorActions(),{selectedDatamodelName:F,connectionType:I,selectedDatamodelId:M}=s.useEditorAside(),E=(ue=s.useDashboardQuery().data)==null?void 0:ue.assistantProfileId,{messages:B,setMessages:$,regenerate:V,error:q,sendMessage:G,status:z,addToolResult:H,stop:_,clearError:P}=s.useChat({transport:new s.DefaultChatTransport({api:`${r.apiServiceUrl}/v1/assistant`,headers:()=>({Authorization:`Bearer ${d==null?void 0:d.accessToken}`}),body:()=>{const K=s.useAIInteractionStore.getState();return{random:Math.random(),assistantId:"editor-assistant",assistantProfileId:E,isDevMode:o,aiContext:K.currentAIContext,reasoningEffort:K.reasoningEffort,showReasoning:K.showReasoning}},credentials:"omit",prepareSendMessagesRequest:({messages:K,body:ee})=>({body:{...ee,messages:s.cleanupIncompleteMessages(K)}})}),onError:K=>{var ee;if(K.name==="AbortError"||(ee=K.message)!=null&&ee.includes("aborted")){console.log("[Editor Assistant] Stream aborted (expected)");return}console.error("[Editor Assistant] Stream error:",K)},onToolCall:Y,onFinish({isAbort:K,isDisconnect:ee,isError:de}){a(""),console.log(K?"[Editor Assistant] Stream aborted by user":ee?"[Editor Assistant] Stream disconnected (network error)":de?"[Editor Assistant] Stream error occurred":"[Editor Assistant] Stream completed successfully")}});L.useEffect(()=>{g&&p&&(G({role:"user",parts:[{type:"text",text:p}]}),b(!1))},[g,p,G,b]);async function Y({toolCall:K}){var ee,de,me,Se,xe,je,he,be,we,ge,Te,Ee,ke,Ie,ve,ye;if(["createChartFromSql","createForecast"].includes(K.toolName)){const J=((ee=K.input)==null?void 0:ee.sql)??((de=K.args)==null?void 0:de.sql),ne=((me=K.input)==null?void 0:me.python)??((Se=K.args)==null?void 0:Se.python),re=((xe=K.input)==null?void 0:xe.chartType)??((je=K.args)==null?void 0:je.chartType);w(re),x(J),m(ne),h(!0),H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",message:"Let the user know that chart is created"}})}else if(K.toolName==="getUserSql"){if(!i){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"error",message:"No SQL found"}});return}H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",sql:i,python:u||""}})}else if(K.toolName==="generateQueryConfig"){a("Generating query config...");const J=((he=K.input)==null?void 0:he.chartType)??((be=K.args)==null?void 0:be.chartType),ne=((we=K.input)==null?void 0:we.queryConfig)??((ge=K.args)==null?void 0:ge.queryConfig);((Te=K.input)==null?void 0:Te.cardConfig)??((Ee=K.args)==null||Ee.cardConfig),w(J),D(ne),x(void 0),m(void 0);const re=await k();if((ke=re==null?void 0:re.data)!=null&&ke.error){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"error",queryConfig:ne,message:(Ie=re==null?void 0:re.data)==null?void 0:Ie.error.message}});return}if((ve=re==null?void 0:re.data)!=null&&ve.records){H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",queryConfig:ne,message:"The data has been visualized"}});return}H({tool:K.toolName,toolCallId:K.toolCallId,output:{status:"success",queryConfig:ne,message:"The data has been visualized",records:(ye=re==null?void 0:re.data)==null?void 0:ye.records}})}else a("Analyzing...")}function Z(K){const ee=s.removeFromScopeArray(f,K);se(ee)}function se(K){const ee=s.sanitizeAIScope(K);S(ee),$(de=>[...de,{id:crypto.randomUUID(),role:"system",parts:[{type:"text",text:`Current scope of tables. Always use the table reference format when generating SQL | ${JSON.stringify(ee)}`}]}])}function W(){const K={databaseName:O||"",schemaName:T||"",tableName:A||"",datamodelId:M||"",datamodelName:s.resolveDatamodelName(F),connectionId:y||"",connectionType:I||""},ee=s.dedup([...f,K]);se(ee)}function ae(){const K={databaseName:O||"",schemaName:T||"",tableName:A||"",datamodelName:s.resolveDatamodelName(F),connectionId:y||"",connectionType:I||"",datamodelId:M||""};if(!s.isInAiScopeArray([...N||[],...f],K)||f.length===0){const de=s.dedup([...N||[],...f,K]).filter(me=>me.connectionId===K.connectionId);se(de)}}const Q=!["ready","error"].includes(z),ce=s.getErrorMessage(q);return{messages:B,setMessages:$,sendMessage:G,regenerate:V,isLoading:Q,handleSetAIScope:ae,handleAddCurrentTableToScope:W,removeFromCurrentSelections:Z,currentAssitantScope:f,setCurrentAssitantScope:S,stop:_,status:z,toolStatus:n,llmUsage:l,onFinish:t,errorMessage:ce,clearError:P}}const vn=[{title:"Help me write a query for",subtitle:"orders by month"},{title:"Create a bar chart for",subtitle:"sales by category"}];function vs({isFullscreen:t,onFullscreenChange:n,fullscreenContainer:a}){const l=L.useRef(null),[c,d]=L.useState(!1),[r,i]=L.useState(s.isDevEnv),[o,u]=L.useState(!1),[m,x]=L.useState(!1),[h,w]=L.useState(!1),j=t??h,f=s.useCurrentAIContext(),{setCurrentAIContext:p}=s.useAIActions(),{clearQueue:g}=s.useQueueActions(),{clearFileAttachments:v}=s.useFileActions(),O=s.useFileAttachments("editor"),A=s.useDashboardStore(J=>J.dashboard.aiContext),y=s.useEditorStore(J=>J.card.dataSource),T=s.useEditorStore(J=>J.actions.setCardSql),N=s.useEditorStore(J=>J.actions.setRunSql),C=s.useEditorStore(J=>J.showAIDialog),k=s.useDashboardStore(J=>J.handleOpenAssistantProfile),{setShowAIDialog:R,setCardPython:b}=s.useEditorActions(),{setDataSource:S}=s.useEditorActions(),{assistantProfile:D}=s.useAssistantProfile(),{messages:F,setMessages:I,sendMessage:M,isLoading:E,stop:B,status:$,llmUsage:V,errorMessage:q,clearError:G}=bn(),[z,H]=L.useState(""),_=J=>{H(J.target.value)},P=J=>{J.preventDefault();const ne=z.trim();if(!ne&&O.length===0)return;const re=[];ne&&re.push({type:"text",text:ne}),O.forEach(Ne=>{Ne.data&&re.push({type:"file",mediaType:Ne.type,filename:Ne.name,url:`data:${Ne.type};base64,${Ne.data}`})}),M({role:"user",parts:re}),H(""),v("editor"),ee({force:!0})},{visibleMessages:Y,hasMoreMessages:Z,totalMessageCount:se,loadMoreMessages:W,resetMessageWindow:ae}=s.useMessageWindow(F,{maxVisibleMessages:15,initialLoadCount:10,loadMoreCount:5}),{scrollRef:Q,isAtBottom:ce,hasNewMessages:ue,newMessageCount:K,scrollToBottom:ee,handleScroll:de,resetNewMessageCount:me}=s.useAutoScroll(Y,{threshold:50,behavior:"smooth",isStreaming:E});L.useEffect(()=>{var J;C&&((J=l.current)==null||J.focus(),p({selectedEntities:(y==null?void 0:y.selectedEntities)||[]}))},[C,y,p]),s.useAutoSubmitQueue({assistantType:"editor",status:$,isLoading:E,sendMessage:M});const{hasActiveTools:Se}=s.useActiveToolStatus(F),xe=L.useCallback(J=>{n==null||n(J),t===void 0&&w(J)},[t,n]);L.useEffect(()=>{if(!j)return;function J(ne){ne.key==="Escape"&&xe(!1)}return window.addEventListener("keydown",J),()=>{window.removeEventListener("keydown",J)}},[j,xe]);const je=L.useCallback(J=>{M({role:"user",parts:[{type:"text",text:J}]}),ee({force:!0})},[M,ee]),he=L.useCallback(async()=>{const J=JSON.stringify(F??[],null,2);try{if(typeof navigator>"u"||!navigator.clipboard)throw new Error("Clipboard API unavailable");await navigator.clipboard.writeText(J),s.ue.success("Conversation copied")}catch(ne){console.error("Failed to copy conversation",ne),s.ue.error("Unable to copy conversation")}},[F]),be=L.useCallback(()=>{xe(!j)},[j,xe]);function we(J,ne){ne==="sql"?(T(J),N(!0)):ne==="python"&&(b(J),N(!0))}function ge(){f.selectedEntities.length===0&&p({selectedEntities:(A==null?void 0:A.selectedEntities)||[]})}function Te(J){ge();const ne={role:"user",parts:[{type:"text",text:J.title+" "+J.subtitle}]};M(ne)}function Ee(J){ge(),M({role:"user",parts:[{type:"text",text:J}]})}function ke(){return e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsx(s.Button,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",variant:"ghost",children:e.jsx(s.EllipsisVertical,{className:"size-3.5"})})}),e.jsxs(s.DropdownMenuContent,{align:"end",children:[e.jsx(s.DropdownMenuLabel,{children:"Options"}),e.jsx(s.DropdownMenuSeparator,{}),e.jsx(s.DropdownMenuCheckboxItem,{checked:c,onCheckedChange:d,children:"Token Usage"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:r,onCheckedChange:i,children:"Tool Invocations"}),e.jsx(s.DropdownMenuCheckboxItem,{checked:o,onCheckedChange:u,children:"System Message"})]})]})}function Ie(){return e.jsx("div",{"data-role":"header",children:e.jsx("div",{className:"text-lg font-semibold",children:"Assistant"})})}const ve=(z==null?void 0:z.length)===0&&(F==null?void 0:F.filter(J=>J.role==="user").length)===0,ye=e.jsxs("div",{"data-role":"editor-assistant",className:s.cn("flex h-full w-full flex-col justify-between px-6 py-4",j&&"rounded-2xl border border-border/60 bg-background px-8 pb-6 pt-6 shadow-xl"),children:[e.jsxs("div",{"data-role":"header-section",className:"flex items-start justify-between gap-2",children:[Ie(),k&&e.jsx(s.Button,{variant:"outline",size:"sm",className:"h-7 p-2",onClick:k,children:(D==null?void 0:D.name)||"Default Profile"}),e.jsxs("div",{"data-role":"header-actions",className:"flex items-center",children:[ke(),r&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"ml-2 h-7 px-2 text-muted-foreground hover:text-foreground",onClick:he,children:"Copy Conversation"}),e.jsx(s.IconButton,{tooltip:j?"Exit Fullscreen":"Expand",className:"h-7 p-2 text-muted-foreground hover:text-foreground",onClick:be,children:j?e.jsx(s.Minimize2,{className:"size-4"}):e.jsx(s.Maximize2,{className:"size-4"})}),e.jsx(s.IconButton,{className:"h-7 p-2 text-muted-foreground hover:text-foreground",tooltip:"New Chat",onClick:()=>{I([]),g("editor"),G(),ae()},children:e.jsx(s.SquarePen,{className:"size-4"})}),e.jsx(s.Button,{variant:"ghost",className:"h-7 p-2 text-muted-foreground hover:text-foreground",size:"sm",onClick:()=>{R(!1)},children:e.jsx(s.X,{className:"size-4"})})]})]}),e.jsx(s.CustomScrollArea,{ref:Q,className:"mt-2 flex w-full grow basis-0",onScroll:de,children:e.jsxs("div",{className:"space-y-4 py-2 pr-3","data-role":"messages",children:[Z&&e.jsx("div",{className:"flex justify-center py-2",children:e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:W,className:"text-xs",children:["Load ",Math.min(5,se-Y.length)," ","more messages (",se-Y.length," ","hidden)"]})}),e.jsx(s.PrintMessages,{messages:Y,showTools:r,handleRun:we,showSystemMessage:o,onQuickReply:je,disableQuickReplies:E}),!Se&&e.jsx(s.AssistantStatus,{status:$,errorMessage:q})]})}),C&&!ce&&e.jsx("div",{className:"absolute bottom-24 right-8 z-10",children:e.jsxs(s.Button,{onClick:()=>{ee({force:!0}),me()},size:"sm",variant:"secondary",className:"rounded-full shadow-lg","aria-label":"Scroll to bottom",children:[e.jsx(s.ChevronDown,{className:"h-4 w-4"}),ue&&K>0&&e.jsx("span",{className:"ml-1.5 flex h-5 w-5 items-center justify-center rounded-full bg-primary text-xs text-primary-foreground",children:K>9?"9+":K})]})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[ve&&!D&&e.jsx(s.Placeholders,{onClick:Te,placeholders:vn}),e.jsx("div",{children:ve&&D&&e.jsx(s.SeedQuestions,{seedQuestions:D.seedQuestions||[],handleSeedQuestionClick:Ee})})]}),e.jsx(s.PendingMessagesDisplay,{assistantType:"editor",className:"mb-3"}),e.jsx(s.ChatInputWithContext,{ref:l,setInput:H,stop:B,input:z,handleInputChange:_,handleSubmit:J=>{ge(),P(J),ee({force:!0})},isLoading:E,status:$,assistantType:"editor",onFocus:()=>{}})]}),V&&c&&e.jsx("div",{className:"flex h-8 items-center justify-between",children:e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Usage: ",V.inputTokens," prompt tokens, ",V.outputTokens," ","completion tokens, ",V.totalTokens," total tokens"]})}),e.jsx(s.DataSourceDialog,{incomingSelectedEntities:(y==null?void 0:y.selectedEntities)||[],open:m,onOpenChange:x,dataSource:y||s.EMPTY_DATA_SOURCE,dialogMode:"aiContext"})]});return j&&a?Xs.createPortal(e.jsx("div",{className:"pointer-events-auto absolute inset-0 z-50 flex bg-background px-6 pb-6 pt-[calc(var(--app-header-height,15px)+1rem)]",children:e.jsx("div",{className:"flex h-full w-full",children:ye})}),a):ye}function yn({selectedChart:t,onChartChange:n}){const[a,l]=L.useState(!1),c=s.useEditorStore(i=>i.card),d=()=>{var o;if(t==="custom")return((o=c==null?void 0:c.customCardPreferences)==null?void 0:o.componentName)||"Custom Visual";const i=s.chartTypes.find(u=>u.id===t);return(i==null?void 0:i.label)||"Select chart type"},r=()=>{var o;if(t==="custom"){const u=(o=c==null?void 0:c.customCardPreferences)==null?void 0:o.componentName;if(u){const m=s.getAutoDetectedChartIcon(u);return m||e.jsx(s.ChartIcon,{chartName:u,isCustom:!0,className:"h-4 w-4"})}return e.jsx(s.Shapes,{className:"h-4 w-4"})}const i=s.chartTypes.find(u=>u.id===t);if(i){const u=i.icon;return e.jsx(u,{className:"h-4 w-4"})}return e.jsx(s.Shapes,{className:"h-4 w-4"})};return e.jsx("div",{children:e.jsxs(s.Popover,{open:a,onOpenChange:l,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":a,className:"w-full justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[r(),e.jsx("span",{children:d()})]}),e.jsx(s.ChevronDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"w-[360px] p-3",align:"start",onOpenAutoFocus:i=>i.preventDefault(),children:e.jsx(Fs,{onChartSelect:i=>n(i),onClose:()=>l(!1)})})]})})}let Nn=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce((n,a)=>(a&=63,a<36?n+=a.toString(36):a<62?n+=(a-26).toString(36).toUpperCase():a>62?n+="-":n+="_",n),"");const Oe=[{id:"sum",label:"Sum",symbol:"+",description:"Add two metrics together",template:()=>"({field_0} + {field_1})",requiresTwoInputs:!0},{id:"difference",label:"Difference",symbol:"−",description:"Subtract one metric from another",template:()=>"({field_0} - {field_1})",requiresTwoInputs:!0},{id:"product",label:"Product (Multiplication)",symbol:"×",description:"Multiply two metrics",template:()=>"({field_0} * {field_1})",requiresTwoInputs:!0},{id:"ratio",label:"Ratio",symbol:"÷",description:"Divide one metric by another",template:()=>"({field_0} / NULLIF({field_1}, 0))",requiresTwoInputs:!0},{id:"change",label:"Change",symbol:"Δ",description:"Calculate percentage change from A to B",template:()=>"(({field_1} - {field_0}) / NULLIF({field_0}, 0))",requiresTwoInputs:!0},{id:"percentage",label:"Percentage",symbol:"%",description:"Calculate (A / B) × 100",template:()=>"(({field_0} / NULLIF({field_1}, 0)) * 100)",requiresTwoInputs:!0},{id:"scale",label:"Scale (Multiply by Factor)",symbol:"×n",description:"Multiply a metric by a constant factor",template:()=>"({field_0} * {constant})",requiresTwoInputs:!1},{id:"custom",label:"Custom Expression",symbol:"{}",description:"Drag fields to create custom expression with tokens",requiresTwoInputs:!1,allowsCustomExpression:!0}];function Cn(t,n,a,l,c,d,r,i,o){const u=Oe.find(f=>f.id===n);if(!u)throw new Error(`Unknown operation: ${n}`);let m,x=[],h;if(n==="custom"){if(!(i!=null&&i.trim()))throw new Error("Custom expression is required");m=i.trim()}else if(n==="scale"){if(!a)throw new Error("Metric A is required for scale operation");if(o===void 0||o===0)throw new Error("Constant factor is required for scale operation");m=u.template().replace("{constant}",String(o)),x=[a],h=o}else{if(!a||!l)throw new Error("Metrics A and B are required for standard operations");m=u.template(),x=[a,l]}const w={operation:n,inputFields:x,...h!==void 0&&{constant:h}};return{id:r||`calc_${Nn(10)}`,name:t.toLowerCase().replace(/\s+/g,"_"),label:t,alias:t.toLowerCase().replace(/\s+/g,"_"),qualifiedFieldName:t.toLowerCase().replace(/\s+/g,"_"),dataType:"number",role:"metric",aggregate:"SUM",expression:m,calculatedFormula:w,entityId:d,entityName:s.CALCULATED_FIELD_ENTITY_MARKER,entityType:"table",description:`Calculated field: ${t}`}}function ys(t){var l,c;const n=Oe.find(d=>d.id===t.operation);if(!n)return"Unknown operation";if(t.operation==="custom")return"Custom expression";if(t.operation==="scale"){const d=((l=t.inputFields[0])==null?void 0:l.label)||((c=t.inputFields[0])==null?void 0:c.name)||"A",r=t.constant??1;return`${d} × ${r}`}return t.inputFields.map(d=>d.label||d.name).join(` ${n.symbol} `)}const Xe=({dataType:t})=>s.getFieldIconComponent(t);function Sn({aliasTemplate:t,onAliasTemplateChange:n}){var o;const[a,l]=L.useState(t),[c,d]=L.useState(!1),r=(o=s.useEditorStore(u=>{var m;return(m=u.card.config)==null?void 0:m.pivotByColumns}))==null?void 0:o[0],i=r?`e.g. Min Sales in {{${r.name}}}`:"e.g. Min Sales in {{region}}";return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-gray-600",children:"Pivot Alias Template"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Input,{placeholder:i,value:c?a:t,onChange:u=>{l(u.target.value),d(!0)},onFocus:()=>d(!0),onPointerDown:u=>u.stopPropagation(),className:"h-8 text-xs",onKeyDown:u=>{u.key==="Enter"&&c&&a!==t?(n(a),d(!1)):u.key==="Escape"&&(l(t),d(!1))},"aria-label":"Edit alias template"}),c&&a!==t&&e.jsxs(e.Fragment,{children:[e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm alias template",onClick:()=>{n(a),d(!1)},children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel alias template edit",onClick:()=>{l(t),d(!1)},children:e.jsx(s.X,{className:"h-4 w-4 text-gray-400"})})]})]})]})}function wn({valueAliases:t,onValueAliasesChange:n}){const[a,l]=L.useState(null),[c,d]=L.useState(""),[r,i]=L.useState(""),o=()=>{n({...t,"":""})},u=x=>{const h={...t};delete h[x],n(h)},m=x=>{const h={...t};delete h[x],h[c]=r,n(h),l(null)};return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Pivot Value Aliases"}),e.jsxs("div",{className:"space-y-2",children:[Object.entries(t).map(([x,h],w)=>e.jsx("div",{className:"flex items-center gap-2",children:a===x?e.jsxs(e.Fragment,{children:[e.jsx(s.Input,{placeholder:"Value key",value:c,onChange:j=>d(j.target.value),className:"h-8 w-24 text-xs",onPointerDown:j=>j.stopPropagation(),onKeyDown:j=>{j.key==="Enter"?m(x):j.key==="Escape"&&l(null)},"aria-label":"Edit value alias key"}),e.jsx(s.Input,{placeholder:"Alias label",value:r,onChange:j=>i(j.target.value),className:"h-8 w-24 text-xs",onPointerDown:j=>j.stopPropagation(),onKeyDown:j=>{j.key==="Enter"?m(x):j.key==="Escape"&&l(null)},"aria-label":"Edit value alias label"}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Confirm value alias edit",onClick:()=>m(x),children:e.jsx(s.Check,{className:"h-4 w-4 text-green-600"})}),e.jsx(s.Button,{variant:"ghost",size:"icon",className:"h-6 w-6 p-0","aria-label":"Cancel value alias edit",onClick:()=>l(null),children:e.jsx(s.X,{className:"h-4 w-4 text-muted-foreground"})})]}):e.jsxs(e.Fragment,{children:[e.jsx(s.Input,{placeholder:"Value key",value:x,className:"h-8 w-24 text-xs",readOnly:!0,onClick:()=>{l(x),d(x),i(h)},"aria-label":"Edit value alias key"}),e.jsx(s.Input,{placeholder:"Alias label",value:h,className:"h-8 w-24 text-xs",readOnly:!0,onClick:()=>{l(x),d(x),i(h)},"aria-label":"Edit value alias label"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-8 w-8 p-0",onClick:()=>u(x),"aria-label":"Remove value alias",children:e.jsx(s.X,{className:"h-3 w-3"})})]})},x+w)),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"mt-1 h-8 text-xs",onClick:o,children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"})," Add Value Alias"]})]})]})}function Tn({idx:t}){var i;const n=s.useEditorStore(o=>{var u;return(u=o.card)==null?void 0:u.preferences}),a=s.useEditorStore(o=>{var u;return(u=o.card.preferences)==null?void 0:u.datasetOptions}),l=s.useEditorStore(o=>o.actions.setCardPreferences),c=(i=n==null?void 0:n.chartOptions)==null?void 0:i.indexAxis,d=a==null?void 0:a.find(o=>o.idx===t);function r(o,u){(o==="end"||o==="start")&&s.getContrastColor(s.getCanvasBackgroundColor());const m={display:o!=="none",align:o,anchor:o,clamp:!0};if(!d)l({...n,datasetOptions:[...a||[],{idx:u,datalabels:m}]});else{const x={...d};x.datalabels=m;const h=a==null?void 0:a.map(w=>w.idx===u?x:w);l({...n,datasetOptions:h})}}return e.jsxs(s.Popover,{children:[e.jsx(s.PopoverTrigger,{children:e.jsx(s.Tag,{className:"size-4 rotate-[135deg] cursor-pointer text-muted-foreground hover:text-foreground"})}),e.jsx(s.PopoverContent,{className:"w-fit px-2 py-1",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>r("none",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(s.X,{className:"size-3.5"})}),e.jsx("button",{onClick:()=>r("start",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(mt,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})}),e.jsx("button",{onClick:()=>r("center",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(pt,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})}),e.jsx("button",{onClick:()=>r("end",t),className:"h-7 p-1 text-xs font-normal",children:e.jsx(ht,{className:s.cn("h-4 w-4",{"rotate-90":c==="y"})})})]})})]})}function Ze({children:t,className:n,delayDuration:a}){return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:a,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:s.cn("overflow-hidden text-ellipsis whitespace-nowrap",n),children:t})}),e.jsx(s.TooltipContent,{children:t})]})})}function En(t,n=30){return t.length>n?t.slice(0,n)+"...":t}function kn({col:t,idx:n,className:a,iconClassName:l,children:c}){const d=s.useEditorStore(m=>{var x;return(x=m.card)==null?void 0:x.preferences}),r=s.useEditorStore(m=>{var x,h;return(h=(x=m.card)==null?void 0:x.preferences)==null?void 0:h.datasetOptions}),i=r==null?void 0:r.find(m=>m.idx===n),{setCardPreferences:o}=s.useEditorStore(m=>m.actions);function u(m){if(!i)o({...d,datasetOptions:[...r||[],{idx:n,type:m==="area"?"line":m,fill:m==="area"?"origin":""}]});else{const x={...i};x.type=m==="area"?"line":m,x.fill=m==="area"?"origin":"";const h=r==null?void 0:r.map(w=>w.idx===n?x:w);o({...d,datasetOptions:h})}}return e.jsxs("div",{className:s.cn("group flex items-center justify-between gap-2 rounded-md",a),children:[c||e.jsx(Ze,{className:"max-w-[100px]",title:t,children:En(t)}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(s.ChartColumnIncreasing,{onClick:()=>u("bar"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(s.ChartLine,{onClick:()=>u("line"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(s.ChartArea,{onClick:()=>u("area"),className:s.cn("size-4 cursor-pointer text-muted-foreground hover:text-foreground",l)}),e.jsx(Tn,{idx:n})]})]})}function In(){const t=s.useEditorStore(i=>i.card.type),n=s.useEditorStore(i=>i.isDevMode),a=s.useEditorStore(i=>i.card.preferences),{setCardPreferences:l}=s.useEditorStore(i=>i.actions);function c(i){var m;const o={datalabels:{display:i!=="none",align:i,anchor:i,clamp:!0}},u={...a,chartOptions:{...a==null?void 0:a.chartOptions,plugins:{...(m=a==null?void 0:a.chartOptions)==null?void 0:m.plugins,...o}}};l(u)}function d(){var i,o,u;return e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"font-medium",children:"Data Labels"}),e.jsxs(s.Select,{value:((u=(o=(i=a==null?void 0:a.chartOptions)==null?void 0:i.plugins)==null?void 0:o.datalabels)==null?void 0:u.anchor)||"none",onValueChange:c,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full focus:ring-1 focus:ring-offset-0",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"Data Labels"}),e.jsx(s.SelectItem,{value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"start",children:"Start"}),e.jsx(s.SelectItem,{value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{value:"end",children:"End"})]})})]})]})}function r(){var i,o,u;return e.jsx("div",{children:e.jsxs(s.Select,{value:((u=(o=(i=a==null?void 0:a.chartOptions)==null?void 0:i.plugins)==null?void 0:o.datalabels)==null?void 0:u.anchor)||"none",onValueChange:c,defaultValue:"none",children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select position"})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"none",children:"None"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"start",children:"Start"}),e.jsx(s.SelectItem,{className:"text-xs",value:"center",children:"Center"}),!["pyramid","funnel"].includes(t)&&e.jsx(s.SelectItem,{className:"text-xs",value:"end",children:"End"})]})})]})})}return e.jsx(e.Fragment,{children:n?d():r()})}function An({column:t}){const n=s.useEditorStore(o=>o.card.type),a=s.useEditorStore(o=>{var u,m;return(m=(u=o.card)==null?void 0:u.config)==null?void 0:m.metricColumns}),l=(a==null?void 0:a.map(o=>o.label))||[],c=l==null?void 0:l.find(o=>o===t.label||o===t.name),d=c?l.indexOf(c):-1;function r(){return e.jsx("div",{className:"pr-1",children:c&&e.jsx(kn,{idx:d,col:c,children:e.jsx("div",{className:"w-[130px] text-xs text-muted-foreground",children:"Display Series As"})})})}function i(){return e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Data Labels"}),e.jsx(In,{})]})}return e.jsxs("div",{className:"space-y-1",children:[["stackedBar","stackedLine","bar","line"].includes(n)&&r(),["pie","doughnut"].includes(n)&&i()]})}function Dn({field:t,searchQuery:n="",limit:a=500}){const{authToken:l,tokenProps:c}=s.useSemaphorContext(),d=s.useEditorStore(u=>u.card),r=d==null?void 0:d.dataSource,i=["attribute-values",t.entityName,t.name,n,a],o=s.useQuery({queryKey:i,queryFn:async()=>{if(!(r!=null&&r.connectionId))return console.warn("No connection ID available for attribute values query"),[];try{const u={groupByColumns:[{...t,role:"groupby"}],metricColumns:[],filters:n?{id:"search-filter",logicalOperator:"AND",negate:!1,conditions:[{id:"search-condition",field:t,operator:"contains",value:n}],groups:[]}:void 0,rowLimit:a},m={connection_id:r.connectionId,cardConfig:u,cardDataSource:r,cache_config:s.getCacheConfig({card:d})};console.log("requestBody",m);const x=await s.postRequestRegular(`${c.apiServiceUrl}/v1/query`,(l==null?void 0:l.accessToken)||"",m);if(x!=null&&x.error)return console.error("Error fetching attribute values:",x.error),[];const h=t.name;return((x==null?void 0:x.records)||[]).map(j=>{const f=j[h]||j[t.alias||h]||j[t.label];return{id:f||"",value:f||"",label:String(f||"(empty)")}})}catch(u){return console.error("Error fetching attribute values:",u),[]}},enabled:!!t&&!!(r!=null&&r.connectionId),staleTime:5*60*1e3,gcTime:10*60*1e3});return{values:o.data||[],isLoading:o.isLoading,isFetching:o.isFetching,isError:o.isError,error:o.error,refetch:o.refetch}}function Ks({open:t,onOpenChange:n,availableFields:a,existingFilters:l,editingCondition:c,onSave:d}){var O,A;const r=!!c,[i,o]=L.useState(r?"values":"fields"),[u,m]=L.useState(""),[x,h]=L.useState({id:(c==null?void 0:c.id)||s.v4(),field:(c==null?void 0:c.field)||null,selectedValues:c?Array.isArray(c.value)?c.value:[c.value]:[]}),w=a.filter(y=>{var N;return r?!1:!((N=l==null?void 0:l.conditions)==null?void 0:N.some(C=>C.field.id===y.id))&&y.label.toLowerCase().includes(u.toLowerCase())}),j=y=>{h({...x,field:y}),o("values")},f=()=>{if(x.field&&x.selectedValues.length>0){const y={id:x.id,field:x.field,operator:"in",value:x.selectedValues};let T;if(r){const N=((l==null?void 0:l.conditions)||[]).map(C=>C.id===c.id?y:C);T={...l,conditions:N}}else{const N=(l==null?void 0:l.conditions)||[];T={id:(l==null?void 0:l.id)||s.v4(),logicalOperator:"AND",negate:!1,conditions:[...N,y],groups:[]}}d(T),n(!1)}},p=()=>{if(!r||!c)return;const y=((l==null?void 0:l.conditions)||[]).filter(T=>T.id!==c.id);if(y.length>0){const T={...l,conditions:y};d(T)}else d(void 0);n(!1)},g=y=>{h({...x,selectedValues:y})},v=()=>{r&&c?(h({id:c.id,field:c.field,selectedValues:Array.isArray(c.value)?c.value:[c.value]}),o("values")):(h({id:s.v4(),field:null,selectedValues:[]}),o("fields")),m("")};return L.useEffect(()=>{t&&v()},[t,r,c]),e.jsx(s.Dialog,{open:t,onOpenChange:n,children:e.jsxs(s.DialogContent,{className:"max-w-2xl",children:[e.jsx(s.DialogHeader,{children:e.jsx(s.DialogTitle,{children:r?`Edit Filter: ${(O=x.field)==null?void 0:O.label}`:i==="fields"?"Select Field to Filter":`Select Values for ${(A=x.field)==null?void 0:A.label}`})}),e.jsxs("div",{className:"space-y-4",children:[!r&&(l==null?void 0:l.conditions)&&l.conditions.length>0&&i==="fields"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Current Filters:"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:l.conditions.map(y=>e.jsxs("div",{className:"flex items-center gap-1 px-2 py-1 bg-muted rounded-md text-xs",children:[e.jsxs("span",{className:"font-medium",children:[y.field.label,":"]}),e.jsx("span",{className:"text-muted-foreground",children:Array.isArray(y.value)?`${y.value.length} values`:String(y.value)})]},y.id))})]}),i==="fields"&&!r?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search fields...",value:u,onChange:y=>m(y.target.value),className:"pl-10"})]}),e.jsx("div",{className:"grid grid-cols-2 gap-2 max-h-[400px] overflow-y-auto",children:w.map(y=>e.jsxs(s.Button,{variant:"outline",className:"justify-start h-auto py-2 px-3",onClick:()=>j(y),children:[e.jsx(Xe,{dataType:s.getDataType(y.dataType)}),e.jsxs("div",{className:"ml-2 text-left",children:[e.jsx("div",{className:"text-sm font-medium",children:y.label}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[y.entityName,".",y.name]})]})]},y.id))}),w.length===0&&u&&e.jsxs("div",{className:"text-center py-4 text-sm text-muted-foreground",children:['No fields found matching "',u,'"']}),w.length===0&&!u&&a.length>0&&e.jsx("div",{className:"text-center py-4 text-sm text-muted-foreground",children:"All available fields are already filtered"})]}):e.jsx(Ln,{field:x.field,selectedValues:x.selectedValues,onValuesChange:g,onBack:r?void 0:()=>o("fields")}),e.jsxs("div",{className:"flex justify-between pt-4 border-t",children:[e.jsx("div",{children:r?e.jsx(s.Button,{variant:"destructive",onClick:p,children:"Delete Filter"}):e.jsx(s.Button,{variant:"outline",onClick:v,children:"Reset"})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(s.Button,{variant:"outline",onClick:()=>n(!1),children:"Cancel"}),e.jsx(s.Button,{onClick:f,disabled:!x.field||x.selectedValues.length===0,children:"Apply"})]})]})]})]})})}function Ln({field:t,selectedValues:n,onValuesChange:a,onBack:l}){const[c,d]=L.useState(""),{values:r,isLoading:i,isFetching:o}=Dn({field:t,searchQuery:c,limit:500}),u=c?r.filter(h=>String(h.value).toLowerCase().includes(c.toLowerCase())):r,m=h=>{const w=n.includes(h);a(w?n.filter(j=>j!==h):[...n,h])},x=()=>{n.length===u.length&&u.length>0?a([]):a(u.map(h=>h.value))};return e.jsxs("div",{className:"space-y-4",children:[l&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:l,children:"← Back"}),e.jsxs(s.Label,{className:"text-sm font-medium",children:["Select values for ",t.label]})]}),e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input,{placeholder:"Search values...",value:c,onChange:h=>d(h.target.value),className:"pl-10"})]}),i||o?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(s.LoaderCircle,{className:"h-6 w-6 animate-spin"})}):e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 pb-2 border-b",children:[e.jsx(s.Checkbox,{checked:n.length===u.length&&u.length>0,onCheckedChange:x}),e.jsxs(s.Label,{className:"text-sm",children:["Select All (",u.length,")"]})]}),e.jsx("div",{className:"max-h-[300px] overflow-y-auto space-y-1",children:u.map(h=>e.jsxs("div",{className:"flex items-center gap-2 p-2 hover:bg-muted/50 rounded cursor-pointer",onClick:()=>m(h.value),children:[e.jsx(s.Checkbox,{checked:n.includes(h.value),onCheckedChange:()=>m(h.value)}),e.jsx(Ze,{className:"text-sm",children:h.label})]},h.id))})]})]})}function Gs({open:t,onOpenChange:n,availableMetrics:a,existingField:l,onSave:c,cardId:d}){const[r,i]=L.useState(""),[o,u]=L.useState("ratio"),[m,x]=L.useState(null),[h,w]=L.useState(null),[j,f]=L.useState(1),[p,g]=L.useState(""),[v,O]=L.useState({format:"number",decimalPlaces:2}),[A,y]=L.useState({}),T=Oe.find(S=>S.id===o),N=a.find(S=>S.id===m),C=a.find(S=>S.id===h);L.useEffect(()=>{if(l&&t)if(i(l.label),l.calculatedFormula)if(u(l.calculatedFormula.operation),l.calculatedFormula.operation==="custom")g(l.expression||"");else if(l.calculatedFormula.operation==="scale"){const S=l.calculatedFormula.inputFields;if(S.length>=1){const D=a.find(F=>F.qualifiedFieldName===S[0].qualifiedFieldName);x((D==null?void 0:D.id)||null)}l.calculatedFormula.constant!==void 0&&f(l.calculatedFormula.constant)}else{const S=l.calculatedFormula.inputFields;if(S.length>=2){const D=a.find(I=>I.qualifiedFieldName===S[0].qualifiedFieldName),F=a.find(I=>I.qualifiedFieldName===S[1].qualifiedFieldName);x((D==null?void 0:D.id)||null),w((F==null?void 0:F.id)||null)}}else u("custom"),g(l.expression||"")},[l,t,a]);const k=()=>{const S={};return r.trim()||(S.name="Name is required"),o==="custom"?p.trim()||(S.customExpression="Custom expression is required"):o==="scale"?(m||(S.inputA="Please select a metric for input A"),(isNaN(j)||j===0)&&(S.constantFactor="Please enter a valid non-zero number"),l&&m===l.id&&(S.circular="Cannot reference the field being edited")):(m||(S.inputA="Please select a metric for input A"),h||(S.inputB="Please select a metric for input B"),m===h&&(S.inputs="Inputs A and B must be different metrics"),l&&(m===l.id||h===l.id)&&(S.circular="Cannot reference the field being edited")),y(S),Object.keys(S).length===0},R=()=>{if(!k())return;const S=Cn(r,o,N||null,C||null,v,d,l==null?void 0:l.id,o==="custom"?p:void 0,o==="scale"?j:void 0);c(S),b()},b=()=>{i(""),u("ratio"),x(null),w(null),f(1),g(""),y({}),n(!1)};return e.jsxs(s.Dialog,{modal:!1,open:t,onOpenChange:n,children:[t&&e.jsx("div",{className:"pointer-events-none fixed inset-0 z-40 bg-background/80 backdrop-blur-sm"}),e.jsxs(s.DialogContent,{className:"max-w-md max-h-[90vh] overflow-y-auto",onInteractOutside:S=>{S.preventDefault()},children:[e.jsxs(s.DialogHeader,{children:[e.jsxs(s.DialogTitle,{children:[l?"Edit":"Create"," Calculated Metric"]}),e.jsx(s.DialogDescription,{children:"Combine existing metrics to create a new calculated field"})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"name",children:"Name"}),e.jsx(s.Input$1,{id:"name",value:r,onChange:S=>i(S.target.value),placeholder:"e.g., Profit Margin",className:A.name?"border-red-500":""}),A.name&&e.jsx("p",{className:"text-xs text-red-500",children:A.name})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"operation",children:"Outcome is"}),e.jsxs(s.Select,{value:o,onValueChange:S=>{u(S),S==="custom"?(x(null),w(null)):o==="custom"&&g("")},children:[e.jsx(s.SelectTrigger,{id:"operation",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:e.jsxs(s.SelectGroup,{children:[e.jsx(s.SelectLabel,{children:"CALCULATED"}),Oe.map(S=>e.jsx(s.SelectItem,{value:S.id,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-mono text-sm",children:S.symbol}),e.jsx("span",{children:S.label})]})},S.id))]})})]}),T&&e.jsx("p",{className:"text-xs text-muted-foreground",children:T.description})]}),o==="custom"?e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"customExpression",children:"SQL Expression"}),e.jsx(s.Textarea,{id:"customExpression",value:p,onChange:S=>g(S.target.value),placeholder:"e.g., CASE WHEN revenue > 1000 THEN revenue - cost ELSE 0 END",className:s.cn("font-mono text-sm",A.customExpression&&"border-red-500"),rows:4}),A.customExpression&&e.jsx("p",{className:"text-xs text-red-500",children:A.customExpression}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Write a SQL expression using column names from your data source"})]}):e.jsxs("div",{className:"rounded-md bg-muted p-3 font-mono text-sm",children:["= ",m&&N?N.label:"A"," ",T==null?void 0:T.symbol," ",h&&C?C.label:"B"]}),o!=="custom"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"inputA",children:"Where"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex h-8 w-8 items-center justify-center rounded bg-muted font-mono text-sm font-medium",children:"A"}),e.jsxs(s.Select,{value:m||"",onValueChange:x,children:[e.jsx(s.SelectTrigger,{id:"inputA",className:A.inputA?"border-red-500":"",children:e.jsx(s.SelectValue,{placeholder:"+ Choose metric"})}),e.jsx(s.SelectContent,{children:a.filter(S=>!s.isCalculatedMetric(S)||S.id!==(l==null?void 0:l.id)).map(S=>e.jsxs(s.SelectItem,{value:S.id,children:[S.label||S.name,S.aggregate&&` (${S.aggregate})`]},S.id))})]})]}),A.inputA&&e.jsx("p",{className:"text-xs text-red-500",children:A.inputA})]}),o!=="custom"&&o!=="scale"&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex h-8 w-8 items-center justify-center rounded bg-muted font-mono text-sm font-medium",children:"B"}),e.jsxs(s.Select,{value:h||"",onValueChange:w,children:[e.jsx(s.SelectTrigger,{id:"inputB",className:A.inputB?"border-red-500":"",children:e.jsx(s.SelectValue,{placeholder:"+ Choose metric"})}),e.jsx(s.SelectContent,{children:a.filter(S=>!s.isCalculatedMetric(S)||S.id!==(l==null?void 0:l.id)).map(S=>e.jsxs(s.SelectItem,{value:S.id,children:[S.label||S.name,S.aggregate&&` (${S.aggregate})`]},S.id))})]})]}),A.inputB&&e.jsx("p",{className:"text-xs text-red-500",children:A.inputB})]}),o==="scale"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"constantFactor",children:"Multiplication Factor"}),e.jsx(s.Input$1,{id:"constantFactor",type:"number",step:"any",value:j,onChange:S=>f(parseFloat(S.target.value)||0),placeholder:"e.g., 0.15",className:A.constantFactor?"border-red-500":""}),A.constantFactor&&e.jsx("p",{className:"text-xs text-red-500",children:A.constantFactor}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Enter the constant value to multiply the metric by (e.g., 0.15 for 15%)"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(s.Label,{htmlFor:"format",children:"Format"}),e.jsxs(s.Select,{value:v.format,onValueChange:S=>O({...v,format:S}),children:[e.jsx(s.SelectTrigger,{id:"format",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsxs(s.SelectItem,{value:"number",children:["Decimal (",v.decimalPlaces,")"]}),e.jsx(s.SelectItem,{value:"currency",children:"Currency"}),e.jsx(s.SelectItem,{value:"percent",children:"Percentage"})]})]})]}),(A.inputs||A.circular)&&e.jsxs(s.Alert,{variant:"destructive",children:[e.jsx(s.CircleAlert,{className:"h-4 w-4"}),e.jsx(s.AlertDescription,{children:A.inputs||A.circular})]})]}),e.jsxs(s.DialogFooter,{children:[e.jsx(s.Button,{variant:"outline",onClick:b,children:"Cancel"}),e.jsxs(s.Button,{onClick:R,children:[l?"Update":"Create"," Metric"]})]})]})]})}function Mn({field:t,zone:n,index:a,sortDirection:l,onSortDirectionChange:c,hasPivotColumns:d=!1,cardType:r}){var ss,ts,as,ns,rs;const{attributes:i,listeners:o,setNodeRef:u,transform:m,transition:x,isDragging:h}=s.useSortable({id:t.id}),w=s.useEditorStore(U=>U.card),{updateLabel:j,handleAggregationChange:f,handleDateFormatChange:p,handleGranularityChange:g,handleAliasTemplateChange:v,handleValueAliasesChange:O,removeField:A,handleKpiComparisonTypeChange:y,handleKpiShowTrendlineChange:T,handleKpiTrendlineWindowChange:N,handleKpiTrendlineGranularityChange:C,handleKpiTargetValueChange:k}=s.useFieldManagement(w),[R,b]=L.useState(!1),[S,D]=L.useState(!1),[F,I]=L.useState(!1),[M,E]=L.useState(null),[B,$]=L.useState(t.dateFormat||"YYYY-MM-DD"),[V,q]=L.useState(t.customFormat||""),[G,z]=L.useState(!!t.customFormat),[H,_]=L.useState(t.granularity||"day"),[P,Y]=L.useState(t.aliasTemplate||""),[Z,se]=L.useState(t.valueAliases||{}),[W,ae]=L.useState((ss=t.parameters)==null?void 0:ss.filters),Q=U=>{U==="custom"?z(!0):(z(!1),$(U),n==="groupBy"&&p(a,U,""))},ce=U=>{q(U),n==="groupBy"&&p(a,"custom",U)},ue=U=>{var le;_(U);const X=(le=s.getDateFormatOptionsForGranularity(U)[0])==null?void 0:le.value;X&&!G&&($(X),n==="groupBy"&&p(a,X,"")),n==="groupBy"&&g(a,U)},K=U=>{Y(U),n==="metrics"&&v(a,U)},ee=U=>{se(U),n==="metrics"&&O(a,U)},de=U=>{j(n,a,U)},me=U=>{var te;if(ae(U),n==="metrics"){const X={...t,parameters:{...t.parameters,filters:U}},le=[...((te=w.config)==null?void 0:te.metricColumns)||[]];le[a]=X,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:le})}D(!1)},Se=()=>{A(n,a)},xe={transform:s.CSS.Transform.toString(m),transition:x},je=s.getDataType(t.dataType),he=je==="date",be=r==="kpi",we=s.getDateFormatOptionsForGranularity(H),ge=n!=="pivotBy",Te=he&&n==="groupBy"&&(!be||((ts=w.config)==null?void 0:ts.comparisonType)==="start_vs_end"),Ee=!be&&he&&n==="groupBy",ke=r&&s.COMPARISON_SUPPORTED_CHART_TYPES.includes(r)&&he&&n==="groupBy",Ie=r==="kpi"&&he&&n==="groupBy",ve=r&&r!=="pivotTable"&&n==="metrics"&&((as=w.config)==null?void 0:as.comparisonType)==="target",ye=n==="metrics",J=()=>{const U=t,te=n==="metrics"&&s.isCalculatedMetric(U);return e.jsxs("div",{className:"space-y-1",children:[!te&&e.jsx("div",{title:`${t.entityName}.${t.name}`,onPointerDown:X=>{X.stopPropagation()},className:"max-w-[270px] cursor-default overflow-hidden text-ellipsis whitespace-nowrap px-0.5 text-xs text-gray-500",children:`${t.entityName}.${t.name}`}),te&&U.calculatedFormula&&e.jsxs("div",{title:ys(U.calculatedFormula),onPointerDown:X=>{X.stopPropagation()},className:"max-w-[270px] cursor-default overflow-hidden text-ellipsis whitespace-nowrap rounded bg-muted/50 px-1.5 py-0.5 font-mono text-xs text-primary",children:["= ",ys(U.calculatedFormula)]})]})},ne=()=>ge?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Label"}),e.jsx(ie,{value:t.label??"",onSave:de,placeholder:"Display label...",type:"text"})]}):null,re=()=>Te?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Granularity"}),e.jsxs(s.Select,{value:H,onValueChange:ue,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select granularity..."})}),e.jsx(s.SelectContent,{children:["day","week","month","quarter","year"].map(U=>e.jsx(s.SelectItem,{className:"text-xs",value:U,children:s.toTitleCase(U)},U))})]})]}):null,Ne=()=>Ee?e.jsxs(e.Fragment,{children:[re(),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Date Format"}),e.jsxs(s.Select,{value:G?"custom":B,onValueChange:Q,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select date format..."})}),e.jsx(s.SelectContent,{children:we.map(U=>e.jsx(s.SelectItem,{className:"text-xs",value:U.value,children:U.label},U.value))})]}),G&&e.jsxs("div",{className:"mt-2 space-y-1",children:[e.jsx(s.Input,{placeholder:"Enter custom format (e.g., YYYY-MM-DD HH:mm)",value:V,onChange:U=>{U.stopPropagation(),ce(U.target.value)},onClick:U=>U.stopPropagation(),className:"h-8 text-xs"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Examples: YYYY-MM-DD, MM/DD/YY, DD-MMM-YYYY HH:mm"})]})]})]}):null,_s=()=>{var X,le,Ce,Ae,fe;const U=ke,te=Ie;return!U&&!te?null:e.jsxs(e.Fragment,{children:[U&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Comparison Type"}),e.jsxs(s.Select,{value:((X=w.config)==null?void 0:X.comparisonType)||"none",onValueChange:y,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select comparison type..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"none",children:"No Comparison"}),e.jsx(s.SelectItem,{value:"previous_period",children:"Previous Period"}),e.jsx(s.SelectItem,{value:"same_period_last_year",children:"Same Period Last Year"}),e.jsx(s.SelectItem,{value:"start_vs_end",children:"Start vs End"})]})]})]}),te&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"space-y-1",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Show Trendline"}),e.jsx(s.SmallSwitch,{checked:((le=w.config)==null?void 0:le.showTrendline)||!1,onCheckedChange:T})]})}),((Ce=w.config)==null?void 0:Ce.showTrendline)&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Trendline Window"}),e.jsx(s.Input,{type:"number",min:"1",max:"365",value:((Ae=w.config)==null?void 0:Ae.trendlineWindow)||30,onChange:Le=>{const Ws=parseInt(Le.target.value)||30;N(Ws)},className:"h-8 text-xs",placeholder:"30"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Trendline Granularity"}),e.jsxs(s.Select,{value:((fe=w.config)==null?void 0:fe.trendlineGranularity)||"day",onValueChange:C,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select granularity..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"day",children:"Day"}),e.jsx(s.SelectItem,{value:"week",children:"Week"}),e.jsx(s.SelectItem,{value:"month",children:"Month"})]})]})]})]})]})]})},Hs=()=>{var U,te;return ve?e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Target Value"}),e.jsx(ie,{value:((U=w.config)==null?void 0:U.targetValue)!==void 0&&((te=w.config)==null?void 0:te.targetValue)!==null?String(w.config.targetValue):"",onSave:X=>{!isNaN(Number(X))&&X!==null?k(Number(X)):k(0)},placeholder:"Enter target value...",type:"text"})]}):null},Js=()=>{if(!ye)return null;const U=t,te=s.isCalculatedMetric(U);return e.jsxs(e.Fragment,{children:[te&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Formula"}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8 w-full justify-start text-xs",onClick:()=>I(!0),children:[e.jsx(s.Pencil,{className:"mr-2 h-3 w-3"}),"Edit Formula"]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Aggregation"}),e.jsxs(s.Select,{value:t.aggregate,onValueChange:X=>{f(a,X)},disabled:te,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-full text-xs",children:e.jsx(s.SelectValue,{placeholder:"Select function..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"COUNT",children:"Count"}),e.jsx(s.SelectItem,{className:"text-xs",value:"DISTINCT",children:"Count Distinct"}),s.getDataType(t.dataType)==="number"&&e.jsxs(e.Fragment,{children:[e.jsx(s.SelectItem,{className:"text-xs",value:"SUM",children:"Sum"}),e.jsx(s.SelectItem,{className:"text-xs",value:"AVG",children:"Average"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MIN",children:"Min"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MAX",children:"Max"}),e.jsx(s.SelectItem,{className:"text-xs",value:"MEDIAN",children:"Median"})]})]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(s.Label,{className:"text-xs font-medium text-muted-foreground",children:"Attribute Filters"}),(W==null?void 0:W.conditions)&&W.conditions.length>0&&e.jsx("div",{className:"mb-2 space-y-1",children:W.conditions.map(X=>e.jsxs("div",{className:"flex cursor-pointer items-center gap-2 rounded bg-muted/50 p-2 text-xs transition-colors hover:bg-muted/70",onClick:()=>{E(X),D(!0)},children:[e.jsxs("span",{className:"font-medium",children:[X.field.label,":"]}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("span",{className:"cursor-help text-muted-foreground",children:Array.isArray(X.value)?X.value.length===1?String(X.value[0]):X.value.length<=2?X.value.join(", "):`${X.value.slice(0,2).join(", ")} +${X.value.length-2}`:String(X.value)})}),Array.isArray(X.value)&&X.value.length>1&&e.jsx(s.TooltipContent,{side:"top",className:"max-w-[300px]",children:e.jsxs("div",{className:"text-xs",children:[e.jsxs("div",{className:"mb-1 font-medium",children:["Selected values (",X.value.length,"):"]}),e.jsx("div",{className:"max-h-[200px] overflow-y-auto",children:X.value.map((le,Ce)=>e.jsx("div",{children:String(le)},Ce))})]})})]})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"ml-auto h-4 w-4 p-0",onClick:le=>{var Ae;le.stopPropagation();const Ce=W.conditions.filter(fe=>fe.id!==X.id);if(Ce.length>0){const fe={...W,conditions:Ce};me(fe)}else if(ae(void 0),n==="metrics"){const fe={...t,parameters:{...t.parameters,filters:void 0}},Le=[...((Ae=w.config)==null?void 0:Ae.metricColumns)||[]];Le[a]=fe,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:Le})}},children:e.jsx(s.X,{className:"h-3 w-3"})})]},X.id))}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-8 w-full justify-start text-xs",onClick:()=>{E(null),D(!0)},children:[e.jsx(s.Filter,{className:"mr-2 h-3 w-3"}),"Add attribute filter"]})]}),e.jsx(An,{column:t}),d&&e.jsxs(e.Fragment,{children:[e.jsx(Sn,{aliasTemplate:P,onAliasTemplateChange:K}),e.jsx(wn,{valueAliases:Z,onValueAliasesChange:ee})]})]})},{columns:Ys=[]}=s.useColumns(),Qs=(W==null?void 0:W.conditions)&&W.conditions.length>0;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{ref:u,style:xe,...i,...o,className:`group mx-auto flex w-full flex-col gap-2 rounded border border-border bg-background p-2 transition-all hover:shadow-sm dark:border-gray-800 ${h?"z-50 scale-95 opacity-50":"border-slate-200"} ${n==="filters"?"cursor-default":"cursor-move"}`,children:[e.jsxs("div",{className:"flex grow items-center gap-2",children:[e.jsx(Xe,{dataType:je}),e.jsx(Ze,{className:s.cn("w-[140px] flex-1 text-sm",Qs&&"w-[120px]"),children:s.getMetricDisplayLabel(t)}),n==="metrics"&&((ns=W==null?void 0:W.conditions)==null?void 0:ns.length)&&!R&&e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs("div",{className:"flex items-center gap-1 rounded bg-primary/10 px-1.5 py-0.5 text-xs",children:[e.jsx(s.Filter,{className:"h-3 w-3"}),e.jsx("span",{children:W.conditions.length})]})}),e.jsx(s.TooltipContent,{side:"top",children:e.jsx("div",{className:"text-xs",children:W.conditions.map(U=>e.jsxs("div",{children:[U.field.label,":"," ",Array.isArray(U.value)?`${U.value.length} values`:String(U.value)]},U.id))})})]})}),n!=="sortBy"&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),b(!R)},children:e.jsx(s.ChevronDown,{className:`h-3 w-3 transition-transform ${R?"rotate-180":""}`})}),n==="sortBy"&&e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),console.log("DraggableField - Current sortDirection:",l);const te=l==="asc"?"desc":"asc";console.log("DraggableField - New direction to set:",te),c(te)},children:l==="asc"?e.jsx(s.ArrowUp,{className:"h-3 w-3"}):e.jsx(s.ArrowDown,{className:"h-3 w-3"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-6 w-6 p-0",onClick:U=>{U.stopPropagation(),Se()},children:e.jsx(s.X,{className:"h-2.5 w-2.5"})})]}),R&&e.jsxs("div",{className:"space-y-3",children:[J(),ne(),r==="kpi"&&re(),Ne(),_s(),Hs(),Js()]})]}),S&&e.jsx(Ks,{open:S,onOpenChange:U=>{D(U),U||E(null)},availableFields:Ys,existingFilters:W,editingCondition:M,onSave:U=>{var te;if(U)me(U);else if(ae(void 0),n==="metrics"){const X={...t,parameters:{...t.parameters,filters:void 0}},le=[...((te=w.config)==null?void 0:te.metricColumns)||[]];le[a]=X,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:le})}E(null)}}),F&&n==="metrics"&&e.jsx(Gs,{open:F,onOpenChange:I,availableMetrics:((rs=w.config)==null?void 0:rs.metricColumns)||[],existingField:t,onSave:U=>{var X;const te=[...((X=w.config)==null?void 0:X.metricColumns)||[]];te[a]=U,s.useEditorStore.getState().actions.updateCardConfig(w.id,{metricColumns:te})},cardId:w.id})]})}function On({concept:t}){const n=()=>{switch(t){case"groupBy":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Rows = Group By"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that define how your data is grouped into rows."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"flex border-b pb-1 dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-100 text-xs font-medium dark:bg-blue-200/20",children:"Product"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"Sales"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"Profit"})]}),e.jsxs("div",{className:"flex border-b dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/50 text-xs font-medium dark:bg-blue-200/20",children:"Electronics"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$5,200"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$1,300"})]}),e.jsxs("div",{className:"flex border-b dark:border-border",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/30 text-xs font-medium dark:bg-blue-300/20",children:"Clothing"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$3,100"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$620"})]}),e.jsxs("div",{className:"flex",children:[e.jsx("div",{className:"w-1/3 bg-blue-200/20 text-xs font-medium dark:bg-blue-200/20",children:"Food"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$2,400"}),e.jsx("div",{className:"w-1/3 text-center text-xs",children:"$480"})]})]})]});case"pivotBy":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Pivot Columns = Pivot By"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that split your data into multiple columns or series."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"flex border-b pb-1 dark:border-border",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Region"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"North"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"South"}),e.jsx("div",{className:"w-1/4 rounded-t bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"East"})]}),e.jsxs("div",{className:"flex border-b py-1 dark:border-border",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Sales"}),e.jsx("div",{className:"w-1/4 bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$2,300"}),e.jsx("div",{className:"w-1/4 bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"$1,800"}),e.jsx("div",{className:"w-1/4 bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$3,400"})]}),e.jsxs("div",{className:"flex py-1",children:[e.jsx("div",{className:"w-1/4 text-xs font-medium",children:"Profit"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$460"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-100 text-center text-xs dark:bg-purple-400/20",children:"$360"}),e.jsx("div",{className:"w-1/4 rounded-b bg-purple-50 text-center text-xs dark:bg-purple-300/20",children:"$680"})]})]})]});case"metrics":return e.jsxs("div",{className:"w-64 space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Metric Values (Cells)"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Fields that represent the values shown in your visualization."}),e.jsxs("div",{className:"rounded-md border bg-white p-2 dark:border-muted dark:bg-muted",children:[e.jsxs("div",{className:"grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"col-span-1"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q1"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q2"}),e.jsx("div",{className:"col-span-1 text-center text-xs font-medium",children:"Q3"})]}),e.jsxs("div",{className:"mt-1 grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"text-xs font-medium",children:"Sales"}),e.jsx("div",{className:"rounded bg-blue-100 p-1 text-center text-xs dark:bg-blue-300/20",children:"$2,300"}),e.jsx("div",{className:"rounded bg-blue-200 p-1 text-center text-xs dark:bg-blue-400/20",children:"$3,100"}),e.jsx("div",{className:"rounded bg-blue-300 p-1 text-center text-xs dark:bg-blue-500/20",children:"$4,200"})]}),e.jsxs("div",{className:"mt-1 grid grid-cols-4 gap-1",children:[e.jsx("div",{className:"text-xs font-medium",children:"Profit"}),e.jsx("div",{className:"rounded bg-green-100 p-1 text-center text-xs dark:bg-green-300/20",children:"$460"}),e.jsx("div",{className:"rounded bg-green-200 p-1 text-center text-xs dark:bg-green-400/20",children:"$620"}),e.jsx("div",{className:"rounded bg-green-300 p-1 text-center text-xs dark:bg-green-500/20",children:"$840"})]})]})]});default:return null}};return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:300,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.CircleHelp,{className:"size-4 text-muted-foreground hover:text-foreground"})}),e.jsx(s.TooltipContent,{side:"right",align:"start",className:"p-3",children:n()})]})})}function Me({zone:t,fields:n,label:a,placeholder:l,shouldShow:c,hasPivotColumns:d=!1,activeField:r=null}){const{isOver:i,setNodeRef:o}=s.useDroppable({id:t}),{updateSortByColumn:u}=s.useEditorActions(),m=s.useEditorStore(p=>p.card),x=s.useEditorStore(p=>p.card.type),h=n.length>0,w=L.useMemo(()=>!r||!i?!0:s.validateFieldForChartType(r,t,x).isValid,[r,i,t,x]),j=(p,g)=>{var O;!((O=m.config)!=null&&O.sortByColumns)||m.config.sortByColumns.findIndex(A=>A.id===p)===-1||u(m.id,p,{direction:g})};if(t==="sortBy"&&c===!1)return null;const f=t!=="sortBy"&&x!=="kpi";return e.jsxs("div",{className:"mb-4 w-full",children:[e.jsxs("div",{className:"mb-2 flex items-center gap-2",children:[e.jsx(s.Label,{className:s.cn("block text-sm font-medium",{}),children:a}),f&&e.jsx(On,{concept:t})]}),e.jsx("div",{ref:o,className:s.cn("rounded-lg border-2 border-dotted p-0.5 transition-all",{"border-green-500 bg-green-50 dark:bg-green-950":i&&w,"border-red-500 bg-red-50 dark:bg-red-950":i&&!w,"border-muted-foreground/30 bg-muted/40":!i&&h,"min-h-[60px] border-muted-foreground/20 bg-background":!i&&!h}),children:n.length===0?e.jsx("div",{className:"flex min-h-[60px] items-center justify-center text-sm text-muted-foreground/60",children:l}):e.jsx(s.SortableContext,{items:n.map(p=>p.id),strategy:s.verticalListSortingStrategy,children:e.jsx("div",{className:"flex flex-wrap gap-1",children:n.map((p,g)=>{const O=p.direction;return e.jsx("div",{className:"flex w-full items-center gap-2",children:e.jsx("div",{className:"flex-1",children:e.jsx(Mn,{field:p,zone:t,index:g,sortDirection:O,onSortDirectionChange:A=>j(p.id,A),hasPivotColumns:d,cardType:x})})},p.id)})})})})]})}function Rn({groupByFields:t,pivotByFields:n,metricsFields:a,sortByFields:l,activeField:c}){const[d,r]=L.useState(!1),i=s.useEditorStore(O=>{var A;return((A=O.card.customCardPreferences)==null?void 0:A.url)&&O.card.type==="custom"}),o=s.useEditorStore(O=>O.card),u=s.useEditorStore(O=>O.actions.updateCardConfig),m=o==null?void 0:o.type,x=t.length>0||a.length>0,h=m!=="kpi"||a.length<=2,w=m!=="kpi",j=m!=="kpi"&&x,f=["pie","doughnut","polar","funnel","aggregateTable","bubble","scatter"].includes(m),p=w&&!f,g=()=>m==="kpi"?{label:"Trend By",placeholder:"Drop fields to trend data"}:{label:"Rows",placeholder:"Drop fields to group data"},v=()=>m==="kpi"?{label:"Metric",placeholder:"Drop field for KPI"}:{label:"Columns",placeholder:"Drop fields to measure"};return e.jsxs(e.Fragment,{children:[e.jsxs(s.Accordion,{type:"single",collapsible:!0,defaultValue:"fields",className:"w-full",children:[e.jsxs(s.AccordionItem,{value:"fields",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.List,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"FIELDS"})]})}),e.jsxs(s.AccordionContent,{className:"space-y-4 pt-2",children:[h&&e.jsx(Me,{zone:"groupBy",fields:t,label:g().label,placeholder:g().placeholder,activeField:c}),p&&e.jsx(Me,{zone:"pivotBy",fields:n,label:"Pivot Columns",placeholder:"Drop fields to pivot data",activeField:c}),e.jsx(Me,{zone:"metrics",fields:a,label:v().label,placeholder:v().placeholder,hasPivotColumns:n.length>0,activeField:c}),a.length>=1&&e.jsxs(s.Button,{variant:"outline",size:"sm",onClick:()=>r(!0),className:"mt-2 w-full justify-start text-xs text-muted-foreground hover:text-foreground",children:[e.jsx(s.Calculator,{className:"mr-2 h-4 w-4"}),"Create Metric"]}),j&&e.jsx(Me,{zone:"sortBy",fields:l,label:"Sort By",placeholder:"Drop fields from Rows or Metrics",activeField:c})]})]}),!i&&e.jsxs(s.AccordionItem,{value:"configuration",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Settings2,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"CONFIGURATION"})]})}),e.jsx(s.AccordionContent,{className:"rounded-md border border-border bg-muted/50 px-3 py-0",children:e.jsx(s.Accordion,{type:"single",collapsible:!0,className:"w-full",children:e.jsx(qs,{mode:"config"})})})]}),i&&e.jsxs(s.AccordionItem,{value:"plugin",className:"border-b-0",children:[e.jsx(s.AccordionTrigger,{className:"flex w-full items-center gap-2 py-2 text-sm font-medium hover:no-underline",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ss,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:"CONFIGURATION"})]})}),e.jsx(s.AccordionContent,{className:"rounded-md border border-border bg-muted/50 px-3 py-0",children:e.jsx(Vs,{})})]})]}),e.jsx(Gs,{open:d,onOpenChange:r,availableMetrics:a,onSave:O=>{u(o.id,{metricColumns:[...a,O]})},cardId:o.id})]})}function Fn({onEditJoins:t}){const n=s.useEditorStore(i=>i.card),{autoJoinMetadata:a}=s.useDashboardCardQuery(n);if(!(a!=null&&a.autoResolved))return null;const{resolvedJoinPlan:l}=a,c=(l==null?void 0:l.joins)||[];if(c.length===0)return null;const d=i=>!i||i.length===0?"No conditions":i.map(o=>{var h,w,j,f;const u=`${(h=o.source)==null?void 0:h.entityName}.${(w=o.source)==null?void 0:w.name}`,m=`${(j=o.target)==null?void 0:j.entityName}.${(f=o.target)==null?void 0:f.name}`,x=o.operator||"=";return`${u} ${x} ${m}`}).join(" AND "),r=i=>!i.joinKeyGroups||i.joinKeyGroups.length===0?"No conditions defined":i.joinKeyGroups.length===1?d(i.joinKeyGroups[0].keys):i.joinKeyGroups.map((o,u)=>({id:u,operator:o.operator,condition:d(o.keys)}));return e.jsx(s.Card,{className:"border-blue-500/30 bg-blue-50/30 dark:bg-blue-950/20",children:e.jsxs("div",{className:"flex items-center justify-between p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(St,{className:"size-4 text-blue-600 dark:text-blue-400"}),e.jsx("h3",{className:"text-sm font-medium text-blue-900 dark:text-blue-100",children:"Auto-Resolved Joins"}),e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx(s.Badge,{variant:"secondary",className:"cursor-help bg-blue-100 text-xs text-blue-700 dark:bg-blue-900 dark:text-blue-300",children:c.length})}),e.jsx(s.TooltipContent,{side:"bottom",align:"start",className:"max-w-md p-3",children:e.jsx("div",{className:"space-y-2",children:c.map((i,o)=>{const u=r(i),m=Array.isArray(u);return e.jsxs("div",{className:"rounded-md border border-border bg-background p-2",children:[e.jsxs("div",{className:"mb-1 flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs",children:[e.jsx("span",{className:"font-medium text-foreground",children:i.source.name}),e.jsx(s.ArrowRight,{className:"size-3 text-muted-foreground"}),e.jsx("span",{className:"font-medium text-foreground",children:i.target.name})]}),e.jsx(s.Badge,{variant:"outline",className:"border-green-500/40 bg-green-50/30 text-xs text-green-700 dark:bg-green-950/30 dark:text-green-300",children:i.joinType})]}),e.jsx("div",{className:"mt-1 space-y-1",children:m?u.map(x=>e.jsxs("div",{className:"rounded bg-muted/30 px-2 py-1",children:[e.jsx("code",{className:"block break-all text-xs text-muted-foreground",children:x.condition}),x.operator&&e.jsxs("span",{className:"ml-2 text-xs text-muted-foreground",children:["(",x.operator,")"]})]},x.id)):e.jsx("div",{className:"rounded bg-muted/30 px-2 py-1",children:e.jsx("code",{className:"block break-all text-xs text-muted-foreground",children:u})})})]},i.id||o)})})})]})})]}),t&&e.jsx(s.Button,{variant:"ghost",size:"sm",onClick:t,className:"h-7 gap-1 p-2 text-xs text-blue-700 hover:text-blue-900 dark:text-blue-300 dark:hover:text-blue-100",children:e.jsx(s.SquarePen,{className:"size-3.5"})})]})})}const Ns={getItem(t){if(typeof window>"u")return null;try{return localStorage.getItem(t)}catch(n){return console.warn(`Failed to read from localStorage (${t}):`,n),null}},setItem(t,n){if(!(typeof window>"u"))try{localStorage.setItem(t,n)}catch(a){console.warn(`Failed to write to localStorage (${t}):`,a)}}};function Bn({selectedChart:t,onChartChange:n,groupByFields:a,pivotByFields:l,metricsFields:c,sortByFields:d,activeField:r}){var N,C;const[i,o]=L.useState(()=>Ns.getItem("chart-selector-expanded")==="true"),[u,m]=L.useState(!1),x=s.useEditorStore(k=>k.card),h=s.useEditorStore(k=>k.actions.updateCardConfig),w=a.length>0||l.length>0||c.length>0,{data:j,autoJoinMetadata:f}=s.useDashboardCardQuery(x),p=((N=x==null?void 0:x.config)==null?void 0:N.joinPlan)||(f!=null&&f.autoResolved?f.resolvedJoinPlan:{baseEntity:{type:"table",name:"",database:"",schema:"",connectionId:"",connectionType:"none",dialect:"none"},joins:[]}),g=((C=x==null?void 0:x.dataSource)==null?void 0:C.selectedEntities)||[],v=k=>{h(x.id,{...x==null?void 0:x.config,joinPlan:k}),m(!1)},O=()=>{h(x.id,{...x==null?void 0:x.config,joinPlan:void 0}),m(!1)},A=()=>{m(!1)},y=()=>{m(!0)};L.useEffect(()=>{Ns.setItem("chart-selector-expanded",String(i))},[i]);const T=()=>{o(k=>!k)};return e.jsxs("div",{"data-content":"Chart Configuration Panel",className:"flex w-[350px] flex-col border-x border-border",children:[e.jsxs("div",{className:s.cn("px-6 pt-4",{"space-y-4":i}),children:[e.jsx("div",{className:s.cn("overflow-hidden transition-all duration-300 ease-in-out",i?"max-h-[200px] opacity-100":"max-h-0 opacity-0"),children:i&&e.jsx(yn,{selectedChart:t,onChartChange:n})}),e.jsx("div",{"data-content":"Chart Quick Selector",className:"flex items-center justify-center",children:e.jsx(s.ChartQuickSelector,{selectedChart:t,onChartChange:n,isDropdownExpanded:i,onToggleDropdown:T})})]}),e.jsx("div",{className:s.cn("flex justify-end gap-2 px-6 py-6"),children:e.jsx("div",{className:s.cn("invisible flex gap-2",{visible:w||j}),children:e.jsx(Re.EditorClearButton,{})})}),e.jsxs(s.ScrollArea,{className:"w-full flex-1 basis-0 px-6 pb-6",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx(Fn,{onEditJoins:y}),e.jsx(Rn,{groupByFields:a,pivotByFields:l,metricsFields:c,sortByFields:d,activeField:r})]}),e.jsx(s.ScrollBar,{orientation:"horizontal"})]}),e.jsx(Is,{open:u,onOpenChange:m,joinPlan:p,availableDataSources:g,onSave:v,onCancel:A,onRemove:O,children:e.jsx("div",{})})]})}function Pn({initialFilters:t,availableFields:n}={}){const[a,l]=L.useState(t||{id:`group-${Date.now()}`,logicalOperator:"AND",negate:!1,conditions:[],groups:[]});s.useEditorStore(y=>y.card.dataSource);const{columns:c,isLoading:d}=s.useColumns(),i=n||c||[],o={id:"default",qualifiedFieldName:"default",name:"default",dataType:"string",label:"Default",entityId:"default",entityName:"Default",entityType:"table"},u=y=>{const T=N=>{if(N.id===y){const C=i[0]||o,k=s.getDataType(C.dataType)==="date";return{...N,conditions:[...N.conditions,{id:`condition-${Date.now()}`,field:C,operator:k?"between":"=",value:k?[]:""}]}}return{...N,groups:N.groups.map(T)}};l(T(a))},m=y=>{const T=N=>{if(N.id===y){const C=i[0]||o,k=s.getDataType(C.dataType)==="date";return{...N,groups:[...N.groups,{id:`group-${Date.now()}`,logicalOperator:"AND",negate:!1,conditions:[{id:`condition-${Date.now()}`,field:C,operator:k?"between":"=",value:k?[]:""}],groups:[]}]}}return{...N,groups:N.groups.map(T)}};l(T(a))},x=(y,T)=>{const N=C=>C.id===y?{...C,conditions:C.conditions.filter(k=>k.id!==T)}:{...C,groups:C.groups.map(N)};l(N(a))},h=(y,T)=>{const N=C=>C.id===y?{...C,groups:C.groups.filter(k=>k.id!==T)}:{...C,groups:C.groups.map(N)};l(N(a))},w=(y,T,N,C)=>{if(N==="field")throw new Error("Use a dedicated method to update the field property with a Field object");const k=R=>R.id===y?{...R,conditions:R.conditions.map(b=>b.id===T?{...b,[N]:C}:b)}:{...R,groups:R.groups.map(k)};l(k(a))},j=(y,T,N)=>{const C=k=>k.id===y?{...k,conditions:k.conditions.map(R=>R.id===T?{...R,...N}:R)}:{...k,groups:k.groups.map(C)};l(C(a))},f=(y,T)=>{const N=C=>C.id===y?{...C,logicalOperator:T}:{...C,groups:C.groups.map(N)};l(N(a))},p=(y,T)=>{const N=C=>C.id===y?{...C,negate:T}:{...C,groups:C.groups.map(N)};l(N(a))},g=(y,T,N)=>{const C=k=>k.id===y?{...k,conditions:s.arrayMove(k.conditions,T,N)}:{...k,groups:k.groups.map(C)};l(C(a))},v=(y,T,N)=>{const C=k=>k.id===y?{...k,groups:s.arrayMove(k.groups,T,N)}:{...k,groups:k.groups.map(C)};l(C(a))},O=y=>{if(!y||y.conditions.length===0&&y.groups.length===0)return"-- No filters defined";const T=C=>{let k="";typeof C.field=="object"?k=C.field.entityName?`${C.field.entityName}.${C.field.name}`:C.field.name:k=String(C.field);const R=C.operator;let b=C.value;if(C.field.dataType==="string"&&!["in","not in"].includes(C.operator))typeof b=="string"?b=`'${b}'`:Array.isArray(b)&&(b=`'${b.join(", ")}'`);else if(C.field.dataType==="date"||C.field.dataType==="datetime"||C.field.dataType==="timestamp")if(C.operator==="between"&&Array.isArray(b)&&b.length===2){const S=b[0]?`'${b[0]}'`:"NULL",D=b[1]?`'${b[1]}'`:"NULL";return`${k} BETWEEN ${S} AND ${D}`}else if((C.operator==="in"||C.operator==="not in")&&Array.isArray(b)){const S=b.map(D=>D?`'${D}'`:"NULL").join(", ");return`${k} ${R} (${S})`}else typeof b=="string"||b instanceof Date?b=`'${b}'`:Array.isArray(b)&&b.length>0&&(b=`'${b[0]}'`);else if(["in","not in"].includes(C.operator)){let S=[];Array.isArray(b)?S=b.map(D=>typeof D=="string"?`'${D}'`:String(D)):typeof b=="string"?S=b.split(",").map(D=>{const F=D.trim();return C.field.dataType==="string"?`'${F}'`:F}):typeof b=="number"&&(S=[String(b)]),b=`(${S.join(", ")})`}switch(R){case"contains":return typeof b=="string"?`${k} LIKE '%${b.replace(/'/g,"")}'`:`${k} LIKE '%${String(b)}'`;case"startsWith":return typeof b=="string"?`${k} LIKE '${b.replace(/'/g,"")}%'`:`${k} LIKE '${String(b)}%'`;case"endsWith":return typeof b=="string"?`${k} LIKE '%${b.replace(/'/g,"")}'`:`${k} LIKE '%${String(b)}'`;case"isNull":return`${k} IS NULL`;case"isNotNull":return`${k} IS NOT NULL`;default:return`${k} ${R} ${b}`}},N=C=>{const k=C.conditions.map(T),R=C.groups.map(F=>N(F)),b=[...k,...R].filter(Boolean);if(b.length===0)return"";const S=b.join(` ${C.logicalOperator} `),D=b.length>1?`(${S})`:S;return C.negate?`NOT ${D}`:D};return N(y)},A=a.conditions.length>0||a.groups.length>0;return{currentFilters:a,setCurrentFilters:l,availableFields:i,isLoading:d,addCondition:u,addGroup:m,removeCondition:x,removeGroup:h,updateCondition:w,updateConditionFields:j,updateGroupOperator:f,updateGroupNegate:p,reorderConditions:g,reorderGroups:v,generateSQLPreview:O,hasAnyFilters:A}}function Vn({children:t,className:n,delayDuration:a}){return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:a,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsx("div",{className:s.cn("w-[200px] overflow-hidden text-ellipsis whitespace-nowrap text-sm",n),children:t})}),e.jsx(s.PortalTooltipContent,{children:t})]})})}const zn=t=>{if(t.qualifiedEntityName){const n=t.qualifiedEntityName.split(".");return n.length>1?n[0]:"default"}return"default"};function $n({value:t,onValueChange:n,availableFields:a,placeholder:l="Select field",className:c="w-64"}){const[d,r]=L.useState(!1),i=L.useMemo(()=>{const o={};return a.forEach(u=>{const m=zn(u);o[m]||(o[m]={}),o[m][u.entityId]||(o[m][u.entityId]=[]),o[m][u.entityId].push(u)}),o},[a]);return e.jsxs(s.Popover,{modal:!0,open:d,onOpenChange:r,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":d,className:s.cn("justify-between",!t&&"text-muted-foreground",c),children:[t?e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs(Vn,{children:[t.entityName,".",t.name]})}):l,e.jsx(s.ChevronsUpDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"z-50 w-80 p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search fields...",className:"h-9"}),e.jsx(s.CommandEmpty,{children:"No fields found."}),e.jsx(s.CommandList,{className:"max-h-80",children:Object.entries(i).map(([o,u])=>e.jsx(s.CommandGroup,{heading:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(s.Database,{className:"h-3 w-3"}),o]}),children:Object.entries(u).map(([m,x])=>e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 bg-gray-50/50 px-2 py-1 text-xs font-medium text-gray-600",children:[s.getEntityIcon(x[0].entityType,{size:"h-3 w-3"}),e.jsx("span",{children:x[0].entityName})]}),x.map((h,w)=>e.jsxs(s.CommandItem,{value:h.id,onSelect:()=>{n(h),r(!1)},className:"overflow-x-auto",children:[e.jsx(s.Check,{className:s.cn("mr-2 h-4 w-4",(t==null?void 0:t.id)===h.id?"opacity-100":"opacity-0")}),e.jsxs("div",{className:"flex min-w-0 flex-1 flex-col",children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"",children:h.name})}),e.jsx("div",{className:"flex items-center gap-2 text-xs text-gray-500",children:h.description&&e.jsx("span",{children:h.description})})]})]},h.id+w))]},`${o}-${m}`))},o))})]})})]})}function es(t){var f;const n=s.useEditorStore(p=>p.card),{authToken:a,tokenProps:l}=s.useSemaphorContext(),c=s.useEditorStore(p=>p.card.config),d=c==null?void 0:c.joinPlan,r={...t,alias:t.name,label:"value",role:"groupby"},i={...t,role:"metric",label:"count",alias:"count",aggregate:"COUNT"},o={...r,role:"sortby",direction:"asc"},u={joinPlan:d,groupByColumns:[r],metricColumns:[i],sortByColumns:[o]},m=((f=n.dataSource)==null?void 0:f.connectionId)||n.connectionId,{data:x,isPending:h,error:w}=s.useQuery({queryKey:["filterValues",t.entityId,t.name,t.qualifiedEntityName],queryFn:async()=>{try{return await s.postRequestRegular(`${l.apiServiceUrl}/v1/query`,(a==null?void 0:a.accessToken)||"",{connection_id:m,cardConfig:u,cardDataSource:n.dataSource})}catch(p){console.log(t),console.error("error",p)}},enabled:!!m,refetchOnMount:!0,refetchOnWindowFocus:!1});return{filterValues:((x==null?void 0:x.records)||[]).map(p=>({label:p.value||"BLANK",value:p.value||"BLANK",count:(p==null?void 0:p.count)||0})),isPending:h,error:w}}function qn({field:t,selected:n,onChange:a,placeholder:l="Select items...",className:c}){const[d,r]=_e.useState(!1),{filterValues:i}=es(t),o=m=>{a(n.filter(x=>x!==m))},u=m=>{n.includes(m)?a(n.filter(x=>x!==m)):a([...n,m])};return e.jsx("div",{className:s.cn("w-full",c),children:e.jsxs(s.Popover,{open:d,onOpenChange:r,modal:!0,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"outline",role:"combobox","aria-expanded":d,className:"h-auto min-h-10 w-full justify-between p-2",children:[e.jsx("div",{className:"flex flex-1 flex-wrap gap-1",children:n.length>0?n.map(m=>{const x=i.find(h=>h.value===m);return e.jsxs(s.Badge,{variant:"secondary",className:"text-xs",children:[x==null?void 0:x.label,e.jsx("div",{className:"ml-1 rounded-full outline-none ring-offset-background focus:ring-2 focus:ring-ring focus:ring-offset-2",onKeyDown:h=>{h.key==="Enter"&&o(m)},onMouseDown:h=>{h.preventDefault(),h.stopPropagation()},onClick:h=>{h.stopPropagation(),o(m)},children:e.jsx(s.X,{className:"h-3 w-3 text-muted-foreground hover:text-foreground"})})]},m)}):e.jsx("span",{className:"text-muted-foreground",children:l})}),e.jsx(s.ChevronDown,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(s.PopoverContent,{className:"z-50 w-fit overflow-auto p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No results found."}),e.jsx(s.CommandGroup,{children:i.map((m,x)=>e.jsxs(s.CommandItem,{onSelect:()=>u(m.value),children:[e.jsx(s.Check,{className:s.cn("mr-2 h-4 w-4",n.includes(m.value)?"opacity-100":"opacity-0")}),e.jsxs("div",{className:"flex grow items-center justify-between gap-4",children:[e.jsx("span",{children:m.label}),e.jsx("span",{className:"text-xs text-muted-foreground",children:m.count})]})]},m.value+x))})]})]})})]})})}function Kn({field:t,value:n,onChange:a,placeholder:l="Select value...",className:c}){const{filterValues:d,isPending:r,error:i}=es(t);return r?e.jsxs("div",{className:"flex flex-1 items-center justify-center p-2 text-xs text-gray-500",children:[e.jsx(s.LoaderCircle,{className:"mr-2 size-3.5 animate-spin"})," Loading values..."]}):i?e.jsx("div",{className:"flex-1 p-2 text-xs text-red-500",children:"Error loading values"}):e.jsxs(s.Select,{value:n,onValueChange:a,children:[e.jsx(s.SelectTrigger,{className:c||"flex-1",children:e.jsx(s.SelectValue,{placeholder:l,children:n})}),e.jsx(s.SelectContent,{children:d.map((o,u)=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:o.value,children:o.label},o.value+u))})]})}function Gn({value:t,operator:n,onChange:a,className:l}){var o;const{tokenProps:c}=s.useSemaphorContext(),d=(o=c==null?void 0:c.params)==null?void 0:o.timezone,r=L.useMemo(()=>{if(t){if(n==="between"||n==="not between"){if(Array.isArray(t)&&t.length===2){const u=t[0]?new Date(t[0]):void 0,m=t[1]?new Date(t[1]):void 0;if(u&&m)return{from:u,to:m}}}else if(n==="="||n==="!="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:u,to:u}}}else if(n===">"||n===">="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:u,to:void 0}}}else if(n==="<"||n==="<="){if(!Array.isArray(t)&&t){const u=new Date(t);if(!isNaN(u.getTime()))return{from:void 0,to:u}}}else if((n==="in"||n==="not in")&&Array.isArray(t)&&t.length>0){const u=new Date(t[0]),m=t.length>1?new Date(t[t.length-1]):u;if(!isNaN(u.getTime()))return{from:u,to:m}}}},[t,n]),i=u=>{if(!u){a(void 0);return}if(u.from&&u.to)a([u.from.toISOString(),u.to.toISOString()]);else if(u.from){const m=new Date(u.from);m.setHours(23,59,59,999),a([u.from.toISOString(),m.toISOString()])}else if(u.to){const m=new Date(u.to);m.setHours(0,0,0,0),a([m.toISOString(),u.to.toISOString()])}};return e.jsx(s.DateRangePicker,{value:r,onChange:i,timezone:d,showRelativeTab:!0,className:l,placeholder:"Select date..."})}const Un=["=","!=",">",">=","<","<=","contains","startsWith","endsWith","in","not in","isNull","isNotNull"],_n=["=","!=","contains","startsWith","endsWith","in","not in","isNull","isNotNull"],Hn=["=","!=",">",">=","<","<=","in","not in","isNull","isNotNull"],Jn=["=","!=","isNull","isNotNull"],Yn=["between","isNull","isNotNull"],Qn={"=":"Equals","!=":"Not Equals",">":"Greater Than",">=":"Greater Than or Equal","<":"Less Than","<=":"Less Than or Equal",contains:"Contains",startsWith:"Starts With",endsWith:"Ends With",in:"In","not in":"Not In",between:"Between",isNull:"Is Null",isNotNull:"Is Not Null","not between":"Not Between"};function Cs(t){return Qn[t]||s.titleCase(t)}function Wn(t){switch(t){case"string":return _n;case"number":return Hn;case"boolean":return Jn;case"date":return Yn;default:return Un}}function Us({condition:t,index:n,groupId:a,groupLogicalOperator:l,onUpdateCondition:c,onRemoveCondition:d,onUpdateGroupOperator:r,availableFields:i,updateConditionFields:o}){const{attributes:u,listeners:m,setNodeRef:x,transform:h,transition:w,isDragging:j}=s.useSortable({id:t.id}),{isPending:f,error:p}=es(t.field),g={transform:s.CSS.Transform.toString(h),transition:w};return e.jsxs("div",{ref:x,style:g,className:`mb-2 flex items-center gap-2 rounded p-2 hover:bg-muted/50 ${j?"z-50 opacity-50":""}`,children:[e.jsx("div",{...u,...m,className:"cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-4 w-4 flex-shrink-0 text-gray-400"})}),n>0&&e.jsxs(s.Select,{value:l,onValueChange:v=>r(a,v),children:[e.jsx(s.SelectTrigger,{className:"w-24",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"AND",children:"AND"}),e.jsx(s.SelectItem,{value:"OR",children:"OR"})]})]}),n===0&&e.jsx("div",{className:"w-24"}),e.jsx($n,{value:t.field,onValueChange:v=>{const O=v,A=s.getDataType(O.dataType)==="date";o(a,t.id,{field:O,value:"",operator:A?"between":"="})},availableFields:i,placeholder:"Select field",className:"w-64"}),e.jsxs(s.Select,{value:t.operator,onValueChange:v=>{v==="in"||v==="not in"?o(a,t.id,{operator:v,value:[]}):v==="isNull"||v==="isNotNull"?o(a,t.id,{operator:v,value:null}):o(a,t.id,{operator:v,value:""})},children:[e.jsx(s.SelectTrigger,{className:"w-32",children:e.jsx(s.SelectValue,{children:t.operator?Cs(t.operator):""})}),e.jsx(s.SelectContent,{children:Wn(s.getDataType(t.field.dataType)).map(v=>e.jsx(s.SelectItem,{className:"hover:bg-muted",value:v,children:Cs(v)},v))})]}),t.operator==="isNull"||t.operator==="isNotNull"?e.jsx("div",{className:"flex-1"}):s.getDataType(t.field.dataType)==="string"?t.operator==="in"||t.operator==="not in"?f?e.jsxs("div",{className:"flex flex-1 items-center justify-center p-2 text-xs text-gray-500",children:[e.jsx(s.LoaderCircle,{className:"mr-2 size-3.5 animate-spin"})," Loading values..."]}):p?e.jsx("div",{className:"flex-1 p-2 text-xs text-red-500",children:"Error loading values"}):e.jsx(qn,{field:t.field,selected:Array.isArray(t.value)&&t.value.every(v=>typeof v=="string")?t.value:[],onChange:v=>o(a,t.id,{value:v}),placeholder:`Select values for ${t.operator}...`,className:"flex-1"}):["contains","startsWith","endsWith"].includes(t.operator)?e.jsx(s.Input,{value:typeof t.value=="string"?t.value:"",onChange:v=>o(a,t.id,{value:v.target.value}),className:"flex-1",placeholder:t.operator.charAt(0).toUpperCase()+t.operator.slice(1)+"..."}):e.jsx(Kn,{field:t.field,value:typeof t.value=="string"?t.value:"",onChange:v=>o(a,t.id,{value:v}),placeholder:"Select value...",className:"flex-1"}):s.getDataType(t.field.dataType)==="boolean"?e.jsxs(s.Select,{value:typeof t.value=="boolean"?String(t.value):"",onValueChange:v=>o(a,t.id,{value:v==="true"}),children:[e.jsx(s.SelectTrigger,{className:"flex-1",children:e.jsx(s.SelectValue,{placeholder:"Select value..."})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{value:"true",children:"True"}),e.jsx(s.SelectItem,{value:"false",children:"False"})]})]}):s.getDataType(t.field.dataType)==="date"?e.jsx(Gn,{value:t.value,operator:t.operator,onChange:v=>o(a,t.id,{value:v}),className:"flex-1"}):e.jsx(s.Input,{value:typeof t.value=="string"||typeof t.value=="number"?t.value:"",onChange:v=>c(a,t.id,"value",v.target.value),className:"flex-1",placeholder:t.operator==="in"||t.operator==="not in"?"value1, value2, value3":"Value"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 flex-shrink-0 p-0",onClick:()=>d(a,t.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]})}function Xn({group:t,depth:n,parentGroupId:a,onUpdateGroupOperator:l,onUpdateGroupNegate:c,onRemoveGroup:d,onUpdateCondition:r,onUpdateField:i,onRemoveCondition:o,onReorderConditions:u,onReorderGroups:m,onAddCondition:x,onAddGroup:h,availableFields:w,renderFilterGroup:j,updateConditionFields:f}){const{attributes:p,listeners:g,setNodeRef:v,transform:O,transition:A,isDragging:y}=s.useSortable({id:t.id}),T={transform:s.CSS.Transform.toString(O),transition:A},N=k=>{const{active:R,over:b}=k;if(b&&R.id!==b.id){const S=t.conditions.findIndex(F=>F.id===R.id),D=t.conditions.findIndex(F=>F.id===b.id);S!==-1&&D!==-1&&u(t.id,S,D)}},C=k=>{const{active:R,over:b}=k;if(b&&R.id!==b.id&&a){const S=t.groups.findIndex(F=>F.id===R.id),D=t.groups.findIndex(F=>F.id===b.id);S!==-1&&D!==-1&&m(a,S,D)}};return e.jsxs("div",{ref:v,style:T,className:`rounded-lg ${n>0?"mt-2 border border-border":""} ${y?"z-50 opacity-50":""}`,children:[n>0&&e.jsxs("div",{className:"flex items-center justify-between rounded-t-lg bg-muted p-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{...p,...g,className:"cursor-grab active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-4 w-4 text-muted-foreground"})}),e.jsx(s.Button,{variant:t.negate?"default":"outline",size:"sm",className:"h-7 px-2 text-xs",onClick:()=>c(t.id,!t.negate),children:"NOT"}),e.jsxs(s.Select,{value:t.logicalOperator,onValueChange:k=>l(t.id,k),children:[e.jsx(s.SelectTrigger,{className:"h-7 w-20",children:e.jsx(s.SelectValue,{})}),e.jsxs(s.SelectContent,{children:[e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"AND",children:"AND"}),e.jsx(s.SelectItem,{className:"hover:bg-muted",value:"OR",children:"OR"})]})]}),e.jsx("span",{className:"text-sm font-medium",children:"Group"})]}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>a&&d(a,t.id),children:e.jsx(s.Trash2,{className:"h-3 w-3"})})]}),e.jsxs("div",{className:`p-3 ${n>0?"":"px-0"}`,children:[e.jsx(s.DndContext,{sensors:s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),collisionDetection:s.closestCenter,onDragEnd:N,children:e.jsx(s.SortableContext,{items:t.conditions.map(k=>k.id),strategy:s.verticalListSortingStrategy,children:t.conditions.map((k,R)=>e.jsx(Us,{condition:k,index:R,groupId:t.id,groupLogicalOperator:t.logicalOperator,onUpdateCondition:r,onUpdateField:i,onRemoveCondition:o,onUpdateGroupOperator:l,availableFields:w,updateConditionFields:f},k.id))})}),e.jsx(s.DndContext,{sensors:s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),collisionDetection:s.closestCenter,onDragEnd:C,children:e.jsx(s.SortableContext,{items:t.groups.map(k=>k.id),strategy:s.verticalListSortingStrategy,children:t.groups.map(k=>j(k,n+1,t.id))})}),e.jsxs("div",{className:"mt-3 flex items-center gap-2",children:[e.jsxs(s.Button,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:()=>x(t.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Condition"]}),e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>h(t.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Group"]})]})]})]})}function Zn({filters:t,availableFields:n,onSave:a,onClearAll:l,onCancel:c}){const{currentFilters:d,availableFields:r,isLoading:i,addCondition:o,addGroup:u,removeCondition:m,removeGroup:x,updateCondition:h,updateConditionFields:w,updateGroupOperator:j,updateGroupNegate:f,reorderConditions:p,reorderGroups:g,generateSQLPreview:v,hasAnyFilters:O,setCurrentFilters:A}=Pn({initialFilters:t,availableFields:n}),y=s.useSensors(s.useSensor(s.PointerSensor),s.useSensor(s.KeyboardSensor,{coordinateGetter:s.sortableKeyboardCoordinates})),[T,N]=L.useState(!1),C=(b,S,D)=>{const F=I=>I.id===b?{...I,conditions:I.conditions.map(M=>M.id===S?{...M,field:D,dataType:D.dataType}:M)}:{...I,groups:I.groups.map(F)};A(F(d))},k=(b,S=0,D)=>S===0?e.jsx("div",{className:"rounded-lg",children:e.jsxs("div",{className:"p-3 px-0",children:[e.jsx(s.DndContext,{sensors:y,collisionDetection:s.closestCenter,onDragEnd:F=>{const{active:I,over:M}=F;if(M&&I.id!==M.id){const E=b.conditions.findIndex($=>$.id===I.id),B=b.conditions.findIndex($=>$.id===M.id);E!==-1&&B!==-1&&p(b.id,E,B)}},children:e.jsx(s.SortableContext,{items:b.conditions.map(F=>F.id),strategy:s.verticalListSortingStrategy,children:b.conditions.map((F,I)=>e.jsx(Us,{condition:F,index:I,groupId:b.id,groupLogicalOperator:b.logicalOperator,onUpdateCondition:h,onUpdateField:C,onRemoveCondition:m,onUpdateGroupOperator:j,availableFields:r,updateConditionFields:w},F.id))})}),e.jsx(s.DndContext,{sensors:y,collisionDetection:s.closestCenter,onDragEnd:F=>{const{active:I,over:M}=F;if(M&&I.id!==M.id){const E=b.groups.findIndex($=>$.id===I.id),B=b.groups.findIndex($=>$.id===M.id);E!==-1&&B!==-1&&g(b.id,E,B)}},children:e.jsx(s.SortableContext,{items:b.groups.map(F=>F.id),strategy:s.verticalListSortingStrategy,children:b.groups.map(F=>k(F,S+1,b.id))})}),e.jsxs("div",{className:"mt-3 flex items-center gap-2",children:[e.jsxs(s.Button,{size:"sm",className:"h-7 text-xs",variant:"outline",onClick:()=>o(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Condition"]}),T&&e.jsxs(s.Button,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>u(b.id),children:[e.jsx(s.Plus,{className:"mr-1 h-3 w-3"}),"Add Group"]})]})]})},b.id):e.jsx(Xn,{group:b,depth:S,parentGroupId:D,onUpdateGroupOperator:j,onUpdateGroupNegate:f,onRemoveGroup:x,onUpdateCondition:h,onUpdateField:C,onRemoveCondition:m,onReorderConditions:p,onReorderGroups:g,onAddCondition:o,onAddGroup:u,availableFields:r,renderFilterGroup:k,updateConditionFields:w},b.id),R=s.getFilterCount(t);return e.jsxs("div",{className:"space-y-4",children:[e.jsx(s.Card,{children:e.jsx(s.CardContent,{className:"p-4",children:e.jsxs("div",{className:"space-y-4",children:[i?e.jsxs("div",{className:"flex items-center justify-center p-4",children:[e.jsx("div",{className:"h-5 w-5 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600"}),e.jsx("span",{className:"ml-2 text-sm text-gray-600",children:"Loading columns..."})]}):k(d),e.jsx("div",{className:"mt-4 border-t border-border pt-4",children:e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(s.SmallSwitch,{id:"advanced-mode",checked:T,onCheckedChange:b=>N(b)}),e.jsx(s.Label,{htmlFor:"advanced-mode",className:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",children:"Advanced mode (enable grouping)"})]})}),O&&e.jsxs("div",{className:"mt-4 border-t border-border pt-4",children:[e.jsx("div",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"SQL WHERE Clause Preview"}),e.jsx("div",{className:"overflow-x-auto rounded-md border bg-muted p-3",children:e.jsx("pre",{className:"whitespace-pre-wrap font-mono text-xs text-muted-foreground",children:v(d)})})]})]})})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(s.Button,{size:"sm",variant:"outline",onClick:c,children:"Cancel"}),R>0&&e.jsx(s.Button,{variant:"outline",size:"sm",onClick:l,children:"Clear All"}),e.jsx(s.Button,{size:"sm",onClick:()=>a(O?d:null),children:"Apply Filters"})]})]})}function er({cardId:t,filters:n,onFiltersChange:a,onClearFilters:l,availableFields:c,isOpen:d,onOpenChange:r}){const i=u=>{a(t,u),r(!1)},o=()=>{l(t),r(!1)};return e.jsxs(s.Dialog,{modal:!1,open:d,onOpenChange:r,children:[d&&e.jsx("div",{className:"pointer-events-none fixed inset-0 z-40 bg-background/80 backdrop-blur-sm"}),e.jsxs(s.DialogContent,{className:"max-h-[80vh] max-w-4xl overflow-y-auto",onInteractOutside:u=>{u.preventDefault()},children:[e.jsxs(s.DialogHeader,{children:[e.jsx(s.DialogTitle,{children:"Card Filters"}),e.jsx(s.DialogDescription,{children:"Add filters to the card to limit the data that is displayed."})]}),e.jsx(Zn,{filters:n,availableFields:c,onSave:i,onCancel:()=>r(!1),onClearAll:o})]})]})}function sr({activeCardId:t,sourceFields:n,getFieldsForContainer:a,setFieldsForContainer:l}){var p,g;const[c,d]=L.useState(null),r=s.useEditorStore(v=>v.card),i=s.useEditorStore(v=>v.actions.updateCardConfig),o=(p=r==null?void 0:r.config)==null?void 0:p.joinPlan,u=(g=r==null?void 0:r.dataSource)==null?void 0:g.semanticDomainId,{data:m}=s.useJoinabilityMap(u),x=[...n,...a("groupBy"),...a("pivotBy"),...a("metrics"),...a("sortBy")],h=L.useCallback(v=>v.startsWith("card-")?null:v.startsWith("source-")||n.find(O=>O.id===v)?"source":a("groupBy").find(O=>O.id===v)?"groupBy":a("metrics").find(O=>O.id===v)?"metrics":a("pivotBy").find(O=>O.id===v)?"pivotBy":a("sortBy").find(O=>O.id===v)?"sortBy":null,[n,a]),w=L.useCallback(v=>{d(v.active.id)},[]),j=L.useCallback(v=>{var k;const{active:O,over:A}=v;if(!A||!t){d(null);return}const y=O.id,T=A.id,N=h(y),C=h(T)||T;if(C==="sortBy"){if(N==="source"){const R=n.find(S=>S.id===y);if(!R){d(null);return}if(!(a("groupBy").some(S=>S.name===R.name&&S.qualifiedEntityName===R.qualifiedEntityName)||a("metrics").some(S=>S.name===R.name&&S.qualifiedEntityName===R.qualifiedEntityName))){d(null);return}}else if(N==="pivotBy"){d(null);return}else if(N!=="groupBy"&&N!=="metrics"&&N!=="sortBy"){d(null);return}}if(N==="source"&&C==="sortBy"){const R=[...n].find(F=>F.id===y);if(!R){d(null);return}const b=a("sortBy");if(s.fieldAlreadyExists(R,b)){d(null);return}const S=s.validateFieldForChartType(R,"sortBy",r.type);if(!S.isValid){s.ue.error("Invalid Field Type",{description:S.reason,position:"top-center",duration:3e3}),d(null);return}const D=s.getNewField(R,"sortBy",b);d(null),l("sortBy",[...b,D]);return}if(N==="source"&&C!=="source"&&C!=="sortBy"){const R=[...n].find(M=>M.id===y);if(!R){d(null);return}const b=[...a("groupBy"),...a("pivotBy"),...a("metrics"),...a("sortBy")];if(b.length>0){const M=b[0].qualifiedEntityName;if(!M){d(null);return}if(M!==R.qualifiedEntityName&&R.qualifiedEntityName)if(u&&(m!=null&&m.joinabilityMap)){if(s.calculateFieldState(R,{joinabilityMap:m.joinabilityMap,selectedFields:b,baseEntityName:(k=o==null?void 0:o.baseEntity)==null?void 0:k.name}).state==="unreachable"){s.showJoinRequiredNotification(),d(null);return}}else{if(!o){s.showJoinRequiredNotification(),d(null);return}if(!s.areEntitiesJoined(M,R.qualifiedEntityName,o)){s.showJoinRequiredNotification(),d(null);return}}}const S=a(C);if(s.fieldAlreadyExists(R,S)){d(null);return}const D=s.validateFieldForChartType(R,C,r.type);if(!D.isValid){s.ue.error("Invalid Field Type",{description:D.reason,position:"top-center",duration:3e3}),d(null);return}const F=s.getNewField(R,C,S),I=[...S,F];d(null),l(C,I),C==="metrics"&&t&&s.cleanupKpiMetricComparisonMode(r.type,r.config,i,t,I);return}if(N&&C&&N!=="source"&&C!=="source"&&N!==C){const R=a(N),b=a(C),S=R.find(E=>E.id===y);if(!S){d(null);return}const D=s.validateFieldForChartType(S,C,r.type);if(!D.isValid){s.ue.error("Invalid Field Type",{description:D.reason,position:"top-center",duration:3e3}),d(null);return}const F=s.getNewField(S,C,b);if(d(null),C==="sortBy"){s.fieldAlreadyExists(S,b)||l(C,[...b,F]);return}const I=R.filter(E=>E.id!==y),M=[...b,F];l(N,I),l(C,M),C==="metrics"&&t&&s.cleanupKpiMetricComparisonMode(r.type,r.config,i,t,M);return}if(N&&C&&N===C&&N!=="source"){const R=a(N),b=R.findIndex(D=>D.id===y),S=R.findIndex(D=>D.id===T);if(b!==S){const D=s.arrayMove(R,b,S);l(N,D)}}d(null)},[t,n,a,l,h,o,u,m,r.type,i,r.config]),f=c?x.find(v=>v.id===c):null;return{activeId:c,activeField:f,handleDragStart:w,handleDragEnd:j}}function tr({activeView:t,onViewChange:n,splitViewEnabled:a}){const l=s.useEditorStore(i=>i.card),{config:c}=L.useContext(s.SemaphorContext).tokenProps,d=(c==null?void 0:c.showInfoTab)!==!1,r=()=>a?!1:!["table","pivotTable","aggregateTable"].includes(l.type);return e.jsx(s.Tabs,{value:t,onValueChange:i=>n(i),children:e.jsxs(s.TabsList,{className:"h-9 w-fit gap-1 bg-background p-0",children:[e.jsx(s.TabsTrigger,{value:"chart",className:"rounded-md transition data-[state=active]:bg-muted",children:"Chart"}),r()&&e.jsx(s.TabsTrigger,{value:"table",className:"rounded-md transition data-[state=active]:bg-muted",children:"Results"}),l.type!=="kpi"&&d&&e.jsx(s.TabsTrigger,{value:"info",className:"rounded-md transition data-[state=active]:bg-muted",children:"Info"})]})})}const ar=[{value:"10",label:"Top 10"},{value:"20",label:"Top 20"},{value:"50",label:"Top 50"},{value:"100",label:"Top 100"},{value:"200",label:"Top 200"},{value:"500",label:"Top 500"},{value:"1000",label:"Top 1000"}];function nr(){var o,u;const t=s.useEditorStore(m=>m.card),{updateRowLimit:n}=s.useEditorActions(),[a,l]=L.useState(!1),[c,d]=L.useState(""),r=m=>{if(m==="custom"){l(!0);return}l(!1),n(t.id,parseInt(m))},i=m=>{const x=m.target.value;d(x),x&&!isNaN(parseInt(x))&&n(t.id,parseInt(x))};return e.jsx("div",{className:"flex items-center gap-2",children:a?e.jsx(s.Input$1,{type:"number",value:c,onChange:i,className:"h-7 w-24",placeholder:"Enter limit",min:1}):e.jsxs(s.Select,{value:((u=(o=t.config)==null?void 0:o.rowLimit)==null?void 0:u.toString())||"100",onValueChange:r,children:[e.jsx(s.SelectTrigger,{className:"h-8 w-24 border-none px-1 [&>svg]:hidden",children:e.jsx(s.SelectValue,{placeholder:"Select limit"})}),e.jsx(s.SelectContent,{children:ar.map(m=>e.jsx(s.SelectItem,{value:m.value,children:m.label},m.value))})]})})}function rr({card:t,queryData:n,isLoading:a,isFetching:l,pagination:c,onPaginationChange:d,onSortChange:r,sortState:i,splitViewEnabled:o}){const u=L.useMemo(()=>(n==null?void 0:n.records)||[],[n==null?void 0:n.records]);return!o||["table","pivotTable","aggregateTable"].includes(t.type)?e.jsx(He,{}):e.jsxs(s.ResizablePanelGroup,{direction:"vertical",className:"h-full min-h-0 w-full",children:[e.jsx(s.ResizablePanel,{defaultSize:60,minSize:30,role:"chart-section",className:"flex flex-col pb-3",children:e.jsx(He,{})}),e.jsx(s.ResizableHandle,{withHandle:!0}),e.jsx(s.ResizablePanel,{defaultSize:40,minSize:20,role:"results-section",className:"flex flex-col",children:e.jsxs("div",{className:"relative flex h-full min-h-0 max-w-full flex-1 grow basis-0 p-6",children:[(a||l)&&(n==null?void 0:n.records)&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),(n==null?void 0:n.records)&&e.jsx(s.TableVisual,{card:t,data:u,paginationMetadata:c,onPaginationChange:d,isLoading:a||l,onSortChange:r,sortState:i})]})})]})}const lr="semaphor-split-view-enabled";function or(){return As(lr,!0)}function ir({activeView:t,onViewChange:n,showDebug:a,onDebugToggle:l,onFiltersClick:c,activeCard:d,columns:r,columnsLoading:i,columnsError:o}){var se,W,ae;const u=s.useEditorStore(Q=>Q.card),m=s.useDashboardStore(Q=>Q.filterValues),x=s.useDashboardStore(Q=>Q.selectedSheetId),h=s.useDashboardStore(Q=>Q.inlineFilterValuesMap[(d==null?void 0:d.id)||""]),{updateCardConfig:w,setCard:j,updateCardInFrame:f,setShowAIDialog:p}=s.useEditorActions(),g=s.useEditorStore(Q=>Q.showAIDialog),v=s.useDashboardStore(Q=>Q.isVisualEditing),[O,A]=L.useState(!1),[y,T]=L.useState(!1),[N,C]=L.useState(null),{config:k}=L.useContext(s.SemaphorContext).tokenProps,R=(k==null?void 0:k.showInfoTab)!==!1,[b,S]=or();s.useEditorStore(Q=>{var ce;return((ce=Q.card.customCardPreferences)==null?void 0:ce.url)&&Q.card.type==="custom"});const{data:D,errorMessage:F,isLoading:I,isFetching:M,errorSql:E,pagination:B,refetch:$,comparisonMetadata:V}=s.useDashboardCardQuery(d),q=L.useMemo(()=>(D==null?void 0:D.records)||[],[D==null?void 0:D.records]),[G,z]=L.useState(!1);L.useEffect(()=>{M||z(!1)},[M]);const H=()=>{const Q=!b;Q&&t==="table"&&n("chart"),S(Q)},_=(Q,ce)=>{const ue={...d,paginationConfig:{page:Q+1,pageSize:ce}};j(ue),f(ue)};function P(){return e.jsxs("div",{className:"relative border-b border-border bg-background p-4",children:[e.jsx("button",{onClick:l,className:"absolute right-4 top-4 rounded-md p-1 hover:bg-muted","aria-label":"Close Debug Panel",children:e.jsx(s.X,{className:"h-4 w-4"})}),e.jsxs("div",{className:"space-y-4 pr-8",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Active Card Config"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(u.config||{},null,2)})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Dashboard Filters"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(m||{},null,2)})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"mb-2 text-sm font-medium",children:"Card Data Source State"}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:JSON.stringify(d.dataSource||{},null,2)})]}),e.jsxs("div",{children:[e.jsxs("h3",{className:"mb-2 text-sm font-medium",children:["Columns (",r.length,")"]}),e.jsx("pre",{className:"max-h-40 overflow-auto rounded border bg-background p-3 text-xs",children:i?"Loading...":o?"Error loading columns":JSON.stringify(r.slice(0,5),null,2)})]})]})]})}const Y=Q=>{if(!u.config)return;const ce=s.sortStateToSortByColumns(Q,u.config.metricColumns,u.config.groupByColumns);w(u.id,{...u.config,sortByColumns:ce})},Z=L.useMemo(()=>{var Q;return s.sortByColumnsToSortState((Q=u.config)==null?void 0:Q.sortByColumns)},[(se=u.config)==null?void 0:se.sortByColumns]);return e.jsxs("div",{className:s.cn("relative flex w-0 grow flex-col",{"border-l":g&&v}),children:[a&&P(),e.jsxs("div",{className:"group/visualization relative flex h-full max-w-full grow flex-col pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 px-6",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[t==="chart"&&g&&e.jsx(Bs,{variant:"outline"}),e.jsx(tr,{activeView:t,onViewChange:n,splitViewEnabled:b}),e.jsx(s.FilterInfo,{cardId:d.id,cardFilters:(W=d.config)==null?void 0:W.filters,cardFilterCount:s.getFilterCount(((ae=d.config)==null?void 0:ae.filters)||null),onFiltersClick:c,inlineFilters:d.inlineFilters,inlineFilterValues:h,sheetId:x||""}),V&&e.jsx(s.ComparisonMetadataBadge,{metadata:V,variant:"compact"}),e.jsx("div",{className:"ml-2",children:e.jsx(nr,{})}),g&&e.jsx(Re.EditorClearButton,{})]}),e.jsxs("div",{className:s.cn("flex grow items-center justify-end gap-2"),children:[e.jsx(s.IconButton,{tooltip:"Refresh",disabled:I||M,onClick:()=>{z(!0),$()},children:e.jsx(s.RefreshCcw,{className:s.cn("h-4 w-4",{"animate-spin":G})})}),t==="chart"&&e.jsx(s.IconButton,{tooltip:b?"Hide Results Table":"Show Results Table",onClick:H,children:b?e.jsx(lt,{className:"size-4"}):e.jsx(yt,{className:"size-4"})}),t==="chart"&&e.jsx(e.Fragment,{children:e.jsx(Ps,{})}),!g&&e.jsx(s.IconButton,{onClick:()=>p(!g),tooltip:"AI",className:"h-8","aria-label":"AI",children:e.jsx(s.Sparkles,{className:"size-4"})}),l&&s.isDevEnv]})]}),e.jsxs("div",{className:"mt-3 flex min-h-0 grow flex-col",children:[t==="chart"&&e.jsx("div",{className:s.cn("flex grow flex-col pb-6",{"pb-0":b&&!["table","pivotTable","aggregateTable"].includes(d.type)}),children:e.jsx(rr,{card:d,queryData:D,isLoading:I,isFetching:M,pagination:B,onPaginationChange:_,onSortChange:Y,sortState:Z,splitViewEnabled:b})}),t==="table"&&(D==null?void 0:D.records)&&e.jsxs("div",{className:"relative flex h-full min-h-0 max-w-full flex-1 grow basis-0 p-6",children:[(I||M)&&(D==null?void 0:D.records)&&e.jsx("div",{className:"absolute inset-0 z-50 flex items-center justify-center bg-background/50",children:e.jsx(s.LoadingDots,{size:"md",color:"muted","aria-label":"Loading data"})}),e.jsx(s.TableVisual,{card:d,data:q,paginationMetadata:B,onPaginationChange:_,isLoading:I||M,onSortChange:Y,sortState:Z})]}),t==="info"&&R&&e.jsx("div",{className:"flex min-h-0 flex-1 flex-col pb-6",children:e.jsx("div",{className:"flex min-h-0 flex-1 flex-col rounded-md bg-background px-6",children:F?e.jsx(Ye,{error:F,errorSql:E,onSendFeedback:()=>{const Q=s.prepareFeedbackData(s.FeedbackCategory.QUERY_GENERATION,{cardConfig:u.config,dataSource:d.dataSource,dashboardFilters:m,errorMessage:F,errorSql:E,generatedSql:D==null?void 0:D.sql});C(Q),T(!0)},onOpenInEditor:()=>{p(!0),setTimeout(()=>{const Q=s.useEditorStore.getState().actions;Q.setIsDevMode(!0),Q.setCardSql(E||""),setTimeout(()=>{Q.setRunSql(!0)},100)},100)}}):(D==null?void 0:D.sql)&&e.jsx(Ms,{sql:D.sql,showCardDetails:!0})})})]}),N&&e.jsx(s.FeedbackDialog,{open:y,onOpenChange:T,feedbackData:N})]})]})}function cr({activeField:t}){const[n,a]=L.useState(!0);return L.useEffect(()=>{t&&a(!0)},[t]),t?e.jsxs("div",{className:`flex max-w-[300px] items-center gap-2 rounded-lg border border-blue-500 bg-background p-2 shadow-lg transition-all duration-200 ${n?"scale-100 opacity-100":"scale-95 opacity-0"}`,children:[e.jsx(s.GripVertical,{className:"h-3 w-3 shrink-0 text-muted-foreground"}),e.jsx(Xe,{dataType:t.dataType}),e.jsx("span",{className:"overflow-hidden text-ellipsis whitespace-nowrap text-sm dark:text-muted-foreground",children:t.label||t.name})]}):null}const dr=()=>{const t=console.error;console.error=(...l)=>{l.length>0&&typeof l[0]=="string"&&l[0].includes("ResizeObserver loop completed with undelivered notifications")||t.apply(console,l)};const n=l=>{if(l.message&&(l.message.includes("ResizeObserver loop completed with undelivered notifications")||l.message.includes("ResizeObserver loop limit exceeded")))return l.preventDefault(),l.stopPropagation(),!1},a=l=>{var c;if((c=l.reason)!=null&&c.message&&(l.reason.message.includes("ResizeObserver loop completed with undelivered notifications")||l.reason.message.includes("ResizeObserver loop limit exceeded")))return l.preventDefault(),!1};return window.addEventListener("error",n,!0),window.addEventListener("unhandledrejection",a,!0),()=>{console.error=t,window.removeEventListener("error",n,!0),window.removeEventListener("unhandledrejection",a,!0)}};function ur({column:t,isSelected:n,onSelect:a,availableFields:l,width:c,onWidthChange:d}){var $,V,q;const r=s.useEditorStore(G=>G.card),{updateCardConfig:i,removeGroupByColumn:o,removeMetricColumn:u,updateFilters:m}=s.useEditorActions(),[x,h]=L.useState(!1),[w,j]=L.useState(!1),f=L.useRef(null),p=L.useCallback(G=>{G.preventDefault(),G.stopPropagation(),j(!0),f.current={startX:G.clientX,startWidth:c||150};const z=_=>{if(!f.current)return;const P=_.clientX-f.current.startX,Y=Math.max(80,f.current.startWidth+P);d==null||d(t.id,Y)},H=()=>{j(!1),f.current=null,document.removeEventListener("mousemove",z),document.removeEventListener("mouseup",H)};document.addEventListener("mousemove",z),document.addEventListener("mouseup",H)},[c,t.id,d]),g=($=r==null?void 0:r.config)==null?void 0:$.filters,v=(V=g==null?void 0:g.conditions)==null?void 0:V.some(G=>G.field.name===t.name&&G.field.qualifiedEntityName===t.qualifiedEntityName),O=G=>{G?m(r.id,G):i(r.id,{filters:void 0})},{attributes:A,listeners:y,setNodeRef:T,transform:N,transition:C,isDragging:k}=s.useSortable({id:t.id}),R={transform:s.CSS.Transform.toString(N),transition:C},b=s.getDataType(t.dataType),S=mr(b),D=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.sortByColumns)||[],z=G.findIndex(_=>_.name===t.name);if(z!==-1){const _=[...G];_[z]={..._[z],direction:"asc"},i(r.id,{sortByColumns:_})}else i(r.id,{sortByColumns:[...G,{...t,id:`sort-${t.id}`,role:"sortby",direction:"asc"}]})},F=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.sortByColumns)||[],z=G.findIndex(_=>_.name===t.name);if(z!==-1){const _=[...G];_[z]={..._[z],direction:"desc"},i(r.id,{sortByColumns:_})}else i(r.id,{sortByColumns:[...G,{...t,id:`sort-${t.id}`,role:"sortby",direction:"desc"}]})},I=()=>{var H;const G=((H=r==null?void 0:r.config)==null?void 0:H.groupByColumns)||[];if(!G.some(_=>_.name===t.name)){const _={...t,id:`groupby-${t.id}-${Date.now()}`,role:"groupby"};i(r.id,{groupByColumns:[...G,_]})}},M=G=>{var _;const z=((_=r==null?void 0:r.config)==null?void 0:_.metricColumns)||[];if(!z.some(P=>P.name===t.name&&P.aggregate===G)){const P={...t,id:`metric-${t.id}-${G}-${Date.now()}`,label:`${G} of ${t.label||t.name}`,role:"metric",aggregate:G};i(r.id,{metricColumns:[...z,P]})}},E=()=>{t.role==="groupby"?o(r.id,t.id):t.role==="metric"&&u(r.id,t.id)},B={...R,width:c?`${c}px`:void 0,minWidth:c?`${c}px`:"120px"};return e.jsxs("th",{ref:T,style:B,className:s.cn("group relative border-r border-border bg-background px-1 py-0",k&&"opacity-50",n&&"bg-primary/10",w&&"select-none"),children:[e.jsxs("div",{className:"flex items-center",children:[e.jsx("button",{...A,...y,className:"cursor-grab p-1 text-muted-foreground opacity-0 transition-opacity hover:text-foreground group-hover:opacity-100 active:cursor-grabbing",children:e.jsx(s.GripVertical,{className:"h-3 w-3"})}),e.jsxs(s.DropdownMenu,{children:[e.jsx(s.DropdownMenuTrigger,{asChild:!0,children:e.jsxs("button",{onClick:a,className:s.cn("flex flex-1 items-center justify-between gap-1 rounded px-2 py-2 text-left text-sm font-medium transition-colors hover:bg-muted/50",n&&"bg-primary/10"),children:[e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(S,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"truncate",children:t.label||t.name}),t.role==="metric"&&t.aggregate&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",t.aggregate,")"]}),v&&e.jsx(s.Filter,{className:"h-3 w-3 text-primary"})]}),e.jsx(s.ChevronDown,{className:"h-3 w-3 text-muted-foreground"})]})}),e.jsxs(s.DropdownMenuContent,{align:"start",className:"w-48",children:[e.jsxs(s.DropdownMenuItem,{onClick:()=>h(!0),children:[e.jsx(s.Filter,{className:s.cn("mr-2 h-4 w-4",v&&"text-primary")}),v?"Edit Filter":"Filter"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{onClick:D,children:[e.jsx(st,{className:"mr-2 h-4 w-4"}),"Sort A → Z"]}),e.jsxs(s.DropdownMenuItem,{onClick:F,children:[e.jsx(et,{className:"mr-2 h-4 w-4"}),"Sort Z → A"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuSub,{children:[e.jsxs(s.DropdownMenuSubTrigger,{children:[e.jsx(s.Calculator,{className:"mr-2 h-4 w-4"}),"Summarize"]}),e.jsxs(s.DropdownMenuSubContent,{children:[e.jsx(s.DropdownMenuItem,{onClick:()=>M("SUM"),children:"Sum"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("AVG"),children:"Average"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("COUNT"),children:"Count"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("DISTINCT"),children:"Count Distinct"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("MIN"),children:"Min"}),e.jsx(s.DropdownMenuItem,{onClick:()=>M("MAX"),children:"Max"})]})]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{onClick:I,children:[e.jsx(Je,{className:"mr-2 h-4 w-4"}),"Group by this column"]}),e.jsxs(s.DropdownMenuItem,{disabled:!0,children:[e.jsx(ws,{className:"mr-2 h-4 w-4"}),"Pivot by this column"]}),e.jsx(s.DropdownMenuSeparator,{}),e.jsxs(s.DropdownMenuItem,{disabled:!0,children:[e.jsx(s.EyeOff,{className:"mr-2 h-4 w-4"}),"Hide column"]}),e.jsxs(s.DropdownMenuItem,{onClick:E,className:"text-destructive focus:text-destructive",children:[e.jsx(s.Trash2,{className:"mr-2 h-4 w-4"}),"Remove column"]})]})]})]}),e.jsx("div",{onMouseDown:p,className:s.cn("absolute right-0 top-0 h-full w-1 cursor-col-resize bg-transparent hover:bg-primary/50",w&&"bg-primary")}),e.jsx(Ks,{open:x,onOpenChange:h,availableFields:l.length>0?l:[t],existingFilters:g,editingCondition:v?(q=g==null?void 0:g.conditions)==null?void 0:q.find(G=>G.field.name===t.name&&G.field.qualifiedEntityName===t.qualifiedEntityName):void 0,onSave:O})]})}function mr(t){switch(t){case"number":return s.Hash;case"date":return s.Calendar;case"string":default:return s.Type}}function xr({selectedColumn:t,onColumnSelect:n,isLoading:a=!1,sourceFields:l=[]}){var C,k,R,b;const c=s.useEditorStore(S=>S.card),{updateCardConfig:d}=s.useEditorActions(),[r,i]=L.useState(null),[o,u]=L.useState({}),[m,x]=L.useState(!1),h=L.useMemo(()=>{var F,I;const S=((F=c==null?void 0:c.config)==null?void 0:F.groupByColumns)||[],D=((I=c==null?void 0:c.config)==null?void 0:I.metricColumns)||[];return[...S,...D]},[(C=c==null?void 0:c.config)==null?void 0:C.groupByColumns,(k=c==null?void 0:c.config)==null?void 0:k.metricColumns]),w=L.useMemo(()=>{const S=new Set(h.map(D=>`${D.qualifiedEntityName}.${D.name}`));return l.filter(D=>!S.has(`${D.qualifiedEntityName}.${D.name}`))},[l,h]),{data:j,isLoading:f}=s.useDashboardCardQuery(c),p=(j==null?void 0:j.records)||[],g=L.useCallback((S,D)=>{u(F=>({...F,[S]:D}))},[]),v=L.useCallback(S=>{var M,E;const D=s.getDataType(S.dataType),F=((M=c==null?void 0:c.config)==null?void 0:M.groupByColumns)||[],I=((E=c==null?void 0:c.config)==null?void 0:E.metricColumns)||[];if(D==="number"){const B={...S,id:`metric-${S.id}-${Date.now()}`,role:"metric",aggregate:"SUM"};d(c.id,{metricColumns:[...I,B]})}else{const B={...S,id:`groupby-${S.id}-${Date.now()}`,role:"groupby"};d(c.id,{groupByColumns:[...F,B]})}x(!1)},[(R=c==null?void 0:c.config)==null?void 0:R.groupByColumns,(b=c==null?void 0:c.config)==null?void 0:b.metricColumns,c.id,d]),O=s.useSensors(s.useSensor(s.PointerSensor,{activationConstraint:{distance:8}})),A=S=>{i(S.active.id)},y=S=>{const{active:D,over:F}=S;if(i(null),!F||D.id===F.id)return;const I=h.findIndex(E=>E.id===D.id),M=h.findIndex(E=>E.id===F.id);if(I!==-1&&M!==-1){const E=s.arrayMove(h,I,M),B=E.filter(V=>V.role==="groupby"),$=E.filter(V=>V.role==="metric");d(c.id,{groupByColumns:B,metricColumns:$})}},T=r?h.find(S=>S.id===r):null,N=S=>{let D="";for(;S>=0;)D=String.fromCharCode(S%26+65)+D,S=Math.floor(S/26)-1;return D};return a?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsx(s.LoaderCircle,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):h.length===0?e.jsx("div",{className:"flex h-full flex-col items-center justify-center gap-4 p-8 text-center",children:e.jsxs("div",{className:"rounded-lg border-2 border-dashed border-muted-foreground/25 p-12",children:[e.jsx("h3",{className:"text-lg font-medium text-muted-foreground",children:"No columns selected"}),e.jsx("p",{className:"mt-2 text-sm text-muted-foreground/75",children:"Select columns from the panel on the right to build your table"})]})}):e.jsxs("div",{className:"flex h-full flex-col",children:[e.jsx("div",{className:"flex-1 overflow-auto",children:e.jsxs(s.DndContext,{sensors:O,collisionDetection:s.closestCenter,onDragStart:A,onDragEnd:y,children:[e.jsxs("table",{className:"w-full border-collapse",children:[e.jsxs("thead",{children:[e.jsxs("tr",{className:"border-b border-border bg-muted/30",children:[e.jsx("th",{className:"sticky left-0 z-20 w-12 border-r border-border bg-muted/50 px-2 py-1 text-center text-xs font-medium text-muted-foreground"}),e.jsx(s.SortableContext,{items:h.map(S=>S.id),strategy:s.horizontalListSortingStrategy,children:h.map((S,D)=>e.jsx("th",{style:{width:o[S.id]?`${o[S.id]}px`:void 0,minWidth:o[S.id]?`${o[S.id]}px`:"120px"},className:"border-r border-border bg-muted/30 px-2 py-1 text-center text-xs font-medium text-muted-foreground",children:N(D)},`letter-${S.id}`))}),e.jsx("th",{className:"w-10 border-r border-border bg-muted/30 px-2 py-1"})]}),e.jsxs("tr",{className:"border-b-2 border-border bg-background",children:[e.jsx("th",{className:"sticky left-0 z-20 w-12 border-r border-border bg-muted/50 px-2 py-2 text-center text-xs font-medium text-muted-foreground",children:"#"}),e.jsx(s.SortableContext,{items:h.map(S=>S.id),strategy:s.horizontalListSortingStrategy,children:h.map(S=>e.jsx(ur,{column:S,isSelected:(t==null?void 0:t.id)===S.id,onSelect:()=>n(S),availableFields:l,width:o[S.id],onWidthChange:g},S.id))}),e.jsx("th",{className:"w-10 border-r border-border bg-background px-1 py-2",children:e.jsxs(s.Popover,{open:m,onOpenChange:x,children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",title:"Add column",children:e.jsx(s.Plus,{className:"h-4 w-4"})})}),e.jsx(s.PopoverContent,{className:"w-[280px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search columns..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No columns available."}),e.jsx(s.CommandGroup,{heading:"Available Columns",children:w.map(S=>{const D=s.getDataType(S.dataType),F=D==="number"?s.Hash:D==="date"?s.Calendar:s.Type;return e.jsxs(s.CommandItem,{value:S.label||S.name,onSelect:()=>v(S),children:[e.jsx(F,{className:"mr-2 h-3.5 w-3.5 text-muted-foreground"}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm",children:S.label||S.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:S.entityName})]})]},S.id)})})]})]})})]})})]})]}),e.jsx("tbody",{children:f?e.jsx("tr",{children:e.jsx("td",{colSpan:h.length+2,className:"h-32 text-center",children:e.jsx(s.LoaderCircle,{className:"mx-auto h-6 w-6 animate-spin text-muted-foreground"})})}):p.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:h.length+2,className:"h-32 text-center text-muted-foreground",children:"No data available"})}):p.slice(0,100).map((S,D)=>e.jsxs("tr",{className:s.cn("border-b border-border transition-colors hover:bg-muted/50",D%2===0?"bg-background":"bg-muted/20"),children:[e.jsx("td",{className:"sticky left-0 z-10 w-12 border-r border-border bg-muted/50 px-2 py-2 text-center text-xs font-medium text-muted-foreground",children:D+1}),h.map(F=>{const I=S[F.name]??S[F.label??""];return e.jsx("td",{style:{width:o[F.id]?`${o[F.id]}px`:void 0,minWidth:o[F.id]?`${o[F.id]}px`:"120px"},className:s.cn("border-r border-border px-3 py-2 text-sm",(t==null?void 0:t.id)===F.id&&"bg-primary/5"),children:e.jsx("span",{className:"block truncate",children:hr(I,F)})},`${D}-${F.id}`)}),e.jsx("td",{className:"w-10 border-r border-border"})]},D))})]}),e.jsx(s.DragOverlay,{children:T&&e.jsx("div",{className:"rounded border border-primary bg-background px-3 py-2 text-sm font-medium shadow-lg",children:T.label||T.name})})]})}),e.jsxs("div",{className:"flex items-center justify-between border-t border-border bg-muted/30 px-4 py-2 text-xs text-muted-foreground",children:[e.jsxs("span",{children:["Showing ",Math.min(p.length,100)," of ",p.length," rows"]}),e.jsxs("span",{children:[h.length," columns"]})]})]})}function hr(t,n){if(t==null)return"—";if(typeof t=="number")return n.role==="metric"?new Intl.NumberFormat("en-US",{minimumFractionDigits:0,maximumFractionDigits:2}).format(t):t.toString();if(t instanceof Date||typeof t=="string"&&!isNaN(Date.parse(t))){const a=new Date(t);if(!isNaN(a.getTime()))return a.toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}return String(t)}function pr({entityGroups:t,isLoading:n=!1}){var O,A;const a=s.useEditorStore(y=>y.card),{updateCardConfig:l,removeGroupByColumn:c,removeMetricColumn:d}=s.useEditorActions(),[r,i]=L.useState(!0),[o,u]=L.useState(""),[m,x]=L.useState(new Set),h=((O=a==null?void 0:a.config)==null?void 0:O.groupByColumns)||[],w=((A=a==null?void 0:a.config)==null?void 0:A.metricColumns)||[],j=y=>{const T=new Set(m);T.has(y)?T.delete(y):T.add(y),x(T)},f=y=>h.some(T=>T.name===y.name&&T.qualifiedEntityName===y.qualifiedEntityName)||w.some(T=>T.name===y.name&&T.qualifiedEntityName===y.qualifiedEntityName),p=(y,T)=>{if(T)if(s.getDataType(y.dataType)==="number"){const C={...y,id:`metric-${y.id}-${Date.now()}`,role:"metric",aggregate:"SUM"};l(a.id,{metricColumns:[...w,C]})}else{const C={...y,id:`groupby-${y.id}-${Date.now()}`,role:"groupby"};l(a.id,{groupByColumns:[...h,C]})}else{const N=h.findIndex(k=>k.name===y.name&&k.qualifiedEntityName===y.qualifiedEntityName),C=w.findIndex(k=>k.name===y.name&&k.qualifiedEntityName===y.qualifiedEntityName);N!==-1&&c(a.id,h[N].id),C!==-1&&d(a.id,w[C].id)}},g=y=>{if(!o)return y;const T=o.toLowerCase();return y.filter(N=>N.name.toLowerCase().includes(T)||N.label&&N.label.toLowerCase().includes(T))},v=y=>{switch(s.getDataType(y)){case"number":return s.Hash;case"date":return s.Calendar;default:return s.Type}};return r?e.jsxs("div",{className:"flex h-full w-72 flex-col border-l border-border bg-background",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-border px-3 py-2",children:[e.jsx("span",{className:"text-sm font-medium",children:"Columns"}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",onClick:()=>i(!1),title:"Close panel",children:e.jsx(ft,{className:"h-4 w-4"})})]}),e.jsx("div",{className:"border-b border-border p-2",children:e.jsxs("div",{className:"relative",children:[e.jsx(s.Search,{className:"absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground"}),e.jsx(s.Input$1,{placeholder:"Search columns...",value:o,onChange:y=>u(y.target.value),className:"h-8 pl-8 text-sm"})]})}),(h.length>0||w.length>0)&&e.jsxs("div",{className:"border-b border-border",children:[h.length>0&&e.jsxs(s.Collapsible,{defaultOpen:!0,children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 px-3 py-2 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:"h-3.5 w-3.5 transition-transform duration-200 [[data-state=open]>&]:rotate-90"}),e.jsx(Je,{className:"h-3.5 w-3.5 text-blue-500"}),e.jsx("span",{children:"Groupings"}),e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:h.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"space-y-0.5 px-3 pb-2",children:h.map(y=>{const T=v(y.dataType);return e.jsxs("div",{className:"group flex items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-muted/50",children:[e.jsx(s.GripVertical,{className:"h-3 w-3 cursor-grab text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100"}),e.jsx(T,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:y.label||y.name}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-5 w-5 p-0 opacity-0 transition-opacity group-hover:opacity-100",onClick:()=>c(a.id,y.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]},y.id)})})})]}),w.length>0&&e.jsxs(s.Collapsible,{defaultOpen:!0,children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 px-3 py-2 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:"h-3.5 w-3.5 transition-transform duration-200 [[data-state=open]>&]:rotate-90"}),e.jsx(s.Calculator,{className:"h-3.5 w-3.5 text-green-500"}),e.jsx("span",{children:"Metrics"}),e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:w.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"space-y-0.5 px-3 pb-2",children:w.map(y=>{const T=v(y.dataType);return e.jsxs("div",{className:"group flex items-center gap-2 rounded px-2 py-1.5 text-sm hover:bg-muted/50",children:[e.jsx(s.GripVertical,{className:"h-3 w-3 cursor-grab text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100"}),e.jsx(T,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:y.label||y.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:y.aggregate}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-5 w-5 p-0 opacity-0 transition-opacity group-hover:opacity-100",onClick:()=>d(a.id,y.id),children:e.jsx(s.X,{className:"h-3 w-3"})})]},y.id)})})})]})]}),e.jsx(s.ScrollArea,{className:"flex-1",children:e.jsx("div",{className:"p-2",children:n?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx("div",{className:"h-6 w-6 animate-spin rounded-full border-2 border-muted-foreground border-t-transparent"})}):t.length===0?e.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"No columns available"}):t.map(y=>{const T=g(y.columns);if(T.length===0)return null;const N=m.has(y.entityId)||o.length>0;return e.jsxs(s.Collapsible,{open:N,onOpenChange:()=>j(y.entityId),children:[e.jsxs(s.CollapsibleTrigger,{className:"flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm font-medium hover:bg-muted/50",children:[e.jsx(s.ChevronRight,{className:s.cn("h-3.5 w-3.5 transition-transform duration-200",N&&"rotate-90")}),e.jsx(s.Table2,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate text-left",children:y.label||y.entityName}),e.jsx("span",{className:"text-xs text-muted-foreground",children:T.length})]}),e.jsx(s.CollapsibleContent,{children:e.jsx("div",{className:"ml-4 space-y-0.5 border-l border-border pl-2",children:T.map(C=>{const k=v(C.dataType),R=f(C);return e.jsxs("label",{className:s.cn("flex cursor-pointer items-center gap-2 rounded px-2 py-1.5 text-sm transition-colors hover:bg-muted/50",R&&"bg-primary/5"),children:[e.jsx(s.Checkbox,{checked:R,onCheckedChange:b=>p(C,b),className:"h-3.5 w-3.5"}),e.jsx(k,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"flex-1 truncate",children:C.label||C.name})]},C.id)})})})]},y.entityId)})})})]}):e.jsx("div",{className:"flex h-full items-start border-l border-border bg-muted/30 p-2",children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-8 w-8 p-0",onClick:()=>i(!0),title:"Open panel",children:e.jsx(jt,{className:"h-4 w-4"})})})}const gr=[{name:"SUM",description:"Sum of values",syntax:"SUM([column])"},{name:"AVG",description:"Average of values",syntax:"AVG([column])"},{name:"COUNT",description:"Count of rows",syntax:"COUNT([column])"},{name:"COUNTD",description:"Count distinct values",syntax:"COUNTD([column])"},{name:"MIN",description:"Minimum value",syntax:"MIN([column])"},{name:"MAX",description:"Maximum value",syntax:"MAX([column])"},{name:"IF",description:"Conditional logic",syntax:"IF(condition, then, else)"},{name:"CASE",description:"Multiple conditions",syntax:"CASE WHEN ... THEN ... END"},{name:"CONCAT",description:"Concatenate text",syntax:"CONCAT([col1], [col2])"},{name:"ROUND",description:"Round number",syntax:"ROUND([column], decimals)"},{name:"FLOOR",description:"Round down",syntax:"FLOOR([column])"},{name:"CEIL",description:"Round up",syntax:"CEIL([column])"},{name:"ABS",description:"Absolute value",syntax:"ABS([column])"},{name:"DATEPART",description:"Extract date part",syntax:"DATEPART(year, [date])"},{name:"DATEDIFF",description:"Difference between dates",syntax:"DATEDIFF(day, [date1], [date2])"}];function fr({value:t,onChange:n,onSubmit:a,onCancel:l,selectedColumn:c,availableColumns:d}){const[r,i]=L.useState(!1),[o,u]=L.useState("column"),m=L.useRef(null);L.useEffect(()=>{const j=f=>{f.key==="Escape"&&t?l():f.key==="Enter"&&t&&!r&&a(t)};return window.addEventListener("keydown",j),()=>window.removeEventListener("keydown",j)},[t,l,a,r]);const x=j=>{const f=`[${j.label||j.name}]`,p=m.current;if(p){const g=p.selectionStart||0,v=p.selectionEnd||0,O=t.slice(0,g)+f+t.slice(v);n(O),setTimeout(()=>{p.setSelectionRange(g+f.length,g+f.length),p.focus()},0)}else n(t+f);i(!1)},h=j=>{const f=m.current;if(f){const p=f.selectionStart||0,g=f.selectionEnd||0,v=t.slice(0,p)+j.syntax+t.slice(g);n(v),setTimeout(()=>{f.setSelectionRange(p+j.name.length+1,p+j.syntax.length-1),f.focus()},0)}else n(t+j.syntax);i(!1)},w=j=>{switch(s.getDataType(j)){case"number":return s.Hash;case"date":return s.Calendar;default:return s.Type}};return e.jsxs("div",{className:"flex items-center gap-2 border-b border-border bg-muted/30 px-3 py-1.5",children:[e.jsx("div",{className:"flex min-w-[100px] items-center justify-center rounded border border-border bg-background px-2 py-1 text-xs font-medium text-muted-foreground",children:c?e.jsx("span",{className:"truncate",children:c.label||c.name}):e.jsx("span",{children:"—"})}),e.jsxs(s.Popover,{open:r&&o==="function",onOpenChange:j=>{i(j),j&&u("function")},children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-muted-foreground hover:text-foreground",title:"Insert function",children:e.jsx(vt,{className:"h-4 w-4"})})}),e.jsx(s.PopoverContent,{className:"w-[300px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search functions..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No functions found."}),e.jsx(s.CommandGroup,{heading:"Functions",children:gr.map(j=>e.jsx(s.CommandItem,{value:j.name,onSelect:()=>h(j),children:e.jsxs("div",{className:"flex flex-col gap-0.5",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-mono text-sm font-medium",children:j.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:j.description})]}),e.jsx("span",{className:"font-mono text-xs text-muted-foreground/75",children:j.syntax})]})},j.name))})]})]})})]}),e.jsxs(s.Popover,{open:r&&o==="column",onOpenChange:j=>{i(j),j&&u("column")},children:[e.jsx(s.PopoverTrigger,{asChild:!0,children:e.jsxs(s.Button,{variant:"ghost",size:"sm",className:"h-7 gap-1 px-2 text-xs text-muted-foreground hover:text-foreground",title:"Insert column reference",children:[e.jsx("span",{children:"Columns"}),e.jsx(s.ChevronDown,{className:"h-3 w-3"})]})}),e.jsx(s.PopoverContent,{className:"w-[250px] p-0",align:"start",children:e.jsxs(s.Command,{children:[e.jsx(s.CommandInput,{placeholder:"Search columns..."}),e.jsxs(s.CommandList,{children:[e.jsx(s.CommandEmpty,{children:"No columns found."}),e.jsx(s.CommandGroup,{heading:"Available Columns",children:d.map(j=>{const f=w(j.dataType);return e.jsxs(s.CommandItem,{value:j.label||j.name,onSelect:()=>x(j),children:[e.jsx(f,{className:"mr-2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"truncate",children:j.label||j.name})]},j.id)})})]})]})})]}),e.jsx("div",{className:"relative flex-1",children:e.jsx(s.Input$1,{ref:m,value:t,onChange:j=>n(j.target.value),placeholder:"Enter a formula or select a column...",className:"h-7 border-border bg-background font-mono text-sm"})}),t&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-destructive hover:bg-destructive/10 hover:text-destructive",onClick:l,title:"Cancel (Esc)",children:e.jsx(s.X,{className:"h-4 w-4"})}),e.jsx(s.Button,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-green-600 hover:bg-green-600/10 hover:text-green-600",onClick:()=>a(t),title:"Apply (Enter)",children:e.jsx(s.Check,{className:"h-4 w-4"})})]})]})}function jr({className:t}){var h,w,j,f;const n=s.useEditorStore(p=>p.card),[a,l]=L.useState(null),[c,d]=L.useState(""),{columns:r=[],columnGroups:i=[],isLoading:o}=s.useColumns();(h=n==null?void 0:n.config)!=null&&h.groupByColumns,(w=n==null?void 0:n.config)!=null&&w.metricColumns,(j=n==null?void 0:n.config)!=null&&j.groupByColumns,(f=n==null?void 0:n.config)!=null&&f.pivotByColumns;const u=p=>{l(p),d(p?`[${p.label||p.name}]`:"")},m=p=>{console.log("Formula submitted:",p),d(""),l(null)},x=()=>{d(""),l(null)};return e.jsxs("div",{className:s.cn("flex h-full w-full flex-col",t),children:[e.jsx(fr,{value:c,onChange:d,onSubmit:m,onCancel:x,selectedColumn:a,availableColumns:r}),e.jsxs("div",{className:"flex flex-1 overflow-hidden",children:[e.jsx("div",{className:"flex-1 overflow-auto",children:e.jsx(xr,{selectedColumn:a,onColumnSelect:u,isLoading:o,sourceFields:r})}),e.jsx(pr,{entityGroups:i,isLoading:o})]})]})}function br(){var $,V;const[t,n]=L.useState("chart"),[a,l]=L.useState(!1),[c,d]=L.useState(!1),[r,i]=L.useState(!0),o=s.useEditorStore(q=>q.showAIDialog),u=s.useDashboardStore(q=>q.isVisualEditing),{setCardType:m}=s.useEditorActions(),x=s.useEditorStore(q=>q.card.type);L.useEffect(()=>dr(),[]);const h=q=>{m(q)},w=s.useEditorStore(q=>q.frame),j=s.useEditorStore(q=>q.card);s.useEditorStore(q=>q.activeTabCardId),s.useEditorStore(q=>q.actions.getAvailableFields);const{addCard:f,removeCard:p,updateCard:g,setActiveCard:v,updateFilters:O}=s.useEditorActions(),{columns:A=[],isLoading:y,error:T}=s.useColumns(),N=s.useSensors(s.useSensor(s.PointerSensor,{activationConstraint:{distance:8}})),C=j,{getFieldsForContainer:k,setFieldsForContainer:R}=s.useFieldManagement(C),{activeField:b,handleDragStart:S,handleDragEnd:D}=sr({activeCardId:w.activeCardId,sourceFields:A,getFieldsForContainer:k,setFieldsForContainer:R}),F=(q,G)=>{G&&O(q,G)},I=q=>{var G;g(q,{config:{...(G=w.cards.find(z=>z.id===q))==null?void 0:G.config,filters:void 0}})},M=A,E=q=>{if(!q)return 0;let G=q.conditions.length;return q.groups.forEach(z=>{G+=E(z)}),G},B=E((($=C==null?void 0:C.config)==null?void 0:$.filters)||null);return e.jsxs(s.DndContext,{sensors:N,collisionDetection:s.closestCenter,onDragStart:S,onDragEnd:D,children:[e.jsxs("div",{className:"flex h-full grow",children:[e.jsx(Ds,{isVisible:r,onToggleVisibility:()=>i(!r)}),e.jsx("div",{className:"flex flex-1 transition-all duration-300 ease-in-out",children:x==="tableBuilder"?e.jsx(jr,{className:"flex-1"}):e.jsxs(e.Fragment,{children:[!o&&e.jsx(Bn,{selectedChart:x,onChartChange:h,groupByFields:k("groupBy"),pivotByFields:k("pivotBy"),metricsFields:k("metrics"),sortByFields:k("sortBy"),activeField:b}),e.jsx(ir,{selectedChart:x,activeView:t,onViewChange:n,showDebug:a,onDebugToggle:()=>l(!a),onFiltersClick:()=>d(!0),filterCount:B,activeCard:C,dataSourceState:j.dataSource||s.EMPTY_DATA_SOURCE,columns:A,columnsLoading:y,columnsError:T,getFieldsForContainer:k,cards:w.cards,activeCardId:w.activeCardId,addCard:f,setActiveCard:v})]})}),!r&&u&&e.jsx("div",{className:"fixed left-6 top-[105px] z-50",children:e.jsx(s.Button,{size:"sm",variant:"outline",className:"h-10 w-10 rounded-full border-border bg-background p-0 shadow-lg hover:bg-muted",onClick:()=>i(!0),title:"Show Data Source Panel",children:e.jsx(s.Database,{className:"h-4 w-4"})})})]}),C&&e.jsx(er,{cardId:C.id,filters:((V=C.config)==null?void 0:V.filters)||null,onFiltersChange:F,onClearFilters:I,availableFields:M,isOpen:c,onOpenChange:d}),e.jsx(s.DragOverlay,{dropAnimation:null,children:e.jsx(cr,{activeField:b||null})})]})}function vr({authToken:t,onSave:n,onClose:a}){const l=L.useRef(null),c=s.useEditorStore(T=>T.showAIDialog),d=s.useEditorStore(T=>T.isDevMode),r=s.useAssistantLayoutStore(T=>T.setFullscreen),i=s.useAssistantLayoutStore(T=>T.setPanelOpen),o=s.useAssistantLayoutStore(T=>T.setContainer),u=s.useAssistantPanel("editor"),m=s.useFullscreenAssistant(),x=s.useAssistantContainer("editor"),h=m==="editor",w=L.useCallback(T=>{o("editor",T)},[o]),{setAuthToken:j}=s.useDashboardActions(),{setShowAIDialog:f,setOnSave:p,setOnClose:g}=s.useEditorActions(),{setActiveAssistantType:v}=s.useAIActions();L.useEffect(()=>{n&&p(n),a&&g(a)},[n,p,a,g]),L.useEffect(()=>{t&&t.accessToken&&j(t)},[t,j]),L.useEffect(()=>{var T,N;c?((T=l.current)==null||T.resize(40),i("editor",!0),v("editor")):((N=l.current)==null||N.resize(0),i("editor",!1),m==="editor"&&r(null),v(null))},[c,i,v,r,m]);function O(T){var N;T<1?(f(!1),i("editor",!1),m==="editor"&&r(null),v(null),(N=l.current)==null||N.resize(0)):(c||(f(!0),v("editor")),u.isOpen||i("editor",!0))}function A(){return e.jsxs(e.Fragment,{children:[e.jsx(Ds,{isVisible:!0,onToggleVisibility:()=>{}}),e.jsxs("div",{className:"relative flex grow border-l",children:[e.jsxs(s.ResizablePanelGroup,{"data-content":"editor-main",className:s.cn("grow transition-opacity",h&&"pointer-events-none opacity-0"),direction:"horizontal",children:[e.jsx(s.ResizablePanel,{className:"min-w-[510px] rounded-none",children:e.jsx(jn,{})}),e.jsx(s.ResizableHandle,{disabled:!c}),e.jsx(s.ResizablePanel,{"data-role":"editor-assistant-panel",ref:l,onResize:O,defaultSize:u.isOpen?40:0,className:"flex",children:e.jsx("div",{className:"grow overflow-y-auto",children:e.jsx(vs,{isFullscreen:h,onFullscreenChange:T=>r(T?"editor":null),fullscreenContainer:x})})})]}),e.jsx("div",{ref:w,className:s.cn("absolute inset-0",!h&&"pointer-events-none")})]})]})}function y(){return e.jsxs("div",{className:"relative flex h-full grow",children:[e.jsxs(s.ResizablePanelGroup,{"data-content":"explorer",className:s.cn("flex grow transition-opacity",h&&"pointer-events-none opacity-0"),direction:"horizontal",children:[e.jsx(s.ResizablePanel,{className:"min-w-[510px] grow rounded-none",children:e.jsx(br,{})}),e.jsx(s.ResizableHandle,{disabled:!c}),e.jsx(s.ResizablePanel,{"data-role":"editor-assistant-panel",ref:l,onResize:O,defaultSize:u.isOpen?40:0,className:"flex",children:e.jsx("div",{className:"grow overflow-y-auto",children:e.jsx(vs,{isFullscreen:h,onFullscreenChange:T=>r(T?"editor":null),fullscreenContainer:x})})})]}),e.jsx("div",{ref:w,className:s.cn("absolute inset-0",!h&&"pointer-events-none")})]})}return e.jsx(s.ErrorBoundary,{FallbackComponent:s.Fallback,children:d?e.jsx("section",{className:"isolate flex h-full grow","data-content":"editor-panel",children:A()}):y()})}function yr(){s.useDashboardStore(r=>r.selectedCardId);const t=s.useDashboardStore(r=>r.selectedSheetId),{setIsVisualEditing:n}=s.useDashboardStore(r=>r.actions),a=s.useDashboardStore(r=>r.isVisualEditing),l=s.useDashboardStore(r=>r.actions.updateFrame);function c(r){t&&(l(t,r),n(!1))}function d(){n(!1)}return e.jsx("div",{role:"editor-container",className:s.cn("flex-1 grow basis-0 overflow-auto",{"max-h-0 overflow-hidden":!a}),children:e.jsx(vr,{onSave:c,onClose:d})})}exports.default=yr;
|