import React__default, { useState, useEffect } from 'react';
import { U as Un } from '../openChannel-1548376e.js';
import useSendbirdStateContext from '../useSendbirdStateContext.js';
import { U as UserProfileProvider } from '../UserProfileContext-143a6a42.js';
import { u as uuidv4 } from '../uuid-60ae25c1.js';
import '../__bundle-6316ce22-cd546820.js';
import '../__bundle-7dfccc8c-6c6e7ab4.js';
import '../withSendbird.js';
import '../_rollupPluginBabelHelpers-3043541a.js';
import 'prop-types';

var OpenChannelSettingsContext = /*#__PURE__*/React__default.createContext(undefined);

var OpenChannelSettingsProvider = function OpenChannelSettingsProvider(props) {
  var _a, _b, _c, _d;

  var children = props.children,
      channelUrl = props.channelUrl,
      onCloseClick = props.onCloseClick,
      onChannelModified = props.onChannelModified,
      onBeforeUpdateChannel = props.onBeforeUpdateChannel,
      onDeleteChannel = props.onDeleteChannel; // fetch store from <SendbirdProvider />

  var globalStore = useSendbirdStateContext();
  var sdk = (_b = (_a = globalStore === null || globalStore === void 0 ? void 0 : globalStore.stores) === null || _a === void 0 ? void 0 : _a.sdkStore) === null || _b === void 0 ? void 0 : _b.sdk;
  var logger = (_c = globalStore === null || globalStore === void 0 ? void 0 : globalStore.config) === null || _c === void 0 ? void 0 : _c.logger;
  var currentUserId = (_d = sdk === null || sdk === void 0 ? void 0 : sdk.currentUser) === null || _d === void 0 ? void 0 : _d.userId;

  var _e = useState(null),
      currentChannel = _e[0],
      setChannel = _e[1];

  useEffect(function () {
    if (!channelUrl || !sdk.openChannel) {
      setChannel(null);
      return;
    }

    sdk.openChannel.getChannel(channelUrl).then(function (channel) {
      logger.info('open channel setting: fetched', channel); // TODO: Add pending status

      channel.enter().then(function () {
        setChannel(channel);
        logger.info('OpenChannelSettings | Succeeded to enter channel');
      }).catch(function (error) {
        setChannel(null);
        logger.warning('OpenChannelSettings | Failed to enter channel', error);
      });
    }).catch(function (error) {
      logger.error('open channel setting: error fetching', error);
      setChannel(null);
    });
    return function () {
      if (currentChannel && currentChannel.exit) {
        currentChannel.exit().then(function () {
          logger.info('OpenChannelSettings | Succeeded to exit channel');
        }).catch(function (error) {
          logger.warning('OpenChannelSettings | Failed to exit channel', error);
        });
      }
    };
  }, [channelUrl, sdk]);
  useEffect(function () {
    var _a;

    var channelHandlerId = uuidv4();

    if (currentChannel !== null && ((_a = sdk === null || sdk === void 0 ? void 0 : sdk.openChannel) === null || _a === void 0 ? void 0 : _a.addOpenChannelHandler)) {
      var channelHandlerParams = new Un({
        onOperatorUpdated: function onOperatorUpdated(channel) {
          if ((channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url)) {
            setChannel(channel);
          }
        },
        onUserMuted: function onUserMuted(channel, user) {
          if ((channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url) && (user === null || user === void 0 ? void 0 : user.userId) === currentUserId) {
            setChannel(channel);
          }
        },
        onUserUnmuted: function onUserUnmuted(channel, user) {
          if ((channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url) && (user === null || user === void 0 ? void 0 : user.userId) === currentUserId) {
            setChannel(channel);
          }
        },
        onUserBanned: function onUserBanned(channel, user) {
          if ((channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url) && (user === null || user === void 0 ? void 0 : user.userId) === currentUserId) {
            setChannel(null);
          }
        },
        onUserUnbanned: function onUserUnbanned(channel, user) {
          if ((user === null || user === void 0 ? void 0 : user.userId) === currentUserId) {
            setChannel(channel);
          }
        },
        onChannelChanged: function onChannelChanged(channel) {
          if ((channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url)) {
            setChannel(channel);
          }
        },
        onChannelDeleted: function onChannelDeleted(channelUrl) {
          if (channelUrl === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url)) {
            setChannel(null);
          }
        }
      });
      sdk.openChannel.addOpenChannelHandler(channelHandlerId, channelHandlerParams);
    }

    return function () {
      var _a, _b, _c;

      if (((_a = sdk === null || sdk === void 0 ? void 0 : sdk.openChannel) === null || _a === void 0 ? void 0 : _a.removeOpenChannelHandler) && channelHandlerId) {
        logger.info('OpenChannelSettings | Removing channel handlers', channelHandlerId);
        (_c = (_b = sdk.openChannel).removeOpenChannelHandler) === null || _c === void 0 ? void 0 : _c.call(_b, channelHandlerId);
      }
    };
  }, [currentChannel]);
  return /*#__PURE__*/React__default.createElement(OpenChannelSettingsContext.Provider, {
    value: {
      channelUrl: channelUrl,
      channel: currentChannel,
      setChannel: setChannel,
      onCloseClick: onCloseClick,
      onChannelModified: onChannelModified,
      onBeforeUpdateChannel: onBeforeUpdateChannel,
      onDeleteChannel: onDeleteChannel
    }
  }, /*#__PURE__*/React__default.createElement(UserProfileProvider, {
    isOpenChannel: true,
    renderUserProfile: props === null || props === void 0 ? void 0 : props.renderUserProfile,
    disableUserProfile: props === null || props === void 0 ? void 0 : props.disableUserProfile
  }, children));
};

var useOpenChannelSettingsContext = function useOpenChannelSettingsContext() {
  return React__default.useContext(OpenChannelSettingsContext);
};

export { OpenChannelSettingsProvider, useOpenChannelSettingsContext };
//# sourceMappingURL=context.js.map
