"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 = void 0;

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

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

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

var _Paper = _interopRequireDefault(require("@material-ui/core/Paper"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

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

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

var _ListItemIcon = _interopRequireDefault(require("@material-ui/core/ListItemIcon"));

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

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

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

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

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

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

/* eslint react/prop-types: 0 */
const useStyles = (0, _styles.makeStyles)(theme => ({
  root: {
    width: '100%',
    // maxWidth: 360,
    backgroundColor: theme.palette.background.paper
  },
  nested: {
    paddingLeft: theme.spacing(4)
  }
}));

const PodcastItem = props => {
  const {
    classes,
    subsonic,
    handleItemClick,
    ...p
  } = props;
  return /*#__PURE__*/_react.default.createElement(_ListItem.default, {
    button: true,
    className: classes.nested,
    onClick: () => handleItemClick(p)
  }, /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
    inset: true,
    primary: p.title,
    secondary: p.description
  }));
};

const Podcast = props => {
  const {
    subsonic,
    classes,
    open,
    handleExpandClick,
    handleItemClick,
    ...podcastProps
  } = props;
  const p = podcastProps;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ListItem.default, {
    button: true,
    onClick: () => handleExpandClick(p.id)
  }, /*#__PURE__*/_react.default.createElement(_ListItemIcon.default, null, /*#__PURE__*/_react.default.createElement("img", {
    src: subsonic.getCoverArtURL(p.coverArt, 24),
    alt: ""
  })), /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
    inset: true,
    primary: p.title
  }), open === p.id ? /*#__PURE__*/_react.default.createElement(_ExpandLess.default, null) : /*#__PURE__*/_react.default.createElement(_ExpandMore.default, null)), /*#__PURE__*/_react.default.createElement(_Collapse.default, {
    in: open === p.id,
    timeout: "auto",
    unmountOnExit: true
  }, /*#__PURE__*/_react.default.createElement(_List.default, {
    component: "div",
    disablePadding: true
  }, p.episode.slice(0, 12).map(e => {
    return /*#__PURE__*/_react.default.createElement(PodcastItem, _extends({
      key: e.id
    }, e, {
      classes: classes,
      subsonic: subsonic,
      handleItemClick: handleItemClick
    }));
  }))));
};

function PodcastsBrowser(props) {
  const {
    cache,
    subsonic,
    isLoggedIn
  } = (0, _hooks.useSubsonic)();
  const [podcasts, setPodcasts] = (0, _react.useState)(cache.Podcasts);
  const [loading, setLoading] = (0, _hooks.useLoadingState)();
  const [open, setOpen] = (0, _react.useState)('-1');
  const {
    onClick
  } = props;
  const classes = useStyles();
  (0, _react.useEffect)(() => {
    if (!isLoggedIn) {
      if ((podcasts === null || podcasts === void 0 ? void 0 : podcasts.length) !== undefined) {
        setPodcasts(() => null);
        cache.Podcasts = null;
      }

      return;
    }

    if ((podcasts === null || podcasts === void 0 ? void 0 : podcasts.length) !== undefined) return;
    if (loading) return;
    setLoading({
      name: 'Podcasts',
      id: -1,
      coverArt: -1
    });
    subsonic.getPodcasts(true).then(res => {
      var _res$podcasts2;

      setPodcasts(p => {
        var _res$podcasts;

        return ((_res$podcasts = res.podcasts) === null || _res$podcasts === void 0 ? void 0 : _res$podcasts.channel) || [];
      });
      cache.Podcasts = ((_res$podcasts2 = res.podcasts) === null || _res$podcasts2 === void 0 ? void 0 : _res$podcasts2.channel) || [];
    }).catch(err => {
      console.error(err);
      setPodcasts(p => []);
      cache.Podcasts = [];
    }).finally(() => {
      setLoading(null);
    });
  }, [isLoggedIn, podcasts, loading] // eslint-disable-line
  );

  const handleItemClick = episode => {
    console.warn('playing', episode);
    onClick(episode); // SubsonicActions.playPodcast(episode);
    // this.props.history.push('/player');
  };

  const handleExpandClick = id => {
    setOpen(open => open === id ? '-1' : id);
  };

  let warning = null; // do we need this anymore?
  // if (remotes.remoteState === 'sender'){
  //   warning = (
  //     <Typography>
  //       Note: currently, podcast loading does not work correctly over remote casting. It will load locally.
  //     </Typography>
  //   );}

  if (!loading && podcasts === null) {
    return /*#__PURE__*/_react.default.createElement(_Paper.default, null, /*#__PURE__*/_react.default.createElement(_Typography.default, {
      variant: "h4"
    }, "Podcasts will start loading soon..."));
  }

  if (!loading && (podcasts === null || podcasts === void 0 ? void 0 : podcasts.length) === 0) {
    return /*#__PURE__*/_react.default.createElement(_Paper.default, null, /*#__PURE__*/_react.default.createElement(_Typography.default, {
      variant: "body2"
    }, "No Podcasts have been added to this server."));
  }

  const state = {
    subsonic,
    classes,
    open,
    handleExpandClick,
    handleItemClick
  };
  const listItems = (podcasts || []).map(p => /*#__PURE__*/_react.default.createElement(Podcast, _extends({}, p, {
    key: p.id
  }, state)));
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.root
  }, warning, loading && /*#__PURE__*/_react.default.createElement(_LoadingCard.default, {
    object: loading,
    top: 0
  }), /*#__PURE__*/_react.default.createElement(_List.default, {
    component: "nav"
  }, listItems));
}

var _default = PodcastsBrowser;
exports.default = _default;