import '@angular/compiler';
import { ComponentRef } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import type { Option } from '../types';
import { MultipleSearchSelect } from './multiple-search-select.component';

describe('MultipleSearchSelect', () => {
  let fixture: ComponentFixture<MultipleSearchSelect>;
  let component: MultipleSearchSelect;
  let componentRef: ComponentRef<MultipleSearchSelect>;

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

    fixture = TestBed.createComponent(MultipleSearchSelect);
    component = fixture.componentInstance;
    componentRef = fixture.componentRef;
  });

  describe('selectedOptions computed', () => {
    it('should be empty when value is []', () => {
      componentRef.setInput('value', []);
      componentRef.setInput('options', [{ value: 'a', label: 'A' }]);
      fixture.detectChanges();
      // biome-ignore lint/suspicious/noExplicitAny: access protected computed for test
      expect((component as any).selectedOptions()).toEqual([]);
    });

    it('should resolve matching options from value + options', () => {
      const options: Option[] = [
        { value: 'a', label: 'A' },
        { value: 'b', label: 'B' },
        { value: 'c', label: 'C' },
      ];
      componentRef.setInput('value', ['a', 'c']);
      componentRef.setInput('options', options);
      fixture.detectChanges();
      // biome-ignore lint/suspicious/noExplicitAny: access protected computed for test
      expect((component as any).selectedOptions()).toEqual([options[0], options[2]]);
    });

    it('should ignore ids without a matching option', () => {
      componentRef.setInput('value', ['a', 'missing']);
      componentRef.setInput('options', [{ value: 'a', label: 'A' }]);
      fixture.detectChanges();
      // biome-ignore lint/suspicious/noExplicitAny: access protected computed for test
      expect((component as any).selectedOptions()).toEqual([{ value: 'a', label: 'A' }]);
    });
  });

  describe('valueChange output', () => {
    it('should emit ids array when options are selected', () => {
      const emitted: string[][] = [];
      component.valueChange.subscribe((v) => emitted.push(v));

      // biome-ignore lint/suspicious/noExplicitAny: invoke protected handler
      (component as any).onValueChange([
        { value: 'x', label: 'X' },
        { value: 'y', label: 'Y' },
      ]);

      expect(emitted).toEqual([['x', 'y']]);
    });

    it('should emit empty array when nothing is selected', () => {
      const emitted: string[][] = [];
      component.valueChange.subscribe((v) => emitted.push(v));

      // biome-ignore lint/suspicious/noExplicitAny: invoke protected handler
      (component as any).onValueChange([]);

      expect(emitted).toEqual([[]]);
    });
  });

  describe('touched output', () => {
    it('should fire when emitted', () => {
      let emittedCount = 0;
      component.touched.subscribe(() => emittedCount++);
      component.touched.emit();
      expect(emittedCount).toBe(1);
    });
  });
});
