{"ast":null,"code":"import _defineProperty from \"@babel/runtime/helpers/defineProperty\";\nimport _toConsumableArray from \"@babel/runtime/helpers/toConsumableArray\";\nimport _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport _classCallCheck from \"@babel/runtime/helpers/classCallCheck\";\nimport _createClass from \"@babel/runtime/helpers/createClass\";\nfunction ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }\nimport { useMemo as $58Phs$useMemo, useRef as $58Phs$useRef, useEffect as $58Phs$useEffect } from \"react\";\nimport { useMultipleSelectionState as $58Phs$useMultipleSelectionState, SelectionManager as $58Phs$SelectionManager } from \"@react-stately/selection\";\nimport { useCollection as $58Phs$useCollection } from \"@react-stately/collections\";\nimport $58Phs$swchelperssrc_define_propertymjs from \"@swc/helpers/src/_define_property.mjs\";\nimport { useControlledState as $58Phs$useControlledState } from \"@react-stately/utils\";\nvar $a02d57049d202695$var$_Symbol_iterator = Symbol.iterator;\nvar $a02d57049d202695$export$d085fb9e920b5ca7 = function () {\n  function $a02d57049d202695$export$d085fb9e920b5ca7(nodes) {\n    var _this = this;\n    _classCallCheck(this, $a02d57049d202695$export$d085fb9e920b5ca7);\n    (0, $58Phs$swchelperssrc_define_propertymjs)(this, \"keyMap\", new Map());\n    this.iterable = nodes;\n    var visit = function visit(node) {\n      _this.keyMap.set(node.key, node);\n      if (node.childNodes && node.type === \"section\") for (var child of node.childNodes) visit(child);\n    };\n    for (var node of nodes) visit(node);\n    var last;\n    var index = 0;\n    for (var _ref of this.keyMap) {\n      var _ref2 = _slicedToArray(_ref, 2);\n      var key = _ref2[0];\n      var node1 = _ref2[1];\n      if (last) {\n        last.nextKey = key;\n        node1.prevKey = last.key;\n      } else {\n        this.firstKey = key;\n        node1.prevKey = undefined;\n      }\n      if (node1.type === \"item\") node1.index = index++;\n      last = node1;\n      last.nextKey = undefined;\n    }\n    this.lastKey = last === null || last === void 0 ? void 0 : last.key;\n  }\n  _createClass($a02d57049d202695$export$d085fb9e920b5ca7, [{\n    key: $a02d57049d202695$var$_Symbol_iterator,\n    value: function* value() {\n      yield* this.iterable;\n    }\n  }, {\n    key: \"size\",\n    get: function get() {\n      return this.keyMap.size;\n    }\n  }, {\n    key: \"getKeys\",\n    value: function getKeys() {\n      return this.keyMap.keys();\n    }\n  }, {\n    key: \"getKeyBefore\",\n    value: function getKeyBefore(key) {\n      var node = this.keyMap.get(key);\n      return node ? node.prevKey : null;\n    }\n  }, {\n    key: \"getKeyAfter\",\n    value: function getKeyAfter(key) {\n      var node = this.keyMap.get(key);\n      return node ? node.nextKey : null;\n    }\n  }, {\n    key: \"getFirstKey\",\n    value: function getFirstKey() {\n      return this.firstKey;\n    }\n  }, {\n    key: \"getLastKey\",\n    value: function getLastKey() {\n      return this.lastKey;\n    }\n  }, {\n    key: \"getItem\",\n    value: function getItem(key) {\n      return this.keyMap.get(key);\n    }\n  }, {\n    key: \"at\",\n    value: function at(idx) {\n      var keys = _toConsumableArray(this.getKeys());\n      return this.getItem(keys[idx]);\n    }\n  }]);\n  return $a02d57049d202695$export$d085fb9e920b5ca7;\n}();\nfunction $e72dd72e1c76a225$export$2f645645f7bca764(props) {\n  var filter = props.filter;\n  var selectionState = (0, $58Phs$useMultipleSelectionState)(props);\n  var disabledKeys = (0, $58Phs$useMemo)(function () {\n    return props.disabledKeys ? new Set(props.disabledKeys) : new Set();\n  }, [props.disabledKeys]);\n  var factory = function factory(nodes) {\n    return filter ? new (0, $a02d57049d202695$export$d085fb9e920b5ca7)(filter(nodes)) : new (0, $a02d57049d202695$export$d085fb9e920b5ca7)(nodes);\n  };\n  var context = (0, $58Phs$useMemo)(function () {\n    return {\n      suppressTextValueWarning: props.suppressTextValueWarning\n    };\n  }, [props.suppressTextValueWarning]);\n  var collection = (0, $58Phs$useCollection)(props, factory, context, [filter]);\n  var selectionManager = (0, $58Phs$useMemo)(function () {\n    return new (0, $58Phs$SelectionManager)(collection, selectionState);\n  }, [collection, selectionState]);\n  var cachedCollection = (0, $58Phs$useRef)(null);\n  (0, $58Phs$useEffect)(function () {\n    if (selectionState.focusedKey != null && !collection.getItem(selectionState.focusedKey)) {\n      var startItem = cachedCollection.current.getItem(selectionState.focusedKey);\n      var cachedItemNodes = _toConsumableArray(cachedCollection.current.getKeys()).map(function (key) {\n        var itemNode = cachedCollection.current.getItem(key);\n        return itemNode.type === \"item\" ? itemNode : null;\n      }).filter(function (node) {\n        return node !== null;\n      });\n      var itemNodes = _toConsumableArray(collection.getKeys()).map(function (key) {\n        var itemNode = collection.getItem(key);\n        return itemNode.type === \"item\" ? itemNode : null;\n      }).filter(function (node) {\n        return node !== null;\n      });\n      var diff = cachedItemNodes.length - itemNodes.length;\n      var index = Math.min(diff > 1 ? Math.max(startItem.index - diff + 1, 0) : startItem.index, itemNodes.length - 1);\n      var newNode;\n      while (index >= 0) {\n        if (!selectionManager.isDisabled(itemNodes[index].key)) {\n          newNode = itemNodes[index];\n          break;\n        }\n        if (index < itemNodes.length - 1) index++;else {\n          if (index > startItem.index) index = startItem.index;\n          index--;\n        }\n      }\n      selectionState.setFocusedKey(newNode ? newNode.key : null);\n    }\n    cachedCollection.current = collection;\n  }, [collection, selectionManager, selectionState, selectionState.focusedKey]);\n  return {\n    collection: collection,\n    disabledKeys: disabledKeys,\n    selectionManager: selectionManager\n  };\n}\nfunction $a0d645289fe9b86b$export$e7f05e985daf4b5f(props) {\n  var _props_defaultSelectedKey;\n  var _ref3 = (0, $58Phs$useControlledState)(props.selectedKey, (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null, props.onSelectionChange),\n    _ref4 = _slicedToArray(_ref3, 2),\n    selectedKey = _ref4[0],\n    setSelectedKey = _ref4[1];\n  var selectedKeys = (0, $58Phs$useMemo)(function () {\n    return selectedKey != null ? [selectedKey] : [];\n  }, [selectedKey]);\n  var _ref5 = (0, $e72dd72e1c76a225$export$2f645645f7bca764)(_objectSpread(_objectSpread({}, props), {}, {\n      selectionMode: \"single\",\n      disallowEmptySelection: true,\n      allowDuplicateSelectionEvents: true,\n      selectedKeys: selectedKeys,\n      onSelectionChange: function onSelectionChange(keys) {\n        var key = keys.values().next().value;\n        if (key === selectedKey && props.onSelectionChange) props.onSelectionChange(key);\n        setSelectedKey(key);\n      }\n    })),\n    collection = _ref5.collection,\n    disabledKeys = _ref5.disabledKeys,\n    selectionManager = _ref5.selectionManager;\n  var selectedItem = selectedKey != null ? collection.getItem(selectedKey) : null;\n  return {\n    collection: collection,\n    disabledKeys: disabledKeys,\n    selectionManager: selectionManager,\n    selectedKey: selectedKey,\n    setSelectedKey: setSelectedKey,\n    selectedItem: selectedItem\n  };\n}\nexport { $e72dd72e1c76a225$export$2f645645f7bca764 as useListState, $a0d645289fe9b86b$export$e7f05e985daf4b5f as useSingleSelectListState, $a02d57049d202695$export$d085fb9e920b5ca7 as ListCollection };","map":{"version":3,"sources":["packages/@react-stately/list/src/useListState.ts","packages/@react-stately/list/src/ListCollection.ts","packages/@react-stately/list/src/useSingleSelectListState.ts"],"names":[],"mappings":";;;;;;;;;;;;IC+DI,sCAAA,GAAA,MAAA,CAAO,QAAQ;AAAA,IAhDN,yCAAA;EAMX,SAAA,0CAAY,KAAwB,EAAE;IAAA,IAAA,KAAA;IAAA,eAAA,OAAA,yCAAA;IALtC,CAAA,CAAA,EAAA,uCAAA,EAAA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,MAAM,CAAA,CAAA,CAAG,IAAI,CAAA,CAAA,CAAG,KAAK;QAIpC,IAAI,GAAA,KAAQ,WAAA,IAAe,KAAA,CAAM,iBAAiB,EAChD,KAAA,CAAM,iBAAiB,CAAC,GAAA,CAAA;QAG1B,cAAA,CAAe,GAAA,CAAA;MACjB;IAAA,EACF,CAAA;IAjBK,UAAA,GAAA,KAAA,CAAD,UAAA;IAAa,YAAA,GAAA,KAAA,CAAF,YAAA;IAAgB,gBAAA,GAAA,KAAA,CAAF,gBAAA;EAmB7B,IAAI,YAAA,GAAe,WAAA,IAAe,IAAI,GAClC,UAAA,CAAW,OAAO,CAAC,WAAA,CAAA,GACnB,IAAI;EAER,OAAO;gBACL,UAAA;kBACA,YAAA;sBACA,gBAAA;iBACA,WAAA;oBACA,cAAA;kBACA;EACF,CAAA;AACF","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 {Collection, CollectionBase, Node} from '@react-types/shared';\nimport {Key, useEffect, useMemo, useRef} from 'react';\nimport {ListCollection} from './ListCollection';\nimport {MultipleSelectionStateProps, SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {useCollection} from '@react-stately/collections';\n\nexport interface ListProps<T> extends CollectionBase<T>, MultipleSelectionStateProps {\n  /** Filter function to generate a filtered list of nodes. */\n  filter?: (nodes: Iterable<Node<T>>) => Iterable<Node<T>>,\n  /** @private */\n  suppressTextValueWarning?: boolean\n}\n\nexport interface ListState<T> {\n  /** A collection of items in the list. */\n  collection: Collection<Node<T>>,\n\n  /** A set of items that are disabled. */\n  disabledKeys: Set<Key>,\n\n  /** A selection manager to read and update multiple selection state. */\n  selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for list-like components. Handles building a collection\n * of items from props, and manages multiple selection state.\n */\nexport function useListState<T extends object>(props: ListProps<T>): ListState<T>  {\n  let {filter} = props;\n\n  let selectionState = useMultipleSelectionState(props);\n  let disabledKeys = useMemo(() =>\n    props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n  , [props.disabledKeys]);\n\n  let factory = nodes => filter ? new ListCollection(filter(nodes)) : new ListCollection(nodes as Iterable<Node<T>>);\n  let context = useMemo(() => ({suppressTextValueWarning: props.suppressTextValueWarning}), [props.suppressTextValueWarning]);\n\n  let collection = useCollection(props, factory, context, [filter]);\n\n  let selectionManager = useMemo(() =>\n    new SelectionManager(collection, selectionState)\n    , [collection, selectionState]\n  );\n\n  // Reset focused key if that item is deleted from the collection.\n  const cachedCollection = useRef(null);\n  useEffect(() => {\n    if (selectionState.focusedKey != null && !collection.getItem(selectionState.focusedKey)) {\n      const startItem = cachedCollection.current.getItem(selectionState.focusedKey);\n      const cachedItemNodes = [...cachedCollection.current.getKeys()].map(\n        key => {\n          const itemNode = cachedCollection.current.getItem(key);\n          return itemNode.type === 'item' ? itemNode : null;\n        }\n      ).filter(node => node !== null);\n      const itemNodes = [...collection.getKeys()].map(\n        key => {\n          const itemNode = collection.getItem(key);\n          return itemNode.type === 'item' ? itemNode : null;\n        }\n      ).filter(node => node !== null);\n      const diff = cachedItemNodes.length - itemNodes.length;\n      let index = Math.min(\n        (\n          diff > 1 ?\n          Math.max(startItem.index - diff + 1, 0) :\n          startItem.index\n        ),\n        itemNodes.length - 1);\n      let newNode:Node<T>;\n      while (index >= 0) {\n        if (!selectionManager.isDisabled(itemNodes[index].key)) {\n          newNode = itemNodes[index];\n          break;\n        }\n        // Find next, not disabled item.\n        if (index < itemNodes.length - 1) {\n          index++;\n        // Otherwise, find previous, not disabled item.\n        } else {\n          if (index > startItem.index) {\n            index = startItem.index;\n          }\n          index--;\n        }\n      }\n      selectionState.setFocusedKey(newNode ? newNode.key : null);\n    }\n    cachedCollection.current = collection;\n  }, [collection, selectionManager, selectionState, selectionState.focusedKey]);\n\n  return {\n    collection,\n    disabledKeys,\n    selectionManager\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 {Collection, Node} from '@react-types/shared';\nimport {Key} from 'react';\n\nexport class ListCollection<T> implements Collection<Node<T>> {\n  private keyMap: Map<Key, Node<T>> = new Map();\n  private iterable: Iterable<Node<T>>;\n  private firstKey: Key;\n  private lastKey: Key;\n\n  constructor(nodes: Iterable<Node<T>>) {\n    this.iterable = nodes;\n\n    let visit = (node: Node<T>) => {\n      this.keyMap.set(node.key, node);\n\n      if (node.childNodes && node.type === 'section') {\n        for (let child of node.childNodes) {\n          visit(child);\n        }\n      }\n    };\n\n    for (let node of nodes) {\n      visit(node);\n    }\n\n    let last: Node<T>;\n    let index = 0;\n    for (let [key, node] of this.keyMap) {\n      if (last) {\n        last.nextKey = key;\n        node.prevKey = last.key;\n      } else {\n        this.firstKey = key;\n        node.prevKey = undefined;\n      }\n\n      if (node.type === 'item') {\n        node.index = index++;\n      }\n\n      last = node;\n\n      // Set nextKey as undefined since this might be the last node\n      // If it isn't the last node, last.nextKey will properly set at start of new loop\n      last.nextKey = undefined;\n    }\n\n    this.lastKey = last?.key;\n  }\n\n  *[Symbol.iterator]() {\n    yield* this.iterable;\n  }\n\n  get size() {\n    return this.keyMap.size;\n  }\n\n  getKeys() {\n    return this.keyMap.keys();\n  }\n\n  getKeyBefore(key: Key) {\n    let node = this.keyMap.get(key);\n    return node ? node.prevKey : null;\n  }\n\n  getKeyAfter(key: Key) {\n    let node = this.keyMap.get(key);\n    return node ? node.nextKey : null;\n  }\n\n  getFirstKey() {\n    return this.firstKey;\n  }\n\n  getLastKey() {\n    return this.lastKey;\n  }\n\n  getItem(key: Key) {\n    return this.keyMap.get(key);\n  }\n\n  at(idx: number) {\n    const keys = [...this.getKeys()];\n    return this.getItem(keys[idx]);\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 {CollectionBase, SingleSelection} from '@react-types/shared';\nimport {Key, useMemo} from 'react';\nimport {ListState, useListState} from './useListState';\nimport {Node} from '@react-types/shared';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface SingleSelectListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'disallowEmptySelection'> {\n  /** Filter function to generate a filtered list of nodes. */\n  filter?: (nodes: Iterable<Node<T>>) => Iterable<Node<T>>,\n  /** @private */\n  suppressTextValueWarning?: boolean\n}\n\nexport interface SingleSelectListState<T> extends ListState<T> {\n  /** The key for the currently selected item. */\n  readonly selectedKey: Key,\n\n  /** Sets the selected key. */\n  setSelectedKey(key: Key): void,\n\n  /** The value of the currently selected item. */\n  readonly selectedItem: Node<T>\n}\n\n/**\n * Provides state management for list-like components with single selection.\n * Handles building a collection of items from props, and manages selection state.\n */\nexport function useSingleSelectListState<T extends object>(props: SingleSelectListProps<T>): SingleSelectListState<T>  {\n  let [selectedKey, setSelectedKey] = useControlledState(props.selectedKey, props.defaultSelectedKey ?? null, props.onSelectionChange);\n  let selectedKeys = useMemo(() => selectedKey != null ? [selectedKey] : [], [selectedKey]);\n  let {collection, disabledKeys, selectionManager} = useListState({\n    ...props,\n    selectionMode: 'single',\n    disallowEmptySelection: true,\n    allowDuplicateSelectionEvents: true,\n    selectedKeys,\n    onSelectionChange: (keys: Set<Key>) => {\n      let key = keys.values().next().value;\n\n      // Always fire onSelectionChange, even if the key is the same\n      // as the current key (useControlledState does not).\n      if (key === selectedKey && props.onSelectionChange) {\n        props.onSelectionChange(key);\n      }\n\n      setSelectedKey(key);\n    }\n  });\n\n  let selectedItem = selectedKey != null\n    ? collection.getItem(selectedKey)\n    : null;\n\n  return {\n    collection,\n    disabledKeys,\n    selectionManager,\n    selectedKey,\n    setSelectedKey,\n    selectedItem\n  };\n}\n"]},"metadata":{},"sourceType":"module"}