import { ToOptionsPipe } from './to-options.pipe';

describe('ToOptionsPipe', () => {
  let pipe: ToOptionsPipe;

  beforeEach(() => {
    pipe = new ToOptionsPipe();
  });

  it('emits {value, label} for identifier-shape template', () => {
    expect(pipe.transform([{ id: 'a', name: 'Foo' }], 'name')).toEqual([
      { value: 'a', label: 'Foo' },
    ]);
  });

  it('emits {value, label} for curly-wrapped identifier template', () => {
    expect(pipe.transform([{ id: 'a', name: 'Foo' }], '{name}')).toEqual([
      { value: 'a', label: 'Foo' },
    ]);
  });

  it('interpolates multi-placeholder template and preserves separators', () => {
    expect(pipe.transform([{ id: 'a', code: 'AB', name: 'Foo' }], '{code} - {name}')).toEqual([
      { value: 'a', label: 'AB - Foo' },
    ]);
  });

  it('defaults to {name} when no template is provided', () => {
    expect(pipe.transform([{ id: 'a', name: 'Foo' }])).toEqual([{ value: 'a', label: 'Foo' }]);
  });

  it('skips items without an id', () => {
    expect(
      pipe.transform(
        [
          { id: null, name: 'X' },
          { id: 'a', name: 'Y' },
        ],
        'name',
      ),
    ).toEqual([{ value: 'a', label: 'Y' }]);
  });

  it('skips items with undefined id', () => {
    expect(pipe.transform([{ name: 'X' }, { id: 'b', name: 'Y' }], 'name')).toEqual([
      { value: 'b', label: 'Y' },
    ]);
  });

  it('skips null items', () => {
    expect(pipe.transform([null, { id: 'a', name: 'Y' }], 'name')).toEqual([
      { value: 'a', label: 'Y' },
    ]);
  });

  it('returns an empty array when items is null', () => {
    expect(pipe.transform(null)).toEqual([]);
  });

  it('returns an empty array when items is undefined', () => {
    expect(pipe.transform(undefined)).toEqual([]);
  });

  it('returns an empty array when items is empty', () => {
    expect(pipe.transform([])).toEqual([]);
  });

  it('coerces null property values to empty string', () => {
    expect(pipe.transform([{ id: 'a', code: null, name: 'Foo' }], '{code} - {name}')).toEqual([
      { value: 'a', label: ' - Foo' },
    ]);
  });

  it('coerces undefined property values to empty string', () => {
    expect(
      pipe.transform(
        [{ id: 'a', name: 'Foo' } as { id: string; code?: string; name: string }],
        '{code} - {name}',
      ),
    ).toEqual([{ value: 'a', label: ' - Foo' }]);
  });
});
