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

var _dropSelector = _interopRequireWildcard(require("../../../../drop-selector"));

var _requestApi = require("../../../request-api");

var _dialogConfirm = _interopRequireWildcard(require("../../../../dialog-confirm"));

var _toast = _interopRequireDefault(require("../../../../toast"));

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

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

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

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

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

var DynamicItemHeader = function DynamicItemHeader(_ref) {
  var _ref$onDeleteFeed = _ref.onDeleteFeed,
      onDeleteFeed = _ref$onDeleteFeed === void 0 ? function () {} : _ref$onDeleteFeed,
      _ref$showCollect = _ref.showCollect,
      showCollect = _ref$showCollect === void 0 ? true : _ref$showCollect,
      _ref$showDelete = _ref.showDelete,
      showDelete = _ref$showDelete === void 0 ? false : _ref$showDelete,
      _ref$showTopping = _ref.showTopping,
      showTopping = _ref$showTopping === void 0 ? false : _ref$showTopping,
      data = _ref.data,
      _ref$desc = _ref.desc,
      desc = _ref$desc === void 0 ? "" : _ref$desc,
      user = _ref.user,
      _ref$suffixContent = _ref.suffixContent,
      suffixContent = _ref$suffixContent === void 0 ? "" : _ref$suffixContent;
  var createDate = data.createDate,
      feedId = data.feedId,
      _data$isTop = data.isTop,
      isTop = _data$isTop === void 0 ? false : _data$isTop,
      _data$isCollect = data.isCollect,
      isCollect = _data$isCollect === void 0 ? false : _data$isCollect,
      feedType = data.feedType;
  var isOldSummary = feedType === _constant.EFeedType.Report && !data.newSummary;

  var _useState = (0, _react.useState)(isTop),
      _useState2 = _slicedToArray(_useState, 2),
      nowIsTop = _useState2[0],
      setNowIsTop = _useState2[1];

  var _useState3 = (0, _react.useState)(isCollect),
      _useState4 = _slicedToArray(_useState3, 2),
      nowIsCollect = _useState4[0],
      setNowIsCollect = _useState4[1];

  var _useState5 = (0, _react.useState)(false),
      _useState6 = _slicedToArray(_useState5, 2),
      showDialogConfirm = _useState6[0],
      setShowDialogConfirm = _useState6[1];

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

  var url = "";

  var onChangeHandler = function onChangeHandler(name) {};

  var handleTopOperateClick = function handleTopOperateClick() {
    if (nowIsTop) {
      (0, _requestApi.cancelTopDynamic)({
        feedId: feedId
      }).then(function (resp) {
        if (resp.Code === 1) {
          setNowIsTop(!nowIsTop);
          setSelectVisible(false);

          _toast["default"].Success((0, _getLocale.getLocale)("Mod_Successfuloperat"), {
            canClose: false
          });
        } else {
          _toast["default"].Error(resp.Message, {
            canClose: false
          });
        }
      });
    } else {
      (0, _requestApi.topDynamic)({
        feedId: feedId
      }).then(function (resp) {
        if (resp.Code === 1) {
          setNowIsTop(!nowIsTop);
          setSelectVisible(false);

          _toast["default"].Success((0, _getLocale.getLocale)("Mod_Successfuloperat"), {
            canClose: false
          });

          setTimeout(function () {
            // @ts-ignore
            window.reFreshDynamicTopping();
          }, 1000);
        } else {
          _toast["default"].Error(resp.Message, {
            canClose: false
          });
        }
      });
    }
  };

  var handleDeleteClick = function handleDeleteClick() {
    setShowDialogConfirm(true);
    setSelectVisible(false);
  };

  var handleCollectClick = function handleCollectClick() {
    if (nowIsCollect) {
      (0, _requestApi.cancelCollectDynamic)({
        feedId: feedId
      }).then(function (resp) {
        if (resp.Code === 1) {
          setNowIsCollect(!nowIsCollect);
          setSelectVisible(false);

          _toast["default"].Success((0, _getLocale.getLocale)("Mod_Successfuloperat"), {
            canClose: false
          });
        } else {
          _toast["default"].Error(resp.Message, {
            canClose: false
          });
        }
      });
    } else {
      (0, _requestApi.collectDynamic)({
        feedId: feedId
      }).then(function (resp) {
        if (resp.Code === 1) {
          setNowIsCollect(!nowIsCollect);
          setSelectVisible(false);

          _toast["default"].Success((0, _getLocale.getLocale)("Mod_Successfuloperat"), {
            canClose: false
          });
        } else {
          _toast["default"].Error(resp.Message, {
            canClose: false
          });
        }
      });
    }
  };

  var enSureDeleteDynamic = function enSureDeleteDynamic() {
    (0, _requestApi.deleteDynamic)({
      feedId: feedId
    }).then(function (resp) {
      if (resp.Code === 1) {
        _toast["default"].Success((0, _getLocale.getLocale)("Pro_detail_Deletsuccess"), {
          canClose: false
        });

        setShowDialogConfirm(false);

        if (onDeleteFeed) {
          onDeleteFeed(feedId);
        }
      }
    });
  };

  var cancelDeleteDynamic = function cancelDeleteDynamic() {
    setShowDialogConfirm(false);
  };

  var handleShowDropSelector = function handleShowDropSelector() {
    setSelectVisible(true);
  };

  var handleOpenUserPage = function handleOpenUserPage() {
    if (!url) return;
    window.open(url, "_blank");
  };

  var renderDialogConfirm = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement(_dialogConfirm["default"], {
      visible: showDialogConfirm,
      type: "warning"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      type: "title"
    }, feedType === _constant.EFeedType.Share ? (0, _getLocale.getLocale)("OKR_MyO_Err_Delesh") : (0, _getLocale.getLocale)("OKR_MyO_Err_Delepost")), /*#__PURE__*/_react["default"].createElement("span", {
      type: "content"
    }, feedType === _constant.EFeedType.Share ? (0, _getLocale.getLocale)("OKR_MyO_Err_Deleshre") : (0, _getLocale.getLocale)("OKR_MyO_Err_Delepostsu")), /*#__PURE__*/_react["default"].createElement(_dialogConfirm.CancelButton, {
      type: "button",
      onClick: cancelDeleteDynamic
    }, (0, _getLocale.getLocale)("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement(_dialogConfirm.WarningButton, {
      type: "button",
      onClick: enSureDeleteDynamic
    }, (0, _getLocale.getLocale)("Mod_Delete")));
  }, [showDialogConfirm]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix)
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "-left")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(prefix, "-desc-avatar"), _defineProperty({}, "".concat(prefix, "-desc-avatar--not-link"), !url)),
    onClick: handleOpenUserPage
  }, /*#__PURE__*/_react["default"].createElement(_avatar["default"], {
    name: user.name,
    userId: user.id || user.userId,
    src: user.avatar.original || user.avatar.medium,
    color: user.avatar.color,
    size: {
      width: 48,
      textNum: 1,
      fontSize: 18
    }
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "-desc")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-desc-title")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-desc-title-left")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(prefix, "-desc-user"), _defineProperty({}, "".concat(prefix, "-desc-user--not-link"), !url)),
    onClick: handleOpenUserPage
  }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"].Text, {
    maxWidth: 98,
    text: /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
      name: user.name,
      id: user.userId
    })
  })), desc, suffixContent), (showCollect || showTopping || showDelete) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__more-operate")
  }, /*#__PURE__*/_react["default"].createElement(_dropSelector["default"], {
    popupPlacement: "bottomRight",
    checked: ["collect"],
    onChange: onChangeHandler,
    multi: false,
    visible: selectVisible,
    trigger: /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])("".concat(prefix, "-right"), "tu-icon-more1"),
      onClick: handleShowDropSelector
    })
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: showCollect
  }, /*#__PURE__*/_react["default"].createElement(_dropSelector.Item, {
    name: "collect",
    onClick: handleCollectClick
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__more-operate-selection")
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: (0, _classnames["default"])("tu-icon-APP-shoucang", "".concat(prefix, "__more-operate-selection-icon"))
  }), nowIsCollect ? (0, _getLocale.getLocale)("Mod_CancCollection") : (0, _getLocale.getLocale)("Mod_Collection")))), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: showTopping
  }, /*#__PURE__*/_react["default"].createElement(_dropSelector.Item, {
    name: "top",
    onClick: handleTopOperateClick
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__more-operate-selection")
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: (0, _classnames["default"])("tu-icon-zhiding", "".concat(prefix, "__more-operate-selection-icon"))
  }), nowIsTop ? (0, _getLocale.getLocale)("OKR_MyO_Unpinned") : (0, _getLocale.getLocale)("Feed_Top")))), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: showDelete
  }, /*#__PURE__*/_react["default"].createElement(_dropSelector.Item, {
    name: "delete",
    onClick: handleDeleteClick
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__more-operate-selection")
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: (0, _classnames["default"])("tu-icon-del", "".concat(prefix, "__more-operate-selection-icon"))
  }), (0, _getLocale.getLocale)("Mod_Delete"))))))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-desc-time")
  }, createDate))), renderDialogConfirm);
};

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