import { Id, Uuid } from '@win2win/shared';
import { computed, ComputedRef, Ref, ref } from 'vue';

export function useSelectRows(allIds: Ref<(Id | Uuid)[]>, idKey: ComputedRef<string>) {
  const selected = ref<any[]>([]);
  const selectedIds = computed(() => selected.value.map((item) => item[idKey.value || '']));
  const allSelected = computed(() => selected.value.length > 0 && selected.value.length === (allIds.value|| []).length);
  const toggleAllSelected = (val: boolean) => {
    if (val) {
      selected.value = allIds.value.map((id) => ({ [idKey.value || '']: id }));
    } else {
      selected.value = [];
    }
  };

  return {
    allSelected,
    selected,
    selectedIds,
    toggleAllSelected,
  };
}
