import { isIntersect } from '@/common/util.js'

// 获取选中商品
export function fetchSelectedProducts(goodsList) {
    const productList = []
    goodsList.forEach(shop => productList.push(...shop.productList.filter(prod => prod.productsChecked)))
    return productList
}

// 根据选中商品筛选可用优惠券并排序
export function findCouponBySelected(productIds, couponList) {
    const list = couponList.filter(coupon => coupon.productType === 1 || isIntersect(productIds, coupon.productIds
        .split(',')))
    return couponSort(list)
}

/**
 * 优惠券是否可用
 * 1 无门槛
 * 2 有门槛
 *      1 全部商品可用
 *          全部商品价格总计 >= 当前优惠券的触发金额
 *      2 指定商品可用
 *          当前优惠券可用的商品的价格总计 >= 当前优惠券的触发金额
 * */

// 判断可用优惠券是否满足使用条件 返回抵扣金额最高的优惠券索引 -1:无可用优惠券
export function canUseCoupon(current, productIds, canUseCouponList, selectedPoducts) {
    if (productIds.length <= 0 || canUseCouponList.length <= 0) return -1
    const len = canUseCouponList.length
    if (current >= len) return len
    const currentCoupon = canUseCouponList[current]
    if (
        currentCoupon.noThresholdFlag === 1 ||
        (currentCoupon.productType === 1 && allProdoceUseCheck(selectedPoducts, currentCoupon)) ||
        (currentCoupon.productType === 2 && someProductUseCheck(selectedPoducts, currentCoupon))
    ) {
        return current
    } else {
        return canUseCoupon(++current, productIds, canUseCouponList, selectedPoducts)
    }
}

// 判断全部商品可用 (全部商品价格总计 是否大于 当前优惠券的触发金额)
export function allProdoceUseCheck(productList, coupon) {
    const countPrice = productList.reduce((countPrice, prod) => countPrice + prod.price * prod.productCount, 0)
    return countPrice >= coupon.touchPrice
}

// 判断指定商品可用 (当前优惠券可用的商品的价格总计 是否大于 当前优惠券的触发金额)
export function someProductUseCheck(productList, coupon) {
    const countPrice = productList.reduce((countPrice, prod) => {
        // 当前优惠券可用的商品总价
        const isIncludes = coupon.productIds.indexOf(prod.productId.toString()) > -1
        return isIncludes ? countPrice + prod.price * prod.productCount : countPrice
    }, 0)
    return countPrice >= coupon.touchPrice
}

// 计算当前优惠券可使用的商品的总价
export function getCountPrice(productList, coupon) {
    let countPrice = 0
    // 针对全部商品
    if (coupon.productType === 1) {
        countPrice = productList.reduce((countPrice, prod) => countPrice + prod.price * prod.productCount, 0)
    }
    // 只针对优惠券可用商品
    if (coupon.productType === 2) {
        countPrice = productList.reduce((countPrice, prod) => {
            // 当前优惠券可用的商品总价
            const isIncludes = coupon.productIds.indexOf(prod.productId.toString()) > -1
            return isIncludes ? countPrice + prod.price * prod.productCount : countPrice
        }, 0)
    }
    return countPrice
}

// 将优惠券按抵扣金额从大到小排序
export function couponSort(couponList) {
    return couponList.sort((a, b) => b.couponAmount - a.couponAmount)
}

// 对优惠券去重 过滤掉相同id的优惠券
export function couponFilterRepeat(couponList) {
    const list = []
    couponList.forEach(coupon => {
        if (list.findIndex(v => v.couponId === coupon.couponId) === -1) list.push(coupon)
    })
    return list
}

// 为优惠券设置唯一ID
export function setCouponUniqueId(couponList = []) {
    return couponList.map((coupon, index) => {
        coupon.uniqueId = index
        return coupon
    }) 
}
