"use strict";

exports.__esModule = true;
exports.default = exports.ContextArtistList = exports.StyledArtistList = exports.ArtistList = void 0;

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

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

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

var _SubsonicContext = require("subfirelib/hooks/contexts/SubsonicContext");

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

var _Collapse = _interopRequireDefault(require("@material-ui/core/Collapse"));

var _List = _interopRequireDefault(require("@material-ui/core/List"));

var _ListItem = _interopRequireDefault(require("@material-ui/core/ListItem"));

var _ListItemText = _interopRequireDefault(require("@material-ui/core/ListItemText"));

var _styles = require("@material-ui/core/styles");

var _ExpandLess = _interopRequireDefault(require("@material-ui/icons/ExpandLess"));

var _ExpandMore = _interopRequireDefault(require("@material-ui/icons/ExpandMore"));

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

var _classnames = _interopRequireDefault(require("classnames"));

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

/* todo: change this to useStyles */
const styles = theme => ({
  listRoot: {
    width: '100%',
    overflow: 'auto',
    display: 'inline-block',
    paddingBottom: 75
  },
  subheader: {
    backgroundColor: theme.palette.background.paper
  },
  li: {
    listStyle: 'none'
  },
  listSection: {
    backgroundColor: 'inherit'
  },
  ul: {
    backgroundColor: 'inherit',
    padding: 0
  },
  avatar: {
    margin: 5
  },
  bigAvatar: {
    width: 40,
    height: 40
  }
});

const ArtistList = props => {
  const {
    musicFolderId,
    ArtistIndexes,
    subsonic,
    classes,
    indexLabel,
    onArtistClick,
    onArtistSecondaryClick,
    secondaryActionLabel,
    SecondaryIcon,
    scrollSelector
  } = props;
  let artistIndexes = ArtistIndexes['' + musicFolderId];
  let [currentIndex, theSetCurrentIndex] = (0, _reactUse.useSessionStorage)('subfire.id3.currentIndex', 'A');

  if (!artistIndexes) {
    artistIndexes = [];
  }

  if (!Array.isArray(artistIndexes)) artistIndexes = [artistIndexes];

  const setCurrentIndex = i => {
    console.log('setCurrentIndex', i, typeof i);
    theSetCurrentIndex(i);
  };

  if (artistIndexes.length && !artistIndexes.find(i => i.name === currentIndex)) {
    currentIndex = artistIndexes[0].name;
  }

  const artistCount = (0, _react.useMemo)(() => {
    return artistIndexes.reduce((count, i) => count + i.artist.length, 0);
  }, [artistIndexes]);
  const showAll = artistCount < 201;
  const hideCollapse = artistCount < 21;

  const Artists = ({
    index
  }) => /*#__PURE__*/_react.default.createElement(_List.default, {
    component: "div",
    disablePadding: true
  }, index.artist.map(item => /*#__PURE__*/_react.default.createElement(_EntryListItem.default, {
    key: item.id,
    useAvatar: true,
    item: item,
    index: index,
    subsonic: subsonic,
    onEntryClick: onArtistClick,
    onEntrySecondaryClick: onArtistSecondaryClick,
    SecondaryIcon: SecondaryIcon,
    secondaryActionLabel: secondaryActionLabel
  })));

  const ArtistsWrapper = ({
    index,
    open
  }) => {
    if (hideCollapse) {
      return /*#__PURE__*/_react.default.createElement(Artists, {
        index: index
      });
    }

    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ListItem.default, {
      button: true,
      onClick: setCurrentIndex.bind(setCurrentIndex, index.name)
    }, /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
      primary: `${indexLabel}: ${index.name}`
    }), showAll ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null) : open ? /*#__PURE__*/_react.default.createElement(_ExpandLess.default, null) : /*#__PURE__*/_react.default.createElement(_ExpandMore.default, null)), /*#__PURE__*/_react.default.createElement(_Collapse.default, {
      in: open,
      timeout: "auto",
      unmountOnExit: true
    }, /*#__PURE__*/_react.default.createElement(Artists, {
      index: index
    })));
  };

  ArtistsWrapper.propTypes = {
    open: _propTypes.default.bool,
    index: _propTypes.default.object,
    artists: _propTypes.default.array
  };
  Artists.propTypes = { ...ArtistsWrapper.propTypes
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_List.default, {
    className: (0, _classnames.default)(classes.listRoot, 'artistList')
  }, artistIndexes.map(index => /*#__PURE__*/_react.default.createElement(ArtistsWrapper, {
    key: `index-${index.name}`,
    index: index,
    open: showAll || index.name === currentIndex
  }))), /*#__PURE__*/_react.default.createElement(_ScrollToTopFab.default, {
    selector: scrollSelector
  }));
};

exports.ArtistList = ArtistList;
ArtistList.propTypes = {
  index: _propTypes.default.object,
  // not really but eslint was confused
  classes: _propTypes.default.object,
  subsonic: _propTypes.default.object,
  ArtistIndexes: _propTypes.default.object,
  onArtistClick: _propTypes.default.func,
  onArtistSecondaryClick: _propTypes.default.func,
  SecondaryIcon: _propTypes.default.any,
  secondaryActionLabel: _propTypes.default.string,
  indexLabel: _propTypes.default.string,
  musicFolderId: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  scrollSelector: _propTypes.default.string
};
const StyledArtistList = (0, _styles.withStyles)(styles)(ArtistList);
exports.StyledArtistList = StyledArtistList;

const ContextArtistList = props => {
  const {
    musicFolderId,
    subsonic,
    cache
  } = (0, _SubsonicContext.useSubsonic)();
  const {
    onArtistClick,
    onArtistSecondaryClick,
    SecondaryIcon,
    secondaryActionLabel,
    indexLabel,
    scrollSelector
  } = props;
  const {
    ArtistIndexes
  } = cache;
  const theProps = {
    musicFolderId,
    ArtistIndexes,
    subsonic,
    onArtistClick,
    onArtistSecondaryClick,
    SecondaryIcon,
    secondaryActionLabel,
    indexLabel,
    scrollSelector
  };
  return /*#__PURE__*/_react.default.createElement(StyledArtistList, theProps);
};

exports.ContextArtistList = ContextArtistList;
ContextArtistList.propTypes = {
  onArtistClick: _propTypes.default.func,
  onArtistSecondaryClick: _propTypes.default.func,
  SecondaryIcon: _propTypes.default.any,
  secondaryActionLabel: _propTypes.default.string,
  indexLabel: _propTypes.default.string,
  scrollSelector: _propTypes.default.string
};
ContextArtistList.defaultProps = {
  onArtistClick: () => {},
  onArtistSecondaryClick: () => {},
  indexLabel: 'Artists'
};
var _default = ContextArtistList;
exports.default = _default;