"use strict";

require("core-js/modules/es.object.define-property.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrParticipantsFooterWithRevokeHost = exports.FcrParticipantsFooterRename = exports.FcrParticipantsFooter = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.array.from.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.array.unshift.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.map.js");
var _react = require("react");
var _button = require("../../button");
var _checkbox = require("../../checkbox");
var _dropMenu = require("../../drop-menu");
var _icon = require("../../icon");
var _type = require("../../icon/type");
var _popover = require("../../popover");
var _i18n = require("../../../i18n");
require("./index.css");
var _jsxRuntime = require("react/jsx-runtime");
var ACTION_MUTE_ALL_AUDIO = 'MUTE_ALL_AUDIO';
var ACTION_UNMUTE_ALL_AUDIO = 'UNMUTE_ALL_AUDIO';
var ACTION_REVOKE_HOST = 'REVOKE_HOST';
var ACTION_PLAY_JOIN_LEAVE_SOUND = 'PLAY_JOIN_LEAVE_SOUND';
var ACTION_LOCK_ROOM = 'LOCK_ROOM';
var ACTION_ENABLE_WAITING_ROOM = 'ENABLE_WAITING_ROOM';
var ACTION_ALLOW_ATTENDEE_TO_UNMUTE = 'ALLOW_ATTENDEE_TO_UNMUTE';
var ACTION_RENAME = 'RENAME';
var ACTION_ALLOW_ATTENDEE_TO_OPEN_CAMERA = 'ALLOW_ATTENDEE_TO_OPEN_CAMERA';
var ACTION_REVOKE_HOST_BY_TICKET = 'ACTION_REVOKE_HOST_BY_TICKET';
var ACTION_CHANGE_NAME = 'ACTION_CHANGE_NAME';
var ACTION_REJECT_ALL_HANDS_UP = 'REJECT_ALL_HANDS_UP';
var createAllActions = function createAllActions(t, onRoomControlClick, hasRevokeHostPermission) {
  var allActions = [{
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_security_buttons_allhandsdown'),
      styleType: "white"
    }),
    key: 'mute_all',
    type: 'disabled',
    onButtonClick: function onButtonClick() {},
    classNames: 'room-control-drop-with-under-line'
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: true,
      label: t('fmt_security_options_silence'),
      styleType: "white"
    }),
    key: 'join_room_with_mute',
    type: 'primary',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_MUTE_ALL_AUDIO);
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_security_options_playnotification'),
      styleType: "white"
    }),
    key: 'play_join_leave_sound',
    type: 'disabled',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_PLAY_JOIN_LEAVE_SOUND);
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_screenshare_controlbar_security_list_lock'),
      styleType: "white"
    }),
    key: 'lock_room',
    type: 'disabled',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_LOCK_ROOM);
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_security_options_activatewaitingroom'),
      styleType: "white"
    }),
    key: 'enable_waiting_room',
    type: 'disabled',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_ENABLE_WAITING_ROOM);
    },
    classNames: 'room-control-drop-with-under-line'
  }, {
    title: t('fmt_screenshare_options_allow'),
    key: 'allow_attendee_title',
    type: 'title'
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_screenshare_controlbar_security_list_participant_unmute'),
      styleType: "white"
    }),
    key: 'unmute_yourself',
    type: 'primary',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_ALLOW_ATTENDEE_TO_UNMUTE);
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_screenshare_controlbar_security_list_participant_changeName'),
      styleType: "white"
    }),
    key: 'rename',
    type: 'disabled',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_RENAME);
    }
  }, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
      checked: false,
      label: t('fmt_security_start_video'),
      styleType: "white"
    }),
    key: 'start_video',
    type: 'primary',
    onButtonClick: function onButtonClick() {
      onRoomControlClick(ACTION_ALLOW_ATTENDEE_TO_OPEN_CAMERA);
    },
    classNames: 'room-control-drop-with-under-line'
  }];
  if (hasRevokeHostPermission) {
    allActions.unshift({
      title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
        checked: false,
        label: t('fmt_security_options_restract'),
        styleType: "white"
      }),
      key: 'revoke_host',
      type: 'primary',
      onButtonClick: function onButtonClick() {
        onRoomControlClick(ACTION_REVOKE_HOST);
      },
      classNames: 'room-control-drop-with-under-line'
    });
  }
  return allActions;
};
var FOOTER_ACTION_GAP_PX = 8;
function computeVisiblePrefixCount(widths, containerWidth, overflowTriggerWidth) {
  var n = widths.length;
  if (n === 0 || containerWidth <= 0) {
    return 0;
  }
  var prefixSum = new Array(n + 1);
  prefixSum[0] = 0;
  for (var i = 0; i < n; i++) {
    prefixSum[i + 1] = prefixSum[i] + widths[i];
  }
  for (var k = n; k >= 0; k--) {
    var prefix = prefixSum[k] + (k > 0 ? (k - 1) * FOOTER_ACTION_GAP_PX : 0);
    var needOverflow = k < n;
    var total = prefix + (needOverflow ? (k > 0 ? FOOTER_ACTION_GAP_PX : 0) + overflowTriggerWidth : 0);
    if (total <= containerWidth) {
      return k;
    }
  }
  return 0;
}
var FcrParticipantsFooter = exports.FcrParticipantsFooter = function FcrParticipantsFooter(props) {
  var _props$onRoomControlC = props.onRoomControlClick,
    onRoomControlClick = _props$onRoomControlC === void 0 ? function () {} : _props$onRoomControlC,
    moreDropMenu = props.moreDropMenu,
    _props$showMeetingMan = props.showMeetingManagement,
    showMeetingManagement = _props$showMeetingMan === void 0 ? true : _props$showMeetingMan,
    hasRevokeHostPermission = props.hasRevokeHostPermission,
    _props$openMuteAllDia = props.openMuteAllDialog,
    openMuteAllDialog = _props$openMuteAllDia === void 0 ? function () {} : _props$openMuteAllDia,
    _props$allowChangeSel = props.allowChangeSelfName,
    allowChangeSelfName = _props$allowChangeSel === void 0 ? false : _props$allowChangeSel,
    participantListTab = props.participantListTab,
    _props$raiseHandsCoun = props.raiseHandsCount,
    raiseHandsCount = _props$raiseHandsCoun === void 0 ? 0 : _props$raiseHandsCoun;
  var t = (0, _i18n.useI18n)();
  var meetingManagementContent = (0, _react.useMemo)(function () {
    return moreDropMenu !== null && moreDropMenu !== void 0 ? moreDropMenu : /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropMenu.FcrDropMenu, {
      options: createAllActions(t, onRoomControlClick, !!hasRevokeHostPermission)
    });
  }, [moreDropMenu, t, onRoomControlClick, hasRevokeHostPermission]);
  var itemElements = (0, _react.useMemo)(function () {
    var el = [];
    if (hasRevokeHostPermission) {
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(FcrParticipantsFooterWithRevokeHost, {
          type: "revoke",
          onRoomControlClick: onRoomControlClick
        })
      }, "revoke"));
    }
    if (participantListTab !== 'audience') {
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
          type: "secondary-bordered",
          size: "XS",
          onClick: openMuteAllDialog,
          children: t('fmt_participants_options_allmute')
        })
      }, "mute"));
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
          type: "secondary-bordered",
          size: "XS",
          onClick: function onClick() {
            return onRoomControlClick(ACTION_UNMUTE_ALL_AUDIO);
          },
          children: t('fmt_participants_button_unmuteAll')
        })
      }, "unmute"));
    }
    if (participantListTab === 'audience') {
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
          type: "secondary-bordered",
          size: "XS",
          disabled: raiseHandsCount === 0,
          onClick: function onClick() {
            return onRoomControlClick(ACTION_REJECT_ALL_HANDS_UP);
          },
          children: t('fmt_security_buttons_allhandsdown')
        })
      }, "reject-all-hands-up"));
    }
    if (showMeetingManagement) {
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_popover.FcrPopover, {
          showArrow: false,
          trigger: "click",
          overlayClassName: "attendee__footer-more-actions",
          mouseEnterDelay: 0,
          overlayInnerStyle: {
            boxShadow: 'none',
            border: 'none',
            minWidth: '200px',
            width: 'auto',
            maxWidth: '280px'
          },
          content: meetingManagementContent,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
            type: "secondary-bordered",
            size: "XS",
            postIcon: _type.FcrIconType.FCR_DROPDOWN,
            children: t('fmt_participants_button_meetingManagement')
          })
        })
      }, "meeting-mgmt"));
    }
    if (allowChangeSelfName) {
      el.push(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(FcrParticipantsFooterRename, {
          onRoomControlClick: onRoomControlClick
        })
      }, "rename"));
    }
    return el;
  }, [t, onRoomControlClick, hasRevokeHostPermission, openMuteAllDialog, meetingManagementContent, showMeetingManagement, allowChangeSelfName, participantListTab, raiseHandsCount]);
  var containerRef = (0, _react.useRef)(null);
  var itemsMeasureRef = (0, _react.useRef)(null);
  var overflowMeasureRef = (0, _react.useRef)(null);
  var totalCount = itemElements.length;
  var _useState = (0, _react.useState)(totalCount),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    visiblePrefix = _useState2[0],
    setVisiblePrefix = _useState2[1];
  (0, _react.useLayoutEffect)(function () {
    var recalc = function recalc() {
      var _overflowMeasureRef$c, _overflowMeasureRef$c2;
      var container = containerRef.current;
      var itemsMeasure = itemsMeasureRef.current;
      if (!container || !itemsMeasure) {
        return;
      }
      var cw = container.clientWidth;
      var widths = Array.from(itemsMeasure.children).map(function (c) {
        return c.offsetWidth;
      });
      var overflowW = (_overflowMeasureRef$c = (_overflowMeasureRef$c2 = overflowMeasureRef.current) === null || _overflowMeasureRef$c2 === void 0 ? void 0 : _overflowMeasureRef$c2.offsetWidth) !== null && _overflowMeasureRef$c !== void 0 ? _overflowMeasureRef$c : 40;
      var next = computeVisiblePrefixCount(widths, cw, overflowW);
      setVisiblePrefix(function (prev) {
        return prev === next ? prev : next;
      });
    };
    recalc();
    var el = containerRef.current;
    var rafId = 0;
    var ro = new ResizeObserver(function () {
      if (rafId) cancelAnimationFrame(rafId);
      rafId = requestAnimationFrame(function () {
        rafId = 0;
        recalc();
      });
    });
    if (el) {
      ro.observe(el);
    }
    return function () {
      if (rafId) cancelAnimationFrame(rafId);
      ro.disconnect();
    };
  }, [itemElements]);
  var overflowPopover = /*#__PURE__*/(0, _jsxRuntime.jsx)(_popover.FcrPopover, {
    showArrow: false,
    trigger: "click",
    overlayClassName: "attendee__footer-more-actions fcr-participant__footer-overflow-popover",
    mouseEnterDelay: 0,
    overlayInnerStyle: {
      boxShadow: 'none',
      border: 'none',
      minWidth: '150px',
      width: 'auto',
      maxWidth: '280px',
      padding: '8px'
    },
    content: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-participant__footer-overflow-list",
      children: itemElements.slice(visiblePrefix).map(function (node, i) {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-participant__footer-overflow-row",
          children: node
        }, node.key != null ? String(node.key) : "footer-overflow-".concat(i));
      })
    }),
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.FcrButton, {
      type: "secondary-bordered",
      size: "XS",
      children: [t('fmt_participants_member_button_more'), /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
        type: _type.FcrIconType.FCR_MORE2,
        size: 14,
        colors: {
          iconPrimary: 'var(--fcr_ui_scene_icontext1)'
        },
        style: {
          display: 'inline-block',
          marginTop: -2
        }
      })]
    })
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "fcr-participant__footer fcr-participant__footer--host-actions",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-participant__footer-measure",
      "aria-hidden": true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        ref: itemsMeasureRef,
        className: "fcr-participant__footer-measure-items",
        children: itemElements
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        ref: overflowMeasureRef,
        className: "fcr-participant__footer-overflow-trigger-measure",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
          type: "secondary-bordered",
          size: "XS",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
            type: _type.FcrIconType.FCR_MORE2,
            size: 14,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1)'
            }
          })
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      ref: containerRef,
      className: "fcr-participant__footer-visible-row",
      children: [itemElements.slice(0, visiblePrefix), visiblePrefix < totalCount && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "fcr-participant__footer-item fcr-participant__footer-overflow-trigger",
        children: overflowPopover
      })]
    })]
  });
};
var FcrParticipantsFooterRename = exports.FcrParticipantsFooterRename = function FcrParticipantsFooterRename(_ref) {
  var onRoomControlClick = _ref.onRoomControlClick;
  var t = (0, _i18n.useI18n)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
    type: "secondary-bordered",
    shape: "rounded",
    size: "XS",
    onClick: function onClick() {
      return onRoomControlClick === null || onRoomControlClick === void 0 ? void 0 : onRoomControlClick(ACTION_CHANGE_NAME);
    },
    children: t('fmt_additional_option_rename_in_meeting')
  });
};
var FcrParticipantsFooterWithRevokeHost = exports.FcrParticipantsFooterWithRevokeHost = function FcrParticipantsFooterWithRevokeHost(_ref2) {
  var type = _ref2.type,
    onRoomControlClick = _ref2.onRoomControlClick;
  var t = (0, _i18n.useI18n)();
  var isRevoke = type === 'revoke';
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
    type: "secondary-bordered",
    size: "XS",
    onClick: function onClick() {
      return onRoomControlClick === null || onRoomControlClick === void 0 ? void 0 : onRoomControlClick(isRevoke ? ACTION_REVOKE_HOST : ACTION_REVOKE_HOST_BY_TICKET);
    },
    children: isRevoke ? t('fmt_hostkey_participant_button_reclaim_host') : t('fmt_hostkey_participant_button_claim_host')
  });
};