import { useState, useRef, useEffect, useCallback, forwardRef, useMemo } from 'react';
import { jsx } from 'react/jsx-runtime';
import { V as VirtualizedOptionsPanel, O as OptionsPanel } from './syncFilterUtils-BVclVovw.js';

function useBulkActionRunner({
  setDraft
}) {
  const [pending, setPending] = useState(false);
  const genRef = useRef(0);
  const isMountedRef = useRef(true);
  useEffect(() => {
    return () => {
      isMountedRef.current = false;
    };
  }, []);
  const runBulk = useCallback(
    (compute, current) => {
      const result = compute(current);
      genRef.current += 1;
      const myGen = genRef.current;
      if (Array.isArray(result)) {
        setDraft(result);
        if (pending) setPending(false);
        return;
      }
      setPending(true);
      result.then((next) => {
        if (!isMountedRef.current) return;
        if (genRef.current !== myGen) return;
        setDraft(next);
        setPending(false);
      }).catch(() => {
        if (!isMountedRef.current) return;
        if (genRef.current !== myGen) return;
        setPending(false);
      });
    },
    [pending, setDraft]
  );
  const setDraftAndBump = useCallback(
    (next) => {
      genRef.current += 1;
      setDraft(next);
    },
    [setDraft]
  );
  const reset = useCallback(() => {
    genRef.current += 1;
    setPending(false);
  }, []);
  return { pending, runBulk, setDraftAndBump, reset };
}

const MultiSelectOptions = forwardRef(({ selectedOptions, items, virtualize, ...rest }, ref) => {
  const selectedIds = useMemo(
    () => new Set(selectedOptions.map((opt) => opt.id)),
    [selectedOptions]
  );
  const getCheckedState = useCallback(
    (item) => {
      if (item.type === "select-all" || item.type === "select-filtered") {
        return item.checkState ?? "indeterminate";
      }
      const checkId = item.type === "pinned-option" ? item.original.id : item.id;
      return selectedIds.has(checkId) ? "checked" : "unchecked";
    },
    [selectedIds]
  );
  const ListComponent = virtualize ? VirtualizedOptionsPanel : OptionsPanel;
  return /* @__PURE__ */ jsx(
    ListComponent,
    {
      ...rest,
      ref,
      items,
      selectionType: "multiple",
      getCheckedState
    }
  );
});
MultiSelectOptions.displayName = "MultiSelectOptions";

const useToggleSelection = ({
  options,
  pinnedSections,
  selectedOptions,
  onSelectedOptionsChange
}) => {
  const allPinnedOptions = useMemo(() => {
    return pinnedSections.flatMap((section) => section.options);
  }, [pinnedSections]);
  const handleItemClick = useCallback(
    (option) => {
      const isSelected = selectedOptions.some((opt) => opt.id === option.id);
      if (isSelected) {
        onSelectedOptionsChange(
          selectedOptions.filter((opt) => opt.id !== option.id)
        );
      } else {
        const fullOption = options.find((opt) => opt.id === option.id) || allPinnedOptions.find((opt) => opt.id === option.id);
        if (fullOption) {
          onSelectedOptionsChange([...selectedOptions, fullOption]);
        }
      }
    },
    [selectedOptions, onSelectedOptionsChange, options, allPinnedOptions]
  );
  return { handleItemClick, allPinnedOptions };
};

function resolveBulkActionCheckState(checkState, current) {
  const value = typeof checkState === "function" ? checkState(current) : checkState;
  return value === true ? "checked" : value === false ? "unchecked" : value ?? "indeterminate";
}
function resolveBulkActionLabel(label, current, fallback = "Select All") {
  if (label === void 0) return fallback;
  return typeof label === "function" ? label(current) : label;
}

export { MultiSelectOptions as M, useToggleSelection as a, resolveBulkActionLabel as b, resolveBulkActionCheckState as r, useBulkActionRunner as u };
//# sourceMappingURL=bulkActionTypes-CTKwMpO7.js.map
