"use strict";

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

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

var _dateUtil = require("./dateUtil");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

// 获取优惠券有效期描述
var getCouponVaildity = function getCouponVaildity(coupon) {
  if (coupon.getOffsetExp) {
    return "Valid within ".concat(coupon.getOffsetExp, " days after collection");
  }

  return "".concat((0, _dateUtil.formatDate)(coupon.effDate), "~").concat((0, _dateUtil.formatDate)(coupon.expDate));
};

exports.getCouponVaildity = getCouponVaildity;

var getCouponCollectTime = function getCouponCollectTime(coupon) {
  return "".concat((0, _dateUtil.formatDateTime)(coupon.getStartDate), "~").concat((0, _dateUtil.formatDateTime)(coupon.getEndDate));
}; // 获取商品活动标签 包含秒杀、团规、满系


exports.getCouponCollectTime = getCouponCollectTime;

var getPromotionList = function getPromotionList(offer, locale) {
  var PROMOTION_TYPE_MAP = {
    2: locale.FLASH,
    // 秒杀
    4: locale.GROUP,
    // 团购
    C: locale.DISCOUNT,
    // 满减
    D: locale.DISCOUNT,
    // 满折
    G: locale.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 _toConsumableArray(new Set(promotNames));
};

exports.getPromotionList = getPromotionList;

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

  var _url$split = url.split('-'),
      _url$split2 = _slicedToArray(_url$split, 3),
      type = _url$split2[0],
      arg1 = _url$split2[1],
      arg2 = _url$split2[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;