// imports

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

// types

export type CreateOrUpdateAddressResponse = Address;

const useCreateOrUpdateAddress = (update: ComputedRef<Boolean>) => {
    // state

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

    // methods

    const handleCreateOrUpdateAddress = async (
        addressData: CreateOrUpdateAddressResponse
    ) => {
        const { data } = await axios[update.value ? "put" : "post"](
            update.value
                ? `${API_ENDPOINTS.account.address.update}/${addressData.id}`
                : API_ENDPOINTS.account.address.create,
            {
                ...addressData,
                for_me: addressData.for_me == "بله" ? true : false,
            }
        );
        return data;
    };

    return useMutation({
        mutationFn: (addressData: CreateOrUpdateAddressResponse) =>
            handleCreateOrUpdateAddress(addressData),
    });
};

export default useCreateOrUpdateAddress;
