import { useCallback as i, useState as O, useEffect as g } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { EMPTY_ARRAY as t } from "../../../../../utils/value/constants.js";
const z = ({ items: f, multiSelect: r, selected: l }) => {
  const a = i(
    (c = t) => {
      const e = t.concat(c ?? t).filter(Boolean), n = f.filter((x) => e.includes(x.id)), o = r ? n : n.slice(0, 1);
      return o.length ? Object.freeze(o) : t;
    },
    [f, r]
  ), [d, s] = O(a(l)), b = i(
    (c = t) => {
      const e = c.filter((n) => f.includes(n));
      s(e.length ? Object.freeze(e) : t);
    },
    [f, s]
  ), u = i(
    (c) => {
      s((e) => {
        const n = e.indexOf(c);
        if (n < 0) return Object.freeze((r ? e : t).concat(c));
        if (!r) return e;
        const o = [...e];
        return o.splice(n, 1), o.length ? Object.freeze(o) : t;
      });
    },
    [r, s]
  );
  return g(() => s(a(l)), [a, l, s]), { resetSelection: b, select: u, selection: d };
};
export {
  z as default
};
