import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[msfaAnchorLink]',
})
export class AnchorLinkDirective {
  @HostListener('click', ['$event'])
  onClick(event: MouseEvent): void {
    const target = event.target as HTMLElement;
    const link = target.tagName === 'a' ? target : target.closest('a');
    const href = link?.getAttribute('href');
    const element = document.getElementById(href?.trim().replace('#', ''));

    if (element && element.focus) {
      element.focus();
    }

    event.stopPropagation();
    event.preventDefault();
  }
}
