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

var _mblog = _interopRequireDefault(require("../mblog"));

var _requistApi = require("./requist-api");

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

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

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

require("./publish-dynamic-modal.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 _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 = "publish-dynamic-modal";
var PublishDynamicModal = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var onSubmitSuccess = _ref.onSubmitSuccess;

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

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

  var _useState5 = (0, _react.useState)(true),
      _useState6 = _slicedToArray(_useState5, 2),
      isShare = _useState6[0],
      setIsShare = _useState6[1];

  var onMblogSubmit = function onMblogSubmit(submitType, sendData) {
    sendData.FileUrls = sendData.FileUrl;
    sendData.Visibility = sendData.visibility;
    delete sendData.FileUrl;
    delete sendData.visibility;

    if (submitType === "submit") {
      setSubmitStatus(2);

      if (isShare) {
        (0, _requistApi.addShare)(sendData).then(function (resp) {
          if (resp.Code === 1) {
            _toast["default"].Success((0, _getLocale.getLocale)("Pro_tag_pop_Pubsuc"), {
              canClose: false
            });

            localStorage.removeItem("mblogLocalStroage1_".concat((0, _bsGlobal.getUserInfo)().Id, "_commentsTypeKey_").concat(isShare ? "share" : "announcement"));
            setVisible(false);
            setSubmitStatus(1);

            if (onSubmitSuccess) {
              onSubmitSuccess();
            }
          } else {
            _toast["default"].Error(resp.Message, {
              canClose: false
            });

            setSubmitStatus(0);
          }
        })["catch"](function (err) {
          setSubmitStatus(0);

          _toast["default"].Error(err, {
            canClose: false
          });
        });
      } else {
        (0, _requistApi.addNotice)(sendData).then(function (resp) {
          if (resp.Code === 1) {
            _toast["default"].Success((0, _getLocale.getLocale)("Pro_tag_pop_Pubsuc"), {
              canClose: false
            });

            localStorage.removeItem("mblogLocalStroage1_".concat((0, _bsGlobal.getUserInfo)().Id, "_commentsTypeKey_").concat(isShare ? "share" : "announcement"));
            setVisible(false);
            setSubmitStatus(1);
            setTimeout(function () {
              // @ts-ignore 跳到我的发布页签
              window.setTabsToPublish && window.setTabsToPublish();
            }, 2000);
          } else {
            _toast["default"].Error(resp.Message, {
              canClose: false
            });

            setSubmitStatus(0);
          }
        })["catch"](function (err) {
          setSubmitStatus(0);

          _toast["default"].Error(err, {
            canClose: false
          });
        });
      }
    }
  };

  var hidePublishDynamicModal = function hidePublishDynamicModal() {
    setVisible(false);
  };

  var showPublishShareDynamicModal = function showPublishShareDynamicModal() {
    setIsShare(true);
    setVisible(true);
  };

  var showPublishAnnouncementDynamicModal = function showPublishAnnouncementDynamicModal() {
    setIsShare(false);
    setVisible(true);
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      hide: hidePublishDynamicModal,
      showShareModal: showPublishShareDynamicModal,
      showAnnouncementModal: showPublishAnnouncementDynamicModal
    };
  });
  (0, _react.useEffect)(function () {
    // @ts-ignore
    window.hidePublishDynamicModal = hidePublishDynamicModal; // @ts-ignore

    window.showPublishShareDynamicModal = showPublishShareDynamicModal; // @ts-ignore

    window.showPublishAnnouncementDynamicModal = showPublishAnnouncementDynamicModal;
  }, []);
  return /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    title: isShare ? "".concat((0, _getLocale.getLocale)("Feed_Set_Pop_Butt_Sendshare")) : "".concat((0, _getLocale.getLocale)("Feed_Set_Pop_Butt_Announcement")),
    visible: visible,
    onClose: hidePublishDynamicModal,
    noFooter: true,
    style: {
      width: _getLocale.isEn ? "680px" : "480px"
    },
    destroyOnClose: true,
    noHeadLine: true
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: prefix
  }, /*#__PURE__*/_react["default"].createElement(_mblog["default"], {
    defaultContent: isShare ? "" : "@[所有同事:all_members] ",
    placeholderText: isShare ? "分享你此刻的想法" : "重要的通知可以发公告哦",
    isHasPlaceholder: true,
    isShowRangeDropDown: true,
    isShowSendSms: false,
    isShowBtnCancel: false,
    isShowRange: true,
    submitText: (0, _getLocale.getLocale)("OKR_MyO_KRP_Butt_Release"),
    initialTextHeight: 96,
    autoResizing: false,
    autoFocus: false,
    showAtAll: !isShare,
    privateValue: 1,
    userSelectorPopAlign: "top left",
    emotionsSelectorPopAlign: "top left",
    commentsTypeKey: isShare ? "share" : "announcement",
    disableRangeSelect: false,
    submitStatus: submitStatus // 提交状态，0：失败，1：成功，2：提交中
    ,
    onMblogSubmit: onMblogSubmit
  })));
});
PublishDynamicModal.defaultProps = {
  onSubmitSuccess: function onSubmitSuccess() {}
};
var _default = PublishDynamicModal;
exports["default"] = _default;