"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 _styledComponents = require("./styledComponents");

var _openData = require("../../utils/open-data");

var _constant = require("./constant");

var _constant2 = require("../../utils/constant");

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

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

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

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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 colorMap = {
  1: '132,117,255',
  2: '79,115,255',
  3: '46,156,255',
  4: '12,192,201'
};

var formatKeywordsFunc = function formatKeywordsFunc(str, key) {
  var reg = new RegExp("(".concat(key, ")"), 'g');
  var newstr = str.replace(reg, "<span style=\"color: #2879ff;\">".concat(key, "</span>"));
  return newstr;
};

var unFormatKeywordsFunc = function unFormatKeywordsFunc(str, key) {
  var regExpKey = "<span style=\"color: #2879ff;\">".concat(key, "</span>");
  var reg = new RegExp("(".concat(regExpKey, ")"), 'g');
  var newstr = str.replace(reg, key);
  return newstr;
};

var htmlToText = function htmlToText(text) {
  var div = document.createElement('div');
  div.innerHTML = text;
  var newtext = div.innerText || div.textContent;
  return newtext;
};

var dealOkrData = function dealOkrData(data, keyWords) {
  var newData = data;

  if (keyWords) {
    newData.workName = formatKeywordsFunc(data.workName, keyWords);
    newData.Krs = newData.Krs.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        mileStoneName: formatKeywordsFunc(item.mileStoneName, keyWords)
      });
    });
  }

  return data;
};

