import { isArray } from "lodash";

export function setSelected( types,orderDetail ) {
  function run( name ) {
    switch ( name ) {
      case 'memberGrade':
        // 如果是会员等级减免
        if ( orderDetail.discountInfo?.memberGrade?.length ) {
          const [memberGrade] = orderDetail.discountInfo?.memberGrade;
          if ( !memberGrade?.discountFee ) {
            memberGrade.discountFee = (memberGrade.hasOwnProperty('defaultDiscountTime')? memberGrade.defaultDiscountTime : memberGrade.discountTime ) * orderDetail.parkingRule.hourPrice || 0
          }
          orderDetail.discountInfo.memberGrade = [{
            ...memberGrade,
            selected: memberGrade.hasOwnProperty('selected') ? memberGrade.selected : memberGrade?.defaultSelected || false
          }]
        }
        break
      case 'consume':
        // 如果是消费减免
        if ( orderDetail.discountInfo?.consume?.length ) {
          const [consume] = orderDetail.discountInfo?.consume;
          consume.discountFee = consume.hasOwnProperty('discountFee') ? consume.discountFee : consume.defaultDiscountTime * orderDetail.parkingRule.hourPrice || 0
          orderDetail.discountInfo.consume = [{
            ...consume,
            selected: consume.hasOwnProperty('selected') ? consume.selected : consume?.defaultSelected || false
          }]
        }
        break
      case 'points':
        // 如果是积分减免
        if ( orderDetail.discountInfo?.points?.length ) {
          const [points] = orderDetail.discountInfo?.points;
          if ( !points?.discountFee ) {
            points.discountFee = (points.hasOwnProperty('defaultDiscountTime')? points.defaultDiscountTime : points.discountTime ) * orderDetail.parkingRule.hourPrice || 0
          }
          orderDetail.discountInfo.points = [{
            ...points,
            selected: points.hasOwnProperty('discountFee')
          }]
        }
        break
      case 'paperCoupons':
        // 如果是纸质优惠券的话
        if ( orderDetail.discountInfo?.paperCoupons?.length ) {
          const [paperCoupons] = orderDetail.discountInfo?.paperCoupons;
          orderDetail.discountInfo.paperCoupons = [{
            ...paperCoupons,
            selected: paperCoupons.hasOwnProperty('selected') ? paperCoupons.selected : paperCoupons?.defaultSelected || false
          }]
        }
        break
      case 'coupons':
        // 如果是电子优惠券的话
        if ( orderDetail.discountInfo?.coupons?.length ) {
          orderDetail.discountInfo.coupons = orderDetail.discountInfo.coupons.map(elm => {
            return {
              ...elm,
              selected: elm.hasOwnProperty('selected') ? elm.selected : elm?.defaultSelected || false
            }
          })
        }
        break
    }
  }

  if ( typeof types === 'string' ) {
    run(types)
  }
  if ( isArray(types) ) {
    types.forEach(key => {
      run(key)
    })
  }
}