import { ChangeDetectionStrategy, Component, Input, inject } from '@angular/core';
import { SectionConfig } from '../../shared/models/config';
import { RecentlyViewedStripComponent } from '../../features/website/user-experience/components/recently-viewed-strip/recently-viewed-strip.component';
import { TranslateService } from '../../i18n/translate.service';

@Component({
  selector: 'app-recently-viewed-widget',
  standalone: true,
  imports: [RecentlyViewedStripComponent],
  template: `
    <app-recently-viewed-strip
      [title]="title"
      [maxItems]="maxItems"
      [showClear]="showClear"
    />
  `,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RecentlyViewedWidgetComponent {
  private readonly translate = inject(TranslateService);

  @Input() section: SectionConfig | null = null;
  @Input() data: { settings?: Record<string, unknown> } | null = null;

  get title(): string {
    const raw = this.data?.settings?.['title'];
    return typeof raw === 'string' && raw.trim().length > 0 ? raw : this.translate.t('ux.recentlyViewedTitle');
  }

  get maxItems(): number {
    const raw = this.data?.settings?.['maxItems'];
    const value = typeof raw === 'number' ? raw : Number(raw);
    return Number.isFinite(value) ? Math.max(1, value) : 8;
  }

  get showClear(): boolean {
    return this.data?.settings?.['showClear'] === true;
  }
}
