import { queryForGoodsList } from '../components/mall-index/service';
/* eslint-disable arrow-parens */

/**
 * 传入初始请求的tabs 以及 carList
 * @params {tabs, carList}
 * return leftTabs, goodsList
 */

const initMallData = ({
  tabs = [],
  carList = []
}) => {
  // 封装 goodsList
  const tempGoodsList = []; // 封装 左侧tabs

  const leftTabs = tabs.map(value => {
    const {
      name,
      groupId,
      goods
    } = value; // 单个类别下商品总数量

    let num = 0;
    const tempGoods = goods.map(v => {
      const carIndex = carList.findIndex(carItem => carItem.skuId === v.skuId); // 商品存在于购物车里  找到了

      if (carIndex > -1) {
        // 更新某一分类下的商品总数
        num += carList[carIndex].quantity;
        return { ...v,
          quantity: carList[carIndex].quantity
        };
      }

      return { ...v,
        quantity: 0
      };
    });
    tempGoodsList.push({
      list: tempGoods,
      anchor: groupId
    });
    return {
      title: name,
      anchor: groupId,
      number: num,
      goodsNum: tempGoods.length
    };
  });
  return {
    leftTabs,
    goodsList: tempGoodsList
  };
};
/**
 * 传入左侧tabs goodsList 以及 carList
 * @params {leftTabs, goodsList, carList}
 */


const updateMallData = ({
  leftTabs = [],
  goodsList = [],
  carList = []
}) => {
  if (!carList.length) {
    const newGoodsList = goodsList.map(goodsAnchor => {
      // goodsAnchor: { list: goodsItem[], anchor: string}
      const newGoodsAnchorList = goodsAnchor.list.map(goodsItem => ( // 对应购物车的商品 修改数量 和 状态
      { ...goodsItem,
        quantity: 0
      })); // 更新原来的list

      return { ...goodsAnchor,
        list: newGoodsAnchorList
      };
    }); // 将左侧tabs列表 商品数量置为0
    // { title: name, anchor: groupId, number: num, goodsNum: tempGoods.length, };

    const newLeftTabs = leftTabs.map(v => ({ ...v,
      number: 0
    }));
    return {
      newLeftTabs,
      newGoodsList
    };
  }

  const newGoodsList = goodsList.map((goodsAnchor, index) => {
    // goodsAnchor: { list: goodsItem[], anchor: string}
    const newGoodsAnchorList = goodsAnchor.list.map(goodsItem => {
      //  寻找skuId相等的索引
      const carIndex = carList.findIndex(carItem => goodsItem.skuId === carItem.skuId); // 找到了 用购物车数据更新原列表数据

      if (carIndex > -1) {
        return { ...goodsItem,
          ...carList[carIndex]
        };
      } // 没找到


      return { ...goodsItem,
        quantity: 0
      };
    }); // 某一分类anchor下的数量和

    const anchorTotal = newGoodsAnchorList.reduce((value, item) => value + +item.quantity, 0); // 更新 分类数量总和

    leftTabs[index].number = anchorTotal;
    return { ...goodsAnchor,
      list: newGoodsAnchorList
    };
  });
  return {
    newLeftTabs: leftTabs,
    newGoodsList
  };
};

async function updateGlobalData(car) {
  const {
    carts: carList = []
  } = car;
  this.commitGlobal && this.commitGlobal({
    globalCar: car
  }); // 获取商品列表数据

  const list = await queryForGoodsList({
    shopId: this.data.globalShopId
  }); // 初始化数据

  const {
    leftTabs,
    goodsList
  } = initMallData({
    tabs: list,
    carList
  });
  const {
    newLeftTabs,
    newGoodsList
  } = updateMallData({
    carList,
    leftTabs,
    goodsList
  });
  this.commit && this.commit({
    globalTabs: newLeftTabs,
    globalGoodsList: newGoodsList
  });
}

export default {
  initMallData,
  updateMallData,
  updateGlobalData
};