import axios from "@/utils/axios";
import { enqueueSnackbar } from "notistack";
import { BenefitConfigurationListType } from "./Types";

/**
 * Get Benefit Configuration List
 */
export const getBenefitConfigurationList = async ( claim_id: string ): Promise<BenefitConfigurationListType[]> => {
    const response = await axios.get(`/claims/${claim_id}/benefit-configuration`)
        .then((res) =>{
            return res.data.data.benefit_list;
        })
        .catch((res) => {
            enqueueSnackbar("server error !", {
                variant: 'error',
            });

            return [];
        });

    return response;
};

/**
 * Edit Benefit Configuration
 */
export const editBenefitConfiguration = async ( data: BenefitConfigurationListType ): Promise<boolean> => {
    const response = await axios.put(`/claims/benefit-configuration/edit/${data.claim_service_benefits_id}`, {
            ...data
        })
        .then((res) =>{
            enqueueSnackbar(res.data.message, {
                variant: 'success',
            });

            return true;
        })
        .catch((res) => {
            if (res.response.status == 400) {
                let arr_message = res.response.data.message;

                for (const key in arr_message) {
                    enqueueSnackbar(arr_message[key][0], {
                        variant: 'warning',
                    });
                }
            }
            else {
                enqueueSnackbar("server error !", {
                    variant: 'error',
                });
            }

            return false;
        });

    return response;
};
