import { api } from "@/lib/api/axiosInstance";
import {
  CreateAddressType,
  AddressesResponse,
  AddressResponse,
  UpdateAddressType,
} from "../types/Types";

export const getAddresses = async (): Promise<AddressesResponse> => {
  const { data } = await api.get<AddressesResponse>("/public/user/addresses");
  return data;
};

export const createAddress = async (params: CreateAddressType) => {
  const { data } = await api.post("/public/user/addresses", params);
  return data;
};

export const updateAddress = async (params: UpdateAddressType) => {
  const { id, ...rest } = params;
  const { data } = await api.patch(`/public/user/addresses/${id}`, rest);
  return data;
};

export const deleteAddress = async (id: string) => {
  const { data } = await api.delete(`/public/user/addresses/${id}`);
  return data;
};

export const setDefaultAddress = async (id: string) => {
  const { data } = await api.patch(`/public/user/addresses/${id}/default`);
  return data;
};

export const getAddressById = async (id: string): Promise<AddressResponse> => {
  const { data } = await api.get<AddressResponse>(
    `/public/user/addresses/${id}`
  );
  return data;
};
