"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.searchGroups = exports.searchDuties = exports.search = exports.requestDomain = exports.getTreeData = exports.getSubordinateData = exports.getGourps = exports.getDuties = void 0;

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

var _utils = require("./utils");

var _enum = require("./enum");

var _BSGlobal, _BSGlobal$tenantInfo, _BSGlobal2, _BSGlobal2$loginUserI;

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 = ((_BSGlobal = BSGlobal) === null || _BSGlobal === void 0 ? void 0 : (_BSGlobal$tenantInfo = _BSGlobal.tenantInfo) === null || _BSGlobal$tenantInfo === void 0 ? void 0 : _BSGlobal$tenantInfo.Id) || 0;
var userId = ((_BSGlobal2 = BSGlobal) === null || _BSGlobal2 === void 0 ? void 0 : (_BSGlobal2$loginUserI = _BSGlobal2.loginUserInfo) === null || _BSGlobal2$loginUserI === void 0 ? void 0 : _BSGlobal2$loginUserI.Id) || 0;

var requestDomain = function requestDomain() {
  var modulePath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var domain = modulePath.length > 0 ? window.BSGlobal[modulePath] : window.BSGlobal['apiPath'];
  return "".concat(location.protocol).concat(domain);
};

exports.requestDomain = requestDomain;

var getDepartments = function getDepartments(_ref) {
  var nodeId = _ref.nodeId,
      _ref$isGetUser = _ref.isGetUser,
      isGetUser = _ref$isGetUser === void 0 ? true : _ref$isGetUser,
      _ref$auth = _ref.auth,
      auth = _ref$auth === void 0 ? false : _ref$auth,
      authType = _ref.authType,
      isGetRoot = _ref.isGetRoot,
      isLoadNodes = _ref.isLoadNodes,
      _ref$staffStatus = _ref.staffStatus,
      staffStatus = _ref$staffStatus === void 0 ? _enum.StaffStatus.OnTheJob : _ref$staffStatus;
  // 根据权限类型使用不同的objtype
  var authTypeId;
  if (auth && !authType) authType = 'okr';
  if (authType == 'okr') authTypeId = '62';
  if (authType == 'performance') authTypeId = '31';
  if (authType == 'survey') authTypeId = '83';
  if (authType == 'total') authTypeId = '0';
  if (authType == 'summary') authTypeId = '8'; // 获取根节点
  // 第一次获取根部门 nodeId == -1

  var isGetRootNode = isGetRoot && !isLoadNodes;
  var prevPath = "".concat(requestDomain(), "/api/v2/").concat(tenantId, "/").concat(userId, "/department/sub"); // 请求参数

  var urlParams = "departmentId=".concat(nodeId, "&isGetUser=").concat(isGetUser).concat(isGetRootNode ? '&isRoot=true' : '');
  return _axios["default"].get(auth ? "".concat(prevPath, "/depsAndUsersByPrivilege?").concat(urlParams, "&objtype=").concat(authTypeId, "&staffStatus=").concat(staffStatus) : "".concat(prevPath, "/depsAndUsers?").concat(urlParams, "&staffStatus=").concat(staffStatus)).then(function (departs) {
    if (isGetRootNode) {
      // 第一次获取根节点
      // 无权时，获取的节点列表中存在 -1 的节点，用 -1 的节点作为根节点
      // 有权时，获取的节点列表可能存在多个根部门
      var firstRootNodes = [];
      var hasGetRootNode = false;
      var _departs$data$Data$de = departs.data.Data.departments,
          departments = _departs$data$Data$de === void 0 ? [] : _departs$data$Data$de;

      for (var i = 0; i < (departments || []).length; i++) {
        var it = (departments || [])[i];

        if (it.id == '-1') {
          hasGetRootNode = true;
          firstRootNodes.push(it);
          break;
        }
      }

      if (!hasGetRootNode) firstRootNodes = _toConsumableArray(departments || []);
      return {
        Data: {
          departments: firstRootNodes,
          users: []
        }
      };
    }

    return departs.data;
  });
};

var getGourps = function getGourps(_ref2) {
  var _ref2$sourceFrom = _ref2.sourceFrom,
      sourceFrom = _ref2$sourceFrom === void 0 ? '' : _ref2$sourceFrom,
      getUsedGroups = _ref2.getUsedGroups;

  // 获取有OKR的群组
  if (getUsedGroups) {
    return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/okr/map/group")).then(function (groups) {
      return groups.data;
    });
  }

  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/Group/getAll").concat(sourceFrom ? "?sourceFrom=".concat(sourceFrom) : '')).then(function (groups) {
    return groups.data;
  });
};

exports.getGourps = getGourps;

