"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 _popup = _interopRequireDefault(require("../../../popup"));

var _conditionRender = _interopRequireDefault(require("../../../condition-render"));

var _msg = _interopRequireDefault(require("./msg"));

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

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

var _redPack = _interopRequireDefault(require("./components/red-pack"));

var _request = require("../../request.apis");

var _bell = _interopRequireDefault(require("./images/bell-1.svg"));

var _useGlobalValue = _interopRequireDefault(require("../../useGlobalValue"));

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

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 prefixCls = 'titaui-user-message';

function UserMessage() {
  var _useState = (0, _react.useState)(0),
      _useState2 = _slicedToArray(_useState, 2),
      infosNum = _useState2[0],
      setInfosNum = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      allData = _useState4[0],
      setAllData = _useState4[1];

  var _useState5 = (0, _react.useState)(0),
      _useState6 = _slicedToArray(_useState5, 2),
      redPackNum = _useState6[0],
      setRedPackNum = _useState6[1];

  var _useState7 = (0, _react.useState)(false),
      _useState8 = _slicedToArray(_useState7, 2),
      popupVisible = _useState8[0],
      setPopupVisible = _useState8[1];

  var _useState9 = (0, _react.useState)(0),
      _useState10 = _slicedToArray(_useState9, 2),
      preData = _useState10[0],
      setPreData = _useState10[1];

  var _useState11 = (0, _react.useState)('todos'),
      _useState12 = _slicedToArray(_useState11, 2),
      firstTab = _useState12[0],
      setFirstTab = _useState12[1];

  var setGlobalState = (0, _useGlobalValue["default"])()[1];

  var handleShowInfos = function handleShowInfos() {
    setPopupVisible(true);
  };

  var handleIntervalRequestMessageInfo = function handleIntervalRequestMessageInfo() {
    (0, _request.getMessages)().then(function (res) {
      if (res.Code === 1) {
        setAllData(res.Data.indicators);
        var indicators = res.Data.indicators;
        var todos = indicators.find(function (item) {
          return item.type === 'todoIndicator';
        });
        var informs = indicators.find(function (item) {
          return item.type === 'messageIndicator';
        });
        var replys = indicators.find(function (item) {
          return item.type === 'mLeaveandComment';
        });
        var reminds = indicators.find(function (item) {
          return item.type === 'Tx';
        });
        var total = 0; // @与回复的数量在通知里面包含了

        if (reminds && reminds.value) {
          total += reminds.value;
        }

        if (informs) {
          total += informs.value;
        }

        if (todos) {
          total += todos.value;
        }

        setPreData(infosNum);
        setInfosNum(total);

        if (todos && todos.value) {
          setFirstTab('todos');
        } else if (informs && !replys && informs.value) {
          setFirstTab('informs');
        } else if (replys && informs.value - replys.value === 0) {
          setFirstTab('replys');
        } else if (replys && informs.value - replys.value > 0) {
          setFirstTab('informs');
        } else if (reminds && reminds.value) {
          setFirstTab('reminds');
        } else {
          setFirstTab('todos');
        } // 判断是企业勋章


        var enterpriseMedalMsg = indicators.filter(function (item) {
          return item.messageType === 1 && item.indicatorType === 1969002;
        });

        if (enterpriseMedalMsg.length > 0) {
          window.util.upaasEnterpriseMedal();
        } // upaas 勋章


        var medalDetail = (0, _bsGlobal.getBSGlobal)('medalDetail');

        if (medalDetail) {
          window.util.upaasMedal()(medalDetail);
        } // 红包


        var mRedPackIndicator = indicators.filter(function (item) {
          return item.type === 'mRedPackIndicator';
        });

        if (mRedPackIndicator.length > 0) {
          setRedPackNum(mRedPackIndicator[0].value);
        } // 祝福，拆礼物


        var blessIndicator = indicators.filter(function (item) {
          return item.type === 'mZhufu';
        });
        var weeklyReportIndicator = indicators.filter(function (item) {
          return item.type === 'mWeekStatisticIndicator';
        });

        if (blessIndicator.length === 1) {
          var userInfo = (0, _bsGlobal.getUserInfo)();
          var options = {
            needMask: true,
            toUserId: userInfo.Id,
            blessingTime: blessIndicator[0].latestUpdateDate
          }; // @ts-ignore

          window.util.openGift(options);
        }

        if (weeklyReportIndicator.length) {
          var _window;

          (_window = window) === null || _window === void 0 ? void 0 : _window.Talent.app.vent.trigger('open-weekly-report-modal');
        }

        var feedInfo = indicators.find(function (item) {
          return item.type === 'feedAboutMeIndicator';
        });
        var feedInsightIndicator = indicators.find(function (item) {
          return item.type === 'feedInsightIndicator';
        });
        setGlobalState({
          feedNum: (feedInfo === null || feedInfo === void 0 ? void 0 : feedInfo.value) || 0,
          insightNum: (feedInsightIndicator === null || feedInsightIndicator === void 0 ? void 0 : feedInsightIndicator.value) || 0
        });
        window.globalState = {
          feedNum: (feedInfo === null || feedInfo === void 0 ? void 0 : feedInfo.value) || 0,
          insightNum: (feedInsightIndicator === null || feedInsightIndicator === void 0 ? void 0 : feedInsightIndicator.value) || 0
        };
      }
    });
  };

  (0, _react.useEffect)(function () {
    setPreData(infosNum);
    handleIntervalRequestMessageInfo();
    var interval = setInterval(handleIntervalRequestMessageInfo, 15000);
    return function () {
      return clearInterval(interval);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_popup["default"], {
    popup: /*#__PURE__*/_react["default"].createElement(_msg["default"], {
      onItemClick: setPopupVisible,
      allData: allData,
      handleIntervalRequestMessageInfo: handleIntervalRequestMessageInfo,
      firstTab: firstTab
    }),
    popupVisible: popupVisible,
    popupTransitionName: (0, _classnames["default"])("".concat(prefixCls, "__popup-right-top")),
    popupAlign: _constant.popupAlign,
    mask: false,
    destroyPopupOnHide: true
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefixCls, "__msg")),
    onClick: handleShowInfos
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: infosNum !== 0
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefixCls, "__msg-total"))
  }, infosNum > 100 ? '99+' : infosNum)), /*#__PURE__*/_react["default"].createElement("img", {
    src: _bell["default"],
    className: (0, _classnames["default"])(_defineProperty({}, "".concat(prefixCls, "__msg-dot"), infosNum !== preData)),
    alt: ""
  }))), /*#__PURE__*/_react["default"].createElement(_redPack["default"], {
    redPackNum: redPackNum
  }));
}

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