import { inject, OnInit } from "@angular/core";
import { IconResolver, MatIconRegistry } from "@angular/material/icon";
import { DomSanitizer } from "@angular/platform-browser";
import { TranslateService } from "@ngx-translate/core";
import { ServiceComm } from "../services/service-comm.service";

@decorator
export class BaseComponent implements OnInit {
  private titleId: string = "???";
  protected serviceComm: ServiceComm;
  protected translateService: TranslateService;

  constructor(titleId: string) {
    let iconRegistry = inject(MatIconRegistry);
    let sanitizer = inject(DomSanitizer);
    const resolver: IconResolver = (name) =>
      sanitizer.bypassSecurityTrustResourceUrl(`/assets/icon/${name}.svg`);
    iconRegistry.addSvgIconResolver(resolver);

    this.serviceComm = inject(ServiceComm);
    this.translateService = inject(TranslateService);
    this.titleId = titleId;
  }

  ngOnInit() {
    this.translateService.get(this.titleId).subscribe((data) => {
      this.serviceComm.updatedComponentTitle(data);
      // this.serviceComm.componentTitle.subscribe((title) => (this.title = data));
    });
  }
}

function decorator(target: typeof BaseComponent): void | typeof BaseComponent {}
