"use strict";

exports.__esModule = true;
exports.default = exports.MusicDirectory = 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 _List = _interopRequireDefault(require("@material-ui/core/List"));

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

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"));

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 MusicDirectory = props => {
  const {
    id,
    list,
    grid,
    showDirectoryActions,
    onDirectoryClick,
    onDirectorySecondaryClick,
    onSongClick,
    showBackButton,
    scrollSelector
  } = props;
  const {
    cache,
    subsonic
  } = (0, _hooks.useSubsonic)();
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const [toggleList, setToggleList] = (0, _react.useState)(list || false);
  const [directory, setDirectory] = (0, _react.useState)(cache.MusicPaths[id]);
  const name = (directory == null ? void 0 : directory.name) || (directory == null ? void 0 : directory.title) || '...unknown...';
  let contents = (directory == null ? void 0 : directory.child) || [];
  const routerHistory = (0, _reactRouterDom.useHistory)();
  const MusicPaths = cache.MusicPaths;
  (0, _react.useEffect)(() => {
    if (id !== (directory == null ? void 0 : directory.id)) {
      console.log('changing directory to', id);
      setDirectory(MusicPaths[id] || {
        name: name,
        id: id,
        coverArt: id
      });
    }
  }, [id, name, directory, MusicPaths]);
  (0, _react.useEffect)(() => {
    if (!loading && id === (directory == null ? void 0 : directory.id) && (!contents || contents.length === 0)) {
      // load the cached partial version
      console.log(id, cache.MusicPaths[id]);
      setLoading(cache.MusicPaths[id] || {
        name: name,
        id: id,
        coverArt: id
      });
      subsonic.getMusicDirectory(id).then(a => {
        setDirectory(a);
      }).catch(e => {
        console.error(e);
      }).finally(() => {
        setLoading(() => null);
      });
    }

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

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

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

  const sortedContentsInit = {
    folders: [],
    albums: [],
    songs: []
  };
  const sortedContents = contents.reduce((sortedContents, v, i, array) => {
    if (v.isDir && v.coverArt) sortedContents.albums.push(v);else if (v.isDir) sortedContents.folders.push(v);else sortedContents.songs.push(v);
    return sortedContents;
  }, sortedContentsInit);
  const {
    folders,
    albums,
    songs
  } = sortedContents;
  const songItemProperties = {
    showCover: true,
    onClick: onSongClick
  };
  const songListProperties = {
    songItemProperties,
    coverSize: 48,
    getCoverArtURL: subsonic.getCoverArtURL
  };
  const folderContent = folders != null && folders.length ? /*#__PURE__*/_react.default.createElement(_List.default, {
    component: "div",
    disablePadding: true
  }, folders.map((item, index) => /*#__PURE__*/_react.default.createElement(_EntryListItem.default, {
    key: item.id,
    useAvatar: true,
    item: item,
    index: {
      name: 'x'
    },
    subsonic: subsonic,
    onEntryClick: onDirectoryClick,
    onEntrySecondaryClick: onDirectorySecondaryClick,
    SecondaryIcon: _icons.PlayArrow,
    secondaryActionLabel: 'Play All'
  }))) : null;
  const showGrid = grid || !toggleList;
  const albumContent = albums != null && albums.length ? showGrid ? /*#__PURE__*/_react.default.createElement(_SubsonicGridList.default, {
    getSubTitle: () => {},
    Icon: _icons.PlayArrow,
    content: albums,
    onClick: onDirectorySecondaryClick,
    onImageClick: onDirectoryClick
  }) : /*#__PURE__*/_react.default.createElement(_List.default, {
    component: "div",
    disablePadding: true
  }, albums.map((item, index) => /*#__PURE__*/_react.default.createElement(_EntryListItem.default, {
    key: item.id,
    useAvatar: true,
    item: item,
    index: directory,
    subsonic: subsonic,
    onEntryClick: onDirectoryClick,
    onEntrySecondaryClick: onDirectorySecondaryClick,
    SecondaryIcon: _icons.PlayArrow,
    secondaryActionLabel: 'Play All'
  }))) : null;
  const songContent = songs != null && songs.length ? /*#__PURE__*/_react.default.createElement(_SongList.default, _extends({}, songListProperties, {
    songs: songs
  })) : null;
  const showGridListSelector = !(list || grid);

  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 history = routerHistory || {
    goBack: () => {
      console.log('back');
    },
    push: x => {
      console.log('push', x);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_TG.Th3, null, name), showDirectoryActions && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(_ArtistActionButtons.default, {
    id: id,
    loadbase: "directoryAction",
    showBackButton: showBackButton,
    history: history
  }, gridSelector)), !showDirectoryActions && gridSelector, /*#__PURE__*/_react.default.createElement("div", {
    style: {
      paddingBottom: 75
    }
  }, folderContent, albumContent, songContent, /*#__PURE__*/_react.default.createElement(_ScrollToTopFab.default, {
    selector: scrollSelector
  })));
};

exports.MusicDirectory = MusicDirectory;
MusicDirectory.propTypes = {
  id: _propTypes.default.string.isRequired,
  list: _propTypes.default.bool,
  grid: _propTypes.default.bool,
  showDirectoryActions: _propTypes.default.bool,
  onDirectoryClick: _propTypes.default.func,
  onDirectorySecondaryClick: _propTypes.default.func,
  onSongClick: _propTypes.default.func,
  scrollSelector: _propTypes.default.string,
  showBackButton: _propTypes.default.bool
};

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

RouterMusicDirectory.propTypes = {
  list: _propTypes.default.bool,
  grid: _propTypes.default.bool,
  showArtistActions: _propTypes.default.bool,
  onDirectoryClick: _propTypes.default.func,
  onDirectorySecondaryClick: _propTypes.default.func
};
var _default = RouterMusicDirectory;
exports.default = _default;