'use strict';

var React = require('react');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var ChannelSettings_context = require('../context.js');
var utils = require('../../utils-33e5dfc7.js');
var ui_Modal = require('../../ui/Modal.js');
var LocalizationContext = require('../../LocalizationContext-caedb682.js');
var MediaQueryContext = require('../../MediaQueryContext-cdba25c1.js');
var ui_TextButton = require('../../ui/TextButton.js');
var ui_Label = require('../../index-06816ee5.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-41d3db2d.js');
require('../../UserProfileContext-80409a4d.js');
require('prop-types');
require('../../uuid-2c11c530.js');
require('react-dom');
require('../../index-d47bc642.js');
require('../../ui/Button.js');
require('../../tslib.es6-f1d22d7f.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../stringSet-af541fde.js');
require('../../index-e01cd4a9.js');
require('../../color-d7217651.js');

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

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

var LeaveChannel = function (props) {
  var _a, _b;

  var _c = props.onSubmit,
      onSubmit = _c === void 0 ? utils.noop : _c,
      _d = props.onCancel,
      onCancel = _d === void 0 ? utils.noop : _d;

  var _e = ChannelSettings_context.useChannelSettingsContext(),
      channel = _e.channel,
      onLeaveChannel = _e.onLeaveChannel;

  var stringSet = LocalizationContext.useLocalization().stringSet;
  var state = useSendbirdStateContext();
  var logger = (_a = state === null || state === void 0 ? void 0 : state.config) === null || _a === void 0 ? void 0 : _a.logger;
  var isOnline = (_b = state === null || state === void 0 ? void 0 : state.config) === null || _b === void 0 ? void 0 : _b.isOnline;
  var isMobile = MediaQueryContext.useMediaQueryContext().isMobile;

  var getChannelName = function (channel) {
    if ((channel === null || channel === void 0 ? 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 ? void 0 : channel.name)) {
      return ((channel === null || channel === void 0 ? void 0 : channel.members) || []).map(function (member) {
        return 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: function () {
        logger.info('ChannelSettings: Leaving channel', channel);
        channel === null || channel === void 0 ? void 0 : channel.leave().then(function () {
          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: function () {
      logger.info('ChannelSettings: Leaving channel', channel);
      channel === null || channel === void 0 ? void 0 : channel.leave().then(function () {
        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
