"use strict";

exports.__esModule = true;
exports.default = ZZPlaylistPurgeDialog;

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

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

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Grid = _interopRequireDefault(require("@material-ui/core/Grid"));

var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));

var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));

var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));

var _IconButton = _interopRequireDefault(require("@material-ui/core/IconButton"));

var _Close = _interopRequireDefault(require("@material-ui/icons/Close"));

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 _Checkbox = _interopRequireDefault(require("@material-ui/core/Checkbox"));

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 styles = theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(2)
  },
  closeButton: {
    position: 'absolute',
    right: theme.spacing(1),
    top: theme.spacing(1),
    color: theme.palette.grey[500]
  }
});

const DialogTitle = (0, _styles.withStyles)(styles)(props => {
  const {
    children,
    classes,
    onClose,
    ...other
  } = props;
  return /*#__PURE__*/_react.default.createElement(_DialogTitle.default, _extends({
    disableTypography: true,
    className: classes.root
  }, other), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "h6"
  }, children), onClose ? /*#__PURE__*/_react.default.createElement(_IconButton.default, {
    "aria-label": "close",
    className: classes.closeButton,
    onClick: onClose
  }, /*#__PURE__*/_react.default.createElement(_Close.default, null)) : null);
});
const DialogContent = (0, _styles.withStyles)(theme => ({
  root: {
    padding: theme.spacing(2)
  }
}))(_DialogContent.default);
const DialogActions = (0, _styles.withStyles)(theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(1)
  }
}))(_DialogActions.default);

function ZZPlaylistPurgeDialog(props) {
  const {
    open,
    setOpen,
    subsonic,
    isLoggedIn,
    classes = {}
  } = props; // eslint-disable-line

  const [receivers, setReceivers] = (0, _react.useState)([]);

  const reloadPlaylists = () => {
    subsonic.getPlaylists().then(subsonic.categorizePlaylists).then(({
      receivers
    }) => {
      setReceivers(receivers);
    });
  };

  const deleteSelected = () => {
    (async () => {
      for (const r of receivers) {
        if (r.TODELETE) {
          await subsonic.deletePlaylist(r.id);
        }
      }

      reloadPlaylists();
    })();
  };

  (0, _react.useEffect)(() => {
    if (!isLoggedIn) return;
    reloadPlaylists();
  }, [isLoggedIn]); // eslint-disable-line

  const handleClose = () => {
    setOpen(false);
  };

  const toggleDelete = i => {
    receivers[i].TODELETE = !receivers[i].TODELETE;
    setReceivers([...receivers]);
  };

  const selectedItems = receivers.filter(r => r.TODELETE);
  const deleteDisabled = selectedItems.length === 0;
  const allSelected = selectedItems.length === receivers.length;

  const selectAll = value => {
    const recs = [...receivers];
    recs.forEach((r, i) => {
      r.TODELETE = value;
    });
    setReceivers(recs);
  };

  return /*#__PURE__*/_react.default.createElement(_Dialog.default, {
    fullWidth: true,
    maxWidth: "md",
    onClose: handleClose,
    open: open
  }, /*#__PURE__*/_react.default.createElement(DialogTitle, {
    id: "customized-dialog-title",
    onClose: handleClose
  }, "Purge SubFire Remote Playlists"), /*#__PURE__*/_react.default.createElement(DialogContent, null, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 8
  }, /*#__PURE__*/_react.default.createElement(_List.default, {
    style: {
      height: '60vh',
      overflow: 'scroll'
    }
  }, receivers.map((r, i) => /*#__PURE__*/_react.default.createElement(_ListItem.default, {
    key: r.id
  }, /*#__PURE__*/_react.default.createElement(_ListItemIcon.default, null, /*#__PURE__*/_react.default.createElement(_Checkbox.default, {
    edge: "start",
    checked: !!r.TODELETE,
    onClick: () => toggleDelete(i),
    tabIndex: -1,
    disableRipple: true,
    inputProps: {
      'aria-labelledby': '' + r.id
    }
  })), /*#__PURE__*/_react.default.createElement(_ListItemText.default, {
    id: '' + r.id,
    primary: r.name
  }))))), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "subtitle1"
  }, "About..."), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "body1",
    component: 'p'
  }, 'The SubFire 2 apps create a playlist that is used to handle the remote control feature. ' + 'These can hang around even if the app is no longer being used on a particular box. ' + "You can use this dialog to delete these playlists that aren't being used."), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "body2",
    component: 'p',
    style: {
      paddingTop: '1em'
    }
  }, "It is safe to delete one that actually is still being used (so long as it isn't active). " + 'It will just be recreated the next time that particular instance starts up again.'), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "body2",
    component: 'p',
    style: {
      paddingTop: '1em'
    }
  }, 'This app does not support remote control at this time.')))), /*#__PURE__*/_react.default.createElement(DialogActions, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    autoFocus: true,
    onClick: () => selectAll(!allSelected),
    color: "default"
  }, allSelected ? 'Unselect All' : 'Select All'), /*#__PURE__*/_react.default.createElement(_Button.default, {
    disabled: deleteDisabled,
    autoFocus: true,
    onClick: deleteSelected,
    color: "default"
  }, "Delete"), /*#__PURE__*/_react.default.createElement(_Button.default, {
    autoFocus: true,
    onClick: handleClose,
    color: "default"
  }, "Close")));
}