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

/**
 * Mark a leftover item as lost by opening context menu and selecting the lost option
 */
export default async function markLeftoverAsLost(
  page: Page,
  ingredientTitle: string,
): Promise<void> {
  // Open the context menu for the ingredient
  const bottomSheet = await openLeftoverContextMenu(page, ingredientTitle);

  // Set up dialog handler before clicking the button
  page.once("dialog", (dialog) => dialog.accept());

  // Click "Отметить, что закончилось" from context menu
  const markAsLostButton = bottomSheet.getByRole("button", {
    name: /Отметить, что закончилось/,
  });
  await markAsLostButton.waitFor({ state: "visible" });
  await markAsLostButton.click();

  // Wait for the bottom sheet to close
  await bottomSheet.waitFor({ state: "hidden", timeout: 5000 });
}
