import React__default from 'react';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { useChannelSettingsContext } from '../context.js';
import { n as noop } from '../../utils-4ce9435a.js';
import Modal from '../../ui/Modal.js';
import { u as useLocalization } from '../../LocalizationContext-bcaf447a.js';
import { u as useMediaQueryContext } from '../../MediaQueryContext-0d6b7936.js';
import TextButton from '../../ui/TextButton.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-208f60f9.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-52628376.js';
import '../../UserProfileContext-96f68187.js';
import 'prop-types';
import '../../uuid-9e3aeec6.js';
import 'react-dom';
import '../../index-5ca598bf.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../stringSet-7f300d56.js';
import '../../index-26c66412.js';
import '../../color-5dee2e2d.js';

const LeaveChannel = props => {
  var _state$config, _state$config2;
  const {
    onSubmit = noop,
    onCancel = noop
  } = props;
  const {
    channel,
    onLeaveChannel
  } = useChannelSettingsContext();
  const {
    stringSet
  } = useLocalization();
  const state = useSendbirdStateContext();
  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;
  const {
    isMobile
  } = useMediaQueryContext();
  const getChannelName = channel => {
    if (channel !== null && channel !== void 0 && channel.name && (channel === null || channel === void 0 ? void 0 : channel.name) !== 'Group Channel') {
      return channel.name;
    }
    if ((channel === null || channel === void 0 ? void 0 : channel.name) === 'Group Channel' || !(channel !== null && channel !== void 0 && channel.name)) {
      return ((channel === null || channel === void 0 ? void 0 : channel.members) || []).map(member => member.nickname || stringSet.NO_NAME).join(', ');
    }
    return stringSet.NO_TITLE;
  };
  if (isMobile) {
    return /*#__PURE__*/React__default.createElement(Modal, {
      className: "sendbird-channel-settings__leave--mobile",
      titleText: getChannelName(channel),
      hideFooter: true,
      isCloseOnClickOutside: true,
      onCancel: onCancel
    }, /*#__PURE__*/React__default.createElement(TextButton, {
      onClick: () => {
        logger.info('ChannelSettings: Leaving channel', channel);
        channel === null || channel === void 0 ? void 0 : channel.leave().then(() => {
          logger.info('ChannelSettings: Leaving channel successful!', channel);
          onLeaveChannel();
        });
      },
      className: "sendbird-channel-settings__leave-label--mobile"
    }, /*#__PURE__*/React__default.createElement(Label, {
      type: LabelTypography.SUBTITLE_1,
      color: LabelColors.ONBACKGROUND_1
    }, stringSet.CHANNEL_PREVIEW_MOBILE_LEAVE)));
  }
  return /*#__PURE__*/React__default.createElement(Modal, {
    isFullScreenOnMobile: true,
    disabled: !isOnline,
    onCancel: onCancel,
    onSubmit: () => {
      logger.info('ChannelSettings: Leaving channel', channel);
      channel === null || channel === void 0 ? void 0 : channel.leave().then(() => {
        logger.info('ChannelSettings: Leaving channel successful!', channel);
        // is for backward compactability
        if (onLeaveChannel) {
          onLeaveChannel();
        } else {
          onSubmit();
        }
      });
    },
    submitText: stringSet.MODAL__LEAVE_CHANNEL__FOOTER,
    titleText: stringSet.MODAL__LEAVE_CHANNEL__TITLE
  });
};

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