import { getDepositCreateData } from '@/manager/modules/deposit/deposit-factories';
import { test } from '@/manager/modules/deposit/deposit-fixtures';
import { applyDepositEditTax } from '@/manager/modules/deposit/deposit-mappers';
import { depositEditTaxHappyTestCases } from '@/manager/modules/deposit/test-cases/deposit-edit-tax-happy-test-cases';
import { verifyTaxCard } from '@/manager/modules/ui/tax/tax-assertions';
import { expect } from '@/shared/utils/matchers';

for (const tc of depositEditTaxHappyTestCases) {
  test(tc.description, async ({ createDeposit, depositDetailsPage }) => {
    const deposit = await createDeposit(getDepositCreateData(tc.initialOptions));
    const newDeposit = applyDepositEditTax(deposit, tc.changes);

    await depositDetailsPage.goto(deposit.id);

    await test.step('edit deposit tax', async () => {
      const dialog = await depositDetailsPage.openTaxEditDialog();
      await dialog.edit(tc.changes);
    });

    await test.step('verify new deposit tax on details page', async () => {
      await verifyTaxCard(depositDetailsPage.taxCard, newDeposit.taxes);
    });
  });
}

test('rejects an edit without all required fields', async ({ createDeposit, depositDetailsPage }) => {
  const deposit = await createDeposit(getDepositCreateData());

  await depositDetailsPage.goto(deposit.id);

  const dialog = await test.step('submit edit with cleared required fields', async () => {
    const editDialog = await depositDetailsPage.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);
  });
});
