'use strict';

var React = require('react');
var LocalizationContext = require('./LocalizationContext-6adf6aa8.js');
var VoicePlayer_useVoicePlayer = require('./VoicePlayer/useVoicePlayer.js');
var VoiceRecorder_useVoiceRecorder = require('./VoiceRecorder/useVoiceRecorder.js');
var Channel_context = require('./ChannelProvider-5e48d37b.js');
var ui_VoiceMessgeInput = require('./index-8d25341b.js');
var ui_Modal = require('./ui/Modal.js');
var ui_Button = require('./ui/Button.js');
var useSendbirdStateContext = require('./useSendbirdStateContext.js');
var consts = require('./consts-a6f34df8.js');
var VoicePlayer_context = require('./index-ce276e22.js');
var uuid = require('./uuid-f1d9be11.js');

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

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

const VoiceMessageInputWrapper = _ref => {
  var _config$voiceRecord;
  let {
    channel,
    onCancelClick,
    onSubmitClick
  } = _ref;
  const uuid$1 = React.useRef(uuid.uuidv4()).current;
  const [audioFile, setAudioFile] = React.useState(null);
  const [voiceInputState, setVoiceInputState] = React.useState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
  const [isSubmitted, setSubmit] = React.useState(false);
  const [isDisabled, setDisabled] = React.useState(false);
  const [showModal, setShowModal] = React.useState(false);
  const {
    stringSet
  } = LocalizationContext.useLocalization();
  const {
    config
  } = useSendbirdStateContext();
  const minRecordingTime = (config === null || config === void 0 ? void 0 : (_config$voiceRecord = config.voiceRecord) === null || _config$voiceRecord === void 0 ? void 0 : _config$voiceRecord.minRecordingTime) || consts.VOICE_RECORDER_DEFAULT_MIN;
  const {
    start,
    stop,
    cancel,
    recordingTime,
    recordingStatus,
    recordingLimit
  } = VoiceRecorder_useVoiceRecorder.useVoiceRecorder({
    onRecordingStarted: () => {
      setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.RECORDING);
    },
    onRecordingEnded: audioFile => {
      setAudioFile(audioFile);
    }
  });
  const voicePlayer = VoicePlayer_useVoicePlayer.useVoicePlayer({
    channelUrl: channel === null || channel === void 0 ? void 0 : channel.url,
    key: uuid$1,
    audioFile: audioFile
  });
  const {
    play,
    pause,
    playbackTime,
    playingStatus
  } = voicePlayer;
  const stopVoicePlayer = voicePlayer.stop;

  // disabled state: muted & frozen
  React.useEffect(() => {
    if (Channel_context.isDisabledBecauseFrozen(channel) || Channel_context.isDisabledBecauseMuted(channel)) {
      setDisabled(true);
    } else {
      setDisabled(false);
    }
  }, [channel === null || channel === void 0 ? void 0 : channel.myRole, channel === null || channel === void 0 ? void 0 : channel.isFrozen, channel === null || channel === void 0 ? void 0 : channel.myMutedState]);

  // call onSubmitClick when submit button is clicked and recorded audio file is created
  React.useEffect(() => {
    if (isSubmitted && audioFile) {
      onSubmitClick(audioFile, recordingTime);
      setSubmit(false);
      setAudioFile(null);
    }
  }, [isSubmitted, audioFile, recordingTime]);
  // operate which control button should be displayed
  React.useEffect(() => {
    if (audioFile) {
      if (recordingTime < minRecordingTime) {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
        setAudioFile(null);
      } else if (playingStatus === VoicePlayer_context.VOICE_PLAYER_STATUS.PLAYING) {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.PLAYING);
      } else {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_PLAY);
      }
    }
  }, [audioFile, recordingTime, playingStatus]);
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-voice-message-input-wrapper"
  }, /*#__PURE__*/React__default["default"].createElement(ui_VoiceMessgeInput.VoiceMessageInput, {
    currentValue: recordingStatus === VoiceRecorder_useVoiceRecorder.VoiceRecorderStatus.COMPLETED ? playbackTime : recordingTime,
    maximumValue: recordingStatus === VoiceRecorder_useVoiceRecorder.VoiceRecorderStatus.COMPLETED ? recordingTime : recordingLimit,
    currentType: voiceInputState,
    onCancelClick: () => {
      onCancelClick();
      cancel();
      stopVoicePlayer();
    },
    onSubmitClick: () => {
      if (isDisabled) {
        setShowModal(true);
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
      } else {
        stop();
        pause();
        setSubmit(true);
      }
    },
    onControlClick: type => {
      switch (type) {
        case ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD:
          {
            stopVoicePlayer();
            start();
            break;
          }
        case ui_VoiceMessgeInput.VoiceMessageInputStatus.RECORDING:
          {
            if (recordingTime >= minRecordingTime && !isDisabled) {
              stop();
            } else if (isDisabled) {
              cancel();
              setShowModal(true);
              setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
            } else {
              cancel();
              setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
            }
            break;
          }
        case ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_PLAY:
          {
            play();
            break;
          }
        case ui_VoiceMessgeInput.VoiceMessageInputStatus.PLAYING:
          {
            pause();
            break;
          }
      }
    }
  }), showModal && /*#__PURE__*/React__default["default"].createElement(ui_Modal["default"], {
    className: "sendbird-voice-message-input-wrapper-alert",
    titleText: Channel_context.isDisabledBecauseMuted(channel) ? stringSet.MODAL__VOICE_MESSAGE_INPUT_DISABLED__TITLE_MUTED : stringSet.MODAL__VOICE_MESSAGE_INPUT_DISABLED__TITLE_FROZEN,
    hideFooter: true,
    isCloseOnClickOutside: true,
    onCancel: () => {
      setShowModal(false);
      onCancelClick();
    }
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-voice-message-input-wrapper-alert__body"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Button["default"], {
    className: "sendbird-voice-message-input-wrapper-alert__body__ok-button",
    type: ui_Button.ButtonTypes.PRIMARY,
    size: ui_Button.ButtonSizes.BIG,
    onClick: () => {
      setShowModal(false);
      onCancelClick();
    }
  }, stringSet.BUTTON__OK))));
};

exports.VoiceMessageInputWrapper = VoiceMessageInputWrapper;
//# sourceMappingURL=VoiceMessageInputWrapper-4cca3b34.js.map
