import type { cartItem, addCartItem, editCartItem } from '@/types/cart'
import type { PageParams, PageResult } from '@/types/global'
import { request } from '@/utils/request'

/**
 * 添加到购物车
 * @param data
 * @returns
 */
export const addCartApi = (data: addCartItem) => {
  return request<
    cartItem & {
      total_num: number
    }
  >({
    method: 'POST',
    url: 'shop/userCart/add',
    data,
  })
}

/**
 * 获取购物车
 * @returns
 */
export const getCartApi = (data: PageParams) => {
  return request<
    PageResult<cartItem[]> & {
      total_num: number
    }
  >({
    method: 'GET',
    url: 'shop/userCart',
    data,
  })
}

/**
 * 删除购物车
 * @param ids skuId集合
 * @returns
 */
export const deleteCartApi = (data: { id: string[] | number[] }) => {
  return request({
    method: 'POST',
    url: 'shop/userCart/delete',
    data,
  })
}

/**
 * 修改购物车
 * @param sku_id
 * @param data
 * @returns
 */
export const putCartApi = (data: editCartItem) => {
  return request({
    method: 'POST',
    url: `shop/userCart/edit`,
    data,
  })
}

/**
 * 修改购物车全选状态
 * @param data
 * @returns
 */
export const putCartSelectedAllApi = (data: { selected: boolean }) => {
  return request({
    method: 'POST',
    url: 'shop/userCart/selectAll',
    data,
  })
}

export const getCartTotalNumApi = () => {
  return request<number>({
    method: 'GET',
    url: 'shop/userCart/totalNum',
  })
}
