"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultRefObject = exports["default"] = exports.PAGE_OKR_MAP = exports.PAGE_OKR_FUNCTION = exports.PAGE_OKR_DASHBOARD = exports.PAGE_OKR_CASES = exports.PAGE_MY_TARGET_OKR = exports.PAGE_MY_PART_OKR = exports.PAGE_MY_OWNER_OKR = void 0;

var _auth = require("../../../../utils/auth");

var _i18n = require("../../i18n");

var _index = require("../../components/menu-tree/tree-node/index");

var _helpers = require("../../../../utils/helpers");

var _getLocale = require("../../../../utils/getLocale");

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; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var defaultRefObject = {
  menus: [],
  insertConcern: function insertConcern() {},
  insertSubordinates: function insertSubordinates() {},
  getMenus: function getMenus() {},
  setConcernLoading: function setConcernLoading() {},
  setSubordinatesLoading: function setSubordinatesLoading() {},
  addConcerner: function addConcerner() {},
  removeConcerner: function removeConcerner() {},
  // 'showSubordinateMenu': () => { },
  USERCONCERN: '',
  SUBORDINATES: '',
  MYOKR: ''
};
exports.defaultRefObject = defaultRefObject;
var PAGE_MY_OWNER_OKR = 'myownerokr';
exports.PAGE_MY_OWNER_OKR = PAGE_MY_OWNER_OKR;
var PAGE_MY_PART_OKR = 'mypartokr';
exports.PAGE_MY_PART_OKR = PAGE_MY_PART_OKR;
var PAGE_MY_TARGET_OKR = 'mytargetokr';
exports.PAGE_MY_TARGET_OKR = PAGE_MY_TARGET_OKR;
var PAGE_OKR_MAP = 'map';
exports.PAGE_OKR_MAP = PAGE_OKR_MAP;
var PAGE_OKR_DASHBOARD = 'panzi';
exports.PAGE_OKR_DASHBOARD = PAGE_OKR_DASHBOARD;
var PAGE_OKR_FUNCTION = 'panel';
exports.PAGE_OKR_FUNCTION = PAGE_OKR_FUNCTION;
var PAGE_OKR_CASES = 'cases';
exports.PAGE_OKR_CASES = PAGE_OKR_CASES;

