"use strict";
const common_vendor = require("../../common/vendor.js");
const sheep_store_index = require("../store/index.js");
const sheep_platform_index = require("./index.js");
const sheep_router_index = require("../router/index.js");
const sheep_url_index = require("../url/index.js");
const platformMap = ["H5", "WechatOfficialAccount", "WechatMiniProgram", "App"];
const fromMap = ["forward", "poster", "link"];
const getShareInfo = (scene = {
  title: "",
  // 自定义分享标题
  desc: "",
  // 自定义描述
  image: "",
  // 自定义分享图片
  params: {}
  // 自定义分享参数
}, poster = {
  // 自定义海报数据
  type: "user"
}) => {
  let shareInfo = {
    title: "",
    // 分享标题
    desc: "",
    // 描述
    image: "",
    // 分享图片
    path: "",
    // 分享页面+参数
    link: "",
    // 分享Url+参数
    query: "",
    // 分享参数
    poster
    // 海报所需数据
  };
  const app = sheep_store_index.$store("app");
  const shareConfig = app.platform.share;
  const query = buildSpmQuery(scene.params);
  shareInfo.query = query;
  shareInfo.link = buildSpmLink(query, shareConfig.linkAddress);
  if (shareConfig.methods.includes("forward")) {
    if (shareConfig.forwardInfo.title === "" || shareConfig.forwardInfo.image === "") {
      console.log("请在平台设置中配置转发信息");
    }
    shareInfo.title = scene.title || shareConfig.forwardInfo.title;
    shareInfo.image = sheep_url_index.$url.cdn(scene.image || shareConfig.forwardInfo.image);
    shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
    shareInfo.path = buildSpmPath(query);
  }
  return shareInfo;
};
const buildSpmQuery = (params) => {
  const user = sheep_store_index.$store("user");
  let shareId = "0";
  if (typeof params.shareId === "undefined") {
    if (user.isLogin) {
      shareId = user.userInfo.id;
    }
  }
  let page = "1";
  if (typeof params.page !== "undefined") {
    page = params.page;
  }
  let query = "0";
  if (typeof params.query !== "undefined") {
    query = params.query;
  }
  let platform = platformMap.indexOf(sheep_platform_index._platform.name) + 1;
  let from = "1";
  if (typeof params.from !== "undefined") {
    from = platformMap.indexOf(params.from) + 1;
  }
  return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
};
const buildSpmPath = (query) => {
  return `/pages/index/index?${query}`;
};
const buildSpmLink = (query, linkAddress = "") => {
  return `${linkAddress}?${query}`;
};
const decryptSpm = (spm) => {
  const user = sheep_store_index.$store("user");
  let shareParamsArray = spm.split(".");
  let shareParams = {
    spm,
    shareId: 0,
    page: "",
    query: {},
    platform: "",
    from: ""
  };
  let query;
  shareParams.shareId = shareParamsArray[0];
  switch (shareParamsArray[1]) {
    case "1":
      shareParams.page = "/pages/index/index";
      break;
    case "2":
      shareParams.page = "/pages/goods/index";
      shareParams.query = {
        linkId: shareParamsArray[2]
      };
      console.log(shareParams.query);
      break;
    case "3":
      shareParams.page = "/pages/goods/groupon";
      query = shareParamsArray[2].split(",");
      shareParams.query = {
        id: query[0],
        activity_id: query[1]
      };
      break;
    case "4":
      shareParams.page = "/pages/goods/seckill";
      query = shareParamsArray[2].split(",");
      shareParams.query = {
        id: query[1]
      };
      break;
    case "5":
      shareParams.page = "/pages/activity/groupon/detail";
      shareParams.query = {
        id: shareParamsArray[2]
      };
      break;
  }
  shareParams.platform = platformMap[shareParamsArray[3] - 1];
  shareParams.from = fromMap[shareParamsArray[4] - 1];
  if (shareParams.shareId != 0) {
    if (user.isLogin) {
      user.addShareLog(shareParams);
    } else {
      common_vendor.index.setStorageSync("shareLog", shareParams);
    }
  }
  if (shareParams.page !== "/pages/index/index") {
    sheep_router_index.$router.go(shareParams.page, shareParams.query);
  }
  return shareParams;
};
const updateShareInfo = (shareInfo) => {
};
const $share = {
  getShareInfo,
  updateShareInfo,
  decryptSpm
};
exports.$share = $share;
