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

export async function testRouteForToken(
  page: Page,
  chainId: string,
  tokenId: string,
  amount = "50",
) {
  const _tokenId = tokenId.toLowerCase();
  const _chainId = chainId.toLowerCase();

  await page.goto(urlParams);

  // Open token picker
  await page.getByTestId("token-picker-button").click();

  // Filter tokens by tokenId
  const searchInput = page.getByTestId("token-picker-search-input");
  await expect(searchInput).toBeVisible();
  await searchInput.fill(_tokenId);

  // Click token item
  const tokenItem = page.getByTestId(`token-item-${_tokenId}-${_chainId}`);
  await tokenItem.click();

  // Wait for the cash amount input and fill it
  const amountInput = page.getByTestId("swap-cash-amount-input");
  await expect(amountInput).toBeVisible();
  await amountInput.fill("");
  await amountInput.type(amount);
  await expect(amountInput).toHaveValue(`$${amount}`);

  // Check if route is fetched
  const estimated = page.getByTestId("estimated-amount");
  await expect(estimated).toHaveText(/^(?!0$)\d+(\.\d+)?$/, {
    timeout: 20_000,
  }); // Non-zero estimated amount
}
