import { useQuery } from 'react-query';

import { goalsKeys } from '../../queries';
import { getGoalCyclesConfiguration, getGoalWeights } from '../../services';

export const useTeamGoalsPageQuery = ({
  goalCycleId,
  userId,
}: {
  goalCycleId: number;
  userId: number;
}) => {
  const { data: weightsData, isLoading: isLoadingWeights } = useQuery(
    goalsKeys.weights(goalCycleId, userId),
    () => getGoalWeights(goalCycleId, userId),
    {
      select: r => r.data,
      enabled: !!goalCycleId && !!userId,
    },
  );

  const { isLoading: isLoadingConfiguration, data: goalCyclesConfiguration } =
    useQuery(goalsKeys.allConfiguration(), () => getGoalCyclesConfiguration(), {
      select: r => r.data,
    });

  return {
    isLoading: isLoadingWeights || isLoadingConfiguration,
    goalCyclesConfiguration,
    goalWeights: weightsData?.goalWeights,
    weightedProgress: weightsData?.weightedProgress,
  };
};
