import { useQuery } from "@tanstack/react-query";
import { STATUSES } from "../../../config";
import { exportPoints } from "../../../api";
import { handleExportSuccess } from "../ExportButton";
import { useWorkingPointsFilters } from "../../../stores/useWorkingPointsFilters";

export const useExportWorkingData = (enabled, onSettled) => {
  const {
    filters: { region, deltaTraffic, factTraffic, age },
  } = useWorkingPointsFilters();

  return useQuery(
    ["export-working", region, deltaTraffic, factTraffic, age],
    async () => {
      const params = new URLSearchParams({
        "status[]": [STATUSES.working],
        "delta_current[]": deltaTraffic,
        "fact[]": factTraffic,
        "age_day[]": age,
      });

      return await exportPoints(params, region);
    },
    {
      enabled,
      onSuccess: handleExportSuccess,
      onSettled,
    }
  );
};
