'use strict';

var React = require('react');
var PropTypes = require('prop-types');
var LocalizationContext = require('../../LocalizationContext-6adf6aa8.js');
var ui_ContextMenu = require('../../ui/ContextMenu.js');
var ui_IconButton = require('../../ui/IconButton.js');
var ui_Icon = require('../../ui/Icon.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var utils = require('../../utils-58f8eea0.js');
var ui_Modal = require('../../ui/Modal.js');
var ChannelList_context = require('../../ChannelListProvider-ffde067b.js');
require('../../stringSet-5157206d.js');
require('../../index-6a7f94de.js');
require('../../_rollupPluginBabelHelpers-ea66c137.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../uuid-f1d9be11.js');
require('../../index-86e680cb.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../index-bff918b2.js');
require('../../withSendbird.js');
require('../../index-f84d188a.js');
require('../../ui/Button.js');
require('../../MediaQueryContext-1dd78ce3.js');
require('@sendbird/chat/groupChannel');
require('../../topics-2fe6fef6.js');
require('../../UserProfileContext-2ff8462c.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);

const LeaveChannel = props => {
  var _useChannelListContex, _state$config, _state$config2;
  const {
    channel = null,
    onSubmit = utils.noop,
    onCancel = utils.noop
  } = props;
  const channelFromContext = (_useChannelListContex = ChannelList_context.useChannelListContext()) === null || _useChannelListContex === void 0 ? void 0 : _useChannelListContex.currentChannel;
  const leavingChannel = channel || channelFromContext;
  const state = useSendbirdStateContext();
  const {
    stringSet
  } = LocalizationContext.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["default"].createElement(ui_Modal["default"], {
      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 = React.useRef(null);
  const parentContainerRef = React.useRef(null);
  const [showModal, setShowModal] = React.useState(false);
  const {
    stringSet
  } = React.useContext(LocalizationContext.LocalizationContext);
  return /*#__PURE__*/React__default["default"].createElement("div", {
    role: "button",
    style: {
      display: 'inline-block'
    },
    onKeyDown: e => {
      e.stopPropagation();
    },
    tabIndex: 0,
    onClick: e => {
      e.stopPropagation();
    },
    ref: parentContainerRef
  }, /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"]
  /* eslint-disable react/no-unstable-nested-components */, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default["default"].createElement(ui_IconButton, {
      ref: parentRef,
      onClick: toggleDropdown,
      height: "32px",
      width: "32px"
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      type: ui_Icon.IconTypes.MORE,
      fillColor: ui_Icon.IconColors.PRIMARY,
      width: "24px",
      height: "24px"
    }))
    /* eslint-disable react/no-unstable-nested-components */,
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      parentRef: parentRef,
      parentContainRef: parentContainerRef,
      closeDropdown: closeDropdown
    }, /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.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["default"].createElement(LeaveChannel, {
    channel: channel,
    onSubmit: () => {
      setShowModal(false);
      onLeaveChannel();
    },
    onCancel: () => setShowModal(false)
  }));
}
ChannelPreviewAction.propTypes = {
  disabled: PropTypes__default["default"].bool,
  onLeaveChannel: PropTypes__default["default"].func.isRequired,
  channel: PropTypes__default["default"].shape({})
};
ChannelPreviewAction.defaultProps = {
  disabled: false,
  channel: null
};

module.exports = ChannelPreviewAction;
//# sourceMappingURL=ChannelPreviewAction.js.map
