"use strict";

exports.__esModule = true;
exports.default = exports.useBookmarks = void 0;

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

var _notistack = require("notistack");

var _icons = require("@material-ui/icons");

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

const useBookmarks = ({
  subsonic,
  isLoggedIn,
  loading,
  setLoading,
  onePerRule
}) => {
  const [bookmarks, setBookmarks] = (0, _react.useState)(null);
  const [replaceType, setReplaceType] = (0, _react.useState)(onePerRule);
  const {
    enqueueSnackbar
  } = (0, _notistack.useSnackbar)();
  (0, _react.useEffect)(() => {
    if (!isLoggedIn) {
      if ((bookmarks == null ? void 0 : bookmarks.length) !== undefined) {
        setBookmarks(() => null);
      }

      return;
    }

    if ((bookmarks == null ? void 0 : bookmarks.length) !== undefined) return;
    if (loading) return;
    setLoading(loading => ({
      name: 'Bookmarks',
      id: -1,
      coverArt: -1
    }));
    subsonic.getBookmarks(true).then(res => {
      setBookmarks(p => {
        var _res$bookmarks;

        return ((_res$bookmarks = res.bookmarks) == null ? void 0 : _res$bookmarks.bookmark) || [];
      });
    }).catch(err => {
      console.error(err);
      setBookmarks(p => []);
    }).finally(() => {
      setLoading(l => null);
    });
  }, [isLoggedIn, bookmarks, loading] // eslint-disable-line
  );

  const deleteBookmark = bookmark => {
    var _bookmark$entry;

    const id = (bookmark == null ? void 0 : bookmark.id) || (bookmark == null ? void 0 : (_bookmark$entry = bookmark.entry) == null ? void 0 : _bookmark$entry.id);
    if (!id) return;
    subsonic.deleteBookmark(id).then(() => {
      setBookmarks(b => undefined);
    });
  };

  const createBookmark = (id, position, comment) => {
    // console.log(id, position, comment);
    subsonic.createBookmark(id, position, comment).then(() => {
      setBookmarks(b => undefined);
    });
  };

  const queueRuleToComment = queueRule => {
    return JSON.stringify({ ...queueRule,
      bookmarkSource: 'SubFire'
    } || '');
  };

  const deleteBookmarkByQueueRule = qr => {
    for (const b of bookmarks || []) {
      try {
        const bq = JSON.parse(b.comment);

        if (!bq.bookmarkSource) {
          continue;
        }

        if ((bq == null ? void 0 : bq.type) === qr.type && (bq == null ? void 0 : bq.id) === qr.id && (bq == null ? void 0 : bq.mode) === qr.mode) {
          deleteBookmark(b);
          break;
        }
      } catch (e) {// console.log('not a subfire', e);
      }
    }
  };

  const createSubfireBookmark = (id, position, queueRule) => {
    if (replaceType) {
      deleteBookmarkByQueueRule(queueRule);
    }

    createBookmark(id, position, queueRuleToComment(queueRule));
  };

  const bookmarkForId = id => (bookmarks || []).filter(b => b.entry.id + '' === id + '')[0];

  const bookmarkIcon = id => bookmarkForId(id) ? /*#__PURE__*/_react.default.createElement(_icons.Bookmark, null) : /*#__PURE__*/_react.default.createElement(_icons.BookmarkBorder, null);

  const reloadBookmarks = () => {
    setBookmarks(b => undefined);
  };

  const savePlayQueue = (queue, id, position) => {
    const ids = queue.queue.map(s => s.id);
    subsonic.savePlayQueue(ids, id, position).then(res => {
      enqueueSnackbar('Play Queue Saved', {
        variant: 'success'
      });
    });
  };

  return {
    bookmarks,
    deleteBookmark,
    createBookmark,
    reloadBookmarks,
    createSubfireBookmark,
    bookmarkForId,
    bookmarkIcon,
    replaceType,
    setReplaceType,
    savePlayQueue
  };
};

exports.useBookmarks = useBookmarks;
var _default = useBookmarks;
exports.default = _default;