'use strict';

var React = require('react');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var ChannelSettings_context = require('../context.js');
var utils = require('../../utils-6a748c02.js');
var ui_Modal = require('../../ui/Modal.js');
var LocalizationContext = require('../../LocalizationContext-44d2f61b.js');
var MediaQueryContext = require('../../MediaQueryContext-5d311a86.js');
var ui_TextButton = require('../../ui/TextButton.js');
var ui_Label = require('../../index-2bfd366f.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-21ae158b.js');
require('../../UserProfileContext-9d843d68.js');
require('prop-types');
require('../../uuid-dee74248.js');
require('react-dom');
require('../../index-0a446cca.js');
require('../../ui/Button.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../stringSet-7aebf034.js');
require('../../index-88e8fdfa.js');
require('../../color-0c04efbd.js');

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

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

const LeaveChannel = props => {
  var _state$config, _state$config2;
  const {
    onSubmit = utils.noop,
    onCancel = utils.noop
  } = props;
  const {
    channel,
    onLeaveChannel
  } = ChannelSettings_context.useChannelSettingsContext();
  const {
    stringSet
  } = LocalizationContext.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
  } = MediaQueryContext.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["default"].createElement(ui_Modal["default"], {
      className: "sendbird-channel-settings__leave--mobile",
      titleText: getChannelName(channel),
      hideFooter: true,
      isCloseOnClickOutside: true,
      onCancel: onCancel
    }, /*#__PURE__*/React__default["default"].createElement(ui_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["default"].createElement(ui_Label.Label, {
      type: ui_Label.LabelTypography.SUBTITLE_1,
      color: ui_Label.LabelColors.ONBACKGROUND_1
    }, stringSet.CHANNEL_PREVIEW_MOBILE_LEAVE)));
  }
  return /*#__PURE__*/React__default["default"].createElement(ui_Modal["default"], {
    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
  });
};

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