/// Closed Shadow DOM host helper.
///
/// All in-page UI (capture prompt, autofill icon, candidate picker, TOFU
/// banner) mounts into a closed-mode ShadowRoot so the host page cannot
/// read or mutate the overlay via document.querySelector / DOM APIs. The
/// returned ShadowSurface provides {host, root, destroy} for callers that
/// want to populate the root, position the host, and tear everything down.

export interface ShadowSurface {
  /// The host <div> that's appended to document.body. Style/position this
  /// from the caller (position: fixed, z-index, transform, etc.).
  host: HTMLDivElement;
  /// Closed-mode ShadowRoot. Populate via textContent / appendChild —
  /// NEVER innerHTML, NEVER insertAdjacentHTML. Treat any caller-supplied
  /// string (hostname, username) as untrusted.
  root: ShadowRoot;
  /// Remove the host from the DOM and drop all references.
  destroy: () => void;
}

/// Create a closed Shadow DOM host attached to document.body.
///
/// Callers are responsible for positioning `host` and filling `root`.
export function createShadowHost(): ShadowSurface {
  const host = document.createElement('div');
  // Reset host-side styling so page CSS cannot leak in/out via inheritance.
  host.style.all = 'initial';
  const root = host.attachShadow({ mode: 'closed' });
  document.body.appendChild(host);
  return {
    host,
    root,
    destroy: () => {
      host.remove();
    },
  };
}
