"use strict";

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

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

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

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

var _api = require("subfirelib/api");

var _CredentialsContext = require("subfirelib/hooks/contexts/CredentialsContext");

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 SubsonicContext = (0, _react.createContext)();
exports.SubsonicContext = SubsonicContext;

const SubsonicProvider = ({
  children
}) => {
  const value = (0, _react.useContext)(_CredentialsContext.CredentialsContext);
  const [current] = value; // storing the folder

  const [musicFolderId, setMusicFolderId] = (0, _react.useState)("-1"); // establishing that the user is logged in and the actual folder's limited data is loaded.

  const [isLoggedIn, setLoggedIn] = (0, _react.useState)(false);
  const [musicFolderLoaded, setMusicFolderLoaded] = (0, _react.useState)(false);

  const tryAgain = () => {
    setLoggedIn(false);
    const s = _api.Subsonic;
    const {
      server,
      username,
      password,
      bitrate,
      clientName = 'SubsonicStorybook'
    } = current;
    s.open(server, username, password, bitrate, clientName).then(res => {// no-op, just move on.
    }).then(s.getMusicFolders).then(s.getIndexes).then(s.getArtists).then(s.getGenres).then(() => {
      // console.log('done');
      setLoggedIn(true);
    }).catch(rej => {
      console.log(rej);
    });
  };

  const loadFolderIds = () => {
    if (!isLoggedIn) return;
    setMusicFolderLoaded(false);

    _api.Subsonic.getIndexes(musicFolderId).then(_api.Subsonic.getArtists.bind(_api.Subsonic, musicFolderId)).then(() => {
      setMusicFolderLoaded(true);
    });
  };

  (0, _react.useEffect)(tryAgain, [current]);
  (0, _react.useEffect)(loadFolderIds, [isLoggedIn, musicFolderId]);
  const properties = {
    subsonic: _api.Subsonic,
    cache: _api.SubsonicCache,
    isLoggedIn,
    musicFolderLoaded,
    musicFolderId,
    tryAgain,
    setMusicFolderId
  };
  return /*#__PURE__*/_react.default.createElement(SubsonicContext.Provider, {
    value: properties
  }, children);
};

exports.SubsonicProvider = SubsonicProvider;
SubsonicProvider.propTypes = {
  children: _propTypes.default.any
};