"use strict";

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

require("core-js/modules/es.promise.finally.js");

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

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

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

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

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

var _Switch = _interopRequireDefault(require("@material-ui/core/Switch"));

var _icons = require("@material-ui/icons");

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

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

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

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

var _EntryListItem = _interopRequireDefault(require("subfirelib/ui/EntryListItem"));

var _SubsonicGridList = _interopRequireDefault(require("subfirelib/ui/SubsonicGridList"));

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 ArtistAlbums = props => {
  const {
    id,
    showArtistInfo,
    list,
    grid,
    showArtistActions,
    onAlbumClick,
    onAlbumPlayClick,
    showBackButton,
    scrollSelector
  } = props;
  const {
    cache,
    subsonic
  } = (0, _hooks.useSubsonic)();
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const [sortProperty, setSortProperty] = (0, _react.useState)('year'); // eslint-disable-line

  const [toggleList, setToggleList] = (0, _react.useState)(list || false);
  const [artist, setArtist] = (0, _react.useState)(cache.ArtistsById[id]);
  const [artistInfo, setArtistInfo] = (0, _react.useState)(cache.ArtistInfoById[id]);
  let albums = (artist === null || artist === void 0 ? void 0 : artist.album) || [];
  if (sortProperty) albums = albums.sort((a, b) => (0, _utils.compare)(a[sortProperty], b[sortProperty]));
  const name = (artist === null || artist === void 0 ? void 0 : artist.name) || '...unknown...';
  const routerHistory = (0, _reactRouterDom.useHistory)();
  (0, _react.useEffect)(() => {
    if (!loading && (!albums || albums.length === 0)) {
      // load the cached partial version
      console.log(id, cache.ArtistsById[id]);
      setLoading(cache.ArtistIndexes[id] || {
        name: '...artist...',
        id: id,
        coverArt: 'ar-' + id
      });
      subsonic.getArtist(id).then(a => {
        setArtist(a);
      }).catch(e => {
        console.error(e);
      }).finally(() => {
        setLoading(() => null);
      });

      if (showArtistInfo) {
        subsonic.getArtistInfo2(id).then(ai => {
          setArtistInfo(ai);
        }).catch(() => {
          setArtistInfo({
            biography: 'no artist info'
          });
        });
      }
    }

    return () => {
      setLoading(() => null);
    };
  }, [id, showArtistInfo, albums, loading]); // eslint-disable-line

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

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

  const handleChange = event => {
    setToggleList(event.target.checked);
  };

  const showGridListSelector = !(list || grid);
  const showGrid = grid || !toggleList;
  const showList = toggleList;
  const history = routerHistory || {
    goBack: () => {
      console.log('back');
    },
    push: x => {
      console.log('push', x);
    }
  };
  const gridSelector = showGridListSelector ? /*#__PURE__*/_react.default.createElement(_TG.B, {
    className: "grid-list-select",
    alignItems: "center"
  }, /*#__PURE__*/_react.default.createElement(_icons.GridOn, null), /*#__PURE__*/_react.default.createElement(_Switch.default, {
    checked: list,
    onChange: handleChange
  }), /*#__PURE__*/_react.default.createElement(_icons.ViewList, null)) : null;
  return /*#__PURE__*/_react.default.createElement("div", {
    style: {
      paddingBottom: 75
    }
  }, /*#__PURE__*/_react.default.createElement(_TG.Th3, null, name), showArtistInfo && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(_TG.Tb2, null, (artistInfo === null || artistInfo === void 0 ? void 0 : artistInfo.biography) || '')), showArtistActions && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(_ArtistActionButtons.default, {
    id: id,
    history: history,
    loadbase: "artistAction",
    showBackButton: showBackButton
  }, gridSelector)), !showArtistActions && gridSelector, showGrid && /*#__PURE__*/_react.default.createElement(_SubsonicGridList.default, {
    getSubTitle: () => {},
    Icon: _icons.PlayArrow,
    content: albums,
    onClick: onAlbumPlayClick,
    onImageClick: onAlbumClick,
    ScrollToTop: true,
    scrollSelector: scrollSelector
  }), showList && albums.map(a => /*#__PURE__*/_react.default.createElement(_EntryListItem.default, {
    key: a.id,
    item: a,
    index: artist,
    subsonic: subsonic,
    onEntryClick: onAlbumClick,
    onEntrySecondaryClick: onAlbumPlayClick,
    secondaryActionLabel: "Play",
    SecondaryIcon: _icons.PlayArrow
  })));
};

exports.ArtistAlbums = ArtistAlbums;
ArtistAlbums.propTypes = {
  id: _propTypes.default.string,
  showArtistInfo: _propTypes.default.bool,
  list: _propTypes.default.bool,
  grid: _propTypes.default.bool,
  showArtistActions: _propTypes.default.bool,
  showBackButton: _propTypes.default.bool,
  onAlbumClick: _propTypes.default.func,
  onAlbumPlayClick: _propTypes.default.func,
  scrollSelector: _propTypes.default.string
};

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

RouterArtistAlbums.propTypes = {
  showArtistInfo: _propTypes.default.bool,
  list: _propTypes.default.bool,
  grid: _propTypes.default.bool,
  showArtistActions: _propTypes.default.bool,
  onAlbumClick: _propTypes.default.func,
  onAlbumPlayClick: _propTypes.default.func
};
var _default = RouterArtistAlbums;
exports.default = _default;