// imports

import { useMutation } from "@tanstack/vue-query";
import { API_ENDPOINTS } from "~/constants";

// types

export type SubmitDiscountCodeRequest = {
    code: string;
};

const useSubmitDiscountCode = () => {
    // state

    const { $axios: axios } = useNuxtApp();

    // methods

    const handleSubmitDiscountCode = async (
        params: SubmitDiscountCodeRequest
    ) => {
        const { data } = await axios.post(
            API_ENDPOINTS.orders.cart.add_discount,
            {
                ...params,
            }
        );
        return data;
    };

    return useMutation({
        mutationFn: (discountData: SubmitDiscountCodeRequest) =>
            handleSubmitDiscountCode(discountData),
    });
};

export default useSubmitDiscountCode;
