"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addGroup = addGroup;
exports.addGroupStaff = addGroupStaff;
exports.deleteGroupType = deleteGroupType;
exports.editGroup = editGroup;
exports.getApiUrl = getApiUrl;
exports.getDomain = getDomain;
exports.getGroupList = getGroupList;
exports.getTenantInfo = getTenantInfo;
exports.getUserInfo = getUserInfo;
exports.getVisible = getVisible;
exports.isWx = exports.isDingDing = void 0;
exports.removeGroupStaff = removeGroupStaff;
exports.searchGroupStaffList = searchGroupStaffList;
exports.searchUserDetail = searchUserDetail;
exports.taker = taker;

var _BSGlobal$tenantInfo, _BSGlobal$loginUserIn;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var tenantId = BSGlobal && ((_BSGlobal$tenantInfo = BSGlobal.tenantInfo) === null || _BSGlobal$tenantInfo === void 0 ? void 0 : _BSGlobal$tenantInfo.Id) || 0;
var userId = BSGlobal && ((_BSGlobal$loginUserIn = BSGlobal.loginUserInfo) === null || _BSGlobal$loginUserIn === void 0 ? void 0 : _BSGlobal$loginUserIn.Id) || 0;

function getTenantInfo() {
  return BSGlobal && BSGlobal.tenantInfo;
}

function getUserInfo() {
  return BSGlobal && BSGlobal.loginUserInfo;
} // 模拟获取基础路径


function getDomain() {
  if (!window.location.hostname.match(/dev/)) return "";
  return "http://www.tita.gift";
}
/**
 * 封装taker
 * @param {路由} url
 */


function taker(_x, _x2, _x3) {
  return _taker.apply(this, arguments);
}
/**
 * 拼接 v1 url
 * @param {v1/之后的路径} url
 */


function _taker() {
  _taker = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url, method, body) {
    var header,
        settings,
        bodyStr,
        key,
        response,
        data,
        _args = arguments;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            header = _args.length > 3 && _args[3] !== undefined ? _args[3] : {};
            settings = {};

            if (method) {
              settings.headers = _objectSpread({
                Accept: "application/json, application/xml, text/play, text/html, *.*",
                "Content-Type": "application/json; charset=utf-8"
              }, header);
              settings.method = method;
            }

            if (body) settings.body = JSON.stringify(body);

            if (settings.headers) {
              if (settings.headers["Content-Type"] == "application/x-www-form-urlencoded") {
                bodyStr = "";

                for (key in body) {
                  bodyStr += "".concat(key, "=").concat(body[key], "&");
                }

                bodyStr.substring(0, bodyStr.length - 1);
                settings.body = bodyStr;
              }
            }

            settings.credentials = "same-origin"; // 有些组件里的参数已经做过一次编码****

            url = encodeURI(decodeURI(url));
            _context.next = 9;
            return fetch(url, settings);

          case 9:
            response = _context.sent;
            _context.next = 12;
            return response.json();

          case 12:
            data = _context.sent;
            return _context.abrupt("return", data);

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _taker.apply(this, arguments);
}

function getApiUrl(url) {
  var apiServer = getDomain(); // 获取基础路径***

  return "".concat(apiServer, "/api/v1/").concat(tenantId, "/").concat(userId, "/").concat(url);
}
/**
 * 检索群组列表
 * @param {*}
 */


function getGroupList() {
  var url = getApiUrl("group/getAll?sourceFrom=fromback");
  return taker(url, "GET").then(function (res) {
    return res;
  });
}
/**
 * 添加群组列表
 * @param {群组名} groupName
 * @param {类型} groupvisible 0是全部,1是选特定人
 * @param {群组可使用人} groupvisible
 */


function addGroup(obj) {
  var apiUrl = getApiUrl("group/add");
  return taker(apiUrl, "POST", {
    groupName: obj.groupName,
    groupVisiblity: obj.groupVisiblity,
    selectUserids: obj.selectUserids,
    selectDepartmentIds: obj.selectDepartmentIds,
    selectGroupIds: obj.selectGroupIds
  }).then(function (res) {
    return res;
  });
}
/**
 * 编辑群组列表前要获取群组可见范围
 * @param {群组名称} groupName
 * @param {群组id} groupId
 * @param {群组可使用人} groupvisible
 */


function getVisible(groupId) {
  var apiUrl = getApiUrl("group/getVisible");
  var url = "".concat(apiUrl, "?groupId=").concat(groupId);
  return taker(url, "GET").then(function (res) {
    return res;
  });
}
/**
 * 编辑群组列表
 * @param {群组名称} groupName
 * @param {群组id} groupId
 * @param {群组可使用人} groupvisible
 */


function editGroup(obj) {
  var apiUrl = getApiUrl("group/edit");
  return taker(apiUrl, "POST", {
    groupName: obj.groupName,
    groupId: obj.groupId,
    groupVisiblity: obj.groupVisiblity,
    selectUserids: obj.selectUserids,
    selectDepartmentIds: obj.selectDepartmentIds,
    selectGroupIds: obj.selectGroupIds
  }).then(function (res) {
    return res;
  });
}
/**
 * 删除群组
 * @param {群组名称} groupName
 * @param {群组id} groupId
 * @param {群组可使用人} groupvisible
 */


function deleteGroupType(id) {
  var apiUrl = getApiUrl("group/delete");
  var url = "".concat(apiUrl, "?groupId=").concat(id);
  return taker(url, "POST").then(function (res) {
    return res;
  });
}
/**
 * 检索群组人员列表
 * @param {群组id} groupid
 * @param {群组当前页} groupPageNum
 */


function searchGroupStaffList(groupid, groupPageNum, keyword) {
  var apiUrl = getApiUrl("group/user/get");
  return taker(apiUrl, "POST", {
    groupid: groupid,
    pagenum: groupPageNum,
    pagesize: 10,
    keyWords: keyword
  }).then(function (res) {
    return res;
  });
}
/**
 * 添加员工
 * @param {群组id} groupId
 * @param {selectedStaffList} list 选人列表的所有信息
 * groupid,（selectUserids或者selectDepartmentIds或者selectGroupIds必填）
 */


function addGroupStaff(opt) {
  var apiUrl = getApiUrl("group/user/add");
  return taker(apiUrl, "POST", _objectSpread({}, opt)).then(function (res) {
    return res;
  });
}
/**
 * 移除员工
 * @param {群组id} groupId
 * @param {selectedStaffList} list 选人列表的所有信息
 */


function removeGroupStaff(list, groupId) {
  var apiUrl = getApiUrl("group/user/remove");
  return taker(apiUrl, "POST", {
    groupid: groupId,
    selectUserids: list
  }).then(function (res) {
    return res;
  });
}
/**
 * 获取用户添加时的部门、群组信息
 * @param {群组id} groupId
 */


function searchUserDetail(groupId) {
  var apiUrl = getApiUrl("group/user/detail");
  var url = "".concat(apiUrl, "?groupId=").concat(groupId);
  return taker(url, "get");
}
/*
 *   判断是钉钉
 */


var isDingDing = function isDingDing() {
  var _getTenantInfo;

  return ((_getTenantInfo = getTenantInfo()) === null || _getTenantInfo === void 0 ? void 0 : _getTenantInfo.Source) == 861;
};
/*
*   判断是企业微信
*/


exports.isDingDing = isDingDing;

var isWx = function isWx() {
  var _getTenantInfo2;

  return ((_getTenantInfo2 = getTenantInfo()) === null || _getTenantInfo2 === void 0 ? void 0 : _getTenantInfo2.Source) == 860;
};

exports.isWx = isWx;