(function (root, factory) {
  if (typeof module === 'object' && module.exports) module.exports = factory;
  else root.createAgendaSessionLauncher = factory;
})(typeof self !== 'undefined' ? self : this, function createAgendaSessionLauncher(options) {
  let pending = null;

  function open() {
    if (pending) return pending;
    options.selectAgenda();
    pending = Promise.resolve()
      .then(() => options.discover())
      .then(complete => {
        if (!options.isAgendaSelected()) return 'cancelled';
        if (complete === false || options.hasMore()) {
          options.announce('Agenda check paused. Retry to check older assigned deadlines.');
          return 'incomplete';
        }
        const opened = options.hasCheckpoint() ? options.resume() : options.start();
        if (!opened) options.announce('No deadlines are ready in Agenda.');
        return opened ? 'opened' : 'empty';
      })
      .catch(() => {
        if (options.isAgendaSelected()) {
          options.announce('Agenda check paused. Retry to check older assigned deadlines.');
        }
        return 'incomplete';
      })
      .finally(() => { pending = null; });
    return pending;
  }

  return {open};
});
