"use strict";

require("core-js/modules/es.array.reduce.js");

require("core-js/modules/web.dom-collections.iterator.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSubsonicPlayerImpl = useSubsonicPlayerImpl;
exports.default = exports.useSubsonicPlayer = void 0;

var _react = require("react");

var _reactUse = require("react-use");

var _useMediaSession = _interopRequireDefault(require("subfirelib/hooks/useMediaSession"));

var _reactSingletonHook = require("react-singleton-hook");

var _utils = require("subfirelib/utils/utils.js");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const init = {};

function useSubsonicPlayerImpl() {
  const [audio, state, controls, ref] = (0, _reactUse.useAudio)({
    id: 'media_element',
    src: null,
    autoPlay: true,
    controls: false
  });
  const [currentPlaying, setCurrentPlaying] = (0, _react.useState)({});
  const [{
    nextSong,
    prevSong,
    setCurrentPlayingTime
  }, setQueueControls] = (0, _react.useState)({});

  const beginingOrPrevSong = function () {
    if (state.time > 10) {
      controls.seek(0);
    } else {
      prevSong();
    }
  };

  const mediaSessionControls = {
    play: controls.play,
    pause: controls.pause,
    nexttrack: nextSong,
    previoustrack: beginingOrPrevSong,
    seekbackward: () => controls.seek(state.time - 10),
    seekforward: () => controls.seek(state.time + 30),
    seekto: details => controls.seek(details.seekTime)
  };
  (0, _useMediaSession.default)({
    element: ref === null || ref === void 0 ? void 0 : ref.current,
    mediaMetadata: {
      src: audio.src,
      ...currentPlaying
    },
    controls: mediaSessionControls
  });
  (0, _react.useEffect)(() => {
    // this should be using ref.current, but things get stale
    const audio = document.querySelector('audio');

    if (audio && currentPlaying !== null && currentPlaying !== void 0 && currentPlaying.url) {
      audio.src = currentPlaying.url;
      audio.play().then(() => {
        if (currentPlaying.startTime) {
          // audio reference was stale, fetch again.
          const audio = document.querySelector('audio');
          audio.currentTime = currentPlaying.startTime / 1000;
          currentPlaying.startTime = undefined;
        }
      }).catch(e => console.error(e));
    } else if (audio) {
      audio.src = null;
    }
  }, [currentPlaying]);
  (0, _react.useEffect)(() => {
    if (state.time && nextSong && state.paused && state.time >= state.duration) {
      if (nextSong()) return;
      controls.seek(0);
      controls.play();
    }
  }, [state, nextSong]); // eslint-disable-line

  if (setCurrentPlayingTime) {
    setCurrentPlayingTime(state.time);
  }

  const marks = (state.buffered || []).reduce((cv, b) => {
    if (b.start > state.time) cv.push({
      value: b.start
    });
    if (b.end > state.time) cv.push({
      value: b.end
    });
    return cv;
  }, []);
  return {
    audio,
    state,
    controls,
    ref,
    marks,
    toHHMMSS: _utils.toHHMMSS,
    mediaSessionControls,
    currentPlaying,
    setCurrentPlaying,
    setQueueControls
  };
}

const useSubsonicPlayer = (0, _reactSingletonHook.singletonHook)(init, useSubsonicPlayerImpl);
exports.useSubsonicPlayer = useSubsonicPlayer;
var _default = useSubsonicPlayer;
exports.default = _default;