"use strict";

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

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

var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from, pack) {
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
    if (ar || !(i in from)) {
      if (!ar) ar = Array.prototype.slice.call(from, 0, i);
      ar[i] = from[i];
    }
  }
  return to.concat(ar || Array.prototype.slice.call(from));
};

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/".concat(arg1);
      h5Link = "/product/detail/".concat(arg1, "?productId=").concat(arg2);
      params = {
        type: 'PROD_DETAIL',
        data: {
          offerId: arg1,
          productId: arg2
        }
      };
      break;

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

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

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

    case _constants.urlType.PROD_FILTER:
      pcLink = '';
      h5Link = "/product/list?".concat(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 __spreadArray([], new Set(promotNames), true);
};

exports.getPromotionList = getPromotionList;