import { expect, type Page } from '@playwright/test';

export const E2E_PLUGIN_SOURCE_URL = 'http://localhost:4200/plugins/e2e-plugin-source.json';
export const E2E_PLUGIN_TITLE = 'E2E All API Plugin';

export async function addPluginSource(page: Page, sourceUrl = E2E_PLUGIN_SOURCE_URL): Promise<void> {
  const sourceInput = page.getByLabel('Plugin source manifest URL');

  await expect(sourceInput).toBeVisible({ timeout: 15_000 });
  await sourceInput.click();
  await sourceInput.fill(sourceUrl);
  await expect(sourceInput).toHaveValue(sourceUrl, { timeout: 5_000 });

  const addSourceButton = page.getByRole('button', { name: 'Add Source' });

  await expect(addSourceButton).toBeEnabled({ timeout: 10_000 });
  await addSourceButton.click();
  await expect(page.getByRole('heading', { name: E2E_PLUGIN_TITLE })).toBeVisible({ timeout: 20_000 });
}
