"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderPreview = exports.parseUrl = exports.listToTree = exports.getPromotionList = void 0;

var _constants = require("../constants");

var parseUrl = function parseUrl(url) {
  var h5Link = url;
  var pcLink = url;
  var params;

  var _a = url.split('-'),
      type = _a[0],
      arg1 = _a[1],
      arg2 = _a[2];

  switch (type) {
    // 商品详情
    case _constants.urlType.PRODUCT_DETAIL:
      pcLink = "/offer-detail/" + arg1;
      h5Link = "/product/detail/" + arg1 + "?productId=" + (arg2 !== null && arg2 !== void 0 ? arg2 : '');
      params = {
        type: 'PROD_DETAIL',
        data: {
          offerId: arg1,
          productId: arg2
        }
      };
      break;

    case _constants.urlType.CATG_PROD_LIST:
      // 商品列表
      pcLink = "/offer-list/catg/" + arg1;
      h5Link = "/product/list/" + arg1;
      params = {
        type: 'PROD_LIST',
        data: {
          catgId: arg1
        }
      };
      break;

    case _constants.urlType.GIVEAWAY_MISSION:
      // 零元购
      pcLink = '';
      h5Link = "/special-mission?campaignId=" + arg1;
      params = {
        type: 'GIVEAWAY_MISSION',
        data: {
          campaignId: arg1
        }
      };
      break;

    case _constants.urlType.CUSTOMIZED:
      // 自定义页面
      pcLink = "/promotion/" + arg1;
      h5Link = "/group/" + arg1;
      params = {
        type: 'CUSTOMIZED',
        data: {
          url: "/group/" + arg1
        }
      };
      break;

    case _constants.urlType.PROD_FILTER:
      pcLink = '';
      h5Link = "/product/list?" + arg1;
      params = {
        type: 'PROD_FILTER',
        data: {
          params: arg1
        }
      };
      break;

    case _constants.urlType.ANCHOR:
      // document.getElementById(id) && document.getElementById(id).scrollIntoView({ behavior: 'smooth' });
      params = {
        type: 'ANCHOR',
        data: {
          id: arg1
        }
      };
      break;

    case _constants.urlType.GET_COUPON:
      // 领取优惠券
      params = {
        type: 'GET_COUPON',
        data: {
          couponId: arg1
        }
      };
      break;

    case '/spike':
      params = {
        type: 'FLASH_SHOP_MORE'
      };
      break;

    default:
      pcLink = url;
      h5Link = url;
      params = {
        type: url
      };
  }

  return {
    pcLink: pcLink,
    h5Link: h5Link,
    params: params
  };
};
/** 刷新预览区域 */


exports.parseUrl = parseUrl;

var renderPreview = function renderPreview() {
  var evt = new Event('render-view', {
    bubbles: false
  });
  window.document.dispatchEvent(evt);
}; // 分类列表 数组转换成树结构


exports.renderPreview = renderPreview;

var listToTree = function listToTree(list) {
  var map = {};
  var node;
  var tree = [];

  for (var i = 0; i < list.length; i++) {
    map[list[i].catgId] = list[i];
    list[i].children = list[i].isLeaf == 0 ? null : [];
  }

  for (var i = 0; i < list.length; i += 1) {
    node = {
      catgName: list[i].catgName,
      value: list[i].catgId,
      key: list[i].catgId,
      children: list[i].children
    };

    if (list[i].parentCatgId !== null) {
      map[list[i].parentCatgId] && map[list[i].parentCatgId].children.push(node);
    } else {
      tree.push(node);
    }
  }

  return tree;
}; // 获取商品活动标签 包含秒杀、团规、满系


exports.listToTree = listToTree;

var getPromotionList = function getPromotionList(offer) {
  var PROMOTION_TYPE_MAP = {
    2: 'SPECIAL PROMO',
    4: 'Group',
    C: 'Discount',
    D: 'Discount',
    G: 'Gift'
  };
  var promotType = offer.promotType,
      sppType = offer.sppType;
  var typeList = []; // 满系

  if (promotType) {
    typeList = promotType.match(/(C|G|D)/g) || [];
  } // 秒杀团购


  if (sppType) {
    typeList = typeList.concat(sppType.split(','));
  }

  var promotNames = typeList.map(function (type) {
    return PROMOTION_TYPE_MAP[type];
  });
  return Array.from(new Set(promotNames));
};

exports.getPromotionList = getPromotionList;