"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _richEditor = require("@titaui/rich-editor");

var _oClassify = _interopRequireDefault(require("./o-classify"));

var _oProgressUpdate = _interopRequireDefault(require("../o-progress-update"));

var _oAvatar = _interopRequireDefault(require("./o-avatar"));

var _okrType = _interopRequireDefault(require("../okr-type"));

var _oCycle = _interopRequireDefault(require("./o-cycle"));

var _editName = _interopRequireDefault(require("./edit-name"));

var _oScore = _interopRequireDefault(require("./o-score"));

var _progress = _interopRequireDefault(require("../../../progress"));

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

var _helper = require("../../helper");

var _tooltip = _interopRequireDefault(require("../../../tooltip"));

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

var _context = require("../../context");

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

require("./index.css");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var precls = 'okr-drawer-header';

function Header(props) {
  var _window, _window$BSGlobal, _window$BSGlobal$OkrA, _window$BSGlobal$OkrA2;

  // 外部取到的 headInfo
  var okrInfo = props.okrInfo;
  var workName = okrInfo.workName,
      workNameRichText = okrInfo.workNameRichText;
  var richText = workNameRichText ? JSON.parse(workNameRichText) : (0, _richEditor.TitaRichEditorInitTextState)(workName);

  var _useState = (0, _react.useState)({
    mentionsUsers: [],
    pureText: workName,
    pureTextExcludeAt: workName,
    richText: richText
  }),
      _useState2 = _slicedToArray(_useState, 2),
      okrNameData = _useState2[0],
      setOkrNameData = _useState2[1];

  var _useContext = (0, _react.useContext)(_context.GeneralDataContext),
      generalPrincipalUser = _useContext.generalPrincipalUser,
      generalOkrType = _useContext.generalOkrType,
      setGeneralOkrType = _useContext.setGeneralOkrType,
      generalOkrClassify = _useContext.generalOkrClassify,
      setGeneralOkrClassify = _useContext.setGeneralOkrClassify,
      showOkrEvalute = _useContext.showOkrEvalute;

  var commonContext = (0, _react.useContext)(_context.CommonContext);
  var approvalSetting = commonContext.approvalSetting;
  var showOScore = (_window = window) === null || _window === void 0 ? void 0 : (_window$BSGlobal = _window.BSGlobal) === null || _window$BSGlobal === void 0 ? void 0 : (_window$BSGlobal$OkrA = _window$BSGlobal.OkrAdvancedSetting) === null || _window$BSGlobal$OkrA === void 0 ? void 0 : (_window$BSGlobal$OkrA2 = _window$BSGlobal$OkrA.KRSetting) === null || _window$BSGlobal$OkrA2 === void 0 ? void 0 : _window$BSGlobal$OkrA2.OScore;
  var _okrInfo$progressGrow = okrInfo.progressGrow,
      progressGrow = _okrInfo$progressGrow === void 0 ? 0 : _okrInfo$progressGrow,
      progress = okrInfo.progress;

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      auth = _useState4[0],
      setAuth = _useState4[1];

  (0, _react.useEffect)(function () {
    setAuth(new _auth.OAuth({
      principalUser: generalPrincipalUser,
      createUserId: okrInfo.createUserId
    }).hasAuth());
  }, [generalPrincipalUser]);
  var editable = auth && okrInfo.status == 1; // 有权限 && 进行中

  var progressEdit = (auth || okrInfo.isParticipant) && okrInfo.status == 1;

  var renderContent = function renderContent(_ref) {
    var onOpenModal = _ref.onOpenModal;

    var progressCmp = /*#__PURE__*/_react["default"].createElement(_progress["default"], {
      percent: progress,
      backgroundColor: "#F7F8FA",
      textColor: okrInfo.status === 2 ? '#89919f' : _utils.OkrRiskMapping[okrInfo.manualRiskLevel].textColor,
      color: okrInfo.status === 2 ? 'linear-gradient(270deg, #BFC7D5, #E1E5EC)' : _utils.OkrRiskMapping[okrInfo.manualRiskLevel].line
    });

    if (!progressEdit) {
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: "okr-drawer-header__action-progress"
      }, progressCmp, progressGrow !== 0 && /*#__PURE__*/_react["default"].createElement("span", {
        className: (0, _classnames["default"])('okr-drawer-header__action-progress-diff', {
          'okr-drawer-header__action-progress-diff--up': progressGrow > 0,
          'okr-drawer-header__action-progress-diff--down': progressGrow < 0
        })
      }, progressGrow > 0 ? /*#__PURE__*/_react["default"].createElement("span", {
        className: "tu-icon-simple-sort-up"
      }) : /*#__PURE__*/_react["default"].createElement("span", {
        className: "tu-icon-simple-sort-down"
      }), progressGrow.toString().replace('-', ''), "%"));
    }

    return /*#__PURE__*/_react["default"].createElement("span", {
      className: "okr-drawer-header__action-progress"
    }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Upd'),
      placement: "top"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      onClick: onOpenModal,
      style: {
        cursor: 'pointer'
      }
    }, progressCmp)), progressGrow !== 0 && /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('okr-drawer-header__action-progress-diff', {
        'okr-drawer-header__action-progress-diff--up': progressGrow > 0,
        'okr-drawer-header__action-progress-diff--down': progressGrow < 0
      })
    }, progressGrow > 0 ? /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-simple-sort-up"
    }) : /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-simple-sort-down"
    }), progressGrow.toString().replace('-', ''), "%"));
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: precls,
    draggable: false
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(precls, "__avatar")
  }, /*#__PURE__*/_react["default"].createElement(_oAvatar["default"], {
    editable: editable && (okrInfo === null || okrInfo === void 0 ? void 0 : okrInfo.applyState) !== 1,
    okrId: okrInfo.workId
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(precls, "__content")
  }, /*#__PURE__*/_react["default"].createElement(_editName["default"], {
    editable: editable && (0, _helper.getApprovalOkr)(approvalSetting, 'changeOkrName', okrInfo === null || okrInfo === void 0 ? void 0 : okrInfo.applyState, okrInfo === null || okrInfo === void 0 ? void 0 : okrInfo.OkrAndKrApprovalTypes),
    okrName: okrNameData,
    okrId: okrInfo.workId,
    setOkrName: setOkrNameData
  }), /*#__PURE__*/_react["default"].createElement("section", {
    className: "".concat(precls, "__action")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      position: 'relative',
      left: -4
    }
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(precls, "__action-item")
  }, /*#__PURE__*/_react["default"].createElement(_okrType["default"], {
    editable: editable,
    workId: okrInfo.workId,
    type: generalOkrType,
    onTypeChange: setGeneralOkrType
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(precls, "__action-item")
  }, /*#__PURE__*/_react["default"].createElement(_oClassify["default"], {
    editable: editable,
    okrId: okrInfo.workId,
    okrClassify: generalOkrClassify,
    onOkrClassifyChange: setGeneralOkrClassify
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(precls, "__action-item")
  }, /*#__PURE__*/_react["default"].createElement(_oCycle["default"], {
    editable: editable && (0, _helper.getApprovalOkr)(approvalSetting, 'changeOkrTime', okrInfo === null || okrInfo === void 0 ? void 0 : okrInfo.applyState, okrInfo === null || okrInfo === void 0 ? void 0 : okrInfo.OkrAndKrApprovalTypes),
    okrId: okrInfo.workId
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(precls, "__action-item")
  }, (showOkrEvalute || showOScore || okrInfo.manualEvaluateValue) && /*#__PURE__*/_react["default"].createElement(_oScore["default"], {
    editable: editable && showOScore,
    okrId: okrInfo.workId,
    score: showOkrEvalute || okrInfo.manualEvaluateValue ? okrInfo.evaluateValue : 0,
    isEvaluate: okrInfo.manualEvaluateValue || showOkrEvalute && !!okrInfo.evaluateValue
  }))), /*#__PURE__*/_react["default"].createElement(_oProgressUpdate["default"], {
    okrInfo: okrInfo,
    renderContent: renderContent
  }))));
}

var _default = Header;
exports["default"] = _default;