'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var useSendbirdStateContext = require('../useSendbirdStateContext.js');
var UserProfileContext = require('../UserProfileContext-9d843d68.js');
var uuid = require('../uuid-dee74248.js');
require('../withSendbird.js');
require('../_rollupPluginBabelHelpers-21ae158b.js');
require('prop-types');

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

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

const ChannelSettingsContext = /*#__PURE__*/React__default["default"].createContext(undefined);
const ChannelSettingsProvider = props => {
  var _props$disableUserPro;
  const {
    children,
    className,
    channelUrl,
    onCloseClick,
    onLeaveChannel,
    onChannelModified,
    overrideInviteUser,
    onBeforeUpdateChannel,
    queries
  } = props;

  // fetch store from <SendbirdProvider />
  const globalStore = useSendbirdStateContext();
  const {
    config,
    stores
  } = globalStore;
  const {
    sdkStore
  } = stores;
  const {
    logger,
    onUserProfileMessage
  } = config;
  const {
    initialized
  } = sdkStore;
  const sdk = sdkStore === null || sdkStore === void 0 ? void 0 : sdkStore.sdk;

  // hack to keep track of channel updates by triggering useEffect
  const [channelUpdateId, setChannelUpdateId] = React.useState(uuid.uuidv4());
  const [channel, setChannel] = React.useState(null);
  const [invalidChannel, setInvalidChannel] = React.useState(false);
  const forceUpdateUI = () => {
    setChannelUpdateId(uuid.uuidv4());
  };
  React.useEffect(() => {
    logger.info('ChannelSettings: Setting up');
    if (!channelUrl || !initialized || !sdk) {
      logger.warning('ChannelSettings: Setting up failed', 'No channelUrl or sdk uninitialized');
      setInvalidChannel(false);
    } else {
      if (!sdk || !sdk.groupChannel) {
        logger.warning('ChannelSettings: No GroupChannel');
        return;
      }
      sdk.groupChannel.getChannel(channelUrl).then(groupChannel => {
        if (!groupChannel) {
          logger.warning('ChannelSettings: Channel not found');
          setInvalidChannel(true);
        } else {
          logger.info('ChannelSettings: Fetched group channel', groupChannel);
          setInvalidChannel(false);
          setChannel(groupChannel);
        }
      });
    }
  }, [channelUrl, initialized, channelUpdateId]);
  return /*#__PURE__*/React__default["default"].createElement(ChannelSettingsContext.Provider, {
    value: {
      channelUrl,
      onCloseClick,
      onLeaveChannel,
      onChannelModified,
      onBeforeUpdateChannel,
      queries,
      overrideInviteUser,
      setChannelUpdateId,
      forceUpdateUI,
      channel,
      invalidChannel
    }
  }, /*#__PURE__*/React__default["default"].createElement(UserProfileContext.UserProfileProvider, {
    renderUserProfile: props === null || props === void 0 ? void 0 : props.renderUserProfile,
    disableUserProfile: (_props$disableUserPro = props === null || props === void 0 ? void 0 : props.disableUserProfile) !== null && _props$disableUserPro !== void 0 ? _props$disableUserPro : config === null || config === void 0 ? void 0 : config.disableUserProfile,
    onUserProfileMessage: onUserProfileMessage
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: `sendbird-channel-settings ${className}`
  }, children)));
};
const useChannelSettingsContext = () => React__default["default"].useContext(ChannelSettingsContext);

exports.ChannelSettingsProvider = ChannelSettingsProvider;
exports.useChannelSettingsContext = useChannelSettingsContext;
//# sourceMappingURL=context.js.map
