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

export const slaughterGetBuyerDataService = createAsyncThunk(
  "SLAUGHTER_GET_BUYER_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get(
      "base-out-province-carcasses-buyer/?mobile=" + d
    );
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const slaughterSubmitBuyerDataService = createAsyncThunk(
  "SLAUGHTER_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 };
    }
  }
);

export const slaughterEditBuyerDataService = createAsyncThunk(
  "SLAUGHTER_EDIT_BUYER_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.put(
        "out-province-carcasses-buyer/0/",
        d
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