var getTreeData = function getTreeData(id, opts, resolve) {
  var _opts$departOnly = opts.departOnly,
      departOnly = _opts$departOnly === void 0 ? false : _opts$departOnly,
      auth = opts.auth,
      _opts$isLoadNodes = opts.isLoadNodes,
      isLoadNodes = _opts$isLoadNodes === void 0 ? false : _opts$isLoadNodes,
      isGetRoot = opts.isGetRoot,
      authType = opts.authType,
      staffStatus = opts.staffStatus;
  return getDepartments({
    nodeId: id,
    isGetUser: !departOnly,
    // 接口返回值是否带有人员
    auth: auth,
    isGetRoot: isGetRoot,
    authType: authType,
    isLoadNodes: isLoadNodes,
    staffStatus: staffStatus
  }).then(function (res) {
    var _res$Data = res.Data,
        _res$Data$departments = _res$Data.departments,
        departments = _res$Data$departments === void 0 ? [] : _res$Data$departments,
        _res$Data$users = _res$Data.users,
        users = _res$Data$users === void 0 ? [] : _res$Data$users;
    var transDeparts = (0, _utils.translateDepartsToTreeNode)(departments || []);
    var transUsers = (0, _utils.translateUsersToTreeNode)(users || [], id);
    resolve && resolve([].concat(_toConsumableArray(transUsers), _toConsumableArray(transDeparts)));
    return [].concat(_toConsumableArray(transUsers), _toConsumableArray(transDeparts));
  });
};

exports.getTreeData = getTreeData;

var getSubordinateData = function getSubordinateData(_ref3) {
  var auth = _ref3.auth,
      authType = _ref3.authType;
  var authTypeId;
  if (auth && !authType) authType = 'okr';
  if (authType == 'okr') authTypeId = '62';
  if (authType == 'performance') authTypeId = '31';
  if (authType == 'survey') authTypeId = '83';
  if (authType == 'total') authTypeId = '0';
  return _axios["default"].get("".concat(requestDomain(), "/api/v2/").concat(tenantId, "/").concat(userId, "/org/subordinate?to_userId=").concat(userId)).then(function (res) {
    // const { users = [] } = res.data.Data;
    var data = res.data.Data;
    var users = [];
    data.forEach(function (ele) {
      users.push(ele.user);
    });
    var transUsers = (0, _utils.translateUsersToTreeNode)(users || []); // resolve && resolve([...transUsers]);

    return _toConsumableArray(transUsers);
  });
};

exports.getSubordinateData = getSubordinateData;

var search = function search(_ref4) {
  var searchKey = _ref4.searchKey,
      departOnly = _ref4.departOnly,
      searchUserOnly = _ref4.searchUserOnly,
      panes = _ref4.panes,
      _ref4$auth = _ref4.auth,
      auth = _ref4$auth === void 0 ? false : _ref4$auth,
      _ref4$isVerify = _ref4.isVerify,
      isVerify = _ref4$isVerify === void 0 ? false : _ref4$isVerify,
      _ref4$staffStatus = _ref4.staffStatus,
      staffStatus = _ref4$staffStatus === void 0 ? _enum.StaffStatus.OnTheJob : _ref4$staffStatus;
  var isGetUser = true;
  var isGetDep = true;
  var isGetGroup = false;

  if (panes) {
    var depart = panes.depart,
        group = panes.group;

    if (depart) {
      isGetDep = true;
    }

    if (group) {
      isGetGroup = true;
    }
  }

  if (departOnly) {
    isGetUser = false;
  }

  if (searchUserOnly) {
    isGetDep = false;
    isGetGroup = false;
  }

  return _axios["default"].get("".concat(requestDomain(), "/api/v2/").concat(tenantId, "/").concat(userId, "/search/searchAll?keyWord=").concat(searchKey, "&isVerify=").concat(isVerify, "&isGetUser=").concat(isGetUser, "&isGetDep=").concat(isGetDep, "&isGetGroup=").concat(isGetGroup, "&staffStatus=").concat(staffStatus)).then(function (searchRes) {
    return searchRes.data;
  });
};

exports.search = search;

var searchGroups = function searchGroups(_ref5) {
  var searchKey = _ref5.searchKey;
  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/okr/map/group?keyWork=").concat(searchKey)).then(function (searchRes) {
    return searchRes.data;
  });
};

exports.searchGroups = searchGroups;

var getDuties = function getDuties() {
  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/position/get?status=1&pageNum=1&pageSize=1000")).then(function (res) {
    return res.data;
  });
};

exports.getDuties = getDuties;

var searchDuties = function searchDuties(_ref6) {
  var searchKey = _ref6.searchKey;
  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/position/search?status=1&pageNum=1&pageSize=100&filter=").concat(searchKey)).then(function (res) {
    return res.data;
  });
};

exports.searchDuties = searchDuties;