{"version":3,"file":"findComponent.mjs","names":["React","CHILDREN_COMPONENT","INHERITED_NAME","getOriginChildren","findComponent","Children","names","recursively","limit","arguments","length","undefined","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/findComponent.ts"],"sourcesContent":["import React, { ReactElement, ReactNode, MutableRefObject } from 'react';\nimport { CHILDREN_COMPONENT, INHERITED_NAME } from './core/index';\nimport getOriginChildren from './getOriginChildren';\n\nfunction findComponent(\n  Children: any,\n  names: string[],\n  recursively?: boolean,\n  limit = 100,\n): Exclude<ReactNode, boolean | null | undefined> | 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  });\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,MAAqD,OAAO;AACxE,SAASC,kBAAkB,EAAEC,cAAc,QAAQ,kBAAc;AACjE,OAAOC,iBAAiB,MAAM,yBAAqB;AAEnD,SAASC,aAAaA,CACpBC,QAAa,EACbC,KAAe,EACfC,WAAqB,EAEuC;EAAA,IAD5DC,KAAK,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,GAAG;EAEX,IAAMG,QA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