import * as React from 'react';

export const getChildrenOfClass = (ComponentClass: any, children: any) => {
  const isElementFn = isOfComponentClass(ComponentClass);
  const filteredChildren = filterChildren(isElementFn)(children);

  return filteredChildren;
};

export const isElementOfComponentClass = (ComponentClass: any, element: any) =>
  element.type === ComponentClass;

export const isOfComponentClass = (ComponentClass: any) => (element: any) =>
  isElementOfComponentClass(ComponentClass, element);

export const filterElement = (isElementFn: any, childrenArray: any) =>
  childrenArray.filter(isElementFn);

const filterChildren = (isElementFn: any) => (children: any) => {
  const childrenArray = React.Children.toArray(children);

  return filterElement(isElementFn, childrenArray);
};
