import { getProductOneTimeCreateData } from '@/manager/modules/product/product-factories';
import { test } from '@/manager/modules/product/product-fixtures';
import { applyProductOneTimeEditTax } from '@/manager/modules/product/product-mappers';
import { productOneTimeEditTaxHappyTestCases } from '@/manager/modules/product/test-cases/product-one-time-edit-tax-happy-test-cases';
import { verifyTaxCard } from '@/manager/modules/ui/tax/tax-assertions';
import { expect } from '@/shared/utils/matchers';

for (const tc of productOneTimeEditTaxHappyTestCases) {
  test(tc.description, async ({ createProductOneTime, productOneTimeDetailsPage }) => {
    const product = await createProductOneTime(getProductOneTimeCreateData(tc.initialOptions));
    const newProduct = applyProductOneTimeEditTax(product, tc.changes);

    await productOneTimeDetailsPage.goto(product.id);

    await test.step('edit one-time product tax', async () => {
      const dialog = await productOneTimeDetailsPage.openTaxEditDialog();
      await dialog.edit(tc.changes);
    });

    await test.step('verify new one-time product tax on details page', async () => {
      await verifyTaxCard(productOneTimeDetailsPage.taxCard, newProduct.taxes);
    });
  });
}

test('rejects an edit without all required fields', async ({ createProductOneTime, productOneTimeDetailsPage }) => {
  const product = await createProductOneTime(getProductOneTimeCreateData());

  await productOneTimeDetailsPage.goto(product.id);

  const dialog = await test.step('submit edit with cleared required fields', async () => {
    const editDialog = await productOneTimeDetailsPage.openTaxEditDialog();
    await editDialog.b2cTaxAutocomplete.fillWithoutEnter('');
    await editDialog.b2bTaxAutocomplete.fillWithoutEnter('');
    await editDialog.submit();
    return editDialog;
  });

  await test.step('verify errors on form', async () => {
    await expect(dialog.errors).toHaveCountGreaterThan(0);
  });
});
