"use strict";

exports.__esModule = true;
exports.default = exports.ContextMusicFolderChooser = exports.MusicFolderChooser = void 0;

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

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

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

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

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

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

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

var _SubsonicContext = require("subfirelib/hooks/contexts/SubsonicContext");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const MusicFolderChooser = props => {
  // props:
  // musicFolder (id)
  // musicFolders
  // musicFolderChanged (optional)
  const {
    caption,
    native,
    musicFolderId,
    musicFolderChanged
  } = props;
  let {
    musicFolders
  } = props;
  if (musicFolders.length === 0) musicFolders = [{
    id: "-1",
    name: "still loading..."
  }];

  const handleChange = evt => {
    musicFolderChanged(evt.target.value);
  };

  const folderItems = musicFolders.map(m => {
    if (native) return /*#__PURE__*/_react.default.createElement("option", {
      key: m.id,
      value: m.id
    }, m.name);
    return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
      key: m.id,
      value: m.id
    }, m.name);
  });
  return /*#__PURE__*/_react.default.createElement(_FormControl.default, {
    fullWidth: true
  }, /*#__PURE__*/_react.default.createElement(_InputLabel.default, {
    htmlFor: "folder-query-select"
  }, caption), /*#__PURE__*/_react.default.createElement(_Select.default, {
    native: native,
    value: musicFolderId,
    onChange: handleChange,
    input: /*#__PURE__*/_react.default.createElement(_Input.default, {
      name: "folder-query",
      id: "folder-query-select"
    })
  }, folderItems));
};

exports.MusicFolderChooser = MusicFolderChooser;
MusicFolderChooser.propTypes = {
  caption: _propTypes.default.string,
  native: _propTypes.default.bool,
  musicFolderId: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  musicFolders: _propTypes.default.arrayOf(_propTypes.default.object),
  musicFolderChanged: _propTypes.default.func
};
MusicFolderChooser.defaultProps = {
  caption: 'Music Folder',
  native: false,
  musicFolderId: -1,
  musicFolders: [],
  musicFolderChanged: () => {
    console.warn('no callback');
  }
};

const ContextMusicFolderChooser = props => {
  const {
    musicFolderId,
    setMusicFolderId,
    cache
  } = (0, _SubsonicContext.useSubsonic)();
  const {
    MusicFolders
  } = cache;
  return /*#__PURE__*/_react.default.createElement(MusicFolderChooser, {
    caption: props.caption,
    native: props.native,
    musicFolderId: musicFolderId,
    musicFolders: MusicFolders,
    musicFolderChanged: setMusicFolderId
  });
};

exports.ContextMusicFolderChooser = ContextMusicFolderChooser;
ContextMusicFolderChooser.propTypes = {
  native: _propTypes.default.bool,
  caption: _propTypes.default.string
};
var _default = ContextMusicFolderChooser;
exports.default = _default;