"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getOkrHeadActions = getOkrHeadActions;

var _bsGlobal = require("../../../utils/bs-global");

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

var _enums = require("./enums");

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

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

var addShareButton = function addShareButton(_ref, actions) {
  var openShareModal = _ref.openShareModal;
  actions.push({
    icon: 'tu-icon-fenxiang',
    title: (0, _getLocale.getLocale)('OKR_MyO_Share'),
    onClick: openShareModal
  });
};

var addVisibilityButton = function addVisibilityButton(_ref2, actions) {
  var auth = _ref2.auth,
      visibility = _ref2.visibility,
      detailInfo = _ref2.detailInfo,
      changeVisiblilty = _ref2.changeVisiblilty;
  var children = [{
    title: (0, _getLocale.getLocale)('OKR_MyO_From_Associatedmem'),
    onClick: function onClick() {
      return changeVisiblilty(2);
    }
  }, {
    title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Directrep'),
    onClick: function onClick() {
      return changeVisiblilty(3);
    }
  }, {
    title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Specified'),
    onClick: function onClick() {
      return changeVisiblilty(4);
    }
  }];

  if (window.BSGlobal.tenantInfo.isOpenInfo) {
    children.unshift({
      title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Public'),
      onClick: function onClick() {
        return changeVisiblilty(1);
      }
    });
  }

  var getVisibilityMapText = {
    1: (0, _getLocale.getLocale)('OKR_MyO_Pop_Public'),
    2: (0, _getLocale.getLocale)('OKR_MyO_From_Associatedmem'),
    3: (0, _getLocale.getLocale)('OKR_MyO_Pop_Directrep'),
    4: (0, _getLocale.getLocale)('OKR_MyO_Pop_Specified')
  };

  var editable = auth.hasAuth() && detailInfo.status === _enums.OkrStatus.inProgress;

  actions.push({
    icon: visibility !== 1 ? 'tu-icon-simi-icon' : 'tu-icon-gongkai',
    title: getVisibilityMapText[visibility] + (0, _getLocale.getLocale)('OKR_MyO_Text_Visible'),
    children: editable ? children : null
  });
};

var addFollowButton = function addFollowButton(_ref3, actions) {
  var _getBSGlobal$KRSettin, _detailInfo$aiteUsers;

  var roleType = _ref3.roleType,
      onChangeOkrFollow = _ref3.onChangeOkrFollow,
      detailInfo = _ref3.detailInfo;

  // 未开启关注目标功能
  if (!((_getBSGlobal$KRSettin = (0, _bsGlobal.getBSGlobal)('OkrAdvancedSetting').KRSetting) !== null && _getBSGlobal$KRSettin !== void 0 && _getBSGlobal$KRSettin.TargetFollowers)) {
    return;
  } // 是负责人或是内部成员不能关注目标


  if (roleType === _enums.OkrRole.principal || roleType === _enums.OkrRole.member || !!(detailInfo !== null && detailInfo !== void 0 && (_detailInfo$aiteUsers = detailInfo.aiteUsers) !== null && _detailInfo$aiteUsers !== void 0 && _detailInfo$aiteUsers.find(function (item) {
    return (0, _bsGlobal.getUserInfo)().Id == item;
  }))) {
    return;
  } // 已关注目标


  if (roleType === _enums.OkrRole.follower) {
    actions.push({
      icon: 'okr-drawer-header-followed',
      title: (0, _getLocale.getLocale)('OKR_MyO_Butt_Unfollow'),
      onClick: function onClick() {
        return onChangeOkrFollow(false);
      }
    });
    return;
  } // 未关注


  actions.push({
    icon: 'tu-icon-APP-shoucang',
    title: '关注目标',
    onClick: function onClick() {
      return onChangeOkrFollow(true);
    }
  });
};

var addExitButton = function addExitButton(_ref4, actions) {
  var roleType = _ref4.roleType,
      onQuit = _ref4.onQuit;

  if (roleType === _enums.OkrRole.member) {
    actions.push({
      icon: 'tu-icon-tuichu',
      title: (0, _getLocale.getLocale)('Pro_Admin_pop_Withdraw'),
      onClick: onQuit
    });
  }
};

var addFinishOrReopenButton = function addFinishOrReopenButton(_ref5, actions) {
  var detailInfo = _ref5.detailInfo,
      auth = _ref5.auth,
      onFinishOrReOpen = _ref5.onFinishOrReOpen;
  if (!auth.hasAuth()) return;

  if (detailInfo.status === _enums.OkrStatus.inProgress) {
    actions.push({
      icon: 'tu-icon-liebiao-jieshu',
      title: (0, _getLocale.getLocale)('OKR_MyO_EndObjective'),
      onClick: function onClick() {
        return onFinishOrReOpen(true);
      }
    });
  } else {
    actions.push({
      icon: 'tu-icon-gengxin1',
      title: '重启目标',
      onClick: function onClick() {
        return onFinishOrReOpen(false);
      }
    });
  }
};

var addCopyButton = function addCopyButton(_ref6, actions) {
  var onCopyOkr = _ref6.onCopyOkr;
  actions.push({
    icon: 'tu-icon-liebiao-fuzhi',
    title: (0, _getLocale.getLocale)('OKR_MyO_Butt_CopyObjective'),
    onClick: function onClick() {
      return onCopyOkr();
    }
  });
};

var addDeleteButton = function addDeleteButton(_ref7, actions) {
  var auth = _ref7.auth,
      onDeleteOkr = _ref7.onDeleteOkr;
  if (!auth.hasAuth()) return;
  actions.push({
    icon: 'tu-icon-del',
    title: (0, _getLocale.getLocale)('OKR_MyO_Butt_DeleteO'),
    onClick: onDeleteOkr
  });
}; // const addDetailButton: ActionDealFunction = ({ detailInfo }, actions) => {
//   const registerDate = window.BSGlobal.tenantAuthentication?.StartDate;
//   dayjs(registerDate).year() < 2021
//     && actions.push({
//       icon: "tu-icon-xiangqing1",
//       title: "目标详情",
//       onClick: () => {
//         window.open(
//           `${location.pathname}#okr/manage?work_id=${detailInfo.workId}`,
//         );
//       },
//     });
// };


function getOkrHeadActions(data) {
  var actions = [];
  var auth = new _auth.OAuth({
    principalUser: data.principalUser,
    createUserId: data.detailInfo.createUserId
  });

  var dataWithAuth = _objectSpread(_objectSpread({}, data), {}, {
    auth: auth
  });

  addShareButton(dataWithAuth, actions);
  addVisibilityButton(dataWithAuth, actions);
  addFollowButton(dataWithAuth, actions);
  addExitButton(dataWithAuth, actions);
  addFinishOrReopenButton(dataWithAuth, actions);
  addCopyButton(dataWithAuth, actions);
  addDeleteButton(dataWithAuth, actions); // addDetailButton(dataWithAuth,actions)

  return actions;
}