"use strict";

var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
var __generator = this && this.__generator || function (thisArg, body) {
  var _ = {
      label: 0,
      sent: function sent() {
        if (t[0] & 1) throw t[1];
        return t[1];
      },
      trys: [],
      ops: []
    },
    f,
    y,
    t,
    g;
  return g = {
    next: verb(0),
    "throw": verb(1),
    "return": verb(2)
  }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
    return this;
  }), g;
  function verb(n) {
    return function (v) {
      return step([n, v]);
    };
  }
  function step(op) {
    if (f) throw new TypeError("Generator is already executing.");
    while (_) try {
      if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
      if (y = 0, t) op = [op[0] & 2, t.value];
      switch (op[0]) {
        case 0:
        case 1:
          t = op;
          break;
        case 4:
          _.label++;
          return {
            value: op[1],
            done: false
          };
        case 5:
          _.label++;
          y = op[1];
          op = [0];
          continue;
        case 7:
          op = _.ops.pop();
          _.trys.pop();
          continue;
        default:
          if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
            _ = 0;
            continue;
          }
          if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
            _.label = op[1];
            break;
          }
          if (op[0] === 6 && _.label < t[1]) {
            _.label = t[1];
            t = op;
            break;
          }
          if (t && _.label < t[2]) {
            _.label = t[2];
            _.ops.push(op);
            break;
          }
          if (t[2]) _.ops.pop();
          _.trys.pop();
          continue;
      }
      op = body.call(thisArg, _);
    } catch (e) {
      op = [6, e];
      y = 0;
    } finally {
      f = t = 0;
    }
    if (op[0] & 5) throw op[1];
    return {
      value: op[0] ? op[1] : void 0,
      done: true
    };
  }
};
exports.__esModule = true;
exports.lottery = exports.getWxAppid = exports.getEnv = exports.getSearchParams = exports.b64toFile = exports.$_GET = exports.getObjData = exports.getSchemaAll = exports.getSchema = exports.delCookieData = exports.getCookieData = exports.setCookieData = void 0;
var js_cookie_1 = require("js-cookie");
var lowcode_engine_1 = require("@alilc/lowcode-engine");
var base_1 = require("../api/base");
var map = {
  cookiePrefix: '__xiatu__'
};
function setCookieData(jsonData) {
  if (jsonData === void 0) {
    jsonData = {
      token: ''
    };
  }
  var curCookie = getCookieData();
  js_cookie_1["default"].set(map.cookiePrefix + "cookie_data", JSON.stringify(__assign(__assign({}, curCookie), jsonData)));
}
exports.setCookieData = setCookieData;
function getCookieData() {
  var _cookie = js_cookie_1["default"].get(map.cookiePrefix + "cookie_data");
  return _cookie ? JSON.parse(_cookie) : {
    token: ''
  };
}
exports.getCookieData = getCookieData;
function delCookieData(keyName) {
  var curCookie = getCookieData();
  delete curCookie[keyName];
  js_cookie_1["default"].set(map.cookiePrefix + "cookie_data", JSON.stringify(curCookie));
}
exports.delCookieData = delCookieData;
var $_GET = function () {
  var search = window.location.href.split('?');
  var searchArr = search[1] ? search[1].split('&') : [];
  var _get = {};
  for (var item in searchArr) {
    if (typeof Array.prototype[item] === 'undefined') {
      var params = searchArr[item].split('=');
      _get[params[0]] = decodeURIComponent(params[1]);
    }
  }
  return _get;
}();
exports.$_GET = $_GET;
// 获取当前页面
function getSchema() {
  var projectSchema = lowcode_engine_1.project.exportSchema();
  var componentsMapArray = projectSchema.componentsMap,
    componentsTree = projectSchema.componentsTree;
  var componentsMap = {};
  componentsMapArray && componentsMapArray.forEach(function (component) {
    componentsMap[component.componentName] = component;
  });
  var schema = componentsTree[0];
  return schema;
}
exports.getSchema = getSchema;
// 获取当前页面所有参数
function getSchemaAll() {
  var projectSchema = lowcode_engine_1.project.exportSchema();
  var componentsMapArray = projectSchema.componentsMap,
    componentsTree = projectSchema.componentsTree;
  var componentsMap = {};
  componentsMapArray && componentsMapArray.forEach(function (component) {
    componentsMap[component.componentName] = component;
  });
  var obj = {
    componentsMap: componentsMap,
    componentsTree: componentsTree
  };
  return obj;
}
exports.getSchemaAll = getSchemaAll;
// 获取浏览器的search
function getSearchParams(search) {
  var searchParams = {};
  // 如果 location 中有 search 值 ，去除 search 值中的第一个字符 ‘?’
  var searchStr = search.substring(1);
  // 以 & 符为分隔符，解析出 key value 组合 ，组合的形式为 'key=value'
  var searchArr = searchStr.length ? searchStr.split('&') : [];
  searchArr.forEach(function (item) {
    // 以 = 为分隔符，解析出 key 和 value , 其中数组的第一项为 key ，数组的第二项为 value
    var keyAndValue = item.split('=');
    // 如果查询参数中包含中文，会被编码成一串字符串，需要使用 decodeURIComponent 进行 解码
    var key = decodeURIComponent(keyAndValue[0]);
    var value = decodeURIComponent(keyAndValue[1]);
    if (key) {
      searchParams[key] = value;
    }
  });
  return searchParams;
}
exports.getSearchParams = getSearchParams;
// 获取对象类型数据
function getObjData() {
  // const temp = window.JSON.parse(window.JSON.stringify(e));
  // console.log('temp', temp);
  // return new Function(`return ${temp}`)();
}
exports.getObjData = getObjData;
function b64toFile(dataurl, filename) {
  var arr = dataurl.split(',');
  var mime = arr[0].match(/:(.*?);/)[1];
  var bstr = atob(arr[1]);
  var n = bstr.length;
  var u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, {
    type: mime
  });
}
exports.b64toFile = b64toFile;
/**
 * 获取环境变量
 */
