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