"use strict";

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

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

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

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

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

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

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

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

var _MusicFolderChooser = _interopRequireDefault(require("subfirelib/browsers/MusicFolderChooser"));

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

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

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

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

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

var _AlbumQueryChooser = _interopRequireDefault(require("./AlbumQueryChooser"));

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; }

const AlbumsBrowser = props => {
  const {
    cache,
    subsonic,
    musicFolderId
  } = (0, _hooks.useSubsonic)();
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const {
    native,
    list,
    grid,
    onAlbumClick,
    onAlbumSecondaryClick,
    scrollSelector
  } = props;
  const [albumQuery, setAlbumQuery] = (0, _reactUse.useLocalStorage)('initAlbumQuery', 'recent');
  const [albumID3, setAlbumID3] = (0, _reactUse.useLocalStorage)('initAlbumID3', '-1');
  const [toggleList, setToggleList] = (0, _react.useState)(list || false);
  const [albums, setAlbums] = (0, _react.useState)([]);

  const onClick = id => {
    onAlbumClick(id, albumID3);
  };

  const onSecondaryClick = id => {
    onAlbumSecondaryClick(id, albumID3);
  };

  (0, _react.useEffect)(() => {
    async function x() {
      var _res, _res$albumList, _res2, _res2$albumList;

      let query = albumQuery;
      let res;
      const params = {
        size: 60
      };

      if (query.indexOf('byYear') === 0) {
        if (query === 'byYear reversed') {
          query = 'byYear';
          params.fromYear = 2100;
          params.toYear = 1500;
        } else {
          params.fromYear = 1500;
          params.toYear = 2100;
        }
      }

      if (musicFolderId !== -1) params.musicFolderId = musicFolderId;

      if (albumID3) {
        res = await subsonic.getAlbumList2(query, params);
      } else {
        res = await subsonic.getAlbumList(query, params);
      }

      setAlbums(((_res = res) === null || _res === void 0 ? void 0 : (_res$albumList = _res.albumList) === null || _res$albumList === void 0 ? void 0 : _res$albumList.album) || ((_res2 = res) === null || _res2 === void 0 ? void 0 : (_res2$albumList = _res2.albumList2) === null || _res2$albumList === void 0 ? void 0 : _res2$albumList.album) || []);
    }

    x();
  }, [albumQuery, albumID3, subsonic, musicFolderId]);

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

  const showGridListSelector = !(list || grid);
  const showGrid = grid || !toggleList;

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

  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;

  const musicFolderChooser = /*#__PURE__*/_react.default.createElement(_MusicFolderChooser.default, {
    native: native
  });

  const aqcProps = {
    list,
    grid,
    albumQuery,
    setAlbumQuery,
    albumID3,
    setAlbumID3,
    native,
    gridListToggle: gridSelector,
    musicFolderChooser
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_AlbumQueryChooser.default, aqcProps), showGrid && /*#__PURE__*/_react.default.createElement(_SubsonicGridList.default, {
    getSubTitle: () => {},
    Icon: _icons.PlayArrow,
    content: albums,
    onClick: onSecondaryClick,
    onImageClick: onClick,
    ScrollToTop: true,
    scrollSelector: scrollSelector
  }), !showGrid && albums.map(a => /*#__PURE__*/_react.default.createElement(_EntryListItem.default, {
    key: a.id,
    item: a,
    index: {
      name: 'albums'
    },
    subsonic: subsonic,
    onEntryClick: onClick,
    onEntrySecondaryClick: onSecondaryClick,
    secondaryActionLabel: "Play",
    SecondaryIcon: _icons.PlayArrow
  })));
};

exports.AlbumsBrowser = AlbumsBrowser;
AlbumsBrowser.propTypes = {
  native: _propTypes.default.bool,
  list: _propTypes.default.bool,
  grid: _propTypes.default.bool,
  SecondaryIcon: _propTypes.default.any,
  secondaryActionLabel: _propTypes.default.string,
  onAlbumClick: _propTypes.default.func,
  onAlbumSecondaryClick: _propTypes.default.func,
  scrollSelector: _propTypes.default.string
};
var _default = AlbumsBrowser;
exports.default = _default;