"use strict";

require("core-js/modules/web.dom-collections.iterator");

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

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

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

var _Input = _interopRequireDefault(require("@material-ui/core/Input"));

var _InputLabel = _interopRequireDefault(require("@material-ui/core/InputLabel"));

var _MenuItem = _interopRequireDefault(require("@material-ui/core/MenuItem"));

var _FormHelperText = _interopRequireDefault(require("@material-ui/core/FormHelperText"));

var _FormControl = _interopRequireDefault(require("@material-ui/core/FormControl"));

var _Select = _interopRequireDefault(require("@material-ui/core/Select"));

var _utils = require("subfirelib/utils/utils");

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

const SongSorterProperties = {
  unsorted: 'Unsorted',
  title: 'Title',
  path: 'Path',
  album: 'Album',
  artist: 'Artist',
  year: 'Year',
  created: 'Date Added',
  duration: 'Length',
  genre: 'Genre',
  random: 'Random'
}; // todo: native support (options instead of MenuItem)

exports.SongSorterProperties = SongSorterProperties;

const SongSorterSelect = props => {
  const {
    classes = {},
    onChange,
    sortProperty = 'unsorted',
    fullWidth,
    ...rest
  } = props;

  const theOnChange = evt => {
    onChange(evt.target.value);
  };

  return /*#__PURE__*/_react.default.createElement(_FormControl.default, {
    className: classes.formControl,
    fullWidth: fullWidth
  }, /*#__PURE__*/_react.default.createElement(_InputLabel.default, {
    shrink: true,
    htmlFor: "sortby-label-placeholder"
  }, props.label || 'Sort By'), /*#__PURE__*/_react.default.createElement(_Select.default, _extends({
    value: sortProperty,
    onChange: theOnChange,
    input: /*#__PURE__*/_react.default.createElement(_Input.default, {
      name: "sortby-input",
      id: "sortby-label-placeholder"
    }),
    displayEmpty: true,
    name: "sortby",
    className: classes.selectEmpty
  }, rest), Object.entries(SongSorterProperties).map(([k, v]) => /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
    key: k,
    value: k
  }, v))), props.helperText && /*#__PURE__*/_react.default.createElement(_FormHelperText.default, null, props.helperText));
};

exports.SongSorterSelect = SongSorterSelect;
SongSorterSelect.propTypes = {
  classes: _propTypes.default.object,
  label: _propTypes.default.string,
  helperText: _propTypes.default.string,
  sortProperty: _propTypes.default.string,
  fullWidth: _propTypes.default.bool,
  onChange: _propTypes.default.func
};

const sortFunction = (prop, dir, prop2, prop3, a, b) => {
  const av = a[prop] || '';
  const bv = b[prop] || '';
  if (av < bv) return dir === 'asc' ? -1 : 1;
  if (av > bv) return dir === 'asc' ? 1 : -1;
  return prop2 ? sortFunction(prop2, dir, prop3, null, a, b) : 0;
};

const sort = (prop, dir, songList) => {
  let prop2 = null;
  let prop3 = null;

  if (prop === 'album') {
    prop2 = 'track';
  }

  if (prop === 'artist') {
    prop2 = 'album';
    prop3 = 'track';
  }

  const rv = songList.slice().sort(sortFunction.bind(sortFunction, prop, dir, prop2, prop3));
  return rv;
};

const SongSorter = props => {
  const {
    songList,
    setSongList,
    OptionsRenderer = SongSorterSelect,
    optionsRendererProps = {}
    /*, activeSort = false */

  } = props;
  const [sortProperty, setSortProperty] = (0, _react.useState)('unsorted');

  const onChange = newSortProperty => {
    let newSongList = songList;

    if (newSortProperty === 'unsorted') {// no-op
    } else if (newSortProperty === 'random') {
      newSongList = (0, _utils.arrayShuffle)(songList);
    } else {
      newSongList = sort(newSortProperty, 'asc', songList);
    }

    setSongList(newSongList.slice());
    setSortProperty(newSortProperty);
  }; // future - actively re-sort it each time the array changes, e.g., if new song is added. don't want infinite loop, so pushing for now
  // useEffect(() => {
  //   if (activeSort) {
  //     const newSongList = songList.sort((s1, s2) => (s1[sortProperty] || '').localeCompare(s2[sortProperty] || ''));
  //     setSongList(newSongList);
  //   }
  // }, [songList, activeSort]); // eslint-disable-line


  return /*#__PURE__*/_react.default.createElement(OptionsRenderer, _extends({
    onChange: onChange,
    sortProperty: sortProperty
  }, optionsRendererProps));
};

exports.SongSorter = SongSorter;
SongSorter.propTypes = {
  songList: _propTypes.default.array.isRequired,
  setSongList: _propTypes.default.func.isRequired,
  OptionsRenderer: _propTypes.default.elementType,
  optionsRendererProps: _propTypes.default.object,
  activeSort: _propTypes.default.bool
};
var _default = SongSorter;
exports.default = _default;