// For finding the scrollable element within a container for pull-to-refresh functionality.

export const findVirtuosoScrollableElement = (
  container: HTMLElement
): HTMLElement => {
  const selector = '[data-virtuoso-scroller]'

  let scrollableElement: HTMLElement | null = null

  scrollableElement = container.querySelector(selector) as HTMLElement

  if (scrollableElement) return scrollableElement

  if (!scrollableElement) {
    const allDivs = Array.from(container.querySelectorAll('div'))
    for (const div of allDivs) {
      const style = window.getComputedStyle(div)
      if (style.overflowY === 'auto' || style.overflowY === 'scroll') {
        scrollableElement = div as HTMLElement
        break
      }
    }
  }

  return scrollableElement || container
}
