"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.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.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.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.for-each.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.FcrTargetUserType = exports.FcrRoomType = exports.ChatManager = void 0;
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.object.to-string.js");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../icon/type");
var _dropMenu = require("../drop-menu");
var _tooltip = require("../tooltip");
var _iconButton = require("../icon-button");
var _i18n = require("../../i18n");
var _popover = require("../popover");
var _iconText = require("../icon-text");
var _switch = require("../switch");
require("./index.css");
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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /** Room type (copied from core for ui-foundation independence). */
var FcrRoomType = exports.FcrRoomType = /*#__PURE__*/function (FcrRoomType) {
  FcrRoomType[FcrRoomType["Mainroom"] = 0] = "Mainroom";
  FcrRoomType[FcrRoomType["Subroom"] = 1] = "Subroom";
  FcrRoomType[FcrRoomType["Waitingroom"] = 2] = "Waitingroom";
  FcrRoomType[FcrRoomType["Interpreterroom"] = 3] = "Interpreterroom";
  FcrRoomType[FcrRoomType["JoinBeforeHostWaitingRoom"] = 4] = "JoinBeforeHostWaitingRoom";
  FcrRoomType[FcrRoomType["Infinityroom"] = 5] = "Infinityroom";
  return FcrRoomType;
}({});
/** Target user role for privilege (copied from core). */
var FcrTargetUserType = exports.FcrTargetUserType = /*#__PURE__*/function (FcrTargetUserType) {
  FcrTargetUserType[FcrTargetUserType["SELF"] = 1] = "SELF";
  FcrTargetUserType[FcrTargetUserType["HOST"] = 2] = "HOST";
  FcrTargetUserType[FcrTargetUserType["COHOST"] = 4] = "COHOST";
  FcrTargetUserType[FcrTargetUserType["PARTICIPANT"] = 8] = "PARTICIPANT";
  FcrTargetUserType[FcrTargetUserType["AUDIENCE"] = 16] = "AUDIENCE";
  return FcrTargetUserType;
}({});
/** Private chat privilege config (copied from core). */
/** Chat privilege config (copied from core). */
/** Single chat permission level (used for one panel or legacy single-level mode). */
/** Role key for per-role chat level (e.g. webinar guest vs audience). */
/** Chat level conditions by role. Use when room shows separate guest/audience menus. */
var ChatLevelOption = function ChatLevelOption(_ref) {
  var txt = _ref.txt,
    _ref$selected = _ref.selected,
    selected = _ref$selected === void 0 ? false : _ref$selected;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_iconText.IconText, {
    icon: _type.FcrIconType.FCR_CHOOSEIT,
    txt: txt,
    colors: selected ? {
      iconPrimary: 'var(--fcr_ui_scene_icontext1)'
    } : {},
    classnames: selected ? {
      'fcr-icon-text--selected': true
    } : {}
  });
};
var ChatManager = exports.ChatManager = function ChatManager(_ref2) {
  var _allowChatLevelByRole, _allowChatLevelByRole2;
  var isManager = _ref2.isManager,
    setAllowChat = _ref2.setAllowChat,
    setAllowChatWithPayload = _ref2.setAllowChatWithPayload,
    setAllowChatByUserRole = _ref2.setAllowChatByUserRole,
    allowChatLevel = _ref2.allowChatLevel,
    _ref2$showArrow = _ref2.showArrow,
    showArrow = _ref2$showArrow === void 0 ? false : _ref2$showArrow,
    roomType = _ref2.roomType,
    allowChatLevelByRole = _ref2.allowChatLevelByRole,
    _ref2$showChatBubbles = _ref2.showChatBubbles,
    showChatBubbles = _ref2$showChatBubbles === void 0 ? true : _ref2$showChatBubbles,
    onShowChatBubblesChange = _ref2.onShowChatBubblesChange;
  var t = (0, _i18n.useI18n)();
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    managerVisible = _useState2[0],
    setManagerVisible = _useState2[1];
  var guestLevel = (_allowChatLevelByRole = allowChatLevelByRole === null || allowChatLevelByRole === void 0 ? void 0 : allowChatLevelByRole.participant) !== null && _allowChatLevelByRole !== void 0 ? _allowChatLevelByRole : allowChatLevel;
  var audienceLevel = (_allowChatLevelByRole2 = allowChatLevelByRole === null || allowChatLevelByRole === void 0 ? void 0 : allowChatLevelByRole.audience) !== null && _allowChatLevelByRole2 !== void 0 ? _allowChatLevelByRole2 : allowChatLevel;
  var guestOptions = [{
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_presenter_chat_permissions_everyone'),
      selected: guestLevel === 'everyone'
    }),
    key: 'everyone',
    onButtonClick: function onButtonClick() {
      if (guestLevel === 'everyone') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": true,
          "private": {
            host: true,
            cohost: true,
            participant: true
          }
        }, [FcrTargetUserType.PARTICIPANT]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChat === null || setAllowChat === void 0 || setAllowChat(true);
        setManagerVisible(false);
      }
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_presenter_chat_permission_group_only'),
      selected: guestLevel === 'public'
    }),
    key: 'only_public',
    onButtonClick: function onButtonClick() {
      if (guestLevel === 'public') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": true,
          "private": {
            host: true,
            cohost: true,
            participant: false
          }
        }, [FcrTargetUserType.PARTICIPANT]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChatWithPayload === null || setAllowChatWithPayload === void 0 || setAllowChatWithPayload('public');
        setManagerVisible(false);
      }
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_presenter_chat_permission_host_only'),
      selected: guestLevel === 'private_to_manager'
    }),
    key: 'private_to_manager',
    onButtonClick: function onButtonClick() {
      if (guestLevel === 'private_to_manager') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": false,
          "private": {
            host: true,
            cohost: true,
            participant: false
          }
        }, [FcrTargetUserType.PARTICIPANT]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChatWithPayload === null || setAllowChatWithPayload === void 0 || setAllowChatWithPayload('private_to_manager');
        setManagerVisible(false);
      }
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_presenter_chat_permission_disabled'),
      selected: guestLevel === 'no_one'
    }),
    key: 'no_one',
    onButtonClick: function onButtonClick() {
      if (guestLevel === 'no_one') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": false,
          "private": {
            host: false,
            cohost: false,
            participant: false
          }
        }, [FcrTargetUserType.PARTICIPANT]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChat === null || setAllowChat === void 0 || setAllowChat(false);
        setManagerVisible(false);
      }
    }
  }];
  var audienceOptions = [{
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_attendee_chat_permissions_everyone'),
      selected: audienceLevel === 'everyone'
    }),
    key: 'everyone',
    onButtonClick: function onButtonClick() {
      if (audienceLevel === 'everyone') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": true,
          "private": {
            host: true,
            cohost: true,
            participant: false
          }
        }, [FcrTargetUserType.AUDIENCE]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChat === null || setAllowChat === void 0 || setAllowChat(true);
        setManagerVisible(false);
      }
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_attendee_chat_permissions_host_only'),
      selected: audienceLevel === 'private_to_manager'
    }),
    key: 'private_to_manager',
    onButtonClick: function onButtonClick() {
      if (audienceLevel === 'private_to_manager') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": false,
          "private": {
            host: true,
            cohost: true,
            participant: false
          }
        }, [FcrTargetUserType.AUDIENCE]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChatWithPayload === null || setAllowChatWithPayload === void 0 || setAllowChatWithPayload('private_to_manager');
        setManagerVisible(false);
      }
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_webinar_attendee_chat_permissions_disabled'),
      selected: audienceLevel === 'no_one'
    }),
    key: 'no_one',
    onButtonClick: function onButtonClick() {
      if (audienceLevel === 'no_one') return;
      if (setAllowChatByUserRole) {
        setAllowChatByUserRole({
          "public": false,
          "private": {
            host: false,
            cohost: false,
            participant: false
          }
        }, [FcrTargetUserType.AUDIENCE]).then(function () {
          return setManagerVisible(false);
        });
      } else {
        setAllowChat === null || setAllowChat === void 0 || setAllowChat(false);
        setManagerVisible(false);
      }
    }
  }];
  var actions = [{
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_chat_send_open'),
      selected: allowChatLevel === 'everyone'
    }),
    key: 'everyone',
    onButtonClick: function onButtonClick() {
      if (allowChatLevel === 'everyone') {
        return;
      }
      // 开放聊天
      setAllowChat === null || setAllowChat === void 0 || setAllowChat(true);
      setManagerVisible(false);
    }
  },
  // TODO: 环信暂不支持
  // {
  //   title: (
  //     <ChatLevelOption txt={t('chat_permission_public')} selected={allowChatLevel === 'public'} />
  //   ),
  //   key: 'public',
  //   onButtonClick: () => {
  //     if (allowChatLevel === 'public') {
  //       return;
  //     }
  //     setAllowChatWithPayload?.('public');
  //   },
  // },
  // {
  //   title: (
  //     <ChatLevelOption
  //       txt={t('fmt_additional_options_onlyprivatechat')}
  //       selected={allowChatLevel === 'private_to_manager'}
  //     />
  //   ),
  //   key: 'private_to_manager',
  //   onButtonClick: () => {
  //     if (allowChatLevel === 'private_to_manager') {
  //       return;
  //     }
  //     setAllowChatWithPayload?.('private_to_manager');
  //   },
  // },
  {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(ChatLevelOption, {
      txt: t('fmt_chat_send_limit'),
      selected: allowChatLevel === 'no_one'
    }),
    key: 'no_one',
    onButtonClick: function onButtonClick() {
      if (allowChatLevel === 'no_one') {
        return;
      }
      // 全员禁言
      setAllowChat === null || setAllowChat === void 0 || setAllowChat(false);
      setManagerVisible(false);
    }
  }];
  var closePopover = function closePopover() {
    setManagerVisible(false);
  };
  var popoverContent = (0, _react.useMemo)(function () {
    if (roomType === FcrRoomType.Infinityroom) {
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        onClick: function onClick(e) {
          return e.stopPropagation();
        },
        className: "fcr_chat_drop_menu fcr_chat_drop_menu_dual fcr_chat_settings_tooltip",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr_chat_settings_header",
          children: t('fmt_chat_setting_title')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr_chat_settings_divider"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropMenu.FcrDropMenu, {
          title: t('fmt_webinar_presenter_chat_permissions'),
          options: guestOptions.map(function (data) {
            return _objectSpread(_objectSpread({}, data), {}, {
              classNames: (data.key === 'only_public' ? guestLevel === 'public' : guestLevel === data.key) ? 'fcr-drop-menu-option-primary-selected' : ''
            });
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr_chat_settings_divider"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropMenu.FcrDropMenu, {
          title: t('fmt_webinar_attendee_chat_permissions'),
          options: audienceOptions.map(function (data) {
            return _objectSpread(_objectSpread({}, data), {}, {
              classNames: audienceLevel === data.key ? 'fcr-drop-menu-option-primary-selected' : ''
            });
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr_chat_settings_divider"
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "fcr_chat_settings_bubbles",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: "fcr_chat_settings_bubbles_label",
            children: t('fmt_chat_label_show_bubble_tips')
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "fcr_chat_settings_switch",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_switch.FcrSwitch, {
              value: showChatBubbles,
              onChange: function onChange(v) {
                onShowChatBubblesChange === null || onShowChatBubblesChange === void 0 || onShowChatBubblesChange(v);
              },
              labels: {
                on: 'ON',
                off: 'OFF'
              }
            })
          })]
        })]
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      onClick: closePopover,
      className: "fcr_chat_drop_menu",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropMenu.FcrDropMenu, {
        title: t('fmt_chat_setting_value'),
        options: actions.map(function (data) {
          return _objectSpread(_objectSpread({}, data), {}, {
            classNames: allowChatLevel === data.key ? 'fcr-drop-menu-option-primary-selected' : ''
          });
        })
      })
    });
  }, [allowChatLevel, allowChatLevelByRole, setAllowChatByUserRole, setAllowChat, setAllowChatWithPayload, guestLevel, audienceLevel, actions, roomType, showChatBubbles, onShowChatBubblesChange, t]);
  if (!isManager) return null;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    id: "fcr_chat_manager",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_popover.FcrPopover, {
      getTooltipContainer: function getTooltipContainer(e) {
        return e.parentElement || e;
      },
      showArrow: showArrow,
      visible: managerVisible,
      overlayClassName: 'fcr_chat_manager_popover_overlay',
      trigger: "click",
      placement: "bottom",
      overlayInnerStyle: {
        border: 'none',
        minWidth: '200px',
        maxWidth: '280px',
        width: 'fit-content'
      },
      verticalOffset: -90,
      content: popoverContent,
      onVisibleChange: function onVisibleChange(visible) {
        setManagerVisible(visible);
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.FcrToolTip, {
        content: t('fmt_participants_member_button_more'),
        mouseEnterDelay: 0,
        trigger: "hover",
        placement: "bottom",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_iconButton.FcrIconButton, {
            className: "frc-window-header__icon frc-window-header-setting__icon  fcr-no-drag",
            icon: _type.FcrIconType.FCR_DIALOG_MORE,
            onClick: function onClick() {
              setManagerVisible(true);
            }
          })
        })
      })
    })
  });
};