/** @format */

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

enum Api {
  CategoryList = '/client/category/merchandise',
  NewCategoryList = '/api-s002/categoryUsa/rootList',
  OssUploadFile = '/sys/oss/file/otherUpload',
  DictUrl = '/shop/sys/dict',
  Subscribe = '/client-s003/guestbook/add',
  ContactUs = '/client/guestbook/add',
  labels = '/client/centerProductTag/listTree',
  BannerData = '/client/category/detail',
  NoticeRemind = '/client/notification/unReadList',
}

/**
 * 获取分类列表
 * @param params
 * @returns
 */
export async function getCategoryListApi(params?: any) {
  const data = await useMyRequest().get(Api.CategoryList, 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 && item.children.length > 0)
        handleData(item.children)
    })
  }
  return data
}

/**
 * 获取分类数据
 * @returns
 */
export async function getBannerDataApi(data: any) {
  return await useMyRequest().get(Api.BannerData, data)
}
/**
 * 获取字典列表数据
 * @returns
 */
export async function getDictListApi(code: string) {
  return await useMyRequest().get(Api.DictUrl, {
    code,
  })
}
/**
 * 获取产品标签 + Trending
 * @returns
 */
export async function getProductLabelAndTrendApi(data: any) {
  return await useMyRequest().get(Api.labels, data)
}

/**
 * 上传文件
 * @param file
 * @returns
 */
export async function ossUploadApi(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  try {
    const result = await useMyRequest().post(Api.OssUploadFile, formData)
    return result
  }
  catch (error) {
    console.error('Upload failed:', error)
  }
}

/**
 * 订阅
 */
export async function submitSubscribeApi(params: any) {
  return await useMyRequest().post(Api.Subscribe, params)
}
/**
 * 联系我们
 */
export async function getContactUsApi(params: any) {
  return await useMyRequest().post(Api.ContactUs, params)
}
/**
 * 获取未读提醒列表
 */
export async function getNoticeRemindApi(params: any) {
  return await useMyRequest().get(Api.NoticeRemind, params)
}
