import { useLocation } from 'react-router';
import { useSelector } from 'react-redux';

export function useQuery() {
  return new URLSearchParams(useLocation().search);
}

export function useAllInvestigations() {
  return useSelector((state) => {
    const {
      scientistInstrumentInvestigations,
      myInvestigations,
      releasedInvestigations,
      investigations,
    } = state;

    return {
      investigations: [
        ...scientistInstrumentInvestigations.data,
        ...myInvestigations.data,
        ...releasedInvestigations.data,
        ...investigations.data,
      ],
      fetching:
        scientistInstrumentInvestigations.fetching ||
        myInvestigations.fetching ||
        releasedInvestigations.fetching ||
        investigations.fetching,
    };
  });
}

export function useInvestigation(investigationId) {
  const { investigations, fetching } = useAllInvestigations();

  return {
    investigation: investigations.find(({ id }) => `${id}` === investigationId),
    fetching,
  };
}
