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

/**
 * Add an ingredient to the shopping list
 */
export default async function addToShoppingList(
  page: Page,
  ingredientName: string,
  searchKey?: string,
): Promise<void> {
  // Click the FloatingActionButton to add an item
  await page.getByRole("button", { name: "add" }).click();

  // Wait for modal to appear and search within it
  const autocomplete = page.getByTestId("IngredientAutocomplete");
  await findIngredient(autocomplete, ingredientName, searchKey);

  // Verify the item was added by checking if it appears in the shopping list
  await page
    .getByText(ingredientName)
    .waitFor({ state: "visible", timeout: 5000 });
}
