import React__default, { useRef, useState, useContext } from 'react';
import PropTypes from 'prop-types';
import { u as useLocalization, a as LocalizationContext } from '../../LocalizationContext-426f3d58.js';
import ContextMenu, { MenuItems, MenuItem } from '../../ui/ContextMenu.js';
import IconButton from '../../ui/IconButton.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { n as noop } from '../../utils-4bf6e4e6.js';
import Modal from '../../ui/Modal.js';
import { u as useChannelListContext } from '../../ChannelListProvider-e2fb41aa.js';
import '../../stringSet-a62108b2.js';
import '../../index-90420b99.js';
import '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import 'react-dom';
import '../../ui/SortByRow.js';
import '../../uuid-2e8efc75.js';
import '../../index-f83f0aac.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../index-4105bc4d.js';
import '../../withSendbird.js';
import '../../index-3e548496.js';
import '../../ui/Button.js';
import '../../MediaQueryContext-b531adc9.js';
import '@sendbird/chat/groupChannel';
import '../../topics-247fcf3a.js';
import '../../UserProfileContext-a59f0533.js';

const LeaveChannel = props => {
  var _useChannelListContex, _state$config, _state$config2;
  const {
    channel = null,
    onSubmit = noop,
    onCancel = noop
  } = props;
  const channelFromContext = (_useChannelListContex = useChannelListContext()) === null || _useChannelListContex === void 0 ? void 0 : _useChannelListContex.currentChannel;
  const leavingChannel = channel || channelFromContext;
  const state = useSendbirdStateContext();
  const {
    stringSet
  } = useLocalization();
  const logger = state === null || state === void 0 ? void 0 : (_state$config = state.config) === null || _state$config === void 0 ? void 0 : _state$config.logger;
  const isOnline = state === null || state === void 0 ? void 0 : (_state$config2 = state.config) === null || _state$config2 === void 0 ? void 0 : _state$config2.isOnline;
  if (leavingChannel) {
    return /*#__PURE__*/React__default.createElement(Modal, {
      disabled: !isOnline,
      onCancel: onCancel,
      onSubmit: () => {
        logger.info('ChannelSettings: Leaving channel', leavingChannel);
        leavingChannel === null || leavingChannel === void 0 ? void 0 : leavingChannel.leave().then(() => {
          logger.info('ChannelSettings: Leaving channel successful!', leavingChannel);
          onSubmit();
        });
      },
      submitText: stringSet.MODAL__LEAVE_CHANNEL__FOOTER,
      titleText: stringSet.MODAL__LEAVE_CHANNEL__TITLE
    });
  }
};

function ChannelPreviewAction(_ref) {
  let {
    channel,
    disabled,
    onLeaveChannel
  } = _ref;
  const parentRef = useRef(null);
  const parentContainerRef = useRef(null);
  const [showModal, setShowModal] = useState(false);
  const {
    stringSet
  } = useContext(LocalizationContext);
  return /*#__PURE__*/React__default.createElement("div", {
    role: "button",
    style: {
      display: 'inline-block'
    },
    onKeyDown: e => {
      e.stopPropagation();
    },
    tabIndex: 0,
    onClick: e => {
      e.stopPropagation();
    },
    ref: parentContainerRef
  }, /*#__PURE__*/React__default.createElement(ContextMenu
  /* eslint-disable react/no-unstable-nested-components */, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(IconButton, {
      ref: parentRef,
      onClick: toggleDropdown,
      height: "32px",
      width: "32px"
    }, /*#__PURE__*/React__default.createElement(Icon, {
      type: IconTypes.MORE,
      fillColor: IconColors.PRIMARY,
      width: "24px",
      height: "24px"
    }))
    /* eslint-disable react/no-unstable-nested-components */,
    menuItems: closeDropdown => /*#__PURE__*/React__default.createElement(MenuItems, {
      parentRef: parentRef,
      parentContainRef: parentContainerRef,
      closeDropdown: closeDropdown
    }, /*#__PURE__*/React__default.createElement(MenuItem, {
      onClick: () => {
        if (disabled) {
          return;
        }
        setShowModal(true);
        closeDropdown();
      },
      dataSbId: "channel_list_item_context_menu_leave_channel"
    }, stringSet.CHANNEL_SETTING__LEAVE_CHANNEL__TITLE))
  }), showModal && /*#__PURE__*/React__default.createElement(LeaveChannel, {
    channel: channel,
    onSubmit: () => {
      setShowModal(false);
      onLeaveChannel();
    },
    onCancel: () => setShowModal(false)
  }));
}
ChannelPreviewAction.propTypes = {
  disabled: PropTypes.bool,
  onLeaveChannel: PropTypes.func.isRequired,
  channel: PropTypes.shape({})
};
ChannelPreviewAction.defaultProps = {
  disabled: false,
  channel: null
};

export { ChannelPreviewAction as default };
//# sourceMappingURL=ChannelPreviewAction.js.map
