"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeSummaryFollower = exports.removeFollower = exports.getUserGuidSetting = exports.getTongjiUsersData = exports.getSummaryFollowerList = exports.getSubordinates = exports.getShareUsersData = exports.getMyDepartMent = exports.getFollowerList = exports.getDepsAndUsers = exports.addSummaryFollower = exports.addFollower = void 0;

var _axios = _interopRequireDefault(require("axios"));

var _request = require("../../utils/request");

var _helper = require("./export-modules/okr-menus/depart-menus/helper");

var _window$BSGlobal$tena, _window$BSGlobal$logi;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 tenantId = window.BSGlobal && ((_window$BSGlobal$tena = window.BSGlobal.tenantInfo) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Id) || 0;
var userId = window.BSGlobal && ((_window$BSGlobal$logi = window.BSGlobal.loginUserInfo) === null || _window$BSGlobal$logi === void 0 ? void 0 : _window$BSGlobal$logi.Id) || 0;

var requestDomain = function requestDomain() {
  if (!window.location.hostname.match(/dev/)) return "";
  return "".concat(location.protocol, "//www.tita.gift");
};

var getUserGuidSetting = function getUserGuidSetting(moduleType) {
  return (0, _request.rpost)("v1")("guidPage/getUserGuidSetting?moduleType=".concat(moduleType));
};

exports.getUserGuidSetting = getUserGuidSetting;

var getFollowerList = function getFollowerList() {
  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/followerList")).then(function (res) {
    return res.data.Data;
  });
};

exports.getFollowerList = getFollowerList;

var getSummaryFollowerList = function getSummaryFollowerList() {
  return _axios["default"].post("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/general/getFollower?moduleType=8")).then(function (res) {
    return res.data.Data;
  });
};

exports.getSummaryFollowerList = getSummaryFollowerList;

// export const checkHasSubordinates = () => {
//   return axios.get(`${requestDomain()}/api/v2/${tenantId}/${userId}/user/getSubUserCount?toUserId=${userId}&countType=1`, {
//     'headers': {
//       'Content-Type': 'application/json; charset=utf-8'
//     },
//   }).then(res => {
//     return res.data;
//   })
// }
var getSubordinates = function getSubordinates(_ref) {
  var _ref$toUserId = _ref.toUserId,
      toUserId = _ref$toUserId === void 0 ? 0 : _ref$toUserId,
      _ref$relationIds = _ref.relationIds,
      relationIds = _ref$relationIds === void 0 ? "3" : _ref$relationIds;
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var disabledDottedSubordinatesDig = options.disabledDottedSubordinatesDig;
  return _axios["default"].get("".concat(requestDomain(), "/api/v2/").concat(tenantId, "/").concat(userId, "/user/getRelationUsers"), {
    headers: {
      "Content-Type": "application/json; charset=utf-8"
    },
    params: {
      relationIds: relationIds,
      toUserId: toUserId || userId
    }
  }).then(function (res) {
    var relationUsers = res.data.Data.relationUsers;

    if (!relationUsers || !Object.keys(relationUsers)) {
      // 没有下属
      return [];
    }

    var _relationUsers$subord = relationUsers.subordinate,
        subordinate = _relationUsers$subord === void 0 ? [] : _relationUsers$subord,
        _relationUsers$dotted = relationUsers.dottedSubordinate,
        dottedSubordinate = _relationUsers$dotted === void 0 ? [] : _relationUsers$dotted,
        _relationUsers$sameLe = relationUsers.sameLevelUser,
        sameLevelUser = _relationUsers$sameLe === void 0 ? [] : _relationUsers$sameLe,
        _relationUsers$mySelf = relationUsers.mySelf,
        mySelf = _relationUsers$mySelf === void 0 ? [] : _relationUsers$mySelf;

    if (disabledDottedSubordinatesDig) {
      dottedSubordinate = dottedSubordinate.map(function (it) {
        it.hasSubordinates = false;
        return it;
      });
    }

    var users = [].concat(_toConsumableArray(subordinate), _toConsumableArray(sameLevelUser), _toConsumableArray(mySelf), _toConsumableArray(dottedSubordinate));
    return users;
  });
};

exports.getSubordinates = getSubordinates;

var getShareUsersData = function getShareUsersData() {
  return (0, _request.rget)("v1")("summary/getShareUsers?toUserId=".concat(userId)).then(function (res) {
    var users = [];

    if (res.Code === 1) {
      users = res.Data;
    }

    return users;
  });
};

exports.getShareUsersData = getShareUsersData;

var getTongjiUsersData = function getTongjiUsersData() {
  return (0, _request.rget)('v2')("user/getRelationUsers?relationIds=3&toUserId=".concat(userId)).then(function (res) {
    var users = [];

    if (res.Code === 1) {
      if (res.Data.relationUsers) {
        users = res.Data.relationUsers.sameLevelUser;
      }
    }

    return users;
  });
};

exports.getTongjiUsersData = getTongjiUsersData;

var addFollower = function addFollower(id) {
  return _axios["default"].post("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/addFollower?followerId=").concat(id), null, {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Accept: "application/json, application/xml, text/play, text/html, *.*"
    },
    withCredentials: true
  }).then(function (res) {
    return res.data;
  });
};

exports.addFollower = addFollower;

var removeFollower = function removeFollower(id) {
  return _axios["default"].post("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/delFollower?followerId=").concat(id), null, {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Accept: "application/json, application/xml, text/play, text/html, *.*"
    },
    withCredentials: true
  }).then(function (res) {
    return res.data;
  });
};

exports.removeFollower = removeFollower;

var getDepsAndUsers = function getDepsAndUsers() {
  var departmentId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
  var isGetUser = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  return (0, _request.rget)("v2")("department/sub/depsAndUsers?departmentId=".concat(departmentId, "&isGetUser=").concat(isGetUser)).then(function (res) {
    var _res$Data = res.Data,
        departments = _res$Data.departments,
        users = _res$Data.users;
    var transDeparts = (0, _helper.translateDepartsToTreeNode)(departments || []);
    var transUsers = (0, _helper.translateUsersToTreeNode)(users || [], departmentId);
    return [].concat(_toConsumableArray(transUsers), _toConsumableArray(transDeparts));
  });
};

exports.getDepsAndUsers = getDepsAndUsers;

var getMyDepartMent = function getMyDepartMent() {
  return (0, _request.rget)('v2')("department/sub/depsAndUsersByPrivilege?objType=8&isGetUser=false");
};

exports.getMyDepartMent = getMyDepartMent;

var addSummaryFollower = function addSummaryFollower(id) {
  return _axios["default"].post("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/general/addFollower?followerId=").concat(id, "&moduleType=8"), null, {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Accept: "application/json, application/xml, text/play, text/html, *.*"
    },
    withCredentials: true
  }).then(function (res) {
    return res.data;
  });
};

exports.addSummaryFollower = addSummaryFollower;

var removeSummaryFollower = function removeSummaryFollower(id) {
  return _axios["default"].post("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/follow/general/delFollower?followerId=").concat(id, "&moduleType=8"), null, {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Accept: "application/json, application/xml, text/play, text/html, *.*"
    },
    withCredentials: true
  }).then(function (res) {
    return res.data;
  });
};

exports.removeSummaryFollower = removeSummaryFollower;