// merge multiple react refs into a single ref callback
// inspired by https://github.com/gregberge/react-merge-refs

export function mergeRefs<T = any>(
  refs: (React.MutableRefObject<T> | React.Ref<T> | undefined)[],
): React.RefCallback<T> {
  return (value) => {
    refs.forEach((ref) => {
      if (typeof ref === 'function') {
        ref(value)
      } else if (ref != null) {
        ;(ref as React.MutableRefObject<T | null>).current = value
      }
    })
  }
}
