import React__default, { useReducer, useContext, createContext } from 'react';
import { _ as __assign } from './tslib.es6-cd680863.js';
import { f as VOICE_PLAYER_ROOT_ID, g as VOICE_PLAYER_AUDIO_ID, d as VOICE_MESSAGE_FILE_NAME, e as VOICE_MESSAGE_MIME_TYPE } from './consts-886b25ca.js';

var INITIALIZE_AUDIO_UNIT = 'INITIALIZE_AUDIO_UNIT';
var SET_CURRENT_PLAYER = 'SET_CURRENT_PLAYER';
var ON_VOICE_PLAYER_PLAY = 'ON_VOICE_PLAYER_PLAY';
var ON_VOICE_PLAYER_PAUSE = 'ON_VOICE_PLAYER_PAUSE';
var ON_CURRENT_TIME_UPDATE = 'ON_CURRENT_TIME_UPDATE';

var VoicePlayerStatus = {
  IDLE: 'IDLE',
  PREPARING: 'PREPARING',
  PLAYING: 'PLAYING',
  PAUSED: 'PAUSED',
  COMPLETED: 'COMPLETED'
};
var AudioUnitDefaultValue = function () {
  return {
    audioFile: null,
    playbackTime: 0,
    duration: 1000,
    playingStatus: VoicePlayerStatus.IDLE
  };
};
var voicePlayerInitialState = {
  currentPlayer: null,
  currentGroupKey: '',
  audioStorage: {}
};

function voicePlayerReducer(state, action) {
  var _a, _b, _c, _d;

  var _e, _f, _g, _h;

  switch (action.type) {
    case INITIALIZE_AUDIO_UNIT:
      {
        var groupKey = action.payload.groupKey;
        var audioUnit = ((_e = state.audioStorage) === null || _e === void 0 ? void 0 : _e[groupKey]) ? state.audioStorage[groupKey] : AudioUnitDefaultValue();
        audioUnit.playingStatus = VoicePlayerStatus.PREPARING;
        return __assign(__assign({}, state), {
          audioStorage: __assign(__assign({}, state.audioStorage), (_a = {}, _a[groupKey] = audioUnit, _a))
        });
      }

    case SET_CURRENT_PLAYER:
      {
        var _j = action.payload,
            audioPlayer = _j.audioPlayer,
            groupKey = _j.groupKey;
        return __assign(__assign({}, state), {
          currentPlayer: audioPlayer,
          currentGroupKey: groupKey
        });
      }

    case ON_VOICE_PLAYER_PLAY:
      {
        var _k = action.payload,
            groupKey = _k.groupKey,
            audioFile = _k.audioFile;
        var audioUnit = ((_f = state.audioStorage) === null || _f === void 0 ? void 0 : _f[groupKey]) ? state.audioStorage[groupKey] : AudioUnitDefaultValue();
        audioUnit.audioFile = audioFile;
        audioUnit.playingStatus = VoicePlayerStatus.PLAYING;
        return __assign(__assign({}, state), {
          audioStorage: __assign(__assign({}, state.audioStorage), (_b = {}, _b[groupKey] = audioUnit, _b))
        });
      }

    case ON_VOICE_PLAYER_PAUSE:
      {
        var groupKey = action.payload.groupKey;
        var audioUnit = ((_g = state.audioStorage) === null || _g === void 0 ? void 0 : _g[groupKey]) ? state.audioStorage[groupKey] : AudioUnitDefaultValue();
        audioUnit.playingStatus = VoicePlayerStatus.PAUSED;
        var _l = state.currentPlayer,
            currentTime = _l.currentTime,
            duration = _l.duration;

        if (audioUnit.playbackTime === audioUnit.duration) {
          audioUnit.playbackTime = 0;
        } else if (currentTime > 0 && duration > 0) {
          audioUnit.playbackTime = currentTime;
          audioUnit.duration = duration;
        }

        return __assign(__assign({}, state), {
          audioStorage: __assign(__assign({}, state.audioStorage), (_c = {}, _c[groupKey] = audioUnit, _c))
        });
      }

    case ON_CURRENT_TIME_UPDATE:
      {
        var groupKey = action.payload.groupKey;
        var _m = state.currentPlayer,
            currentTime = _m.currentTime,
            duration = _m.duration;
        var audioUnit = ((_h = state.audioStorage) === null || _h === void 0 ? void 0 : _h[groupKey]) ? state.audioStorage[groupKey] : AudioUnitDefaultValue();

        if (currentTime > 0 && duration > 0) {
          audioUnit.playbackTime = currentTime;
          audioUnit.duration = duration;
        }

        return __assign(__assign({}, state), {
          audioStorage: __assign(__assign({}, state.audioStorage), (_d = {}, _d[groupKey] = audioUnit, _d))
        });
      }

    default:
      return state;
  }
}

var noop = function () {};

