import Fuse from 'fuse.js';

const annotateType = type => tab => Object.assign({}, tab, { type });

export default function filterResults(
  query,
  options,
  {
    showRecentlyClosed,
    recentlyClosedLimit,
  },
) {
  return function promiseTabResults(loadedTabs) {
    const tabsToSearch = loadedTabs.map(annotateType('tab'));
    return showRecentlyClosed
      ? Promise.all([
        tabsToSearch,
        getRecentlyClosed(recentlyClosedLimit),
      ]).then(([tabs, sessions]) => [...tabs, ...sessions])
      : Promise.resolve(new Fuse(tabsToSearch, options).search(query));
  };
}

function getRecentlyClosed(maxResults) {
  const tab = ({ tab: _tab }) => _tab;
  return browser.sessions.getRecentlyClosed({ maxResults })
    .then(sessionObjects =>
      sessionObjects.filter(tab).map(tab).map(annotateType('session')),
    )
    .then(x => { console.log('sessions', x); return x; });
}


import Fuse from 'fuse.js';
import { deletedTabsCache } from './caches';
import {
  TAB_TYPE,
  OTHER_WINDOW_TAB_TYPE,
  SESSION_TYPE,
} from './constants';
import {
  identity,
  isOfWindow,
  isOfType,
  annotateTypeCondtionally,
  annotateType,
  doubleFilterAndMerge,
} from './utils/array';

export default function filterResult(
  query,
  options,
  {
    showRecentlyClosed,
    recentlyClosedLimit,
    alwaysShowRecentlyClosedAtTheBottom,
  },
) {
  return function promiseSearchResults(loadedTabs) {
    const isQueryEmpty = query.length === 0;
    const isTabType = isOfType(TAB_TYPE);
    const tabFilter = showRecentlyClosed
      ? identity
      : ({ id }) => !deletedTabsCache().includes(id);
    // First filter any unwanted results
    const annotatedTabs = loadedTabs.filter(tabFilter)
      .map(
        annotateTypeCondtionally(
          isOfWindow(3),
          [TAB_TYPE, OTHER_WINDOW_TAB_TYPE],
        ),
      );
    // If we want to move the closed tabs to the botttom filter it
    const shouldMoveClosedToBottom = showRecentlyClosed
      && alwaysShowRecentlyClosedAtTheBottom;
    const arrayToSearch = showRecentlyClosed
      ? Promise.all([
        annotatedTabs,
        getRecentlyClosed(recentlyClosedLimit),
      ]).then(([tabs, sessions]) => [...tabs, ...sessions])
      : Promise.resolve(annotatedTabs);
    const doFinalOperation = shouldMoveClosedToBottom
      ? doubleFilterAndMerge(isTabType)
      : identity;
    const search = isQueryEmpty
      ? identity
      : arr => new Fuse(arr, options).search(query);
    return arrayToSearch
      .then(search)
      .then(doFinalOperation);
  };
}

function getRecentlyClosed(maxResults) {
  const tab = ({ tab: _tab }) => _tab;
  const getTabsAndAnnotate = objects =>
    objects.filter(tab).map(tab).map(annotateType(SESSION_TYPE));
  return browser.sessions.getRecentlyClosed({ maxResults })
    .then(getTabsAndAnnotate);
}

Annotate object array with the object types, and handle specified settings
Annotate object array with the object types, and handle specified settings
