import { Component, ChangeDetectionStrategy, Input } from '@angular/core';
import { ApplicationService } from '@core/application';
import { ProductsFeed } from '@domain/isa';
import { ProductCatalogNavigationService } from '@shared/services/navigation';
import { first } from 'rxjs/operators';

@Component({
  selector: 'page-products-card',
  templateUrl: 'product-card.component.html',
  styleUrls: ['product-card.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class ProductsCardComponent {
  @Input()
  feed: ProductsFeed;

  constructor(
    private _navigation: ProductCatalogNavigationService,
    private _app: ApplicationService,
  ) {}

  async navigatetToProduct(ean: string) {
    let processes = await this._app.getProcesses$('customer').pipe(first()).toPromise();

    processes = processes.sort((a, b) => b.activated - a.activated);

    this._navigation
      .getArticleDetailsPathByEan({
        processId: processes[0]?.id ?? Date.now(),
        ean,
        extras: { queryParams: { main_qs: this.feed.items.map((i) => i.product.ean).join(';') } },
      })
      .navigate();
  }
}
