/** POD 商品子项，与 useOrderDictionaries 中 options 一致 */
export const PRODUCT_TYPE_POD_SINGLE = 'pod_single'
export const PRODUCT_TYPE_POD_MULTIPLE = 'pod_multiple'
/** 一件定制局部印子项 */
export const PRODUCT_TYPE_CUSTOM_PART_SINGLE = 'custom_part_single'
export const PRODUCT_TYPE_CUSTOM_PART_MULTIPLE = 'custom_part_multiple'

export interface ProductMarkQueryResult {
  productMarkList?: string[]
  podCustomizedQuantity?: string
  cpCustomizedQuantity?: string
}
export function normalizeProductMarkListForQuery(
  raw: string[] | undefined,
): ProductMarkQueryResult {
  if (!raw?.length) {
    return {}
  }
  const set = new Set(raw)
  const marks: string[] = []
  if (set.has('normal')) marks.push('normal')
  if (set.has('custom_normal')) marks.push('custom_normal')
  if (
    set.has(PRODUCT_TYPE_POD_SINGLE) ||
    set.has(PRODUCT_TYPE_POD_MULTIPLE)
  ) {
    marks.push('pod')
  }
  if (
    set.has(PRODUCT_TYPE_CUSTOM_PART_SINGLE) ||
    set.has(PRODUCT_TYPE_CUSTOM_PART_MULTIPLE)
  ) {
    marks.push('custom_part')
  }

  const podS = set.has(PRODUCT_TYPE_POD_SINGLE)
  const podM = set.has(PRODUCT_TYPE_POD_MULTIPLE)
  const partS = set.has(PRODUCT_TYPE_CUSTOM_PART_SINGLE)
  const partM = set.has(PRODUCT_TYPE_CUSTOM_PART_MULTIPLE)

  const podQty =
    podS && podM ? undefined : podS ? 'single' : podM ? 'multiple' : undefined
  const partQty =
    partS && partM ? undefined : partS ? 'single' : partM ? 'multiple' : undefined

  const result: ProductMarkQueryResult = {}
  if (marks.length) result.productMarkList = marks
  if (podQty !== undefined) result.podCustomizedQuantity = podQty
  if (partQty !== undefined) result.cpCustomizedQuantity = partQty
  return result
}
