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

export const provinceDispenserWithOutGetDashboard = createAsyncThunk(
  "PROVINCE_DISPENSER_SALE_WITHOUT_DASHBOARD",
  async (payload, { dispatch, rejectWithValue }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.get(
        "steward_free_sale_bar_dashboard/",
        {
          params: {
            date1: payload.selectedDate1,
            date2: payload.selectedDate2,
            role: getRoleFromUrl(),
            steward_key: payload.steward_key || "all",
          },
        }
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (error) {
      dispatch(LOADING_END());
      return rejectWithValue(
        error?.response?.data || { message: "ارتباط با سرور برقرار نشد." }
      );
    }
  }
);
