import type { Component, TUI } from '@earendil-works/pi-tui';

type OverlayHost = Pick<TUI, 'showOverlay' | 'hideOverlay' | 'hasOverlay' | 'setFocus'>;

/** Focus management for pi-tui overlays (openclaw pattern). */
export function createOverlayHandlers(host: OverlayHost, fallbackFocus: Component) {
  const openOverlay = (component: Component) => {
    host.showOverlay(component);
  };

  const closeOverlay = () => {
    if (host.hasOverlay()) {
      host.hideOverlay();
      return;
    }
    host.setFocus(fallbackFocus);
  };

  return { openOverlay, closeOverlay };
}
