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

export type LegacyUnitName =
  | "Граммы"
  | "Штуки"
  | "Литры"
  | "Килограммы"
  | "Миллилитры"
  | "Примерно";

export default async function setQuantity(
  page: Page,
  unit: LegacyUnitName,
  quantity?: number,
): Promise<void> {
  // Wait for EditQuantityValue component to be present
  const editQuantity = page.getByTestId("EditQuantityValue");

  await editQuantity.waitFor({
    state: "visible",
    timeout: 10000,
  });

  // Select the unit - use generic combobox selector
  const unitSelect = editQuantity.getByRole("combobox").first();
  await unitSelect.click();
  await page.getByRole("option", { name: unit, exact: true }).click();

  // For "Примерно" (rough) unit, there's no quantity input - it just shows "Есть"
  if (unit === "Примерно") {
    return;
  }

  // Set quantity - use the test-id based selector
  const quantityInput = editQuantity.getByRole("textbox", { name: "Кол-во" });
  await quantityInput.fill(quantity!.toString());
}
