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

export const slaughterDeleteAllocatedService = createAsyncThunk(
  "SLAUGHTER_DELETE_ALLOCATED_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.delete("steward-allocation/0/", {
      params: {
        steward_allocation_key: d.steward_allocation_key,
        role_key: d.role_key || "",
      },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const slaughterReturnAllocatedService = createAsyncThunk(
  "SLAUGHTER_RETURN_ALLOCATED_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    try {
      const { data, status } = await axios.put(
        "return-deleted-allocation/0/",
        d
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
