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 provinceDispensersGetAllStewards = createAsyncThunk(
  "GET_STEWARDS_GUILDS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("real-guilds/", {
      params: {
        role: getRoleFromUrl(),
        ...d,
      },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

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

export const provinceDispensersCreateSteward = createAsyncThunk(
  "CREATE_STEWARDS_GUILDS_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.post("real-guilds/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
