import { useMemo as m } from "react";
const w = ({
  items: f = [],
  groups: s,
  searchQuery: a,
  filterFn: n,
  expandedGroups: L
}) => {
  const r = !!s && s.length > 0, l = m(() => r ? s.flatMap((t) => t.items) : f, [f, s, r]), c = m(() => {
    const t = a.trim();
    if (!t)
      return l;
    const i = t.toLowerCase(), o = (e) => e.label.toLowerCase().includes(i) || e.description?.toLowerCase().includes(i);
    return l.filter(
      (e) => n ? n(e, t) : o(e)
    );
  }, [l, a, n]), u = m(() => {
    const t = a.trim();
    if (!r || !t)
      return s || [];
    const i = t.toLowerCase(), o = (e) => e.label.toLowerCase().includes(i) || e.description?.toLowerCase().includes(i);
    return (s || []).map((e) => ({
      ...e,
      items: e.items.filter(
        (d) => n ? n(d, t) : o(d)
      )
    })).filter((e) => e.items.length > 0);
  }, [s, r, a, n]), h = m(() => {
    if (r) {
      const t = [];
      return u.forEach((i, o) => {
        const e = L.has(i.id);
        t.push({
          type: "group",
          data: i,
          index: t.length,
          groupIndex: o
        }), e && i.items.forEach((d) => {
          t.push({
            type: "item",
            data: d,
            index: t.length,
            groupIndex: o
          });
        });
      }), t;
    }
    return c.map((t, i) => ({
      type: "item",
      data: t,
      index: i
    }));
  }, [r, u, c, L]);
  return {
    isGrouped: r,
    normalizedItems: l,
    filteredItems: c,
    filteredGroups: u,
    virtualizedItems: h
  };
};
export {
  w as useCheckableListItems
};
