import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { LOADING_END, LOADING_START } from "../../../lib/redux/slices/appSlice";
import { getRoleFromUrl } from "../../../utils/getRoleFromUrl";

export const slaughterGetFreeBuyRequests = createAsyncThunk(
  "SLAUGHTER_GET_FREE_BUY_REQUESTS",
  async (params, { dispatch }) => {
    dispatch(LOADING_START());
    try {
      const { data, status } = await axios.get("kill_request/", {
        params: {
          direct_buying: true,
          role: getRoleFromUrl(),
          date1: params.date1,
          date2: params.date2,
          search: "filter",
          role_key: params.role_key || "",
          value: params.value || "",
          ...(params.type && { type: params.type }),
          ...(params.page && { page: params.page }),
          ...(params.page_size && { page_size: params.page_size }),
        },
      });
      dispatch(LOADING_END());
      return { data, status };
    } catch (error) {
      dispatch(LOADING_END());
      throw error;
    }
  }
);
