import { TextAreaField } from '@/manager/shared/components/TextAreaField';
import { languages } from '@/shared/data/seed-languages';
import { BaseComponent } from '@/shared/base/BaseComponent';
import { getInputHostByLabel } from '@/manager/shared/utils/locator-utils';
import { LanguageKey, LanguageRecordPartial } from '@/shared/types/language-types';
import { Locator } from '@playwright/test';

export class LanguageAwareInput extends BaseComponent {
  private readonly main = this.host.getByTestId('language-aware-input');
  private readonly textAreaField = new TextAreaField(getInputHostByLabel(this.main, 'Short description'));

  async fill(values: LanguageRecordPartial): Promise<void> {
    for (const [key, value] of Object.entries(values) as [LanguageKey, string | undefined][]) {
      if (value === undefined) {
        continue;
      }

      await this.getLanguageButton(key).click();
      await this.textAreaField.fill(value);
    }
  }

  private getLanguageButton(key: LanguageKey): Locator {
    return this.main.getByRole('button', { name: languages[key].name, exact: true });
  }
}
