'use strict';

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

var React = require('react');
var useSendbirdStateContext = require('../useSendbirdStateContext.js');
require('../withSendbird.js');
require('../_rollupPluginBabelHelpers-ea66c137.js');

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

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

const CreateOpenChannelContext = /*#__PURE__*/React__default["default"].createContext({
  sdk: null,
  sdkInitialized: false,
  logger: null,
  createNewOpenChannel: null
});
const CreateOpenChannelProvider = _ref => {
  var _stores$sdkStore, _stores$sdkStore2;
  let {
    className,
    children,
    onCreateChannel,
    onBeforeCreateChannel
  } = _ref;
  const {
    stores,
    config
  } = useSendbirdStateContext();
  const {
    logger
  } = config;
  const sdk = (stores === null || stores === void 0 ? void 0 : (_stores$sdkStore = stores.sdkStore) === null || _stores$sdkStore === void 0 ? void 0 : _stores$sdkStore.sdk) || null;
  const sdkInitialized = (stores === null || stores === void 0 ? void 0 : (_stores$sdkStore2 = stores.sdkStore) === null || _stores$sdkStore2 === void 0 ? void 0 : _stores$sdkStore2.initialized) || false;
  const createNewOpenChannel = React.useCallback(params => {
    const {
      name,
      coverUrlOrImage
    } = params;
    if (sdkInitialized) {
      var _sdk$currentUser;
      const params = {};
      params.operatorUserIds = [sdk === null || sdk === void 0 ? void 0 : (_sdk$currentUser = sdk.currentUser) === null || _sdk$currentUser === void 0 ? void 0 : _sdk$currentUser.userId];
      params.name = name;
      params.coverUrlOrImage = coverUrlOrImage;
      sdk.openChannel.createChannel((onBeforeCreateChannel === null || onBeforeCreateChannel === void 0 ? void 0 : onBeforeCreateChannel(params)) || params).then(openChannel => {
        logger.info('CreateOpenChannel: Succeeded creating openChannel', openChannel);
        onCreateChannel(openChannel);
      }).catch(err => {
        logger.warning('CreateOpenChannel: Failed creating openChannel', err);
      });
    }
  }, [sdkInitialized, onBeforeCreateChannel, onCreateChannel]);
  return /*#__PURE__*/React__default["default"].createElement(CreateOpenChannelContext.Provider, {
    value: {
      // interface
      sdk: sdk,
      sdkInitialized: sdkInitialized,
      logger: logger,
      createNewOpenChannel: createNewOpenChannel
    }
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: `sendbird-create-open-channel ${className}`
  }, children));
};
const useCreateOpenChannelContext = () => React__default["default"].useContext(CreateOpenChannelContext);

exports.CreateOpenChannelProvider = CreateOpenChannelProvider;
exports.useCreateOpenChannelContext = useCreateOpenChannelContext;
//# sourceMappingURL=context.js.map
