{"version":3,"file":"use-selection.mjs","names":[],"sources":["../../src/use-selection/use-selection.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\n\nexport interface UseSelectionInput<T> {\n  /** The array of items to select from */\n  data: T[];\n\n  /** The initial selection, empty array by default */\n  defaultSelection?: T[];\n\n  /** If true, selection is reset when data changes */\n  resetSelectionOnDataChange?: boolean;\n}\n\nexport interface UseSelectionHandlers<T> {\n  /** Add an item to the selection */\n  select: (selected: T) => void;\n\n  /** Remove an item from the selection */\n  deselect: (deselected: T) => void;\n\n  /** Toggle an item's selection state */\n  toggle: (toggled: T) => void;\n\n  /** Returns true if all items from the `data` are selected */\n  isAllSelected: () => boolean;\n\n  /** Returns true if at least one item from the `data` is selected */\n  isSomeSelected: () => boolean;\n\n  /** Set the selection to a specific array of items */\n  setSelection: (selection: T[]) => void;\n\n  /** Clear all selections */\n  resetSelection: () => void;\n}\n\nexport type UseSelectionReturnValue<T> = readonly [T[], UseSelectionHandlers<T>];\n\nexport function useSelection<T>(input: UseSelectionInput<T>): UseSelectionReturnValue<T> {\n  const [selectionSet, setSelectionSet] = useState<Set<T>>(new Set(input.defaultSelection || []));\n\n  useDidUpdate(() => {\n    if (input.resetSelectionOnDataChange) {\n      setSelectionSet(new Set());\n    }\n  }, [input.data, input.resetSelectionOnDataChange]);\n\n  const select = useCallback((selected: T) => {\n    setSelectionSet((state) => {\n      if (!state.has(selected)) {\n        const newSet = new Set(state);\n        newSet.add(selected);\n        return newSet;\n      }\n      return state;\n    });\n  }, []);\n\n  const deselect = useCallback((deselected: T) => {\n    setSelectionSet((state) => {\n      if (state.has(deselected)) {\n        const newSet = new Set(state);\n        newSet.delete(deselected);\n        return newSet;\n      }\n      return state;\n    });\n  }, []);\n\n  const toggle = useCallback((toggled: T) => {\n    setSelectionSet((state) => {\n      const newSet = new Set(state);\n      if (state.has(toggled)) {\n        newSet.delete(toggled);\n      } else {\n        newSet.add(toggled);\n      }\n      return newSet;\n    });\n  }, []);\n\n  const resetSelection = useCallback(() => {\n    setSelectionSet(new Set());\n  }, []);\n\n  const setSelection = useCallback((selection: T[]) => {\n    setSelectionSet(new Set(selection));\n  }, []);\n\n  const isAllSelected = useCallback(() => {\n    if (input.data.length === 0) {\n      return false;\n    }\n    return input.data.every((item) => selectionSet.has(item));\n  }, [selectionSet, input.data]);\n\n  const isSomeSelected = useCallback(() => {\n    return input.data.some((item) => selectionSet.has(item));\n  }, [selectionSet, input.data]);\n\n  return [\n    Array.from(selectionSet),\n    {\n      select,\n      deselect,\n      toggle,\n      isAllSelected,\n      isSomeSelected,\n      setSelection,\n      resetSelection,\n    },\n  ];\n}\n\nexport namespace useSelection {\n  export type Input<T> = UseSelectionInput<T>;\n  export type Handlers<T> = UseSelectionHandlers<T>;\n  export type ReturnValue<T> = UseSelectionReturnValue<T>;\n}\n"],"mappings":";;;;AAuCA,SAAgB,aAAgB,OAAyD;CACvF,MAAM,CAAC,cAAc,mBAAmB,SAAiB,IAAI,IAAI,MAAM,oBAAoB,CAAC,CAAC,CAAC;CAE9F,mBAAmB;EACjB,IAAI,MAAM,4BACR,gCAAgB,IAAI,IAAI,CAAC;CAE7B,GAAG,CAAC,MAAM,MAAM,MAAM,0BAA0B,CAAC;CAEjD,MAAM,SAAS,aAAa,aAAgB;EAC1C,iBAAiB,UAAU;GACzB,IAAI,CAAC,MAAM,IAAI,QAAQ,GAAG;IACxB,MAAM,SAAS,IAAI,IAAI,KAAK;IAC5B,OAAO,IAAI,QAAQ;IACnB,OAAO;GACT;GACA,OAAO;EACT,CAAC;CACH,GAAG,CAAC,CAAC;CAEL,MAAM,WAAW,aAAa,eAAkB;EAC9C,iBAAiB,UAAU;GACzB,IAAI,MAAM,IAAI,UAAU,GAAG;IACzB,MAAM,SAAS,IAAI,IAAI,KAAK;IAC5B,OAAO,OAAO,UAAU;IACxB,OAAO;GACT;GACA,OAAO;EACT,CAAC;CACH,GAAG,CAAC,CAAC;CAEL,MAAM,SAAS,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