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

export const useExportOnApprovalData = (enabled, onSettled) => {
  const {
    filters: { region },
  } = useOnApprovalPointsFilters();
  const dbTable = useDbTableName();

  return useQuery(
    ["export-on-approval", region],
    async () => {
      const params = new URLSearchParams({
        "status[]": [STATUSES.onApproval, STATUSES.working],
      });

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