"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"] = _default;

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

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

var _requestApis = require("../../request-apis");

var _avatar = _interopRequireDefault(require("../../../avatar"));

var _selector = _interopRequireDefault(require("../../../upvote/selector"));

var _attitude = _interopRequireDefault(require("../../../upvote/attitude"));

var _upvoteListModal = _interopRequireDefault(require("../../../upvote/upvoteListModal"));

var _firework = _interopRequireDefault(require("../../../firework"));

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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 = "titaui-ao-nice";

function _default(props) {
  var okrInfo = props.okrInfo,
      activeTab = props.activeTab;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      selectorVisible = _useState2[0],
      setSelectorVisible = _useState2[1];

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

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

  var btnRef = (0, _react.useRef)();
  var pending = (0, _react.useRef)(true);
  var praiseUsers = (feedData || {}).praiseUsers || [];
  var isPraise = (feedData || {}).isPraise || false;
  var getData = (0, _react.useCallback)(function () {
    return (0, _requestApis.getFeedById)(okrInfo.feedId, 0).then(function (res) {
      setFeedData(res.Data);
      pending.current = false;
    });
  }, []);
  (0, _react.useEffect)(function () {
    getData();
  }, []);

  var onMakeNiceHander = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!pending.current) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return");

            case 2:
              pending.current = true;

              if (isPraise) {
                _context.next = 9;
                break;
              }

              setFirework();
              _context.next = 7;
              return (0, _requestApis.addPraise)(okrInfo.feedId, 1);

            case 7:
              _context.next = 11;
              break;

            case 9:
              _context.next = 11;
              return (0, _requestApis.cancelPraise)(okrInfo.feedId);

            case 11:
              getData();

            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onMakeNiceHander() {
      return _ref.apply(this, arguments);
    };
  }();

  var setFirework = (0, _react.useCallback)(function () {
    var _btnRef$current$getBo = btnRef.current.getBoundingClientRect(),
        x = _btnRef$current$getBo.x,
        y = _btnRef$current$getBo.y;

    (0, _firework["default"])({
      x: x,
      y: y,
      images: (0, _attitude["default"])().map(function (a) {
        return a.img;
      })
    });
  }, []); // const doPosition = () => {
  //   // 点赞粘底
  //   let totalOffsetHeight = tabsContnetRef.current.offsetHeight;
  //   let oNiceOffsetHeight = oniceRef.current.offsetHeight;
  //   let tabContentOffsetHeight = tabRef.current.offsetHeight;
  //   // 滚动
  //   if (totalOffsetHeight < tabContentOffsetHeight) { }
  //   // 黏底，内容区域高度小于总高度
  //   else if (totalOffsetHeight > (tabContentOffsetHeight + oNiceOffsetHeight)) {
  //     oniceRef.current.classList.add('okr-onice-fixed-top');
  //   }
  // }
  // 只出现在 okrs 和 协作 页签

  if (!(!activeTab || activeTab == "okr" || activeTab == "cooperation")) {
    return /*#__PURE__*/_react["default"].createElement("span", null);
  }

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "titaui-okr-detail-ao-nice"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls)
  }, /*#__PURE__*/_react["default"].createElement(_selector["default"], {
    visible: selectorVisible,
    align: "center",
    onSelect: /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(type) {
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                if (!pending.current) {
                  _context2.next = 2;
                  break;
                }

                return _context2.abrupt("return");

              case 2:
                pending.current = true;
                setFirework();
                _context2.next = 6;
                return (0, _requestApis.addPraise)(okrInfo.feedId, type);

              case 6:
                getData();

              case 7:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }()
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__bigfingler")),
    onClick: onMakeNiceHander,
    ref: btnRef
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__bigfingler-make-good"))
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])({
      "tu-icon-like--solid": isPraise,
      "tu-icon-like": !isPraise
    })
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__bigfingler-count")),
    onClick: function onClick(e) {
      if (!praiseUsers.length) return;
      e.stopPropagation();
      setModalVisible(true);
    }
  }, feedData && feedData.counter.praiseTotal))), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__users"))
  }, praiseUsers.map(function (publishUser) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(preCls, "__user-item"))
    }, /*#__PURE__*/_react["default"].createElement(_avatar["default"], {
      userId: publishUser.userId,
      src: publishUser.avatar.medium,
      color: publishUser.avatar.color,
      name: publishUser.name
    }), /*#__PURE__*/_react["default"].createElement("img", {
      src: (0, _attitude["default"])().find(function (att) {
        return att.type == feedData.praiseUserIds[publishUser.userId];
      }).img
    }));
  })), /*#__PURE__*/_react["default"].createElement(_upvoteListModal["default"], {
    visible: modalVisible,
    onClose: function onClose() {
      return setModalVisible(false);
    },
    feedId: okrInfo.feedId
  })));
}