import React__default, { useState, useEffect } from 'react';
import useSendbirdStateContext from '../useSendbirdStateContext.js';
import { U as UserProfileProvider } from '../UserProfileContext-74e9a128.js';
import { u as uuidv4 } from '../uuid-8b344dd2.js';
import '../withSendbird.js';
import '../_rollupPluginBabelHelpers-cc035350.js';
import 'prop-types';

const ChannelSettingsContext = /*#__PURE__*/React__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] = useState(uuidv4());
  const [channel, setChannel] = useState(null);
  const [invalidChannel, setInvalidChannel] = useState(false);
  const forceUpdateUI = () => {
    setChannelUpdateId(uuidv4());
  };
  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.createElement(ChannelSettingsContext.Provider, {
    value: {
      channelUrl,
      onCloseClick,
      onLeaveChannel,
      onChannelModified,
      onBeforeUpdateChannel,
      queries,
      overrideInviteUser,
      setChannelUpdateId,
      forceUpdateUI,
      channel,
      invalidChannel
    }
  }, /*#__PURE__*/React__default.createElement(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.createElement("div", {
    className: `sendbird-channel-settings ${className}`
  }, children)));
};
const useChannelSettingsContext = () => React__default.useContext(ChannelSettingsContext);

export { ChannelSettingsProvider, useChannelSettingsContext };
//# sourceMappingURL=context.js.map
