text: import { logBookmarks } from '../helpers/Logger';
  import store from 'store';
  
  const BookmarkStore = { key: 'nutab_bookmarks' };
  
  BookmarkStore.importBookmarks = (nodes) => {
    let cache = [];
    const __processBookmarks = (nodes) => {
      for (let i = 0; i < nodes.length; i++) {
        let bookmark = nodes[i];
        if (bookmark.url) {
          const searchString = bookmark.title + ' ' + bookmark.url;
          cache.push({
            search: searchString,
            title: bookmark.title,
            url: bookmark.url,
          });
        }
  
        if (bookmark.children) {
          __processBookmarks(bookmark.children);
        }
      }
    };
  
    __processBookmarks(nodes);
    logBookmarks('load bookmarks', cache);
    store.set(BookmarkStore.key, cache);
  };
  
  BookmarkStore.filterBookmarks = (searchInput, stateCallback) => {
    const bookmarks = store.get(BookmarkStore.key, []);
    const searchSegments = searchInput.trimRight().split(' ');
  
    let filtered = [...bookmarks];
    searchSegments.forEach((segment) => {
      filtered = filtered.filter((bookmark) => bookmark.search.toLowerCase().includes(segment));
    });
  
    stateCallback(filtered);
  };
  
  BookmarkStore.removeBookmarks = () => {
    chrome.storage.local.remove(BookmarkStore.key);
  };
  
  export default BookmarkStore;
  