"use strict";

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

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactRouterDom = require("react-router-dom");

var _TG = require("subfirelib/ui/TG");

var _hooks = require("subfirelib/hooks");

var _LoadingCard = _interopRequireDefault(require("subfirelib/loader/LoadingCard"));

var _ArtistActionButtons = _interopRequireDefault(require("subfirelib/browsers/ArtistActionButtons"));

var _SongList = _interopRequireDefault(require("subfirelib/browsers/songs/SongList"));

var _ScrollToTopFab = _interopRequireDefault(require("subfirelib/ui/ScrollToTopFab"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const AlbumSongs = p => {
  const {
    id,
    showAlbumInfo,
    showAlbumActions,
    onSongClick,
    showBackButton,
    scrollSelector
  } = p;
  const {
    cache,
    subsonic
  } = (0, _hooks.useSubsonic)();
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const [album, setAlbum] = (0, _react.useState)(cache.ArtistsById[id]);
  const [artist, setArtist] = (0, _react.useState)(cache.ArtistsById[id]);
  const [albumInfo, setAlbumInfo] = (0, _react.useState)(cache.ArtistInfoById[id]);
  const songs = (album == null ? void 0 : album.song) || [];
  const name = (album == null ? void 0 : album.name) || '';
  const artistId = album == null ? void 0 : album.artistId;
  const artistName = (artist == null ? void 0 : artist.name) || '';
  const routerHistory = (0, _reactRouterDom.useHistory)();
  (0, _react.useEffect)(() => {
    if (loading) return;

    if (!songs || songs.length === 0) {
      // load the cached partial version
      setLoading(cache.Albums[id] || {
        name: '...album...',
        id: id,
        coverArt: 'al-' + id
      });
      subsonic.getAlbum(id).then(a => {
        setLoading(null);
        setAlbum(a);
      });

      if (showAlbumInfo) {
        subsonic.getAlbumInfo(id).then(ai => {
          setAlbumInfo(ai);
        }).catch(() => {
          setAlbumInfo({
            notes: 'no album info'
          });
        });
      }
    }
  }, [id, artistId, showAlbumInfo, songs]); // eslint-disable-line

  (0, _react.useEffect)(() => {
    if (!artistId) return;
    subsonic.getArtist(artistId).then(a => {
      setArtist(a);
    });
  }, [artistId]); // eslint-disable-line

  if (loading) {
    return /*#__PURE__*/_react.default.createElement(_LoadingCard.default, {
      object: loading,
      top: 0
    });
  }

  if (!songs || songs.length === 0) {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
  }

  const history = routerHistory || {
    goBack: () => {
      console.log('back');
    },
    push: x => {
      console.log('push', x);
    }
  };
  const songItemProperties = {
    showCover: true,
    onClick: onSongClick
  };
  const songListProperties = {
    songItemProperties,
    coverSize: 48,
    getCoverArtURL: subsonic.getCoverArtURL
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    style: {
      paddingBottom: 75
    }
  }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_TG.Th3, {
    component: "span"
  }, `${name} - `), /*#__PURE__*/_react.default.createElement(_TG.Th4, {
    component: "span"
  }, artistName)), showAlbumInfo && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(_TG.Tb2, null, (albumInfo == null ? void 0 : albumInfo.notes) || '')), showAlbumActions && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(_ArtistActionButtons.default, {
    id: id,
    artistId: artistId,
    history: history,
    loadbase: "artistAction",
    showBackButton: showBackButton
  })), /*#__PURE__*/_react.default.createElement(_SongList.default, _extends({}, songListProperties, {
    songs: songs
  })), /*#__PURE__*/_react.default.createElement(_ScrollToTopFab.default, {
    selector: scrollSelector
  }));
};

exports.AlbumSongs = AlbumSongs;
AlbumSongs.propTypes = {
  id: _propTypes.default.string.isRequired,
  showAlbumInfo: _propTypes.default.bool,
  showAlbumActions: _propTypes.default.bool,
  onSongClick: _propTypes.default.func,
  scrollSelector: _propTypes.default.string
};

const RouterAlbumSongs = p => {
  const {
    id
  } = (0, _reactRouterDom.useParams)();
  return /*#__PURE__*/_react.default.createElement(AlbumSongs, _extends({
    id: id
  }, p));
};

RouterAlbumSongs.propTypes = {
  showAlbumInfo: _propTypes.default.bool,
  showAlbumActions: _propTypes.default.bool,
  onSongClick: _propTypes.default.func
};
var _default = RouterAlbumSongs;
exports.default = _default;