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

export const slaughterSellCarcassOutProvinceGetBuyers = createAsyncThunk(
  "SLAUGHTER_GET_BUYERS_CARCASS",
  async (id, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("out-province-carcasses-buyer/", {
      params: { role: getRoleFromUrl() },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const slaughterSellCarcassOutProvinceSubmitSell = createAsyncThunk(
  "SLAUGHTER_SUBMIT_CARCASS_OUT_PROVINCE_SELL",
  async (d, { dispatch }) => {
    try {
      const { data, status } = await axios.post(`kill_house_free_sale_bar/`, d);
      dispatch(LOADING_START());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