function getEnv() {
  switch (true) {
    case process.env.NODE_ENV === 'development':
      // 开发
      switch (true) {
        case /^(192\.168(\.((25[0-5])|(2[0-4]\d)|([1]\d{0,2})|\d{1,2})){2}|10(\.((25[0-5])|(2[0-4]\d)|([1]\d{0,2})|\d{1,2})){3}|172\.(1[6-9]|2\d|3[0-1])(\.((25[0-5])|(2[0-4]\d)|([1]\d{0,2})|\d{1,2})){2})$/g.test(location.hostname):
          // 开发
          return 'lan';
        // 局域网环境
        case location.hostname === 'localhost':
        default:
          return 'local';
        // 本地
      }

    case process.env.NODE_ENV === 'production': // 生产
    default:
      switch (true) {
        case /(?=[^/])([-\w]+\.)+?maimairen\.net$/gi.test(location.hostname):
          return 'test';
        // 测试环境
        case /(?=[^/])([-\w]+\.)+?maimairen\.com$/gi.test(location.hostname):
        default:
          return 'prod';
        // 正式环境
      }
  }
}

exports.getEnv = getEnv;
// 获取微信的 appid
var getWxAppid = getEnv() === 'prod' ? 'wxe7bfcd062bfb8169' : 'wxe677e4aad49ba784';
exports.getWxAppid = getWxAppid;
// 去微信授权登录
var loginWechat = function loginWechat(url) {
  window.location.href = url || "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + getWxAppid + "&redirect_uri=" + encodeURIComponent(window.location.href) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
};
// 抽奖
var lottery = function lottery(id) {
  var activityId = $_GET.activityId,
    code = $_GET.code;
  return new Promise(function (resolve) {
    return __awaiter(void 0, void 0, void 0, function () {
      var _a;
      return __generator(this, function (_b) {
        switch (_b.label) {
          case 0:
            if (!code) resolve({
              code: 10401,
              data: loginWechat,
              msg: '您还没有登录'
            });
            if (!(id || activityId)) return [3 /*break*/, 2];
            _a = resolve;
            return [4 /*yield*/, base_1.lottery({
              b: id || activityId
            })];
          case 1:
            _a.apply(void 0, [_b.sent()]);
            _b.label = 2;
          case 2:
            return [2 /*return*/];
        }
      });
    });
  });
};

exports.lottery = lottery;