(function (root, factory) {
  if (typeof module === 'object' && module.exports) module.exports = factory;
  else root.createMobileQueueLauncher = factory;
})(typeof self !== 'undefined' ? self : this, function createMobileQueueLauncher(options) {
  const emptyMessages = {
    attention: 'No items need attention.',
    update: 'No unread updates are ready to open.',
    later: 'No deferred work is ready to open.',
    draft: 'No drafts are ready to open.',
  };
  const continuation = [
    ['delivery', 'Recover Delivery'],
    ['attention', 'Start Attention'],
    ['today', 'Continue Today'],
    ['update', 'Resume Updates'],
    ['agenda', 'Open Agenda'],
    ['filed', 'Review Filed'],
    ['later', 'Start Later'],
    ['draft', 'Open Drafts'],
  ];

  function recommend() {
    const counts = options.getCounts ? options.getCounts() : {};
    const match = continuation.find(([name]) => Number(counts[name]) > 0);
    if (!match) return {name: 'find', count: 0, label: 'Find Work'};
    const [name, label] = match;
    const count = Math.max(0, Number(counts[name]) || 0);
    return {name, count, label: label + ' (' + count + ')'};
  }

  function open(name) {
    if (name === 'delivery' && options.openDelivery) return options.openDelivery();
    if (name === 'today') return options.openToday();
    if (name === 'agenda') return options.openAgenda();
    if (name === 'update' && options.openUpdates) return options.openUpdates();
    if (name === 'filed' && options.openFiled) return options.openFiled();
    options.selectFilter(name);
    const action = options.firstAction(name);
    if (!action) {
      options.announce(emptyMessages[name] || 'No work is ready to open.');
      return 'empty';
    }
    action.click();
    return 'opened';
  }

  function continueWork() {
    const next = recommend();
    if (next.name === 'find') {
      options.openFindWork();
      return 'find';
    }
    return open(next.name);
  }

  return { open, recommend, continueWork };
});
