import { useQuery } from 'react-query';

import { getCalibrations } from 'src/pages/dashboard/performance/services';

import { performanceKeys } from '../../queries';

export const CALIBRATION_SIDEBAR_PAGE_SIZE = 100;

type UseCalibrationsSidebarParams = {
  cycleId: string | number;
  enabled?: boolean;
};

const useCalibrationsSidebar = ({
  cycleId,
  enabled = true,
}: UseCalibrationsSidebarParams) => {
  const query = useQuery(
    performanceKeys.calibrationsSidebar(cycleId),
    () =>
      getCalibrations(cycleId, {
        page: 0,
        limit: CALIBRATION_SIDEBAR_PAGE_SIZE,
      }),
    {
      enabled: enabled && Boolean(cycleId),
      keepPreviousData: true,
    },
  );

  return {
    ...query,
    items: query.data?.data.items ?? [],
  };
};

export default useCalibrationsSidebar;
