import { useState, useEffect } from 'react';
import { u as useVoicePlayerContext, b as AudioUnitDefaultValue } from '../index-7d496661.js';
import { g as VOICE_PLAYER_AUDIO_ID } from '../consts-33537265.js';
import { useVoiceRecorderContext } from '../VoiceRecorder/context.js';
import '../_rollupPluginBabelHelpers-4f8ebde4.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';
import '../LocalizationContext-b2390bf8.js';
import '../stringSet-eefbc798.js';
import '../index-67135027.js';
import '../ui/Modal.js';
import 'react-dom';
import '../index-896545a0.js';
import '../ui/Button.js';
import '../index-4372fcec.js';
import 'prop-types';
import '../ui/Icon.js';
import '../ui/IconButton.js';
import '../MediaQueryContext-b4904a2e.js';

const generateGroupKey = function () {
  let channelUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  let key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  return `${channelUrl}-${key}`;
};

const useVoicePlayer = _ref => {
  var _voicePlayerStore$aud;
  let {
    key = '',
    channelUrl = '',
    audioFile = null,
    audioFileUrl = ''
  } = _ref;
  const [groupKey] = useState(generateGroupKey(channelUrl, key));
  const {
    play,
    pause,
    stop,
    voicePlayerStore
  } = useVoicePlayerContext();
  const {
    isRecordable
  } = useVoiceRecorderContext();
  const currentAudioUnit = (voicePlayerStore === null || voicePlayerStore === void 0 ? void 0 : (_voicePlayerStore$aud = voicePlayerStore.audioStorage) === null || _voicePlayerStore$aud === void 0 ? void 0 : _voicePlayerStore$aud[groupKey]) || AudioUnitDefaultValue();
  const playVoicePlayer = () => {
    if (!isRecordable) {
      play === null || play === void 0 ? void 0 : play({
        groupKey,
        audioFile,
        audioFileUrl
      });
    }
  };
  const pauseVoicePlayer = () => {
    pause === null || pause === void 0 ? void 0 : pause(groupKey);
  };
  const stopVoicePlayer = function () {
    let text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    stop === null || stop === void 0 ? void 0 : stop(text);
  };
  useEffect(() => {
    return () => {
      if (audioFile || audioFileUrl) {
        var _pause, _ref2;
        // Can't get the current AudioPlayer through the React hooks(useReducer or useState) in this scope
        const voiceAudioPlayerElement = document.getElementById(VOICE_PLAYER_AUDIO_ID);
        voiceAudioPlayerElement === null || voiceAudioPlayerElement === void 0 ? void 0 : (_pause = (_ref2 = voiceAudioPlayerElement).pause) === null || _pause === void 0 ? void 0 : _pause.call(_ref2);
      }
    };
  }, []);
  return {
    play: playVoicePlayer,
    pause: pauseVoicePlayer,
    stop: stopVoicePlayer,
    playbackTime: currentAudioUnit.playbackTime * 1000,
    duration: currentAudioUnit.duration * 1000,
    // the unit of playbackTime and duration should be millisecond
    playingStatus: currentAudioUnit.playingStatus
  };
};

export { useVoicePlayer };
//# sourceMappingURL=useVoicePlayer.js.map
