import { TestBed } from '@angular/core/testing';
import { SectionConfig } from '../../shared/models/config';
import { HeroWidgetData } from '../contracts/widget-data.contract';
import { HeroWidgetComponent } from './hero-widget.component';

function makeSection(columns?: number): SectionConfig {
  return { id: 's1', type: 'hero', order: 0, layout: { columns }, widgets: [] } as unknown as SectionConfig;
}

function makeData(): HeroWidgetData {
  return {
    title: 'First slide',
    subtitle: 'sub',
    ctaLabel: 'Shop now',
    autoplay: false,
    slides: [{ title: 'Second slide' }],
  } as unknown as HeroWidgetData;
}

describe('HeroWidgetComponent panel count regression (layout.columns)', () => {
  it('shows 1 panel when layout.columns is 1 (or unset)', () => {
    const fixture = TestBed.createComponent(HeroWidgetComponent);
    fixture.componentInstance.section = makeSection(1);
    fixture.componentInstance.data = makeData();
    fixture.componentInstance.ngOnChanges({ data: {} as any });
    fixture.detectChanges();

    expect(fixture.componentInstance.panelCount).toBe(1);
    expect(fixture.componentInstance.visibleSlides().length).toBe(1);
  });

  it('shows 2 panels when layout.columns is 2 and more than one slide exists', () => {
    const fixture = TestBed.createComponent(HeroWidgetComponent);
    fixture.componentInstance.section = makeSection(2);
    fixture.componentInstance.data = makeData();
    fixture.componentInstance.ngOnChanges({ data: {} as any });
    fixture.detectChanges();

    expect(fixture.componentInstance.panelCount).toBe(2);
    expect(fixture.componentInstance.visibleSlides().length).toBe(2);
  });

  it('falls back to 1 panel when columns is 2 but there is only a single slide', () => {
    const fixture = TestBed.createComponent(HeroWidgetComponent);
    fixture.componentInstance.section = makeSection(2);
    fixture.componentInstance.data = { title: 'Only slide', autoplay: false } as unknown as HeroWidgetData;
    fixture.componentInstance.ngOnChanges({ data: {} as any });
    fixture.detectChanges();

    expect(fixture.componentInstance.panelCount).toBe(1);
  });
});
