{"ast":null,"code":"import _toConsumableArray from \"@babel/runtime/helpers/toConsumableArray\";\nimport _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport _createClass from \"@babel/runtime/helpers/createClass\";\nimport _classCallCheck from \"@babel/runtime/helpers/classCallCheck\";\nimport _inherits from \"@babel/runtime/helpers/inherits\";\nimport _possibleConstructorReturn from \"@babel/runtime/helpers/possibleConstructorReturn\";\nimport _getPrototypeOf from \"@babel/runtime/helpers/getPrototypeOf\";\nimport _wrapNativeSuper from \"@babel/runtime/helpers/wrapNativeSuper\";\n\nfunction _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== \"undefined\" && o[Symbol.iterator] || o[\"@@iterator\"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === \"number\") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError(\"Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\"); }\n\nfunction _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === \"string\") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === \"Object\" && o.constructor) n = o.constructor.name; if (n === \"Map\" || n === \"Set\") return Array.from(o); if (n === \"Arguments\" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }\n\nfunction _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }\n\nfunction _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }\n\nfunction _isNativeReflectConstruct() { if (typeof Reflect === \"undefined\" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === \"function\") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }\n\nimport { useRef as $chei7$useRef, useState as $chei7$useState, useMemo as $chei7$useMemo } from \"react\";\nimport { useControlledState as $chei7$useControlledState } from \"@react-stately/utils\";\n\nfunction $parcel$export(e, n, v, s) {\n  Object.defineProperty(e, n, {\n    get: v,\n    set: s,\n    enumerable: true,\n    configurable: true\n  });\n}\n\nvar $e961bd55a24935c9$exports = {};\n$parcel$export($e961bd55a24935c9$exports, \"useMultipleSelectionState\", function () {\n  return $e961bd55a24935c9$export$253fe78d46329472;\n});\n\nvar $c5f55c1d1f0007fc$export$52baac22726c72bf = function (_Set) {\n  _inherits($c5f55c1d1f0007fc$export$52baac22726c72bf, _Set);\n\n  var _super = _createSuper($c5f55c1d1f0007fc$export$52baac22726c72bf);\n\n  function $c5f55c1d1f0007fc$export$52baac22726c72bf(keys, anchorKey, currentKey) {\n    var _this;\n\n    _classCallCheck(this, $c5f55c1d1f0007fc$export$52baac22726c72bf);\n\n    _this = _super.call(this, keys);\n\n    if (keys instanceof $c5f55c1d1f0007fc$export$52baac22726c72bf) {\n      _this.anchorKey = anchorKey || keys.anchorKey;\n      _this.currentKey = currentKey || keys.currentKey;\n    } else {\n      _this.anchorKey = anchorKey;\n      _this.currentKey = currentKey;\n    }\n\n    return _this;\n  }\n\n  return _createClass($c5f55c1d1f0007fc$export$52baac22726c72bf);\n}(_wrapNativeSuper(Set));\n\nfunction $e961bd55a24935c9$var$equalSets(setA, setB) {\n  if (setA.size !== setB.size) return false;\n\n  for (var _iterator = _createForOfIteratorHelperLoose(setA), _step; !(_step = _iterator()).done;) {\n    var item = _step.value;\n    if (!setB.has(item)) return false;\n  }\n\n  return true;\n}\n\nfunction $e961bd55a24935c9$export$253fe78d46329472(props) {\n  var _props$selectionMode = props.selectionMode,\n      selectionMode = _props$selectionMode === void 0 ? 'none' : _props$selectionMode,\n      disallowEmptySelection = props.disallowEmptySelection,\n      allowDuplicateSelectionEvents = props.allowDuplicateSelectionEvents;\n  var isFocusedRef = $chei7$useRef(false);\n\n  var _$chei7$useState = $chei7$useState(false),\n      _$chei7$useState2 = _slicedToArray(_$chei7$useState, 2),\n      _setFocused = _$chei7$useState2[1];\n\n  var focusedKeyRef = $chei7$useRef(null);\n  var childFocusStrategyRef = $chei7$useRef(null);\n\n  var _$chei7$useState3 = $chei7$useState(null),\n      _$chei7$useState4 = _slicedToArray(_$chei7$useState3, 2),\n      _setFocusedKey = _$chei7$useState4[1];\n\n  var selectedKeysProp = $chei7$useMemo(function () {\n    return $e961bd55a24935c9$var$convertSelection(props.selectedKeys);\n  }, [props.selectedKeys]);\n  var defaultSelectedKeys = $chei7$useMemo(function () {\n    return $e961bd55a24935c9$var$convertSelection(props.defaultSelectedKeys, new $c5f55c1d1f0007fc$export$52baac22726c72bf());\n  }, [props.defaultSelectedKeys]);\n\n  var _$chei7$useControlled = $chei7$useControlledState(selectedKeysProp, defaultSelectedKeys, props.onSelectionChange),\n      _$chei7$useControlled2 = _slicedToArray(_$chei7$useControlled, 2),\n      selectedKeys = _$chei7$useControlled2[0],\n      _setSelectedKeys = _$chei7$useControlled2[1];\n\n  var disabledKeysProp = $chei7$useMemo(function () {\n    return props.disabledKeys ? new Set(props.disabledKeys) : new Set();\n  }, [props.disabledKeys]);\n\n  var _$chei7$useState5 = $chei7$useState(props.selectionBehavior || 'toggle'),\n      _$chei7$useState6 = _slicedToArray(_$chei7$useState5, 2),\n      selectionBehavior = _$chei7$useState6[0],\n      setSelectionBehavior = _$chei7$useState6[1];\n\n  if (props.selectionBehavior === 'replace' && selectionBehavior === 'toggle' && typeof selectedKeys === 'object' && selectedKeys.size === 0) setSelectionBehavior('replace');\n  return {\n    selectionMode: selectionMode,\n    disallowEmptySelection: disallowEmptySelection,\n    selectionBehavior: selectionBehavior,\n    setSelectionBehavior: setSelectionBehavior,\n\n    get isFocused() {\n      return isFocusedRef.current;\n    },\n\n    setFocused: function setFocused(f) {\n      isFocusedRef.current = f;\n\n      _setFocused(f);\n    },\n\n    get focusedKey() {\n      return focusedKeyRef.current;\n    },\n\n    get childFocusStrategy() {\n      return childFocusStrategyRef.current;\n    },\n\n    setFocusedKey: function setFocusedKey(k) {\n      var childFocusStrategy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'first';\n      focusedKeyRef.current = k;\n      childFocusStrategyRef.current = childFocusStrategy;\n\n      _setFocusedKey(k);\n    },\n    selectedKeys: selectedKeys,\n    setSelectedKeys: function setSelectedKeys(keys) {\n      if (allowDuplicateSelectionEvents || !$e961bd55a24935c9$var$equalSets(keys, selectedKeys)) _setSelectedKeys(keys);\n    },\n    disabledKeys: disabledKeysProp\n  };\n}\n\nfunction $e961bd55a24935c9$var$convertSelection(selection, defaultValue) {\n  if (!selection) return defaultValue;\n  return selection === 'all' ? 'all' : new $c5f55c1d1f0007fc$export$52baac22726c72bf(selection);\n}\n\nvar $2f812fb5fa7ad073$exports = {};\n$parcel$export($2f812fb5fa7ad073$exports, \"SelectionManager\", function () {\n  return $2f812fb5fa7ad073$export$6c8a5aaad13c9852;\n});\n\nvar $2f812fb5fa7ad073$export$6c8a5aaad13c9852 = function () {\n  function $2f812fb5fa7ad073$export$6c8a5aaad13c9852(collection, state, options) {\n    var _options$allowsCellSe;\n\n    _classCallCheck(this, $2f812fb5fa7ad073$export$6c8a5aaad13c9852);\n\n    this.collection = collection;\n    this.state = state;\n    this.allowsCellSelection = (_options$allowsCellSe = options == null ? void 0 : options.allowsCellSelection) != null ? _options$allowsCellSe : false;\n    this._isSelectAll = null;\n  }\n\n  _createClass($2f812fb5fa7ad073$export$6c8a5aaad13c9852, [{\n    key: \"selectionMode\",\n    get: function get() {\n      return this.state.selectionMode;\n    }\n  }, {\n    key: \"disallowEmptySelection\",\n    get: function get() {\n      return this.state.disallowEmptySelection;\n    }\n  }, {\n    key: \"selectionBehavior\",\n    get: function get() {\n      return this.state.selectionBehavior;\n    }\n  }, {\n    key: \"setSelectionBehavior\",\n    value: function setSelectionBehavior(selectionBehavior) {\n      this.state.setSelectionBehavior(selectionBehavior);\n    }\n  }, {\n    key: \"isFocused\",\n    get: function get() {\n      return this.state.isFocused;\n    }\n  }, {\n    key: \"setFocused\",\n    value: function setFocused(isFocused) {\n      this.state.setFocused(isFocused);\n    }\n  }, {\n    key: \"focusedKey\",\n    get: function get() {\n      return this.state.focusedKey;\n    }\n  }, {\n    key: \"childFocusStrategy\",\n    get: function get() {\n      return this.state.childFocusStrategy;\n    }\n  }, {\n    key: \"setFocusedKey\",\n    value: function setFocusedKey(key, childFocusStrategy) {\n      this.state.setFocusedKey(key, childFocusStrategy);\n    }\n  }, {\n    key: \"selectedKeys\",\n    get: function get() {\n      return this.state.selectedKeys === 'all' ? new Set(this.getSelectAllKeys()) : this.state.selectedKeys;\n    }\n  }, {\n    key: \"rawSelection\",\n    get: function get() {\n      return this.state.selectedKeys;\n    }\n  }, {\n    key: \"isSelected\",\n    value: function isSelected(key) {\n      if (this.state.selectionMode === 'none') return false;\n      key = this.getKey(key);\n      return this.state.selectedKeys === 'all' ? !this.state.disabledKeys.has(key) : this.state.selectedKeys.has(key);\n    }\n  }, {\n    key: \"isEmpty\",\n    get: function get() {\n      return this.state.selectedKeys !== 'all' && this.state.selectedKeys.size === 0;\n    }\n  }, {\n    key: \"isSelectAll\",\n    get: function get() {\n      if (this.isEmpty) return false;\n      if (this.state.selectedKeys === 'all') return true;\n      if (this._isSelectAll != null) return this._isSelectAll;\n      var allKeys = this.getSelectAllKeys();\n      var selectedKeys = this.state.selectedKeys;\n      this._isSelectAll = allKeys.every(function (k) {\n        return selectedKeys.has(k);\n      });\n      return this._isSelectAll;\n    }\n  }, {\n    key: \"firstSelectedKey\",\n    get: function get() {\n      var _first;\n\n      var first = null;\n\n      for (var _iterator2 = _createForOfIteratorHelperLoose(this.state.selectedKeys), _step2; !(_step2 = _iterator2()).done;) {\n        var key = _step2.value;\n        var item = this.collection.getItem(key);\n        if (!first || (item == null ? void 0 : item.index) < first.index) first = item;\n      }\n\n      return (_first = first) == null ? void 0 : _first.key;\n    }\n  }, {\n    key: \"lastSelectedKey\",\n    get: function get() {\n      var _last;\n\n      var last = null;\n\n      for (var _iterator3 = _createForOfIteratorHelperLoose(this.state.selectedKeys), _step3; !(_step3 = _iterator3()).done;) {\n        var key = _step3.value;\n        var item = this.collection.getItem(key);\n        if (!last || (item == null ? void 0 : item.index) > last.index) last = item;\n      }\n\n      return (_last = last) == null ? void 0 : _last.key;\n    }\n  }, {\n    key: \"extendSelection\",\n    value: function extendSelection(toKey) {\n      if (this.selectionMode === 'none') return;\n\n      if (this.selectionMode === 'single') {\n        this.replaceSelection(toKey);\n        return;\n      }\n\n      toKey = this.getKey(toKey);\n      var selection;\n      if (this.state.selectedKeys === 'all') selection = new $c5f55c1d1f0007fc$export$52baac22726c72bf([toKey], toKey, toKey);else {\n        var selectedKeys = this.state.selectedKeys;\n        var anchorKey = selectedKeys.anchorKey || toKey;\n        selection = new $c5f55c1d1f0007fc$export$52baac22726c72bf(selectedKeys, anchorKey, toKey);\n\n        for (var _iterator4 = _createForOfIteratorHelperLoose(this.getKeyRange(anchorKey, selectedKeys.currentKey || toKey)), _step4; !(_step4 = _iterator4()).done;) {\n          var key = _step4.value;\n          selection.delete(key);\n        }\n\n        for (var _iterator5 = _createForOfIteratorHelperLoose(this.getKeyRange(toKey, anchorKey)), _step5; !(_step5 = _iterator5()).done;) {\n          var key1 = _step5.value;\n          if (!this.state.disabledKeys.has(key1)) selection.add(key1);\n        }\n      }\n      this.state.setSelectedKeys(selection);\n    }\n  }, {\n    key: \"getKeyRange\",\n    value: function getKeyRange(from, to) {\n      var fromItem = this.collection.getItem(from);\n      var toItem = this.collection.getItem(to);\n\n      if (fromItem && toItem) {\n        if (fromItem.index <= toItem.index) return this.getKeyRangeInternal(from, to);\n        return this.getKeyRangeInternal(to, from);\n      }\n\n      return [];\n    }\n  }, {\n    key: \"getKeyRangeInternal\",\n    value: function getKeyRangeInternal(from, to) {\n      var keys = [];\n      var key = from;\n\n      while (key) {\n        var item = this.collection.getItem(key);\n        if (item && item.type === 'item' || item.type === 'cell' && this.allowsCellSelection) keys.push(key);\n        if (key === to) return keys;\n        key = this.collection.getKeyAfter(key);\n      }\n\n      return [];\n    }\n  }, {\n    key: \"getKey\",\n    value: function getKey(key) {\n      var item = this.collection.getItem(key);\n      if (!item) return key;\n      if (item.type === 'cell' && this.allowsCellSelection) return key;\n\n      while (item.type !== 'item' && item.parentKey != null) {\n        item = this.collection.getItem(item.parentKey);\n      }\n\n      if (!item || item.type !== 'item') return null;\n      return item.key;\n    }\n  }, {\n    key: \"toggleSelection\",\n    value: function toggleSelection(key) {\n      if (this.selectionMode === 'none') return;\n\n      if (this.selectionMode === 'single' && !this.isSelected(key)) {\n        this.replaceSelection(key);\n        return;\n      }\n\n      key = this.getKey(key);\n      if (key == null) return;\n      var keys = new $c5f55c1d1f0007fc$export$52baac22726c72bf(this.state.selectedKeys === 'all' ? this.getSelectAllKeys() : this.state.selectedKeys);\n      if (keys.has(key)) keys.delete(key);else {\n        keys.add(key);\n        keys.anchorKey = key;\n        keys.currentKey = key;\n      }\n      if (this.disallowEmptySelection && keys.size === 0) return;\n      this.state.setSelectedKeys(keys);\n    }\n  }, {\n    key: \"replaceSelection\",\n    value: function replaceSelection(key) {\n      if (this.selectionMode === 'none') return;\n      key = this.getKey(key);\n      if (key == null) return;\n      this.state.setSelectedKeys(new $c5f55c1d1f0007fc$export$52baac22726c72bf([key], key, key));\n    }\n  }, {\n    key: \"setSelectedKeys\",\n    value: function setSelectedKeys(keys) {\n      if (this.selectionMode === 'none') return;\n      var selection = new $c5f55c1d1f0007fc$export$52baac22726c72bf();\n\n      for (var _iterator6 = _createForOfIteratorHelperLoose(keys), _step6; !(_step6 = _iterator6()).done;) {\n        var key = _step6.value;\n        key = this.getKey(key);\n\n        if (key != null) {\n          selection.add(key);\n          if (this.selectionMode === 'single') break;\n        }\n      }\n\n      this.state.setSelectedKeys(selection);\n    }\n  }, {\n    key: \"getSelectAllKeys\",\n    value: function getSelectAllKeys() {\n      var _this2 = this;\n\n      var keys = [];\n\n      var addKeys = function addKeys(key) {\n        while (key) {\n          if (!_this2.state.disabledKeys.has(key)) {\n            var item = _this2.collection.getItem(key);\n\n            if (item.type === 'item') keys.push(key);\n            if (item.hasChildNodes && (_this2.allowsCellSelection || item.type !== 'item')) addKeys(_toConsumableArray(item.childNodes)[0].key);\n          }\n\n          key = _this2.collection.getKeyAfter(key);\n        }\n      };\n\n      addKeys(this.collection.getFirstKey());\n      return keys;\n    }\n  }, {\n    key: \"selectAll\",\n    value: function selectAll() {\n      if (this.selectionMode === 'multiple') this.state.setSelectedKeys('all');\n    }\n  }, {\n    key: \"clearSelection\",\n    value: function clearSelection() {\n      if (!this.disallowEmptySelection && (this.state.selectedKeys === 'all' || this.state.selectedKeys.size > 0)) this.state.setSelectedKeys(new $c5f55c1d1f0007fc$export$52baac22726c72bf());\n    }\n  }, {\n    key: \"toggleSelectAll\",\n    value: function toggleSelectAll() {\n      if (this.isSelectAll) this.clearSelection();else this.selectAll();\n    }\n  }, {\n    key: \"select\",\n    value: function select(key, e) {\n      if (this.selectionMode === 'none') return;\n\n      if (this.selectionMode === 'single') {\n        if (this.isSelected(key) && !this.disallowEmptySelection) this.toggleSelection(key);else this.replaceSelection(key);\n      } else if (this.selectionBehavior === 'toggle' || e && (e.pointerType === 'touch' || e.pointerType === 'virtual')) this.toggleSelection(key);else this.replaceSelection(key);\n    }\n  }, {\n    key: \"isSelectionEqual\",\n    value: function isSelectionEqual(selection) {\n      if (selection === this.state.selectedKeys) return true;\n      var selectedKeys = this.selectedKeys;\n      if (selection.size !== selectedKeys.size) return false;\n\n      for (var _iterator7 = _createForOfIteratorHelperLoose(selection), _step7; !(_step7 = _iterator7()).done;) {\n        var key = _step7.value;\n        if (!selectedKeys.has(key)) return false;\n      }\n\n      for (var _iterator8 = _createForOfIteratorHelperLoose(selectedKeys), _step8; !(_step8 = _iterator8()).done;) {\n        var key2 = _step8.value;\n        if (!selection.has(key2)) return false;\n      }\n\n      return true;\n    }\n  }, {\n    key: \"canSelectItem\",\n    value: function canSelectItem(key) {\n      if (this.state.selectionMode === 'none' || this.state.disabledKeys.has(key)) return false;\n      var item = this.collection.getItem(key);\n      if (!item || item.type === 'cell' && !this.allowsCellSelection) return false;\n      return true;\n    }\n  }]);\n\n  return $2f812fb5fa7ad073$export$6c8a5aaad13c9852;\n}();\n\nvar $39bbcc3a62d931fe$exports = {};\nexport { $e961bd55a24935c9$export$253fe78d46329472 as useMultipleSelectionState, $2f812fb5fa7ad073$export$6c8a5aaad13c9852 as SelectionManager };","map":{"version":3,"sources":["packages/@react-stately/selection/src/useMultipleSelectionState.ts","packages/@react-stately/selection/src/Selection.ts","packages/@react-stately/selection/src/SelectionManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICkBa,yC;;;;;qDAIC,I,EAAkC,S,EAAiB,U,EAAkB;AAAA;;AAAA;;AAC/E,8BAAM,IAAN;;AACA,QAAI,IAAI,YAAY,yCAApB,EAA+B;AAC7B,YAAK,SAAL,GAAiB,SAAS,IAAI,IAAI,CAAC,SAAnC;AACA,YAAK,UAAL,GAAkB,UAAU,IAAI,IAAI,CAAC,UAArC;AACD,KAHD,MAGO;AACL,YAAK,SAAL,GAAiB,SAAjB;AACA,YAAK,UAAL,GAAkB,UAAlB;AACD;;AAR8E;AAShF;;;mBAb4B,G;;SDAtB,+B,CAAU,I,EAAM,I,EAAM;AAC7B,MAAI,IAAI,CAAC,IAAL,KAAc,IAAI,CAAC,IAAvB,EACE,OAAO,KAAP;;AAGF,uDAAiB,IAAjB,wCAAuB;AAAA,QAAd,IAAc;AACrB,QAAE,CAAG,IAAI,CAAC,GAAL,CAAS,IAAT,CA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,GAAV,CAAc,IAAd,CAAL,EACE,OAAO,KAAP;AAEH;;AAED,aAAO,IAAP;AACD;;;WAED,uBAAc,GAAd,EAAwB;AACtB,UAAI,KAAK,KAAL,CAAW,aAAX,KAA6B,MAA7B,IAAuC,KAAK,KAAL,CAAW,YAAX,CAAwB,GAAxB,CAA4B,GAA5B,CAA3C,EACE,OAAO,KAAP;AAGF,UAAI,IAAI,GAAG,KAAK,UAAL,CAAgB,OAAhB,CAAwB,GAAxB,CAAX;AACA,UAAE,CAAG,IAAH,IAAY,IAAI,CAAC,IAAL,KAAc,MAAd,IAAoB,CAAK,KAAK,mBAA5C,EACE,OAAO,KAAP;AAGF,aAAO,IAAP;AACD","sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key, useMemo, useRef, useState} from 'react';\nimport {MultipleSelection, SelectionBehavior, SelectionMode} from '@react-types/shared';\nimport {MultipleSelectionState} from './types';\nimport {Selection} from './Selection';\nimport {useControlledState} from '@react-stately/utils';\n\nfunction equalSets(setA, setB) {\n  if (setA.size !== setB.size) {\n    return false;\n  }\n\n  for (let item of setA) {\n    if (!setB.has(item)) {\n      return false;\n    }\n  }\n\n  return true;\n}\n\nexport interface MultipleSelectionStateProps extends MultipleSelection {\n  /** How multiple selection should behave in the collection. */\n  selectionBehavior?: SelectionBehavior,\n  /** Whether onSelectionChange should fire even if the new set of keys is the same as the last. */\n  allowDuplicateSelectionEvents?: boolean\n}\n\n/**\n * Manages state for multiple selection and focus in a collection.\n */\nexport function useMultipleSelectionState(props: MultipleSelectionStateProps): MultipleSelectionState {\n  let {\n    selectionMode = 'none' as SelectionMode,\n    disallowEmptySelection,\n    allowDuplicateSelectionEvents\n  } = props;\n\n  // We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.\n  // But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).\n  let isFocusedRef = useRef(false);\n  let [, setFocused] = useState(false);\n  let focusedKeyRef = useRef(null);\n  let childFocusStrategyRef = useRef(null);\n  let [, setFocusedKey] = useState(null);\n  let selectedKeysProp = useMemo(() => convertSelection(props.selectedKeys), [props.selectedKeys]);\n  let defaultSelectedKeys = useMemo(() => convertSelection(props.defaultSelectedKeys, new Selection()), [props.defaultSelectedKeys]);\n  let [selectedKeys, setSelectedKeys] = useControlledState(\n    selectedKeysProp,\n    defaultSelectedKeys,\n    props.onSelectionChange\n  );\n  let disabledKeysProp = useMemo(() =>\n    props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n  , [props.disabledKeys]);\n  let [selectionBehavior, setSelectionBehavior] = useState(props.selectionBehavior || 'toggle');\n\n  // If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press\n  // to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.\n  if (props.selectionBehavior === 'replace' && selectionBehavior === 'toggle' && typeof selectedKeys === 'object' && selectedKeys.size === 0) {\n    setSelectionBehavior('replace');\n  }\n\n  return {\n    selectionMode,\n    disallowEmptySelection,\n    selectionBehavior,\n    setSelectionBehavior,\n    get isFocused() {\n      return isFocusedRef.current;\n    },\n    setFocused(f) {\n      isFocusedRef.current = f;\n      setFocused(f);\n    },\n    get focusedKey() {\n      return focusedKeyRef.current;\n    },\n    get childFocusStrategy() {\n      return childFocusStrategyRef.current;\n    },\n    setFocusedKey(k, childFocusStrategy = 'first') {\n      focusedKeyRef.current = k;\n      childFocusStrategyRef.current = childFocusStrategy;\n      setFocusedKey(k);\n    },\n    selectedKeys,\n    setSelectedKeys(keys) {\n      if (allowDuplicateSelectionEvents || !equalSets(keys, selectedKeys)) {\n        setSelectedKeys(keys);\n      }\n    },\n    disabledKeys: disabledKeysProp\n  };\n}\n\nfunction convertSelection(selection: 'all' | Iterable<Key>, defaultValue?: Selection): 'all' | Selection {\n  if (!selection) {\n    return defaultValue;\n  }\n\n  return selection === 'all'\n    ? 'all'\n    : new Selection(selection);\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from 'react';\n\n/**\n * A Selection is a special Set containing Keys, which also has an anchor\n * and current selected key for use when range selecting.\n */\nexport class Selection extends Set<Key> {\n  anchorKey: Key;\n  currentKey: Key;\n\n  constructor(keys?: Iterable<Key> | Selection, anchorKey?: Key, currentKey?: Key) {\n    super(keys);\n    if (keys instanceof Selection) {\n      this.anchorKey = anchorKey || keys.anchorKey;\n      this.currentKey = currentKey || keys.currentKey;\n    } else {\n      this.anchorKey = anchorKey;\n      this.currentKey = currentKey;\n    }\n  }\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {\n  Collection,\n  FocusStrategy,\n  Selection as ISelection,\n  LongPressEvent,\n  Node,\n  PressEvent,\n  SelectionBehavior,\n  SelectionMode\n} from '@react-types/shared';\nimport {Key} from 'react';\nimport {MultipleSelectionManager, MultipleSelectionState} from './types';\nimport {Selection} from './Selection';\n\ninterface SelectionManagerOptions {\n  allowsCellSelection?: boolean\n}\n\n/**\n * An interface for reading and updating multiple selection state.\n */\nexport class SelectionManager implements MultipleSelectionManager {\n  private collection: Collection<Node<unknown>>;\n  private state: MultipleSelectionState;\n  private allowsCellSelection: boolean;\n  private _isSelectAll: boolean;\n\n  constructor(collection: Collection<Node<unknown>>, state: MultipleSelectionState, options?: SelectionManagerOptions) {\n    this.collection = collection;\n    this.state = state;\n    this.allowsCellSelection = options?.allowsCellSelection ?? false;\n    this._isSelectAll = null;\n  }\n\n  /**\n   * The type of selection that is allowed in the collection.\n   */\n  get selectionMode(): SelectionMode {\n    return this.state.selectionMode;\n  }\n\n  /**\n   * Whether the collection allows empty selection.\n   */\n  get disallowEmptySelection(): boolean {\n    return this.state.disallowEmptySelection;\n  }\n\n  /**\n   * The selection behavior for the collection.\n   */\n  get selectionBehavior(): SelectionBehavior {\n    return this.state.selectionBehavior;\n  }\n\n  /**\n   * Sets the selection behavior for the collection.\n   */\n  setSelectionBehavior(selectionBehavior: SelectionBehavior) {\n    this.state.setSelectionBehavior(selectionBehavior);\n  }\n\n  /**\n   * Whether the collection is currently focused.\n   */\n  get isFocused(): boolean {\n    return this.state.isFocused;\n  }\n\n  /**\n   * Sets whether the collection is focused.\n   */\n  setFocused(isFocused: boolean) {\n    this.state.setFocused(isFocused);\n  }\n\n  /**\n   * The current focused key in the collection.\n   */\n  get focusedKey(): Key {\n    return this.state.focusedKey;\n  }\n\n  /** Whether the first or last child of the focused key should receive focus. */\n  get childFocusStrategy(): FocusStrategy {\n    return this.state.childFocusStrategy;\n  }\n\n  /**\n   * Sets the focused key.\n   */\n  setFocusedKey(key: Key, childFocusStrategy?: FocusStrategy) {\n    this.state.setFocusedKey(key, childFocusStrategy);\n  }\n\n  /**\n   * The currently selected keys in the collection.\n   */\n  get selectedKeys(): Set<Key> {\n    return this.state.selectedKeys === 'all'\n      ? new Set(this.getSelectAllKeys())\n      : this.state.selectedKeys;\n  }\n\n  /**\n   * The raw selection value for the collection.\n   * Either 'all' for select all, or a set of keys.\n   */\n  get rawSelection(): ISelection {\n    return this.state.selectedKeys;\n  }\n\n  /**\n   * Returns whether a key is selected.\n   */\n  isSelected(key: Key) {\n    if (this.state.selectionMode === 'none') {\n      return false;\n    }\n\n    key = this.getKey(key);\n    return this.state.selectedKeys === 'all'\n      ? !this.state.disabledKeys.has(key)\n      : this.state.selectedKeys.has(key);\n  }\n\n  /**\n   * Whether the selection is empty.\n   */\n  get isEmpty(): boolean {\n    return this.state.selectedKeys !== 'all' && this.state.selectedKeys.size === 0;\n  }\n\n  /**\n   * Whether all items in the collection are selected.\n   */\n  get isSelectAll(): boolean {\n    if (this.isEmpty) {\n      return false;\n    }\n\n    if (this.state.selectedKeys === 'all') {\n      return true;\n    }\n\n    if (this._isSelectAll != null) {\n      return this._isSelectAll;\n    }\n\n    let allKeys = this.getSelectAllKeys();\n    let selectedKeys = this.state.selectedKeys;\n    this._isSelectAll = allKeys.every(k => selectedKeys.has(k));\n    return this._isSelectAll;\n  }\n\n  get firstSelectedKey(): Key | null {\n    let first: Node<unknown> | null = null;\n    for (let key of this.state.selectedKeys) {\n      let item = this.collection.getItem(key);\n      if (!first || item?.index < first.index) {\n        first = item;\n      }\n    }\n\n    return first?.key;\n  }\n\n  get lastSelectedKey(): Key | null {\n    let last: Node<unknown> | null = null;\n    for (let key of this.state.selectedKeys) {\n      let item = this.collection.getItem(key);\n      if (!last || item?.index > last.index) {\n        last = item;\n      }\n    }\n\n    return last?.key;\n  }\n\n  /**\n   * Extends the selection to the given key.\n   */\n  extendSelection(toKey: Key) {\n    if (this.selectionMode === 'none') {\n      return;\n    }\n\n    if (this.selectionMode === 'single') {\n      this.replaceSelection(toKey);\n      return;\n    }\n\n    toKey = this.getKey(toKey);\n\n    let selection: Selection;\n\n    // Only select the one key if coming from a select all.\n    if (this.state.selectedKeys === 'all') {\n      selection = new Selection([toKey], toKey, toKey);\n    } else {\n      let selectedKeys = this.state.selectedKeys as Selection;\n      let anchorKey = selectedKeys.anchorKey || toKey;\n      selection = new Selection(selectedKeys, anchorKey, toKey);\n      for (let key of this.getKeyRange(anchorKey, selectedKeys.currentKey || toKey)) {\n        selection.delete(key);\n      }\n\n      for (let key of this.getKeyRange(toKey, anchorKey)) {\n        if (!this.state.disabledKeys.has(key)) {\n          selection.add(key);\n        }\n      }\n    }\n\n    this.state.setSelectedKeys(selection);\n  }\n\n  private getKeyRange(from: Key, to: Key) {\n    let fromItem = this.collection.getItem(from);\n    let toItem = this.collection.getItem(to);\n    if (fromItem && toItem) {\n      if (fromItem.index <= toItem.index) {\n        return this.getKeyRangeInternal(from, to);\n      }\n\n      return this.getKeyRangeInternal(to, from);\n    }\n\n    return [];\n  }\n\n  private getKeyRangeInternal(from: Key, to: Key) {\n    let keys: Key[] = [];\n    let key = from;\n    while (key) {\n      let item = this.collection.getItem(key);\n      if (item && item.type === 'item' || (item.type === 'cell' && this.allowsCellSelection)) {\n        keys.push(key);\n      }\n\n      if (key === to) {\n        return keys;\n      }\n\n      key = this.collection.getKeyAfter(key);\n    }\n\n    return [];\n  }\n\n  private getKey(key: Key) {\n    let item = this.collection.getItem(key);\n    if (!item) {\n      // ¯\\_(ツ)_/¯\n      return key;\n    }\n\n    // If cell selection is allowed, just return the key.\n    if (item.type === 'cell' && this.allowsCellSelection) {\n      return key;\n    }\n\n    // Find a parent item to select\n    while (item.type !== 'item' && item.parentKey != null) {\n      item = this.collection.getItem(item.parentKey);\n    }\n\n    if (!item || item.type !== 'item') {\n      return null;\n    }\n\n    return item.key;\n  }\n\n  /**\n   * Toggles whether the given key is selected.\n   */\n  toggleSelection(key: Key) {\n    if (this.selectionMode === 'none') {\n      return;\n    }\n\n    if (this.selectionMode === 'single' && !this.isSelected(key)) {\n      this.replaceSelection(key);\n      return;\n    }\n\n    key = this.getKey(key);\n    if (key == null) {\n      return;\n    }\n\n    let keys = new Selection(this.state.selectedKeys === 'all' ? this.getSelectAllKeys() : this.state.selectedKeys);\n    if (keys.has(key)) {\n      keys.delete(key);\n      // TODO: move anchor to last selected key...\n      // Does `current` need to move here too?\n    } else {\n      keys.add(key);\n      keys.anchorKey = key;\n      keys.currentKey = key;\n    }\n\n    if (this.disallowEmptySelection && keys.size === 0) {\n      return;\n    }\n\n    this.state.setSelectedKeys(keys);\n  }\n\n  /**\n   * Replaces the selection with only the given key.\n   */\n  replaceSelection(key: Key) {\n    if (this.selectionMode === 'none') {\n      return;\n    }\n\n    key = this.getKey(key);\n    if (key == null) {\n      return;\n    }\n\n    this.state.setSelectedKeys(new Selection([key], key, key));\n  }\n\n  /**\n   * Replaces the selection with the given keys.\n   */\n  setSelectedKeys(keys: Iterable<Key>) {\n    if (this.selectionMode === 'none') {\n      return;\n    }\n\n    let selection = new Selection();\n    for (let key of keys) {\n      key = this.getKey(key);\n      if (key != null) {\n        selection.add(key);\n        if (this.selectionMode === 'single') {\n          break;\n        }\n      }\n    }\n\n    this.state.setSelectedKeys(selection);\n  }\n\n  private getSelectAllKeys() {\n    let keys: Key[] = [];\n    let addKeys = (key: Key) => {\n      while (key) {\n        if (!this.state.disabledKeys.has(key)) {\n          let item = this.collection.getItem(key);\n          if (item.type === 'item') {\n            keys.push(key);\n          }\n\n          // Add child keys. If cell selection is allowed, then include item children too.\n          if (item.hasChildNodes && (this.allowsCellSelection || item.type !== 'item')) {\n            addKeys([...item.childNodes][0].key);\n          }\n        }\n\n        key = this.collection.getKeyAfter(key);\n      }\n    };\n\n    addKeys(this.collection.getFirstKey());\n    return keys;\n  }\n\n  /**\n   * Selects all items in the collection.\n   */\n  selectAll() {\n    if (this.selectionMode === 'multiple') {\n      this.state.setSelectedKeys('all');\n    }\n  }\n\n  /**\n   * Removes all keys from the selection.\n   */\n  clearSelection() {\n    if (!this.disallowEmptySelection && (this.state.selectedKeys === 'all' || this.state.selectedKeys.size > 0)) {\n      this.state.setSelectedKeys(new Selection());\n    }\n  }\n\n  /**\n   * Toggles between select all and an empty selection.\n   */\n  toggleSelectAll() {\n    if (this.isSelectAll) {\n      this.clearSelection();\n    } else {\n      this.selectAll();\n    }\n  }\n\n  select(key: Key, e?: PressEvent | LongPressEvent | PointerEvent) {\n    if (this.selectionMode === 'none') {\n      return;\n    }\n\n    if (this.selectionMode === 'single') {\n      if (this.isSelected(key) && !this.disallowEmptySelection) {\n        this.toggleSelection(key);\n      } else {\n        this.replaceSelection(key);\n      }\n    } else if (this.selectionBehavior === 'toggle' || (e && (e.pointerType === 'touch' || e.pointerType === 'virtual'))) {\n      // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys\n      this.toggleSelection(key);\n    } else {\n      this.replaceSelection(key);\n    }\n  }\n\n  /**\n   * Returns whether the current selection is equal to the given selection.\n   */\n  isSelectionEqual(selection: Set<Key>) {\n    if (selection === this.state.selectedKeys) {\n      return true;\n    }\n\n    // Check if the set of keys match.\n    let selectedKeys = this.selectedKeys;\n    if (selection.size !== selectedKeys.size) {\n      return false;\n    }\n\n    for (let key of selection) {\n      if (!selectedKeys.has(key)) {\n        return false;\n      }\n    }\n\n    for (let key of selectedKeys) {\n      if (!selection.has(key)) {\n        return false;\n      }\n    }\n\n    return true;\n  }\n\n  canSelectItem(key: Key) {\n    if (this.state.selectionMode === 'none' || this.state.disabledKeys.has(key)) {\n      return false;\n    }\n\n    let item = this.collection.getItem(key);\n    if (!item || (item.type === 'cell' && !this.allowsCellSelection)) {\n      return false;\n    }\n\n    return true;\n  }\n}\n"]},"metadata":{},"sourceType":"module"}