import { handleData } from 'lz-utils-lib'
import { apiProtocolTempList } from '@/api/protocol/template'
import api from '@/api'
const { apiCommonConfig } = api.common

/** 获取公共配置列表 */
export const handleGetCommonConfig = async (typeList: any) => {
  return await apiCommonConfig.post!({ typeList }).then((res: any) => {
    const data = res.data.map((item: { type: string; configList: any[] }) => {
      return {
        type: item.type,
        configList: handleData.formatOptions(item.configList, 'value', 'code')
      }
    })
    return data
  })
}

// 格式化公共配置数据
export const formatCommonConfig = (data: any, typeCode: string, valueType = 'string') => {
  let arr
  if (valueType === 'number') {
    arr = data.find(
      (it: { type: string }) => it.type === typeCode
    ).configList.map((item: any) => {
      return { label: item.label, value: Number(item.value) }
    })
  } else {
    arr = data.find(
      (it: { type: string }) => it.type === typeCode
    ).configList.map((item: any) => {
      return { label: item.label, value: item.value }
    })
  }
  return arr
}

/** 获取全部资方列表 */
export const handleGetSupplierList = async () => {
  return await api.common.getSupplierList.get!<[]>().then((res) => {
    const supplierId = handleData.formatOptions(res.data, 'supplierName', 'id')
    const supplierCode = handleData.formatOptions(res.data, 'supplierName', 'supplierCode')
    return {
      supplierId,
      supplierCode
    }
  })
}

/** 获取广告全部资方列表 */
export const handleGetAdvSupplierList = async () => {
  return await api.common.getAdvSupplierList.get!<[]>().then((res) => {
    const advId = handleData.formatOptions(res.data, 'advName', 'id')
    const advCode = handleData.formatOptions(res.data, 'advName', 'advChannel')
    const advIdByCode = handleData.formatOptions(res.data, 'id', 'advChannel')
    return {
      advId,
      advCode,
      advIdByCode
    }
  })
}

/** 获取用户的操作渠道 */
export const handleGetUserChannels = async () => {
  return await api.common.getUserChannelList.get!<[]>().then((res) => {
    const userChannel = handleData.formatOptions(res.data, 'pkgName', 'pkgCode')
    const userChannelCode = handleData.formatOptions(res.data, 'pkgCode', 'pkgCode')
    return {
      userChannel,
      userChannelCode
    }
  })
}

/** 获取全部协议列表 */
export const handleGetProtocolTempList = async () => {
  return await apiProtocolTempList().then((res) => {
    const data = res.data.map((it: { id: any; protocolName: any }) => {
      return {
        label: `${it.id} - ${it.protocolName}`,
        value: it.id
      }
    })
    return data
  })
}
