import { Locator, Page } from "@playwright/test";
import getCompletedShoppingItem from "./getCompletedShoppingItem";

export default async function openCompletedQuantityModal(
  page: Page,
  ingredientName: string,
): Promise<Locator> {
  const completedItem = await getCompletedShoppingItem(page, ingredientName);

  const specifyButton = completedItem.getByRole("button", {
    name: /Указать кол-во/,
  });

  if (await specifyButton.isVisible()) {
    await specifyButton.click();
  } else {
    await completedItem.getByTestId("completed-quantity").click();
  }

  const dialog = page.getByRole("dialog", {
    name: /Изменить кол-во|Добавить кол-во/,
  });
  await dialog.waitFor({ state: "visible" });

  return dialog;
}
