"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

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

var _componentSize = _interopRequireDefault(require("@rehooks/component-size"));

var _GridList = _interopRequireDefault(require("@material-ui/core/GridList"));

var _GridListTile = _interopRequireDefault(require("@material-ui/core/GridListTile"));

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

var _ListItemSecondaryAction = _interopRequireDefault(require("@material-ui/core/ListItemSecondaryAction"));

var _ListSubheader = _interopRequireDefault(require("@material-ui/core/ListSubheader"));

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

var _ArrowUpward = _interopRequireDefault(require("@material-ui/icons/ArrowUpward"));

var _ArrowDownward = _interopRequireDefault(require("@material-ui/icons/ArrowDownward"));

var _PlayArrow = _interopRequireDefault(require("@material-ui/icons/PlayArrow"));

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

var _CoverGridTile = _interopRequireDefault(require("subfirelib/ui/CoverGridTile"));

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

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

const scrollIntoView = i => {
  const next = 'grid-category-header' + (i * 1 + 1);
  document.getElementById(next).scrollIntoView({
    block: 'start',
    inline: 'nearest',
    behavior: 'smooth'
  });
};

const useStyles = (0, _styles.makeStyles)(theme => ({
  root: {// display: "flex",
    // flexWrap: "wrap",
    // justifyContent: "space-around",
    // overflow: "hidden",
    // paddingLeft: 10
  },
  header: {
    backgroundColor: theme.palette.background.paper
  },
  gridList: {
    backgroundColor: '#000000'
  },
  icon: {
    color: 'rgba(255, 255, 255, 0.54)'
  }
}));

const SubsonicGridList = props => {
  let ref = (0, _react.useRef)(null);
  let size = (0, _componentSize.default)(ref);
  let {
    width
  } = size;
  if (isNaN(width)) width = 640;
  const spacing = width > 640 ? 10 : 6;
  const cellSize = width > 640 ? 170 : 164;
  const cols = width / (cellSize + spacing);
  const widthAfterPadding = cols * (cellSize + spacing);
  const {
    onClick,
    onImageClick,
    Icon,
    ScrollToTop,
    getSubTitle,
    sectionHeaderLabel,
    sectionHeaderIndex,
    sectionHeaderCount,
    content,
    scrollSelector
  } = props;
  const classes = useStyles();
  const sectionHeader = !sectionHeaderLabel ? null : /*#__PURE__*/_react.default.createElement(_GridListTile.default, {
    className: classes.header,
    cols: cols,
    style: {
      height: 'auto'
    }
  }, /*#__PURE__*/_react.default.createElement(_ListSubheader.default, {
    id: 'grid-category-header' + sectionHeaderIndex,
    component: "div"
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "h4"
  }, sectionHeaderLabel), sectionHeaderIndex + 1 < sectionHeaderCount && /*#__PURE__*/_react.default.createElement(_ListItemSecondaryAction.default, {
    style: {
      width: 48,
      height: 48
    }
  }, /*#__PURE__*/_react.default.createElement(_IconButton.default, {
    "aria-label": "Next",
    onClick: scrollIntoView.bind(void 0, sectionHeaderIndex)
  }, /*#__PURE__*/_react.default.createElement(_ArrowDownward.default, null))), sectionHeaderIndex + 1 === sectionHeaderCount && /*#__PURE__*/_react.default.createElement(_ListItemSecondaryAction.default, {
    style: {
      width: 48,
      height: 48
    }
  }, /*#__PURE__*/_react.default.createElement(_IconButton.default, {
    "aria-label": "First",
    onClick: scrollIntoView.bind(void 0, -1)
  }, /*#__PURE__*/_react.default.createElement(_ArrowUpward.default, null)))));
  return /*#__PURE__*/_react.default.createElement(_GridList.default, {
    ref: ref,
    cellHeight: cellSize,
    width: widthAfterPadding,
    cols: cols,
    spacing: spacing,
    style: ScrollToTop ? {
      paddingBottom: 75
    } : {}
  }, sectionHeader, content.map(n => /*#__PURE__*/_react.default.createElement(_CoverGridTile.default, {
    onClick: onClick,
    onImageClick: onImageClick,
    key: 'gl' + n.id,
    name: n.name,
    size: cellSize,
    artist: n.artist,
    title: n.title,
    subTitle: getSubTitle(n),
    coverArt: n.coverArt,
    id: n.id,
    Icon: Icon,
    useIdforArt: false
  })), ScrollToTop && /*#__PURE__*/_react.default.createElement(_ScrollToTopFab.default, {
    selector: scrollSelector
  }));
};

SubsonicGridList.propTypes = {
  Icon: _propTypes.default.object,
  onClick: _propTypes.default.func,
  onImageClick: _propTypes.default.func,
  content: _propTypes.default.arrayOf(_propTypes.default.object),
  ScrollToTop: _propTypes.default.bool,
  getSubTitle: _propTypes.default.func,
  sectionHeaderLabel: _propTypes.default.string,
  sectionHeaderIndex: _propTypes.default.number,
  sectionHeaderCount: _propTypes.default.number,
  scrollSelector: _propTypes.default.string
};
SubsonicGridList.defaultProps = {
  Icon: _PlayArrow.default,
  ScrollToTop: false,
  getSubTitle: () => null
};
var _default = SubsonicGridList;
exports.default = _default;