'use strict';

var React = require('react');
var LocalizationContext = require('../../LocalizationContext-8cc9717d.js');
var topics = require('../../topics-e24ef339.js');
var ui_Modal = require('../../ui/Modal.js');
var ui_Input = require('../../ui/Input.js');
var ui_Avatar = require('../../ui/Avatar.js');
var ui_Button = require('../../ui/Button.js');
var ui_Label = require('../../index-35633fb6.js');
var ui_TextButton = require('../../ui/TextButton.js');
var ui_OpenChannelAvatar = require('../../ui/OpenChannelAvatar.js');
var OpenChannelSettings_context = require('../context.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
require('../../stringSet-ce782136.js');
require('../../index-d749c4c2.js');
require('react-dom');
require('../../index-8029e07a.js');
require('../../ui/Icon.js');
require('prop-types');
require('../../ui/IconButton.js');
require('../../_rollupPluginBabelHelpers-acf412a3.js');
require('../../MediaQueryContext-fad211ad.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-1eceb0c8.js');
require('../../color-5cfb41c7.js');
require('../../utils-d9c99217.js');
require('@sendbird/chat/openChannel');
require('../../UserProfileContext-d993c96b.js');
require('../../withSendbird.js');

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

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

const EditDetails = props => {
  const {
    onCancel
  } = props;
  const globalState = useSendbirdStateContext();
  const {
    logger,
    theme,
    pubSub
  } = globalState.config;
  const {
    channel,
    onBeforeUpdateChannel,
    onChannelModified,
    setChannel
  } = OpenChannelSettings_context.useOpenChannelSettingsContext();
  const inputRef = React.useRef(null);
  const formRef = React.useRef(null);
  const hiddenInputRef = React.useRef(null);
  const [currentImg, setCurrentImg] = React.useState(null);
  const [newFile, setNewFile] = React.useState(null);
  const {
    stringSet
  } = React.useContext(LocalizationContext.LocalizationContext);
  const title = channel === null || channel === void 0 ? void 0 : channel.name;
  return /*#__PURE__*/React__default["default"].createElement(ui_Modal["default"], {
    isFullScreenOnMobile: true,
    titleText: stringSet.MODAL__CHANNEL_INFORMATION__TITLE,
    submitText: stringSet.BUTTON__SAVE,
    onCancel: onCancel,
    onSubmit: () => {
      if (title !== '' && !inputRef.current.value) {
        if (formRef.current.reportValidity) {
          // might not work in explorer
          formRef.current.reportValidity();
        }
        return;
      }
      const currentTitle = inputRef.current.value;
      const currentImg = newFile;
      logger.info('ChannelSettings: Channel information being updated');
      const params = onBeforeUpdateChannel ? onBeforeUpdateChannel(currentTitle, currentImg, channel === null || channel === void 0 ? void 0 : channel.data) : {
        name: currentTitle,
        coverUrlOrImage: currentImg,
        data: channel === null || channel === void 0 ? void 0 : channel.data
      };
      logger.info('ChannelSettings: Updating channel information', params);
      channel === null || channel === void 0 ? void 0 : channel.updateChannel(params).then(updatedChannel => {
        logger.info('ChannelSettings: Channel information update succeeded', updatedChannel);
        onChannelModified === null || onChannelModified === void 0 ? void 0 : onChannelModified(updatedChannel);
        setChannel(updatedChannel);
        pubSub === null || pubSub === void 0 ? void 0 : pubSub.publish(topics.PUBSUB_TOPICS.UPDATE_OPEN_CHANNEL, updatedChannel);
      }).catch(error => {
        logger.error('ChannelSettings: Channel infomation update failed', error);
        setChannel(null);
      });
      onCancel();
    },
    type: ui_Button.ButtonTypes.PRIMARY
  }, /*#__PURE__*/React__default["default"].createElement("form", {
    className: "channel-profile-form",
    ref: formRef,
    onSubmit: e => {
      e.preventDefault();
    }
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "channel-profile-form__img-section"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Input.InputLabel, null, stringSet.MODAL__CHANNEL_INFORMATION__CHANNEL_IMAGE), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "channel-profile-form__avatar"
  }, currentImg ? /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
    height: "80px",
    width: "80px",
    src: currentImg
  }) : /*#__PURE__*/React__default["default"].createElement(ui_OpenChannelAvatar, {
    height: 80,
    width: 80,
    channel: channel,
    theme: theme
  })), /*#__PURE__*/React__default["default"].createElement("input", {
    ref: hiddenInputRef,
    type: "file",
    accept: "image/gif, image/jpeg, image/png",
    style: {
      display: 'none'
    },
    onChange: e => {
      setCurrentImg(URL.createObjectURL(e.target.files[0]));
      setNewFile(e.target.files[0]);
      hiddenInputRef.current.value = '';
    }
  }), /*#__PURE__*/React__default["default"].createElement(ui_TextButton, {
    className: "channel-profile-form__avatar-button",
    onClick: () => hiddenInputRef.current.click(),
    disableUnderline: true
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    type: ui_Label.LabelTypography.BUTTON_1,
    color: ui_Label.LabelColors.PRIMARY
  }, stringSet.MODAL__CHANNEL_INFORMATION__UPLOAD))), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "channel-profile-form__name-section"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Input.InputLabel, null, stringSet.MODAL__CHANNEL_INFORMATION__CHANNEL_NAME), /*#__PURE__*/React__default["default"].createElement(ui_Input["default"], {
    required: title !== '',
    name: "channel-profile-form__name",
    ref: inputRef,
    value: title,
    placeHolder: stringSet.MODAL__CHANNEL_INFORMATION__INPUT__PLACE_HOLDER
  }))));
};

module.exports = EditDetails;
//# sourceMappingURL=EditDetailsModal.js.map