var okrMenus = /*#__PURE__*/function () {
  function okrMenus(props) {
    var _window$BSGlobal$logi;

    _classCallCheck(this, okrMenus);

    this.COMPANYALL = _index.COMPANY_ALL_NODE;
    this.USERCONCERN = _index.USER_CONCERN_NODE;
    this.MYOKR = 'myokr';
    this.SUBORDINATES = 'subordinates';
    this.TONGJI = _index.TONGJI_NODE;
    var hasDashboardAuth = false;
    var baseAuth = new _auth.BaseAuth();

    if (baseAuth.isBoss() || baseAuth.isManager() || baseAuth.isDepartmentLeader()) {
      hasDashboardAuth = true;
    }

    var isUseNewMenu = window.BSGlobal.isUseNewMenu;
    this.menus = [{
      icon: 'okr',
      label: _i18n.locale.menu_myokr,
      key: this.MYOKR,
      href: '',
      children: [{
        icon: '',
        label: _i18n.locale.menu_responsible,
        key: PAGE_MY_OWNER_OKR,
        href: '#/okr/manage?relation=1'
      }, {
        icon: '',
        label: _i18n.locale.menu_participated,
        key: PAGE_MY_PART_OKR,
        href: '#/okr/manage?relation=2',
        guideTip: '「我参与的」频道中可查看目标名称中@我的  OKR',
        moduleType: 62
      }, {
        icon: '',
        label: _i18n.locale.menu_concerned,
        key: PAGE_MY_TARGET_OKR,
        href: '#/okr/manage?relation=3'
      }]
    }, {
      icon: 'caidan-xiashu',
      label: _i18n.locale.menu_subordinate,
      key: this.SUBORDINATES,
      isLeaf: false,
      children: [],
      href: '',
      isShow: (_window$BSGlobal$logi = window.BSGlobal.loginUserInfo) === null || _window$BSGlobal$logi === void 0 ? void 0 : _window$BSGlobal$logi.HasSub
    }, {
      icon: "caidan-tongji",
      label: _i18n.locale.menu_samevel_okr,
      key: this.TONGJI,
      type: this.TONGJI,
      isLeaf: false,
      children: [],
      href: "",
      isShow: props.isShowTongji
    }, {
      icon: 'guanzhu',
      label: _i18n.locale.menu_concerned_user,
      key: this.USERCONCERN,
      type: this.USERCONCERN,
      isLeaf: false,
      children: [],
      href: ''
    }, {
      icon: 'okr-gongsi-m',
      label: _i18n.locale.menu_alldeparment,
      key: this.COMPANYALL,
      type: this.COMPANYALL,
      href: ''
    }, {
      icon: '',
      label: _i18n.locale.menu_other,
      key: 'other',
      type: _index.GROUP_NAME_NODE,
      href: ''
    }, {
      icon: 'ditu',
      label: _i18n.locale.menu_okrmap,
      key: PAGE_OKR_MAP,
      type: _index.MENU_ITEM_NOE,
      href: '#/okr/map',
      isShow: !isUseNewMenu
    }, {
      icon: 'panel',
      label: _i18n.locale.menu_okr_working_method,
      type: _index.MENU_ITEM_NOE,
      key: PAGE_OKR_FUNCTION,
      href: '#/okr/panel',
      isShow: !isUseNewMenu
    }, {
      icon: 'yibiao',
      label: _i18n.locale.menu_okrdashboard,
      key: PAGE_OKR_DASHBOARD,
      type: _index.MENU_ITEM_NOE,
      href: '#/okr/chart?iTalentFrameType=iframe&iTalentFrame=%2F%2Fwww.tita.gift%2Fmenuroute%3FmenuId%3D44269%26roleId%3D',
      isShow: hasDashboardAuth && !isUseNewMenu
    }, {
      icon: 'cases',
      label: _i18n.locale.menu_okrcases,
      type: _index.MENU_ITEM_NOE,
      key: PAGE_OKR_CASES,
      href: '#/okr/cases',
      isShow: !(0, _helpers.isLocal)()
    }];
  } // showSubordinateMenu() {
  //   const newSubordinateMenu = { ...this.menus[1] };
  //   newSubordinateMenu.isShow = true;
  //   this.menus[1] = newSubordinateMenu;
  // }


  _createClass(okrMenus, [{
    key: "checkConcernExist",
    value: function checkConcernExist(user) {
      var concerns = this.menus[3].children;

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

        if (it.key == "concern-".concat(user.userId)) {
          return true;
        }
      }

      return false;
    }
  }, {
    key: "setConcernLoading",
    value: function setConcernLoading(loading) {
      this.menus[3].children = [];

      if (loading) {
        this.menus[3].children.push({
          icon: '',
          label: 'loading',
          key: 'loading-concern',
          type: _index.LOADING_NODE,
          href: '',
          data: {}
        });
      }
    }
  }, {
    key: "setSubordinatesLoading",
    value: function setSubordinatesLoading(loading) {
      this.menus[1].children = [];

      if (loading) {
        this.menus[1].children.push({
          icon: '',
          label: 'loading',
          key: 'loading-subordinates',
          type: _index.LOADING_NODE,
          href: '',
          data: {}
        });
      }
    }
  }, {
    key: "setTongjiLoading",
    value: function setTongjiLoading(loading) {
      this.menus[2].children = [];

      if (loading) {
        this.menus[2].children.push({
          icon: "",
          label: "loading",
          key: "loading-subordinates",
          type: _index.LOADING_NODE,
          href: "",
          data: {}
        });
      }
    }
  }, {
    key: "insertTongji",
    value: function insertTongji(users) {
      if (!users || !users.length) {
        this.menus[2].children = [{
          icon: "",
          label: "empty-subordinates",
          key: "empty-subordinates",
          type: _index.NO_SUBORDINATES_NODE,
          href: "",
          data: {}
        }];
        return;
      }

      var treeNodeUsers = [];

      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        treeNodeUsers.push({
          icon: "",
          label: user.name,
          key: "tongji-".concat(user.userId),
          type: _index.SAME_LEVEL_USER_NODE,
          href: "",
          data: user
        });
      }

      this.menus[2].children = treeNodeUsers;
    }
  }, {
    key: "insertConcern",
    value: function insertConcern(users) {
      var treeNodeUsers = []; // 如果没有关注者，显示空

      this.setEmptyConcerners(false);

      if (!users || !users.length) {
        this.setEmptyConcerners(true);
      }

      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        if (this.checkConcernExist(user)) continue;
        treeNodeUsers.push({
          icon: '',
          label: user.name,
          key: "concern-".concat(user.userId),
          type: _index.FOLLOWER_NODE,
          href: '',
          data: user
        });
      }

      this.menus[3].children = this.menus[3].children.concat(treeNodeUsers);
    }
  }, {
    key: "setEmptyConcerners",
    value: function setEmptyConcerners(empty) {
      this.menus[3].children = [];

      if (empty) {
        this.menus[3].children.push({
          icon: '',
          label: 'empty-concerners',
          key: 'empty-concerners',
          type: _index.NO_FOLLOWER_NODE,
          href: '',
          data: {}
        });
      }
    }
  }, {
    key: "addConcerner",
    value: function addConcerner(user) {
      if (this.menus[3].children.length && this.menus[3].children[0].key == 'empty-concerners') {
        this.menus[3].children = [];
      }

      this.menus[3].children.push({
        icon: '',
        label: user.name,
        key: "concern-".concat(user.userId),
        type: _index.FOLLOWER_NODE,
        href: '',
        data: user
      });
    }
  }, {
    key: "removeConcerner",
    value: function removeConcerner(user) {
      var newChildren = _toConsumableArray(this.menus[3].children).filter(function (it) {
        return it.key != "concern-".concat(user.userId);
      });

      if (!newChildren.length) {
        this.setEmptyConcerners(true);
      } else {
        this.menus[3].children = newChildren;
      }
    }
  }, {
    key: "insertSubordinates",
    value: function insertSubordinates(users) {
      if (!users || !users.length) {
        this.menus[1].children = [{
          icon: '',
          label: 'empty-subordinates',
          key: 'empty-subordinates',
          type: _index.NO_SUBORDINATES_NODE,
          href: '',
          data: {}
        }];
        return;
      }

      var treeNodeUsers = [];

      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        treeNodeUsers.push({
          icon: '',
          label: user.name,
          key: "".concat(user.userId),
          type: _index.USER_NODE,
          href: '',
          data: user
        });
      }

      this.menus[1].children = treeNodeUsers;
    }
  }, {
    key: "getMenus",
    value: function getMenus() {
      var menus = this.formateToTreeNodeData(this.menus.filter(function (it) {
        return it.isShow === undefined || it.isShow == true;
      }));
      return menus;
    }
  }, {
    key: "formateToTreeNodeData",
    value: function formateToTreeNodeData(menus) {
      var pid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
      var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
      var treeNodes = [];

      for (var i = 0; i < menus.length; i++) {
        var menu = menus[i];
        var hasChildren = !!(menu.children && menu.children.length);
        var isLeaf = menu.isLeaf !== undefined ? menu.isLeaf : !hasChildren;
        var nodeType = menu.type;
        if (!nodeType) nodeType = !isLeaf ? _index.PARENT_MENU_NODE : _index.SUB_MENU_ITEM_NOE;
        treeNodes.push({
          key: "".concat(menu.key),
          title: (0, _getLocale.getLocale)(menu.label) ? (0, _getLocale.getLocale)(menu.label) : menu.label,
          data: menu.data || menu,
          isLeaf: isLeaf,
          children: this.formateToTreeNodeData(menu.children || [], level + 1, menu.key),
          nodeType: nodeType
        });
      }

      return treeNodes;
    }
  }]);

  return okrMenus;
}();

exports["default"] = okrMenus;