"use strict";const s=require("./switch-C0S63HAv.js"),v=require("react"),p=require("./index-YZnHD2NF.js"),e=require("react/jsx-runtime");require("./index-BxM99sFL.js");const re=require("./use-visual-utils-CIHFvxpt.js");/**
 * @license lucide-react v0.453.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ke=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"}]]);/**
 * @license lucide-react v0.453.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const he=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"}]]);/**
 * @license lucide-react v0.453.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ge=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"}]]);/**
 * @license lucide-react v0.453.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ne=s.createLucideIcon("Rows3",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M21 9H3",key:"1338ky"}],["path",{d:"M21 15H3",key:"9uk58r"}]]);/**
 * @license lucide-react v0.453.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const De=s.createLucideIcon("SendHorizontal",[["path",{d:"M3.714 3.048a.498.498 0 0 0-.683.627l2.843 7.627a2 2 0 0 1 0 1.396l-2.842 7.627a.498.498 0 0 0 .682.627l18-8.5a.5.5 0 0 0 0-.904z",key:"117uat"}],["path",{d:"M6 12h16",key:"s4cdu5"}]]);function Ee(){const l=s.useEditorStore(h=>h.card.dataSource),{setSelectedConnectionId:a,setSelectedDatabaseName:i,setSelectedSchemaName:d,setSelectedTableName:x,setSelectedDatamodelId:u}=s.useEditorActions();v.useEffect(()=>{var D;if(!l)return;const{tableName:h,schemaName:y,databaseName:g,connectionId:j}=p.extractTableMetadata(l),b=(D=l.selectedEntities)==null?void 0:D[0],C=(b==null?void 0:b.type)==="model"&&"id"in b?b.id:"";a(j||""),i(g||""),d(y||""),x(h||""),u(C)},[l,a,i,d,x,u])}function ae(){var g,j;const l=s.useEditorStore(b=>b.card),a=s.useEditorStore(b=>b.frame),i=v.useMemo(()=>p.findCustomVisualConfigCard(a,l),[a,l]),{manifest:d}=p.useCustomVisual(((g=i==null?void 0:i.customCardPreferences)==null?void 0:g.url)||""),x=v.useMemo(()=>{var b;return(b=d==null?void 0:d.visuals)==null?void 0:b.find(m=>{var C;return m.name===((C=i==null?void 0:i.customCardPreferences)==null?void 0:C.componentName)})},[d,(j=i==null?void 0:i.customCardPreferences)==null?void 0:j.componentName]),u=p.resolveMultiInputType(i,x),h=v.useMemo(()=>p.getCustomVisualInputCards({frame:a,configCard:i,isMultiInputVisual:u==="multiple"}),[a,i,u]),y=v.useMemo(()=>{const b=h.findIndex(m=>(m==null?void 0:m.id)===l.id);return b<0?-1:p.getSlotIndexForCard(l,b)},[h,l]);return v.useMemo(()=>!i||u!=="multiple"||y<0?l.type:p.getEffectiveCardTypeForSlot(l,y,x==null?void 0:x.slots)??l.type,[i,u,y,l,x==null?void 0:x.slots])}function we({type:l,isLinked:a,className:i,showLabel:d=!1,showLocal:x=!1}){const u=s.useDashboardStore(E=>E.selectedFrameId),h=p.useManagementStore(E=>E.selectedVisual),y=s.useEditorStore(E=>E.frame);let g;if(l)g=l;else if(a!==void 0)g=a?"linked":"library";else if(!!!(y!=null&&y.visualId||h!=null&&h.id)&&u)g="local";else if(h&&!u)g="library";else if(y!=null&&y.visualId&&u)g="linked";else if(h!=null&&h.id&&u)g="library";else return null;if(g==="local"&&!x)return null;const j={local:"border-slate-200/70 bg-slate-50/80 text-slate-700 shadow-sm hover:bg-slate-100/80 dark:border-slate-700 dark:bg-slate-900/70 dark:text-slate-200 dark:hover:bg-slate-800/80",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"},b={local:"Local to this dashboard: Changes made here only affect this dashboard copy.",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."},m={local:"border-slate-200 bg-slate-50 text-slate-900 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-100",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"},C={local:"fill-slate-50 dark:fill-slate-900",linked:"fill-purple-50 dark:fill-purple-950",library:"fill-blue-50 dark:fill-blue-950"},D={local:e.jsx("span",{className:"h-2 w-2 rounded-full bg-current/70"}),linked:e.jsx(ge,{className:"h-3.5 w-3.5"}),library:e.jsx(he,{className:"h-3.5 w-3.5"})},N={local:"Local",linked:"Linked",library:"Library"};return e.jsx(s.TooltipProvider,{children:e.jsxs(s.Tooltip,{delayDuration:300,children:[e.jsx(s.TooltipTrigger,{asChild:!0,children:e.jsxs(p.Badge,{variant:"outline",className:s.cn("cursor-help select-none transition-all",j[g],i),children:[D[g],d&&e.jsx("span",{className:"ml-1 text-xs font-medium leading-none",children:N[g]})]})}),e.jsxs(s.TooltipContent,{side:"bottom",className:s.cn("max-w-xs",m[g]),children:[e.jsx(s.TooltipArrow,{className:C[g]}),e.jsx("p",{children:b[g]})]})]})})}function Ve(l,a){return{...l,sql:"",python:"",config:void 0,customCfg:"",preferences:{},interactionConfig:void 0,linkedInteractionTargetOverrides:void 0,dynamicCardPreferences:void 0,customCardPreferences:a?l.customCardPreferences:void 0,visualizationMode:"builtin",type:a?l.type:"bar"}}function le(l){const a=s.getDynamicFrameSaveIssue(l);return a?(p.ue.error(a),!1):!0}function Te({className:l,variant:a="outline"}){const i=s.useEditorStore(u=>u.onSave),{getUpdatedFrame:d}=re.useVisualUtils();function x(){const u=d();le(u)&&(i==null||i(u))}return e.jsxs(s.Button,{onClick:x,className:l,size:"xs",variant:a,children:[e.jsx(s.Check,{className:"h-3.5 w-3.5"}),"Accept"]})}function Ie({className:l,variant:a="default"}){const i=p.useQueryClient(),[d,x]=v.useState(!1),u=s.useDashboardStore(C=>C.actions.setIsDashboardPanelOpen),{setIsVisualEditing:h}=s.useDashboardActions(),{getUpdatedFrame:y}=re.useVisualUtils(),g=p.useSelectedVisual(),j=s.useEditorStore(C=>C.frame),b=p.useUpdateVisualMutation(),m=()=>{const C=y();if(!le(C))return;const D=(j==null?void 0:j.visualId)||(g==null?void 0:g.id);if(!D){p.ue.error("No visual to save");return}x(!0),b.mutate({visualId:D,data:{frameObject:C}},{onSuccess:()=>{x(!1),u(!0),p.ue.success("Visual updated successfully"),i.invalidateQueries({queryKey:["resource-by-id","visual",D],exact:!0})},onError:()=>{x(!1),p.ue.error("Failed to update visual")}})};return e.jsx(s.Button,{onClick:m,className:l,size:"xs",variant:a,children:d?e.jsxs(e.Fragment,{children:[e.jsx(s.LoaderCircle,{className:"h-3.5 w-3.5 animate-spin"}),"Saving"]}):e.jsxs(e.Fragment,{children:[e.jsx(s.Save,{className:"h-3.5 w-3.5"}),"Save to Library"]})})}function Le({className:l}){const a=p.useQueryClient(),[i,d]=v.useState(!1),x=p.useSelectedVisual(),u=s.useEditorStore(N=>N.frame),h=s.useDashboardStore(N=>N.selectedSheetId),y=s.useDashboardStore(N=>N.dashboard),{updateFrame:g,setIsVisualEditing:j}=s.useDashboardActions(),{getUpdatedFrame:b}=re.useVisualUtils(),m=p.useUpdateVisualMutation(),C=p.useResolveExistingFrameSemanticExecutionPayload(),D=async()=>{if(!h){p.ue.error("No sheet selected");return}const N=(u==null?void 0:u.visualId)||(x==null?void 0:x.id);if(!N){p.ue.error("No visual to update");return}d(!0);try{const E=b();if(!le(E)){d(!1);return}const F={...E,visualId:N},Q=p.findDashboardFrameById(y,F.id),U=await C(F,{previousFrame:Q});await m.mutateAsync({visualId:N,data:{frameObject:E}}),g(h,F,{semanticExecutionPayload:U}),a.invalidateQueries({queryKey:["visuals"]}),a.invalidateQueries({queryKey:["visual",N]}),p.ue.success("Saved to library and applied locally"),setTimeout(()=>{j(!1)},300)}catch(E){console.error("Failed to save and apply:",E),p.ue.error(E instanceof Error?E.message:"Failed to save and apply changes")}finally{d(!1)}};return e.jsx(s.Button,{onClick:D,className:l,size:"xs",variant:"default",disabled:i,children:i?e.jsxs(e.Fragment,{children:[e.jsx(s.LoaderCircle,{className:"h-3.5 w-3.5 animate-spin"}),"Saving..."]}):e.jsxs(e.Fragment,{children:[e.jsx(s.Save,{className:"h-3.5 w-3.5"}),"Save & Apply"]})})}function Fe({className:l}){const{setTempQueryData:a,setSqlGen:i}=s.useEditorActions(),d=p.useQueryClient(),x=s.useEditorStore(m=>m.tempQueryData),u=s.useEditorStore(m=>m.onClose),h=s.useEditorStore(m=>m.card),y=ae(),g=v.useMemo(()=>y!==h.type?{...h,type:y}:h,[h,y]),{queryKey:j}=p.useEditorCardQuery(g);function b(){if(x){const{queryKey:m,queryData:C}=x;m&&C&&d.setQueryData(m,C)}i({}),u==null||u()}return e.jsxs(s.Button,{onClick:b,className:l,size:"xs",variant:"outline",children:[e.jsx(s.X,{className:"h-3.5 w-3.5"}),"Close"]})}function _e({label:l="Clear",variant:a="secondary",className:i}={}){const{setTempQueryData:d}=s.useEditorActions(),x=p.useQueryClient(),u=s.useEditorStore(q=>q.tempQueryData),h=s.useEditorStore(q=>q.card),y=ae(),g=v.useMemo(()=>y!==h.type?{...h,type:y}:h,[h,y]),{queryKey:j}=p.useEditorCardQuery(g),{setCardSql:b,setSqlGen:m,setCardPython:C,setCardCustomCfg:D,setCardPreferences:N,clearQueryConfig:E,setCardConfig:F,setCardType:Q,setFrame:U,setCard:X,setActiveTabCardId:P}=s.useEditorActions();function W(){var Y,G,L,H;const q=x.getQueryData(j);u!=null&&u.queryData||d({queryKey:j,queryData:q}),x.setQueryData(j,null),x.removeQueries({queryKey:j,exact:!0}),b(""),m({}),E(),F(void 0),C(""),D(""),N({});const r=s.useEditorStore.getState().frame,T=s.useEditorStore.getState().card,I=p.findCustomVisualConfigCard(r,T),A=((Y=I==null?void 0:I.customCardPreferences)==null?void 0:Y.visualType)==="multiple"||!!((G=I==null?void 0:I.customCardPreferences)!=null&&G.dataInputCardIds)||(((L=I==null?void 0:I.customCardPreferences)==null?void 0:L.minInputs)||0)>1||(((H=I==null?void 0:I.customCardPreferences)==null?void 0:H.maxInputs)||0)>1,M=Ve(T,A);U({...r,cards:r.cards.map(O=>O.id===M.id?M:O)}),P(M.id),X(M),A||Q("bar")}return e.jsxs(s.Button,{onClick:W,className:i,size:"xs",variant:a,children:[e.jsx(s.X,{className:"h-3.5 w-3.5"}),l]})}function be(l){const{control:a,onChange:i}=l;return e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Label"}),e.jsx(s.Input,{size:"xs",value:a.label,placeholder:"Control label",onChange:d=>i(d.target.value)})]})}function Be(l){const{currentStep:a,labels:i}=l;return e.jsx("div",{className:"sticky top-0 z-10 -mx-1 border-b border-border/60 bg-background/95 px-1 pb-2.5 pt-1 backdrop-blur supports-[backdrop-filter]:bg-background/85",children:e.jsx("div",{className:"flex flex-wrap items-center gap-2 sm:gap-2.5",children:i.map((d,x)=>{const u=x+1,h=u===a,y=u<a;return e.jsxs(v.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:s.cn("flex h-5 w-5 items-center justify-center rounded-full border text-[10px] font-medium tabular-nums",h?"border-foreground bg-foreground text-background":y?"border-foreground/30 bg-foreground/10 text-foreground":"border-border/60 bg-background text-muted-foreground"),children:u}),e.jsx("span",{className:s.cn("text-[12px]",h?"font-medium text-foreground":"text-muted-foreground"),children:d})]}),x<i.length-1?e.jsx("span",{className:"h-px w-4 bg-border/60 sm:w-6","aria-hidden":"true"}):null]},d)})})})}function J(l){const{secondary:a,tertiary:i,primary:d}=l;return e.jsxs("div",{className:"sticky bottom-0 z-10 -mx-1 flex justify-end gap-2 border-t bg-background/95 px-1 pb-1 pt-3 backdrop-blur supports-[backdrop-filter]:bg-background/85",children:[a,i,d]})}function qe(l){const{control:a,scope:i,detail:d}=l;return e.jsxs("div",{className:"flex items-start gap-2.5 rounded-control border border-border/60 bg-muted/30 px-3 py-2.5",children:[e.jsx(p.CircleCheck,{className:"mt-0.5 h-3.5 w-3.5 shrink-0 text-foreground"}),e.jsxs("div",{className:"min-w-0 space-y-0.5",children:[e.jsx("div",{className:"text-[13px] font-medium text-foreground",children:a.label}),e.jsxs("div",{className:"text-[12px] text-muted-foreground",children:[s.getFriendlyTypeOption(a.type).label," ·"," ",s.getControlOptionPreview(a)," ·"," ",s.getPlacementLabel(i,a.placement)]}),d?e.jsx("div",{className:"text-[12px] text-muted-foreground",children:d}):null]})]})}function me(l){const{control:a,defaultValue:i,availableFieldChoices:d,availableMetricChoices:x,previewControlDefinitions:u,previewControlValues:h,onControlChange:y,onLabelChange:g,onDefaultValueChange:j,onBack:b,onNext:m,nextDisabled:C}=l;return e.jsxs("div",{className:"space-y-3",children:[e.jsx(be,{control:a,onChange:g}),e.jsx(p.ControlDefinitionOptionsEditor,{control:a,availableFieldChoices:d,availableMetricChoices:x,previewControlDefinitions:u,previewControlValues:h,controlLifecycle:"create",onChange:y}),e.jsx(p.ControlDefaultValueEditor,{control:a,value:i,onChange:j,allowNoDefault:!0,allowCardDefault:!0,hint:"This is the value viewers will see first.",previewControlDefinitions:u,previewControlValues:h}),e.jsx(J,{secondary:e.jsx(s.Button,{type:"button",variant:"outline",size:"xs",onClick:b,children:"Back"}),primary:e.jsx(s.Button,{type:"button",size:"xs",onClick:m,disabled:C,children:"Next"})})]})}const Ae=new Set(["grain_selector","aggregation_selector","field_selector","metric_selector"]);function xe(l){const{text:a,ariaLabel:i}=l,[d,x]=v.useState(!1);return e.jsxs("button",{type:"button","aria-label":i,onClick:async()=>{try{await navigator.clipboard.writeText(a),x(!0),setTimeout(()=>x(!1),1200)}catch(u){console.error("Failed to copy",u)}},className:"group flex w-full items-center justify-between gap-2 rounded-control bg-muted/40 px-2 py-1 text-left transition-colors hover:bg-muted/60 focus-visible:bg-muted/60 focus-visible:outline-none",children:[e.jsx("code",{className:"min-w-0 truncate font-mono text-[12px] text-foreground",children:a}),e.jsx("span",{className:"shrink-0 text-[11px] text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100",children:d?"Copied":"Click to copy"})]})}function fe(l){const{control:a,scope:i,createdReference:d,explorerContent:x,sqlDescription:u,summaryDetail:h,onBack:y,onCancel:g,onDone:j}=l,b=Ae.has(a.type),m=d.match(/\[\[param\.(.+?)\]\]/),C=m?`{{ param('${m[1]}') }}`:"";return e.jsxs("div",{className:"space-y-4",children:[e.jsx(qe,{control:a,scope:i,detail:h}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-[13px] font-medium text-foreground",children:"How cards use this control"}),e.jsxs("div",{className:"divide-y divide-border/60 overflow-hidden rounded-control border border-border/60",children:[b?e.jsxs("div",{className:"space-y-1 px-3 py-2.5",children:[e.jsx("p",{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Explorer cards"}),e.jsx("div",{className:"text-[12px] text-muted-foreground",children:x})]}):null,e.jsxs("div",{className:"space-y-1.5 px-3 py-2.5",children:[e.jsx("p",{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"SQL cards"}),e.jsx(xe,{text:d,ariaLabel:"Copy SQL control reference"}),C?e.jsx(xe,{text:C,ariaLabel:"Copy Jinja control reference"}):null,e.jsxs("p",{className:"text-[12px] text-muted-foreground",children:[u," ",e.jsx("a",{href:"https://docs.semaphor.cloud/docs/concepts/template-expressions",target:"_blank",rel:"noreferrer",className:"underline underline-offset-2 hover:text-foreground",children:"Template expressions docs"}),"."]})]})]})]}),e.jsx(J,{secondary:e.jsx(s.Button,{type:"button",variant:"outline",size:"xs",onClick:y,children:"Back"}),tertiary:e.jsx(s.Button,{type:"button",variant:"outline",size:"xs",onClick:g,children:"Cancel"}),primary:e.jsx(s.Button,{type:"button",size:"xs",onClick:j,children:"Done"})})]})}function Me(l){const a=s.createBlankControl(l.scope,l.existingControls,l.reservedIds),i=l.type===a.type?{...a,label:s.DEFAULT_LABEL_BY_TYPE[l.type]}:{...s.applyControlTypeChange({controls:[{...a,label:s.DEFAULT_LABEL_BY_TYPE[l.type]}],controlId:a.id,nextType:l.type}).controls[0],label:s.DEFAULT_LABEL_BY_TYPE[l.type]};return{step:2,control:i,defaultValue:s.getDefaultValueForType(i,void 0)}}function Oe({scope:l,controls:a,reservedControlIds:i,defaultValues:d,onChange:x,emptyMessage:u,initialCreateFlow:h=!1,usageByControlId:y,availableFieldChoices:g,availableMetricChoices:j,structuralParticipants:b,structuralParticipantIdsByControlId:m,card:C,allowedControlTypes:D,onCreateFlowActiveChange:N,onCreateFlowComplete:E}){const F=v.useMemo(()=>new Set(i||[]),[i]),Q=s.getPlacementOptionsForScope(l),U=s.getDefaultPlacement(l),[X,P]=v.useState(null),[W,q]=v.useState(null),[r,T]=v.useState(()=>h?{step:1,control:s.createBlankControl(l,a,Array.from(F))}:null),I=v.useRef({}),A=v.useRef(void 0),M=v.useRef(h),Y=v.useMemo(()=>{const t=s.getFriendlyControlTypeOptions({scope:l,card:C});if(!D)return t;const o=new Set(D);return t.filter(n=>o.has(n.value))},[D,C,l]),G=v.useCallback(t=>{const o=s.getFriendlyControlTypeOptions({scope:l,card:C,includeType:D?void 0:t});if(!D)return o;const n=new Set(D);return o.filter(c=>n.has(c.value))},[D,C,l]),L=v.useCallback(t=>l==="dashboard"&&(t==="field_selector"||t==="metric_selector"),[l]),H=v.useMemo(()=>r?s.getEligibleParticipantsForStructuralControl(b||[],r.control.type):[],[r,b]),O=v.useMemo(()=>{if(!r)return{fields:g||[],metrics:j||[]};if(!L(r.control.type))return{fields:g||[],metrics:j||[]};const t=r.selectedCardIds||[],o=s.getCompatibleStructuralChoiceCatalog({controlType:r.control.type,participants:b||[],selectedCardIds:t});return{fields:r.control.type==="field_selector"?o.choices.map(n=>({option:n,target:o.fieldTargetMap[n.value]})):[],metrics:r.control.type==="metric_selector"?o.choices.map(n=>({option:n,target:o.metricTargetMap[n.value]})):[]}},[g,j,r,L,b]);v.useEffect(()=>{N==null||N(!!r)},[r,N]),v.useEffect(()=>{const t=h&&!M.current;M.current=h,t&&(T({step:1,control:s.createBlankControl(l,a,Array.from(F))}),q(null),P(null))},[a,h,F,l]);const ye=t=>{if(!L(t.type))return{fields:g||[],metrics:j||[]};const o=(m==null?void 0:m[t.id])||[];if(o.length===0)return{fields:g||[],metrics:j||[]};const n=s.getCompatibleStructuralChoiceCatalog({controlType:t.type,participants:b||[],selectedCardIds:o});return{fields:t.type==="field_selector"?n.choices.map(c=>({option:c,target:n.fieldTargetMap[c.value]})):[],metrics:t.type==="metric_selector"?n.choices.map(c=>({option:c,target:n.metricTargetMap[c.value]})):[]}},ne=t=>{if(!L(t.control.type))return{nextControl:t.control,cardBindingsById:void 0};const o=(m==null?void 0:m[t.control.id])||[],n=s.getCompatibleStructuralChoiceCatalog({controlType:t.control.type,participants:b||[],selectedCardIds:t.nextSelectedCardIds}),c=s.reconcileStructuralControlChoices({control:t.control,choices:n.choices}),S=Object.fromEntries(o.filter(k=>!t.nextSelectedCardIds.includes(k)).map(k=>[k,void 0]));return t.nextSelectedCardIds.length>0&&c.source.kind==="manual"&&c.source.options.length>0&&Object.assign(S,s.buildStructuralControlBindingsForParticipants({control:c,participants:b||[],selectedCardIds:t.nextSelectedCardIds})),{nextControl:c,cardBindingsById:S}},B=v.useCallback((t,o=d,n={type:"update"})=>{const c=t.map(S=>(s.resolveAuthoringControlDefaultValue({control:S,defaultValues:o}),s.syncSqlQueryControlExecutionRequirement(S)));x(c,o,n)},[d,x]),K=(t,o)=>{const n=a.find(V=>V.id===t);let c=a.map(V=>V.id!==t?V:o(V));const S=c.find(V=>V.id===t);let k,f;if(S&&l==="dashboard"&&(S.type==="field_selector"||S.type==="metric_selector")){const V=(m==null?void 0:m[S.id])||[],R=ne({control:S,nextSelectedCardIds:V});c=c.map(z=>z.id===t?R.nextControl:z),k=R.cardBindingsById,f=[S.id]}else if(n&&l==="dashboard"&&(n.type==="field_selector"||n.type==="metric_selector")){const V=(m==null?void 0:m[n.id])||[];k=Object.fromEntries(V.map(R=>[R,void 0])),f=[n.id]}const w=c.find(V=>V.id===t);let _=d;if(n&&w){const V=s.resolveAuthoringControlDefaultValue({control:n,defaultValues:d});n.source.kind==="manual"&&w.source.kind==="sql_query"&&(I.current[t]=V);const R=V===void 0&&n.source.kind==="sql_query"&&w.source.kind==="manual"?I.current[t]:V,z=s.getNextEditedControlDefaultValue({previousControl:n,nextControl:w,currentDefaultValue:R});w.source.kind==="manual"&&(I.current[t]=z),d&&(_={...d},z===void 0?delete _[t]:_[t]=z),c=c.map(pe=>pe.id===t?{...w,defaultValue:z}:pe)}B(c,_,{type:"update",cardBindingsById:k,affectedBindingControlIds:f})},ve=(t,o)=>{if(!o.trim())return;const n=p.normalizeDashboardInputVariableName(o);if(n===t||F.has(n)||a.some(k=>k.id===n&&k.id!==t))return;const c=a.map(k=>k.id===t?{...k,id:n}:k);if(!d){B(c,void 0,{type:"rename",oldId:t,newId:n});return}const S={...d};Object.prototype.hasOwnProperty.call(S,t)&&(S[n]=S[t],delete S[t]),B(c,S,{type:"rename",oldId:t,newId:n})},Ce=t=>{const o=a.filter(c=>c.id!==t);if(!d){B(o,void 0,{type:"remove",controlId:t});return}const n={...d};delete n[t],B(o,n,{type:"remove",controlId:t})},je=v.useCallback(()=>{A.current=void 0,T({step:1,control:s.createBlankControl(l,a,Array.from(F))}),q(null),P(null)},[a,F,l]),$=t=>{T(o=>o&&(()=>{const n=t(o.control);o.control.source.kind==="manual"&&n.source.kind==="sql_query"&&(A.current=o.defaultValue);const c=o.defaultValue===void 0&&o.control.source.kind==="sql_query"&&n.source.kind==="manual"?A.current:o.defaultValue,S=s.getNextEditedControlDefaultValue({previousControl:o.control,nextControl:n,currentDefaultValue:c});return n.source.kind==="manual"&&(A.current=S),{...o,control:n,defaultValue:S}})())},oe=t=>{T(o=>o&&(o.control.source.kind==="manual"&&(A.current=t),{...o,defaultValue:t}))},Z=(()=>{if(!r)return!1;if(r.step===2&&L(r.control.type))return(r.selectedCardIds||[]).length>0;if(r.step!==2&&r.step!==3||!r.control.label.trim())return!1;const o=r.control.source.kind==="sql_query"?r.control:s.ensureManualSource(r.control);if(o.source.kind==="manual"&&(s.needsManualOptions(o)&&o.source.options.length===0||o.source.options.some(c=>c.label.trim().length===0||c.value.trim().length===0)))return!1;if(o.source.kind==="sql_query"){const{connectionId:n,sql:c,valueColumn:S}=o.source;return n.trim().length>0&&c.trim().length>0&&S.trim().length>0}return s.hasConcreteDefaultValue(s.getDefaultValueForType(o,r.defaultValue))})(),ie=()=>{var k;if(!r)return;const t=s.ensureManualSource(r.control),o=s.deriveUniqueControlId({label:t.label,fallbackId:t.id,existingControls:a,reservedIds:Array.from(F)}),n={...t,id:o},c=l==="dashboard"&&(n.type==="field_selector"||n.type==="metric_selector")&&((k=r.selectedCardIds)!=null&&k.length)?s.buildStructuralControlBindingsForParticipants({control:n,participants:b||[],selectedCardIds:r.selectedCardIds}):void 0,S=s.getDefaultValueForType(n,r.defaultValue);T({step:l==="dashboard"&&(n.type==="field_selector"||n.type==="metric_selector")?4:3,control:n,defaultValue:S,createdControlId:o,selectedCardIds:r.selectedCardIds,cardBindingsById:c})},ee=v.useCallback(()=>{T(null)},[]),de=v.useCallback(()=>{if(!(r!=null&&r.createdControlId)){T(null);return}const t=r.control,o=r.createdControlId,n=s.getDefaultValueForType(t,r.defaultValue);if(d){const c={...d};n===void 0?delete c[o]:c[o]=n,B([...a,{...t,defaultValue:n}],c,{type:"add",cardBindingsById:r.cardBindingsById})}else B([...a,{...t,defaultValue:n}],void 0,{type:"add",cardBindingsById:r.cardBindingsById});q(o),T(null),E==null||E()},[a,r,d,B,E]),Se=r?L(r.control.type)?["Choose type","Select cards","Configure","Finish"]:["Choose type","Configure","Finish"]:[],ce=r!=null&&r.createdControlId?`[[param.${r.createdControlId}]]`:r?`[[param.${s.slugifyControlValue(r.control.label)||r.control.id}]]`:"",te=s.buildAuthoringPreviewControlValues({controls:a,defaultValues:d}),se=r?[...a,r.control]:a,ue=r?s.buildAuthoringPreviewControlValues({controls:se,defaultValues:d,overrides:[{controlId:r.control.id,value:r.defaultValue}]}):te;return e.jsxs("div",{className:"space-y-4",children:[r?e.jsxs("div",{className:"space-y-4 pb-1",children:[e.jsx(Be,{currentStep:r.step,labels:Se}),r.step===1&&(()=>{const t=new Set(["grain_selector","aggregation_selector","field_selector","metric_selector"]),o=new Set(["date","text"]),n=Y.filter(k=>!t.has(k.value)&&!o.has(k.value)),c=Y.filter(k=>t.has(k.value)),S=k=>e.jsxs("button",{type:"button",className:"rounded-control border border-border/60 bg-background px-3 py-2.5 text-left transition-colors hover:bg-muted/40 focus-visible:bg-muted/40 focus-visible:outline-none",onClick:()=>T(Me({scope:l,existingControls:a,reservedIds:Array.from(F),type:k.value})),children:[e.jsx("div",{className:"text-[13px] font-medium text-foreground",children:k.label}),e.jsx("p",{className:"mt-0.5 text-[12px] text-muted-foreground",children:k.description})]},k.value);return e.jsxs("div",{className:"space-y-4",children:[l==="card"&&(C==null?void 0:C.sql)==null?e.jsx("p",{className:"text-[12px] text-muted-foreground",children:"Explorer cards support displayed metric, group by field, aggregation, and time grain controls here."}):null,n.length>0&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Input controls"}),e.jsx("div",{className:"grid gap-1.5 sm:grid-cols-2",children:n.map(S)})]}),c.length>0&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Data-driven controls"}),e.jsx("div",{className:"grid gap-1.5 sm:grid-cols-2",children:c.map(S)})]}),e.jsx(J,{secondary:e.jsx(s.Button,{type:"button",variant:"outline",size:"xs",onClick:ee,children:"Back to controls"})})]})})(),r.step===2&&L(r.control.type)&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx(be,{control:r.control,onChange:t=>$(o=>({...o,label:t}))}),e.jsx("p",{className:"text-[12px] text-muted-foreground",children:"Choose which cards should respond to this shared control."})]}),H.length===0?e.jsx("div",{className:"rounded-control border border-border/60 bg-muted/30 px-3 py-2.5 text-[12px] text-muted-foreground",children:"No eligible cards are available yet. Add compatible metrics or group-by fields to cards first."}):e.jsx("div",{className:"space-y-1 rounded-control border border-border/60 bg-background p-2",children:H.map(t=>{const o=(r.selectedCardIds||[]).includes(t.cardId);return e.jsxs("label",{className:"flex items-start gap-2 rounded-control px-1.5 py-1 text-[13px] hover:bg-muted/40",children:[e.jsx(p.Checkbox,{checked:o,onCheckedChange:n=>T(c=>c&&{...c,selectedCardIds:n===!0?[...c.selectedCardIds||[],t.cardId]:(c.selectedCardIds||[]).filter(S=>S!==t.cardId)})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"font-medium text-foreground",children:t.title}),e.jsx("div",{className:"text-[12px] text-muted-foreground",children:r.control.type==="field_selector"?`${t.fieldChoices.length} compatible group-by fields`:`${t.metricChoices.length} compatible metrics`})]})]},t.cardId)})}),e.jsx(J,{secondary:e.jsx(s.Button,{type:"button",variant:"outline",size:"xs",onClick:()=>T(t=>t&&{...t,step:1,selectedCardIds:[]}),children:"Back"}),primary:e.jsx(s.Button,{type:"button",size:"xs",onClick:()=>T(t=>t&&{...t,step:3}),disabled:!Z,children:"Next"})})]}),r.step===2&&!L(r.control.type)&&e.jsx(me,{control:r.control,defaultValue:r.defaultValue,availableFieldChoices:O.fields,availableMetricChoices:O.metrics,previewControlDefinitions:se,previewControlValues:ue,onControlChange:t=>$(()=>t),onLabelChange:t=>$(o=>({...o,label:t})),onDefaultValueChange:oe,onBack:()=>T(t=>t&&{...t,step:1}),onNext:ie,nextDisabled:!Z}),r.step===3&&L(r.control.type)&&e.jsx(me,{control:r.control,defaultValue:r.defaultValue,availableFieldChoices:O.fields,availableMetricChoices:O.metrics,previewControlDefinitions:se,previewControlValues:ue,onControlChange:t=>$(()=>t),onLabelChange:t=>$(o=>({...o,label:t})),onDefaultValueChange:oe,onBack:()=>T(t=>t&&{...t,step:2}),onNext:ie,nextDisabled:!Z}),r.step===3&&!L(r.control.type)&&e.jsx(fe,{control:r.control,scope:l,createdReference:ce,explorerContent:e.jsxs(e.Fragment,{children:["Open the card and use"," ",e.jsx("span",{className:"font-medium text-foreground",children:"Settings → Controls"})," ","to choose what this control changes."]}),sqlDescription:"Reference this control directly in the SQL editor.",onBack:()=>T(t=>t&&{...t,step:2}),onCancel:ee,onDone:de}),r.step===4&&L(r.control.type)&&e.jsx(fe,{control:r.control,scope:l,createdReference:ce,summaryDetail:e.jsxs(e.Fragment,{children:["Used by ",(r.selectedCardIds||[]).length," selected card",(r.selectedCardIds||[]).length===1?"":"s"]}),explorerContent:e.jsx(e.Fragment,{children:"Selected cards have been set up to respond to this control automatically."}),sqlDescription:"SQL cards can still reference this control directly in the SQL editor.",onBack:()=>T(t=>t&&{...t,step:3}),onCancel:ee,onDone:de})]}):null,!r&&a.length===0?e.jsx("div",{className:"rounded-md border border-dashed p-3 text-sm text-muted-foreground",children:u}):null,!r&&a.map(t=>{const o=ye(t),n=y==null?void 0:y[t.id],c=s.resolveAuthoringControlDefaultValue({control:t,defaultValues:d}),S=W===t.id,k="inline-flex shrink-0 items-center rounded-[4px] border border-border/60 bg-muted/40 px-1.5 py-0.5 text-[11px] font-medium text-muted-foreground";return e.jsxs("div",{className:"rounded-control border border-border/60",children:[e.jsxs("div",{className:"flex items-center gap-2 px-3 py-2.5",children:[e.jsxs("button",{type:"button",className:"flex min-w-0 flex-1 items-start gap-2 rounded-control text-left focus:outline-none focus-visible:bg-muted/40",onClick:()=>q(f=>f===t.id?null:t.id),children:[S?e.jsx(s.ChevronDown,{className:"mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground"}):e.jsx(p.ChevronRight,{className:"mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground"}),e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-1.5",children:[e.jsx("span",{className:"truncate text-[13px] font-medium text-foreground",children:t.label||"Untitled control"}),e.jsx("span",{className:k,children:s.getFriendlyTypeOption(t.type).label}),e.jsx("span",{className:k,children:s.getPlacementLabel(l,t.placement)})]}),e.jsxs("div",{className:"mt-0.5 text-[12px] text-muted-foreground",children:[s.getControlOptionPreview(t),n!=null&&n.status?` · ${n.status}`:""]})]})]}),e.jsx(s.Button,{type:"button",size:"xs",variant:"ghost",className:"h-7 w-7 shrink-0 p-0 text-muted-foreground",onClick:()=>Ce(t.id),"aria-label":`Remove ${t.label}`,children:e.jsx(p.Trash2,{className:"h-3.5 w-3.5"})})]}),S&&e.jsxs("div",{className:"space-y-4 border-t border-border/60 px-3 py-3",children:[e.jsxs("div",{className:"grid gap-3 md:grid-cols-3",children:[e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Label"}),e.jsx(s.Input,{size:"xs",value:t.label,placeholder:"Control label",onChange:f=>K(t.id,w=>({...w,label:f.target.value}))})]}),e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Type"}),e.jsxs(s.Select,{value:t.type,onValueChange:f=>{const w=s.applyControlTypeChange({controls:a,controlId:t.id,nextType:f,defaultValues:d});B(w.controls,w.defaultValues)},children:[e.jsx(s.SelectTrigger,{size:"xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:G(t.type).map(f=>e.jsx(s.SelectItem,{value:f.value,children:f.label},f.value))})]})]}),e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Show in"}),e.jsxs(s.Select,{value:t.placement||U,onValueChange:f=>K(t.id,w=>({...w,placement:f})),children:[e.jsx(s.SelectTrigger,{size:"xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:Q.map(f=>e.jsx(s.SelectItem,{value:f.value,children:f.label},f.value))})]})]})]}),L(t.type)?e.jsx(p.StructuralParticipantSelector,{type:t.type,participants:s.getEligibleParticipantsForStructuralControl(b||[],t.type).map(f=>({cardId:f.cardId,title:f.title,choiceCount:t.type==="field_selector"?f.fieldChoices.length:f.metricChoices.length})),selectedCardIds:(m==null?void 0:m[t.id])||[],onChange:f=>{const w=ne({control:t,nextSelectedCardIds:f}),_=a.map(V=>V.id===t.id?w.nextControl:V);B(_,d,{type:"update",cardBindingsById:w.cardBindingsById})}}):null,e.jsx(p.ControlDefinitionOptionsEditor,{control:t,availableFieldChoices:o.fields,availableMetricChoices:o.metrics,previewControlDefinitions:a,previewControlValues:te,controlLifecycle:"edit",onChange:f=>K(t.id,()=>f)}),t.initialValueStrategy==="saved_default"&&e.jsx(p.ControlDefaultValueEditor,{control:t,value:c,allowNoDefault:!0,allowCardDefault:!0,previewControlDefinitions:a,previewControlValues:te,onChange:f=>{const w=a.map(_=>_.id===t.id?{..._,defaultValue:f}:_);if(t.source.kind==="manual"&&(I.current[t.id]=f),d){const _={...d};f===void 0?delete _[t.id]:_[t.id]=f,B(w,_);return}B(w,void 0)}}),e.jsxs(p.Collapsible,{open:X===t.id,onOpenChange:f=>P(f?t.id:null),children:[e.jsx(p.CollapsibleTrigger,{asChild:!0,children:e.jsxs(s.Button,{type:"button",variant:"ghost",size:"xs",className:"-ml-2 gap-1 text-muted-foreground hover:text-foreground",children:[X===t.id?e.jsx(s.ChevronDown,{className:"h-3.5 w-3.5"}):e.jsx(p.ChevronRight,{className:"h-3.5 w-3.5"}),"Advanced"]})}),e.jsxs(p.CollapsibleContent,{className:"space-y-3 pt-2",children:[e.jsxs("div",{className:"grid gap-3 md:grid-cols-2",children:[e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Variable name"}),e.jsx(p.ControlIdInput,{controlId:t.id,onCommit:f=>ve(t.id,f)},t.id)]}),e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsx(s.Label,{className:"text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground",children:"Initial value"}),e.jsxs(s.Select,{value:t.initialValueStrategy,onValueChange:f=>K(t.id,w=>({...w,initialValueStrategy:f})),children:[e.jsx(s.SelectTrigger,{size:"xs",children:e.jsx(s.SelectValue,{})}),e.jsx(s.SelectContent,{children:s.INITIAL_VALUE_STRATEGY_OPTIONS.map(f=>e.jsx(s.SelectItem,{value:f.value,children:f.label},f.value))})]})]})]}),e.jsx(p.ControlVisibilitySettings,{control:t,onChange:f=>K(t.id,()=>f)})]})]})]})]},t.id)}),!r&&e.jsxs(s.Button,{type:"button",variant:"outline",size:"xs",onClick:je,children:[e.jsx(p.Plus,{className:"h-3.5 w-3.5"}),"Add control"]})]})}function ze(l){const{open:a,onOpenChange:i,initialCreateFlowActive:d=!1,dismissMode:x="passive"}=l,u=a!==void 0,[h,y]=v.useState(!1),[g,j]=v.useState(d),b=u?a:h,m=v.useCallback(N=>{!N&&g&&x==="passive"||(u||y(N),N||j(!1),i==null||i(N))},[u,g,x,i]),C=v.useCallback(()=>{u||y(!1),j(!1),i==null||i(!1)},[u,i]);v.useEffect(()=>{b||j(!1)},[b]);const D=v.useCallback(N=>{g&&N.preventDefault()},[g]);return{forceClose:C,handleOpenChange:m,isCreateFlowActive:g,preventPassiveDismissDuringCreate:D,resolvedOpen:b,setIsCreateFlowActive:j}}exports.CircleX=ke;exports.ControlDefinitionListEditor=Oe;exports.EditorAcceptButton=Te;exports.EditorCancelButton=Fe;exports.EditorClearButton=_e;exports.Library=he;exports.Link2=ge;exports.Rows3=Ne;exports.SendHorizontal=De;exports.VisualBadge=we;exports.VisualSaveAndApplyButton=Le;exports.VisualSaveButton=Ie;exports.useCreateFlowOverlayState=ze;exports.useEffectiveEditorCardType=ae;exports.useSyncLegacyEditorState=Ee;
