import { useApolloClient } from "@apollo/client/react";
import { 
    CREATE_SAVE_CHECKOUT_CART
} from "@/graphql";

import { SaveCheckoutCartVariables, SaveCheckoutCart } from "@/types/checkout/type";

interface SaveCheckoutCartResult {
    data: SaveCheckoutCart | null;
    msg: string;
    error: boolean;
}

export const useSaveCheckoutCart = () => {
    const client = useApolloClient();
    const saveCheckoutCart = (params: SaveCheckoutCartVariables) => {
        return client.mutate({
            mutation: CREATE_SAVE_CHECKOUT_CART,
            variables: params
        }).then((res) => {
            const resData = res.data?.createSaveCheckoutCart.saveCheckoutCart ?? null; 
            const result: SaveCheckoutCartResult = { 
                data:resData,
                msg: '',
                error: false
            };
            return result;
        }).catch((err) => {
            const result: SaveCheckoutCartResult = {
                data: null,
                msg: err.message,
                error: true
            };
            return result;
        });

    };



  return {
    saveCheckoutCart
  };
};
