{"version":3,"file":"findComponent.mjs","names":["React","getOriginChildren","CHILDREN_COMPONENT","INHERITED_NAME","findComponent","Children","names","recursively","limit","children","toArray","find","child","isValidElement","type","Fragment","props","inheritedNames","displayName","result","name","includes","isAdvanceMode","findAllComponents","findAllAndAdd","forEach","component","push","extractFrom","extracted","rest","extractor"],"sources":["../../src/utils/findComponent.ts"],"sourcesContent":["import React, { type ReactElement, type MutableRefObject } from 'react';\n\nimport getOriginChildren from './getOriginChildren';\nimport { CHILDREN_COMPONENT, INHERITED_NAME } from '../core-types/symbols';\n\nfunction findComponent<P = any>(\n  Children: any,\n  names: string[],\n  recursively?: boolean,\n  limit = 100,\n): ReactElement<P> | undefined {\n  const children = Children[CHILDREN_COMPONENT] ? getOriginChildren(Children) : Children;\n  return React.Children.toArray(children).find((child) => {\n    if (React.isValidElement(child)) {\n      if (child.type === React.Fragment) {\n        return findComponent(child.props.children, names, recursively, limit);\n      }\n      // @ts-ignore\n      const inheritedNames = child.type[INHERITED_NAME] || [child.type.displayName];\n      const result = !!inheritedNames.find((name: string) => names.includes(name));\n\n      if (!result && child.props.children && recursively && limit > 0) {\n        return findComponent(child.props.children, names, recursively, limit - 1);\n      }\n\n      return result;\n    }\n  }) as ReactElement<P> | undefined;\n}\n\nexport function isAdvanceMode(Children: any, name: string[], recursively?: boolean) {\n  const children = Children[CHILDREN_COMPONENT] ? getOriginChildren(Children) : Children;\n  if (!children) return false;\n  if (typeof children === 'function') {\n    return true;\n  }\n  return !!findComponent(children, name, recursively);\n}\n\ntype IntergalacticComponent = ReactElement & {\n  ref?: MutableRefObject<HTMLElement>;\n};\n\nexport function findAllComponents(Children: any, names: string[]): IntergalacticComponent[] {\n  const result: IntergalacticComponent[] = [];\n\n  const findAllAndAdd = (Children: any) => {\n    const children = Children[CHILDREN_COMPONENT] ? getOriginChildren(Children) : Children;\n    React.Children.toArray(children).forEach((child) => {\n      if (React.isValidElement(child)) {\n        if (child.type === React.Fragment) {\n          findAllAndAdd(child.props.children);\n        } else if (\n          typeof child.type === 'function' &&\n          CHILDREN_COMPONENT in child.type &&\n          child.type[CHILDREN_COMPONENT]\n        ) {\n          findAllAndAdd(child.type);\n        } else {\n          // @ts-ignore\n          const inheritedNames = child.type[INHERITED_NAME] || [child.type.displayName];\n          const component = !!inheritedNames.find((name: string) => names.includes(name));\n\n          if (component) {\n            result.push(child);\n          }\n\n          if (child.props.children) {\n            return findAllAndAdd(child.props.children);\n          }\n        }\n      }\n    });\n  };\n\n  findAllAndAdd(Children);\n\n  return result;\n}\n\n/**\n * Extract some components from Children and return them and rest of components as tuple.\n */\nexport function extractFrom(Children: any, names: string[]): IntergalacticComponent[][] {\n  const extracted: IntergalacticComponent[] = [];\n  const rest: IntergalacticComponent[] = [];\n\n  const extractor = (Children: any) => {\n    const children = Children[CHILDREN_COMPONENT] ? getOriginChildren(Children) : Children;\n    React.Children.toArray(children).forEach((child) => {\n      if (React.isValidElement(child)) {\n        if (child.type === React.Fragment) {\n          extractor(child.props.children);\n        } else if (\n          typeof child.type === 'function' &&\n          CHILDREN_COMPONENT in child.type &&\n          child.type[CHILDREN_COMPONENT]\n        ) {\n          extractor(child.type);\n        } else {\n          // @ts-ignore\n          const inheritedNames = child.type[INHERITED_NAME] || [child.type.displayName];\n          const component = !!inheritedNames.find((name: string) => names.includes(name));\n\n          if (component) {\n            extracted.push(child);\n          } else {\n            rest.push(child);\n          }\n        }\n      }\n    });\n  };\n\n  extractor(Children);\n\n  return [extracted, rest];\n}\n\nexport default findComponent;\n"],"mappings":"AAAA,OAAOA,KAAK,MAAoD,OAAO;AAEvE,OAAOC,iBAAiB,MAAM,yBAAqB;AACnD,SAASC,kBAAkB,EAAEC,cAAc,QAAQ,2BAAuB;AAE1E,SAASC,aAAaA,CACpBC,QAAa,EACbC,KAAe,EACfC,WAAqB,EACrBC,KAAK,GAAG,GAAG,EACkB;EAC7B,MAAMC,QAAQ,GAAGJ,QAAQ,CAACH,kBAAkB,CAAC,GAAGD,iBAAiB,CAACI,QAA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