import { useFilters } from "../../../stores/useFilters";
import { usePointSelection } from "../../../stores/usePointSelection";
import { useQuery } from "@tanstack/react-query";
import { exportPoints } from "../../../api";
import { handleExportSuccess } from "../ExportButton";

export const useExportInitialData = (enabled, onSettled) => {
  const { filters } = useFilters();
  const { prediction, status, categories, region } = filters;
  const { selection } = usePointSelection();

  return useQuery(
    ["export-initial", filters, selection],
    async () => {
      const params = new URLSearchParams({
        "prediction_current[]": prediction,
        "status[]": status,
        "categories[]": categories,
        "included[]": [...selection.included],
        "excluded[]": [...selection.excluded],
      });

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