import { Directive, inject, type AfterViewInit } from '@angular/core';
import Inputmask from 'inputmask';
import { Calendar } from 'primeng/calendar';

@Directive({
  selector: '[appDateMask]',
  standalone: true,
})
export class DateMaskDirective implements AfterViewInit {
  readonly #primeCalendar = inject(Calendar);

  ngAfterViewInit() {
    new Inputmask(this.getDateMask()).mask(this.getHTMLInput());
  }

  getHTMLInput(): HTMLInputElement {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return this.#primeCalendar.el.nativeElement.querySelector('input');
  }

  getDateMask(): string {
    if (this.#primeCalendar.timeOnly) {
      return '99:99';
    } else if (this.#primeCalendar.showTime) {
      return '99/99/9999 99:99';
    } else {
      return '99/99/9999';
    }
  }
}
