import { Page } from '@playwright/test'

/**
 * Clicks a checkbox element by its name attribute.
 *
 * This function uses page.evaluate() to execute the click in the browser context,
 * which can be more reliable than using Playwright's click() method for certain
 * checkbox implementations or when dealing with custom styled checkboxes.
 *
 */
export const clickCheckboxByName = async (page: Page, name: string): Promise<void> => {
  await page.evaluate((name) => {
    // Find the checkbox element by its name attribute
    const checkbox = document.querySelector(`input[name="${name}"]`) as HTMLElement
    if (checkbox) {
      // Use native DOM click() method for reliable interaction
      checkbox.click()
    }
  }, name)
}
