'use strict';

var React = require('react');
var LocalizationContext = require('./LocalizationContext-53fd4ae9.js');
var VoicePlayer_useVoicePlayer = require('./VoicePlayer/useVoicePlayer.js');
var VoiceRecorder_useVoiceRecorder = require('./VoiceRecorder/useVoiceRecorder.js');
var Channel_context = require('./ChannelProvider-498847bc.js');
var ui_VoiceMessgeInput = require('./index-e42edbd5.js');
var ui_Modal = require('./ui/Modal.js');
var ui_Button = require('./ui/Button.js');
var useSendbirdStateContext = require('./useSendbirdStateContext.js');
var consts = require('./consts-b08b4ca6.js');
var VoicePlayer_context = require('./index-178229c9.js');
var uuid = require('./uuid-e879709a.js');

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

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

var VoiceMessageInputWrapper = function (_a) {
  var _b;

  var channel = _a.channel,
      onCancelClick = _a.onCancelClick,
      onSubmitClick = _a.onSubmitClick;

  var _c = React.useState(null),
      audioFile = _c[0],
      setAudioFile = _c[1];

  var uuid$1 = React.useState(uuid.uuidv4())[0];

  var _d = React.useState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD),
      voiceInputState = _d[0],
      setVoiceInputState = _d[1];

  var _e = React.useState(false),
      isSubmited = _e[0],
      setSubmit = _e[1];

  var _f = React.useState(false),
      isDisabled = _f[0],
      setDisabled = _f[1];

  var _g = React.useState(false),
      showModal = _g[0],
      setShowModal = _g[1];

  var stringSet = LocalizationContext.useLocalization().stringSet;
  var config = useSendbirdStateContext().config;
  var minRecordingTime = ((_b = config === null || config === void 0 ? void 0 : config.voiceRecord) === null || _b === void 0 ? void 0 : _b.minRecordingTime) || consts.VOICE_RECORDER_DEFAULT_MIN;

  var _h = VoiceRecorder_useVoiceRecorder.useVoiceRecorder({
    onRecordingStarted: function () {
      setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.RECORDING);
    },
    onRecordingEnded: function (audioFile) {
      setAudioFile(audioFile);
    }
  }),
      start = _h.start,
      stop = _h.stop,
      cancel = _h.cancel,
      recordingTime = _h.recordingTime,
      recordingStatus = _h.recordingStatus,
      recordingLimit = _h.recordingLimit;

  var voicePlayer = VoicePlayer_useVoicePlayer.useVoicePlayer({
    channelUrl: channel === null || channel === void 0 ? void 0 : channel.url,
    key: uuid$1,
    audioFile: audioFile
  });
  var play = voicePlayer.play,
      pause = voicePlayer.pause,
      playbackTime = voicePlayer.playbackTime,
      playingStatus = voicePlayer.playingStatus;
  var stopVoicePlayer = voicePlayer.stop; // disabled state: muted & frozen

  React.useEffect(function () {
    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]);
  React.useEffect(function () {
    if (isSubmited && audioFile) {
      onSubmitClick(audioFile, recordingTime);
    }

    if (audioFile) {
      if (recordingTime < minRecordingTime) {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
        setAudioFile(null);
      } else if (playingStatus === VoicePlayer_context.VoicePlayerStatus.PLAYING) {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.PLAYING);
      } else {
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_PLAY);
      }
    }
  }, [isSubmited, 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,
    onSubmitClick: function () {
      if (isDisabled) {
        setShowModal(true);
        setVoiceInputState(ui_VoiceMessgeInput.VoiceMessageInputStatus.READY_TO_RECORD);
      } else {
        stop();
        setSubmit(true);
      }
    },
    onControlClick: function (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: function () {
      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: function () {
      setShowModal(false);
      onCancelClick();
    }
  }, stringSet.BUTTON__OK))));
};

exports.VoiceMessageInputWrapper = VoiceMessageInputWrapper;
//# sourceMappingURL=VoiceMessageInputWrapper-965de260.js.map
