'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var VoicePlayer_context = require('../index-50c4a83b.js');
var consts = require('../consts-900e72b9.js');
var VoiceRecorder_context = require('../VoiceRecorder/context.js');
require('../_rollupPluginBabelHelpers-21ae158b.js');
require('../useSendbirdStateContext.js');
require('../withSendbird.js');
require('../LocalizationContext-44d2f61b.js');
require('../stringSet-7aebf034.js');
require('../index-88e8fdfa.js');
require('../ui/Modal.js');
require('react-dom');
require('../index-0a446cca.js');
require('../ui/Button.js');
require('../index-2bfd366f.js');
require('prop-types');
require('../ui/Icon.js');
require('../ui/IconButton.js');
require('../MediaQueryContext-5d311a86.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] = React.useState(generateGroupKey(channelUrl, key));
  const {
    play,
    pause,
    stop,
    voicePlayerStore
  } = VoicePlayer_context.useVoicePlayerContext();
  const {
    isRecordable
  } = VoiceRecorder_context.useVoiceRecorderContext();
  const currentAudioUnit = (voicePlayerStore === null || voicePlayerStore === void 0 ? void 0 : (_voicePlayerStore$aud = voicePlayerStore.audioStorage) === null || _voicePlayerStore$aud === void 0 ? void 0 : _voicePlayerStore$aud[groupKey]) || VoicePlayer_context.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);
  };
  React.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(consts.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
  };
};

exports.useVoicePlayer = useVoicePlayer;
//# sourceMappingURL=useVoicePlayer.js.map
