/**
 * Single-source "is the user typing in an editable surface?" helper
 * (docs/architecture/04 §2.2).
 *
 * App layer (browser APIs allowed): reads `document.activeElement`. Every global
 * shortcut path and the paste-to-import handler call this so they never fire
 * while the user is typing in an input or the Monaco editor. Do not inline these
 * element-type checks at call sites — one place to get it right, one to fix.
 *
 * Monaco difference (important): Astrolabe's spec editor is Monaco, not
 * CodeMirror. Monaco renders into a `.monaco-editor` container and keeps focus on
 * a hidden `<textarea class="inputarea">` inside it. We detect via the
 * `.monaco-editor` ancestor — never a `.cm-editor` / `.cm-content` CodeMirror
 * selector, which would silently fail and let shortcuts fire mid-edit.
 */

/**
 * True when focus is in an editable surface where global shortcuts and
 * paste-to-import must be suppressed: `<input>`, `<textarea>`, `<select>`,
 * contenteditable, or the Monaco editor.
 *
 * This is the SINGLE source of truth — do not inline these checks elsewhere.
 */
export function isInInteractiveContext(): boolean {
  const el = document.activeElement as HTMLElement | null;
  if (!el) return false;

  const tag = el.tagName.toLowerCase();
  if (tag === 'input' || tag === 'textarea' || tag === 'select') return true;
  if (el.isContentEditable) return true;

  // Monaco renders into a .monaco-editor container; its focused element is a
  // hidden <textarea class="inputarea"> (already caught above) but guard the
  // container explicitly so focus on any inner node still counts.
  if (el.closest?.('.monaco-editor')) return true;

  return false;
}
