// Copyright 2017-2026 @pezkuwi/app-addresses authors & contributors
// SPDX-License-Identifier: Apache-2.0

/// <reference types="@pezkuwi/dev-test/globals.d.ts" />

import type { Table } from '@pezkuwi/test-support/pagesElements';

import { screen } from '@testing-library/react';

import i18next from '@pezkuwi/react-components/i18n';
import { aContactWithBalance } from '@pezkuwi/test-support/creation/contact';
import { MemoryStore } from '@pezkuwi/test-support/keyring';
import { balance } from '@pezkuwi/test-support/utils';
import { keyring } from '@pezkuwi/ui-keyring';

import { AddressesPage } from '../../test/pages/addressesPage.js';

// FIXME isSplit Table
// eslint-disable-next-line jest/no-disabled-tests
describe.skip('Addresses page', () => {
  let addressesPage: AddressesPage;

  beforeAll(async () => {
    await i18next.changeLanguage('en');

    if (keyring.getAccounts().length === 0) {
      keyring.loadAll({ isDevelopment: true, store: new MemoryStore() });
    }
  });

  beforeEach(() => {
    addressesPage = new AddressesPage();
    addressesPage.clearAccounts();
  });

  describe('when no contacts', () => {
    let addressesTable: Table;

    beforeEach(async () => {
      addressesPage.render([]);
      addressesTable = await addressesPage.getTable();
    });

    it('shows a table', () => {
      expect(addressesTable).not.toBeNull();
    });

    it('the contacts table contains no contact rows', async () => {
      expect(await addressesTable.getRows()).toHaveLength(0);
    });

    // eslint-disable-next-line jest/expect-expect
    it('the contacts table contains a message about no contacts available', async () => {
      const noContactsMessage = 'no addresses saved yet, add any existing address';

      await addressesTable.assertText(noContactsMessage);
    });

    it('no summary is displayed', () => {
      const summaries = screen.queryAllByTestId(/card-summary:total \w+/i);

      expect(summaries).toHaveLength(0);
    });
  });

  describe('when some contacts exist', () => {
    it('the contacts table contains some contact rows', async () => {
      addressesPage.renderDefaultContacts(2);

      const rows = await addressesPage.getAddressesRows();

      expect(rows).toHaveLength(2);
    });

    // eslint-disable-next-line jest/expect-expect
    it('contact rows display the total balance info', async () => {
      addressesPage.renderContactsWithDefaultAddresses(
        aContactWithBalance({ freeBalance: balance(500) }),
        aContactWithBalance({ freeBalance: balance(200), reservedBalance: balance(150) })
      );

      const rows = await addressesPage.getAddressesRows();

      await rows[0].assertBalancesTotal(balance(500));
      await rows[1].assertBalancesTotal(balance(350));
    });

    // eslint-disable-next-line jest/expect-expect
    it('contact rows display the details balance info', async () => {
      addressesPage.renderContactsWithDefaultAddresses(
        aContactWithBalance({ freeBalance: balance(500), lockedBalance: balance(30) }),
        aContactWithBalance({ availableBalance: balance(50), freeBalance: balance(200), reservedBalance: balance(150) })
      );

      const rows = await addressesPage.getAddressesRows();

      await rows[0].assertBalancesDetails([
        { amount: balance(0), name: 'transferable' },
        { amount: balance(30), name: 'locked' }]);
      await rows[1].assertBalancesDetails([
        { amount: balance(50), name: 'transferable' },
        { amount: balance(150), name: 'reserved' }]);
    });

    // eslint-disable-next-line jest/expect-expect
    it('when a contact is not tagged, details row displays no tags info', async () => {
      addressesPage.renderDefaultContacts(1);
      const rows = await addressesPage.getAddressesRows();

      await rows[0].assertTags('none');
    });

    it('when a contact is tagged, the details row displays tags', async () => {
      const injectedAddress = '5CMCFVfsauWXmKaUB6tbznVUpBxcUZyU78DzvPTYrhdXe8Xp';

      addressesPage.renderContacts([
        [injectedAddress, { meta: { tags: ['my tag', 'Super Tag'] } }]
      ]);

      const rows = await addressesPage.getAddressesRows();

      expect(rows).toHaveLength(1);

      await rows[0].assertTags('Super Tagmy tag');
    });

    it('contact details rows toggled on icon toggle click', async () => {
      addressesPage.renderDefaultContacts(1);
      const row = (await addressesPage.getAddressesRows())[0];

      expect(row.detailsRow).toHaveClass('isCollapsed');

      await row.expand();

      expect(row.detailsRow).toHaveClass('isExpanded');
    });
  });
});
