import { jsxs as L, Fragment as O, jsx as e } from "react/jsx-runtime";
import { p as Q, u as W, a as Y, ce as X, cf as Z, B as S, cg as J, b2 as ee, c as oe, ch as re, ci as te, as as ne, at as se, cj as ae, ck as le, cl as $, T as R, aY as ie, z as E, br as ce, cm as H, b4 as de, aL as ue, bd as N, E as pe, cn as he, bQ as fe, bT as ge, co as be, aw as me, l as ve, q as ye, bZ as xe, cp as we, b7 as Ee, ag as Ce, ca as Se, Q as A, aR as Ie, cb as ke, C as Te, A as Le, b_ as ze, bo as Re, c0 as De, c1 as Ge } from "./index-Dmwc5xdZ.mjs";
import { useReducer as He, memo as _e, useState as M, useRef as Ne, useCallback as z, useMemo as P, useTransition as Pe } from "react";
import { T as Oe, W as j, a as Ve, P as $e } from "./WarningsHoverCardContent-BU5ho7PT.mjs";
var B = { root: "m_f0824112", description: "m_57492dcc", section: "m_690090b5", label: "m_1f6ac4c4", body: "m_f07af9d2", children: "m_e17b862f", chevron: "m_1fd8a00b" };
const Ae = {}, je = oe(
  (r, { variant: l, color: s, childrenOffset: c, autoContrast: d }) => {
    const n = r.variantColorResolver({
      color: s || r.primaryColor,
      theme: r,
      variant: l || "light",
      autoContrast: d
    });
    return {
      root: {
        "--nl-bg": s || l ? n.background : void 0,
        "--nl-hover": s || l ? n.hover : void 0,
        "--nl-color": s || l ? n.color : void 0
      },
      children: {
        "--nl-offset": re(c)
      }
    };
  }
), D = Q((r, l) => {
  const s = W("NavLink", Ae, r), {
    classNames: c,
    className: d,
    style: n,
    styles: p,
    unstyled: u,
    vars: g,
    opened: b,
    defaultOpened: f,
    onChange: x,
    children: m,
    onClick: I,
    active: o,
    disabled: t,
    leftSection: a,
    rightSection: h,
    label: w,
    description: i,
    disableRightSectionRotation: v,
    noWrap: G,
    childrenOffset: Ze,
    onKeyDown: F,
    autoContrast: Je,
    mod: U,
    ...q
  } = s, y = Y({
    name: "NavLink",
    props: s,
    classes: B,
    className: d,
    style: n,
    classNames: c,
    styles: p,
    unstyled: u,
    vars: g,
    varsResolver: je
  }), [k, V] = X({
    value: b,
    defaultValue: f,
    finalValue: !1,
    onChange: x
  }), T = !!m, K = (C) => {
    I?.(C), T && (C.preventDefault(), V(!k));
  };
  return /* @__PURE__ */ L(O, { children: [
    /* @__PURE__ */ L(
      Z,
      {
        ...y("root"),
        component: "a",
        ref: l,
        onClick: K,
        onKeyDown: (C) => {
          F?.(C), C.nativeEvent.code === "Space" && T && (C.preventDefault(), V(!k));
        },
        unstyled: u,
        mod: [{ disabled: t, active: o, expanded: k }, U],
        ...q,
        children: [
          a && /* @__PURE__ */ e(S, { component: "span", ...y("section"), mod: { position: "left" }, children: a }),
          /* @__PURE__ */ L(S, { ...y("body"), mod: { "no-wrap": G }, children: [
            /* @__PURE__ */ e(S, { component: "span", ...y("label"), children: w }),
            /* @__PURE__ */ e(S, { component: "span", mod: { active: o }, ...y("description"), children: i })
          ] }),
          (T || h !== void 0) && /* @__PURE__ */ e(
            S,
            {
              ...y("section"),
              component: "span",
              mod: { rotate: k && !v, position: "right" },
              children: T ? h !== void 0 ? h : /* @__PURE__ */ e(J, { ...y("chevron") }) : h
            }
          )
        ]
      }
    ),
    T && /* @__PURE__ */ e(ee, { in: k, ...y("collapse"), children: /* @__PURE__ */ e("div", { ...y("children"), children: m }) })
  ] });
});
D.classes = B;
D.displayName = "@mantine/core/NavLink";
function Me(r = [!1, !0]) {
  const [[l], s] = He((c, d) => {
    const n = d instanceof Function ? d(c[0]) : d, p = Math.abs(c.indexOf(n));
    return c.slice(p).concat(c.slice(0, p));
  }, r);
  return [l, s];
}
const Be = async (r, l) => se({
  url: `/projects/${r}/explores?filtered=true`,
  method: "GET",
  body: void 0
}), Fe = (r, l, s) => {
  const c = te();
  return ne({
    queryKey: ["tables", r, "filtered"],
    queryFn: () => Be(r),
    onError: (n) => c(n),
    retry: !1,
    enabled: !!r,
    ...s
  });
}, Ue = ({
  explore: r,
  query: l,
  onClick: s
}) => {
  const [c, d] = Me(), n = ae(r), p = !n && "warnings" in r ? r.warnings ?? [] : [], u = p.length > 0, g = n || u, b = n && r.errors.every(
    (m) => m.type === le.NO_DIMENSIONS_FOUND
  );
  let f;
  n ? f = b ? /* @__PURE__ */ e(
    de,
    {
      role: "button",
      href: "https://docs.lightdash.com/guides/how-to-create-dimensions",
      target: "_blank",
      rel: "noreferrer",
      children: /* @__PURE__ */ e(E, { icon: ue, color: "ldGray.7", size: "lg" })
    }
  ) : /* @__PURE__ */ e(E, { icon: N, size: "lg", color: "yellow.9" }) : u && (f = /* @__PURE__ */ e(E, { icon: N, size: "lg", color: "yellow.9" }));
  const x = /* @__PURE__ */ e(
    D,
    {
      role: "listitem",
      disabled: n,
      leftSection: /* @__PURE__ */ e(
        ie,
        {
          radius: "sm",
          bg: "ldGray.0",
          p: 0,
          shadow: "none",
          withBorder: !1,
          children: /* @__PURE__ */ e(
            E,
            {
              icon: ce,
              size: "md",
              color: "ldGray.7",
              stroke: 1.5
            }
          )
        }
      ),
      onClick: n ? void 0 : s,
      onMouseEnter: g ? void 0 : () => d(!0),
      onMouseLeave: g ? void 0 : () => d(!1),
      label: n ? /* @__PURE__ */ e(
        $,
        {
          component: R,
          highlight: l ?? "",
          truncate: !0,
          children: r.label
        }
      ) : /* @__PURE__ */ e(
        Oe,
        {
          label: r.label,
          description: r.description,
          showPreview: g ? !1 : c,
          closePreview: () => d(!1),
          offset: 0,
          children: /* @__PURE__ */ e(
            $,
            {
              component: R,
              highlight: l ?? "",
              truncate: !0,
              fz: "sm",
              fw: 500,
              c: "ldDark.8",
              children: r.label
            }
          )
        }
      ),
      rightSection: f
    }
  );
  return g ? /* @__PURE__ */ L(
    H,
    {
      withinPortal: !0,
      position: "right",
      withArrow: !0,
      radius: "md",
      shadow: "sm",
      children: [
        /* @__PURE__ */ e(H.Target, { children: /* @__PURE__ */ e(S, { children: x }) }),
        /* @__PURE__ */ e(H.Dropdown, { maw: 400, p: "xs", children: n ? /* @__PURE__ */ e(
          j,
          {
            type: "errors",
            warnings: r.errors
          }
        ) : /* @__PURE__ */ e(
          j,
          {
            type: "warnings",
            warnings: p
          }
        ) })
      ]
    }
  ) : x;
}, qe = ({ label: r, isExpanded: l, onToggle: s }) => /* @__PURE__ */ e(
  D,
  {
    opened: l,
    onClick: s,
    disableRightSectionRotation: !0,
    rightSection: /* @__PURE__ */ e(O, {}),
    leftSection: /* @__PURE__ */ e(
      E,
      {
        icon: l ? he : fe,
        size: 14,
        style: {
          margin: 1
        }
      }
    ),
    label: /* @__PURE__ */ e(pe, { children: /* @__PURE__ */ e(R, { fz: "sm", fw: 500, c: "ldGray.7", children: r }) })
  }
), _ = 40, Ke = 40, Qe = 32, We = 16, Ye = ({
  sortedGroupLabels: r,
  exploreGroupMap: l,
  defaultUngroupedExplores: s,
  customUngroupedExplores: c,
  searchQuery: d,
  onExploreClick: n
}) => {
  const [p, u] = M(
    /* @__PURE__ */ new Set()
  ), g = Ne(null), b = z((o) => {
    u((t) => {
      const a = new Set(t);
      return a.has(o) ? a.delete(o) : a.add(o), a;
    });
  }, []), f = P(() => {
    const o = [];
    let t = 0;
    for (const a of r) {
      const h = l[a] || [], w = p.has(a);
      if (o.push({
        type: "group-header",
        id: `group-${t++}`,
        label: a,
        explores: h,
        isExpanded: w
      }), w)
        for (const i of h)
          o.push({
            type: "explore",
            id: `explore-${t++}`,
            explore: i,
            groupLabel: a
          });
    }
    for (const a of s)
      o.push({
        type: "explore",
        id: `ungrouped-${t++}`,
        explore: a
      });
    if (c.length > 0) {
      o.push({
        type: "divider",
        id: `divider-${t++}`
      }), o.push({
        type: "section-header",
        id: `section-${t++}`,
        label: "Virtual Views"
      });
      for (const a of c)
        o.push({
          type: "explore",
          id: `virtual-${t++}`,
          explore: a
        });
    }
    return o;
  }, [
    r,
    l,
    s,
    c,
    p
  ]), x = z(
    (o) => {
      const t = f[o];
      if (!t) return _;
      switch (t.type) {
        case "group-header":
          return Ke;
        case "section-header":
          return Qe;
        case "divider":
          return We;
        case "explore":
          return _;
        default:
          return _;
      }
    },
    [f]
  ), m = ge({
    count: f.length,
    getScrollElement: () => g.current,
    estimateSize: x,
    overscan: 5
  }), I = z(
    (o) => {
      switch (o.type) {
        case "group-header":
          return /* @__PURE__ */ e(
            qe,
            {
              label: o.label,
              isExpanded: o.isExpanded,
              onToggle: () => b(o.label)
            },
            o.id
          );
        case "explore":
          return /* @__PURE__ */ e(
            "div",
            {
              style: {
                paddingLeft: o.groupLabel ? "16px" : "0px"
              },
              children: /* @__PURE__ */ e(
                Ue,
                {
                  explore: o.explore,
                  query: d,
                  onClick: () => n(o.explore)
                }
              )
            },
            o.id
          );
        case "divider":
          return /* @__PURE__ */ e(
            be,
            {
              size: 0.5,
              c: "ldGray.5",
              my: "xs"
            },
            o.id
          );
        case "section-header":
          return /* @__PURE__ */ e(
            R,
            {
              fw: 500,
              fz: "xs",
              c: "ldGray.6",
              mb: "xs",
              children: o.label
            },
            o.id
          );
        default:
          return null;
      }
    },
    [d, b, n]
  );
  return /* @__PURE__ */ e(
    "div",
    {
      ref: g,
      style: {
        height: "100%",
        overflow: "auto"
      },
      children: /* @__PURE__ */ e(
        "div",
        {
          style: {
            height: m.getTotalSize(),
            position: "relative"
          },
          children: m.getVirtualItems().map((o) => {
            const t = f[o.index];
            return t ? /* @__PURE__ */ e(
              "div",
              {
                style: {
                  position: "absolute",
                  top: 0,
                  left: 0,
                  width: "100%",
                  height: o.size,
                  transform: `translateY(${o.start}px)`
                },
                children: I(t)
              },
              o.key
            ) : null;
          })
        }
      )
    }
  );
}, Xe = _e(Ye), no = () => {
  const r = me(), l = ve(), s = ye(), [c, d] = M(""), [n] = xe(c, 300), [, p] = Pe(), u = Fe(s), { data: g } = we(), b = P(() => {
    const t = n ? n.toLowerCase() : "";
    if (u.data) {
      let a = Object.values(u.data);
      return t !== "" && (a = new Ee(Object.values(u.data), {
        keys: ["label"],
        ignoreLocation: !0,
        threshold: 0.3
      }).search(t).map((h) => h.item)), a;
    }
  }, [u.data, n]), [
    f,
    x,
    m,
    I
  ] = P(() => {
    if (b) {
      const t = {}, a = [], h = [];
      for (const i of b)
        i.groupLabel ? t[i.groupLabel] ? t[i.groupLabel].push(i) : t[i.groupLabel] = [i] : i.type === Ce.VIRTUAL ? h.push(i) : a.push(i);
      const w = Object.keys(t).sort(
        (i, v) => i.localeCompare(v)
      );
      for (const i of w)
        t[i].sort(
          (v, G) => v.label.localeCompare(G.label)
        );
      return a.sort((i, v) => i.label.localeCompare(v.label)), h.sort((i, v) => i.label.localeCompare(v.label)), [w, t, a, h];
    }
    return [[], {}, [], []];
  }, [b]), o = z(
    (t) => {
      p(() => {
        r({
          pathname: `/projects/${s}/tables/${t.name}`,
          search: l.search
        });
      });
    },
    [r, s, l.search, p]
  );
  return u.status === "loading" ? /* @__PURE__ */ e(Se, {}) : u.status === "error" ? /* @__PURE__ */ e(
    A,
    {
      icon: Ie,
      title: "Could not load explores"
    }
  ) : u.data ? /* @__PURE__ */ e(O, { children: /* @__PURE__ */ e(Ve, { children: /* @__PURE__ */ L(ke, { h: "100%", sx: { flexGrow: 1 }, children: [
    /* @__PURE__ */ e(
      Te,
      {
        I: "manage",
        this: Le("Explore", {
          organizationUuid: g?.organizationUuid,
          projectUuid: s
        }),
        children: /* @__PURE__ */ e(
          $e,
          {
            size: "md",
            items: [{ title: "Tables", active: !0 }]
          }
        )
      }
    ),
    /* @__PURE__ */ e(
      ze,
      {
        icon: /* @__PURE__ */ e(E, { icon: Ge }),
        rightSection: c ? /* @__PURE__ */ e(Re, { onClick: () => d(""), children: /* @__PURE__ */ e(E, { icon: De }) }) : null,
        placeholder: "Search tables",
        value: c,
        onChange: (t) => d(t.target.value)
      }
    ),
    /* @__PURE__ */ e(
      Xe,
      {
        sortedGroupLabels: f,
        exploreGroupMap: x,
        defaultUngroupedExplores: m,
        customUngroupedExplores: I,
        searchQuery: n,
        onExploreClick: o
      }
    )
  ] }) }) }) : /* @__PURE__ */ e(
    A,
    {
      icon: N,
      title: "Could not load explores"
    }
  );
};
export {
  no as default
};
//# sourceMappingURL=BasePanel-DEbHgaLu.mjs.map
