import { describe, it, beforeEach, expect } from 'vitest';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProductShelfMetaInfoComponent } from './product-shelf-meta-info.component';

describe('ProductShelfMetaInfoComponent', () => {
  let fixture: ComponentFixture<ProductShelfMetaInfoComponent>;
  let component: ProductShelfMetaInfoComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ProductShelfMetaInfoComponent],
    }).compileComponents();

    fixture = TestBed.createComponent(ProductShelfMetaInfoComponent);
    component = fixture.componentInstance;
  });

  describe('Component Setup and Initialization', () => {
    it('should create', () => {
      expect(component).toBeTruthy();
    });
  });

  describe('shelfInfos computed property', () => {
    it('should return "department / shelfLabel" when both are set', () => {
      fixture.componentRef.setInput('department', 'Reise');
      fixture.componentRef.setInput('shelfLabel', 'Europa');
      fixture.detectChanges();
      expect(component.shelfInfos()).toBe('Reise / Europa');
    });

    it('should return only department when shelfLabel is empty', () => {
      fixture.componentRef.setInput('department', 'Reise');
      fixture.componentRef.setInput('shelfLabel', '');
      fixture.detectChanges();
      expect(component.shelfInfos()).toBe('Reise');
    });

    it('should return only shelfLabel when department is empty', () => {
      fixture.componentRef.setInput('department', '');
      fixture.componentRef.setInput('shelfLabel', 'Europa');
      fixture.detectChanges();
      expect(component.shelfInfos()).toBe('Europa');
    });

    it('should return empty string when both are empty', () => {
      fixture.componentRef.setInput('department', '');
      fixture.componentRef.setInput('shelfLabel', '');
      fixture.detectChanges();
      expect(component.shelfInfos()).toBe('');
    });
  });

  describe('productGroup computed property', () => {
    it('should return "key: value" when both are set', () => {
      fixture.componentRef.setInput('productGroupKey', 'PG1');
      fixture.componentRef.setInput('productGroupValue', 'Bücher');
      fixture.detectChanges();
      expect(component.productGroup()).toBe('PG1: Bücher');
    });

    it('should return only key when value is empty', () => {
      fixture.componentRef.setInput('productGroupKey', 'PG1');
      fixture.componentRef.setInput('productGroupValue', '');
      fixture.detectChanges();
      expect(component.productGroup()).toBe('PG1');
    });

    it('should return only value when key is empty', () => {
      fixture.componentRef.setInput('productGroupKey', '');
      fixture.componentRef.setInput('productGroupValue', 'Bücher');
      fixture.detectChanges();
      expect(component.productGroup()).toBe('Bücher');
    });

    it('should return empty string when both are empty', () => {
      fixture.componentRef.setInput('productGroupKey', '');
      fixture.componentRef.setInput('productGroupValue', '');
      fixture.detectChanges();
      expect(component.productGroup()).toBe('');
    });
  });

  describe('assortmentFormatted computed property', () => {
    it('should format "Basissortiment|BPrämienartikel|n" as "B: Basissortiment, n: Prämienartikel"', () => {
      fixture.componentRef.setInput(
        'assortment',
        'Basissortiment|BPrämienartikel|n',
      );
      fixture.detectChanges();
      expect(component.assortmentFormatted()).toBe(
        'B: Basissortiment, n: Prämienartikel',
      );
    });

    it('should format "Archivartikel|C" as "C: Archivartikel"', () => {
      fixture.componentRef.setInput('assortment', 'Archivartikel|C');
      fixture.detectChanges();
      expect(component.assortmentFormatted()).toBe('C: Archivartikel');
    });

    it('should return the raw value if no delimiter is present', () => {
      fixture.componentRef.setInput('assortment', 'Basissortiment');
      fixture.detectChanges();
      expect(component.assortmentFormatted()).toBe('Basissortiment');
    });

    it('should return empty string if assortment is empty', () => {
      fixture.componentRef.setInput('assortment', '');
      fixture.detectChanges();
      expect(component.assortmentFormatted()).toBe('');
    });
  });

  describe('returnReason input', () => {
    it('should expose returnReason input value', () => {
      fixture.componentRef.setInput('returnReason', 'Defekt');
      fixture.detectChanges();
      expect(component.returnReason()).toBe('Defekt');
    });

    it('should return empty string if returnReason is not set', () => {
      fixture.componentRef.setInput('returnReason', '');
      fixture.detectChanges();
      expect(component.returnReason()).toBe('');
    });
  });
});
