import { InvoiceProductOneTime } from '@/manager/modules/invoice/invoice-types';
import { NumberField } from '@/manager/shared/components/NumberField';
import { SingleAutocomplete } from '@/manager/shared/components/SingleAutocomplete';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class InvoiceCreateProductList extends BaseComponent {
  readonly rows = this.host.getByTestId('product-row-body');
  readonly addButton = this.host.getByTestId('add-product-button');

  async select(items: InvoiceProductOneTime[]): Promise<void> {
    for (const [index, item] of items.entries()) {
      await this.addButton.click();

      const row = this.rows.nth(index);
      await new SingleAutocomplete(row.getByTestId('column-product')).fill(item.product.name);

      if (item.quantity !== 1) {
        await new NumberField(row.getByTestId('column-quantity')).fill(item.quantity);
      }
    }
  }
}
