import { ChangeDetectionStrategy, Component, input, output, signal } from '@angular/core';

@Component({
  selector: 'app-locale-tabs',
  standalone: true,
  templateUrl: './locale-tabs.component.html',
  styleUrl: './locale-tabs.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class LocaleTabsComponent {
  readonly locales = input.required<string[]>();
  readonly defaultLocale = input<string | null>(null);
  readonly activeLocale = output<string>();

  private readonly selected = signal<string | null>(null);

  protected currentLocale(): string {
    const explicit = this.selected();
    if (explicit && this.locales().includes(explicit)) return explicit;
    return this.defaultLocale() ?? this.locales()[0] ?? '';
  }

  protected select(locale: string): void {
    this.selected.set(locale);
    this.activeLocale.emit(locale);
  }
}
