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tG,kCAAkC;oBAClC,MAAM,GAAG,GAAG,GAAG,KAAK,GAAG,GAAG,MAAM,GAAG,EAAE;oBAGvC,IAAI,QAAQ,QAAQ,WAAW,CAAC;wBAAC,GAAG,KAAK;+BAAE;oBAAK,GAAG,QAAQ,aAAa,CAAC,OAAO,QAAQ,KAAK,GAAG,EAAE;oBAClG,KAAK,IAAI,QAAQ,MAAO;wBACtB;wBACA,MAAM;oBACR;gBACF;YACF;QACF;QAEA,MAAM;IACR;;aAtNQ,QAA6B,IAAI;;AAuN3C;AAEA,4EAA4E;AAC5E,SAAS,+BAAY,QAAyC;IAC5D,IAAI,QAAwB,EAAE;IAC9B,IAAI,WAA6C;IACjD,OAAO;QACL,CAAC,CAAC,OAAO,QAAQ,CAAC;YAChB,KAAK,IAAI,QAAQ,MACf,MAAM;YAGR,IAAI,CAAC,UACH,WAAW;YAGb,KAAK,IAAI,QAAQ,SAAU;gBACzB,MAAM,IAAI,CAAC;gBACX,MAAM;YACR;QACF;IACF;AACF;AAGA,SAAS,8BAAQ,KAAqB,EAAE,KAAqB;IAC3D,IAAI,SAAS,OACX,OAAO,CAAC,UAAY,MAAM,MAAM;IAGlC,IAAI,OACF,OAAO;IAGT,IAAI,OACF,OAAO;AAEX;AAEA,SAAS,iCAAW,GAAW;IAC7B,OAAO,GAAG,CAAC,EAAE,CAAC,WAAW,KAAK,IAAI,KAAK,CAAC;AAC1C","sources":["packages/react-stately/src/collections/CollectionBuilder.ts"],"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 {CollectionBase, CollectionElement, Key, Node} from '@react-types/shared';\nimport {PartialNode} from './types';\nimport React, {ReactElement} from 'react';\n\ninterface CollectionBuilderState {\n  renderer?: (value: any) => ReactElement | null\n}\n\ninterface CollectReactElement<T> extends ReactElement {\n  getCollectionNode(props: any, context: any): Generator<PartialNode<T>, void, Node<T>[]>\n}\n\nexport class CollectionBuilder<T extends object> {\n  private context?: unknown;\n  private cache: WeakMap<T, Node<T>> = new WeakMap();\n\n  build(props: Partial<CollectionBase<T>>, context?: unknown): Iterable<Node<T>> {\n    this.context = context;\n    return iterable(() => this.iterateCollection(props));\n  }\n\n  private *iterateCollection(props: Partial<CollectionBase<T>>): Generator<Node<T>> {\n    let {children, items} = props;\n\n    if (React.isValidElement<{children: CollectionElement<T>}>(children) && children.type === React.Fragment) {\n      yield* this.iterateCollection({\n        children: children.props.children,\n        items\n      });\n    } else if (typeof children === 'function') {\n      if (!items) {\n        throw new Error('props.children was a function but props.items is missing');\n      }\n\n      let index = 0;\n      for (let item of items) {\n        yield* this.getFullNode({\n          value: item,\n          index\n        }, {renderer: children});\n        index++;\n      }\n    } else {\n      let items: CollectionElement<T>[] = [];\n      React.Children.forEach(children, child => {\n        if (child) {\n          items.push(child);\n        }\n      });\n\n      let index = 0;\n      for (let item of items) {\n        let nodes = this.getFullNode({\n          element: item,\n          index: index\n        }, {});\n\n        for (let node of nodes) {\n          index++;\n          yield node;\n        }\n      }\n    }\n  }\n\n  private getKey(item: NonNullable<CollectionElement<T>>, partialNode: PartialNode<T>, state: CollectionBuilderState, parentKey?: Key | null): Key {\n    if (item.key != null) {\n      return item.key;\n    }\n\n    if (partialNode.type === 'cell' && partialNode.key != null) {\n      return `${parentKey}${partialNode.key}`;\n    }\n\n    let v = partialNode.value as any;\n    if (v != null) {\n      let key = v.key ?? v.id;\n      if (key == null) {\n        throw new Error('No key found for item');\n      }\n\n      return key;\n    }\n\n    return parentKey ? `${parentKey}.${partialNode.index}` : `$.${partialNode.index}`;\n  }\n\n  private getChildState(state: CollectionBuilderState, partialNode: PartialNode<T>) {\n    return {\n      renderer: partialNode.renderer || state.renderer\n    };\n  }\n\n  private *getFullNode(partialNode: PartialNode<T> & {index: number}, state: CollectionBuilderState, parentKey?: Key | null, parentNode?: Node<T>): Generator<Node<T>> {\n    if (React.isValidElement<{children: CollectionElement<T>}>(partialNode.element) && partialNode.element.type === React.Fragment) {\n      let children: CollectionElement<T>[] = [];\n\n      React.Children.forEach(partialNode.element.props.children, child => {\n        children.push(child);\n      });\n\n      let index = partialNode.index ?? 0;\n\n      for (const child of children) {\n        yield* this.getFullNode({\n          element: child,\n          index: index++\n        }, state, parentKey, parentNode);\n      }\n\n      return;\n    }\n\n    // If there's a value instead of an element on the node, and a parent renderer function is available,\n    // use it to render an element for the value.\n    let element = partialNode.element;\n    if (!element && partialNode.value && state && state.renderer) {\n      let cached = this.cache.get(partialNode.value);\n      if (cached && (!cached.shouldInvalidate || !cached.shouldInvalidate(this.context))) {\n        cached.index = partialNode.index;\n        cached.parentKey = parentNode ? parentNode.key : null;\n        yield cached;\n        return;\n      }\n\n      element = state.renderer(partialNode.value);\n    }\n\n    // If there's an element with a getCollectionNode function on its type, then it's a supported component.\n    // Call this function to get a partial node, and recursively build a full node from there.\n    if (React.isValidElement(element)) {\n      let type = element.type as unknown as CollectReactElement<T>;\n      if (typeof type !== 'function' && typeof type.getCollectionNode !== 'function') {\n        let name = element.type;\n        throw new Error(`Unknown element <${name}> in collection.`);\n      }\n\n      let childNodes = type.getCollectionNode(element.props, this.context) as Generator<PartialNode<T>, void, Node<T>[]>;\n      let index = partialNode.index ?? 0;\n      let result = childNodes.next();\n      while (!result.done && result.value) {\n        let childNode = result.value;\n\n        partialNode.index = index;\n\n        let nodeKey = childNode.key ?? null;\n        if (nodeKey == null) {\n          nodeKey = childNode.element ? null : this.getKey(element as NonNullable<CollectionElement<T>>, partialNode, state, parentKey);\n        }\n\n        let nodes = this.getFullNode({\n          ...childNode,\n          key: nodeKey,\n          index,\n          wrapper: compose(partialNode.wrapper, childNode.wrapper)\n        }, this.getChildState(state, childNode), parentKey ? `${parentKey}${element.key}` : element.key, parentNode);\n\n        let children = [...nodes];\n        for (let node of children) {\n          // Cache the node based on its value\n          node.value = childNode.value ?? partialNode.value ?? null;\n          if (node.value) {\n            this.cache.set(node.value, node);\n          }\n\n          // The partial node may have specified a type for the child in order to specify a constraint.\n          // Verify that the full node that was built recursively matches this type.\n          if (partialNode.type && node.type !== partialNode.type) {\n            throw new Error(`Unsupported type <${capitalize(node.type)}> in <${capitalize(parentNode?.type ?? 'unknown parent type')}>. Only <${capitalize(partialNode.type)}> is supported.`);\n          }\n\n          index++;\n          yield node;\n        }\n\n        result = childNodes.next(children);\n      }\n\n      return;\n    }\n\n    // Ignore invalid elements\n    if (partialNode.key == null || partialNode.type == null) {\n      return;\n    }\n\n    // Create full node\n    let builder = this;\n    let node: Node<T> = {\n      type: partialNode.type,\n      props: partialNode.props,\n      key: partialNode.key,\n      parentKey: parentNode ? parentNode.key : null,\n      value: partialNode.value ?? null,\n      level: (parentNode?.level ?? 0) + (parentNode?.type === 'item' ? 1 : 0),\n      index: partialNode.index,\n      rendered: partialNode.rendered,\n      textValue: partialNode.textValue ?? '',\n      'aria-label': partialNode['aria-label'],\n      wrapper: partialNode.wrapper,\n      shouldInvalidate: partialNode.shouldInvalidate,\n      hasChildNodes: partialNode.hasChildNodes || false,\n      childNodes: iterable(function *() {\n        if (!partialNode.hasChildNodes || !partialNode.childNodes) {\n          return;\n        }\n\n        let index = 0;\n        for (let child of partialNode.childNodes()) {\n          // Ensure child keys are globally unique by prepending the parent node's key\n          if (child.key != null) {\n            // TODO: Remove this line entirely and enforce that users always provide unique keys.\n            // Currently this line will have issues when a parent has a key `a` and a child with key `bc`\n            // but another parent has key `ab` and its child has a key `c`. The combined keys would result in both\n            // children having a key of `abc`.\n            child.key = `${node.key}${child.key}`;\n          }\n\n          let nodes = builder.getFullNode({...child, index}, builder.getChildState(state, child), node.key, node);\n          for (let node of nodes) {\n            index++;\n            yield node;\n          }\n        }\n      })\n    };\n\n    yield node;\n  }\n}\n\n// Wraps an iterator function as an iterable object, and caches the results.\nfunction iterable<T>(iterator: () => IterableIterator<Node<T>>): Iterable<Node<T>> {\n  let cache: Array<Node<T>> = [];\n  let iterable: null | IterableIterator<Node<T>> = null;\n  return {\n    *[Symbol.iterator]() {\n      for (let item of cache) {\n        yield item;\n      }\n\n      if (!iterable) {\n        iterable = iterator();\n      }\n\n      for (let item of iterable) {\n        cache.push(item);\n        yield item;\n      }\n    }\n  };\n}\n\ntype Wrapper = (element: ReactElement) => ReactElement;\nfunction compose(outer: Wrapper | void, inner: Wrapper | void): Wrapper | undefined {\n  if (outer && inner) {\n    return (element) => outer(inner(element));\n  }\n\n  if (outer) {\n    return outer;\n  }\n\n  if (inner) {\n    return inner;\n  }\n}\n\nfunction capitalize(str: string) {\n  return str[0].toUpperCase() + str.slice(1);\n}\n"],"names":[],"version":3,"file":"CollectionBuilder.cjs.map"}