function createDetailDefer({
  laterWork,
  session,
  continueSession = null,
  close,
  refresh,
  focus,
  announce,
  formatTime = value => new Date(value).toLocaleString(),
}) {
  return {
    deferUntil(item, until, { closeSheet = true, restoreFocus = true } = {}) {
      if (!item) return false;
      const result = laterWork.defer(item, until);
      if (result !== 'deferred') {
        announce(result === 'invalid' ? 'Choose a valid future time.' : 'Could not save Later on this device.');
        return false;
      }
      const inSession = session.active();
      if (continueSession && session.checkpointed?.(item)) {
        if (continueSession(item) === true) return true;
        laterWork.restore(item);
        refresh();
        announce('Could not remove this item from Today, so it was restored from Later.');
        return false;
      }
      if (!inSession && closeSheet) close();
      refresh();
      announce('Deferred until ' + formatTime(until) + '. It stays unread and unchanged in Gitea.');
      if (!inSession && restoreFocus) focus();
      return true;
    },
    defer(item, preset) {
      return this.deferUntil(item, laterWork.presetUntil(preset));
    },
  };
}

if (typeof module !== 'undefined' && module.exports) module.exports = createDetailDefer;
