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

export const stewardSellOutGetBuyers = createAsyncThunk(
  "STEWARD_GET_BUYERS_SELL_OUT",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("out-province-carcasses-buyer/", {
      params: {
        role: getRoleFromUrl(),
        role_key: d?.role_key || "",
        mobile: d?.mobile,
      },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const stewatdSubmitBuyerDataService = createAsyncThunk(
  "STEWARD_SUBMIT_BUYER_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.post(
        "out-province-carcasses-buyer/",
        {
          ...d,
        }
      );

      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
