import _ from 'lodash';
import { useMemo } from 'react';

import { SortValue, SORT_DIRECTION } from '../constants/sorting';

const useSortList = (list: any[], sortDirection: string, sort: string, sortList: SortValue[]) => {
  return useMemo(() => {
    return _.orderBy(
      list,
      sortList.find(s => s.label === sort)?.value ?? '',
      (SORT_DIRECTION.find(s => s.label === sortDirection)?.value ?? 'asc') as 'asc' | 'desc',
    );
  }, [list, sort, sortDirection]);
};

export default useSortList;
