import { c as te, G as L, H as ne, u as oe, Z as ft, _ as ht, $ as gt, a as ie, a1 as bt, a9 as yt, B as V, e as xt, X as Ke, E as Pe, f as Ye, S as Ge, aa as vt, ab as Ct, ac as se, ad as Ae, ae as ze, af as be, ag as ye, ah as kt, ai as Me, aj as Nt, ak as St, al as le, am as Dt, an as Oe, g as Be, ao as Xe, ap as Je, aq as Ze, L as P, a2 as We, a3 as xe, a4 as ve, a5 as Ce, a6 as ke, a7 as Ne, ar as et, as as wt, at as It, au as Re, av as Et, aw as Qe, ax as Tt, ay as Se } from "./switch-xcWLtWs0.js";
import { useEffect as de, useMemo as z, useState as Q, useRef as De, useCallback as O, Fragment as Vt } from "react";
import { al as _t, am as tt, an as qt, ao as Ft, ap as Lt, aq as At, ar as zt, y as Mt, as as Ot, ae as ue, at, ag as rt, au as st, av as Bt, p as R, aw as Rt, ax as Qt, ay as je, az as jt, aA as Ut, aB as lt, aC as nt, aD as Ht, aE as $t, aF as Kt, aG as Pt, aH as Yt, aI as Gt, aJ as Xt, aK as Jt } from "./index-DZ3lq1Pk.js";
import { jsx as t, jsxs as d, Fragment as $ } from "react/jsx-runtime";
import "./index-CuHybtft.js";
import { u as we } from "./use-visual-utils-H2FXMsGo.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 ua = te("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 Zt = te("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 Wt = te("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 pa = te("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 ma = te("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 fa() {
  const s = L((h) => h.card.dataSource), {
    setSelectedConnectionId: r,
    setSelectedDatabaseName: o,
    setSelectedSchemaName: i,
    setSelectedTableName: p,
    setSelectedDatamodelId: u
  } = ne();
  de(() => {
    var S;
    if (!s) return;
    const { tableName: h, schemaName: y, databaseName: g, connectionId: v } = _t(s), b = (S = s.selectedEntities) == null ? void 0 : S[0], x = (b == null ? void 0 : b.type) === "model" && "id" in b ? b.id : "";
    r(v || ""), o(g || ""), i(y || ""), p(h || ""), u(x);
  }, [
    s,
    r,
    o,
    i,
    p,
    u
  ]);
}
function ot() {
  var g, v;
  const s = L((b) => b.card), r = L((b) => b.frame), o = z(
    () => tt(r, s),
    [r, s]
  ), { manifest: i } = qt(
    ((g = o == null ? void 0 : o.customCardPreferences) == null ? void 0 : g.url) || ""
  ), p = z(
    () => {
      var b;
      return (b = i == null ? void 0 : i.visuals) == null ? void 0 : b.find(
        (m) => {
          var x;
          return m.name === ((x = o == null ? void 0 : o.customCardPreferences) == null ? void 0 : x.componentName);
        }
      );
    },
    [i, (v = o == null ? void 0 : o.customCardPreferences) == null ? void 0 : v.componentName]
  ), u = Ft(o, p), h = z(
    () => Lt({
      frame: r,
      configCard: o,
      isMultiInputVisual: u === "multiple"
    }),
    [r, o, u]
  ), y = z(() => {
    const b = h.findIndex(
      (m) => (m == null ? void 0 : m.id) === s.id
    );
    return b < 0 ? -1 : At(s, b);
  }, [h, s]);
  return z(() => !o || u !== "multiple" || y < 0 ? s.type : zt(
    s,
    y,
    p == null ? void 0 : p.slots
  ) ?? s.type, [o, u, y, s, p == null ? void 0 : p.slots]);
}
function ha({
  type: s,
  isLinked: r,
  className: o,
  showLabel: i = !1,
  showLocal: p = !1
}) {
  const u = oe((D) => D.selectedFrameId), h = Mt((D) => D.selectedVisual), y = L((D) => D.frame);
  let g;
  if (s)
    g = s;
  else if (r !== void 0)
    g = r ? "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" && !p)
    return null;
  const v = {
    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"
  }, x = {
    local: "fill-slate-50 dark:fill-slate-900",
    linked: "fill-purple-50 dark:fill-purple-950",
    library: "fill-blue-50 dark:fill-blue-950"
  }, S = {
    local: /* @__PURE__ */ t("span", { className: "h-2 w-2 rounded-full bg-current/70" }),
    linked: /* @__PURE__ */ t(Wt, { className: "h-3.5 w-3.5" }),
    library: /* @__PURE__ */ t(Zt, { className: "h-3.5 w-3.5" })
  }, N = {
    local: "Local",
    linked: "Linked",
    library: "Library"
  };
  return /* @__PURE__ */ t(ft, { children: /* @__PURE__ */ d(ht, { delayDuration: 300, children: [
    /* @__PURE__ */ t(gt, { asChild: !0, children: /* @__PURE__ */ d(
      Ot,
      {
        variant: "outline",
        className: ie(
          "cursor-help select-none transition-all",
          v[g],
          o
        ),
        children: [
          S[g],
          i && /* @__PURE__ */ t("span", { className: "ml-1 text-xs font-medium leading-none", children: N[g] })
        ]
      }
    ) }),
    /* @__PURE__ */ d(
      bt,
      {
        side: "bottom",
        className: ie("max-w-xs", m[g]),
        children: [
          /* @__PURE__ */ t(yt, { className: x[g] }),
          /* @__PURE__ */ t("p", { children: b[g] })
        ]
      }
    )
  ] }) });
}
function ea(s, r) {
  return {
    ...s,
    sql: "",
    python: "",
    config: void 0,
    customCfg: "",
    preferences: {},
    interactionConfig: void 0,
    linkedInteractionTargetOverrides: void 0,
    dynamicCardPreferences: void 0,
    customCardPreferences: r ? s.customCardPreferences : void 0,
    visualizationMode: "builtin",
    type: r ? s.type : "bar"
  };
}
function Ie(s) {
  const r = vt(s);
  return r ? (R.error(r), !1) : !0;
}
function ga({
  className: s,
  variant: r = "outline"
}) {
  const o = L((u) => u.onSave), { getUpdatedFrame: i } = we();
  function p() {
    const u = i();
    Ie(u) && (o == null || o(u));
  }
  return /* @__PURE__ */ d(
    V,
    {
      onClick: p,
      className: s,
      size: "xs",
      variant: r,
      children: [
        /* @__PURE__ */ t(xt, { className: "h-3.5 w-3.5" }),
        "Accept"
      ]
    }
  );
}
function ba({
  className: s,
  variant: r = "default"
}) {
  const o = ue(), [i, p] = Q(!1), u = oe(
    (x) => x.actions.setIsDashboardPanelOpen
  ), { setIsVisualEditing: h } = Pe(), { getUpdatedFrame: y } = we(), g = rt(), v = L((x) => x.frame), b = st();
  return /* @__PURE__ */ t(
    V,
    {
      onClick: () => {
        const x = y();
        if (!Ie(x))
          return;
        const S = (v == null ? void 0 : v.visualId) || (g == null ? void 0 : g.id);
        if (!S) {
          R.error("No visual to save");
          return;
        }
        p(!0), b.mutate(
          {
            visualId: S,
            data: {
              frameObject: x
            }
          },
          {
            onSuccess: () => {
              p(!1), u(!0), R.success("Visual updated successfully"), o.invalidateQueries({
                queryKey: ["resource-by-id", "visual", S],
                exact: !0
              });
            },
            onError: () => {
              p(!1), R.error("Failed to update visual");
            }
          }
        );
      },
      className: s,
      size: "xs",
      variant: r,
      children: i ? /* @__PURE__ */ d($, { children: [
        /* @__PURE__ */ t(Ye, { className: "h-3.5 w-3.5 animate-spin" }),
        "Saving"
      ] }) : /* @__PURE__ */ d($, { children: [
        /* @__PURE__ */ t(Ge, { className: "h-3.5 w-3.5" }),
        "Save to Library"
      ] })
    }
  );
}
function ya({
  className: s
}) {
  const r = ue(), [o, i] = Q(!1), p = rt(), u = L((N) => N.frame), h = oe((N) => N.selectedSheetId), y = oe((N) => N.dashboard), { updateFrame: g, setIsVisualEditing: v } = Pe(), { getUpdatedFrame: b } = we(), m = st(), x = Bt();
  return /* @__PURE__ */ t(
    V,
    {
      onClick: async () => {
        if (!h) {
          R.error("No sheet selected");
          return;
        }
        const N = (u == null ? void 0 : u.visualId) || (p == null ? void 0 : p.id);
        if (!N) {
          R.error("No visual to update");
          return;
        }
        i(!0);
        try {
          const D = b();
          if (!Ie(D)) {
            i(!1);
            return;
          }
          const q = {
            ...D,
            visualId: N
            // Keep the link to library visual
          }, Y = Rt(y, q.id), G = await x(q, {
            previousFrame: Y
          });
          await m.mutateAsync({
            visualId: N,
            data: {
              frameObject: D
            }
          }), g(h, q, {
            semanticExecutionPayload: G
          }), r.invalidateQueries({ queryKey: ["visuals"] }), r.invalidateQueries({ queryKey: ["visual", N] }), R.success("Saved to library and applied locally"), setTimeout(() => {
            v(!1);
          }, 300);
        } catch (D) {
          console.error("Failed to save and apply:", D), R.error(
            D instanceof Error ? D.message : "Failed to save and apply changes"
          );
        } finally {
          i(!1);
        }
      },
      className: s,
      size: "xs",
      variant: "default",
      disabled: o,
      children: o ? /* @__PURE__ */ d($, { children: [
        /* @__PURE__ */ t(Ye, { className: "h-3.5 w-3.5 animate-spin" }),
        "Saving..."
      ] }) : /* @__PURE__ */ d($, { children: [
        /* @__PURE__ */ t(Ge, { className: "h-3.5 w-3.5" }),
        "Save & Apply"
      ] })
    }
  );
}
function xa({ className: s }) {
  const { setTempQueryData: r, setSqlGen: o } = ne(), i = ue(), p = L((m) => m.tempQueryData), u = L((m) => m.onClose), h = L((m) => m.card), y = ot(), g = z(() => y !== h.type ? { ...h, type: y } : h, [h, y]), { queryKey: v } = at(g);
  function b() {
    if (p) {
      const { queryKey: m, queryData: x } = p;
      m && x && i.setQueryData(m, x);
    }
    o({}), u == null || u();
  }
  return /* @__PURE__ */ d(
    V,
    {
      onClick: b,
      className: s,
      size: "xs",
      variant: "outline",
      children: [
        /* @__PURE__ */ t(Ke, { className: "h-3.5 w-3.5" }),
        "Close"
      ]
    }
  );
}
function va({
  label: s = "Clear",
  variant: r = "secondary",
  className: o
} = {}) {
  const { setTempQueryData: i } = ne(), p = ue(), u = L((M) => M.tempQueryData), h = L((M) => M.card), y = ot(), g = z(() => y !== h.type ? { ...h, type: y } : h, [h, y]), { queryKey: v } = at(g), {
    setCardSql: b,
    setSqlGen: m,
    setCardPython: x,
    setCardCustomCfg: S,
    setCardPreferences: N,
    clearQueryConfig: D,
    setCardConfig: q,
    setCardType: Y,
    setFrame: G,
    setCard: ae,
    setActiveTabCardId: X
  } = ne();
  function pe() {
    var J, re, _, Z;
    const M = p.getQueryData(v);
    u != null && u.queryData || i({
      queryKey: v,
      queryData: M
    }), p.setQueryData(v, null), p.removeQueries({ queryKey: v, exact: !0 }), b(""), m({}), D(), q(void 0), x(""), S(""), N({});
    const a = L.getState().frame, E = L.getState().card, T = tt(a, E), B = ((J = T == null ? void 0 : T.customCardPreferences) == null ? void 0 : J.visualType) === "multiple" || !!((re = T == null ? void 0 : T.customCardPreferences) != null && re.dataInputCardIds) || (((_ = T == null ? void 0 : T.customCardPreferences) == null ? void 0 : _.minInputs) || 0) > 1 || (((Z = T == null ? void 0 : T.customCardPreferences) == null ? void 0 : Z.maxInputs) || 0) > 1, j = ea(E, B);
    G({
      ...a,
      cards: a.cards.map(
        (U) => U.id === j.id ? j : U
      )
    }), X(j.id), ae(j), B || Y("bar");
  }
  return /* @__PURE__ */ d(
    V,
    {
      onClick: pe,
      className: o,
      size: "xs",
      variant: r,
      children: [
        /* @__PURE__ */ t(Ke, { className: "h-3.5 w-3.5" }),
        s
      ]
    }
  );
}
function it(s) {
  const { control: r, onChange: o } = s;
  return /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
    /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Label" }),
    /* @__PURE__ */ t(
      We,
      {
        size: "xs",
        value: r.label,
        placeholder: "Control label",
        onChange: (i) => o(i.target.value)
      }
    )
  ] });
}
function ta(s) {
  const { currentStep: r, labels: o } = s;
  return /* @__PURE__ */ t("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: /* @__PURE__ */ t("div", { className: "flex flex-wrap items-center gap-2 sm:gap-2.5", children: o.map((i, p) => {
    const u = p + 1, h = u === r, y = u < r;
    return /* @__PURE__ */ d(Vt, { children: [
      /* @__PURE__ */ d("div", { className: "flex items-center gap-1.5", children: [
        /* @__PURE__ */ t(
          "span",
          {
            className: ie(
              "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
          }
        ),
        /* @__PURE__ */ t(
          "span",
          {
            className: ie(
              "text-[12px]",
              h ? "font-medium text-foreground" : "text-muted-foreground"
            ),
            children: i
          }
        )
      ] }),
      p < o.length - 1 ? /* @__PURE__ */ t(
        "span",
        {
          className: "h-px w-4 bg-border/60 sm:w-6",
          "aria-hidden": "true"
        }
      ) : null
    ] }, i);
  }) }) });
}
function ce(s) {
  const { secondary: r, tertiary: o, primary: i } = s;
  return /* @__PURE__ */ d("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: [
    r,
    o,
    i
  ] });
}
function aa(s) {
  const { control: r, scope: o, detail: i } = s;
  return /* @__PURE__ */ d("div", { className: "flex items-start gap-2.5 rounded-control border border-border/60 bg-muted/30 px-3 py-2.5", children: [
    /* @__PURE__ */ t(Jt, { className: "mt-0.5 h-3.5 w-3.5 shrink-0 text-foreground" }),
    /* @__PURE__ */ d("div", { className: "min-w-0 space-y-0.5", children: [
      /* @__PURE__ */ t("div", { className: "text-[13px] font-medium text-foreground", children: r.label }),
      /* @__PURE__ */ d("div", { className: "text-[12px] text-muted-foreground", children: [
        Xe(r.type).label,
        " ·",
        " ",
        Ze(r),
        " ·",
        " ",
        Je(o, r.placement)
      ] }),
      i ? /* @__PURE__ */ t("div", { className: "text-[12px] text-muted-foreground", children: i }) : null
    ] })
  ] });
}
function Ue(s) {
  const {
    control: r,
    defaultValue: o,
    availableFieldChoices: i,
    availableMetricChoices: p,
    previewControlDefinitions: u,
    previewControlValues: h,
    onControlChange: y,
    onLabelChange: g,
    onDefaultValueChange: v,
    onBack: b,
    onNext: m,
    nextDisabled: x
  } = s;
  return /* @__PURE__ */ d("div", { className: "space-y-3", children: [
    /* @__PURE__ */ t(it, { control: r, onChange: g }),
    /* @__PURE__ */ t(
      lt,
      {
        control: r,
        availableFieldChoices: i,
        availableMetricChoices: p,
        previewControlDefinitions: u,
        previewControlValues: h,
        controlLifecycle: "create",
        onChange: y
      }
    ),
    /* @__PURE__ */ t(
      nt,
      {
        control: r,
        value: o,
        onChange: v,
        allowNoDefault: !0,
        allowCardDefault: !0,
        hint: "This is the value viewers will see first.",
        previewControlDefinitions: u,
        previewControlValues: h
      }
    ),
    /* @__PURE__ */ t(
      ce,
      {
        secondary: /* @__PURE__ */ t(V, { type: "button", variant: "outline", size: "xs", onClick: b, children: "Back" }),
        primary: /* @__PURE__ */ t(
          V,
          {
            type: "button",
            size: "xs",
            onClick: m,
            disabled: x,
            children: "Next"
          }
        )
      }
    )
  ] });
}
const ra = /* @__PURE__ */ new Set([
  "grain_selector",
  "aggregation_selector",
  "field_selector",
  "metric_selector"
]);
function He(s) {
  const { text: r, ariaLabel: o } = s, [i, p] = Q(!1);
  return /* @__PURE__ */ d(
    "button",
    {
      type: "button",
      "aria-label": o,
      onClick: async () => {
        try {
          await navigator.clipboard.writeText(r), p(!0), setTimeout(() => p(!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: [
        /* @__PURE__ */ t("code", { className: "min-w-0 truncate font-mono text-[12px] text-foreground", children: r }),
        /* @__PURE__ */ t("span", { className: "shrink-0 text-[11px] text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100", children: i ? "Copied" : "Click to copy" })
      ]
    }
  );
}
function $e(s) {
  const {
    control: r,
    scope: o,
    createdReference: i,
    explorerContent: p,
    sqlDescription: u,
    summaryDetail: h,
    onBack: y,
    onCancel: g,
    onDone: v
  } = s, b = ra.has(r.type), m = i.match(/\[\[param\.(.+?)\]\]/), x = m ? `{{ param('${m[1]}') }}` : "";
  return /* @__PURE__ */ d("div", { className: "space-y-4", children: [
    /* @__PURE__ */ t(
      aa,
      {
        control: r,
        scope: o,
        detail: h
      }
    ),
    /* @__PURE__ */ d("div", { className: "space-y-2", children: [
      /* @__PURE__ */ t("p", { className: "text-[13px] font-medium text-foreground", children: "How cards use this control" }),
      /* @__PURE__ */ d("div", { className: "divide-y divide-border/60 overflow-hidden rounded-control border border-border/60", children: [
        b ? /* @__PURE__ */ d("div", { className: "space-y-1 px-3 py-2.5", children: [
          /* @__PURE__ */ t("p", { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Explorer cards" }),
          /* @__PURE__ */ t("div", { className: "text-[12px] text-muted-foreground", children: p })
        ] }) : null,
        /* @__PURE__ */ d("div", { className: "space-y-1.5 px-3 py-2.5", children: [
          /* @__PURE__ */ t("p", { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "SQL cards" }),
          /* @__PURE__ */ t(
            He,
            {
              text: i,
              ariaLabel: "Copy SQL control reference"
            }
          ),
          x ? /* @__PURE__ */ t(
            He,
            {
              text: x,
              ariaLabel: "Copy Jinja control reference"
            }
          ) : null,
          /* @__PURE__ */ d("p", { className: "text-[12px] text-muted-foreground", children: [
            u,
            " ",
            /* @__PURE__ */ t(
              "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"
              }
            ),
            "."
          ] })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ t(
      ce,
      {
        secondary: /* @__PURE__ */ t(V, { type: "button", variant: "outline", size: "xs", onClick: y, children: "Back" }),
        tertiary: /* @__PURE__ */ t(V, { type: "button", variant: "outline", size: "xs", onClick: g, children: "Cancel" }),
        primary: /* @__PURE__ */ t(V, { type: "button", size: "xs", onClick: v, children: "Done" })
      }
    )
  ] });
}
function sa(s) {
  const r = se(
    s.scope,
    s.existingControls,
    s.reservedIds
  ), o = s.type === r.type ? {
    ...r,
    label: Se[s.type]
  } : {
    ...et({
      controls: [
        {
          ...r,
          label: Se[s.type]
        }
      ],
      controlId: r.id,
      nextType: s.type
    }).controls[0],
    label: Se[s.type]
  };
  return {
    step: 2,
    control: o,
    defaultValue: le(o, void 0)
  };
}
function Ca({
  scope: s,
  controls: r,
  reservedControlIds: o,
  defaultValues: i,
  onChange: p,
  emptyMessage: u,
  initialCreateFlow: h = !1,
  usageByControlId: y,
  availableFieldChoices: g,
  availableMetricChoices: v,
  structuralParticipants: b,
  structuralParticipantIdsByControlId: m,
  card: x,
  allowedControlTypes: S,
  onCreateFlowActiveChange: N,
  onCreateFlowComplete: D
}) {
  const q = z(
    () => new Set(o || []),
    [o]
  ), Y = wt(s), G = Ct(s), [ae, X] = Q(
    null
  ), [pe, M] = Q(
    null
  ), [a, E] = Q(
    () => h ? {
      step: 1,
      control: se(s, r, Array.from(q))
    } : null
  ), T = De({}), B = De(void 0), j = De(h), J = z(() => {
    const e = Ae({ scope: s, card: x });
    if (!S)
      return e;
    const n = new Set(S);
    return e.filter((l) => n.has(l.value));
  }, [S, x, s]), re = O(
    (e) => {
      const n = Ae({
        scope: s,
        card: x,
        includeType: S ? void 0 : e
      });
      if (!S)
        return n;
      const l = new Set(S);
      return n.filter((c) => l.has(c.value));
    },
    [S, x, s]
  ), _ = O(
    (e) => s === "dashboard" && (e === "field_selector" || e === "metric_selector"),
    [s]
  ), Z = z(
    () => a ? ze(
      b || [],
      a.control.type
    ) : [],
    [a, b]
  ), U = z(() => {
    if (!a)
      return {
        fields: g || [],
        metrics: v || []
      };
    if (!_(a.control.type))
      return {
        fields: g || [],
        metrics: v || []
      };
    const e = a.selectedCardIds || [], n = be({
      controlType: a.control.type,
      participants: b || [],
      selectedCardIds: e
    });
    return {
      fields: a.control.type === "field_selector" ? n.choices.map((l) => ({
        option: l,
        target: n.fieldTargetMap[l.value]
      })) : [],
      metrics: a.control.type === "metric_selector" ? n.choices.map((l) => ({
        option: l,
        target: n.metricTargetMap[l.value]
      })) : []
    };
  }, [
    g,
    v,
    a,
    _,
    b
  ]);
  de(() => {
    N == null || N(!!a);
  }, [a, N]), de(() => {
    const e = h && !j.current;
    j.current = h, e && (E({
      step: 1,
      control: se(s, r, Array.from(q))
    }), M(null), X(null));
  }, [r, h, q, s]);
  const dt = (e) => {
    if (!_(e.type))
      return {
        fields: g || [],
        metrics: v || []
      };
    const n = (m == null ? void 0 : m[e.id]) || [];
    if (n.length === 0)
      return {
        fields: g || [],
        metrics: v || []
      };
    const l = be({
      controlType: e.type,
      participants: b || [],
      selectedCardIds: n
    });
    return {
      fields: e.type === "field_selector" ? l.choices.map((c) => ({
        option: c,
        target: l.fieldTargetMap[c.value]
      })) : [],
      metrics: e.type === "metric_selector" ? l.choices.map((c) => ({
        option: c,
        target: l.metricTargetMap[c.value]
      })) : []
    };
  }, Ee = (e) => {
    if (!_(e.control.type))
      return {
        nextControl: e.control,
        cardBindingsById: void 0
      };
    const n = (m == null ? void 0 : m[e.control.id]) || [], l = be({
      controlType: e.control.type,
      participants: b || [],
      selectedCardIds: e.nextSelectedCardIds
    }), c = Tt({
      control: e.control,
      choices: l.choices
    }), C = Object.fromEntries(
      n.filter((k) => !e.nextSelectedCardIds.includes(k)).map((k) => [k, void 0])
    );
    return e.nextSelectedCardIds.length > 0 && c.source.kind === "manual" && c.source.options.length > 0 && Object.assign(
      C,
      Qe({
        control: c,
        participants: b || [],
        selectedCardIds: e.nextSelectedCardIds
      })
    ), {
      nextControl: c,
      cardBindingsById: C
    };
  }, A = O(
    (e, n = i, l = { type: "update" }) => {
      const c = e.map((C) => (ye({
        control: C,
        defaultValues: n
      }), kt(
        C
      )));
      p(c, n, l);
    },
    [i, p]
  ), W = (e, n) => {
    const l = r.find(
      (I) => I.id === e
    );
    let c = r.map((I) => I.id !== e ? I : n(I));
    const C = c.find(
      (I) => I.id === e
    );
    let k, f;
    if (C && s === "dashboard" && (C.type === "field_selector" || C.type === "metric_selector")) {
      const I = (m == null ? void 0 : m[C.id]) || [], K = Ee({
        control: C,
        nextSelectedCardIds: I
      });
      c = c.map(
        (H) => H.id === e ? K.nextControl : H
      ), k = K.cardBindingsById, f = [C.id];
    } else if (l && s === "dashboard" && (l.type === "field_selector" || l.type === "metric_selector")) {
      const I = (m == null ? void 0 : m[l.id]) || [];
      k = Object.fromEntries(
        I.map((K) => [K, void 0])
      ), f = [l.id];
    }
    const w = c.find(
      (I) => I.id === e
    );
    let F = i;
    if (l && w) {
      const I = ye({
        control: l,
        defaultValues: i
      });
      l.source.kind === "manual" && w.source.kind === "sql_query" && (T.current[e] = I);
      const K = I === void 0 && l.source.kind === "sql_query" && w.source.kind === "manual" ? T.current[e] : I, H = Re({
        previousControl: l,
        nextControl: w,
        currentDefaultValue: K
      });
      w.source.kind === "manual" && (T.current[e] = H), i && (F = { ...i }, H === void 0 ? delete F[e] : F[e] = H), c = c.map(
        (Le) => Le.id === e ? { ...w, defaultValue: H } : Le
      );
    }
    A(c, F, {
      type: "update",
      cardBindingsById: k,
      affectedBindingControlIds: f
    });
  }, ct = (e, n) => {
    if (!n.trim())
      return;
    const l = Xt(n);
    if (l === e || q.has(l) || r.some(
      (k) => k.id === l && k.id !== e
    ))
      return;
    const c = r.map(
      (k) => k.id === e ? { ...k, id: l } : k
    );
    if (!i) {
      A(c, void 0, {
        type: "rename",
        oldId: e,
        newId: l
      });
      return;
    }
    const C = { ...i };
    Object.prototype.hasOwnProperty.call(C, e) && (C[l] = C[e], delete C[e]), A(c, C, {
      type: "rename",
      oldId: e,
      newId: l
    });
  }, ut = (e) => {
    const n = r.filter((c) => c.id !== e);
    if (!i) {
      A(n, void 0, { type: "remove", controlId: e });
      return;
    }
    const l = { ...i };
    delete l[e], A(n, l, { type: "remove", controlId: e });
  }, pt = O(() => {
    B.current = void 0, E({
      step: 1,
      control: se(s, r, Array.from(q))
    }), M(null), X(null);
  }, [r, q, s]), ee = (e) => {
    E(
      (n) => n && (() => {
        const l = e(n.control);
        n.control.source.kind === "manual" && l.source.kind === "sql_query" && (B.current = n.defaultValue);
        const c = n.defaultValue === void 0 && n.control.source.kind === "sql_query" && l.source.kind === "manual" ? B.current : n.defaultValue, C = Re({
          previousControl: n.control,
          nextControl: l,
          currentDefaultValue: c
        });
        return l.source.kind === "manual" && (B.current = C), {
          ...n,
          control: l,
          defaultValue: C
        };
      })()
    );
  }, Te = (e) => {
    E(
      (n) => n && (n.control.source.kind === "manual" && (B.current = e), {
        ...n,
        defaultValue: e
      })
    );
  }, me = (() => {
    if (!a)
      return !1;
    if (a.step === 2 && _(a.control.type))
      return (a.selectedCardIds || []).length > 0;
    if (a.step !== 2 && a.step !== 3 || !a.control.label.trim())
      return !1;
    const n = a.control.source.kind === "sql_query" ? a.control : Me(a.control);
    if (n.source.kind === "manual" && (Nt(n) && n.source.options.length === 0 || n.source.options.some(
      (c) => c.label.trim().length === 0 || c.value.trim().length === 0
    )))
      return !1;
    if (n.source.kind === "sql_query") {
      const { connectionId: l, sql: c, valueColumn: C } = n.source;
      return l.trim().length > 0 && c.trim().length > 0 && C.trim().length > 0;
    }
    return St(
      le(n, a.defaultValue)
    );
  })(), Ve = () => {
    var k;
    if (!a)
      return;
    const e = Me(a.control), n = Et({
      label: e.label,
      fallbackId: e.id,
      existingControls: r,
      reservedIds: Array.from(q)
    }), l = {
      ...e,
      id: n
    }, c = s === "dashboard" && (l.type === "field_selector" || l.type === "metric_selector") && ((k = a.selectedCardIds) != null && k.length) ? Qe({
      control: l,
      participants: b || [],
      selectedCardIds: a.selectedCardIds
    }) : void 0, C = le(
      l,
      a.defaultValue
    );
    E({
      step: s === "dashboard" && (l.type === "field_selector" || l.type === "metric_selector") ? 4 : 3,
      control: l,
      defaultValue: C,
      createdControlId: n,
      selectedCardIds: a.selectedCardIds,
      cardBindingsById: c
    });
  }, fe = O(() => {
    E(null);
  }, []), _e = O(() => {
    if (!(a != null && a.createdControlId)) {
      E(null);
      return;
    }
    const e = a.control, n = a.createdControlId, l = le(
      e,
      a.defaultValue
    );
    if (i) {
      const c = { ...i };
      l === void 0 ? delete c[n] : c[n] = l, A(
        [
          ...r,
          {
            ...e,
            defaultValue: l
          }
        ],
        c,
        {
          type: "add",
          cardBindingsById: a.cardBindingsById
        }
      );
    } else
      A(
        [
          ...r,
          {
            ...e,
            defaultValue: l
          }
        ],
        void 0,
        {
          type: "add",
          cardBindingsById: a.cardBindingsById
        }
      );
    M(n), E(null), D == null || D();
  }, [r, a, i, A, D]), mt = a ? _(a.control.type) ? ["Choose type", "Select cards", "Configure", "Finish"] : ["Choose type", "Configure", "Finish"] : [], qe = a != null && a.createdControlId ? `[[param.${a.createdControlId}]]` : a ? `[[param.${Dt(a.control.label) || a.control.id}]]` : "", he = Oe({
    controls: r,
    defaultValues: i
  }), ge = a ? [...r, a.control] : r, Fe = a ? Oe({
    controls: ge,
    defaultValues: i,
    overrides: [
      {
        controlId: a.control.id,
        value: a.defaultValue
      }
    ]
  }) : he;
  return /* @__PURE__ */ d("div", { className: "space-y-4", children: [
    a ? /* @__PURE__ */ d("div", { className: "space-y-4 pb-1", children: [
      /* @__PURE__ */ t(
        ta,
        {
          currentStep: a.step,
          labels: mt
        }
      ),
      a.step === 1 && (() => {
        const e = /* @__PURE__ */ new Set([
          "grain_selector",
          "aggregation_selector",
          "field_selector",
          "metric_selector"
        ]), n = /* @__PURE__ */ new Set([
          // Temporarily hide these from the add-control dialog while
          // keeping the shared definitions for existing controls.
          "date",
          "text"
        ]), l = J.filter(
          (k) => !e.has(k.value) && !n.has(k.value)
        ), c = J.filter(
          (k) => e.has(k.value)
        ), C = (k) => /* @__PURE__ */ d(
          "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: () => E(
              sa({
                scope: s,
                existingControls: r,
                reservedIds: Array.from(q),
                type: k.value
              })
            ),
            children: [
              /* @__PURE__ */ t("div", { className: "text-[13px] font-medium text-foreground", children: k.label }),
              /* @__PURE__ */ t("p", { className: "mt-0.5 text-[12px] text-muted-foreground", children: k.description })
            ]
          },
          k.value
        );
        return /* @__PURE__ */ d("div", { className: "space-y-4", children: [
          s === "card" && (x == null ? void 0 : x.sql) == null ? /* @__PURE__ */ t("p", { className: "text-[12px] text-muted-foreground", children: "Explorer cards support displayed metric, group by field, aggregation, and time grain controls here." }) : null,
          l.length > 0 && /* @__PURE__ */ d("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ t("p", { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Input controls" }),
            /* @__PURE__ */ t("div", { className: "grid gap-1.5 sm:grid-cols-2", children: l.map(C) })
          ] }),
          c.length > 0 && /* @__PURE__ */ d("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ t("p", { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Data-driven controls" }),
            /* @__PURE__ */ t("div", { className: "grid gap-1.5 sm:grid-cols-2", children: c.map(C) })
          ] }),
          /* @__PURE__ */ t(
            ce,
            {
              secondary: /* @__PURE__ */ t(
                V,
                {
                  type: "button",
                  variant: "outline",
                  size: "xs",
                  onClick: fe,
                  children: "Back to controls"
                }
              )
            }
          )
        ] });
      })(),
      a.step === 2 && _(a.control.type) && /* @__PURE__ */ d("div", { className: "space-y-3", children: [
        /* @__PURE__ */ d("div", { className: "space-y-3", children: [
          /* @__PURE__ */ t(
            it,
            {
              control: a.control,
              onChange: (e) => ee((n) => ({
                ...n,
                label: e
              }))
            }
          ),
          /* @__PURE__ */ t("p", { className: "text-[12px] text-muted-foreground", children: "Choose which cards should respond to this shared control." })
        ] }),
        Z.length === 0 ? /* @__PURE__ */ t("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." }) : /* @__PURE__ */ t("div", { className: "space-y-1 rounded-control border border-border/60 bg-background p-2", children: Z.map((e) => {
          const n = (a.selectedCardIds || []).includes(e.cardId);
          return /* @__PURE__ */ d(
            "label",
            {
              className: "flex items-start gap-2 rounded-control px-1.5 py-1 text-[13px] hover:bg-muted/40",
              children: [
                /* @__PURE__ */ t(
                  Qt,
                  {
                    checked: n,
                    onCheckedChange: (l) => E(
                      (c) => c && {
                        ...c,
                        selectedCardIds: l === !0 ? [
                          ...c.selectedCardIds || [],
                          e.cardId
                        ] : (c.selectedCardIds || []).filter(
                          (C) => C !== e.cardId
                        )
                      }
                    )
                  }
                ),
                /* @__PURE__ */ d("div", { className: "min-w-0", children: [
                  /* @__PURE__ */ t("div", { className: "font-medium text-foreground", children: e.title }),
                  /* @__PURE__ */ t("div", { className: "text-[12px] text-muted-foreground", children: a.control.type === "field_selector" ? `${e.fieldChoices.length} compatible group-by fields` : `${e.metricChoices.length} compatible metrics` })
                ] })
              ]
            },
            e.cardId
          );
        }) }),
        /* @__PURE__ */ t(
          ce,
          {
            secondary: /* @__PURE__ */ t(
              V,
              {
                type: "button",
                variant: "outline",
                size: "xs",
                onClick: () => E(
                  (e) => e && {
                    ...e,
                    step: 1,
                    selectedCardIds: []
                  }
                ),
                children: "Back"
              }
            ),
            primary: /* @__PURE__ */ t(
              V,
              {
                type: "button",
                size: "xs",
                onClick: () => E(
                  (e) => e && {
                    ...e,
                    step: 3
                  }
                ),
                disabled: !me,
                children: "Next"
              }
            )
          }
        )
      ] }),
      a.step === 2 && !_(a.control.type) && /* @__PURE__ */ t(
        Ue,
        {
          control: a.control,
          defaultValue: a.defaultValue,
          availableFieldChoices: U.fields,
          availableMetricChoices: U.metrics,
          previewControlDefinitions: ge,
          previewControlValues: Fe,
          onControlChange: (e) => ee(() => e),
          onLabelChange: (e) => ee((n) => ({
            ...n,
            label: e
          })),
          onDefaultValueChange: Te,
          onBack: () => E(
            (e) => e && {
              ...e,
              step: 1
            }
          ),
          onNext: Ve,
          nextDisabled: !me
        }
      ),
      a.step === 3 && _(a.control.type) && /* @__PURE__ */ t(
        Ue,
        {
          control: a.control,
          defaultValue: a.defaultValue,
          availableFieldChoices: U.fields,
          availableMetricChoices: U.metrics,
          previewControlDefinitions: ge,
          previewControlValues: Fe,
          onControlChange: (e) => ee(() => e),
          onLabelChange: (e) => ee((n) => ({
            ...n,
            label: e
          })),
          onDefaultValueChange: Te,
          onBack: () => E(
            (e) => e && {
              ...e,
              step: 2
            }
          ),
          onNext: Ve,
          nextDisabled: !me
        }
      ),
      a.step === 3 && !_(a.control.type) && /* @__PURE__ */ t(
        $e,
        {
          control: a.control,
          scope: s,
          createdReference: qe,
          explorerContent: /* @__PURE__ */ d($, { children: [
            "Open the card and use",
            " ",
            /* @__PURE__ */ t("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: () => E(
            (e) => e && {
              ...e,
              step: 2
            }
          ),
          onCancel: fe,
          onDone: _e
        }
      ),
      a.step === 4 && _(a.control.type) && /* @__PURE__ */ t(
        $e,
        {
          control: a.control,
          scope: s,
          createdReference: qe,
          summaryDetail: /* @__PURE__ */ d($, { children: [
            "Used by ",
            (a.selectedCardIds || []).length,
            " selected card",
            (a.selectedCardIds || []).length === 1 ? "" : "s"
          ] }),
          explorerContent: /* @__PURE__ */ t($, { 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: () => E(
            (e) => e && {
              ...e,
              step: 3
            }
          ),
          onCancel: fe,
          onDone: _e
        }
      )
    ] }) : null,
    !a && r.length === 0 ? /* @__PURE__ */ t("div", { className: "rounded-md border border-dashed p-3 text-sm text-muted-foreground", children: u }) : null,
    !a && r.map((e) => {
      const n = dt(e), l = y == null ? void 0 : y[e.id], c = ye({
        control: e,
        defaultValues: i
      }), C = pe === e.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 /* @__PURE__ */ d(
        "div",
        {
          className: "rounded-control border border-border/60",
          children: [
            /* @__PURE__ */ d("div", { className: "flex items-center gap-2 px-3 py-2.5", children: [
              /* @__PURE__ */ d(
                "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: () => M(
                    (f) => f === e.id ? null : e.id
                  ),
                  children: [
                    C ? /* @__PURE__ */ t(Be, { className: "mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" }) : /* @__PURE__ */ t(je, { className: "mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" }),
                    /* @__PURE__ */ d("div", { className: "min-w-0", children: [
                      /* @__PURE__ */ d("div", { className: "flex flex-wrap items-center gap-1.5", children: [
                        /* @__PURE__ */ t("span", { className: "truncate text-[13px] font-medium text-foreground", children: e.label || "Untitled control" }),
                        /* @__PURE__ */ t("span", { className: k, children: Xe(e.type).label }),
                        /* @__PURE__ */ t("span", { className: k, children: Je(s, e.placement) })
                      ] }),
                      /* @__PURE__ */ d("div", { className: "mt-0.5 text-[12px] text-muted-foreground", children: [
                        Ze(e),
                        l != null && l.status ? ` · ${l.status}` : ""
                      ] })
                    ] })
                  ]
                }
              ),
              /* @__PURE__ */ t(
                V,
                {
                  type: "button",
                  size: "xs",
                  variant: "ghost",
                  className: "h-7 w-7 shrink-0 p-0 text-muted-foreground",
                  onClick: () => ut(e.id),
                  "aria-label": `Remove ${e.label}`,
                  children: /* @__PURE__ */ t(jt, { className: "h-3.5 w-3.5" })
                }
              )
            ] }),
            C && /* @__PURE__ */ d("div", { className: "space-y-4 border-t border-border/60 px-3 py-3", children: [
              /* @__PURE__ */ d("div", { className: "grid gap-3 md:grid-cols-3", children: [
                /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
                  /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Label" }),
                  /* @__PURE__ */ t(
                    We,
                    {
                      size: "xs",
                      value: e.label,
                      placeholder: "Control label",
                      onChange: (f) => W(e.id, (w) => ({
                        ...w,
                        label: f.target.value
                      }))
                    }
                  )
                ] }),
                /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
                  /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Type" }),
                  /* @__PURE__ */ d(
                    xe,
                    {
                      value: e.type,
                      onValueChange: (f) => {
                        const w = et({
                          controls: r,
                          controlId: e.id,
                          nextType: f,
                          defaultValues: i
                        });
                        A(w.controls, w.defaultValues);
                      },
                      children: [
                        /* @__PURE__ */ t(ve, { size: "xs", children: /* @__PURE__ */ t(Ce, {}) }),
                        /* @__PURE__ */ t(ke, { children: re(e.type).map(
                          (f) => /* @__PURE__ */ t(
                            Ne,
                            {
                              value: f.value,
                              children: f.label
                            },
                            f.value
                          )
                        ) })
                      ]
                    }
                  )
                ] }),
                /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
                  /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Show in" }),
                  /* @__PURE__ */ d(
                    xe,
                    {
                      value: e.placement || G,
                      onValueChange: (f) => W(e.id, (w) => ({
                        ...w,
                        placement: f
                      })),
                      children: [
                        /* @__PURE__ */ t(ve, { size: "xs", children: /* @__PURE__ */ t(Ce, {}) }),
                        /* @__PURE__ */ t(ke, { children: Y.map((f) => /* @__PURE__ */ t(Ne, { value: f.value, children: f.label }, f.value)) })
                      ]
                    }
                  )
                ] })
              ] }),
              _(e.type) ? /* @__PURE__ */ t(
                Ut,
                {
                  type: e.type,
                  participants: ze(
                    b || [],
                    e.type
                  ).map((f) => ({
                    cardId: f.cardId,
                    title: f.title,
                    choiceCount: e.type === "field_selector" ? f.fieldChoices.length : f.metricChoices.length
                  })),
                  selectedCardIds: (m == null ? void 0 : m[e.id]) || [],
                  onChange: (f) => {
                    const w = Ee({
                      control: e,
                      nextSelectedCardIds: f
                    }), F = r.map(
                      (I) => I.id === e.id ? w.nextControl : I
                    );
                    A(F, i, {
                      type: "update",
                      cardBindingsById: w.cardBindingsById
                    });
                  }
                }
              ) : null,
              /* @__PURE__ */ t(
                lt,
                {
                  control: e,
                  availableFieldChoices: n.fields,
                  availableMetricChoices: n.metrics,
                  previewControlDefinitions: r,
                  previewControlValues: he,
                  controlLifecycle: "edit",
                  onChange: (f) => W(e.id, () => f)
                }
              ),
              e.initialValueStrategy === "saved_default" && /* @__PURE__ */ t(
                nt,
                {
                  control: e,
                  value: c,
                  allowNoDefault: !0,
                  allowCardDefault: !0,
                  previewControlDefinitions: r,
                  previewControlValues: he,
                  onChange: (f) => {
                    const w = r.map(
                      (F) => F.id === e.id ? { ...F, defaultValue: f } : F
                    );
                    if (e.source.kind === "manual" && (T.current[e.id] = f), i) {
                      const F = { ...i };
                      f === void 0 ? delete F[e.id] : F[e.id] = f, A(w, F);
                      return;
                    }
                    A(w, void 0);
                  }
                }
              ),
              /* @__PURE__ */ d(
                Ht,
                {
                  open: ae === e.id,
                  onOpenChange: (f) => X(f ? e.id : null),
                  children: [
                    /* @__PURE__ */ t($t, { asChild: !0, children: /* @__PURE__ */ d(
                      V,
                      {
                        type: "button",
                        variant: "ghost",
                        size: "xs",
                        className: "-ml-2 gap-1 text-muted-foreground hover:text-foreground",
                        children: [
                          ae === e.id ? /* @__PURE__ */ t(Be, { className: "h-3.5 w-3.5" }) : /* @__PURE__ */ t(je, { className: "h-3.5 w-3.5" }),
                          "Advanced"
                        ]
                      }
                    ) }),
                    /* @__PURE__ */ d(Kt, { className: "space-y-3 pt-2", children: [
                      /* @__PURE__ */ d("div", { className: "grid gap-3 md:grid-cols-2", children: [
                        /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
                          /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Variable name" }),
                          /* @__PURE__ */ t(
                            Pt,
                            {
                              controlId: e.id,
                              onCommit: (f) => ct(e.id, f)
                            },
                            e.id
                          )
                        ] }),
                        /* @__PURE__ */ d("div", { className: "flex flex-col gap-1.5", children: [
                          /* @__PURE__ */ t(P, { className: "text-[10px] font-medium uppercase tracking-[0.08em] text-muted-foreground", children: "Initial value" }),
                          /* @__PURE__ */ d(
                            xe,
                            {
                              value: e.initialValueStrategy,
                              onValueChange: (f) => W(e.id, (w) => ({
                                ...w,
                                initialValueStrategy: f
                              })),
                              children: [
                                /* @__PURE__ */ t(ve, { size: "xs", children: /* @__PURE__ */ t(Ce, {}) }),
                                /* @__PURE__ */ t(ke, { children: It.map((f) => /* @__PURE__ */ t(
                                  Ne,
                                  {
                                    value: f.value,
                                    children: f.label
                                  },
                                  f.value
                                )) })
                              ]
                            }
                          )
                        ] })
                      ] }),
                      /* @__PURE__ */ t(
                        Yt,
                        {
                          control: e,
                          onChange: (f) => W(e.id, () => f)
                        }
                      )
                    ] })
                  ]
                }
              )
            ] })
          ]
        },
        e.id
      );
    }),
    !a && /* @__PURE__ */ d(
      V,
      {
        type: "button",
        variant: "outline",
        size: "xs",
        onClick: pt,
        children: [
          /* @__PURE__ */ t(Gt, { className: "h-3.5 w-3.5" }),
          "Add control"
        ]
      }
    )
  ] });
}
function ka(s) {
  const {
    open: r,
    onOpenChange: o,
    initialCreateFlowActive: i = !1,
    dismissMode: p = "passive"
  } = s, u = r !== void 0, [h, y] = Q(!1), [g, v] = Q(
    i
  ), b = u ? r : h, m = O(
    (N) => {
      !N && g && p === "passive" || (u || y(N), N || v(!1), o == null || o(N));
    },
    [u, g, p, o]
  ), x = O(() => {
    u || y(!1), v(!1), o == null || o(!1);
  }, [u, o]);
  de(() => {
    b || v(!1);
  }, [b]);
  const S = O(
    (N) => {
      g && N.preventDefault();
    },
    [g]
  );
  return {
    forceClose: x,
    handleOpenChange: m,
    isCreateFlowActive: g,
    preventPassiveDismissDuringCreate: S,
    resolvedOpen: b,
    setIsCreateFlowActive: v
  };
}
export {
  ua as C,
  ga as E,
  Zt as L,
  pa as R,
  ma as S,
  ha as V,
  xa as a,
  ba as b,
  ya as c,
  Wt as d,
  Ca as e,
  fa as f,
  ot as g,
  va as h,
  ka as u
};
