import React__default, { useState, useEffect } from 'react';
import { u as useLocalization } from './LocalizationContext-a6aa8be8.js';
import { useVoicePlayer } from './VoicePlayer/useVoicePlayer.js';
import { useVoiceRecorder, VoiceRecorderStatus } from './VoiceRecorder/useVoiceRecorder.js';
import { a as isDisabledBecauseFrozen, b as isDisabledBecauseMuted } from './ChannelProvider-def8e42f.js';
import { V as VoiceMessageInputStatus, a as VoiceMessageInput } from './index-978b3d6d.js';
import Modal from './ui/Modal.js';
import Button, { ButtonTypes, ButtonSizes } from './ui/Button.js';
import useSendbirdStateContext from './useSendbirdStateContext.js';
import { a as VOICE_RECORDER_DEFAULT_MIN } from './consts-886b25ca.js';
import { a as VoicePlayerStatus } from './index-a3a4b35c.js';
import { u as uuidv4 } from './uuid-9f7da91d.js';

var VoiceMessageInputWrapper = function (_a) {
  var _b;

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

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

  var uuid = useState(uuidv4())[0];

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

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

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

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

  var stringSet = 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) || VOICE_RECORDER_DEFAULT_MIN;

  var _h = useVoiceRecorder({
    onRecordingStarted: function () {
      setVoiceInputState(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 = useVoicePlayer({
    channelUrl: channel === null || channel === void 0 ? void 0 : channel.url,
    key: uuid,
    audioFile: audioFile
  });
  var play = voicePlayer.play,
      pause = voicePlayer.pause,
      playbackTime = voicePlayer.playbackTime,
      playingStatus = voicePlayer.playingStatus;
  var stopVoicePlayer = voicePlayer.stop; // disabled state: muted & frozen

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

    if (audioFile) {
      if (recordingTime < minRecordingTime) {
        setVoiceInputState(VoiceMessageInputStatus.READY_TO_RECORD);
        setAudioFile(null);
      } else if (playingStatus === VoicePlayerStatus.PLAYING) {
        setVoiceInputState(VoiceMessageInputStatus.PLAYING);
      } else {
        setVoiceInputState(VoiceMessageInputStatus.READY_TO_PLAY);
      }
    }
  }, [isSubmited, audioFile, recordingTime, playingStatus]);
  return /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-voice-message-input-wrapper"
  }, /*#__PURE__*/React__default.createElement(VoiceMessageInput, {
    currentValue: recordingStatus === VoiceRecorderStatus.COMPLETED ? playbackTime : recordingTime,
    maximumValue: recordingStatus === VoiceRecorderStatus.COMPLETED ? recordingTime : recordingLimit,
    currentType: voiceInputState,
    onCancelClick: onCancelClick,
    onSubmitClick: function () {
      if (isDisabled) {
        setShowModal(true);
        setVoiceInputState(VoiceMessageInputStatus.READY_TO_RECORD);
      } else {
        stop();
        setSubmit(true);
      }
    },
    onControlClick: function (type) {
      switch (type) {
        case VoiceMessageInputStatus.READY_TO_RECORD:
          {
            stopVoicePlayer();
            start();
            break;
          }

        case VoiceMessageInputStatus.RECORDING:
          {
            if (recordingTime >= minRecordingTime && !isDisabled) {
              stop();
            } else if (isDisabled) {
              cancel();
              setShowModal(true);
              setVoiceInputState(VoiceMessageInputStatus.READY_TO_RECORD);
            } else {
              cancel();
              setVoiceInputState(VoiceMessageInputStatus.READY_TO_RECORD);
            }

            break;
          }

        case VoiceMessageInputStatus.READY_TO_PLAY:
          {
            play();
            break;
          }

        case VoiceMessageInputStatus.PLAYING:
          {
            pause();
            break;
          }
      }
    }
  }), showModal && /*#__PURE__*/React__default.createElement(Modal, {
    className: "sendbird-voice-message-input-wrapper-alert",
    titleText: 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.createElement("div", {
    className: "sendbird-voice-message-input-wrapper-alert__body"
  }, /*#__PURE__*/React__default.createElement(Button, {
    className: "sendbird-voice-message-input-wrapper-alert__body__ok-button",
    type: ButtonTypes.PRIMARY,
    size: ButtonSizes.BIG,
    onClick: function () {
      setShowModal(false);
      onCancelClick();
    }
  }, stringSet.BUTTON__OK))));
};

export { VoiceMessageInputWrapper as V };
//# sourceMappingURL=VoiceMessageInputWrapper-f70b47c7.js.map
