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 slaughterGetDelegatesService = createAsyncThunk(
  "SLAUGHTER_GET_DELEGATES_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.get(
        `representative/?role=${getRoleFromUrl()}&type=${d.type}&search=${d.search}&value=${d.value}&page=${d.page}&page_size=${d.page_size}`
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return {
        error:
          e.response?.data?.result ||
          e.response?.data ||
          "خطا در دریافت اطلاعات",
      };
    }
  }
);

export const slaughterEditDelegatesService = createAsyncThunk(
  "SLAUGHTER_EDIT_DELEGATES_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.put("representative/0/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return {
        error:
          e.response?.data?.result ||
          e.response?.data ||
          "خطا در ویرایش وضعیت",
      };
    }
  }
);

