import { useSelector as useReactReduxSelector } from 'react-redux'
import { StoreState } from '.'

export function createScopeSelectorHook<T extends keyof StoreState>(key:T) {
  return function useSelector<U>(
    selector: (state: StoreState[T]) => U,
    equalityFn?: ((left: any, right: any) => boolean) | undefined
  ) {
    return useReactReduxSelector<
      StoreState,
      U
    >((state) => {
      const data = state[key]
      return selector(data)
    }, equalityFn)
  }
}