// 定义源数据的结构类型
interface Option {
  id: number
  name: string
}

interface Category {
  typeName: string
  typeId: number
  options: Option[]
}

type CategoryDataMock = Category[]

// 定义目标数据的结构类型
interface TargetData {
  serialNo: string
  name: string
  sale: string
  img: string
  amount: string
}

export const generateTargetData = (categoryData: CategoryDataMock): TargetData[] => {
  const result: TargetData[] = []
  const optionGroups: Option[][] = categoryData.map((category) => category.options)

  // 使用递归生成所有可能的组合
  const combine = (index: number, current: Option[]) => {
    if (index === optionGroups.length) {
      const serialNo = current.map((opt) => opt.id.toString()).join('-')
      const name = current.map((opt) => opt.name).join('-')
      result.push({
        serialNo,
        name,
        sale: '',
        img: '',
        amount: ''
      })
      return
    }
    for (const option of optionGroups[index]) {
      combine(index + 1, [...current, option])
    }
  }

  combine(0, [])
  return result
}

// 
export const useCategoryAddOption = (categoryData: any) => {
  const inputVisible = ref(false)
  const InputRef = ref()
  const inputValue = ref('')
  const onAddCategoryDataOption = () => {
    inputVisible.value = true
    nextTick(() => {
      InputRef.value!.input!.focus()
    })
  }
  const handleInputConfirm = () => {
    if (inputValue.value) {
      categoryData.value.push(inputValue.value)
    }
    inputVisible.value = false
    inputValue.value = ''
  }
  return { inputVisible, InputRef, inputValue, onAddCategoryDataOption, handleInputConfirm}
}
