"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ChatWrapper = exports.CHAT_WRAPPER_CLASS = void 0;
require("core-js/modules/web.timers.js");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
require("./index.css");
var _messageTextarea = require("../message-textarea");
var _blinkingBox = require("../blinking-box");
var _type = require("../icon/type");
var _icon = require("../icon");
var _iconText = require("../icon-text");
var _textBubble = require("../text-bubble");
var _multiSelect = require("../multi-select");
var _i18n = require("../../i18n");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var CLEAR_NOTICE_INTERVAL = 10 * 1000;
var FADE_OUT_INTERVAL = 1 * 1000;
var CHAT_WRAPPER_CLASS = exports.CHAT_WRAPPER_CLASS = 'fcr-chat-wrapper';
var ChatWrapper = exports.ChatWrapper = function ChatWrapper(_ref) {
  var styles = _ref.styles,
    errorMsg = _ref.errorMsg,
    singling = _ref.singling,
    forbidden = _ref.forbidden,
    isHost = _ref.isHost,
    forBiddenTxt = _ref.forbiddenTxt,
    children = _ref.children,
    handMsg = _ref.handMsg,
    msgBarElement = _ref.msgBarElement,
    onSelectCancel = _ref.onSelectCancel,
    onSelectConfirm = _ref.onSelectConfirm,
    _ref$multiSelectActiv = _ref.multiSelectActive,
    multiSelectActive = _ref$multiSelectActiv === void 0 ? false : _ref$multiSelectActiv,
    selectCnt = _ref.selectCnt,
    _ref$targetUserIsOnli = _ref.targetUserIsOnline,
    targetUserIsOnline = _ref$targetUserIsOnli === void 0 ? false : _ref$targetUserIsOnli,
    selectTargetNickname = _ref.selectTargetNickname,
    handleClickUploadImage = _ref.handleClickUploadImage,
    _ref$defaultMsg = _ref.defaultMsg,
    defaultMsg = _ref$defaultMsg === void 0 ? '' : _ref$defaultMsg,
    updateChatTextMsg = _ref.updateChatTextMsg,
    privateTarget = _ref.privateTarget,
    autoSizeParams = _ref.autoSizeParams;
  var cls = (0, _classnames["default"])(CHAT_WRAPPER_CLASS, (0, _defineProperty2["default"])({}, "".concat(CHAT_WRAPPER_CLASS, "-radius"), singling));
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    errVisible = _useState2[0],
    setErrVisible = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    showUserOffline = _useState4[0],
    setShowUserOffline = _useState4[1];
  var _useState5 = (0, _react.useState)('info'),
    _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
    noticeType = _useState6[0],
    setNoticeType = _useState6[1];
  var _useState7 = (0, _react.useState)('fcr-notice-animation-in'),
    _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
    tipClassnames = _useState8[0],
    setTipClassnames = _useState8[1];
  var _useState9 = (0, _react.useState)(privateTarget),
    _useState0 = (0, _slicedToArray2["default"])(_useState9, 2),
    lastTarget = _useState0[0],
    setLastTarget = _useState0[1];
  var hideNoticeTimeout = (0, _react.useRef)(0);
  var animationTimeout = (0, _react.useRef)(0);
  var t = (0, _i18n.useI18n)();
  (0, _react.useEffect)(function () {
    if (privateTarget !== lastTarget) {
      setLastTarget(privateTarget);
      // 选择其他在线用户
      if (targetUserIsOnline) {
        handleTipsAnimation(FADE_OUT_INTERVAL);
      }
    }
    return function () {
      clearTimeout(hideNoticeTimeout.current);
      clearTimeout(animationTimeout.current);
    };
  }, [privateTarget]);

  // 渐出
  (0, _react.useEffect)(function () {
    // 用户重新进来, 一定时间后消失
    if (targetUserIsOnline && showUserOffline) {
      handleTipsAnimation(CLEAR_NOTICE_INTERVAL);
    }
    return function () {
      clearTimeout(hideNoticeTimeout.current);
      clearTimeout(animationTimeout.current);
    };
  }, [targetUserIsOnline]);

  // 渐入
  (0, _react.useEffect)(function () {
    if (targetUserIsOnline) {
      return;
    }

    // 选中的用户不在线
    setShowUserOffline(true);
    setNoticeType('info');
    setTipClassnames('fcr-notice-animation-in');
    clearTimeout(hideNoticeTimeout.current);
    clearTimeout(animationTimeout.current);
  }, [selectTargetNickname, targetUserIsOnline]);
  var handleTipsAnimation = function handleTipsAnimation(delay) {
    hideNoticeTimeout.current = window.setTimeout(function () {
      setShowUserOffline(false);
      setNoticeType('info');
      setTipClassnames('fcr-notice-animation-in');
    }, delay);
    animationTimeout.current = window.setTimeout(function () {
      setTipClassnames('fcr-notice-animation-out');
    }, delay - FADE_OUT_INTERVAL);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cls,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames["default"])('fcr-chat-container-wrapper'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-chat-container",
        children: children
      })
    }), multiSelectActive ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_multiSelect.MultiSelect, {
      onCancel: onSelectCancel,
      onConfirm: onSelectConfirm,
      selectCnt: selectCnt
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-chat-msg-box",
      children: forbidden ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_textBubble.TextBubble, {
        styles: {
          minWidth: 312,
          maxHeight: 37
        },
        txt: /*#__PURE__*/(0, _jsxRuntime.jsx)(_iconText.IconText, {
          icon: _type.FcrIconType.FCR_SETTING_NONE,
          colors: {
            iconPrimary: 'var(--fcr_ui_scene_icontext1)'
          },
          txt: forBiddenTxt
        }),
        type: "gray_2",
        lineHeight: "large"
      }) : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blinkingBox.BlinkingBox, {
          visible: errVisible,
          type: 'error',
          onChange: function onChange(val) {
            if (!val) {
              setErrVisible(false);
            }
          },
          content: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            children: errorMsg
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blinkingBox.BlinkingBox, {
          classnames: tipClassnames,
          animationType: "slide",
          styles: {
            justifyContent: 'flex-start'
          },
          visible: showUserOffline,
          type: noticeType,
          onChange: function onChange(val) {
            if (!val) {
              setShowUserOffline(false);
            }
          },
          content: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
            className: "fcr-blink-content--user-state",
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              style: {
                width: 32,
                height: 32
              },
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
                size: 32,
                type: _type.FcrIconType.FCR_ONELEAVE,
                colors: {
                  iconPrimary: noticeType === 'info' ? 'var(--fcr_ui_scene_white10)' : 'var(--fcr_ui_scene_black6)'
                }
              })
            }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("p", {
              children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                className: "fcr-user-offline-nickname fcr-break-one-line fcr-word-break",
                children: selectTargetNickname
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                className: "fcr-user-offline-txt",
                children: t('fmt_chat_tips_otherpartyleave')
              })]
            })]
          })
        }), msgBarElement, /*#__PURE__*/(0, _jsxRuntime.jsx)(_messageTextarea.MessageTextArea, {
          updateErrVisible: function updateErrVisible(visible) {
            setErrVisible(visible);
          },
          onMsg: function onMsg(msg) {
            if (showUserOffline) {
              setNoticeType('warn');
              return;
            }
            setErrVisible(false);
            handMsg && handMsg(msg);
          },
          handleClickUploadImage: handleClickUploadImage,
          defaultMsg: defaultMsg,
          updateChatTextMsg: updateChatTextMsg,
          autoSizeParams: autoSizeParams
        })]
      })
    })]
  });
};