var Item = /*#__PURE__*/function (_PureComponent) {
  _inherits(Item, _PureComponent);

  var _super = _createSuper(Item);

  // @ts-ignore
  // const {data,keywords} = props
  function Item(props) {
    var _window$BSGlobal, _window$BSGlobal$OkrA, _window$BSGlobal$OkrA2;

    var _this;

    _classCallCheck(this, Item);

    _this = _super.call(this, props);
    _this.isObjectiveRelevanceKr = (_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.ObjectiveRelevanceKr;

    _this.onSelect = function (e, pdata) {
      e.stopPropagation();
      e.nativeEvent.stopImmediatePropagation();
      var _this$props = _this.props,
          keywords = _this$props.keywords,
          onSelect = _this$props.onSelect,
          data = _this$props.data;

      var unFormatData = _objectSpread({}, pdata);

      if (keywords) {
        if (unFormatData.workName) {
          unFormatData.workName = unFormatKeywordsFunc(unFormatData.workName, keywords);
        } else {
          unFormatData.mileStoneName = unFormatKeywordsFunc(unFormatData.mileStoneName, keywords);
        }
      }

      onSelect({
        selectData: unFormatData,
        okrInfo: data
      });
    };

    _this.isExtend = function () {
      var _this$props2 = _this.props,
          data = _this$props2.data,
          active = _this$props2.active;
      var isExpand = _this.state.isExpand;

      if (data.Krs.length > 0) {
        var isRelatedKr = data.Krs.some(function (item) {
          return item.mileStoneId === (active.workId || active.mileStoneId);
        });

        _this.setState({
          isExpand: isRelatedKr ? true : isExpand || data.isOpen
        });
      }
    };

    _this.getCycleText = function () {
      var data = _this.props.data;
      var hzNum = [(0, _getLocale.getLocale)('OKR_MyO_Pop_1stQuarter'), (0, _getLocale.getLocale)('OKR_MyO_Pop_2ndQuarter'), (0, _getLocale.getLocale)('OKR_MyO_Pop_3rdQuarter'), (0, _getLocale.getLocale)('OKR_MyO_Pop_4thQuarter')];

      switch (data.cycleType) {
        case 1:
          return "".concat(data.annualNum).concat(_getLocale.isEn ? '' : '年');

        case 2:
          return hzNum[data.yqmnum - 1];

        case 3:
          return (0, _constant2.getMonth)()[data.yqmnum - 1];

        case 4:
          return "".concat((0, _constant2.getMonth)()[data.yqmnum - 1], "-").concat((0, _constant2.getMonth)()[((data.yqmnum + 1) % 12 || 12) - 1]);

        case 5:
          return data.yqmnum === 1 ? (0, _getLocale.getLocale)('OKR_MyO_Pop_Firsthalf') : (0, _getLocale.getLocale)('OKR_MyO_Pop_Secondhalf');

        default:
          return (0, _getLocale.getLocale)('Mod_Customization');
      }
    };

    _this.renderItemKr = function () {
      var _this$props3 = _this.props,
          data = _this$props3.data,
          active = _this$props3.active,
          isShowKr = _this$props3.isShowKr,
          keywords = _this$props3.keywords;
      var okrData = dealOkrData(data, keywords);

      if (okrData.Krs.length > 0) {
        return okrData.Krs.map(function (item, index) {
          if (_this.isObjectiveRelevanceKr) {
            return /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemTop, {
              key: item.mileStoneId
            }, /*#__PURE__*/_react["default"].createElement(_styledComponents.Radio, {
              onClick: function onClick(e) {
                return _this.onSelect(e, data.Krs[index]);
              },
              active: active && item.mileStoneId === (active.workId || active.mileStoneId)
            }), /*#__PURE__*/_react["default"].createElement(_styledComponents.WorkNameWrapper, {
              isShowKr: isShowKr
            }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
              placement: "top",
              overlay: (0, _openData.parseString)(htmlToText(item.mileStoneName))
            }, /*#__PURE__*/_react["default"].createElement(_styledComponents.OkrName, {
              className: "okr-relate-modal__milestonename",
              isKr: true,
              isShowKr: isShowKr // dangerouslySetInnerHTML={{
              //   __html: `KR${index + 1}：${parseString(item.mileStoneName)}`,
              // }}

            }, "KR".concat(index + 1, "\uFF1A"), (0, _openData.parseString)(item.mileStoneName)))));
          }

          if (active && item.mileStoneId === (active.workId || active.mileStoneId)) {
            return /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemTop, {
              key: item.mileStoneId
            }, /*#__PURE__*/_react["default"].createElement(_styledComponents.Radio, {
              onClick: function onClick(e) {
                return _this.onSelect(e, data.Krs[index]);
              },
              active: active && item.mileStoneId === (active.workId || active.mileStoneId)
            }), /*#__PURE__*/_react["default"].createElement(_styledComponents.WorkNameWrapper, {
              isShowKr: isShowKr
            }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
              placement: "top",
              overlay: (0, _openData.parseString)(htmlToText(item.mileStoneName))
            }, /*#__PURE__*/_react["default"].createElement(_styledComponents.OkrName, {
              className: "okr-relate-modal__milestonename",
              isKr: true,
              isShowKr: isShowKr // dangerouslySetInnerHTML={{
              //   __html: KR${index + 1}：${parseString(item.mileStoneName)}`,
              // }}

            }, "KR".concat(index + 1, "\uFF1A"), (0, _openData.parseString)(item.mileStoneName)))));
          }

          return '';
        });
      }

      return /*#__PURE__*/_react["default"].createElement("span", null);
    };

    _this.state = {
      isExpand: false
    };
    return _this;
  }

  _createClass(Item, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.isExtend();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps() {
      this.isExtend();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props4 = this.props,
          data = _this$props4.data,
          active = _this$props4.active,
          isShowKr = _this$props4.isShowKr,
          keywords = _this$props4.keywords;
      var isExpand = this.state.isExpand;
      var okrData = dealOkrData(data, keywords);
      return /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemWrapper, null, /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemObjectiveContainer, {
        onClick: function onClick() {
          return _this2.setState({
            isExpand: !isExpand
          });
        }
      }, /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemTop, null, /*#__PURE__*/_react["default"].createElement(_styledComponents.Radio, {
        onClick: function onClick(e) {
          return _this2.onSelect(e, data);
        },
        active: active && data.workId === active.workId
      }), /*#__PURE__*/_react["default"].createElement(_styledComponents.Tag, {
        color: colorMap[data.cycleType] || colorMap[4]
      }, this.getCycleText()), /*#__PURE__*/_react["default"].createElement(_styledComponents.WorkNameWrapper, {
        isShowKr: isShowKr
      }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
        placement: "top",
        overlay: (0, _openData.parseString)(htmlToText(okrData.workName))
      }, /*#__PURE__*/_react["default"].createElement(_styledComponents.OkrName, {
        className: "okr-relate-modal__workname",
        isKr: false,
        isShowKr: isShowKr // dangerouslySetInnerHTML={{ __html: parseString(okrData.workName) }}

      }, (0, _openData.parseString)(okrData.workName)))), this.isObjectiveRelevanceKr && isShowKr && data.Krs.length > 0 && /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(isExpand ? 'tu-icon-arrow-up' : 'tu-icon-arrow-down')
      })), /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemBottom, null, !isShowKr && [/*#__PURE__*/_react["default"].createElement("span", null, (0, _constant.getTypeMap)()[data.okrType]), /*#__PURE__*/_react["default"].createElement(_styledComponents.Separator, null)], /*#__PURE__*/_react["default"].createElement(_styledComponents.PricipleName, null, /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
        name: data.principalUser.name,
        id: data.principalUser.userId
      })), /*#__PURE__*/_react["default"].createElement(_styledComponents.Separator, null), /*#__PURE__*/_react["default"].createElement("span", null, data.startDate, "-", data.endDate), isShowKr && [/*#__PURE__*/_react["default"].createElement(_styledComponents.Separator, null), /*#__PURE__*/_react["default"].createElement("span", null, data.statusName)])), isShowKr && (this.isObjectiveRelevanceKr || !this.isObjectiveRelevanceKr && okrData.hasSub) && /*#__PURE__*/_react["default"].createElement(_styledComponents.ItemKrContainer, {
        className: "".concat(isExpand ? 'slideIn' : 'slideOut')
      }, this.renderItemKr()));
    }
  }]);

  return Item;
}(_react.PureComponent);

exports["default"] = Item;