import { jsx as e, jsxs as n, Fragment as oe } from "react/jsx-runtime";
import { c as He, u as se, G as Ge, a as Z, B as O, f as te, N as be, V as $e, L as ie, a2 as de, X as ke, Z as We, _ as Xe, $ as Ye, a1 as Ze, az as Je } from "./switch-xcWLtWs0.js";
import Qe, { useState as b, useEffect as ee, useRef as qe } from "react";
import { ah as ue, aL as et, aM as tt, aI as ce, aN as nt, aO as at, aP as st, aQ as it, aR as Y, aS as ot, aT as rt, aU as lt, aV as xe, az as he, aW as dt, aX as ct, aY as Ne, aZ as we, a_ as ut, J as ye, A as Ce, a$ as ve, b0 as Se, b1 as Ee, b2 as Ve, b3 as Le, b4 as Ae, b5 as Oe, b6 as Te, b7 as Re, b8 as ht, b9 as pe, ba as pt, y as ae, u as mt, bb as Ue, c as Ie, d as ze, e as Me, f as Pe, g as Fe, bc as ft, o as je, bd as gt, be as De, bf as xt } from "./index-DZ3lq1Pk.js";
import "./index-CuHybtft.js";
import { u as Be } 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 Dt = He("FolderOpen", [
  [
    "path",
    {
      d: "m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2",
      key: "usdka0"
    }
  ]
]);
function Ke() {
  return /* @__PURE__ */ e("div", { className: "space-y-1.5 py-1", children: [...Array(5)].map((t, f) => /* @__PURE__ */ n(
    "div",
    {
      className: "flex animate-pulse items-start gap-3 rounded-xl border border-transparent px-2.5 py-2.5",
      children: [
        /* @__PURE__ */ e("div", { className: "mt-0.5 h-4 w-4 flex-shrink-0 rounded bg-muted/60" }),
        /* @__PURE__ */ n("div", { className: "flex-1 space-y-1.5", children: [
          /* @__PURE__ */ e("div", { className: "h-4 w-3/5 rounded bg-muted/60" }),
          /* @__PURE__ */ e("div", { className: "h-3 w-4/5 rounded bg-muted/40" })
        ] })
      ]
    },
    f
  )) });
}
const bt = (t) => {
  const f = se((o) => o.isVisualEditing), m = se((o) => o.selectedFrameId), r = Ge((o) => o.frame);
  return !!(f && m && (r == null ? void 0 : r.visualId) === t);
};
function Nt({
  visual: t,
  isDeleting: f,
  isDuplicating: m,
  onAddVisual: r,
  onOpenVisual: o,
  onEditDetails: h,
  onDeleteClick: x,
  onDuplicateVisual: i,
  onShareVisual: D,
  formatDate: N
}) {
  var z, I, P, F;
  const { selectVisual: l } = ue(), c = et(t.id), p = bt(t.id), A = se((d) => d.isVisualEditing), { isConsoleMode: U } = tt(), w = Qe.useRef(null), [S, E] = b(!1), [V, T] = b(!1);
  ee(() => {
    m ? (E(!0), T(!0)) : V && (E(!1), T(!1));
  }, [m, V]), ee(() => {
    p && w.current && w.current.scrollIntoView({
      behavior: "smooth",
      block: "center"
    });
  }, [p]);
  const C = () => {
    l(t), o == null || o(t);
  }, v = (d) => {
    d.currentTarget === d.target && (d.key === "Enter" || d.key === " ") && (d.preventDefault(), C());
  };
  return /* @__PURE__ */ n(
    "div",
    {
      ref: w,
      className: Z(
        "group flex cursor-pointer items-start gap-1 rounded-[5px] px-2 py-1.5 transition-colors hover:bg-muted/50 focus-within:bg-muted/50",
        p ? "bg-muted ring-1 ring-inset ring-foreground/20" : c && "bg-muted"
      ),
      onClick: C,
      children: [
        /* @__PURE__ */ n(
          "div",
          {
            role: "button",
            tabIndex: 0,
            className: "min-w-0 flex-1 cursor-pointer focus-visible:outline-none",
            onKeyDown: v,
            children: [
              /* @__PURE__ */ e(
                "p",
                {
                  className: "truncate text-[13px] font-medium leading-5 text-foreground",
                  title: t.title || "Untitled Visual",
                  children: t.title || "Untitled Visual"
                }
              ),
              /* @__PURE__ */ e(
                "p",
                {
                  className: "truncate text-[11px] leading-4 text-muted-foreground",
                  title: t.description || "No description",
                  children: (z = t.description) != null && z.trim() ? t.description : "No description"
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ e(
          "div",
          {
            className: Z(
              "flex shrink-0 items-center transition-opacity",
              c || p || S ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 group-focus-within:pointer-events-auto group-focus-within:opacity-100"
            ),
            onClick: (d) => d.stopPropagation(),
            children: /* @__PURE__ */ n("div", { className: "inline-flex h-7 items-stretch overflow-hidden rounded-[5px] border border-border/60 bg-background", children: [
              /* @__PURE__ */ n(
                O,
                {
                  size: "xs",
                  variant: "ghost",
                  className: "h-full rounded-none border-0 shadow-none hover:bg-accent/50",
                  disabled: A,
                  onClick: (d) => {
                    d.stopPropagation(), r(t);
                  },
                  "aria-label": `Add ${t.title || "visual"}`,
                  children: [
                    /* @__PURE__ */ e(ce, { className: "h-3.5 w-3.5" }),
                    "Add"
                  ]
                }
              ),
              /* @__PURE__ */ n(
                nt,
                {
                  modal: !1,
                  open: S,
                  onOpenChange: E,
                  children: [
                    /* @__PURE__ */ e(at, { asChild: !0, children: /* @__PURE__ */ e(
                      O,
                      {
                        size: "xs",
                        variant: "ghost",
                        className: "h-full w-7 rounded-none border-0 border-l border-border/60 px-0 shadow-none hover:bg-accent/50",
                        "aria-label": "Open visual actions",
                        children: /* @__PURE__ */ e(st, { className: "h-3.5 w-3.5" })
                      }
                    ) }),
                    /* @__PURE__ */ n(
                      it,
                      {
                        align: "end",
                        className: "z-[51] w-44 rounded-[6px] border border-border/60 p-1 shadow-sm",
                        children: [
                          /* @__PURE__ */ n(
                            Y,
                            {
                              onClick: (d) => {
                                d.stopPropagation(), r(t);
                              },
                              disabled: A,
                              children: [
                                /* @__PURE__ */ e(ce, { className: "h-3.5 w-3.5" }),
                                "Add"
                              ]
                            }
                          ),
                          /* @__PURE__ */ n(
                            Y,
                            {
                              onClick: (d) => {
                                d.stopPropagation(), o == null || o(t);
                              },
                              children: [
                                /* @__PURE__ */ e(Dt, { className: "h-3.5 w-3.5" }),
                                "Open"
                              ]
                            }
                          ),
                          /* @__PURE__ */ n(
                            Y,
                            {
                              onClick: (d) => {
                                d.preventDefault(), d.stopPropagation(), i(t.id);
                              },
                              disabled: m,
                              children: [
                                m ? /* @__PURE__ */ e(te, { className: "h-3.5 w-3.5 animate-spin" }) : /* @__PURE__ */ e(ot, { className: "h-3.5 w-3.5" }),
                                m ? "Duplicating…" : "Duplicate"
                              ]
                            }
                          ),
                          t.ownership.isOwner && /* @__PURE__ */ n(oe, { children: [
                            /* @__PURE__ */ n(
                              Y,
                              {
                                onClick: (d) => {
                                  d.stopPropagation(), h == null || h(t);
                                },
                                children: [
                                  /* @__PURE__ */ e(rt, { className: "h-3.5 w-3.5" }),
                                  "Edit details"
                                ]
                              }
                            ),
                            /* @__PURE__ */ n(
                              Y,
                              {
                                onClick: (d) => {
                                  d.stopPropagation(), D(t);
                                },
                                children: [
                                  /* @__PURE__ */ e(lt, { className: "h-3.5 w-3.5" }),
                                  "Share"
                                ]
                              }
                            ),
                            /* @__PURE__ */ e(xe, {}),
                            /* @__PURE__ */ n(
                              Y,
                              {
                                onClick: (d) => {
                                  d.stopPropagation(), x(t);
                                },
                                className: "text-destructive focus:bg-destructive/10 focus:text-destructive",
                                disabled: f,
                                children: [
                                  /* @__PURE__ */ e(he, { className: "h-3.5 w-3.5" }),
                                  f ? "Deleting…" : "Delete"
                                ]
                              }
                            )
                          ] }),
                          /* @__PURE__ */ e(xe, {}),
                          /* @__PURE__ */ n("div", { className: "space-y-1 px-2 py-1.5 text-[11px] text-muted-foreground", children: [
                            (((I = t.ownership.createdBy) == null ? void 0 : I.type) === "tenant" || ((P = t.ownership.createdBy) == null ? void 0 : P.type) === "organization" && U) && /* @__PURE__ */ n("div", { className: "flex items-center gap-1.5", children: [
                              /* @__PURE__ */ e(dt, { className: "h-3 w-3" }),
                              /* @__PURE__ */ e("span", { children: ((F = t.ownership.createdBy) == null ? void 0 : F.name) || "Unknown" })
                            ] }),
                            /* @__PURE__ */ n("div", { className: "flex items-center gap-1.5", children: [
                              /* @__PURE__ */ e(ct, { className: "h-3 w-3" }),
                              /* @__PURE__ */ e("span", { children: N(t.lastModified) })
                            ] })
                          ] })
                        ]
                      }
                    )
                  ]
                }
              )
            ] })
          }
        )
      ]
    }
  );
}
function wt({ searchQuery: t }) {
  const [f, m] = b(!1), [r, o] = b(
    null
  ), [h, x] = b(!1), [i, D] = b(null), [N, l] = b(null), [c, p] = b(
    null
  ), { handleEditLibraryVisual: A, handleAddLibraryVisual: U } = Be(), {
    resources: w,
    isLoading: S,
    isError: E,
    updateResource: V,
    deleteResource: T,
    duplicateResource: C,
    isDeleting: v
  } = Ne(() => {
    x(!1), D(null);
  }), z = w.filter(
    (s) => {
      var R, u;
      return ((R = s == null ? void 0 : s.title) == null ? void 0 : R.toLowerCase().includes(t.toLowerCase())) || ((u = s == null ? void 0 : s.description) == null ? void 0 : u.toLowerCase().includes(t.toLowerCase()));
    }
  ), I = (s) => new Date(s).toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    year: "numeric"
  }), P = (s) => {
    D(s), x(!0);
  }, F = () => {
    i && T(i.id);
  }, {
    startUpdating: d,
    stopUpdating: $,
    isUpdating: B
  } = we(), L = (s, R, u) => {
    var ne;
    const j = Object.fromEntries(
      Object.entries(R).filter(([, M]) => M !== void 0)
    );
    if (Object.keys(j).length === 0) {
      (ne = u == null ? void 0 : u.onSuccess) == null || ne.call(u);
      return;
    }
    const y = Object.keys(j), _ = y.length === 1 ? y[0] : void 0;
    d(s, _), V(
      {
        resourceId: s,
        data: j
      },
      {
        onSuccess: () => {
          var M;
          $(s, _), (M = u == null ? void 0 : u.onSuccess) == null || M.call(u);
        },
        onError: () => {
          var M;
          $(s, _), (M = u == null ? void 0 : u.onError) == null || M.call(u);
        }
      }
    );
  }, k = (s) => {
    l(s), C(s, {
      onSettled: () => l(null)
    });
  }, K = (s) => {
    A(s);
  }, W = (s) => {
    U(s);
  }, X = (s) => {
    o(s), m(!0);
  }, J = (s) => {
    p(s);
  }, Q = () => {
    p(null);
  }, q = ({
    title: s,
    description: R
  }) => {
    if (!c)
      return;
    const u = {};
    s !== c.title && (u.title = s), (c.description || "") !== R && (u.description = R), L(c.id, u, {
      onSuccess: () => {
        p(null);
      }
    });
  };
  return S ? /* @__PURE__ */ e(Ke, {}) : E ? /* @__PURE__ */ e(be, { className: "w-full flex-1 pr-2", children: /* @__PURE__ */ n("div", { className: "flex flex-col items-center gap-1.5 px-4 py-10 text-center", children: [
    /* @__PURE__ */ e("p", { className: "text-[12px] text-destructive", children: "Failed to load visuals" }),
    /* @__PURE__ */ e(
      O,
      {
        variant: "outline",
        size: "xs",
        onClick: () => window.location.reload(),
        children: "Retry"
      }
    )
  ] }) }) : /* @__PURE__ */ n("div", { className: "flex h-full flex-1 flex-col", children: [
    /* @__PURE__ */ e("div", { className: "flex flex-1 basis-0 flex-col gap-0.5 overflow-y-auto", children: z.length === 0 ? /* @__PURE__ */ n("div", { className: "flex flex-col items-center gap-1.5 px-4 py-10 text-center", children: [
      /* @__PURE__ */ e(ut, { className: "h-5 w-5 text-muted-foreground/60" }),
      /* @__PURE__ */ e("h3", { className: "text-[13px] font-medium text-foreground", children: t ? "No visuals found" : "No visuals yet" }),
      /* @__PURE__ */ e("p", { className: "text-[12px] text-muted-foreground", children: t ? "Try adjusting your search." : 'Click "New" above to add your first visual.' })
    ] }) : z.map((s) => /* @__PURE__ */ e(
      Nt,
      {
        visual: s,
        onAddVisual: W,
        onOpenVisual: K,
        onEditDetails: J,
        onDeleteClick: P,
        onDuplicateVisual: k,
        onShareVisual: X,
        isDeleting: v,
        isDuplicating: N === s.id,
        formatDate: I
      },
      s.id
    )) }),
    f && r && /* @__PURE__ */ e(
      ye,
      {
        resource: r,
        resourceType: Ce.VISUAL,
        onClose: () => {
          m(!1), o(null);
        }
      }
    ),
    c && /* @__PURE__ */ e(
      ve,
      {
        open: !!c,
        resourceLabel: "Visual",
        initialTitle: c.title || "",
        initialDescription: c.description || "",
        onClose: Q,
        onSave: q,
        isSaving: B(c.id)
      }
    ),
    /* @__PURE__ */ e(
      Se,
      {
        open: h,
        onOpenChange: (s) => {
          !s && v || (x(s), s || D(null));
        },
        children: /* @__PURE__ */ n(Ee, { children: [
          /* @__PURE__ */ n(Ve, { children: [
            /* @__PURE__ */ e(Le, { children: "Delete Visual" }),
            /* @__PURE__ */ n(Ae, { children: [
              'Are you sure you want to delete "',
              i == null ? void 0 : i.title,
              '"? This action cannot be undone.'
            ] })
          ] }),
          /* @__PURE__ */ n(Oe, { children: [
            /* @__PURE__ */ e(Te, { disabled: v, children: "Cancel" }),
            /* @__PURE__ */ n(
              Re,
              {
                onClick: (s) => {
                  s.preventDefault(), s.stopPropagation(), F();
                },
                disabled: v,
                children: [
                  v ? /* @__PURE__ */ e(te, { className: "h-3.5 w-3.5 animate-spin" }) : /* @__PURE__ */ e(he, { className: "h-3.5 w-3.5" }),
                  v ? "Deleting…" : "Delete"
                ]
              }
            )
          ] })
        ] })
      }
    )
  ] });
}
function yt({
  dashboard: t,
  isDeleting: f,
  isDuplicating: m,
  onLoadDashboard: r,
  onEditDetails: o,
  onDeleteClick: h,
  onDuplicateDashboard: x,
  onShareDashboard: i,
  formatDate: D,
  onDashboardItemClick: N
}) {
  var V, T;
  const { selectDashboard: l } = ue(), c = ht(t.id), { userContext: p, isLoading: A } = pe(), U = ((V = p == null ? void 0 : p.permissions) == null ? void 0 : V.canEdit) ?? !1, w = A || !U, S = () => {
    l(t), r(t.id), N == null || N(t.id);
  }, E = (C) => {
    C.currentTarget === C.target && (C.key === "Enter" || C.key === " ") && (C.preventDefault(), S());
  };
  return /* @__PURE__ */ n(
    "div",
    {
      className: Z(
        "group flex cursor-pointer items-start gap-1 rounded-[5px] px-2 py-1.5 transition-colors hover:bg-muted/50 focus-within:bg-muted/50",
        c && "bg-muted"
      ),
      onClick: S,
      children: [
        /* @__PURE__ */ n(
          "div",
          {
            role: "button",
            tabIndex: 0,
            className: "min-w-0 flex-1 cursor-pointer focus-visible:outline-none",
            onKeyDown: E,
            children: [
              /* @__PURE__ */ e(
                "p",
                {
                  className: "truncate text-[13px] font-medium leading-5 text-foreground",
                  title: t.title || "Untitled Dashboard",
                  children: t.title || "Untitled Dashboard"
                }
              ),
              /* @__PURE__ */ e(
                "p",
                {
                  className: "truncate text-[11px] leading-4 text-muted-foreground",
                  title: t.description || "No description",
                  children: (T = t.description) != null && T.trim() ? t.description : "No description"
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ e(
          "div",
          {
            className: Z(
              "shrink-0 transition-opacity",
              c ? "opacity-100" : "opacity-0 group-hover:opacity-100 group-focus-within:opacity-100"
            ),
            children: /* @__PURE__ */ e(
              pt,
              {
                dashboard: t,
                isDeleting: f,
                isDuplicating: m,
                shouldHideActions: w,
                onEditDetails: o,
                onDeleteClick: h,
                onDuplicateDashboard: x,
                onShareDashboard: i,
                formatDate: D
              }
            )
          }
        )
      ]
    }
  );
}
function Ct({ onRetry: t }) {
  return /* @__PURE__ */ e(be, { className: "w-full flex-1 pr-2", children: /* @__PURE__ */ n("div", { className: "rounded-xl border border-dashed border-border/60 bg-muted/20 px-4 py-8 text-center text-muted-foreground", children: [
    /* @__PURE__ */ e("p", { className: "text-sm text-destructive", children: "Failed to load dashboards" }),
    /* @__PURE__ */ e(O, { variant: "outline", size: "sm", className: "mt-2", onClick: t, children: "Retry" })
  ] }) });
}
function vt(t) {
  var me;
  const { searchQuery: f, onLoadDashboard: m, onEditDashboard: r, onDashboardItemClick: o } = t, [h, x] = b(!1), [i, D] = b(
    null
  ), [N, l] = b(!1), [c, p] = b(null), [A, U] = b(null), [w, S] = b(null), E = ae(
    (a) => a.lastSelectedDashboard
  ), V = ae(
    (a) => a.selectedDashboard
  ), T = ae(
    (a) => a.actions.selectDashboard
  ), C = ae(
    (a) => a.actions.clearSelectedDashboard
  ), { tokenProps: v, id: z } = mt(), {
    resources: I,
    isLoading: P,
    isError: F,
    updateResource: d,
    deleteResource: $,
    duplicateResource: B,
    isDeleting: L
  } = Ue(() => {
    l(!1);
    const a = c == null ? void 0 : c.id;
    p(null), (V == null ? void 0 : V.id) === a && (!(v != null && v.dashboard_id) && !z ? C() : E && E.id !== a && T(E));
  }), { userContext: k } = pe(), K = ((me = k == null ? void 0 : k.permissions) == null ? void 0 : me.canCreateDashboard) ?? !1, W = I.filter(
    (a) => {
      var H, g;
      return ((H = a == null ? void 0 : a.title) == null ? void 0 : H.toLowerCase().includes(f.toLowerCase())) || ((g = a == null ? void 0 : a.description) == null ? void 0 : g.toLowerCase().includes(f.toLowerCase()));
    }
  ), X = (a) => new Date(a).toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    year: "numeric"
  }), J = (a) => {
    m == null || m(a);
  }, Q = (a) => {
    p(a), l(!0);
  }, q = () => {
    c && $(c.id);
  }, s = (a) => {
    U(a), B(a, {
      onSettled: () => U(null)
    });
  }, {
    startUpdating: R,
    stopUpdating: u,
    isUpdating: j
  } = we(), y = (a, H, g) => {
    var ge;
    const re = Object.fromEntries(
      Object.entries(H).filter(([, G]) => G !== void 0)
    );
    if (Object.keys(re).length === 0) {
      (ge = g == null ? void 0 : g.onSuccess) == null || ge.call(g);
      return;
    }
    const fe = Object.keys(re), le = fe.length === 1 ? fe[0] : void 0;
    R(a, le), d(
      {
        resourceId: a,
        data: re
      },
      {
        onSuccess: () => {
          var G;
          u(a, le), (G = g == null ? void 0 : g.onSuccess) == null || G.call(g);
        },
        onError: () => {
          var G;
          u(a, le), (G = g == null ? void 0 : g.onError) == null || G.call(g);
        }
      }
    );
  }, _ = (a) => {
    D(a), x(!0);
  }, ne = (a) => {
    r == null || r(a.id), S(a);
  }, M = () => {
    S(null);
  }, _e = ({
    title: a,
    description: H
  }) => {
    if (!w)
      return;
    const g = {};
    a !== w.title && (g.title = a), (w.description || "") !== H && (g.description = H), y(w.id, g, {
      onSuccess: () => {
        S(null);
      }
    });
  };
  return P ? /* @__PURE__ */ e(Ke, {}) : F ? /* @__PURE__ */ e(Ct, { onRetry: () => window.location.reload() }) : /* @__PURE__ */ n("div", { className: "flex h-full flex-1 flex-col", children: [
    /* @__PURE__ */ e("div", { className: "flex flex-1 basis-0 flex-col gap-0.5 overflow-y-auto", children: W.length === 0 ? /* @__PURE__ */ n("div", { className: "flex flex-col items-center gap-1.5 px-4 py-10 text-center", children: [
      /* @__PURE__ */ e($e, { className: "h-5 w-5 text-muted-foreground/60" }),
      /* @__PURE__ */ e("h3", { className: "text-[13px] font-medium text-foreground", children: f ? "No dashboards found" : "No dashboards yet" }),
      /* @__PURE__ */ e("p", { className: "text-[12px] text-muted-foreground", children: f ? "Try adjusting your search." : K ? 'Click "New" above to add your first dashboard.' : "No dashboards have been shared with you yet." })
    ] }) : W.map((a) => /* @__PURE__ */ e(
      yt,
      {
        dashboard: a,
        isDeleting: L,
        isDuplicating: A === a.id,
        onLoadDashboard: J,
        onEditDetails: ne,
        onDeleteClick: Q,
        onDuplicateDashboard: s,
        onShareDashboard: _,
        formatDate: X,
        onDashboardItemClick: o
      },
      a.id
    )) }),
    h && i && /* @__PURE__ */ e(
      ye,
      {
        resource: i,
        resourceType: Ce.DASHBOARD,
        onClose: () => {
          x(!1), D(null);
        }
      }
    ),
    /* @__PURE__ */ e(
      Se,
      {
        open: N,
        onOpenChange: (a) => {
          !a && L || (l(a), a || p(null));
        },
        children: /* @__PURE__ */ n(Ee, { children: [
          /* @__PURE__ */ n(Ve, { children: [
            /* @__PURE__ */ e(Le, { children: "Delete Dashboard" }),
            /* @__PURE__ */ n(Ae, { children: [
              'Are you sure you want to delete "',
              c == null ? void 0 : c.title,
              '"? This action cannot be undone.'
            ] })
          ] }),
          /* @__PURE__ */ n(Oe, { children: [
            /* @__PURE__ */ e(Te, { disabled: L, children: "Cancel" }),
            /* @__PURE__ */ n(
              Re,
              {
                onClick: (a) => {
                  a.preventDefault(), a.stopPropagation(), q();
                },
                disabled: L,
                children: [
                  L ? /* @__PURE__ */ e(te, { className: "h-3.5 w-3.5 animate-spin" }) : /* @__PURE__ */ e(he, { className: "h-3.5 w-3.5" }),
                  L ? "Deleting…" : "Delete"
                ]
              }
            )
          ] })
        ] })
      }
    ),
    w && /* @__PURE__ */ e(
      ve,
      {
        open: !!w,
        resourceLabel: "Dashboard",
        initialTitle: w.title || "",
        initialDescription: w.description || "",
        onClose: M,
        onSave: _e,
        isSaving: j(w.id)
      }
    )
  ] });
}
function St(t, f = {}) {
  const { enabled: m = !0, eventType: r = "mousedown" } = f, o = qe(null);
  return ee(() => {
    const h = (x) => {
      const i = x.target;
      o.current && o.current.contains(i) || i instanceof Element && (i.closest("[data-radix-popper-content-wrapper]") || i.closest("[data-radix-select-content]") || i.closest("[data-radix-popover-content]") || i.closest("[data-radix-dropdown-menu-content]") || i.closest("[data-radix-tooltip-content]") || i.closest('[role="listbox"]') || i.closest('[role="option"]') || i.closest('[role="combobox"]')) || i instanceof Element && i.hasAttribute("data-radix-") || t();
    };
    return m && document.addEventListener(r, h), () => {
      document.removeEventListener(r, h);
    };
  }, [t, m, r]), o;
}
function Et({
  open: t,
  onOpenChange: f,
  onSubmit: m,
  isCreating: r
}) {
  const [o, h] = b(""), [x, i] = b("");
  ee(() => {
    t || (h(""), i(""));
  }, [t]);
  const D = () => {
    o.trim() && m({ title: o, description: x });
  }, N = (l) => {
    f(l);
  };
  return /* @__PURE__ */ e(Ie, { open: t, onOpenChange: N, children: /* @__PURE__ */ n(ze, { className: "sm:max-w-[480px]", children: [
    /* @__PURE__ */ n(Me, { children: [
      /* @__PURE__ */ e(Pe, { className: "text-[15px]", children: "New dashboard" }),
      /* @__PURE__ */ e(Fe, { className: "text-[13px]", children: "Give it a name and an optional description." })
    ] }),
    /* @__PURE__ */ n("div", { className: "space-y-3", children: [
      /* @__PURE__ */ n("div", { className: "flex flex-col gap-1.5", children: [
        /* @__PURE__ */ e(ie, { htmlFor: "title", className: "text-[13px] font-medium", children: "Title" }),
        /* @__PURE__ */ e(
          de,
          {
            id: "title",
            size: "xs",
            placeholder: "e.g. Sales analytics, User engagement",
            value: o,
            onChange: (l) => h(l.target.value),
            disabled: r,
            onKeyDown: (l) => {
              l.key === "Enter" && !l.shiftKey && (l.preventDefault(), D());
            }
          }
        )
      ] }),
      /* @__PURE__ */ n("div", { className: "flex flex-col gap-1.5", children: [
        /* @__PURE__ */ e(ie, { htmlFor: "description", className: "text-[13px] font-medium", children: "Description" }),
        /* @__PURE__ */ e(
          ft,
          {
            id: "description",
            placeholder: "Optional — briefly describe what this dashboard tracks",
            rows: 3,
            value: x,
            onChange: (l) => i(l.target.value),
            disabled: r,
            className: "resize-none text-[12px]"
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ n(je, { className: "gap-1.5", children: [
      /* @__PURE__ */ e(
        O,
        {
          type: "button",
          variant: "outline",
          size: "xs",
          onClick: () => N(!1),
          disabled: r,
          children: "Cancel"
        }
      ),
      /* @__PURE__ */ e(
        O,
        {
          type: "button",
          size: "xs",
          onClick: D,
          disabled: !o.trim() || r,
          children: r ? /* @__PURE__ */ n(oe, { children: [
            /* @__PURE__ */ e(te, { className: "h-3 w-3 animate-spin" }),
            "Creating…"
          ] }) : "Create dashboard"
        }
      )
    ] })
  ] }) });
}
function Vt({
  open: t,
  onOpenChange: f,
  onSubmit: m,
  isCreating: r
}) {
  const [o, h] = b(""), [x, i] = b("");
  ee(() => {
    t || (h(""), i(""));
  }, [t]);
  const D = () => {
    o.trim() && m({ title: o, description: x });
  }, N = (l) => {
    f(l);
  };
  return /* @__PURE__ */ e(Ie, { open: t, onOpenChange: N, children: /* @__PURE__ */ n(ze, { children: [
    /* @__PURE__ */ n(Me, { children: [
      /* @__PURE__ */ e(Pe, { children: "Create New Visual" }),
      /* @__PURE__ */ e(Fe, { children: "Add a new visual to your library. You can configure it after creation." })
    ] }),
    /* @__PURE__ */ n("div", { className: "space-y-4 py-4", children: [
      /* @__PURE__ */ n("div", { className: "space-y-2", children: [
        /* @__PURE__ */ e(ie, { htmlFor: "title", children: "Visual Title" }),
        /* @__PURE__ */ e(
          de,
          {
            id: "title",
            placeholder: "Enter visual title...",
            value: o,
            onChange: (l) => h(l.target.value),
            disabled: r,
            onKeyDown: (l) => {
              l.key === "Enter" && !l.shiftKey && (l.preventDefault(), D());
            }
          }
        )
      ] }),
      /* @__PURE__ */ n("div", { className: "space-y-2", children: [
        /* @__PURE__ */ e(ie, { htmlFor: "description", children: "Description (optional)" }),
        /* @__PURE__ */ e(
          de,
          {
            id: "description",
            placeholder: "Enter visual description...",
            value: x,
            onChange: (l) => i(l.target.value),
            disabled: r
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ n(je, { children: [
      /* @__PURE__ */ e(
        O,
        {
          variant: "outline",
          onClick: () => N(!1),
          disabled: r,
          children: "Cancel"
        }
      ),
      /* @__PURE__ */ e(
        O,
        {
          onClick: D,
          disabled: !o.trim() || r,
          children: r ? /* @__PURE__ */ n(oe, { children: [
            /* @__PURE__ */ e(te, { className: "mr-2 h-4 w-4 animate-spin" }),
            "Creating..."
          ] }) : "Create Visual"
        }
      )
    ] })
  ] }) });
}
function It({
  isOpen: t,
  onClose: f,
  className: m,
  onDashboardItemClick: r,
  variant: o = "inline"
}) {
  var u, j;
  const h = se(
    (y) => y.isDashboardEditing
  ), [x, i] = b(!1), [D, N] = b(""), [l, c] = b(!1), { currentUser: p, userContext: A } = pe(), U = ((u = A == null ? void 0 : A.permissions) == null ? void 0 : u.canCreateDashboard) ?? !1, { onCreateSuccess: w } = gt({
    onDashboardSelect: r
  }), { selectVisual: S } = ue(), { handleEditLibraryVisual: E } = Be(), {
    createResource: V,
    isCreating: T,
    refetch: C
  } = Ue(), { createResource: v, isCreating: z } = Ne(
    void 0,
    // onDeleteSuccess (not needed here)
    (y) => {
      i(!1), y && (S(y), E(y));
    }
  ), I = o === "overlay", P = h ? "New Visual" : "New", F = h ? "New Visual" : "New Dashboard", d = h ? "Search visuals..." : "Search dashboards...", $ = h ? "Collapse visuals panel" : "Collapse dashboard panel", B = ((p == null ? void 0 : p.name) ?? "").trim(), L = ((p == null ? void 0 : p.email) ?? "").trim(), K = B && B.toLowerCase() !== "undefined" ? B : L || "Current user", W = ((j = K.charAt(0)) == null ? void 0 : j.toUpperCase()) || "?", X = L && L.toLowerCase() !== "undefined" ? L : "", J = St(f, {
    enabled: t && I
  }), Q = Z(
    "flex h-full flex-col border-r border-border/60 bg-background",
    I ? Z(
      "absolute inset-y-0 left-0 z-[51] w-60 overflow-hidden shadow-lg transition-transform duration-300 ease-in-out",
      t ? "translate-x-0" : "pointer-events-none -translate-x-full"
    ) : "w-full max-w-[17rem] min-w-[11rem] overflow-hidden",
    m
  ), q = (y) => {
  }, s = (y) => {
    console.log("Editing dashboard:", y);
  }, R = (y) => {
    V(y, {
      onSuccess: (_) => {
        i(!1), w(_, C);
      }
    });
  };
  return /* @__PURE__ */ e(oe, { children: (!I || t) && /* @__PURE__ */ n("div", { ref: J, className: Q, children: [
    /* @__PURE__ */ e("div", { className: "border-b border-border/60 bg-background", children: l ? /* @__PURE__ */ n("div", { className: "flex h-10 items-center gap-1 px-3", children: [
      /* @__PURE__ */ e(De, { className: "h-3.5 w-3.5 shrink-0 text-muted-foreground" }),
      /* @__PURE__ */ e(
        "input",
        {
          autoFocus: !0,
          value: D,
          onChange: (y) => N(y.target.value),
          onKeyDown: (y) => {
            y.key === "Escape" && (N(""), c(!1));
          },
          placeholder: d,
          className: "h-7 min-w-0 flex-1 bg-transparent text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none",
          "aria-label": d
        }
      ),
      /* @__PURE__ */ e(
        O,
        {
          size: "xs",
          variant: "ghost",
          className: "w-7 shrink-0 px-0 text-muted-foreground hover:text-foreground",
          onClick: () => {
            N(""), c(!1);
          },
          "aria-label": "Close search",
          children: /* @__PURE__ */ e(ke, { className: "h-3.5 w-3.5" })
        }
      )
    ] }) : /* @__PURE__ */ n("div", { className: "flex h-10 items-center justify-between gap-2 px-3", children: [
      /* @__PURE__ */ e("h2", { className: "whitespace-nowrap text-[13px] font-medium text-foreground", children: h ? "Visuals" : "Dashboards" }),
      /* @__PURE__ */ n("div", { className: "flex items-center gap-0.5", children: [
        /* @__PURE__ */ e(
          O,
          {
            size: "xs",
            variant: "ghost",
            onClick: () => c(!0),
            className: "w-7 px-0 text-muted-foreground hover:text-foreground",
            "aria-label": "Search",
            children: /* @__PURE__ */ e(De, { className: "h-3.5 w-3.5" })
          }
        ),
        (h || U) && /* @__PURE__ */ n(
          O,
          {
            size: "xs",
            variant: "outline",
            onClick: () => i(!0),
            "aria-label": F,
            children: [
              /* @__PURE__ */ e(ce, { className: "h-3.5 w-3.5" }),
              P
            ]
          }
        ),
        /* @__PURE__ */ e(
          O,
          {
            size: "xs",
            variant: "ghost",
            className: "w-7 px-0 text-muted-foreground hover:text-foreground",
            onClick: f,
            "aria-label": $,
            children: /* @__PURE__ */ e(xt, { className: "h-3.5 w-3.5" })
          }
        )
      ] })
    ] }) }),
    /* @__PURE__ */ e("div", { className: "flex flex-1 flex-col overflow-auto px-1.5 py-2", children: h ? /* @__PURE__ */ e(wt, { searchQuery: D }) : /* @__PURE__ */ e(
      vt,
      {
        searchQuery: D,
        onLoadDashboard: q,
        onEditDashboard: s,
        onDashboardItemClick: r
      }
    ) }),
    p && /* @__PURE__ */ e(We, { children: /* @__PURE__ */ e("div", { className: "border-t border-border/60 px-3 py-2", children: /* @__PURE__ */ n(Xe, { children: [
      /* @__PURE__ */ e(Ye, { asChild: !0, children: /* @__PURE__ */ n("div", { className: "flex cursor-default items-center gap-2", children: [
        /* @__PURE__ */ e("div", { className: "flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-muted text-[10px] font-medium text-muted-foreground", children: W }),
        /* @__PURE__ */ e("span", { className: "truncate text-[12px] text-muted-foreground", children: K })
      ] }) }),
      /* @__PURE__ */ e(Ze, { side: "top", className: "max-w-xs", children: /* @__PURE__ */ n("div", { className: "space-y-0.5", children: [
        /* @__PURE__ */ e("div", { className: "font-medium", children: K }),
        X && /* @__PURE__ */ e("div", { className: "text-[11px] opacity-80", children: X }),
        p.role && /* @__PURE__ */ n("div", { className: "text-[11px] opacity-80", children: [
          "Role:",
          " ",
          Je(p.role.replace(/_/g, " "))
        ] })
      ] }) })
    ] }) }) }),
    h ? /* @__PURE__ */ e(
      Vt,
      {
        open: x,
        onOpenChange: i,
        onSubmit: v,
        isCreating: z
      }
    ) : /* @__PURE__ */ e(
      Et,
      {
        open: x,
        onOpenChange: i,
        onSubmit: R,
        isCreating: T
      }
    )
  ] }) });
}
export {
  It as ResourceManagementPanel
};
