"use strict";

exports.__esModule = true;
exports.default = exports.useQueue = exports.useQueueImpl = void 0;

var _react = require("react");

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

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

// import { useLocalStorage } from 'react-use';
// import silenceMp3 from 'subfirelib/static/silence2sec.mp3';
const lastPlayingStorage = localStorage.getItem('subsonic.lastPlaying');
const initLastPlaying = lastPlayingStorage ? JSON.parse(lastPlayingStorage) : {
  queue: [],
  idx: 0,
  queueName: 'Last Time...',
  queueRule: '' // holds the equiv of the bookmark syntax

}; // export const emptytrack = {
//   album: '',
//   albumId: '-1',
//   artist: '',
//   year: null,
//   coverArt: '-1',
//   albumYear: 'restarting the play queue',
//   contentType: 'audio/mp3',
//   duration: 2,
//   title: 'restarting...',
//   track: 1,
//   localPath: silenceMp3
// };

const emptyObject = {};

const useQueueImpl = () => {
  // const [queue, setInternalQueue] = useLocalStorage('subsonic.lastPlaying', initLastPlaying);
  const [queue, setInternalQueue] = (0, _react.useState)(initLastPlaying); // const [restartingTitle, setRestartingTitle] = useState('silence...');

  queue.queueName = queue.queueName || 'Last Time...';
  queue.queue = queue.queue || []; // emptytrack.title = restartingTitle;

  (0, _react.useEffect)(() => {
    localStorage.setItem('subsonic.lastPlaying', JSON.stringify(queue));
  }, [queue]);

  const nextSong = () => {
    setInternalQueue(queue => {
      let newIdx = queue.idx + 1;
      if (newIdx >= queue.queue.length) newIdx = 0;
      return { ...queue,
        idx: newIdx
      };
    });
    return queue.length >= 1;
  };

  const prevSong = () => {
    setInternalQueue(queue => {
      let newIdx = queue.idx - 1;
      if (newIdx < 0) newIdx = queue.queue.length - 1;
      return { ...queue,
        idx: newIdx
      };
    });
  };

  const skipAlbum = () => {
    setInternalQueue(queue => {
      const {
        queue: q,
        idx
      } = queue;
      let newIdx = queue.idx;
      const a = q[idx].albumId;

      do {
        newIdx++;
      } while (q[newIdx] && q[newIdx].albumId === a && newIdx < q.length);

      if (!q[newIdx] || newIdx > q.length) newIdx = 0;
      return { ...queue,
        idx: newIdx
      };
    });
  };

  const shuffleSongs = () => {
    setInternalQueue(queue => {
      let songlist = [...queue.queue];
      const {
        idx,
        queueName,
        queueRule
      } = queue;
      songlist = (0, _utils.arrayShuffle)(songlist);
      songlist = (0, _utils.arrayShuffle)(songlist);
      songlist = (0, _utils.arrayShuffle)(songlist);
      songlist = (0, _utils.arrayShuffle)(songlist);
      songlist = (0, _utils.arrayShuffle)(songlist);
      let newIdx = songlist.indexOf(queue.queue[idx]);
      return {
        queue: songlist,
        idx: newIdx,
        queueName,
        queueRule
      };
    });
  };

  const setCurrentQueue = (songlist, idx, queueName, queueRule, initialStartTime) => {
    console.log('setCurrentQueue', {
      songlist,
      idx,
      queueName,
      queueRule,
      initialStartTime
    });
    const newQueue = {
      queue: songlist,
      idx,
      queueName,
      queueRule,
      initialStartTime
    };
    setInternalQueue(newQueue);
  };

  const pushToTop = (song, play) => {
    setInternalQueue(queue => {
      const {
        queue: songlist,
        idx,
        queueName,
        queueRule
      } = queue;
      songlist.unshift(song);
      let newIdx = idx;

      if (play) {
        newIdx = 0;
      } else {
        newIdx = newIdx + 1;
      }

      return {
        queue: songlist,
        idx: newIdx,
        queueName,
        queueRule
      };
    });
  };

  const queueSong = (song, play) => {
    setInternalQueue(queue => {
      const {
        queue: songlist,
        idx,
        queueName,
        queueRule
      } = queue;
      songlist.push(song);
      let newIdx = idx;

      if (play) {
        newIdx = songlist.length - 1;
      }

      return {
        queue: songlist,
        idx: newIdx,
        queueName,
        queueRule
      };
    });
  };

  const justPlaySong = song => {
    setInternalQueue(queue => {
      return {
        queue: [song],
        idx: 0
      };
    });
  };

  const setIdx = newIdx => {
    setInternalQueue(queue => {
      return { ...queue,
        idx: newIdx
      };
    });
  };

  const clearSeekTo = () => {
    setInternalQueue(queue => {
      return { ...queue,
        initialStartTime: undefined
      };
    });
  };

  const current = queue.length === 0 ? emptyObject : queue.queue[queue.idx];
  return {
    queue,
    current,
    setCurrentQueue,
    nextSong,
    skipAlbum,
    prevSong,
    shuffleSongs,
    justPlaySong,
    queueSong,
    clearSeekTo,
    pushToTop,
    setInternalQueue,
    setIdx
  };
};

exports.useQueueImpl = useQueueImpl;
const useQueue = (0, _reactSingletonHook.singletonHook)({}, useQueueImpl);
exports.useQueue = useQueue;
var _default = useQueue;
exports.default = _default;