/**
 * Shared selection utilities for data-table and multi-select components
 * @countermeasure/web-components/utils/selection
 */

/**
 * Toggle an item in a selection set.
 * Returns a new Set with the item added or removed.
 */
export function toggleSelection<T>(item: T, selected: Set<T>): Set<T> {
  const next = new Set(selected)
  if (next.has(item)) {
    next.delete(item)
  } else {
    next.add(item)
  }
  return next
}

/**
 * Toggle all items — if all are selected, clear; otherwise select all.
 * Returns a new Set.
 */
export function toggleSelectAll<T>(items: T[], selected: Set<T>): Set<T> {
  if (items.length > 0 && selected.size === items.length) {
    return new Set<T>()
  }
  return new Set<T>(items)
}
