import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopUserAddress, ShopUserAddressParam } from './model';

/**
 * 分页查询收货地址
 */
export async function pageShopUserAddress(params: ShopUserAddressParam) {
  const res = await request.get<ApiResult<PageResult<ShopUserAddress>>>(
     '/shop/shop-user-address/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询收货地址列表
 */
export async function listShopUserAddress(params?: ShopUserAddressParam) {
  const res = await request.get<ApiResult<ShopUserAddress[]>>(
    '/shop/shop-user-address',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加收货地址
 */
export async function addShopUserAddress(data: ShopUserAddress) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-user-address',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改收货地址
 */
export async function updateShopUserAddress(data: ShopUserAddress) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-user-address',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除收货地址
 */
export async function removeShopUserAddress(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-user-address/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除收货地址
 */
export async function removeBatchShopUserAddress(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-user-address/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询收货地址
 */
export async function getShopUserAddress(id: number) {
  const res = await request.get<ApiResult<ShopUserAddress>>(
    '/shop/shop-user-address/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
