function createConversationActionHydrator({ load, activate }) {
  let actions = null;
  let pending = null;
  const wired = new WeakSet();

  async function ensure() {
    if (actions) return actions;
    if (!pending) pending = load().then(() => actions = activate()).catch(error => {
      pending = null;
      throw error;
    });
    return pending;
  }

  function show({ root, state, paint, wire, retry }) {
    if (actions) {
      retry.hidden = true;
      if (!wired.has(root)) {
        wire(actions);
        wired.add(root);
      }
      paint(state, actions);
      return Promise.resolve(true);
    }

    paint(state, null);
    retry.hidden = true;
    return ensure().then(controller => {
      if (!wired.has(root)) {
        wire(controller);
        wired.add(root);
      }
      paint(state, controller);
      return true;
    }).catch(() => {
      retry.hidden = false;
      return false;
    });
  }

  return {show,get:ensure};
}

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