import { ref } from 'vue'
import emitter from '/@/utils/emitter'

export default function useAddedItem() {
  // 用于存储从 PopoverFilterTable 组件传来的数据
  const receiveData = ref([])
  // 用于存储已经添加的 parentAsin,避免重复添加
  const parentAsinsSet = ref(new Set<string>())
  const allSkus = ref<string[]>([])
  const searchType = ref('')

  // 点击复选框就会触发, 用于处理从 PopoverFilterTable 组件传来的数据
  emitter.on('PopoverFilterTable-tableData', (data: any) => {
    if (searchType.value === 'sku') {
      // 过滤出 parentAsin 和 sku 都不在 parentAsinsSet 和 allSkus 中的数据,避免重复添加
      const uniqueData = data.filter(
        (item: any) => !parentAsinsSet.value.has(item.parentAsin + item.sku) && !allSkus.value.includes(item.sku)
      )
      // 将过滤后的数据添加到 receiveData 中
      receiveData.value.push(...uniqueData)
      // 将新添加的 parentAsin 和 sku 添加到 parentAsinsSet 和 allSkus 中
      uniqueData.forEach((item: any) => {
        parentAsinsSet.value.add(item.parentAsin + item.sku)
        allSkus.value.push(item.sku)
      })
    } else {
      const uniqueData = data.filter(
        (item: any) => !parentAsinsSet.value.has(item.parentAsin + item.asin) && !allSkus.value.includes(item.asin)
      )
      receiveData.value.push(...uniqueData)
      uniqueData.forEach((item: any) => {
        parentAsinsSet.value.add(item.parentAsin + item.asin)
        allSkus.value.push(item.asin)
      })
    }
  })

  emitter.on('PopoverFilterTable-needDeleteBySearchType', (searchTypeToDelete: string) => {
    // 遍历 receiveData,找到 sku 匹配的项
    receiveData.value = receiveData.value.reduce((acc: any[], item: any) => {
      if (searchType.value === 'sku') {
        if (item.sku !== searchTypeToDelete) {
          acc.push(item)
        } else {
          // 从 parentAsinsSet 和 allSkus 中删除该 parentAsin 和 sku 的组合
          parentAsinsSet.value.delete(item.parentAsin + item.sku)
          allSkus.value = allSkus.value.filter((sku) => sku !== item.sku)
        }
      } else {
        if (item.asin !== searchTypeToDelete) {
          acc.push(item)
        } else {
          parentAsinsSet.value.delete(item.parentAsin + item.asin)
          allSkus.value = allSkus.value.filter((asin) => asin !== item.asin)
        }
      }

      return acc
    }, [])
  })

  emitter.on('PopoverFilterTable-needDelete', (parentAsinToDelete: string) => {
    // 遍历 receiveData,找到 parentAsin 匹配的项
    receiveData.value = receiveData.value.reduce((acc: any[], item: any) => {
      if (searchType.value === 'sku') {
        if (item.parentAsin !== parentAsinToDelete) {
          acc.push(item)
        } else {
          // 从 parentAsinsSet 和 allSkus 中删除该 parentAsin 和 sku 的组合
          parentAsinsSet.value.delete(item.parentAsin + item.sku)
          allSkus.value = allSkus.value.filter((sku) => sku !== item.sku)
        }
      } else {
        if (item.parentAsin !== parentAsinToDelete) {
          acc.push(item)
        } else {
          parentAsinsSet.value.delete(item.parentAsin + item.asin)
          allSkus.value = allSkus.value.filter((asin) => asin !== item.asin)
        }
      }

      return acc
    }, [])
  })

  // 在 TopFilter 组件隐藏时清空 receiveData 和 parentAsinsSet
  emitter.on('TopFilter', (value: any) => {
    if (!value.isVisible) {
      resetReceiveData()
    }
    searchType.value = value.productFilterSelect?.value
  })

  // 点击应用后清空 receiveData 和 parentAsinsSet, 并将 allSkus 发送给 TopFilter 组件
  emitter.on('TopFilter-confirm', (value: any) => {
    value.confirm && emitter.emit('PopoverFilter-allSkus', { allSkus, searchType })
    emitter.emit('PopoverFilter-sendSearchItem', allSkus)
    resetReceiveData()
  })

  // 根据 parentAsin 对 receiveData 数据进行分组
  function groupByParentAsin(receiveData: { parentAsin: string; asin: string; sku: string }[]) {
    return receiveData.reduce((acc: Record<string, { parentAsin: string; asin: string; sku: string }[]>, item) => {
      // acc初始化为空对象, 遍历每一项, 如果不存在 parentAsin的值, 则初始化一个空数组, 然后将parentAsin的值作为key, 添加到数组中
      if (!acc[item.parentAsin]) {
        acc[item.parentAsin] = []
      }
      acc[item.parentAsin].push(item)
      return acc
    }, {})
  }

  function resetReceiveData() {
    receiveData.value = []
    parentAsinsSet.value.clear()
    allSkus.value = []
  }

  return { receiveData, parentAsinsSet, groupByParentAsin, resetReceiveData }
}
