import { cloneElement, memo } from "react"

/**
 * Cascade elements from right to left.
 */
export function cascade(
  ...elements: React.ReactElement[]
): React.FC<{ children: React.ReactNode }> {
  return memo(({ children }) =>
    elements.reduceRight((acc, el) => cloneElement(el, undefined, acc), <>{children}</>),
  )
}
