"use strict";

exports.__esModule = true;
exports.default = exports.useStarred = void 0;

var _react = require("react");

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

const useStarredSongs = (0, _reactUse.createGlobalState)([]);
const useStarredArtistFolders = (0, _reactUse.createGlobalState)([]);
const useStarredAlbumFolders = (0, _reactUse.createGlobalState)([]);
const useStarredArtists = (0, _reactUse.createGlobalState)([]);
const useStarredAlbums = (0, _reactUse.createGlobalState)([]); // TODO finish id3 versions
// as with bookmarks, make sure loading isn't being shared with other pages, so don't use global in thePlayer

const useStarred = ({
  subsonic,
  isLoggedIn,
  loading,
  setLoading,
  musicFolderId
}) => {
  // both are the same
  const [starredSongs, setStarredSongs] = useStarredSongs(); // starred

  const [starredArtistFolders, setStarredArtistFolders] = useStarredArtistFolders();
  const [starredAlbumFolders, setStarredAlbumFolders] = useStarredAlbumFolders(); // starred2

  const [starredArtists, setStarredArtists] = useStarredArtists();
  const [starredAlbums, setStarredAlbums] = useStarredAlbums();

  const refreshStarred = async () => {
    const [star1, star2] = await Promise.all([subsonic.getStarred(musicFolderId), subsonic.getStarred2(musicFolderId)]); // console.log(star1, star2);

    setStarredSongs(x => star1.song || []);
    setStarredArtistFolders(x => star1.artist || []);
    setStarredAlbumFolders(x => star1.album || []);
    setStarredArtists(x => star2.artist || []);
    setStarredAlbums(x => star2.album || []);
    return {
      star1,
      star2
    };
  };

  const clearAll = () => {
    setStarredSongs([]);
    setStarredArtistFolders([]);
    setStarredAlbumFolders([]);
    setStarredArtists([]);
    setStarredAlbums([]);
  };

  (0, _react.useEffect)(() => {
    // console.warn('useStarred useEffect', isLoggedIn);
    if (!isLoggedIn) {
      clearAll();
      return;
    }

    if (loading) return;
    setLoading(l => ({
      name: 'Starred',
      id: -1,
      coverArt: -1
    }));
    console.log('refresh', musicFolderId);
    refreshStarred().then(({
      star1,
      star2
    }) => {// console.log('useEffect done', star1, star2);
    }).catch(err => {
      console.error(err);
      clearAll();
    }).finally(() => {
      setLoading(l => null);
    });
  }, [isLoggedIn, musicFolderId]); // eslint-disable-line
  // backwards compat

  const starred = {
    songs: starredSongs,
    artistFolders: starredArtistFolders,
    albumFolders: starredAlbumFolders,
    albums: starredAlbums,
    artists: starredArtists
  };

  const toggleStarSync = async o => {
    if (typeof o === 'string' || typeof o === 'number') {
      o = {
        id: o
      };
    }

    if (o.starred) {
      await subsonic._execute('unstar', {
        id: o.id
      });
      await refreshStarred();
      delete o.starred; // this will change the cached item
    } else {
      await subsonic._execute('star', {
        id: o.id
      });
      o.starred = new Date().toISOString(); // may be different from the server but I don't care, here for the cache
      // if song, this may override it. that's fine.

      await refreshStarred();
    }
  };

  const toggleStar = o => {
    toggleStarSync(o).then(() => {});
  }; // be generous on what you accept


  const isMediaStarred = m => {
    if (typeof m === 'string' || typeof m === 'number') {
      m = {
        id: m
      };
    } // this list has priority over the cache


    return starred.songs.filter(s => s.id + '' === m.id + '').length || starred.albumFolders.filter(s => s.id + '' === m.id + '').length || starred.artistFolders.filter(s => s.id + '' === m.id + '').length;
  };

  return {
    starredSongs,
    starredArtistFolders,
    starredAlbumFolders,
    starredArtists,
    starredAlbums,
    refreshStarred,
    toggleStar,
    toggleStarSync,
    isMediaStarred,
    // backwards compat:
    starred,
    reloadStarredContent: refreshStarred
  };
};

exports.useStarred = useStarred;
var _default = useStarred;
exports.default = _default;