var VoicePlayerStoreDefaultValue = {
  currentGroupKey: '',
  currentPlayer: null,
  audioStorage: {}
};
var VoicePlayerContext = /*#__PURE__*/createContext({
  play: noop,
  pause: noop,
  stop: noop,
  voicePlayerStore: VoicePlayerStoreDefaultValue
});
var VoicePlayerProvider = function (_a) {
  var children = _a.children;

  var _b = useReducer(voicePlayerReducer, voicePlayerInitialState),
      voicePlayerStore = _b[0],
      voicePlayerDispatcher = _b[1];

  var currentGroupKey = voicePlayerStore.currentGroupKey,
      currentPlayer = voicePlayerStore.currentPlayer,
      audioStorage = voicePlayerStore.audioStorage;

  var stop = function (text) {
    if (text === void 0) {
      text = '';
    }

    if (currentGroupKey.includes(text)) {
      pause(currentGroupKey);
    }
  };

  var pause = function (groupKey) {
    if (currentGroupKey === groupKey && currentPlayer !== null) {
      currentPlayer === null || currentPlayer === void 0 ? void 0 : currentPlayer.pause();
    }
  };

  var play = function (_a) {
    var groupKey = _a.groupKey,
        _b = _a.audioFile,
        audioFile = _b === void 0 ? null : _b,
        _c = _a.audioFileUrl,
        audioFileUrl = _c === void 0 ? '' : _c;

    if (groupKey !== currentGroupKey) {
      pause(currentGroupKey);
    } // Clear the previous AudioPlayer element


    var voicePlayerRoot = document.getElementById(VOICE_PLAYER_ROOT_ID);
    var voicePlayerAudioElement = document.getElementById(VOICE_PLAYER_AUDIO_ID);

    if (voicePlayerAudioElement) {
      voicePlayerRoot.removeChild(voicePlayerAudioElement);
    }

    new Promise(function (resolve) {
      var _a;

      if (audioFile) {
        resolve(audioFile);
      }

      if ((_a = audioStorage === null || audioStorage === void 0 ? void 0 : audioStorage[groupKey]) === null || _a === void 0 ? void 0 : _a.audioFile) {
        resolve(audioStorage[groupKey].audioFile);
      }

      voicePlayerDispatcher({
        type: INITIALIZE_AUDIO_UNIT,
        payload: {
          groupKey: groupKey
        }
      });
      fetch(audioFileUrl).then(function (res) {
        return res.blob();
      }).then(function (blob) {
        var audioFile = new File([blob], VOICE_MESSAGE_FILE_NAME, {
          lastModified: new Date().getTime(),
          type: VOICE_MESSAGE_MIME_TYPE
        });
        resolve(audioFile);
      });
    }).then(function (audioFile) {
      var _a;

      var currentAudioUnit = audioStorage[groupKey] || AudioUnitDefaultValue();
      var audioPlayer = new Audio((_a = URL === null || URL === void 0 ? void 0 : URL.createObjectURL) === null || _a === void 0 ? void 0 : _a.call(URL, audioFile));
      audioPlayer.id = VOICE_PLAYER_AUDIO_ID;
      audioPlayer.currentTime = currentAudioUnit.playbackTime;
      audioPlayer.volume = 1;
      audioPlayer.loop = false;

      audioPlayer.onplaying = function () {
        voicePlayerDispatcher({
          type: ON_VOICE_PLAYER_PLAY,
          payload: {
            groupKey: groupKey,
            audioFile: audioFile
          }
        });
      };

      audioPlayer.onpause = function () {
        voicePlayerDispatcher({
          type: ON_VOICE_PLAYER_PAUSE,
          payload: {
            groupKey: groupKey
          }
        });
      };

      audioPlayer.ontimeupdate = function () {
        voicePlayerDispatcher({
          type: ON_CURRENT_TIME_UPDATE,
          payload: {
            groupKey: groupKey
          }
        });
      };

      audioPlayer === null || audioPlayer === void 0 ? void 0 : audioPlayer.play();
      var voicePlayerRoot = document.getElementById(VOICE_PLAYER_ROOT_ID);
      voicePlayerRoot.appendChild(audioPlayer);
      voicePlayerDispatcher({
        type: SET_CURRENT_PLAYER,
        payload: {
          groupKey: groupKey,
          audioPlayer: audioPlayer
        }
      });
    });
  };

  return /*#__PURE__*/React__default.createElement(VoicePlayerContext.Provider, {
    value: {
      play: play,
      pause: pause,
      stop: stop,
      voicePlayerStore: voicePlayerStore
    }
  }, /*#__PURE__*/React__default.createElement("div", {
    id: VOICE_PLAYER_ROOT_ID,
    style: {
      display: 'none'
    }
  }), children);
};
var useVoicePlayerContext = function () {
  return useContext(VoicePlayerContext);
};

export { AudioUnitDefaultValue as A, VoicePlayerProvider as V, VoicePlayerStatus as a, useVoicePlayerContext as u };
//# sourceMappingURL=index-a3a4b35c.js.map
