"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 _conditionRender = _interopRequireDefault(require("../../../condition-render"));

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

var _communication = _interopRequireDefault(require("../../../communication"));

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

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

var _upvote = _interopRequireDefault(require("../../../upvote"));

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

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

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

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

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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 prefix = "titaui-dynamic-item";

var DynamicFooter = function DynamicFooter(_ref) {
  var _getBSGlobal;

  var data = _ref.data,
      dailyReportType = _ref.dailyReportType;
  var feedId = data.feedId,
      feedComments = data.feedComments,
      praiseType = data.praiseType,
      praiseUsers = data.praiseUsers,
      feedType = data.feedType,
      publishUser = data.publishUser;

  var _useState = (0, _react.useState)(true),
      _useState2 = _slicedToArray(_useState, 2),
      usePropsComment = _useState2[0],
      setUsePropsComment = _useState2[1];

  var _useState3 = (0, _react.useState)(feedComments.total > 0),
      _useState4 = _slicedToArray(_useState3, 2),
      showComment = _useState4[0],
      setShowComment = _useState4[1];

  var _useState5 = (0, _react.useState)(praiseUsers || []),
      _useState6 = _slicedToArray(_useState5, 2),
      praiseData = _useState6[0],
      setPraiseData = _useState6[1];

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

  var communicationRef = (0, _react.useRef)();
  var productName = _constant.PlanReportTrackActionNameMap[dailyReportType];
  var hasRewardBtn = !!((_getBSGlobal = (0, _bsGlobal.getBSGlobal)("WebHead")) !== null && _getBSGlobal !== void 0 && _getBSGlobal.AppHead.find(function (item) {
    return item.AppId === "950";
  }));

  var _useContext = (0, _react.useContext)(_context.SearchKeyContext),
      searchKeyWord = _useContext.searchKeyWord;

  var handleCommentClick = function handleCommentClick() {
    setShowComment(!showComment);
    setUsePropsComment(false);
  };

  var onChangeParse = function onChangeParse(type) {
    var _getBSGlobal2 = (0, _bsGlobal.getBSGlobal)("loginUserInfo"),
        Id = _getBSGlobal2.Id;

    var data = _toConsumableArray(praiseData).filter(function (item) {
      return Number("".concat(item.userId)) !== Number("".concat(Id));
    });

    var actionName = "";

    if (type) {
      var _getBSGlobal3, _getBSGlobal4;

      var avatar = (_getBSGlobal3 = (0, _bsGlobal.getBSGlobal)("loginUserInfo")) === null || _getBSGlobal3 === void 0 ? void 0 : _getBSGlobal3.UserAvatar;
      var name = (_getBSGlobal4 = (0, _bsGlobal.getBSGlobal)("loginUserInfo")) === null || _getBSGlobal4 === void 0 ? void 0 : _getBSGlobal4.Name;
      avatar.color = avatar.Color;
      avatar.medium = avatar.Medium;
      var parseUser = {
        avatar: avatar,
        name: name,
        userId: Id,
        praiseType: type
      };
      setPraiseData([parseUser].concat(_toConsumableArray(data)));
      actionName = "点赞";
    } else {
      setPraiseData(data);
      actionName = "取消点赞";
    }

    if (window.titaTracker) {
      window.titaTracker("action").record({
        actionName: actionName,
        actionGroup: "互动",
        productName: productName
      });
    }
  };

  var handlePayReward = function handlePayReward() {
    var data = {
      multi: false,
      toUserInfo: _objectSpread(_objectSpread({}, publishUser), {}, {
        id: publishUser.userId
      }),
      objId: feedId,
      objType: feedType
    };

    if (window.util) {
      if (window.titaTracker) {
        window.titaTracker("action").record({
          actionName: "打开打赏弹层",
          actionGroup: "互动",
          productName: productName
        });
      }

      window.util.rewardGetFile(function (myReward) {
        myReward.renderPayReward(data);
      });
    }
  };

  var renderUpvoteCount = function renderUpvoteCount(type, count) {
    if (count === 0) {
      return "";
    }

    return count || type;
  };

  var handleReply = function handleReply(_ref2) {
    var submitType = _ref2.submitType;
    var actionName = submitType === "delete" ? "删除评论" : "评论";

    if (window.titaTracker) {
      window.titaTracker("action").record({
        actionName: actionName,
        actionGroup: "互动",
        productName: productName
      });
    }
  };

  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "__footer"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__footer-left")
  }, praiseData.map(function (item, index) {
    if (index >= 8) return /*#__PURE__*/_react["default"].createElement("span", null);
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__footer-left-item"),
      key: item.userId
    }, /*#__PURE__*/_react["default"].createElement(_avatar["default"].Icon, {
      name: item.name,
      userId: item.userId,
      avatar: item.avatar,
      type: item.praiseType,
      size: {
        width: 28,
        textNum: 1,
        fontSize: 12
      }
    }));
  }), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: praiseData && praiseData.length > 8
  }, /*#__PURE__*/_react["default"].createElement(_avatar["default"].Num, {
    onClick: function onClick() {
      return setUpvoteModalVisible(true);
    },
    num: praiseData.length - 8
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__footer-right")
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: hasRewardBtn
  }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
    overlay: (0, _getLocale.getLocale)("Feed_Approval"),
    align: {
      offset: [0, 0]
    },
    placement: "top"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    onClick: handlePayReward,
    className: (0, _classnames["default"])("".concat(prefix, "__footer-icon"), "".concat(prefix, "__footer-icon-redenvelopes"))
  }))), /*#__PURE__*/_react["default"].createElement("span", {
    onClick: handleCommentClick,
    className: (0, _classnames["default"])("tu-icon-pinglun-m", "".concat(prefix, "__footer-icon"))
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: feedComments.total > 0
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "__footer-number")
  }, feedComments.total))), /*#__PURE__*/_react["default"].createElement(_upvote["default"], {
    type: praiseType,
    count: praiseUsers ? praiseUsers.length : 0,
    feedId: feedId,
    onChange: onChangeParse,
    style: {
      marginLeft: 30
    },
    buttonType: "tu-icon-tyle-column",
    customUpvoteBtnCls: "".concat(prefix, "__upvote-btn"),
    renderCount: renderUpvoteCount
  }), /*#__PURE__*/_react["default"].createElement(_upvoteListModal["default"], {
    feedId: feedId,
    visible: upvoteModalVisible,
    onClose: function onClose() {
      return setUpvoteModalVisible(false);
    }
  }))), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: showComment
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "__communication"))
  }, /*#__PURE__*/_react["default"].createElement(_communication["default"], {
    feedId: feedId,
    commentCount: 3,
    ref: communicationRef,
    highLightKeyWord: searchKeyWord,
    autoFocus: feedComments.total === 0,
    usePropsComment: usePropsComment,
    communication: feedComments.comments,
    totalCommunicationNum: feedComments.total,
    onReplyChange: handleReply,
    showUser: true,
    showDefaultInput: true
  }))));
};

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