"use strict";

exports.__esModule = true;
exports.default = exports.PlaylistSongs = 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 _SongList = _interopRequireDefault(require("subfirelib/browsers/songs/SongList"));

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

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

var _TipButton = _interopRequireDefault(require("subfirelib/ui/TipButton"));

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

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 useStyles = (0, _styles.makeStyles)(theme => ({
  buttonRow: {
    display: 'flex',
    width: '100%',
    justifyContent: 'space-around'
  },
  buttonCell: {
    textAlign: 'center'
  },
  button: {
    margin: theme.spacing(1)
  },
  rightIcon: {
    marginLeft: theme.spacing(1)
  }
}));

const PlaylistActionButtons = props => {
  const {
    id,
    showBackButton,
    history,
    children
  } = props;
  const classes = useStyles();
  let backButton = '';

  if (showBackButton && history) {
    backButton = /*#__PURE__*/_react.default.createElement("div", {
      className: classes.buttonCell
    }, /*#__PURE__*/_react.default.createElement(_TipButton.default, {
      placement: "right-end",
      title: "Go Back",
      onClick: history.goBack
    }, /*#__PURE__*/_react.default.createElement(_icons.Reply, null)));
  }

  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.buttonRow
  }, backButton, /*#__PURE__*/_react.default.createElement("div", {
    className: classes.buttonCell
  }, /*#__PURE__*/_react.default.createElement(_TipButton.default, {
    placement: "right-end",
    title: "Play All",
    onClick: () => {
      history.push(`/loading/playlist/${id}`);
    }
  }, /*#__PURE__*/_react.default.createElement(_icons.VideoLibrary, null))), /*#__PURE__*/_react.default.createElement("div", {
    className: classes.buttonCell
  }, /*#__PURE__*/_react.default.createElement(_TipButton.default, {
    placement: "right-end",
    title: "Shuffle",
    onClick: () => {
      history.push(`/loading/playlist/${id}/shuffle`);
    }
  }, /*#__PURE__*/_react.default.createElement(_icons.Shuffle, null))), children);
};

PlaylistActionButtons.propTypes = {
  history: _propTypes.default.any.isRequired,
  id: _propTypes.default.string.isRequired,
  showBackButton: _propTypes.default.bool,
  children: _propTypes.default.any
};

const PlaylistSongs = p => {
  const {
    id,
    showActions,
    onSongClick,
    showBackButton,
    scrollSelector
  } = p;
  const {
    cache,
    subsonic
  } = (0, _hooks.useSubsonic)();
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const [playlist, setPlaylist] = (0, _react.useState)();
  const songs = (playlist == null ? void 0 : playlist.entry) || [];
  const name = (playlist == null ? void 0 : playlist.name) || '';
  const routerHistory = (0, _reactRouterDom.useHistory)();
  const history = routerHistory || {
    goBack: () => {
      console.log('back');
    },
    push: x => {
      console.log('push', x);
    }
  };
  (0, _react.useEffect)(() => {
    if (!id) return;
    if (loading) return;
    setLoading(cache.Playlists[id] || {
      name: '...playlist...',
      id: id,
      coverArt: 'pl-' + id
    });
    console.log('id', id);
    subsonic.getPlaylist(id).then(a => {
      setPlaylist(a);
      setLoading(null);
    });
  }, [id]); // 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,
    showMeta: true,
    showYear: 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)), showActions && /*#__PURE__*/_react.default.createElement(_TG.B, null, /*#__PURE__*/_react.default.createElement(PlaylistActionButtons, {
    id: id,
    history: history,
    showBackButton: showBackButton
  })), /*#__PURE__*/_react.default.createElement(_SongList.default, _extends({}, songListProperties, {
    songs: songs
  })), /*#__PURE__*/_react.default.createElement(_ScrollToTopFab.default, {
    selector: scrollSelector
  }));
};

exports.PlaylistSongs = PlaylistSongs;
PlaylistSongs.propTypes = {
  id: _propTypes.default.string.isRequired,
  showActions: _propTypes.default.bool,
  // play, shuffle, sort
  showBackButton: _propTypes.default.bool,
  onSongClick: _propTypes.default.func,
  scrollSelector: _propTypes.default.string
};

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

RouterPlaylistSongs.propTypes = {
  showActions: _propTypes.default.bool,
  // play, shuffle, sort
  onSongClick: _propTypes.default.func
};
var _default = RouterPlaylistSongs;
exports.default = _default;