import { useState as K, useMemo as x } from "react";
function g(s) {
  let { initialItems: n = [], initialSelectedKeys: d, getKey: i = (o) => {
    var m;
    return (m = o.id) !== null && m !== void 0 ? m : o.key;
  }, filter: r, initialFilterText: e = "" } = s, [l, t] = K({
    items: n,
    selectedKeys: d === "all" ? "all" : new Set(d || []),
    filterText: e
  }), f = x(() => r ? l.items.filter((o) => r(o, l.filterText)) : l.items, [
    l.items,
    l.filterText,
    r
  ]);
  return {
    ...l,
    items: f,
    ...S({
      getKey: i
    }, t),
    getItem(o) {
      return l.items.find((m) => i(m) === o);
    }
  };
}
function S(s, n) {
  let { cursor: d, getKey: i } = s;
  return {
    setSelectedKeys(r) {
      n((e) => ({
        ...e,
        selectedKeys: r
      }));
    },
    addKeysToSelection(r) {
      n((e) => e.selectedKeys === "all" ? e : r === "all" ? {
        ...e,
        selectedKeys: "all"
      } : {
        ...e,
        selectedKeys: /* @__PURE__ */ new Set([
          ...e.selectedKeys,
          ...r
        ])
      });
    },
    removeKeysFromSelection(r) {
      n((e) => {
        if (r === "all") return {
          ...e,
          selectedKeys: /* @__PURE__ */ new Set()
        };
        let l = e.selectedKeys === "all" ? new Set(e.items.map(i)) : new Set(e.selectedKeys);
        for (let t of r) l.delete(t);
        return {
          ...e,
          selectedKeys: l
        };
      });
    },
    setFilterText(r) {
      n((e) => ({
        ...e,
        filterText: r
      }));
    },
    insert(r, ...e) {
      n((l) => u(l, r, ...e));
    },
    insertBefore(r, ...e) {
      n((l) => {
        let t = l.items.findIndex((f) => i?.(f) === r);
        if (t === -1)
          if (l.items.length === 0) t = 0;
          else return l;
        return u(l, t, ...e);
      });
    },
    insertAfter(r, ...e) {
      n((l) => {
        let t = l.items.findIndex((f) => i?.(f) === r);
        if (t === -1)
          if (l.items.length === 0) t = 0;
          else return l;
        return u(l, t + 1, ...e);
      });
    },
    prepend(...r) {
      n((e) => u(e, 0, ...r));
    },
    append(...r) {
      n((e) => u(e, e.items.length, ...r));
    },
    remove(...r) {
      n((e) => {
        let l = new Set(r), t = e.items.filter((o) => !l.has(i(o))), f = "all";
        if (e.selectedKeys !== "all") {
          f = new Set(e.selectedKeys);
          for (let o of r) f.delete(o);
        }
        return d == null && t.length === 0 && (f = /* @__PURE__ */ new Set()), {
          ...e,
          items: t,
          selectedKeys: f
        };
      });
    },
    removeSelectedItems() {
      n((r) => {
        if (r.selectedKeys === "all") return {
          ...r,
          items: [],
          selectedKeys: /* @__PURE__ */ new Set()
        };
        let e = r.selectedKeys, l = r.items.filter((t) => !e.has(i(t)));
        return {
          ...r,
          items: l,
          selectedKeys: /* @__PURE__ */ new Set()
        };
      });
    },
    move(r, e) {
      n((l) => {
        let t = l.items.findIndex((m) => i(m) === r);
        if (t === -1) return l;
        let f = l.items.slice(), [o] = f.splice(t, 1);
        return f.splice(e, 0, o), {
          ...l,
          items: f
        };
      });
    },
    moveBefore(r, e) {
      n((l) => {
        let t = l.items.findIndex((m) => i(m) === r);
        if (t === -1) return l;
        let o = (Array.isArray(e) ? e : [
          ...e
        ]).map((m) => l.items.findIndex((c) => i(c) === m)).sort((m, c) => m - c);
        return y(l, o, t);
      });
    },
    moveAfter(r, e) {
      n((l) => {
        let t = l.items.findIndex((m) => i(m) === r);
        if (t === -1) return l;
        let o = (Array.isArray(e) ? e : [
          ...e
        ]).map((m) => l.items.findIndex((c) => i(c) === m)).sort((m, c) => m - c);
        return y(l, o, t + 1);
      });
    },
    update(r, e) {
      n((l) => {
        let t = l.items.findIndex((f) => i(f) === r);
        return t === -1 ? l : {
          ...l,
          items: [
            ...l.items.slice(0, t),
            e,
            ...l.items.slice(t + 1)
          ]
        };
      });
    }
  };
}
function u(s, n, ...d) {
  return {
    ...s,
    items: [
      ...s.items.slice(0, n),
      ...d,
      ...s.items.slice(n)
    ]
  };
}
function y(s, n, d) {
  d -= n.filter((e) => e < d).length;
  let i = n.map((e) => ({
    from: e,
    to: d++
  }));
  for (let e = 0; e < i.length; e++) {
    let l = i[e].from;
    for (let t = e; t < i.length; t++)
      i[t].from > l && i[t].from--;
  }
  for (let e = 0; e < i.length; e++) {
    let l = i[e];
    for (let t = i.length - 1; t > e; t--) {
      let f = i[t];
      f.from < l.to ? l.to++ : f.from++;
    }
  }
  let r = s.items.slice();
  for (let e of i) {
    let [l] = r.splice(e.from, 1);
    r.splice(e.to, 0, l);
  }
  return {
    ...s,
    items: r
  };
}
export {
  S as $,
  g as a
};
//# sourceMappingURL=useListData-CX8_NxJv.es.js.map
