import { Page, expect } from "@playwright/test";

export default async function loginByEmailAndPassword(
  page: Page,
  email: string,
  password: string,
): Promise<void> {
  await page.goto("/");

  // i18n picks the language from the JWT once the user is logged in, so
  // after a logout the welcome screen stays in whatever language was last
  // set rather than reverting to English. Match either label so this helper
  // works for both first-login and re-login flows. The /i flag matters —
  // Playwright's accessible-name match is case-insensitive for plain
  // strings but case-sensitive for regex unless flagged.
  await page
    .getByText(/I already have an account|У меня уже есть аккаунт/i)
    .click();

  await page.getByRole("textbox", { name: /E-mail/i }).fill(email);
  await page
    .getByRole("textbox", { name: /Password|Пароль/i })
    .fill(password);

  await page.getByRole("button", { name: /Sign In|Войти/i }).click();

  await expect(page.getByRole("button", { name: "Профиль" })).toBeVisible({
    timeout: 20000,
  });
}
