import {
  AfterViewInit,
  Directive,
  ElementRef,
  inject
} from '@angular/core';

/**
 * Focuses the host input, textarea, or select after the view initializes.
 */
@Directive({
  selector: 'input[appAutoFocus], textarea[appAutoFocus], select[appAutoFocus]',
  standalone: true
})
export class AutoFocusDirective implements AfterViewInit {
  private readonly hostRef = inject<ElementRef<HTMLElement>>(ElementRef);

  ngAfterViewInit(): void {
    requestAnimationFrame(() => {
      this.hostRef.nativeElement.focus();
    });
  }
}
