import { createAsyncThunk } from '@reduxjs/toolkit';
import { axiosRetailInstanceData } from '../AuthenicationToken/AuthenticationToken';

export const getCategoryData = createAsyncThunk(
  'CounterCategoryMap/getCategoryData',
  async (data) => {
    if (
      data?.AppId != null &&
      data?.AppId != undefined &&
      data?.CompId != null &&
      data?.CompId != undefined &&
      data?.BranchId != null &&
      data?.BranchId != undefined
    ) {
      return await axiosRetailInstanceData.get(
        `/productCat?AppId=${data?.AppId}&CompId=${data?.CompId}&BranchId=${data?.BranchId}`
      );
    }
  }
);

export const getSubCategoryData = createAsyncThunk(
  'CounterCategoryMap/getSubCategoryData',
  async (data) => {
    if (
      data?.AppId != null &&
      data?.AppId != undefined &&
      data?.CompId != null &&
      data?.CompId != undefined &&
      data?.BranchId != null &&
      data?.BranchId != undefined &&
      data?.ProdCat != null &&
      data?.ProdCat != undefined
    ) {
      return await axiosRetailInstanceData.get(
        `/productSubCat?AppId=${data?.AppId}&CompId=${data?.CompId}&BranchId=${data?.BranchId}&ProdCat=${data?.ProdCat}`
      );
    }
  }
);

export const postCounterCategoryMap = createAsyncThunk(
  'PurchaseOrder/postCounterCategoryMap',
  async (postData) => {
    return await axiosRetailInstanceData.post(`/CounterToken`, postData);
  }
);

export const getCounterTokenData = createAsyncThunk(
  'CounterCategoryMap/getSubCategoryData',
  async (data) => {
    if (
      data?.AppId != null &&
      data?.AppId != undefined &&
      data?.CompId != null &&
      data?.CompId != undefined &&
      data?.BranchId != null &&
      data?.BranchId != undefined
    ) {
      let endpoint = `/CounterToken?AppId=${data?.AppId}&CompId=${data?.CompId}&BranchId=${data?.BranchId}&UserId=${data?.UserId}`;

      if (data?.ActiveStatus !== null && data?.ActiveStatus !== undefined) {
        endpoint += `&ActiveStatus=${data?.ActiveStatus}`;
      }

      return await axiosRetailInstanceData.get(endpoint);
    }
  }
);

export const putCounterCategoryMap = createAsyncThunk(
  'PurchaseOrder/putCounterCategoryMap',
  async (putData) => {
    return await axiosRetailInstanceData.put(`/CounterToken`, putData);
  }
);

export const deleteCounterCategoryMap = createAsyncThunk(
  'PurchaseOrder/deleteCounterCategoryMap',
  async (deleteData) => {
    if (
      deleteData?.counterId != null &&
      deleteData?.counterId != undefined &&
      deleteData?.updatedBy != null &&
      deleteData?.updatedBy != undefined &&
      deleteData?.activeStatus != null &&
      deleteData?.activeStatus != undefined
    ) {
      return await axiosRetailInstanceData.put(`/DeleteToken`, deleteData);
    }
  }
);
