import { useMyRequest } from '~/composables/useFetchRequest'

enum Api {
  BrandList = '/client/brand/list/default',
  BrandFavorite = '/client/brand/favorite',
  BrandGoodsList = '/client/merchandise/list/default',
  DynamicCategoryList = '/client/brand/dynamicCategory',
  BrandDetail = '/client/brand/detail',
}

/**
 * 首页获取品牌列表
 * @param params
 * @returns
 */
export async function getHomeBrandListApi(params?: any) {
  return await useMyRequest().get(Api.BrandList, params)
}
/**
 * 品牌详情页获取相似品牌列表
 * @param params
 * @returns
 */
export async function getSimilarBrandListApi(params?: any) {
  return await useMyRequest().get(Api.BrandList, params)
}
/**
 * 品牌页获取品牌列表
 * @param params
 * @returns
 */
export async function getBrandListApi(params?: any) {
  return await useMyRequest().get(Api.BrandList, params)
}

/**
 * 是否品牌收藏
 * @param params
 * @returns
 */
export async function setBrandFavoriteApi(params: any) {
  return await useMyRequest().post(Api.BrandFavorite, params)
}
/**
 * 获取品牌详情
 * @param params
 * @returns
 */
export async function getBrandDetailApi(params?: any) {
  return await useMyRequest().get(Api.BrandDetail, params)
}

/**
 * 获取品牌详情分类列表
 * @param params
 * @returns
 */
export async function getDynamicCategoryListApi(params?: any) {
  const data = await useMyRequest().get(Api.DynamicCategoryList, params)
  const result = handleData(data)
  return result
}
function handleData(data: any) {
  if (data && isArray(data)) {
    data.forEach((item) => {
      item.label = item.title
      if (item.children === null)
        delete item.children
      if (item.children && item.children.length > 0)
        handleData(item.children)
    })
  }
  return data
}

/**
 * 获取品牌详情分类下的商品列表
 * @param params
 * @returns
 */
export async function getBrandGoodsListApi(params?: any) {
  return await useMyRequest().post(Api.BrandGoodsList, params)
}
